blob: 95d4c095db51c47a81a050171299a8e4ffbd5b7b [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Handle messages from debugger.
19 *
20 * GENERAL NOTE: we're not currently testing the message length for
21 * correctness. This is usually a bad idea, but here we can probably
22 * get away with it so long as the debugger isn't broken. We can
23 * change the "read" macros to use "dataLen" to avoid wandering into
24 * bad territory, and have a single "is dataLen correct" check at the
25 * end of each function. Not needed at this time.
26 */
27
28#include "atomic.h"
29#include "debugger.h"
30#include "jdwp/jdwp_priv.h"
31#include "jdwp/jdwp_handler.h"
32#include "jdwp/jdwp_event.h"
33#include "jdwp/jdwp_constants.h"
34#include "jdwp/jdwp_expand_buf.h"
35#include "logging.h"
36#include "macros.h"
37#include "stringprintf.h"
38
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43namespace art {
44
45namespace JDWP {
46
47/*
48 * Helper function: read a "location" from an input buffer.
49 */
50static void jdwpReadLocation(const uint8_t** pBuf, JdwpLocation* pLoc) {
51 memset(pLoc, 0, sizeof(*pLoc)); /* allows memcmp() later */
Elliott 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/*
59 * Helper function: write a "location" into the reply buffer.
60 */
61void AddLocation(ExpandBuf* pReply, const JdwpLocation* pLoc) {
Elliott Hughes74847412012-06-20 18:10:21 -070062 expandBufAdd1(pReply, pLoc->type_tag);
63 expandBufAddObjectId(pReply, pLoc->class_id);
64 expandBufAddMethodId(pReply, pLoc->method_id);
Elliott Hughes972a47b2012-02-21 18:16:06 -080065 expandBufAdd8BE(pReply, pLoc->dex_pc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070066}
67
68/*
69 * Helper function: read a variable-width value from the input buffer.
70 */
Elliott Hughesdbb40792011-11-18 17:05:22 -080071static uint64_t jdwpReadValue(const uint8_t** pBuf, size_t width) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070072 uint64_t value = -1;
73 switch (width) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070074 case 1: value = Read1(pBuf); break;
75 case 2: value = Read2BE(pBuf); break;
76 case 4: value = Read4BE(pBuf); break;
77 case 8: value = Read8BE(pBuf); break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070078 default: LOG(FATAL) << width; break;
79 }
80 return value;
81}
82
83/*
84 * Helper function: write a variable-width value into the output input buffer.
85 */
86static void jdwpWriteValue(ExpandBuf* pReply, int width, uint64_t value) {
87 switch (width) {
88 case 1: expandBufAdd1(pReply, value); break;
89 case 2: expandBufAdd2BE(pReply, value); break;
90 case 4: expandBufAdd4BE(pReply, value); break;
91 case 8: expandBufAdd8BE(pReply, value); break;
92 default: LOG(FATAL) << width; break;
93 }
94}
95
96/*
97 * Common code for *_InvokeMethod requests.
98 *
Elliott Hughes74847412012-06-20 18:10:21 -070099 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700100 * expected-to-be-void return value of the called function.
101 */
Elliott Hughes74847412012-06-20 18:10:21 -0700102static JdwpError finishInvoke(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
103 ObjectId thread_id, ObjectId object_id,
104 RefTypeId class_id, MethodId method_id, bool is_constructor) {
105 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700106
Elliott Hughes45651fd2012-02-21 15:48:20 -0800107 uint32_t arg_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108
Elliott Hughes74847412012-06-20 18:10:21 -0700109 VLOG(jdwp) << StringPrintf(" --> thread_id=%#llx object_id=%#llx", thread_id, object_id);
110 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 -0800111 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700112
Elliott Hughes45651fd2012-02-21 15:48:20 -0800113 UniquePtr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
114 UniquePtr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
115 for (uint32_t i = 0; i < arg_count; ++i) {
116 argTypes[i] = ReadTag(&buf);
117 size_t width = Dbg::GetTagWidth(argTypes[i]);
118 argValues[i] = jdwpReadValue(&buf, width);
Elliott Hughes229feb72012-02-23 13:33:29 -0800119 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#llx", width, argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700120 }
121
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700122 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800123 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 -0700124
Elliott Hughes45651fd2012-02-21 15:48:20 -0800125 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 uint64_t resultValue;
127 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700128 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 -0700129 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800130 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131 }
132
133 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800134 if (is_constructor) {
135 // If we invoked a constructor (which actually returns void), return the receiver,
136 // unless we threw, in which case we return NULL.
137 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700138 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800139 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140
Elliott Hughes45651fd2012-02-21 15:48:20 -0800141 size_t width = Dbg::GetTagWidth(resultTag);
142 expandBufAdd1(pReply, resultTag);
143 if (width != 0) {
144 jdwpWriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700145 }
146 expandBufAdd1(pReply, JT_OBJECT);
147 expandBufAddObjectId(pReply, exceptObjId);
148
Elliott Hughes229feb72012-02-23 13:33:29 -0800149 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" %#llx (except=%#llx)", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700150
151 /* show detailed debug output */
152 if (resultTag == JT_STRING && exceptObjId == 0) {
153 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800154 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700155 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800156 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700157 }
158 }
159 }
160
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700161 return err;
162}
163
164
165/*
166 * Request for version info.
167 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700168static JdwpError handleVM_Version(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169 /* text information on runtime version */
170 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800171 expandBufAddUtf8String(pReply, version);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700172 /* JDWP version numbers */
173 expandBufAdd4BE(pReply, 1); // major
174 expandBufAdd4BE(pReply, 5); // minor
175 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800176 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700177 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800178 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179
180 return ERR_NONE;
181}
182
183/*
184 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
185 * referenceTypeID. We need to send back more than one if the class has
186 * been loaded by multiple class loaders.
187 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700188static JdwpError handleVM_ClassesBySignature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800189 std::string classDescriptor(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800190 VLOG(jdwp) << " Req for class by signature '" << classDescriptor << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700191
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800192 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800193 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700194
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800195 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700196
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800197 for (size_t i = 0; i < ids.size(); ++i) {
198 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800199 JDWP::JdwpTypeTag type_tag;
200 uint32_t class_status;
201 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
202 if (status != ERR_NONE) {
203 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800204 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205
Elliott Hughes436e3722012-02-17 20:01:47 -0800206 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800207 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800208 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700209 }
210
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700211 return ERR_NONE;
212}
213
214/*
215 * Handle request for the thread IDs of all running threads.
216 *
217 * We exclude ourselves from the list, because we don't allow ourselves
218 * to be suspended, and that violates some JDWP expectations.
219 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700220static JdwpError handleVM_AllThreads(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700221 ObjectId* pThreadIds;
222 uint32_t threadCount;
223 Dbg::GetAllThreads(&pThreadIds, &threadCount);
224
225 expandBufAdd4BE(pReply, threadCount);
226
227 ObjectId* walker = pThreadIds;
228 for (uint32_t i = 0; i < threadCount; i++) {
229 expandBufAddObjectId(pReply, *walker++);
230 }
231
232 free(pThreadIds);
233
234 return ERR_NONE;
235}
236
237/*
238 * List all thread groups that do not have a parent.
239 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700240static JdwpError handleVM_TopLevelThreadGroups(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700241 /*
242 * TODO: maintain a list of parentless thread groups in the VM.
243 *
244 * For now, just return "system". Application threads are created
245 * in "main", which is a child of "system".
246 */
247 uint32_t groups = 1;
248 expandBufAdd4BE(pReply, groups);
249 //threadGroupId = debugGetMainThreadGroup();
250 //expandBufAdd8BE(pReply, threadGroupId);
251 ObjectId threadGroupId = Dbg::GetSystemThreadGroupId();
252 expandBufAddObjectId(pReply, threadGroupId);
253
254 return ERR_NONE;
255}
256
257/*
258 * Respond with the sizes of the basic debugger types.
259 *
260 * All IDs are 8 bytes.
261 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700262static JdwpError handleVM_IDSizes(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700263 expandBufAdd4BE(pReply, sizeof(FieldId));
264 expandBufAdd4BE(pReply, sizeof(MethodId));
265 expandBufAdd4BE(pReply, sizeof(ObjectId));
266 expandBufAdd4BE(pReply, sizeof(RefTypeId));
267 expandBufAdd4BE(pReply, sizeof(FrameId));
268 return ERR_NONE;
269}
270
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700271static JdwpError handleVM_Dispose(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes86964332012-02-15 19:37:42 -0800272 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700273 return ERR_NONE;
274}
275
276/*
277 * Suspend the execution of the application running in the VM (i.e. suspend
278 * all threads).
279 *
280 * This needs to increment the "suspend count" on all threads.
281 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700282static JdwpError handleVM_Suspend(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700283 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700284 return ERR_NONE;
285}
286
287/*
288 * Resume execution. Decrements the "suspend count" of all threads.
289 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700290static JdwpError handleVM_Resume(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700291 Dbg::ResumeVM();
292 return ERR_NONE;
293}
294
295/*
296 * The debugger wants the entire VM to exit.
297 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700298static JdwpError handleVM_Exit(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700299 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700300
301 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
302
303 Dbg::Exit(exitCode);
304 return ERR_NOT_IMPLEMENTED; // shouldn't get here
305}
306
307/*
308 * Create a new string in the VM and return its ID.
309 *
310 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
311 * string "java.util.Arrays".)
312 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700313static JdwpError handleVM_CreateString(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800314 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800315 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700316 ObjectId stringId = Dbg::CreateString(str);
317 if (stringId == 0) {
318 return ERR_OUT_OF_MEMORY;
319 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700320 expandBufAddObjectId(pReply, stringId);
321 return ERR_NONE;
322}
323
324/*
325 * Tell the debugger what we are capable of.
326 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700327static JdwpError handleVM_Capabilities(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700328 expandBufAdd1(pReply, false); /* canWatchFieldModification */
329 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
330 expandBufAdd1(pReply, false); /* canGetBytecodes */
331 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
332 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
333 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
334 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
335 return ERR_NONE;
336}
337
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700338static JdwpError handleVM_ClassPaths(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800339 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700340
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800341 std::vector<std::string> class_path;
342 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
343 expandBufAdd4BE(pReply, class_path.size());
344 for (size_t i = 0; i < class_path.size(); ++i) {
345 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700346 }
347
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800348 std::vector<std::string> boot_class_path;
349 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
350 expandBufAdd4BE(pReply, boot_class_path.size());
351 for (size_t i = 0; i < boot_class_path.size(); ++i) {
352 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700353 }
354
355 return ERR_NONE;
356}
357
358/*
359 * Release a list of object IDs. (Seen in jdb.)
360 *
361 * Currently does nothing.
362 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700363static JdwpError HandleVM_DisposeObjects(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700364 return ERR_NONE;
365}
366
367/*
368 * Tell the debugger what we are capable of.
369 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700370static JdwpError handleVM_CapabilitiesNew(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700371 expandBufAdd1(pReply, false); /* canWatchFieldModification */
372 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
373 expandBufAdd1(pReply, false); /* canGetBytecodes */
374 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
375 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
376 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
377 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
378 expandBufAdd1(pReply, false); /* canRedefineClasses */
379 expandBufAdd1(pReply, false); /* canAddMethod */
380 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
381 expandBufAdd1(pReply, false); /* canPopFrames */
382 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
383 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
384 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
385 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
386 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
387 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
388 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
389 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
390 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
391 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
392
393 /* fill in reserved22 through reserved32; note count started at 1 */
394 for (int i = 22; i <= 32; i++) {
395 expandBufAdd1(pReply, false); /* reservedN */
396 }
397 return ERR_NONE;
398}
399
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700400static JdwpError handleVM_AllClasses(ExpandBuf* pReply, bool descriptor_and_status, bool generic) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800401 std::vector<JDWP::RefTypeId> classes;
402 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700403
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800404 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700405
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800406 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800407 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800408 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800409 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800410 uint32_t class_status;
411 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
412 if (status != ERR_NONE) {
413 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800414 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700415
Elliott Hughes436e3722012-02-17 20:01:47 -0800416 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800417 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800418 if (descriptor_and_status) {
419 expandBufAddUtf8String(pReply, descriptor);
420 if (generic) {
421 expandBufAddUtf8String(pReply, genericSignature);
422 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800423 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800424 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700425 }
426
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700427 return ERR_NONE;
428}
429
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700430static JdwpError handleVM_AllClasses(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
431 return handleVM_AllClasses(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800432}
433
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700434static JdwpError handleVM_AllClassesWithGeneric(JdwpState*, const uint8_t*, int, ExpandBuf* pReply) {
435 return handleVM_AllClasses(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800436}
437
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700438static JdwpError handleRT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700439 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800440 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441}
442
443/*
444 * Get values from static fields in a reference type.
445 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700446static JdwpError handleRT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800447 RefTypeId refTypeId = ReadRefTypeId(&buf);
448 uint32_t field_count = Read4BE(&buf);
449 expandBufAdd4BE(pReply, field_count);
450 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800452 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800453 if (status != ERR_NONE) {
454 return status;
455 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700456 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700457 return ERR_NONE;
458}
459
460/*
461 * Get the name of the source file in which a reference type was declared.
462 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700463static JdwpError handleRT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700464 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800465 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800466 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
467 if (status != ERR_NONE) {
468 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700469 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800470 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800471 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700472}
473
474/*
475 * Return the current status of the reference type.
476 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700477static JdwpError handleRT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700478 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800479 JDWP::JdwpTypeTag type_tag;
480 uint32_t class_status;
481 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
482 if (status != ERR_NONE) {
483 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800484 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800485 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700486 return ERR_NONE;
487}
488
489/*
490 * Return interfaces implemented directly by this class.
491 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700492static JdwpError handleRT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700493 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800494 VLOG(jdwp) << StringPrintf(" Req for interfaces in %#llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800495 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700496}
497
498/*
499 * Return the class object corresponding to this type.
500 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700501static JdwpError handleRT_ClassObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700502 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800503 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800504 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
505 if (status != ERR_NONE) {
506 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800507 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800508 VLOG(jdwp) << StringPrintf(" RefTypeId %#llx -> ObjectId %#llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800509 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700510 return ERR_NONE;
511}
512
513/*
514 * Returns the value of the SourceDebugExtension attribute.
515 *
516 * JDB seems interested, but DEX files don't currently support this.
517 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700518static JdwpError handleRT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700519 /* referenceTypeId in, string out */
520 return ERR_ABSENT_INFORMATION;
521}
522
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700523static JdwpError handleRT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply, bool with_generic) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700524 RefTypeId refTypeId = ReadRefTypeId(&buf);
525
Elliott Hughes229feb72012-02-23 13:33:29 -0800526 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=%#llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800527 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800528
529 JdwpError status = Dbg::GetSignature(refTypeId, signature);
530 if (status != ERR_NONE) {
531 return status;
532 }
533 expandBufAddUtf8String(pReply, signature);
534 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800535 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700536 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700537 return ERR_NONE;
538}
539
Elliott Hughes98e43f62012-02-24 12:42:35 -0800540static JdwpError handleRT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
541 return handleRT_Signature(state, buf, dataLen, pReply, false);
542}
543
544static JdwpError handleRT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
545 return handleRT_Signature(state, buf, dataLen, pReply, true);
546}
547
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700548/*
549 * Return the instance of java.lang.ClassLoader that loaded the specified
550 * reference type, or null if it was loaded by the system loader.
551 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700552static JdwpError handleRT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700553 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800554 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700555}
556
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800557static std::string Describe(const RefTypeId& refTypeId) {
558 std::string signature("unknown");
559 Dbg::GetSignature(refTypeId, signature);
Elliott Hughes229feb72012-02-23 13:33:29 -0800560 return StringPrintf("refTypeId=%#llx (%s)", refTypeId, signature.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800561}
562
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700563/*
564 * Given a referenceTypeId, return a block of stuff that describes the
565 * fields declared by a class.
566 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700567static JdwpError handleRT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700568 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800569 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800570 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800571}
572
573// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700574static JdwpError handleRT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800575 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800576 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800577 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700578}
579
580/*
581 * Given a referenceTypeID, return a block of goodies describing the
582 * methods declared by a class.
583 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700584static JdwpError handleRT_MethodsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700585 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800586 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800587 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800588}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700589
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800590// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700591static JdwpError handleRT_Methods(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800592 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800593 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800594 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700595}
596
597/*
598 * Return the immediate superclass of a class.
599 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700600static JdwpError handleCT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700601 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800602 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700603 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800604 if (status != ERR_NONE) {
605 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800606 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700607 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700608 return ERR_NONE;
609}
610
611/*
612 * Set static class values.
613 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700614static JdwpError handleCT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700615 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700616 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617
Elliott Hughes74847412012-06-20 18:10:21 -0700618 VLOG(jdwp) << StringPrintf(" Req to set %d values in class_id=%#llx", values, class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700619
620 for (uint32_t i = 0; i < values; i++) {
621 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800622 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800623 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700624 uint64_t value = jdwpReadValue(&buf, width);
625
Elliott Hughes2435a572012-02-17 16:07:41 -0800626 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800627 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
628 if (status != ERR_NONE) {
629 return status;
630 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700631 }
632
633 return ERR_NONE;
634}
635
636/*
637 * Invoke a static method.
638 *
639 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
640 * values in the "variables" display.
641 */
642static JdwpError handleCT_InvokeMethod(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 return finishInvoke(state, buf, dataLen, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700648}
649
650/*
651 * Create a new object of the requested type, and invoke the specified
652 * constructor.
653 *
654 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
655 * see the contents of a byte[] as a string.
656 */
657static JdwpError handleCT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700658 RefTypeId class_id = ReadRefTypeId(&buf);
659 ObjectId thread_id = ReadObjectId(&buf);
660 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700661
Elliott Hughes74847412012-06-20 18:10:21 -0700662 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(class_id);
663 ObjectId object_id;
664 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800665 if (status != ERR_NONE) {
666 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800667 }
Elliott Hughes74847412012-06-20 18:10:21 -0700668 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700669 return ERR_OUT_OF_MEMORY;
670 }
Elliott Hughes74847412012-06-20 18:10:21 -0700671 return finishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700672}
673
674/*
675 * Create a new array object of the requested type and length.
676 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700677static JdwpError handleAT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700678 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700679 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700680
Elliott Hughes2435a572012-02-17 16:07:41 -0800681 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes74847412012-06-20 18:10:21 -0700682 ObjectId object_id;
683 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800684 if (status != ERR_NONE) {
685 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800686 }
Elliott Hughes74847412012-06-20 18:10:21 -0700687 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700688 return ERR_OUT_OF_MEMORY;
689 }
690 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700691 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700692 return ERR_NONE;
693}
694
695/*
696 * Return line number information for the method, if present.
697 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700698static JdwpError handleM_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700699 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700700 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700701
Elliott Hughes74847412012-06-20 18:10:21 -0700702 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId, method_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700703
Elliott Hughes74847412012-06-20 18:10:21 -0700704 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700705
706 return ERR_NONE;
707}
708
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700709static JdwpError handleM_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply, bool generic) {
Elliott Hughes74847412012-06-20 18:10:21 -0700710 RefTypeId class_id = ReadRefTypeId(&buf);
711 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700712
Elliott Hughes74847412012-06-20 18:10:21 -0700713 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 -0700714
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800715 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
716 // information. That will cause Eclipse to make a best-effort attempt at displaying local
717 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
718 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700719 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700720 return ERR_NONE;
721}
722
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800723static JdwpError handleM_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
724 return handleM_VariableTable(state, buf, dataLen, pReply, false);
725}
726
727static JdwpError handleM_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
728 return handleM_VariableTable(state, buf, dataLen, pReply, true);
729}
730
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700731/*
732 * Given an object reference, return the runtime type of the object
733 * (class or array).
734 *
Elliott Hughes74847412012-06-20 18:10:21 -0700735 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700736 * passed in here.
737 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700738static JdwpError handleOR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700739 ObjectId object_id = ReadObjectId(&buf);
740 VLOG(jdwp) << StringPrintf(" Req for type of object_id=%#llx", object_id);
741 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742}
743
744/*
745 * Get values from the fields of an object.
746 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700747static JdwpError handleOR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700748 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800749 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700750
Elliott Hughes74847412012-06-20 18:10:21 -0700751 VLOG(jdwp) << StringPrintf(" Req for %d fields from object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700752
Elliott Hughes0cf74332012-02-23 23:14:00 -0800753 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700754
Elliott Hughes0cf74332012-02-23 23:14:00 -0800755 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700756 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700757 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800758 if (status != ERR_NONE) {
759 return status;
760 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700761 }
762
763 return ERR_NONE;
764}
765
766/*
767 * Set values in the fields of an object.
768 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700769static JdwpError handleOR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -0700770 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800771 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700772
Elliott Hughes74847412012-06-20 18:10:21 -0700773 VLOG(jdwp) << StringPrintf(" Req to set %d fields in object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700774
Elliott Hughes0cf74332012-02-23 23:14:00 -0800775 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700776 FieldId fieldId = ReadFieldId(&buf);
777
Elliott Hughesaed4be92011-12-02 16:16:23 -0800778 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800779 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700780 uint64_t value = jdwpReadValue(&buf, width);
781
Elliott Hughes2435a572012-02-17 16:07:41 -0800782 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700783 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800784 if (status != ERR_NONE) {
785 return status;
786 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700787 }
788
789 return ERR_NONE;
790}
791
792/*
793 * Invoke an instance method. The invocation must occur in the specified
794 * thread, which must have been suspended by an event.
795 *
796 * The call is synchronous. All threads in the VM are resumed, unless the
797 * SINGLE_THREADED flag is set.
798 *
799 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
800 * object), it will try to invoke the object's toString() function. This
801 * feature becomes crucial when examining ArrayLists with Eclipse.
802 */
803static JdwpError handleOR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700804 ObjectId object_id = ReadObjectId(&buf);
805 ObjectId thread_id = ReadObjectId(&buf);
806 RefTypeId class_id = ReadRefTypeId(&buf);
807 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700808
Elliott Hughes74847412012-06-20 18:10:21 -0700809 return finishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700810}
811
812/*
813 * Disable garbage collection of the specified object.
814 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700815static JdwpError handleOR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700816 // this is currently a no-op
817 return ERR_NONE;
818}
819
820/*
821 * Enable garbage collection of the specified object.
822 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700823static JdwpError handleOR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700824 // this is currently a no-op
825 return ERR_NONE;
826}
827
828/*
829 * Determine whether an object has been garbage collected.
830 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700831static JdwpError handleOR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700832 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700833
Elliott Hughes74847412012-06-20 18:10:21 -0700834 object_id = ReadObjectId(&buf);
835 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700836
837 // TODO: currently returning false; must integrate with GC
838 expandBufAdd1(pReply, 0);
839
840 return ERR_NONE;
841}
842
843/*
844 * Return the string value in a string object.
845 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700846static JdwpError handleSR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700847 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800848 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700849
Elliott Hughes82914b62012-04-09 15:56:29 -0700850 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700851
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800852 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700853
854 return ERR_NONE;
855}
856
857/*
858 * Return a thread's name.
859 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700860static JdwpError handleTR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700861 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700862
Elliott Hughes74847412012-06-20 18:10:21 -0700863 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800864 std::string name;
Elliott Hughes74847412012-06-20 18:10:21 -0700865 if (!Dbg::GetThreadName(thread_id, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700866 return ERR_INVALID_THREAD;
867 }
Elliott Hughes74847412012-06-20 18:10:21 -0700868 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800869 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700870
871 return ERR_NONE;
872}
873
874/*
875 * Suspend the specified thread.
876 *
877 * It's supposed to remain suspended even if interpreted code wants to
878 * resume it; only the JDI is allowed to resume it.
879 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700880static JdwpError handleTR_Suspend(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 suspend self";
885 return ERR_THREAD_NOT_SUSPENDED;
886 }
Elliott Hughes74847412012-06-20 18:10:21 -0700887 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
888 Dbg::SuspendThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700889 return ERR_NONE;
890}
891
892/*
893 * Resume the specified thread.
894 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700895static JdwpError handleTR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
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 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700899 LOG(INFO) << " Warning: ignoring request to resume self";
900 return ERR_NONE;
901 }
Elliott Hughes74847412012-06-20 18:10:21 -0700902 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
903 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700904 return ERR_NONE;
905}
906
907/*
908 * Return status of specified thread.
909 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700910static JdwpError handleTR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700911 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700912
Elliott Hughes74847412012-06-20 18:10:21 -0700913 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700914
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800915 JDWP::JdwpThreadStatus threadStatus;
916 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes74847412012-06-20 18:10:21 -0700917 if (!Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700918 return ERR_INVALID_THREAD;
919 }
920
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800921 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700922
923 expandBufAdd4BE(pReply, threadStatus);
924 expandBufAdd4BE(pReply, suspendStatus);
925
926 return ERR_NONE;
927}
928
929/*
930 * Return the thread group that the specified thread is a member of.
931 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700932static JdwpError handleTR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700933 ObjectId thread_id = ReadObjectId(&buf);
934 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700935}
936
937/*
938 * Return the current call stack of a suspended thread.
939 *
940 * If the thread isn't suspended, the error code isn't defined, but should
941 * be THREAD_NOT_SUSPENDED.
942 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700943static JdwpError handleTR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700944 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800945 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700946 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700947
Elliott Hughes74847412012-06-20 18:10:21 -0700948 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700949 return ERR_INVALID_THREAD;
950 }
Elliott Hughes74847412012-06-20 18:10:21 -0700951 if (!Dbg::IsSuspended(thread_id)) {
952 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700953 return ERR_THREAD_NOT_SUSPENDED;
954 }
955
Elliott Hughes74847412012-06-20 18:10:21 -0700956 size_t actual_frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700957
Elliott Hughes74847412012-06-20 18:10:21 -0700958 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 -0800959 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700960 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
961 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700962
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800963 if (start_frame > actual_frame_count) {
964 return ERR_INVALID_INDEX;
965 }
966 if (length == static_cast<uint32_t>(-1)) {
967 length = actual_frame_count - start_frame;
968 }
969 if (start_frame + length > actual_frame_count) {
970 return ERR_INVALID_LENGTH;
971 }
972
973 expandBufAdd4BE(pReply, length);
974 for (uint32_t i = start_frame; i < (start_frame + length); ++i) {
Elliott Hughes546b9862012-06-20 16:06:13 -0700975 FrameId frame_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700976 JdwpLocation loc;
Elliott Hughes530fa002012-03-12 11:44:49 -0700977 // TODO: switch to GetThreadFrames so we don't have to search for each frame
978 // even though we only want them in order.
Elliott Hughes74847412012-06-20 18:10:21 -0700979 Dbg::GetThreadFrame(thread_id, i, &frame_id, &loc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700980
Elliott Hughes546b9862012-06-20 16:06:13 -0700981 expandBufAdd8BE(pReply, frame_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700982 AddLocation(pReply, &loc);
983
Elliott Hughes546b9862012-06-20 16:06:13 -0700984 VLOG(jdwp) << StringPrintf(" Frame %d: id=%#llx ", i, frame_id) << loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700985 }
986
987 return ERR_NONE;
988}
989
990/*
991 * Returns the #of frames on the specified thread, which must be suspended.
992 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700993static JdwpError handleTR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -0700994 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995
Elliott Hughes74847412012-06-20 18:10:21 -0700996 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700997 return ERR_INVALID_THREAD;
998 }
Elliott Hughes74847412012-06-20 18:10:21 -0700999 if (!Dbg::IsSuspended(thread_id)) {
1000 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001001 return ERR_THREAD_NOT_SUSPENDED;
1002 }
1003
Elliott Hughes74847412012-06-20 18:10:21 -07001004 int frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001005 if (frame_count < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001006 return ERR_INVALID_THREAD;
1007 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001008 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001009
1010 return ERR_NONE;
1011}
1012
1013/*
1014 * Get the monitor that the thread is waiting on.
1015 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001016static JdwpError handleTR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -07001017 ReadObjectId(&buf); // thread_id
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001018
1019 // TODO: create an Object to represent the monitor (we're currently
1020 // just using a raw Monitor struct in the VM)
1021
1022 return ERR_NOT_IMPLEMENTED;
1023}
1024
1025/*
1026 * Return the suspend count for the specified thread.
1027 *
1028 * (The thread *might* still be running -- it might not have examined
1029 * its suspend count recently.)
1030 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001031static JdwpError handleTR_SuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -07001032 ObjectId thread_id = ReadObjectId(&buf);
1033 return Dbg::GetThreadSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001034}
1035
1036/*
1037 * Return the name of a thread group.
1038 *
1039 * The Eclipse debugger recognizes "main" and "system" as special.
1040 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001041static JdwpError handleTGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001042 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001043 VLOG(jdwp) << StringPrintf(" Req for name of threadGroupId=%#llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001044
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001045 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(threadGroupId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001046
1047 return ERR_NONE;
1048}
1049
1050/*
1051 * Returns the thread group -- if any -- that contains the specified
1052 * thread group.
1053 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001054static JdwpError handleTGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001055 ObjectId groupId = ReadObjectId(&buf);
1056
1057 ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId);
1058 expandBufAddObjectId(pReply, parentGroup);
1059
1060 return ERR_NONE;
1061}
1062
1063/*
1064 * Return the active threads and thread groups that are part of the
1065 * specified thread group.
1066 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001067static JdwpError handleTGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001068 ObjectId threadGroupId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001069 VLOG(jdwp) << StringPrintf(" Req for threads in threadGroupId=%#llx", threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001070
1071 ObjectId* pThreadIds;
1072 uint32_t threadCount;
1073 Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount);
1074
1075 expandBufAdd4BE(pReply, threadCount);
1076
1077 for (uint32_t i = 0; i < threadCount; i++) {
1078 expandBufAddObjectId(pReply, pThreadIds[i]);
1079 }
1080 free(pThreadIds);
1081
1082 /*
1083 * TODO: finish support for child groups
1084 *
1085 * For now, just show that "main" is a child of "system".
1086 */
1087 if (threadGroupId == Dbg::GetSystemThreadGroupId()) {
1088 expandBufAdd4BE(pReply, 1);
1089 expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId());
1090 } else {
1091 expandBufAdd4BE(pReply, 0);
1092 }
1093
1094 return ERR_NONE;
1095}
1096
1097/*
1098 * Return the #of components in the array.
1099 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001100static JdwpError handleAR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001101 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001102 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001103
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001104 int length;
1105 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1106 if (status != ERR_NONE) {
1107 return status;
1108 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001109 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001110
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001111 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001112
1113 return ERR_NONE;
1114}
1115
1116/*
1117 * Return the values from an array.
1118 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001119static JdwpError handleAR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001120 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001121 uint32_t firstIndex = Read4BE(&buf);
1122 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001123 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001124
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001125 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001126}
1127
1128/*
1129 * Set values in an array.
1130 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001131static JdwpError handleAR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001132 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001133 uint32_t firstIndex = Read4BE(&buf);
1134 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001135
Elliott Hughes229feb72012-02-23 13:33:29 -08001136 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId, firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001137
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001138 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001139}
1140
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001141static JdwpError handleCLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001142 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001143 // TODO: we should only return classes which have the given class loader as a defining or
1144 // initiating loader. The former would be easy; the latter is hard, because we don't have
1145 // any such notion.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001146 return handleVM_AllClasses(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001147}
1148
1149/*
1150 * Set an event trigger.
1151 *
1152 * Reply with a requestID.
1153 */
1154static JdwpError handleER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1155 const uint8_t* origBuf = buf;
1156
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001157 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001158 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001159 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001160
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001161 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001162 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001163 << " mods=" << modifierCount << ")";
1164
1165 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1166
1167 JdwpEvent* pEvent = EventAlloc(modifierCount);
1168 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001169 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001170 pEvent->modCount = modifierCount;
1171
1172 /*
1173 * Read modifiers. Ordering may be significant (see explanation of Count
1174 * mods in JDWP doc).
1175 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001176 for (uint32_t i = 0; i < modifierCount; ++i) {
1177 JdwpEventMod& mod = pEvent->mods[i];
1178 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1179 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001180 case MK_COUNT: /* report once, when "--count" reaches 0 */
1181 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001182 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001183 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001184 if (count == 0) {
1185 return ERR_INVALID_COUNT;
1186 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001187 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001188 }
1189 break;
1190 case MK_CONDITIONAL: /* conditional on expression) */
1191 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001192 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001193 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001194 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001195 }
1196 break;
1197 case MK_THREAD_ONLY: /* only report events in specified thread */
1198 {
Elliott Hughes74847412012-06-20 18:10:21 -07001199 ObjectId thread_id = ReadObjectId(&buf);
1200 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1201 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001202 }
1203 break;
1204 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1205 {
Elliott Hughes74847412012-06-20 18:10:21 -07001206 RefTypeId class_id = ReadRefTypeId(&buf);
1207 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1208 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001209 }
1210 break;
1211 case MK_CLASS_MATCH: /* restrict events to matching classes */
1212 {
Elliott Hughes86964332012-02-15 19:37:42 -08001213 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001214 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001215 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001216 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001217 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001218 }
1219 break;
1220 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1221 {
Elliott Hughes86964332012-02-15 19:37:42 -08001222 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001223 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001224 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001225 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001226 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001227 }
1228 break;
1229 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1230 {
1231 JdwpLocation loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001232 jdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001233 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001234 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001235 }
1236 break;
1237 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1238 {
1239 RefTypeId exceptionOrNull; /* null == all exceptions */
1240 uint8_t caught, uncaught;
1241
1242 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001243 caught = Read1(&buf);
1244 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001245 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001246 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001247
Elliott Hughes972a47b2012-02-21 18:16:06 -08001248 mod.exceptionOnly.refTypeId = exceptionOrNull;
1249 mod.exceptionOnly.caught = caught;
1250 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001251 }
1252 break;
1253 case MK_FIELD_ONLY: /* for field access/mod events */
1254 {
1255 RefTypeId declaring = ReadRefTypeId(&buf);
1256 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001257 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001258 mod.fieldOnly.refTypeId = declaring;
1259 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001260 }
1261 break;
1262 case MK_STEP: /* for use with EK_SINGLE_STEP */
1263 {
Elliott Hughes74847412012-06-20 18:10:21 -07001264 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001265 uint32_t size, depth;
1266
Elliott Hughes74847412012-06-20 18:10:21 -07001267 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001268 size = Read4BE(&buf);
1269 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001270 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001271 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1272
Elliott Hughes74847412012-06-20 18:10:21 -07001273 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001274 mod.step.size = size;
1275 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001276 }
1277 break;
1278 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1279 {
1280 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001281 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001282 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001283 }
1284 break;
1285 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001286 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001287 break;
1288 }
1289 }
1290
1291 /*
1292 * Make sure we consumed all data. It is possible that the remote side
1293 * has sent us bad stuff, but for now we blame ourselves.
1294 */
1295 if (buf != origBuf + dataLen) {
1296 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1297 }
1298
1299 /*
1300 * We reply with an integer "requestID".
1301 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001302 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001303 expandBufAdd4BE(pReply, requestId);
1304
1305 pEvent->requestId = requestId;
1306
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001307 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001308
1309 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001310 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001311 if (err != ERR_NONE) {
1312 /* registration failed, probably because event is bogus */
1313 EventFree(pEvent);
1314 LOG(WARNING) << "WARNING: event request rejected";
1315 }
1316 return err;
1317}
1318
1319/*
1320 * Clear an event. Failure to find an event with a matching ID is a no-op
1321 * and does not return an error.
1322 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001323static JdwpError handleER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001324 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001325 eventKind = Read1(&buf);
1326 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001327
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001328 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001329
Elliott Hughes761928d2011-11-16 18:33:03 -08001330 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001331
1332 return ERR_NONE;
1333}
1334
1335/*
1336 * Return the values of arguments and local variables.
1337 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001338static JdwpError handleSF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes74847412012-06-20 18:10:21 -07001339 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001340 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001341 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001342
Elliott Hughes74847412012-06-20 18:10:21 -07001343 VLOG(jdwp) << StringPrintf(" Req for %d slots in thread_id=%#llx frame_id=%#llx", slots, thread_id, frame_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001344
1345 expandBufAdd4BE(pReply, slots); /* "int values" */
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 reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001349
Elliott Hughes2435a572012-02-17 16:07:41 -08001350 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001351
Elliott Hughesdbb40792011-11-18 17:05:22 -08001352 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001353 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001354 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001355 }
1356
1357 return ERR_NONE;
1358}
1359
1360/*
1361 * Set the values of arguments and local variables.
1362 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001363static JdwpError handleSF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*) {
Elliott Hughes74847412012-06-20 18:10:21 -07001364 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001365 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001366 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001367
Elliott Hughes74847412012-06-20 18:10:21 -07001368 VLOG(jdwp) << StringPrintf(" Req to set %d slots in thread_id=%#llx frame_id=%#llx", slots, thread_id, frame_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001369
1370 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001371 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001372 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001373 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001374 uint64_t value = jdwpReadValue(&buf, width);
1375
Elliott Hughes2435a572012-02-17 16:07:41 -08001376 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001377 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001378 }
1379
1380 return ERR_NONE;
1381}
1382
1383/*
1384 * Returns the value of "this" for the specified frame.
1385 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001386static JdwpError handleSF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughesd07986f2011-12-06 18:27:45 -08001387 ReadObjectId(&buf); // Skip thread id.
Elliott Hughes546b9862012-06-20 16:06:13 -07001388 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001389
Elliott Hughes546b9862012-06-20 16:06:13 -07001390 ObjectId id;
1391 Dbg::GetThisObject(frame_id, &id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001392
Elliott Hughes546b9862012-06-20 16:06:13 -07001393 uint8_t tag;
1394 JdwpError result = Dbg::GetObjectTag(id, tag);
1395 if (result != ERR_NONE) {
1396 return result;
1397 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001398
Elliott Hughes546b9862012-06-20 16:06:13 -07001399 VLOG(jdwp) << StringPrintf(" Req for 'this' in frame=%#llx --> %#llx '%c'", frame_id, id, static_cast<char>(tag));
1400 expandBufAdd1(pReply, tag);
1401 expandBufAddObjectId(pReply, id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001402
1403 return ERR_NONE;
1404}
1405
1406/*
1407 * Return the reference type reflected by this class object.
1408 *
1409 * This appears to be required because ReferenceTypeId values are NEVER
1410 * reused, whereas ClassIds can be recycled like any other object. (Either
1411 * that, or I have no idea what this is for.)
1412 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001413static JdwpError handleCOR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001414 RefTypeId classObjectId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001415 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId, Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001416 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001417}
1418
1419/*
1420 * Handle a DDM packet with a single chunk in it.
1421 */
1422static JdwpError handleDDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1423 uint8_t* replyBuf = NULL;
1424 int replyLen = -1;
1425
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001426 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001427
Elliott Hughesa21039c2012-06-21 12:09:25 -07001428 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001429
1430 /*
1431 * If they want to send something back, we copy it into the buffer.
1432 * A no-copy approach would be nicer.
1433 *
1434 * TODO: consider altering the JDWP stuff to hold the packet header
1435 * in a separate buffer. That would allow us to writev() DDM traffic
1436 * instead of copying it into the expanding buffer. The reduction in
1437 * heap requirements is probably more valuable than the efficiency.
1438 */
1439 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1440 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1441 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1442 free(replyBuf);
1443 }
1444 return ERR_NONE;
1445}
1446
1447/*
1448 * Handler map decl.
1449 */
1450typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1451
1452struct JdwpHandlerMap {
1453 uint8_t cmdSet;
1454 uint8_t cmd;
1455 JdwpRequestHandler func;
1456 const char* descr;
1457};
1458
1459/*
1460 * Map commands to functions.
1461 *
1462 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1463 * and 128-256 are vendor-defined.
1464 */
1465static const JdwpHandlerMap gHandlerMap[] = {
1466 /* VirtualMachine command set (1) */
1467 { 1, 1, handleVM_Version, "VirtualMachine.Version" },
1468 { 1, 2, handleVM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
Elliott Hughes1fe7afb2012-02-13 17:23:03 -08001469 { 1, 3, handleVM_AllClasses, "VirtualMachine.AllClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001470 { 1, 4, handleVM_AllThreads, "VirtualMachine.AllThreads" },
1471 { 1, 5, handleVM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1472 { 1, 6, handleVM_Dispose, "VirtualMachine.Dispose" },
1473 { 1, 7, handleVM_IDSizes, "VirtualMachine.IDSizes" },
1474 { 1, 8, handleVM_Suspend, "VirtualMachine.Suspend" },
1475 { 1, 9, handleVM_Resume, "VirtualMachine.Resume" },
1476 { 1, 10, handleVM_Exit, "VirtualMachine.Exit" },
1477 { 1, 11, handleVM_CreateString, "VirtualMachine.CreateString" },
1478 { 1, 12, handleVM_Capabilities, "VirtualMachine.Capabilities" },
1479 { 1, 13, handleVM_ClassPaths, "VirtualMachine.ClassPaths" },
1480 { 1, 14, HandleVM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001481 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1482 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001483 { 1, 17, handleVM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001484 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1485 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
1486 { 1, 20, handleVM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
1487 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001488
1489 /* ReferenceType command set (2) */
1490 { 2, 1, handleRT_Signature, "ReferenceType.Signature" },
1491 { 2, 2, handleRT_ClassLoader, "ReferenceType.ClassLoader" },
1492 { 2, 3, handleRT_Modifiers, "ReferenceType.Modifiers" },
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001493 { 2, 4, handleRT_Fields, "ReferenceType.Fields" },
1494 { 2, 5, handleRT_Methods, "ReferenceType.Methods" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001495 { 2, 6, handleRT_GetValues, "ReferenceType.GetValues" },
1496 { 2, 7, handleRT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001497 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001498 { 2, 9, handleRT_Status, "ReferenceType.Status" },
1499 { 2, 10, handleRT_Interfaces, "ReferenceType.Interfaces" },
1500 { 2, 11, handleRT_ClassObject, "ReferenceType.ClassObject" },
1501 { 2, 12, handleRT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1502 { 2, 13, handleRT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1503 { 2, 14, handleRT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1504 { 2, 15, handleRT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001505 { 2, 16, NULL, "ReferenceType.Instances" },
1506 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1507 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001508
1509 /* ClassType command set (3) */
1510 { 3, 1, handleCT_Superclass, "ClassType.Superclass" },
1511 { 3, 2, handleCT_SetValues, "ClassType.SetValues" },
1512 { 3, 3, handleCT_InvokeMethod, "ClassType.InvokeMethod" },
1513 { 3, 4, handleCT_NewInstance, "ClassType.NewInstance" },
1514
1515 /* ArrayType command set (4) */
1516 { 4, 1, handleAT_newInstance, "ArrayType.NewInstance" },
1517
1518 /* InterfaceType command set (5) */
1519
1520 /* Method command set (6) */
1521 { 6, 1, handleM_LineTable, "Method.LineTable" },
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001522 { 6, 2, handleM_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001523 { 6, 3, NULL, "Method.Bytecodes" },
1524 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001525 { 6, 5, handleM_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
1526
1527 /* Field command set (8) */
1528
1529 /* ObjectReference command set (9) */
1530 { 9, 1, handleOR_ReferenceType, "ObjectReference.ReferenceType" },
1531 { 9, 2, handleOR_GetValues, "ObjectReference.GetValues" },
1532 { 9, 3, handleOR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001533 { 9, 4, NULL, "ObjectReference.UNUSED" },
1534 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001535 { 9, 6, handleOR_InvokeMethod, "ObjectReference.InvokeMethod" },
1536 { 9, 7, handleOR_DisableCollection, "ObjectReference.DisableCollection" },
1537 { 9, 8, handleOR_EnableCollection, "ObjectReference.EnableCollection" },
1538 { 9, 9, handleOR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001539 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001540
1541 /* StringReference command set (10) */
1542 { 10, 1, handleSR_Value, "StringReference.Value" },
1543
1544 /* ThreadReference command set (11) */
1545 { 11, 1, handleTR_Name, "ThreadReference.Name" },
1546 { 11, 2, handleTR_Suspend, "ThreadReference.Suspend" },
1547 { 11, 3, handleTR_Resume, "ThreadReference.Resume" },
1548 { 11, 4, handleTR_Status, "ThreadReference.Status" },
1549 { 11, 5, handleTR_ThreadGroup, "ThreadReference.ThreadGroup" },
1550 { 11, 6, handleTR_Frames, "ThreadReference.Frames" },
1551 { 11, 7, handleTR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001552 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001553 { 11, 9, handleTR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001554 { 11, 10, NULL, "ThreadReference.Stop" },
Elliott Hughes74847412012-06-20 18:10:21 -07001555 { 11, 11, NULL, "ThreadReference.Interrupt" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001556 { 11, 12, handleTR_SuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001557 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1558 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001559
1560 /* ThreadGroupReference command set (12) */
1561 { 12, 1, handleTGR_Name, "ThreadGroupReference.Name" },
1562 { 12, 2, handleTGR_Parent, "ThreadGroupReference.Parent" },
1563 { 12, 3, handleTGR_Children, "ThreadGroupReference.Children" },
1564
1565 /* ArrayReference command set (13) */
1566 { 13, 1, handleAR_Length, "ArrayReference.Length" },
1567 { 13, 2, handleAR_GetValues, "ArrayReference.GetValues" },
1568 { 13, 3, handleAR_SetValues, "ArrayReference.SetValues" },
1569
1570 /* ClassLoaderReference command set (14) */
1571 { 14, 1, handleCLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
1572
1573 /* EventRequest command set (15) */
1574 { 15, 1, handleER_Set, "EventRequest.Set" },
1575 { 15, 2, handleER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001576 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001577
1578 /* StackFrame command set (16) */
1579 { 16, 1, handleSF_GetValues, "StackFrame.GetValues" },
1580 { 16, 2, handleSF_SetValues, "StackFrame.SetValues" },
1581 { 16, 3, handleSF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001582 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001583
1584 /* ClassObjectReference command set (17) */
Elliott Hughes74847412012-06-20 18:10:21 -07001585 { 17, 1, handleCOR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001586
1587 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001588 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001589
1590 { 199, 1, handleDDM_Chunk, "DDM.Chunk" },
1591};
1592
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001593static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001594 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001595 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1596 return gHandlerMap[i].descr;
1597 }
1598 }
1599 return "?UNKNOWN?";
1600}
1601
1602static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1603 std::string result;
1604 result += "REQ: ";
1605 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1606 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1607 return result;
1608}
1609
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001610/*
1611 * Process a request from the debugger.
1612 *
1613 * On entry, the JDWP thread is in VMWAIT.
1614 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001615void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001616 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001617
1618 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1619 /*
1620 * Activity from a debugger, not merely ddms. Mark us as having an
1621 * active debugger session, and zero out the last-activity timestamp
1622 * so waitForDebugger() doesn't return if we stall for a bit here.
1623 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001624 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001625 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001626 }
1627
1628 /*
1629 * If a debugger event has fired in another thread, wait until the
1630 * initiating thread has suspended itself before processing messages
1631 * from the debugger. Otherwise we (the JDWP thread) could be told to
1632 * resume the thread before it has suspended.
1633 *
1634 * We call with an argument of zero to wait for the current event
1635 * thread to finish, and then clear the block. Depending on the thread
1636 * suspend policy, this may allow events in other threads to fire,
1637 * but those events have no bearing on what the debugger has sent us
1638 * in the current request.
1639 *
1640 * Note that we MUST clear the event token before waking the event
1641 * thread up, or risk waiting for the thread to suspend after we've
1642 * told it to resume.
1643 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001644 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001645
1646 /*
1647 * Tell the VM that we're running and shouldn't be interrupted by GC.
1648 * Do this after anything that can stall indefinitely.
1649 */
1650 Dbg::ThreadRunning();
1651
1652 expandBufAddSpace(pReply, kJDWPHeaderLen);
1653
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001654 size_t i;
1655 for (i = 0; i < arraysize(gHandlerMap); i++) {
1656 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001657 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001658 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001659 break;
1660 }
1661 }
1662 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001663 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1664 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001665 result = ERR_NOT_IMPLEMENTED;
1666 }
1667
1668 /*
1669 * Set up the reply header.
1670 *
1671 * If we encountered an error, only send the header back.
1672 */
1673 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001674 Set4BE(replyBuf + 4, pHeader->id);
1675 Set1(replyBuf + 8, kJDWPFlagReply);
1676 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001677 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001678 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001679 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001680 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001681 }
1682
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001683 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001684 if (false) {
1685 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001686 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001687 }
1688
1689 /*
1690 * Update last-activity timestamp. We really only need this during
1691 * the initial setup. Only update if this is a non-DDMS packet.
1692 */
1693 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001694 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001695 }
1696
1697 /* tell the VM that GC is okay again */
1698 Dbg::ThreadWaiting();
1699}
1700
1701} // namespace JDWP
1702
1703} // namespace art