blob: 1c0a096929c8f5a1fa9defa8ed9627a887c9d04a [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 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070050static void JdwpReadLocation(const uint8_t** pBuf, JdwpLocation* pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070051 memset(pLoc, 0, sizeof(*pLoc)); /* allows memcmp() later */
Elliott Hughes74847412012-06-20 18:10:21 -070052 pLoc->type_tag = ReadTypeTag(pBuf);
53 pLoc->class_id = ReadObjectId(pBuf);
54 pLoc->method_id = 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/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -070059 * Helper function: read a variable-width value from the input buffer.
60 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070061static uint64_t JdwpReadValue(const uint8_t** pBuf, size_t width) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070062 uint64_t value = -1;
63 switch (width) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070064 case 1: value = Read1(pBuf); break;
65 case 2: value = Read2BE(pBuf); break;
66 case 4: value = Read4BE(pBuf); break;
67 case 8: value = Read8BE(pBuf); break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070068 default: LOG(FATAL) << width; break;
69 }
70 return value;
71}
72
73/*
74 * Helper function: write a variable-width value into the output input buffer.
75 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070076static void JdwpWriteValue(ExpandBuf* pReply, int width, uint64_t value) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070077 switch (width) {
78 case 1: expandBufAdd1(pReply, value); break;
79 case 2: expandBufAdd2BE(pReply, value); break;
80 case 4: expandBufAdd4BE(pReply, value); break;
81 case 8: expandBufAdd8BE(pReply, value); break;
82 default: LOG(FATAL) << width; break;
83 }
84}
85
86/*
87 * Common code for *_InvokeMethod requests.
88 *
Elliott Hughes74847412012-06-20 18:10:21 -070089 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -070090 * expected-to-be-void return value of the called function.
91 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070092static JdwpError FinishInvoke(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
Elliott Hughes74847412012-06-20 18:10:21 -070093 ObjectId thread_id, ObjectId object_id,
94 RefTypeId class_id, MethodId method_id, bool is_constructor) {
95 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070096
Elliott Hughes45651fd2012-02-21 15:48:20 -080097 uint32_t arg_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070098
Elliott Hughes74847412012-06-20 18:10:21 -070099 VLOG(jdwp) << StringPrintf(" --> thread_id=%#llx object_id=%#llx", thread_id, object_id);
100 VLOG(jdwp) << StringPrintf(" class_id=%#llx method_id=%x %s.%s", class_id, method_id, Dbg::GetClassName(class_id).c_str(), Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800101 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700102
Elliott Hughes45651fd2012-02-21 15:48:20 -0800103 UniquePtr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
104 UniquePtr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
105 for (uint32_t i = 0; i < arg_count; ++i) {
106 argTypes[i] = ReadTag(&buf);
107 size_t width = Dbg::GetTagWidth(argTypes[i]);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700108 argValues[i] = JdwpReadValue(&buf, width);
Elliott Hughes229feb72012-02-23 13:33:29 -0800109 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#llx", width, argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700110 }
111
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700112 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800113 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 -0700114
Elliott Hughes45651fd2012-02-21 15:48:20 -0800115 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700116 uint64_t resultValue;
117 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700118 JdwpError err = Dbg::InvokeMethod(thread_id, object_id, class_id, method_id, arg_count, argValues.get(), argTypes.get(), options, &resultTag, &resultValue, &exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700119 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800120 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121 }
122
123 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800124 if (is_constructor) {
125 // If we invoked a constructor (which actually returns void), return the receiver,
126 // unless we threw, in which case we return NULL.
127 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700128 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800129 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130
Elliott Hughes45651fd2012-02-21 15:48:20 -0800131 size_t width = Dbg::GetTagWidth(resultTag);
132 expandBufAdd1(pReply, resultTag);
133 if (width != 0) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700134 JdwpWriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700135 }
136 expandBufAdd1(pReply, JT_OBJECT);
137 expandBufAddObjectId(pReply, exceptObjId);
138
Elliott Hughes229feb72012-02-23 13:33:29 -0800139 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" %#llx (except=%#llx)", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140
141 /* show detailed debug output */
142 if (resultTag == JT_STRING && exceptObjId == 0) {
143 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800144 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700145 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800146 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700147 }
148 }
149 }
150
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700151 return err;
152}
153
154
155/*
156 * Request for version info.
157 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700158static JdwpError VM_Version(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700159 /* text information on runtime version */
160 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800161 expandBufAddUtf8String(pReply, version);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700162 /* JDWP version numbers */
163 expandBufAdd4BE(pReply, 1); // major
164 expandBufAdd4BE(pReply, 5); // minor
165 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800166 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700167 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800168 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169
170 return ERR_NONE;
171}
172
173/*
174 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
175 * referenceTypeID. We need to send back more than one if the class has
176 * been loaded by multiple class loaders.
177 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700178static JdwpError VM_ClassesBySignature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800179 std::string classDescriptor(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800180 VLOG(jdwp) << " Req for class by signature '" << classDescriptor << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700181
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800182 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800183 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700184
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800185 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700186
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800187 for (size_t i = 0; i < ids.size(); ++i) {
188 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800189 JDWP::JdwpTypeTag type_tag;
190 uint32_t class_status;
191 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
192 if (status != ERR_NONE) {
193 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800194 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700195
Elliott Hughes436e3722012-02-17 20:01:47 -0800196 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800197 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800198 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199 }
200
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700201 return ERR_NONE;
202}
203
204/*
205 * Handle request for the thread IDs of all running threads.
206 *
207 * We exclude ourselves from the list, because we don't allow ourselves
208 * to be suspended, and that violates some JDWP expectations.
209 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700210static JdwpError VM_AllThreads(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700211 ObjectId* pThreadIds;
212 uint32_t threadCount;
213 Dbg::GetAllThreads(&pThreadIds, &threadCount);
214
215 expandBufAdd4BE(pReply, threadCount);
216
217 ObjectId* walker = pThreadIds;
218 for (uint32_t i = 0; i < threadCount; i++) {
219 expandBufAddObjectId(pReply, *walker++);
220 }
221
222 free(pThreadIds);
223
224 return ERR_NONE;
225}
226
227/*
228 * List all thread groups that do not have a parent.
229 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700230static JdwpError VM_TopLevelThreadGroups(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700231 /*
232 * TODO: maintain a list of parentless thread groups in the VM.
233 *
234 * For now, just return "system". Application threads are created
235 * in "main", which is a child of "system".
236 */
237 uint32_t groups = 1;
238 expandBufAdd4BE(pReply, groups);
239 //threadGroupId = debugGetMainThreadGroup();
240 //expandBufAdd8BE(pReply, threadGroupId);
241 ObjectId threadGroupId = Dbg::GetSystemThreadGroupId();
242 expandBufAddObjectId(pReply, threadGroupId);
243
244 return ERR_NONE;
245}
246
247/*
248 * Respond with the sizes of the basic debugger types.
249 *
250 * All IDs are 8 bytes.
251 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700252static JdwpError VM_IDSizes(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700253 expandBufAdd4BE(pReply, sizeof(FieldId));
254 expandBufAdd4BE(pReply, sizeof(MethodId));
255 expandBufAdd4BE(pReply, sizeof(ObjectId));
256 expandBufAdd4BE(pReply, sizeof(RefTypeId));
257 expandBufAdd4BE(pReply, sizeof(FrameId));
258 return ERR_NONE;
259}
260
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700261static JdwpError VM_Dispose(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes86964332012-02-15 19:37:42 -0800262 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700263 return ERR_NONE;
264}
265
266/*
267 * Suspend the execution of the application running in the VM (i.e. suspend
268 * all threads).
269 *
270 * This needs to increment the "suspend count" on all threads.
271 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700272static JdwpError VM_Suspend(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700273 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700274 return ERR_NONE;
275}
276
277/*
278 * Resume execution. Decrements the "suspend count" of all threads.
279 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700280static JdwpError VM_Resume(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700281 Dbg::ResumeVM();
282 return ERR_NONE;
283}
284
285/*
286 * The debugger wants the entire VM to exit.
287 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700288static JdwpError VM_Exit(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700289 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700290
291 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
292
293 Dbg::Exit(exitCode);
294 return ERR_NOT_IMPLEMENTED; // shouldn't get here
295}
296
297/*
298 * Create a new string in the VM and return its ID.
299 *
300 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
301 * string "java.util.Arrays".)
302 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700303static JdwpError VM_CreateString(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800304 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800305 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700306 ObjectId stringId = Dbg::CreateString(str);
307 if (stringId == 0) {
308 return ERR_OUT_OF_MEMORY;
309 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700310 expandBufAddObjectId(pReply, stringId);
311 return ERR_NONE;
312}
313
314/*
315 * Tell the debugger what we are capable of.
316 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700317static JdwpError VM_Capabilities(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700318 expandBufAdd1(pReply, false); /* canWatchFieldModification */
319 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
320 expandBufAdd1(pReply, false); /* canGetBytecodes */
321 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
322 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
323 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
324 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
325 return ERR_NONE;
326}
327
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700328static JdwpError VM_ClassPaths(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800329 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700330
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800331 std::vector<std::string> class_path;
332 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
333 expandBufAdd4BE(pReply, class_path.size());
334 for (size_t i = 0; i < class_path.size(); ++i) {
335 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700336 }
337
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800338 std::vector<std::string> boot_class_path;
339 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
340 expandBufAdd4BE(pReply, boot_class_path.size());
341 for (size_t i = 0; i < boot_class_path.size(); ++i) {
342 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700343 }
344
345 return ERR_NONE;
346}
347
348/*
349 * Release a list of object IDs. (Seen in jdb.)
350 *
351 * Currently does nothing.
352 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700353static JdwpError VM_DisposeObjects(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700354 return ERR_NONE;
355}
356
357/*
358 * Tell the debugger what we are capable of.
359 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700360static JdwpError VM_CapabilitiesNew(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700361 expandBufAdd1(pReply, false); /* canWatchFieldModification */
362 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
363 expandBufAdd1(pReply, false); /* canGetBytecodes */
364 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
365 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
366 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
367 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
368 expandBufAdd1(pReply, false); /* canRedefineClasses */
369 expandBufAdd1(pReply, false); /* canAddMethod */
370 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
371 expandBufAdd1(pReply, false); /* canPopFrames */
372 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
373 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
374 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
375 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
376 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
377 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
378 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
379 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
380 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
381 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
382
383 /* fill in reserved22 through reserved32; note count started at 1 */
384 for (int i = 22; i <= 32; i++) {
385 expandBufAdd1(pReply, false); /* reservedN */
386 }
387 return ERR_NONE;
388}
389
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700390static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800391 std::vector<JDWP::RefTypeId> classes;
392 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700393
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800394 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700395
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800396 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800397 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800398 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800399 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800400 uint32_t class_status;
401 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
402 if (status != ERR_NONE) {
403 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800404 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700405
Elliott Hughes436e3722012-02-17 20:01:47 -0800406 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800407 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800408 if (descriptor_and_status) {
409 expandBufAddUtf8String(pReply, descriptor);
410 if (generic) {
411 expandBufAddUtf8String(pReply, genericSignature);
412 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800413 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800414 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700415 }
416
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700417 return ERR_NONE;
418}
419
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700420static JdwpError VM_AllClasses(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
421 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800422}
423
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700424static JdwpError VM_AllClassesWithGeneric(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
425 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800426}
427
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700428static JdwpError RT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700429 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800430 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700431}
432
433/*
434 * Get values from static fields in a reference type.
435 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700436static JdwpError RT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800437 RefTypeId refTypeId = ReadRefTypeId(&buf);
438 uint32_t field_count = Read4BE(&buf);
439 expandBufAdd4BE(pReply, field_count);
440 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800442 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800443 if (status != ERR_NONE) {
444 return status;
445 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700446 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700447 return ERR_NONE;
448}
449
450/*
451 * Get the name of the source file in which a reference type was declared.
452 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700453static JdwpError RT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800455 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800456 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
457 if (status != ERR_NONE) {
458 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700459 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800460 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800461 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700462}
463
464/*
465 * Return the current status of the reference type.
466 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700467static JdwpError RT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700468 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800469 JDWP::JdwpTypeTag type_tag;
470 uint32_t class_status;
471 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
472 if (status != ERR_NONE) {
473 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800474 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800475 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700476 return ERR_NONE;
477}
478
479/*
480 * Return interfaces implemented directly by this class.
481 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700482static JdwpError RT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700483 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800484 VLOG(jdwp) << StringPrintf(" Req for interfaces in %#llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800485 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700486}
487
488/*
489 * Return the class object corresponding to this type.
490 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700491static JdwpError RT_ClassObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700492 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800493 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800494 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
495 if (status != ERR_NONE) {
496 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800497 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800498 VLOG(jdwp) << StringPrintf(" RefTypeId %#llx -> ObjectId %#llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800499 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500 return ERR_NONE;
501}
502
503/*
504 * Returns the value of the SourceDebugExtension attribute.
505 *
506 * JDB seems interested, but DEX files don't currently support this.
507 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700508static JdwpError RT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700509 /* referenceTypeId in, string out */
510 return ERR_ABSENT_INFORMATION;
511}
512
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700513static JdwpError RT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply, bool with_generic) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700514 RefTypeId refTypeId = ReadRefTypeId(&buf);
515
Elliott Hughes229feb72012-02-23 13:33:29 -0800516 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=%#llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800517 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800518
519 JdwpError status = Dbg::GetSignature(refTypeId, signature);
520 if (status != ERR_NONE) {
521 return status;
522 }
523 expandBufAddUtf8String(pReply, signature);
524 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800525 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700526 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700527 return ERR_NONE;
528}
529
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700530static JdwpError RT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
531 return RT_Signature(state, buf, dataLen, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800532}
533
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700534static JdwpError RT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
535 return RT_Signature(state, buf, dataLen, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800536}
537
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700538/*
539 * Return the instance of java.lang.ClassLoader that loaded the specified
540 * reference type, or null if it was loaded by the system loader.
541 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700542static JdwpError RT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700543 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800544 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700545}
546
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800547static std::string Describe(const RefTypeId& refTypeId) {
548 std::string signature("unknown");
549 Dbg::GetSignature(refTypeId, signature);
Elliott Hughes229feb72012-02-23 13:33:29 -0800550 return StringPrintf("refTypeId=%#llx (%s)", refTypeId, signature.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800551}
552
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700553/*
554 * Given a referenceTypeId, return a block of stuff that describes the
555 * fields declared by a class.
556 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700557static JdwpError RT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700558 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800559 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800560 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800561}
562
563// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700564static JdwpError RT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800565 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800566 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800567 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700568}
569
570/*
571 * Given a referenceTypeID, return a block of goodies describing the
572 * methods declared by a class.
573 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700574static JdwpError RT_MethodsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700575 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800576 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800577 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800578}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700579
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800580// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700581static JdwpError RT_Methods(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800582 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800583 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800584 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700585}
586
587/*
588 * Return the immediate superclass of a class.
589 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700590static JdwpError CT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700591 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800592 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700593 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800594 if (status != ERR_NONE) {
595 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800596 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700597 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700598 return ERR_NONE;
599}
600
601/*
602 * Set static class values.
603 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700604static JdwpError CT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700605 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700606 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700607
Elliott Hughes74847412012-06-20 18:10:21 -0700608 VLOG(jdwp) << StringPrintf(" Req to set %d values in class_id=%#llx", values, class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700609
610 for (uint32_t i = 0; i < values; i++) {
611 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800612 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800613 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700614 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700615
Elliott Hughes2435a572012-02-17 16:07:41 -0800616 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800617 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
618 if (status != ERR_NONE) {
619 return status;
620 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700621 }
622
623 return ERR_NONE;
624}
625
626/*
627 * Invoke a static method.
628 *
629 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
630 * values in the "variables" display.
631 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700632static JdwpError CT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700633 RefTypeId class_id = ReadRefTypeId(&buf);
634 ObjectId thread_id = ReadObjectId(&buf);
635 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700636
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700637 return FinishInvoke(state, buf, dataLen, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700638}
639
640/*
641 * Create a new object of the requested type, and invoke the specified
642 * constructor.
643 *
644 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
645 * see the contents of a byte[] as a string.
646 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700647static JdwpError CT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700648 RefTypeId class_id = ReadRefTypeId(&buf);
649 ObjectId thread_id = ReadObjectId(&buf);
650 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700651
Elliott Hughes74847412012-06-20 18:10:21 -0700652 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(class_id);
653 ObjectId object_id;
654 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800655 if (status != ERR_NONE) {
656 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800657 }
Elliott Hughes74847412012-06-20 18:10:21 -0700658 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700659 return ERR_OUT_OF_MEMORY;
660 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700661 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700662}
663
664/*
665 * Create a new array object of the requested type and length.
666 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700667static JdwpError AT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700668 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700669 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700670
Elliott Hughes2435a572012-02-17 16:07:41 -0800671 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes74847412012-06-20 18:10:21 -0700672 ObjectId object_id;
673 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800674 if (status != ERR_NONE) {
675 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800676 }
Elliott Hughes74847412012-06-20 18:10:21 -0700677 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700678 return ERR_OUT_OF_MEMORY;
679 }
680 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700681 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700682 return ERR_NONE;
683}
684
685/*
686 * Return line number information for the method, if present.
687 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700688static JdwpError M_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700689 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700690 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700691
Elliott Hughes74847412012-06-20 18:10:21 -0700692 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId, method_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700693
Elliott Hughes74847412012-06-20 18:10:21 -0700694 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700695
696 return ERR_NONE;
697}
698
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700699static JdwpError M_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply, bool generic) {
Elliott Hughes74847412012-06-20 18:10:21 -0700700 RefTypeId class_id = ReadRefTypeId(&buf);
701 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700702
Elliott Hughes74847412012-06-20 18:10:21 -0700703 VLOG(jdwp) << StringPrintf(" Req for LocalVarTab in class=%s method=%s", Dbg::GetClassName(class_id).c_str(), Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700704
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800705 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
706 // information. That will cause Eclipse to make a best-effort attempt at displaying local
707 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
708 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700709 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700710 return ERR_NONE;
711}
712
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700713static JdwpError M_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
714 return M_VariableTable(state, buf, dataLen, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800715}
716
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700717static JdwpError M_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
718 return M_VariableTable(state, buf, dataLen, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800719}
720
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700721/*
722 * Given an object reference, return the runtime type of the object
723 * (class or array).
724 *
Elliott Hughes74847412012-06-20 18:10:21 -0700725 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700726 * passed in here.
727 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700728static JdwpError OR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700729 ObjectId object_id = ReadObjectId(&buf);
730 VLOG(jdwp) << StringPrintf(" Req for type of object_id=%#llx", object_id);
731 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700732}
733
734/*
735 * Get values from the fields of an object.
736 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700737static JdwpError OR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700738 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800739 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700740
Elliott Hughes74847412012-06-20 18:10:21 -0700741 VLOG(jdwp) << StringPrintf(" Req for %d fields from object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742
Elliott Hughes0cf74332012-02-23 23:14:00 -0800743 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700744
Elliott Hughes0cf74332012-02-23 23:14:00 -0800745 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700746 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700747 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800748 if (status != ERR_NONE) {
749 return status;
750 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700751 }
752
753 return ERR_NONE;
754}
755
756/*
757 * Set values in the fields of an object.
758 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700759static JdwpError OR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700760 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800761 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700762
Elliott Hughes74847412012-06-20 18:10:21 -0700763 VLOG(jdwp) << StringPrintf(" Req to set %d fields in object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700764
Elliott Hughes0cf74332012-02-23 23:14:00 -0800765 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700766 FieldId fieldId = ReadFieldId(&buf);
767
Elliott Hughesaed4be92011-12-02 16:16:23 -0800768 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800769 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700770 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700771
Elliott Hughes2435a572012-02-17 16:07:41 -0800772 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700773 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800774 if (status != ERR_NONE) {
775 return status;
776 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700777 }
778
779 return ERR_NONE;
780}
781
782/*
783 * Invoke an instance method. The invocation must occur in the specified
784 * thread, which must have been suspended by an event.
785 *
786 * The call is synchronous. All threads in the VM are resumed, unless the
787 * SINGLE_THREADED flag is set.
788 *
789 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
790 * object), it will try to invoke the object's toString() function. This
791 * feature becomes crucial when examining ArrayLists with Eclipse.
792 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700793static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700794 ObjectId object_id = ReadObjectId(&buf);
795 ObjectId thread_id = ReadObjectId(&buf);
796 RefTypeId class_id = ReadRefTypeId(&buf);
797 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700798
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700799 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700800}
801
802/*
803 * Disable garbage collection of the specified object.
804 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700805static JdwpError OR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700806 // this is currently a no-op
807 return ERR_NONE;
808}
809
810/*
811 * Enable garbage collection of the specified object.
812 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700813static JdwpError OR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700814 // this is currently a no-op
815 return ERR_NONE;
816}
817
818/*
819 * Determine whether an object has been garbage collected.
820 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700821static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700822 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700823
Elliott Hughes74847412012-06-20 18:10:21 -0700824 object_id = ReadObjectId(&buf);
825 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700826
827 // TODO: currently returning false; must integrate with GC
828 expandBufAdd1(pReply, 0);
829
830 return ERR_NONE;
831}
832
833/*
834 * Return the string value in a string object.
835 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700836static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700837 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800838 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700839
Elliott Hughes82914b62012-04-09 15:56:29 -0700840 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700841
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800842 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700843
844 return ERR_NONE;
845}
846
847/*
848 * Return a thread's name.
849 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700850static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700851 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700852
Elliott Hughes74847412012-06-20 18:10:21 -0700853 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800854 std::string name;
Elliott Hughes74847412012-06-20 18:10:21 -0700855 if (!Dbg::GetThreadName(thread_id, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700856 return ERR_INVALID_THREAD;
857 }
Elliott Hughes74847412012-06-20 18:10:21 -0700858 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800859 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700860
861 return ERR_NONE;
862}
863
864/*
865 * Suspend the specified thread.
866 *
867 * It's supposed to remain suspended even if interpreted code wants to
868 * resume it; only the JDI is allowed to resume it.
869 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700870static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700871 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700872
Elliott Hughes74847412012-06-20 18:10:21 -0700873 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700874 LOG(INFO) << " Warning: ignoring request to suspend self";
875 return ERR_THREAD_NOT_SUSPENDED;
876 }
Elliott Hughes74847412012-06-20 18:10:21 -0700877 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
878 Dbg::SuspendThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700879 return ERR_NONE;
880}
881
882/*
883 * Resume the specified thread.
884 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700885static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700886 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700887
Elliott Hughes74847412012-06-20 18:10:21 -0700888 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700889 LOG(INFO) << " Warning: ignoring request to resume self";
890 return ERR_NONE;
891 }
Elliott Hughes74847412012-06-20 18:10:21 -0700892 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
893 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700894 return ERR_NONE;
895}
896
897/*
898 * Return status of specified thread.
899 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700900static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700901 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700902
Elliott Hughes74847412012-06-20 18:10:21 -0700903 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700904
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800905 JDWP::JdwpThreadStatus threadStatus;
906 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes74847412012-06-20 18:10:21 -0700907 if (!Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700908 return ERR_INVALID_THREAD;
909 }
910
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800911 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700912
913 expandBufAdd4BE(pReply, threadStatus);
914 expandBufAdd4BE(pReply, suspendStatus);
915
916 return ERR_NONE;
917}
918
919/*
920 * Return the thread group that the specified thread is a member of.
921 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700922static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700923 ObjectId thread_id = ReadObjectId(&buf);
924 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700925}
926
927/*
928 * Return the current call stack of a suspended thread.
929 *
930 * If the thread isn't suspended, the error code isn't defined, but should
931 * be THREAD_NOT_SUSPENDED.
932 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700933static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700934 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800935 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700936 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700937
Elliott Hughes74847412012-06-20 18:10:21 -0700938 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700939 return ERR_INVALID_THREAD;
940 }
Elliott Hughes74847412012-06-20 18:10:21 -0700941 if (!Dbg::IsSuspended(thread_id)) {
942 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700943 return ERR_THREAD_NOT_SUSPENDED;
944 }
945
Elliott Hughes74847412012-06-20 18:10:21 -0700946 size_t actual_frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700947
Elliott Hughes74847412012-06-20 18:10:21 -0700948 VLOG(jdwp) << StringPrintf(" Request for frames: thread_id=%#llx start=%d length=%d [count=%zd]", thread_id, start_frame, length, actual_frame_count);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800949 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700950 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
951 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700952
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800953 if (start_frame > actual_frame_count) {
954 return ERR_INVALID_INDEX;
955 }
956 if (length == static_cast<uint32_t>(-1)) {
957 length = actual_frame_count - start_frame;
958 }
959 if (start_frame + length > actual_frame_count) {
960 return ERR_INVALID_LENGTH;
961 }
962
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700963 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700964}
965
966/*
967 * Returns the #of frames on the specified thread, which must be suspended.
968 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700969static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700970 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700971
Elliott Hughes74847412012-06-20 18:10:21 -0700972 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700973 return ERR_INVALID_THREAD;
974 }
Elliott Hughes74847412012-06-20 18:10:21 -0700975 if (!Dbg::IsSuspended(thread_id)) {
976 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700977 return ERR_THREAD_NOT_SUSPENDED;
978 }
979
Elliott Hughes74847412012-06-20 18:10:21 -0700980 int frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800981 if (frame_count < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700982 return ERR_INVALID_THREAD;
983 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800984 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700985
986 return ERR_NONE;
987}
988
989/*
990 * Get the monitor that the thread is waiting on.
991 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700992static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700993 ReadObjectId(&buf); // thread_id
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700994
995 // TODO: create an Object to represent the monitor (we're currently
996 // just using a raw Monitor struct in the VM)
997
998 return ERR_NOT_IMPLEMENTED;
999}
1000
1001/*
1002 * Return the suspend count for the specified thread.
1003 *
1004 * (The thread *might* still be running -- it might not have examined
1005 * its suspend count recently.)
1006 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001007static JdwpError TR_SuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -07001008 ObjectId thread_id = ReadObjectId(&buf);
1009 return Dbg::GetThreadSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001010}
1011
1012/*
1013 * Return the name of a thread group.
1014 *
1015 * The Eclipse debugger recognizes "main" and "system" as special.
1016 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001017static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001018 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001019 VLOG(jdwp) << StringPrintf(" Req for name of threadGroupId=%#llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001020
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001021 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(threadGroupId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001022
1023 return ERR_NONE;
1024}
1025
1026/*
1027 * Returns the thread group -- if any -- that contains the specified
1028 * thread group.
1029 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001030static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031 ObjectId groupId = ReadObjectId(&buf);
1032
1033 ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId);
1034 expandBufAddObjectId(pReply, parentGroup);
1035
1036 return ERR_NONE;
1037}
1038
1039/*
1040 * Return the active threads and thread groups that are part of the
1041 * specified thread group.
1042 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001043static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001044 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001045 VLOG(jdwp) << StringPrintf(" Req for threads in threadGroupId=%#llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001046
1047 ObjectId* pThreadIds;
1048 uint32_t threadCount;
1049 Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount);
1050
1051 expandBufAdd4BE(pReply, threadCount);
1052
1053 for (uint32_t i = 0; i < threadCount; i++) {
1054 expandBufAddObjectId(pReply, pThreadIds[i]);
1055 }
1056 free(pThreadIds);
1057
1058 /*
1059 * TODO: finish support for child groups
1060 *
1061 * For now, just show that "main" is a child of "system".
1062 */
1063 if (threadGroupId == Dbg::GetSystemThreadGroupId()) {
1064 expandBufAdd4BE(pReply, 1);
1065 expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId());
1066 } else {
1067 expandBufAdd4BE(pReply, 0);
1068 }
1069
1070 return ERR_NONE;
1071}
1072
1073/*
1074 * Return the #of components in the array.
1075 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001076static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001077 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001078 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001079
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001080 int length;
1081 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1082 if (status != ERR_NONE) {
1083 return status;
1084 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001085 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001086
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001087 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001088
1089 return ERR_NONE;
1090}
1091
1092/*
1093 * Return the values from an array.
1094 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001095static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001096 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001097 uint32_t firstIndex = Read4BE(&buf);
1098 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001099 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001100
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001101 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001102}
1103
1104/*
1105 * Set values in an array.
1106 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001107static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001108 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001109 uint32_t firstIndex = Read4BE(&buf);
1110 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001111
Elliott Hughes229feb72012-02-23 13:33:29 -08001112 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId, firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001113
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001114 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001115}
1116
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001117static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001118 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001119 // TODO: we should only return classes which have the given class loader as a defining or
1120 // initiating loader. The former would be easy; the latter is hard, because we don't have
1121 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001122 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001123}
1124
1125/*
1126 * Set an event trigger.
1127 *
1128 * Reply with a requestID.
1129 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001130static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001131 const uint8_t* origBuf = buf;
1132
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001133 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001134 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001135 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001136
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001137 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001138 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001139 << " mods=" << modifierCount << ")";
1140
1141 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1142
1143 JdwpEvent* pEvent = EventAlloc(modifierCount);
1144 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001145 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001146 pEvent->modCount = modifierCount;
1147
1148 /*
1149 * Read modifiers. Ordering may be significant (see explanation of Count
1150 * mods in JDWP doc).
1151 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001152 for (uint32_t i = 0; i < modifierCount; ++i) {
1153 JdwpEventMod& mod = pEvent->mods[i];
1154 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1155 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001156 case MK_COUNT: /* report once, when "--count" reaches 0 */
1157 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001158 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001159 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001160 if (count == 0) {
1161 return ERR_INVALID_COUNT;
1162 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001163 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001164 }
1165 break;
1166 case MK_CONDITIONAL: /* conditional on expression) */
1167 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001168 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001169 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001170 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001171 }
1172 break;
1173 case MK_THREAD_ONLY: /* only report events in specified thread */
1174 {
Elliott Hughes74847412012-06-20 18:10:21 -07001175 ObjectId thread_id = ReadObjectId(&buf);
1176 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1177 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001178 }
1179 break;
1180 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1181 {
Elliott Hughes74847412012-06-20 18:10:21 -07001182 RefTypeId class_id = ReadRefTypeId(&buf);
1183 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1184 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001185 }
1186 break;
1187 case MK_CLASS_MATCH: /* restrict events to matching classes */
1188 {
Elliott Hughes86964332012-02-15 19:37:42 -08001189 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001190 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001191 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001192 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001193 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001194 }
1195 break;
1196 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1197 {
Elliott Hughes86964332012-02-15 19:37:42 -08001198 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001199 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001200 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001201 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001202 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001203 }
1204 break;
1205 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1206 {
1207 JdwpLocation loc;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001208 JdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001209 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001210 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001211 }
1212 break;
1213 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1214 {
1215 RefTypeId exceptionOrNull; /* null == all exceptions */
1216 uint8_t caught, uncaught;
1217
1218 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001219 caught = Read1(&buf);
1220 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001221 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001222 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001223
Elliott Hughes972a47b2012-02-21 18:16:06 -08001224 mod.exceptionOnly.refTypeId = exceptionOrNull;
1225 mod.exceptionOnly.caught = caught;
1226 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001227 }
1228 break;
1229 case MK_FIELD_ONLY: /* for field access/mod events */
1230 {
1231 RefTypeId declaring = ReadRefTypeId(&buf);
1232 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001233 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001234 mod.fieldOnly.refTypeId = declaring;
1235 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001236 }
1237 break;
1238 case MK_STEP: /* for use with EK_SINGLE_STEP */
1239 {
Elliott Hughes74847412012-06-20 18:10:21 -07001240 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001241 uint32_t size, depth;
1242
Elliott Hughes74847412012-06-20 18:10:21 -07001243 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001244 size = Read4BE(&buf);
1245 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001246 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001247 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1248
Elliott Hughes74847412012-06-20 18:10:21 -07001249 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001250 mod.step.size = size;
1251 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001252 }
1253 break;
1254 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1255 {
1256 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001257 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001258 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001259 }
1260 break;
1261 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001262 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001263 break;
1264 }
1265 }
1266
1267 /*
1268 * Make sure we consumed all data. It is possible that the remote side
1269 * has sent us bad stuff, but for now we blame ourselves.
1270 */
1271 if (buf != origBuf + dataLen) {
1272 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1273 }
1274
1275 /*
1276 * We reply with an integer "requestID".
1277 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001278 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001279 expandBufAdd4BE(pReply, requestId);
1280
1281 pEvent->requestId = requestId;
1282
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001283 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001284
1285 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001286 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001287 if (err != ERR_NONE) {
1288 /* registration failed, probably because event is bogus */
1289 EventFree(pEvent);
1290 LOG(WARNING) << "WARNING: event request rejected";
1291 }
1292 return err;
1293}
1294
1295/*
1296 * Clear an event. Failure to find an event with a matching ID is a no-op
1297 * and does not return an error.
1298 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001299static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001300 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001301 eventKind = Read1(&buf);
1302 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001303
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001304 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001305
Elliott Hughes761928d2011-11-16 18:33:03 -08001306 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001307
1308 return ERR_NONE;
1309}
1310
1311/*
1312 * Return the values of arguments and local variables.
1313 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001314static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -07001315 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001316 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001317 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001318
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001319 VLOG(jdwp) << StringPrintf(" Req for %d slots in thread_id=%#llx frame_id=%lld", slots, thread_id, frame_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001320
1321 expandBufAdd4BE(pReply, slots); /* "int values" */
1322 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001323 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001324 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001325
Elliott Hughes2435a572012-02-17 16:07:41 -08001326 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001327
Elliott Hughesdbb40792011-11-18 17:05:22 -08001328 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001329 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001330 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001331 }
1332
1333 return ERR_NONE;
1334}
1335
1336/*
1337 * Set the values of arguments and local variables.
1338 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001339static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -07001340 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001341 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001342 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001343
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001344 VLOG(jdwp) << StringPrintf(" Req to set %d slots in thread_id=%#llx frame_id=%lld", slots, thread_id, frame_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001345
1346 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001347 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001348 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001349 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001350 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001351
Elliott Hughes2435a572012-02-17 16:07:41 -08001352 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001353 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001354 }
1355
1356 return ERR_NONE;
1357}
1358
1359/*
1360 * Returns the value of "this" for the specified frame.
1361 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001362static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
1363 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001364 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001365
Elliott Hughes546b9862012-06-20 16:06:13 -07001366 ObjectId id;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001367 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &id);
1368 if (rc != ERR_NONE) {
1369 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001370 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001371
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001372 uint8_t tag;
1373 rc = Dbg::GetObjectTag(id, tag);
1374 if (rc != ERR_NONE) {
1375 return rc;
1376 }
1377
1378 VLOG(jdwp) << StringPrintf(" Req for 'this' in thread_id=%#llx frame=%lld --> %#llx '%c'", thread_id, frame_id, id, static_cast<char>(tag));
Elliott Hughes546b9862012-06-20 16:06:13 -07001379 expandBufAdd1(pReply, tag);
1380 expandBufAddObjectId(pReply, id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001381
1382 return ERR_NONE;
1383}
1384
1385/*
1386 * Return the reference type reflected by this class object.
1387 *
1388 * This appears to be required because ReferenceTypeId values are NEVER
1389 * reused, whereas ClassIds can be recycled like any other object. (Either
1390 * that, or I have no idea what this is for.)
1391 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001392static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001393 RefTypeId classObjectId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001394 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId, Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001395 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001396}
1397
1398/*
1399 * Handle a DDM packet with a single chunk in it.
1400 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001401static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001402 uint8_t* replyBuf = NULL;
1403 int replyLen = -1;
1404
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001405 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001406
Elliott Hughesa21039c2012-06-21 12:09:25 -07001407 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001408
1409 /*
1410 * If they want to send something back, we copy it into the buffer.
1411 * A no-copy approach would be nicer.
1412 *
1413 * TODO: consider altering the JDWP stuff to hold the packet header
1414 * in a separate buffer. That would allow us to writev() DDM traffic
1415 * instead of copying it into the expanding buffer. The reduction in
1416 * heap requirements is probably more valuable than the efficiency.
1417 */
1418 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1419 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1420 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1421 free(replyBuf);
1422 }
1423 return ERR_NONE;
1424}
1425
1426/*
1427 * Handler map decl.
1428 */
1429typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1430
1431struct JdwpHandlerMap {
1432 uint8_t cmdSet;
1433 uint8_t cmd;
1434 JdwpRequestHandler func;
1435 const char* descr;
1436};
1437
1438/*
1439 * Map commands to functions.
1440 *
1441 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1442 * and 128-256 are vendor-defined.
1443 */
1444static const JdwpHandlerMap gHandlerMap[] = {
1445 /* VirtualMachine command set (1) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001446 { 1, 1, VM_Version, "VirtualMachine.Version" },
1447 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1448 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1449 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1450 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1451 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1452 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1453 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1454 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1455 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1456 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1457 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1458 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1459 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001460 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1461 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001462 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001463 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1464 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001465 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001466 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001467
1468 /* ReferenceType command set (2) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001469 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1470 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1471 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1472 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1473 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1474 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1475 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001476 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001477 { 2, 9, RT_Status, "ReferenceType.Status" },
1478 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1479 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
1480 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1481 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1482 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1483 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001484 { 2, 16, NULL, "ReferenceType.Instances" },
1485 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1486 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001487
1488 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001489 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1490 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1491 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1492 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001493
1494 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001495 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001496
1497 /* InterfaceType command set (5) */
1498
1499 /* Method command set (6) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001500 { 6, 1, M_LineTable, "Method.LineTable" },
1501 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001502 { 6, 3, NULL, "Method.Bytecodes" },
1503 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001504 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001505
1506 /* Field command set (8) */
1507
1508 /* ObjectReference command set (9) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001509 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1510 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1511 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001512 { 9, 4, NULL, "ObjectReference.UNUSED" },
1513 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001514 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
1515 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
1516 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1517 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001518 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001519
1520 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001521 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001522
1523 /* ThreadReference command set (11) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001524 { 11, 1, TR_Name, "ThreadReference.Name" },
1525 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1526 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1527 { 11, 4, TR_Status, "ThreadReference.Status" },
1528 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1529 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1530 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001531 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001532 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001533 { 11, 10, NULL, "ThreadReference.Stop" },
Elliott Hughes74847412012-06-20 18:10:21 -07001534 { 11, 11, NULL, "ThreadReference.Interrupt" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001535 { 11, 12, TR_SuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001536 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1537 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001538
1539 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001540 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1541 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1542 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001543
1544 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001545 { 13, 1, AR_Length, "ArrayReference.Length" },
1546 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1547 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001548
1549 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001550 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001551
1552 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001553 { 15, 1, ER_Set, "EventRequest.Set" },
1554 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001555 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001556
1557 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001558 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1559 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1560 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001561 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001562
1563 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001564 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001565
1566 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001567 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001568
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001569 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001570};
1571
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001572static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001573 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001574 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1575 return gHandlerMap[i].descr;
1576 }
1577 }
1578 return "?UNKNOWN?";
1579}
1580
1581static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1582 std::string result;
1583 result += "REQ: ";
1584 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1585 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1586 return result;
1587}
1588
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001589/*
1590 * Process a request from the debugger.
1591 *
1592 * On entry, the JDWP thread is in VMWAIT.
1593 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001594void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001595 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001596
1597 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1598 /*
1599 * Activity from a debugger, not merely ddms. Mark us as having an
1600 * active debugger session, and zero out the last-activity timestamp
1601 * so waitForDebugger() doesn't return if we stall for a bit here.
1602 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001603 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001604 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001605 }
1606
1607 /*
1608 * If a debugger event has fired in another thread, wait until the
1609 * initiating thread has suspended itself before processing messages
1610 * from the debugger. Otherwise we (the JDWP thread) could be told to
1611 * resume the thread before it has suspended.
1612 *
1613 * We call with an argument of zero to wait for the current event
1614 * thread to finish, and then clear the block. Depending on the thread
1615 * suspend policy, this may allow events in other threads to fire,
1616 * but those events have no bearing on what the debugger has sent us
1617 * in the current request.
1618 *
1619 * Note that we MUST clear the event token before waking the event
1620 * thread up, or risk waiting for the thread to suspend after we've
1621 * told it to resume.
1622 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001623 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001624
1625 /*
1626 * Tell the VM that we're running and shouldn't be interrupted by GC.
1627 * Do this after anything that can stall indefinitely.
1628 */
1629 Dbg::ThreadRunning();
1630
1631 expandBufAddSpace(pReply, kJDWPHeaderLen);
1632
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001633 size_t i;
1634 for (i = 0; i < arraysize(gHandlerMap); i++) {
1635 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001636 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001637 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001638 break;
1639 }
1640 }
1641 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001642 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1643 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001644 result = ERR_NOT_IMPLEMENTED;
1645 }
1646
1647 /*
1648 * Set up the reply header.
1649 *
1650 * If we encountered an error, only send the header back.
1651 */
1652 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001653 Set4BE(replyBuf + 4, pHeader->id);
1654 Set1(replyBuf + 8, kJDWPFlagReply);
1655 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001656 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001657 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001658 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001659 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001660 }
1661
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001662 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001663 if (false) {
1664 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001665 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001666 }
1667
1668 /*
1669 * Update last-activity timestamp. We really only need this during
1670 * the initial setup. Only update if this is a non-DDMS packet.
1671 */
1672 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001673 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001674 }
1675
1676 /* tell the VM that GC is okay again */
1677 Dbg::ThreadWaiting();
1678}
1679
1680} // namespace JDWP
1681
1682} // namespace art