blob: 7a796feff6b0ecff23a78e8b51ab0504e0ae28d5 [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 Hughescaf76542012-06-28 16:08:22 -0700211 std::vector<ObjectId> thread_ids;
212 Dbg::GetThreads(NULL, thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700213
Elliott Hughescaf76542012-06-28 16:08:22 -0700214 expandBufAdd4BE(pReply, thread_ids.size());
215 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
216 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700217 }
218
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700219 return ERR_NONE;
220}
221
222/*
223 * List all thread groups that do not have a parent.
224 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700225static JdwpError VM_TopLevelThreadGroups(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226 /*
227 * TODO: maintain a list of parentless thread groups in the VM.
228 *
229 * For now, just return "system". Application threads are created
230 * in "main", which is a child of "system".
231 */
232 uint32_t groups = 1;
233 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700234 //thread_group_id = debugGetMainThreadGroup();
235 //expandBufAdd8BE(pReply, thread_group_id);
236 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
237 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700238
239 return ERR_NONE;
240}
241
242/*
243 * Respond with the sizes of the basic debugger types.
244 *
245 * All IDs are 8 bytes.
246 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700247static JdwpError VM_IDSizes(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700248 expandBufAdd4BE(pReply, sizeof(FieldId));
249 expandBufAdd4BE(pReply, sizeof(MethodId));
250 expandBufAdd4BE(pReply, sizeof(ObjectId));
251 expandBufAdd4BE(pReply, sizeof(RefTypeId));
252 expandBufAdd4BE(pReply, sizeof(FrameId));
253 return ERR_NONE;
254}
255
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700256static JdwpError VM_Dispose(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes86964332012-02-15 19:37:42 -0800257 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700258 return ERR_NONE;
259}
260
261/*
262 * Suspend the execution of the application running in the VM (i.e. suspend
263 * all threads).
264 *
265 * This needs to increment the "suspend count" on all threads.
266 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700267static JdwpError VM_Suspend(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700268 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700269 return ERR_NONE;
270}
271
272/*
273 * Resume execution. Decrements the "suspend count" of all threads.
274 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700275static JdwpError VM_Resume(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700276 Dbg::ResumeVM();
277 return ERR_NONE;
278}
279
280/*
281 * The debugger wants the entire VM to exit.
282 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700283static JdwpError VM_Exit(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700284 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700285
286 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
287
288 Dbg::Exit(exitCode);
289 return ERR_NOT_IMPLEMENTED; // shouldn't get here
290}
291
292/*
293 * Create a new string in the VM and return its ID.
294 *
295 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
296 * string "java.util.Arrays".)
297 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700298static JdwpError VM_CreateString(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800299 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800300 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700301 ObjectId stringId = Dbg::CreateString(str);
302 if (stringId == 0) {
303 return ERR_OUT_OF_MEMORY;
304 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700305 expandBufAddObjectId(pReply, stringId);
306 return ERR_NONE;
307}
308
309/*
310 * Tell the debugger what we are capable of.
311 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700312static JdwpError VM_Capabilities(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700313 expandBufAdd1(pReply, false); /* canWatchFieldModification */
314 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
315 expandBufAdd1(pReply, false); /* canGetBytecodes */
316 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
317 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
318 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
319 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
320 return ERR_NONE;
321}
322
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700323static JdwpError VM_ClassPaths(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800324 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700325
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800326 std::vector<std::string> class_path;
327 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
328 expandBufAdd4BE(pReply, class_path.size());
329 for (size_t i = 0; i < class_path.size(); ++i) {
330 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700331 }
332
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800333 std::vector<std::string> boot_class_path;
334 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
335 expandBufAdd4BE(pReply, boot_class_path.size());
336 for (size_t i = 0; i < boot_class_path.size(); ++i) {
337 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700338 }
339
340 return ERR_NONE;
341}
342
343/*
344 * Release a list of object IDs. (Seen in jdb.)
345 *
346 * Currently does nothing.
347 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700348static JdwpError VM_DisposeObjects(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700349 return ERR_NONE;
350}
351
352/*
353 * Tell the debugger what we are capable of.
354 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700355static JdwpError VM_CapabilitiesNew(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700356 expandBufAdd1(pReply, false); /* canWatchFieldModification */
357 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
358 expandBufAdd1(pReply, false); /* canGetBytecodes */
359 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
360 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
361 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
362 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
363 expandBufAdd1(pReply, false); /* canRedefineClasses */
364 expandBufAdd1(pReply, false); /* canAddMethod */
365 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
366 expandBufAdd1(pReply, false); /* canPopFrames */
367 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
368 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
369 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
370 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
371 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
372 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
373 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
374 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
375 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
376 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
377
378 /* fill in reserved22 through reserved32; note count started at 1 */
379 for (int i = 22; i <= 32; i++) {
380 expandBufAdd1(pReply, false); /* reservedN */
381 }
382 return ERR_NONE;
383}
384
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700385static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800386 std::vector<JDWP::RefTypeId> classes;
387 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700388
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800389 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700390
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800391 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800392 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800393 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800394 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800395 uint32_t class_status;
396 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
397 if (status != ERR_NONE) {
398 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800399 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700400
Elliott Hughes436e3722012-02-17 20:01:47 -0800401 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800402 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800403 if (descriptor_and_status) {
404 expandBufAddUtf8String(pReply, descriptor);
405 if (generic) {
406 expandBufAddUtf8String(pReply, genericSignature);
407 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800408 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800409 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700410 }
411
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412 return ERR_NONE;
413}
414
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700415static JdwpError VM_AllClasses(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
416 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800417}
418
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700419static JdwpError VM_AllClassesWithGeneric(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
420 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800421}
422
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700423static JdwpError RT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800425 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700426}
427
428/*
429 * Get values from static fields in a reference type.
430 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700431static JdwpError RT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800432 RefTypeId refTypeId = ReadRefTypeId(&buf);
433 uint32_t field_count = Read4BE(&buf);
434 expandBufAdd4BE(pReply, field_count);
435 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700436 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800437 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800438 if (status != ERR_NONE) {
439 return status;
440 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700442 return ERR_NONE;
443}
444
445/*
446 * Get the name of the source file in which a reference type was declared.
447 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700448static JdwpError RT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700449 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800450 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800451 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
452 if (status != ERR_NONE) {
453 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800455 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800456 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700457}
458
459/*
460 * Return the current status of the reference type.
461 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700462static JdwpError RT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700463 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800464 JDWP::JdwpTypeTag type_tag;
465 uint32_t class_status;
466 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
467 if (status != ERR_NONE) {
468 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800469 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800470 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700471 return ERR_NONE;
472}
473
474/*
475 * Return interfaces implemented directly by this class.
476 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700477static JdwpError RT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700478 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800479 VLOG(jdwp) << StringPrintf(" Req for interfaces in %#llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800480 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700481}
482
483/*
484 * Return the class object corresponding to this type.
485 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700486static JdwpError RT_ClassObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700487 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800488 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800489 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
490 if (status != ERR_NONE) {
491 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800492 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800493 VLOG(jdwp) << StringPrintf(" RefTypeId %#llx -> ObjectId %#llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800494 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700495 return ERR_NONE;
496}
497
498/*
499 * Returns the value of the SourceDebugExtension attribute.
500 *
501 * JDB seems interested, but DEX files don't currently support this.
502 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700503static JdwpError RT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700504 /* referenceTypeId in, string out */
505 return ERR_ABSENT_INFORMATION;
506}
507
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700508static JdwpError RT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply, bool with_generic) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700509 RefTypeId refTypeId = ReadRefTypeId(&buf);
510
Elliott Hughes229feb72012-02-23 13:33:29 -0800511 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=%#llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800512 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800513
514 JdwpError status = Dbg::GetSignature(refTypeId, signature);
515 if (status != ERR_NONE) {
516 return status;
517 }
518 expandBufAddUtf8String(pReply, signature);
519 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800520 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700521 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700522 return ERR_NONE;
523}
524
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700525static JdwpError RT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
526 return RT_Signature(state, buf, dataLen, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800527}
528
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700529static JdwpError RT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
530 return RT_Signature(state, buf, dataLen, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800531}
532
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700533/*
534 * Return the instance of java.lang.ClassLoader that loaded the specified
535 * reference type, or null if it was loaded by the system loader.
536 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700537static JdwpError RT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700538 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800539 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700540}
541
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800542static std::string Describe(const RefTypeId& refTypeId) {
543 std::string signature("unknown");
544 Dbg::GetSignature(refTypeId, signature);
Elliott Hughes229feb72012-02-23 13:33:29 -0800545 return StringPrintf("refTypeId=%#llx (%s)", refTypeId, signature.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800546}
547
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700548/*
549 * Given a referenceTypeId, return a block of stuff that describes the
550 * fields declared by a class.
551 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700552static JdwpError RT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700553 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800554 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800555 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800556}
557
558// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700559static JdwpError RT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800560 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800561 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800562 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700563}
564
565/*
566 * Given a referenceTypeID, return a block of goodies describing the
567 * methods declared by a class.
568 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700569static JdwpError RT_MethodsWithGeneric(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 methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800572 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800573}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700574
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800575// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700576static JdwpError RT_Methods(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 methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800579 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700580}
581
582/*
583 * Return the immediate superclass of a class.
584 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700585static JdwpError CT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700586 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800587 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700588 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800589 if (status != ERR_NONE) {
590 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800591 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700592 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700593 return ERR_NONE;
594}
595
596/*
597 * Set static class values.
598 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700599static JdwpError CT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700600 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700601 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700602
Elliott Hughes74847412012-06-20 18:10:21 -0700603 VLOG(jdwp) << StringPrintf(" Req to set %d values in class_id=%#llx", values, class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700604
605 for (uint32_t i = 0; i < values; i++) {
606 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800607 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800608 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700609 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700610
Elliott Hughes2435a572012-02-17 16:07:41 -0800611 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800612 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
613 if (status != ERR_NONE) {
614 return status;
615 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700616 }
617
618 return ERR_NONE;
619}
620
621/*
622 * Invoke a static method.
623 *
624 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
625 * values in the "variables" display.
626 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700627static JdwpError CT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700628 RefTypeId class_id = ReadRefTypeId(&buf);
629 ObjectId thread_id = ReadObjectId(&buf);
630 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700631
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700632 return FinishInvoke(state, buf, dataLen, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700633}
634
635/*
636 * Create a new object of the requested type, and invoke the specified
637 * constructor.
638 *
639 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
640 * see the contents of a byte[] as a string.
641 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700642static JdwpError CT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700643 RefTypeId class_id = ReadRefTypeId(&buf);
644 ObjectId thread_id = ReadObjectId(&buf);
645 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700646
Elliott Hughes74847412012-06-20 18:10:21 -0700647 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(class_id);
648 ObjectId object_id;
649 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800650 if (status != ERR_NONE) {
651 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800652 }
Elliott Hughes74847412012-06-20 18:10:21 -0700653 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700654 return ERR_OUT_OF_MEMORY;
655 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700656 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700657}
658
659/*
660 * Create a new array object of the requested type and length.
661 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700662static JdwpError AT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700663 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700664 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700665
Elliott Hughes2435a572012-02-17 16:07:41 -0800666 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes74847412012-06-20 18:10:21 -0700667 ObjectId object_id;
668 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800669 if (status != ERR_NONE) {
670 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800671 }
Elliott Hughes74847412012-06-20 18:10:21 -0700672 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700673 return ERR_OUT_OF_MEMORY;
674 }
675 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700676 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700677 return ERR_NONE;
678}
679
680/*
681 * Return line number information for the method, if present.
682 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700683static JdwpError M_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700684 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700685 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700686
Elliott Hughes74847412012-06-20 18:10:21 -0700687 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId, method_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700688
Elliott Hughes74847412012-06-20 18:10:21 -0700689 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700690
691 return ERR_NONE;
692}
693
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700694static JdwpError M_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply, bool generic) {
Elliott Hughes74847412012-06-20 18:10:21 -0700695 RefTypeId class_id = ReadRefTypeId(&buf);
696 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700697
Elliott Hughes74847412012-06-20 18:10:21 -0700698 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 -0700699
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800700 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
701 // information. That will cause Eclipse to make a best-effort attempt at displaying local
702 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
703 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700704 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700705 return ERR_NONE;
706}
707
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700708static JdwpError M_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
709 return M_VariableTable(state, buf, dataLen, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800710}
711
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700712static JdwpError M_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
713 return M_VariableTable(state, buf, dataLen, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800714}
715
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700716/*
717 * Given an object reference, return the runtime type of the object
718 * (class or array).
719 *
Elliott Hughes74847412012-06-20 18:10:21 -0700720 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700721 * passed in here.
722 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700723static JdwpError OR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700724 ObjectId object_id = ReadObjectId(&buf);
725 VLOG(jdwp) << StringPrintf(" Req for type of object_id=%#llx", object_id);
726 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700727}
728
729/*
730 * Get values from the fields of an object.
731 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700732static JdwpError OR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700733 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800734 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700735
Elliott Hughes74847412012-06-20 18:10:21 -0700736 VLOG(jdwp) << StringPrintf(" Req for %d fields from object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700737
Elliott Hughes0cf74332012-02-23 23:14:00 -0800738 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700739
Elliott Hughes0cf74332012-02-23 23:14:00 -0800740 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700741 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700742 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800743 if (status != ERR_NONE) {
744 return status;
745 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700746 }
747
748 return ERR_NONE;
749}
750
751/*
752 * Set values in the fields of an object.
753 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700754static JdwpError OR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700755 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800756 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700757
Elliott Hughes74847412012-06-20 18:10:21 -0700758 VLOG(jdwp) << StringPrintf(" Req to set %d fields in object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700759
Elliott Hughes0cf74332012-02-23 23:14:00 -0800760 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700761 FieldId fieldId = ReadFieldId(&buf);
762
Elliott Hughesaed4be92011-12-02 16:16:23 -0800763 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800764 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700765 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700766
Elliott Hughes2435a572012-02-17 16:07:41 -0800767 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700768 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800769 if (status != ERR_NONE) {
770 return status;
771 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700772 }
773
774 return ERR_NONE;
775}
776
777/*
778 * Invoke an instance method. The invocation must occur in the specified
779 * thread, which must have been suspended by an event.
780 *
781 * The call is synchronous. All threads in the VM are resumed, unless the
782 * SINGLE_THREADED flag is set.
783 *
784 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
785 * object), it will try to invoke the object's toString() function. This
786 * feature becomes crucial when examining ArrayLists with Eclipse.
787 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700788static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700789 ObjectId object_id = ReadObjectId(&buf);
790 ObjectId thread_id = ReadObjectId(&buf);
791 RefTypeId class_id = ReadRefTypeId(&buf);
792 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700793
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700794 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700795}
796
797/*
798 * Disable garbage collection of the specified object.
799 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700800static JdwpError OR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801 // this is currently a no-op
802 return ERR_NONE;
803}
804
805/*
806 * Enable garbage collection of the specified object.
807 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700808static JdwpError OR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700809 // this is currently a no-op
810 return ERR_NONE;
811}
812
813/*
814 * Determine whether an object has been garbage collected.
815 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700816static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700817 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700818
Elliott Hughes74847412012-06-20 18:10:21 -0700819 object_id = ReadObjectId(&buf);
820 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700821
822 // TODO: currently returning false; must integrate with GC
823 expandBufAdd1(pReply, 0);
824
825 return ERR_NONE;
826}
827
828/*
829 * Return the string value in a string object.
830 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700831static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700832 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800833 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700834
Elliott Hughes82914b62012-04-09 15:56:29 -0700835 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700836
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800837 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700838
839 return ERR_NONE;
840}
841
842/*
843 * Return a thread's name.
844 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700845static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700846 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700847
Elliott Hughes74847412012-06-20 18:10:21 -0700848 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800849 std::string name;
Elliott Hughes74847412012-06-20 18:10:21 -0700850 if (!Dbg::GetThreadName(thread_id, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700851 return ERR_INVALID_THREAD;
852 }
Elliott Hughes74847412012-06-20 18:10:21 -0700853 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800854 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700855
856 return ERR_NONE;
857}
858
859/*
860 * Suspend the specified thread.
861 *
862 * It's supposed to remain suspended even if interpreted code wants to
863 * resume it; only the JDI is allowed to resume it.
864 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700865static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700866 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700867
Elliott Hughes74847412012-06-20 18:10:21 -0700868 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700869 LOG(INFO) << " Warning: ignoring request to suspend self";
870 return ERR_THREAD_NOT_SUSPENDED;
871 }
Elliott Hughes74847412012-06-20 18:10:21 -0700872 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
873 Dbg::SuspendThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700874 return ERR_NONE;
875}
876
877/*
878 * Resume the specified thread.
879 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700880static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700881 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700882
Elliott Hughes74847412012-06-20 18:10:21 -0700883 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700884 LOG(INFO) << " Warning: ignoring request to resume self";
885 return ERR_NONE;
886 }
Elliott Hughes74847412012-06-20 18:10:21 -0700887 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
888 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700889 return ERR_NONE;
890}
891
892/*
893 * Return status of specified thread.
894 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700895static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700896 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700897
Elliott Hughes74847412012-06-20 18:10:21 -0700898 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700899
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800900 JDWP::JdwpThreadStatus threadStatus;
901 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes74847412012-06-20 18:10:21 -0700902 if (!Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700903 return ERR_INVALID_THREAD;
904 }
905
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800906 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700907
908 expandBufAdd4BE(pReply, threadStatus);
909 expandBufAdd4BE(pReply, suspendStatus);
910
911 return ERR_NONE;
912}
913
914/*
915 * Return the thread group that the specified thread is a member of.
916 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700917static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700918 ObjectId thread_id = ReadObjectId(&buf);
919 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700920}
921
922/*
923 * Return the current call stack of a suspended thread.
924 *
925 * If the thread isn't suspended, the error code isn't defined, but should
926 * be THREAD_NOT_SUSPENDED.
927 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700928static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700929 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800930 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700931 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700932
Elliott Hughes74847412012-06-20 18:10:21 -0700933 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700934 return ERR_INVALID_THREAD;
935 }
Elliott Hughes74847412012-06-20 18:10:21 -0700936 if (!Dbg::IsSuspended(thread_id)) {
937 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700938 return ERR_THREAD_NOT_SUSPENDED;
939 }
940
Elliott Hughes74847412012-06-20 18:10:21 -0700941 size_t actual_frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700942
Elliott Hughes74847412012-06-20 18:10:21 -0700943 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 -0800944 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700945 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
946 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700947
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800948 if (start_frame > actual_frame_count) {
949 return ERR_INVALID_INDEX;
950 }
951 if (length == static_cast<uint32_t>(-1)) {
952 length = actual_frame_count - start_frame;
953 }
954 if (start_frame + length > actual_frame_count) {
955 return ERR_INVALID_LENGTH;
956 }
957
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700958 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700959}
960
961/*
962 * Returns the #of frames on the specified thread, which must be suspended.
963 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700964static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700965 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700966
Elliott Hughes74847412012-06-20 18:10:21 -0700967 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700968 return ERR_INVALID_THREAD;
969 }
Elliott Hughes74847412012-06-20 18:10:21 -0700970 if (!Dbg::IsSuspended(thread_id)) {
971 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700972 return ERR_THREAD_NOT_SUSPENDED;
973 }
974
Elliott Hughes74847412012-06-20 18:10:21 -0700975 int frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800976 if (frame_count < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700977 return ERR_INVALID_THREAD;
978 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800979 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700980
981 return ERR_NONE;
982}
983
984/*
985 * Get the monitor that the thread is waiting on.
986 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700987static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700988 ReadObjectId(&buf); // thread_id
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700989
990 // TODO: create an Object to represent the monitor (we're currently
991 // just using a raw Monitor struct in the VM)
992
993 return ERR_NOT_IMPLEMENTED;
994}
995
996/*
997 * Return the suspend count for the specified thread.
998 *
999 * (The thread *might* still be running -- it might not have examined
1000 * its suspend count recently.)
1001 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001002static JdwpError TR_SuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -07001003 ObjectId thread_id = ReadObjectId(&buf);
1004 return Dbg::GetThreadSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001005}
1006
1007/*
1008 * Return the name of a thread group.
1009 *
1010 * The Eclipse debugger recognizes "main" and "system" as special.
1011 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001012static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001013 ObjectId thread_group_id = ReadObjectId(&buf);
1014 VLOG(jdwp) << StringPrintf(" Req for name of thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001015
Elliott Hughescaf76542012-06-28 16:08:22 -07001016 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001017
1018 return ERR_NONE;
1019}
1020
1021/*
1022 * Returns the thread group -- if any -- that contains the specified
1023 * thread group.
1024 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001025static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001026 ObjectId thread_group_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001027
Elliott Hughescaf76542012-06-28 16:08:22 -07001028 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001029 expandBufAddObjectId(pReply, parentGroup);
1030
1031 return ERR_NONE;
1032}
1033
1034/*
1035 * Return the active threads and thread groups that are part of the
1036 * specified thread group.
1037 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001038static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001039 ObjectId thread_group_id = ReadObjectId(&buf);
1040 VLOG(jdwp) << StringPrintf(" Req for threads in thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001041
Elliott Hughescaf76542012-06-28 16:08:22 -07001042 std::vector<ObjectId> thread_ids;
1043 Dbg::GetThreads(thread_group_id, thread_ids);
1044 expandBufAdd4BE(pReply, thread_ids.size());
1045 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1046 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001047 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001048
Elliott Hughescaf76542012-06-28 16:08:22 -07001049 std::vector<ObjectId> child_thread_groups_ids;
1050 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1051 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1052 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1053 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001054 }
1055
1056 return ERR_NONE;
1057}
1058
1059/*
1060 * Return the #of components in the array.
1061 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001062static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001063 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001064 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001065
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001066 int length;
1067 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1068 if (status != ERR_NONE) {
1069 return status;
1070 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001071 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001072
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001073 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001074
1075 return ERR_NONE;
1076}
1077
1078/*
1079 * Return the values from an array.
1080 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001081static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001082 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001083 uint32_t firstIndex = Read4BE(&buf);
1084 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001085 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001086
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001087 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001088}
1089
1090/*
1091 * Set values in an array.
1092 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001093static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001094 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001095 uint32_t firstIndex = Read4BE(&buf);
1096 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001097
Elliott Hughes229feb72012-02-23 13:33:29 -08001098 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId, firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001099
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001100 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001101}
1102
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001103static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001104 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001105 // TODO: we should only return classes which have the given class loader as a defining or
1106 // initiating loader. The former would be easy; the latter is hard, because we don't have
1107 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001108 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001109}
1110
1111/*
1112 * Set an event trigger.
1113 *
1114 * Reply with a requestID.
1115 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001116static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001117 const uint8_t* origBuf = buf;
1118
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001119 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001120 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001121 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001122
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001123 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001124 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001125 << " mods=" << modifierCount << ")";
1126
1127 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1128
1129 JdwpEvent* pEvent = EventAlloc(modifierCount);
1130 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001131 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001132 pEvent->modCount = modifierCount;
1133
1134 /*
1135 * Read modifiers. Ordering may be significant (see explanation of Count
1136 * mods in JDWP doc).
1137 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001138 for (uint32_t i = 0; i < modifierCount; ++i) {
1139 JdwpEventMod& mod = pEvent->mods[i];
1140 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1141 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001142 case MK_COUNT: /* report once, when "--count" reaches 0 */
1143 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001144 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001145 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001146 if (count == 0) {
1147 return ERR_INVALID_COUNT;
1148 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001149 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001150 }
1151 break;
1152 case MK_CONDITIONAL: /* conditional on expression) */
1153 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001154 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001155 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001156 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001157 }
1158 break;
1159 case MK_THREAD_ONLY: /* only report events in specified thread */
1160 {
Elliott Hughes74847412012-06-20 18:10:21 -07001161 ObjectId thread_id = ReadObjectId(&buf);
1162 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1163 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001164 }
1165 break;
1166 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1167 {
Elliott Hughes74847412012-06-20 18:10:21 -07001168 RefTypeId class_id = ReadRefTypeId(&buf);
1169 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1170 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001171 }
1172 break;
1173 case MK_CLASS_MATCH: /* restrict events to matching classes */
1174 {
Elliott Hughes86964332012-02-15 19:37:42 -08001175 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001176 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001177 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001178 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001179 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001180 }
1181 break;
1182 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1183 {
Elliott Hughes86964332012-02-15 19:37:42 -08001184 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001185 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001186 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001187 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001188 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001189 }
1190 break;
1191 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1192 {
1193 JdwpLocation loc;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001194 JdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001195 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001196 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001197 }
1198 break;
1199 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1200 {
1201 RefTypeId exceptionOrNull; /* null == all exceptions */
1202 uint8_t caught, uncaught;
1203
1204 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001205 caught = Read1(&buf);
1206 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001207 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001208 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001209
Elliott Hughes972a47b2012-02-21 18:16:06 -08001210 mod.exceptionOnly.refTypeId = exceptionOrNull;
1211 mod.exceptionOnly.caught = caught;
1212 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001213 }
1214 break;
1215 case MK_FIELD_ONLY: /* for field access/mod events */
1216 {
1217 RefTypeId declaring = ReadRefTypeId(&buf);
1218 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001219 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001220 mod.fieldOnly.refTypeId = declaring;
1221 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001222 }
1223 break;
1224 case MK_STEP: /* for use with EK_SINGLE_STEP */
1225 {
Elliott Hughes74847412012-06-20 18:10:21 -07001226 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001227 uint32_t size, depth;
1228
Elliott Hughes74847412012-06-20 18:10:21 -07001229 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001230 size = Read4BE(&buf);
1231 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001232 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001233 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1234
Elliott Hughes74847412012-06-20 18:10:21 -07001235 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001236 mod.step.size = size;
1237 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001238 }
1239 break;
1240 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1241 {
1242 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001243 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001244 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001245 }
1246 break;
1247 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001248 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001249 break;
1250 }
1251 }
1252
1253 /*
1254 * Make sure we consumed all data. It is possible that the remote side
1255 * has sent us bad stuff, but for now we blame ourselves.
1256 */
1257 if (buf != origBuf + dataLen) {
1258 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1259 }
1260
1261 /*
1262 * We reply with an integer "requestID".
1263 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001264 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001265 expandBufAdd4BE(pReply, requestId);
1266
1267 pEvent->requestId = requestId;
1268
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001269 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001270
1271 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001272 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001273 if (err != ERR_NONE) {
1274 /* registration failed, probably because event is bogus */
1275 EventFree(pEvent);
1276 LOG(WARNING) << "WARNING: event request rejected";
1277 }
1278 return err;
1279}
1280
1281/*
1282 * Clear an event. Failure to find an event with a matching ID is a no-op
1283 * and does not return an error.
1284 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001285static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001286 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001287 eventKind = Read1(&buf);
1288 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001289
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001290 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001291
Elliott Hughes761928d2011-11-16 18:33:03 -08001292 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001293
1294 return ERR_NONE;
1295}
1296
1297/*
1298 * Return the values of arguments and local variables.
1299 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001300static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -07001301 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001302 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001303 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001304
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001305 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 -07001306
1307 expandBufAdd4BE(pReply, slots); /* "int values" */
1308 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001309 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001310 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001311
Elliott Hughes2435a572012-02-17 16:07:41 -08001312 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001313
Elliott Hughesdbb40792011-11-18 17:05:22 -08001314 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001315 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001316 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001317 }
1318
1319 return ERR_NONE;
1320}
1321
1322/*
1323 * Set the values of arguments and local variables.
1324 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001325static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -07001326 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001327 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001328 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001329
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001330 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 -07001331
1332 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001333 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001334 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001335 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001336 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001337
Elliott Hughes2435a572012-02-17 16:07:41 -08001338 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001339 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001340 }
1341
1342 return ERR_NONE;
1343}
1344
1345/*
1346 * Returns the value of "this" for the specified frame.
1347 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001348static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
1349 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001350 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001351
Elliott Hughes546b9862012-06-20 16:06:13 -07001352 ObjectId id;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001353 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &id);
1354 if (rc != ERR_NONE) {
1355 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001356 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001357
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001358 uint8_t tag;
1359 rc = Dbg::GetObjectTag(id, tag);
1360 if (rc != ERR_NONE) {
1361 return rc;
1362 }
1363
1364 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 -07001365 expandBufAdd1(pReply, tag);
1366 expandBufAddObjectId(pReply, id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001367
1368 return ERR_NONE;
1369}
1370
1371/*
1372 * Return the reference type reflected by this class object.
1373 *
1374 * This appears to be required because ReferenceTypeId values are NEVER
1375 * reused, whereas ClassIds can be recycled like any other object. (Either
1376 * that, or I have no idea what this is for.)
1377 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001378static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001379 RefTypeId classObjectId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001380 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId, Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001381 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001382}
1383
1384/*
1385 * Handle a DDM packet with a single chunk in it.
1386 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001387static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001388 uint8_t* replyBuf = NULL;
1389 int replyLen = -1;
1390
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001391 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001392
Elliott Hughesa21039c2012-06-21 12:09:25 -07001393 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001394
1395 /*
1396 * If they want to send something back, we copy it into the buffer.
1397 * A no-copy approach would be nicer.
1398 *
1399 * TODO: consider altering the JDWP stuff to hold the packet header
1400 * in a separate buffer. That would allow us to writev() DDM traffic
1401 * instead of copying it into the expanding buffer. The reduction in
1402 * heap requirements is probably more valuable than the efficiency.
1403 */
1404 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1405 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1406 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1407 free(replyBuf);
1408 }
1409 return ERR_NONE;
1410}
1411
1412/*
1413 * Handler map decl.
1414 */
1415typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1416
1417struct JdwpHandlerMap {
1418 uint8_t cmdSet;
1419 uint8_t cmd;
1420 JdwpRequestHandler func;
1421 const char* descr;
1422};
1423
1424/*
1425 * Map commands to functions.
1426 *
1427 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1428 * and 128-256 are vendor-defined.
1429 */
1430static const JdwpHandlerMap gHandlerMap[] = {
1431 /* VirtualMachine command set (1) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001432 { 1, 1, VM_Version, "VirtualMachine.Version" },
1433 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1434 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1435 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1436 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1437 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1438 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1439 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1440 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1441 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1442 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1443 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1444 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1445 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001446 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1447 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001448 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001449 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1450 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001451 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001452 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001453
1454 /* ReferenceType command set (2) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001455 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1456 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1457 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1458 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1459 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1460 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1461 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001462 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001463 { 2, 9, RT_Status, "ReferenceType.Status" },
1464 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1465 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
1466 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1467 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1468 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1469 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001470 { 2, 16, NULL, "ReferenceType.Instances" },
1471 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1472 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001473
1474 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001475 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1476 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1477 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1478 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001479
1480 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001481 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001482
1483 /* InterfaceType command set (5) */
1484
1485 /* Method command set (6) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001486 { 6, 1, M_LineTable, "Method.LineTable" },
1487 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001488 { 6, 3, NULL, "Method.Bytecodes" },
1489 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001490 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001491
1492 /* Field command set (8) */
1493
1494 /* ObjectReference command set (9) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001495 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1496 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1497 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001498 { 9, 4, NULL, "ObjectReference.UNUSED" },
1499 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001500 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
1501 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
1502 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1503 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001504 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001505
1506 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001507 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001508
1509 /* ThreadReference command set (11) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001510 { 11, 1, TR_Name, "ThreadReference.Name" },
1511 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1512 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1513 { 11, 4, TR_Status, "ThreadReference.Status" },
1514 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1515 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1516 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001517 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001518 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001519 { 11, 10, NULL, "ThreadReference.Stop" },
Elliott Hughes74847412012-06-20 18:10:21 -07001520 { 11, 11, NULL, "ThreadReference.Interrupt" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001521 { 11, 12, TR_SuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001522 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1523 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001524
1525 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001526 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1527 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1528 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001529
1530 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001531 { 13, 1, AR_Length, "ArrayReference.Length" },
1532 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1533 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001534
1535 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001536 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001537
1538 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001539 { 15, 1, ER_Set, "EventRequest.Set" },
1540 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001541 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001542
1543 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001544 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1545 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1546 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001547 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001548
1549 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001550 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001551
1552 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001553 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001554
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001555 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001556};
1557
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001558static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001559 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001560 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1561 return gHandlerMap[i].descr;
1562 }
1563 }
1564 return "?UNKNOWN?";
1565}
1566
1567static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1568 std::string result;
1569 result += "REQ: ";
1570 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1571 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1572 return result;
1573}
1574
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001575/*
1576 * Process a request from the debugger.
1577 *
1578 * On entry, the JDWP thread is in VMWAIT.
1579 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001580void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001581 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001582
1583 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1584 /*
1585 * Activity from a debugger, not merely ddms. Mark us as having an
1586 * active debugger session, and zero out the last-activity timestamp
1587 * so waitForDebugger() doesn't return if we stall for a bit here.
1588 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001589 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001590 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001591 }
1592
1593 /*
1594 * If a debugger event has fired in another thread, wait until the
1595 * initiating thread has suspended itself before processing messages
1596 * from the debugger. Otherwise we (the JDWP thread) could be told to
1597 * resume the thread before it has suspended.
1598 *
1599 * We call with an argument of zero to wait for the current event
1600 * thread to finish, and then clear the block. Depending on the thread
1601 * suspend policy, this may allow events in other threads to fire,
1602 * but those events have no bearing on what the debugger has sent us
1603 * in the current request.
1604 *
1605 * Note that we MUST clear the event token before waking the event
1606 * thread up, or risk waiting for the thread to suspend after we've
1607 * told it to resume.
1608 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001609 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001610
1611 /*
1612 * Tell the VM that we're running and shouldn't be interrupted by GC.
1613 * Do this after anything that can stall indefinitely.
1614 */
1615 Dbg::ThreadRunning();
1616
1617 expandBufAddSpace(pReply, kJDWPHeaderLen);
1618
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001619 size_t i;
1620 for (i = 0; i < arraysize(gHandlerMap); i++) {
1621 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001622 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001623 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001624 break;
1625 }
1626 }
1627 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001628 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1629 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001630 result = ERR_NOT_IMPLEMENTED;
1631 }
1632
1633 /*
1634 * Set up the reply header.
1635 *
1636 * If we encountered an error, only send the header back.
1637 */
1638 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001639 Set4BE(replyBuf + 4, pHeader->id);
1640 Set1(replyBuf + 8, kJDWPFlagReply);
1641 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001642 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001643 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001644 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001645 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001646 }
1647
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001648 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001649 if (false) {
1650 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001651 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001652 }
1653
1654 /*
1655 * Update last-activity timestamp. We really only need this during
1656 * the initial setup. Only update if this is a non-DDMS packet.
1657 */
1658 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001659 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001660 }
1661
1662 /* tell the VM that GC is okay again */
1663 Dbg::ThreadWaiting();
1664}
1665
1666} // namespace JDWP
1667
1668} // namespace art