blob: c7db5e9667b6838be117a708b60d48c6175e01da [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Handle messages from debugger.
19 *
20 * GENERAL NOTE: we're not currently testing the message length for
21 * correctness. This is usually a bad idea, but here we can probably
22 * get away with it so long as the debugger isn't broken. We can
23 * change the "read" macros to use "dataLen" to avoid wandering into
24 * bad territory, and have a single "is dataLen correct" check at the
25 * end of each function. Not needed at this time.
26 */
27
28#include "atomic.h"
29#include "debugger.h"
30#include "jdwp/jdwp_priv.h"
31#include "jdwp/jdwp_handler.h"
32#include "jdwp/jdwp_event.h"
33#include "jdwp/jdwp_constants.h"
34#include "jdwp/jdwp_expand_buf.h"
35#include "logging.h"
36#include "macros.h"
37#include "stringprintf.h"
38
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43namespace art {
44
45namespace JDWP {
46
47/*
48 * Helper function: read a "location" from an input buffer.
49 */
50static void jdwpReadLocation(const uint8_t** pBuf, JdwpLocation* pLoc) {
51 memset(pLoc, 0, sizeof(*pLoc)); /* allows memcmp() later */
Elliott Hughesd07986f2011-12-06 18:27:45 -080052 pLoc->typeTag = ReadTypeTag(pBuf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070053 pLoc->classId = ReadObjectId(pBuf);
54 pLoc->methodId = ReadMethodId(pBuf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -070055 pLoc->idx = Read8BE(pBuf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070056}
57
58/*
59 * Helper function: write a "location" into the reply buffer.
60 */
61void AddLocation(ExpandBuf* pReply, const JdwpLocation* pLoc) {
62 expandBufAdd1(pReply, pLoc->typeTag);
63 expandBufAddObjectId(pReply, pLoc->classId);
64 expandBufAddMethodId(pReply, pLoc->methodId);
65 expandBufAdd8BE(pReply, pLoc->idx);
66}
67
68/*
69 * Helper function: read a variable-width value from the input buffer.
70 */
Elliott Hughesdbb40792011-11-18 17:05:22 -080071static uint64_t jdwpReadValue(const uint8_t** pBuf, size_t width) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070072 uint64_t value = -1;
73 switch (width) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070074 case 1: value = Read1(pBuf); break;
75 case 2: value = Read2BE(pBuf); break;
76 case 4: value = Read4BE(pBuf); break;
77 case 8: value = Read8BE(pBuf); break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070078 default: LOG(FATAL) << width; break;
79 }
80 return value;
81}
82
83/*
84 * Helper function: write a variable-width value into the output input buffer.
85 */
86static void jdwpWriteValue(ExpandBuf* pReply, int width, uint64_t value) {
87 switch (width) {
88 case 1: expandBufAdd1(pReply, value); break;
89 case 2: expandBufAdd2BE(pReply, value); break;
90 case 4: expandBufAdd4BE(pReply, value); break;
91 case 8: expandBufAdd8BE(pReply, value); break;
92 default: LOG(FATAL) << width; break;
93 }
94}
95
96/*
97 * Common code for *_InvokeMethod requests.
98 *
99 * If "isConstructor" is set, this returns "objectId" rather than the
100 * expected-to-be-void return value of the called function.
101 */
102static JdwpError finishInvoke(JdwpState* state,
103 const uint8_t* buf, int dataLen, ExpandBuf* pReply,
104 ObjectId threadId, ObjectId objectId, RefTypeId classId, MethodId methodId,
105 bool isConstructor)
106{
107 CHECK(!isConstructor || objectId != 0);
108
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700109 uint32_t numArgs = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700110
111 LOG(VERBOSE) << StringPrintf(" --> threadId=%llx objectId=%llx", threadId, objectId);
Elliott Hughes03181a82011-11-17 17:22:21 -0800112 LOG(VERBOSE) << StringPrintf(" classId=%llx methodId=%x %s.%s", classId, methodId, Dbg::GetClassDescriptor(classId).c_str(), Dbg::GetMethodName(classId, methodId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700113 LOG(VERBOSE) << StringPrintf(" %d args:", numArgs);
114
115 uint64_t* argArray = NULL;
116 if (numArgs > 0) {
117 argArray = (ObjectId*) malloc(sizeof(ObjectId) * numArgs);
118 }
119
120 for (uint32_t i = 0; i < numArgs; i++) {
Elliott Hughesaed4be92011-12-02 16:16:23 -0800121 JDWP::JdwpTag typeTag = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800122 size_t width = Dbg::GetTagWidth(typeTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123 uint64_t value = jdwpReadValue(&buf, width);
124
125 LOG(VERBOSE) << StringPrintf(" '%c'(%d): 0x%llx", typeTag, width, value);
126 argArray[i] = value;
127 }
128
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700129 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130 LOG(VERBOSE) << StringPrintf(" options=0x%04x%s%s", options, (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "", (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
131
Elliott Hughesaed4be92011-12-02 16:16:23 -0800132 JDWP::JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700133 uint64_t resultValue;
134 ObjectId exceptObjId;
135 JdwpError err = Dbg::InvokeMethod(threadId, objectId, classId, methodId, numArgs, argArray, options, &resultTag, &resultValue, &exceptObjId);
136 if (err != ERR_NONE) {
137 goto bail;
138 }
139
140 if (err == ERR_NONE) {
141 if (isConstructor) {
142 expandBufAdd1(pReply, JT_OBJECT);
143 expandBufAddObjectId(pReply, objectId);
144 } else {
Elliott Hughesdbb40792011-11-18 17:05:22 -0800145 size_t width = Dbg::GetTagWidth(resultTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700146
147 expandBufAdd1(pReply, resultTag);
148 if (width != 0) {
149 jdwpWriteValue(pReply, width, resultValue);
150 }
151 }
152 expandBufAdd1(pReply, JT_OBJECT);
153 expandBufAddObjectId(pReply, exceptObjId);
154
155 LOG(VERBOSE) << StringPrintf(" --> returned '%c' 0x%llx (except=%08llx)", resultTag, resultValue, exceptObjId);
156
157 /* show detailed debug output */
158 if (resultTag == JT_STRING && exceptObjId == 0) {
159 if (resultValue != 0) {
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800160 LOG(VERBOSE) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700161 } else {
162 LOG(VERBOSE) << " string (null)";
163 }
164 }
165 }
166
167bail:
168 free(argArray);
169 return err;
170}
171
172
173/*
174 * Request for version info.
175 */
176static JdwpError handleVM_Version(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
177 /* text information on runtime version */
178 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800179 expandBufAddUtf8String(pReply, version);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700180 /* JDWP version numbers */
181 expandBufAdd4BE(pReply, 1); // major
182 expandBufAdd4BE(pReply, 5); // minor
183 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800184 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700185 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800186 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700187
188 return ERR_NONE;
189}
190
191/*
192 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
193 * referenceTypeID. We need to send back more than one if the class has
194 * been loaded by multiple class loaders.
195 */
196static JdwpError handleVM_ClassesBySignature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
197 size_t strLen;
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700198 char* classDescriptor = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199 LOG(VERBOSE) << " Req for class by signature '" << classDescriptor << "'";
200
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800201 std::vector<RefTypeId> ids;
202 Dbg::FindLoadedClassBySignature(classDescriptor, ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700203
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800204 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800206 for (size_t i = 0; i < ids.size(); ++i) {
207 // Get class vs. interface and status flags.
208 JDWP::JdwpTypeTag typeTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700209 uint32_t status;
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800210 Dbg::GetClassInfo(ids[i], &typeTag, &status, NULL);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700211
212 expandBufAdd1(pReply, typeTag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800213 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700214 expandBufAdd4BE(pReply, status);
215 }
216
217 free(classDescriptor);
218
219 return ERR_NONE;
220}
221
222/*
223 * Handle request for the thread IDs of all running threads.
224 *
225 * We exclude ourselves from the list, because we don't allow ourselves
226 * to be suspended, and that violates some JDWP expectations.
227 */
228static JdwpError handleVM_AllThreads(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
229 ObjectId* pThreadIds;
230 uint32_t threadCount;
231 Dbg::GetAllThreads(&pThreadIds, &threadCount);
232
233 expandBufAdd4BE(pReply, threadCount);
234
235 ObjectId* walker = pThreadIds;
236 for (uint32_t i = 0; i < threadCount; i++) {
237 expandBufAddObjectId(pReply, *walker++);
238 }
239
240 free(pThreadIds);
241
242 return ERR_NONE;
243}
244
245/*
246 * List all thread groups that do not have a parent.
247 */
248static JdwpError handleVM_TopLevelThreadGroups(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
249 /*
250 * TODO: maintain a list of parentless thread groups in the VM.
251 *
252 * For now, just return "system". Application threads are created
253 * in "main", which is a child of "system".
254 */
255 uint32_t groups = 1;
256 expandBufAdd4BE(pReply, groups);
257 //threadGroupId = debugGetMainThreadGroup();
258 //expandBufAdd8BE(pReply, threadGroupId);
259 ObjectId threadGroupId = Dbg::GetSystemThreadGroupId();
260 expandBufAddObjectId(pReply, threadGroupId);
261
262 return ERR_NONE;
263}
264
265/*
266 * Respond with the sizes of the basic debugger types.
267 *
268 * All IDs are 8 bytes.
269 */
270static JdwpError handleVM_IDSizes(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
271 expandBufAdd4BE(pReply, sizeof(FieldId));
272 expandBufAdd4BE(pReply, sizeof(MethodId));
273 expandBufAdd4BE(pReply, sizeof(ObjectId));
274 expandBufAdd4BE(pReply, sizeof(RefTypeId));
275 expandBufAdd4BE(pReply, sizeof(FrameId));
276 return ERR_NONE;
277}
278
279/*
280 * The debugger is politely asking to disconnect. We're good with that.
281 *
282 * We could resume threads and clean up pinned references, but we can do
283 * that when the TCP connection drops.
284 */
285static JdwpError handleVM_Dispose(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
286 return ERR_NONE;
287}
288
289/*
290 * Suspend the execution of the application running in the VM (i.e. suspend
291 * all threads).
292 *
293 * This needs to increment the "suspend count" on all threads.
294 */
295static JdwpError handleVM_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700296 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700297 return ERR_NONE;
298}
299
300/*
301 * Resume execution. Decrements the "suspend count" of all threads.
302 */
303static JdwpError handleVM_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
304 Dbg::ResumeVM();
305 return ERR_NONE;
306}
307
308/*
309 * The debugger wants the entire VM to exit.
310 */
311static JdwpError handleVM_Exit(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700312 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700313
314 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
315
316 Dbg::Exit(exitCode);
317 return ERR_NOT_IMPLEMENTED; // shouldn't get here
318}
319
320/*
321 * Create a new string in the VM and return its ID.
322 *
323 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
324 * string "java.util.Arrays".)
325 */
326static JdwpError handleVM_CreateString(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
327 size_t strLen;
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700328 char* str = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700329
330 LOG(VERBOSE) << " Req to create string '" << str << "'";
331
332 ObjectId stringId = Dbg::CreateString(str);
333 if (stringId == 0) {
334 return ERR_OUT_OF_MEMORY;
335 }
336
337 expandBufAddObjectId(pReply, stringId);
338 return ERR_NONE;
339}
340
341/*
342 * Tell the debugger what we are capable of.
343 */
344static JdwpError handleVM_Capabilities(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
345 expandBufAdd1(pReply, false); /* canWatchFieldModification */
346 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
347 expandBufAdd1(pReply, false); /* canGetBytecodes */
348 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
349 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
350 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
351 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
352 return ERR_NONE;
353}
354
355/*
356 * Return classpath and bootclasspath.
357 */
358static JdwpError handleVM_ClassPaths(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
359 char baseDir[2] = "/";
360
361 /*
362 * TODO: make this real. Not important for remote debugging, but
363 * might be useful for local debugging.
364 */
365 uint32_t classPaths = 1;
366 uint32_t bootClassPaths = 0;
367
Elliott Hughesa2155262011-11-16 16:26:58 -0800368 expandBufAddUtf8String(pReply, baseDir);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700369 expandBufAdd4BE(pReply, classPaths);
370 for (uint32_t i = 0; i < classPaths; i++) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800371 expandBufAddUtf8String(pReply, ".");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700372 }
373
374 expandBufAdd4BE(pReply, bootClassPaths);
375 for (uint32_t i = 0; i < classPaths; i++) {
376 /* add bootclasspath components as strings */
377 }
378
379 return ERR_NONE;
380}
381
382/*
383 * Release a list of object IDs. (Seen in jdb.)
384 *
385 * Currently does nothing.
386 */
387static JdwpError HandleVM_DisposeObjects(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
388 return ERR_NONE;
389}
390
391/*
392 * Tell the debugger what we are capable of.
393 */
394static JdwpError handleVM_CapabilitiesNew(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
395 expandBufAdd1(pReply, false); /* canWatchFieldModification */
396 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
397 expandBufAdd1(pReply, false); /* canGetBytecodes */
398 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
399 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
400 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
401 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
402 expandBufAdd1(pReply, false); /* canRedefineClasses */
403 expandBufAdd1(pReply, false); /* canAddMethod */
404 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
405 expandBufAdd1(pReply, false); /* canPopFrames */
406 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
407 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
408 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
409 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
410 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
411 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
412 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
413 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
414 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
415 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
416
417 /* fill in reserved22 through reserved32; note count started at 1 */
418 for (int i = 22; i <= 32; i++) {
419 expandBufAdd1(pReply, false); /* reservedN */
420 }
421 return ERR_NONE;
422}
423
424/*
425 * Cough up the complete list of classes.
426 */
427static JdwpError handleVM_AllClassesWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
428 uint32_t numClasses = 0;
429 RefTypeId* classRefBuf = NULL;
430
431 Dbg::GetClassList(&numClasses, &classRefBuf);
432
433 expandBufAdd4BE(pReply, numClasses);
434
435 for (uint32_t i = 0; i < numClasses; i++) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800436 static const char genericSignature[1] = "";
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800437 JDWP::JdwpTypeTag refTypeTag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800438 std::string descriptor;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700439 uint32_t status;
440
Elliott Hughesa2155262011-11-16 16:26:58 -0800441 Dbg::GetClassInfo(classRefBuf[i], &refTypeTag, &status, &descriptor);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700442
443 expandBufAdd1(pReply, refTypeTag);
444 expandBufAddRefTypeId(pReply, classRefBuf[i]);
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800445 expandBufAddUtf8String(pReply, descriptor);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700446 expandBufAddUtf8String(pReply, genericSignature);
447 expandBufAdd4BE(pReply, status);
448 }
449
450 free(classRefBuf);
451
452 return ERR_NONE;
453}
454
455/*
456 * Given a referenceTypeID, return a string with the JNI reference type
457 * signature (e.g. "Ljava/lang/Error;").
458 */
459static JdwpError handleRT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
460 RefTypeId refTypeId = ReadRefTypeId(&buf);
461
462 LOG(VERBOSE) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800463 std::string signature(Dbg::GetSignature(refTypeId));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800464 expandBufAddUtf8String(pReply, signature);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700465
466 return ERR_NONE;
467}
468
469/*
470 * Return the modifiers (a/k/a access flags) for a reference type.
471 */
472static JdwpError handleRT_Modifiers(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
473 RefTypeId refTypeId = ReadRefTypeId(&buf);
474 uint32_t modBits = Dbg::GetAccessFlags(refTypeId);
475 expandBufAdd4BE(pReply, modBits);
476 return ERR_NONE;
477}
478
479/*
480 * Get values from static fields in a reference type.
481 */
482static JdwpError handleRT_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800483 ReadRefTypeId(&buf); // We don't need this, but we need to skip over it in the request.
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700484 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700485
486 LOG(VERBOSE) << " RT_GetValues " << numFields << ":";
487
488 expandBufAdd4BE(pReply, numFields);
489 for (uint32_t i = 0; i < numFields; i++) {
490 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800491 Dbg::GetStaticFieldValue(fieldId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700492 }
493
494 return ERR_NONE;
495}
496
497/*
498 * Get the name of the source file in which a reference type was declared.
499 */
500static JdwpError handleRT_SourceFile(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
501 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800502 std::string source_file;
503 if (!Dbg::GetSourceFile(refTypeId, source_file)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700504 return ERR_ABSENT_INFORMATION;
505 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800506 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800507 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700508}
509
510/*
511 * Return the current status of the reference type.
512 */
513static JdwpError handleRT_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
514 RefTypeId refTypeId = ReadRefTypeId(&buf);
515
516 /* get status flags */
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800517 JDWP::JdwpTypeTag typeTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700518 uint32_t status;
519 Dbg::GetClassInfo(refTypeId, &typeTag, &status, NULL);
520 expandBufAdd4BE(pReply, status);
521 return ERR_NONE;
522}
523
524/*
525 * Return interfaces implemented directly by this class.
526 */
527static JdwpError handleRT_Interfaces(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
528 RefTypeId refTypeId = ReadRefTypeId(&buf);
529
Elliott Hughesa2155262011-11-16 16:26:58 -0800530 LOG(VERBOSE) << StringPrintf(" Req for interfaces in %llx (%s)", refTypeId, Dbg::GetClassDescriptor(refTypeId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700531
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800532 Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700533
534 return ERR_NONE;
535}
536
537/*
538 * Return the class object corresponding to this type.
539 */
540static JdwpError handleRT_ClassObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
541 RefTypeId refTypeId = ReadRefTypeId(&buf);
542 ObjectId classObjId = Dbg::GetClassObject(refTypeId);
543
544 LOG(VERBOSE) << StringPrintf(" RefTypeId %llx -> ObjectId %llx", refTypeId, classObjId);
545
546 expandBufAddObjectId(pReply, classObjId);
547
548 return ERR_NONE;
549}
550
551/*
552 * Returns the value of the SourceDebugExtension attribute.
553 *
554 * JDB seems interested, but DEX files don't currently support this.
555 */
556static JdwpError handleRT_SourceDebugExtension(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
557 /* referenceTypeId in, string out */
558 return ERR_ABSENT_INFORMATION;
559}
560
561/*
562 * Like RT_Signature but with the possibility of a "generic signature".
563 */
564static JdwpError handleRT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800565 static const char genericSignature[1] = "";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700566
567 RefTypeId refTypeId = ReadRefTypeId(&buf);
568
569 LOG(VERBOSE) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800570 std::string signature(Dbg::GetSignature(refTypeId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700571 if (signature != NULL) {
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800572 expandBufAddUtf8String(pReply, signature);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700573 } else {
574 LOG(WARNING) << StringPrintf("No signature for refTypeId=0x%llx", refTypeId);
Elliott Hughesa2155262011-11-16 16:26:58 -0800575 expandBufAddUtf8String(pReply, "Lunknown;");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700576 }
577 expandBufAddUtf8String(pReply, genericSignature);
578
579 return ERR_NONE;
580}
581
582/*
583 * Return the instance of java.lang.ClassLoader that loaded the specified
584 * reference type, or null if it was loaded by the system loader.
585 */
586static JdwpError handleRT_ClassLoader(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
587 RefTypeId refTypeId = ReadRefTypeId(&buf);
588
589 expandBufAddObjectId(pReply, Dbg::GetClassLoader(refTypeId));
590
591 return ERR_NONE;
592}
593
594/*
595 * Given a referenceTypeId, return a block of stuff that describes the
596 * fields declared by a class.
597 */
598static JdwpError handleRT_FieldsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
599 RefTypeId refTypeId = ReadRefTypeId(&buf);
600 LOG(VERBOSE) << StringPrintf(" Req for fields in refTypeId=0x%llx", refTypeId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800601 LOG(VERBOSE) << StringPrintf(" --> '%s'", Dbg::GetSignature(refTypeId).c_str());
602 Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700603 return ERR_NONE;
604}
605
606/*
607 * Given a referenceTypeID, return a block of goodies describing the
608 * methods declared by a class.
609 */
610static JdwpError handleRT_MethodsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
611 RefTypeId refTypeId = ReadRefTypeId(&buf);
612
613 LOG(VERBOSE) << StringPrintf(" Req for methods in refTypeId=0x%llx", refTypeId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800614 LOG(VERBOSE) << StringPrintf(" --> '%s'", Dbg::GetSignature(refTypeId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700615
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800616 Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617
618 return ERR_NONE;
619}
620
621/*
622 * Return the immediate superclass of a class.
623 */
624static JdwpError handleCT_Superclass(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
625 RefTypeId classId = ReadRefTypeId(&buf);
626
627 RefTypeId superClassId = Dbg::GetSuperclass(classId);
628
629 expandBufAddRefTypeId(pReply, superClassId);
630
631 return ERR_NONE;
632}
633
634/*
635 * Set static class values.
636 */
637static JdwpError handleCT_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
638 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700639 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700640
641 LOG(VERBOSE) << StringPrintf(" Req to set %d values in classId=%llx", values, classId);
642
643 for (uint32_t i = 0; i < values; i++) {
644 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800645 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800646 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700647 uint64_t value = jdwpReadValue(&buf, width);
648
649 LOG(VERBOSE) << StringPrintf(" --> field=%x tag=%c -> %lld", fieldId, fieldTag, value);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800650 Dbg::SetStaticFieldValue(fieldId, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700651 }
652
653 return ERR_NONE;
654}
655
656/*
657 * Invoke a static method.
658 *
659 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
660 * values in the "variables" display.
661 */
662static JdwpError handleCT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
663 RefTypeId classId = ReadRefTypeId(&buf);
664 ObjectId threadId = ReadObjectId(&buf);
665 MethodId methodId = ReadMethodId(&buf);
666
667 return finishInvoke(state, buf, dataLen, pReply, threadId, 0, classId, methodId, false);
668}
669
670/*
671 * Create a new object of the requested type, and invoke the specified
672 * constructor.
673 *
674 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
675 * see the contents of a byte[] as a string.
676 */
677static JdwpError handleCT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
678 RefTypeId classId = ReadRefTypeId(&buf);
679 ObjectId threadId = ReadObjectId(&buf);
680 MethodId methodId = ReadMethodId(&buf);
681
682 LOG(VERBOSE) << "Creating instance of " << Dbg::GetClassDescriptor(classId);
683 ObjectId objectId = Dbg::CreateObject(classId);
684 if (objectId == 0) {
685 return ERR_OUT_OF_MEMORY;
686 }
687 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, true);
688}
689
690/*
691 * Create a new array object of the requested type and length.
692 */
693static JdwpError handleAT_newInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
694 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700695 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700696
Elliott Hughesa2155262011-11-16 16:26:58 -0800697 LOG(VERBOSE) << StringPrintf("Creating array %s[%u]", Dbg::GetClassDescriptor(arrayTypeId).c_str(), length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700698 ObjectId objectId = Dbg::CreateArrayObject(arrayTypeId, length);
699 if (objectId == 0) {
700 return ERR_OUT_OF_MEMORY;
701 }
702 expandBufAdd1(pReply, JT_ARRAY);
703 expandBufAddObjectId(pReply, objectId);
704 return ERR_NONE;
705}
706
707/*
708 * Return line number information for the method, if present.
709 */
710static JdwpError handleM_LineTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
711 RefTypeId refTypeId = ReadRefTypeId(&buf);
712 MethodId methodId = ReadMethodId(&buf);
713
Elliott Hughes03181a82011-11-17 17:22:21 -0800714 LOG(VERBOSE) << StringPrintf(" Req for line table in %s.%s", Dbg::GetClassDescriptor(refTypeId).c_str(), Dbg::GetMethodName(refTypeId,methodId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700715
716 Dbg::OutputLineTable(refTypeId, methodId, pReply);
717
718 return ERR_NONE;
719}
720
721/*
722 * Pull out the LocalVariableTable goodies.
723 */
724static JdwpError handleM_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
725 RefTypeId classId = ReadRefTypeId(&buf);
726 MethodId methodId = ReadMethodId(&buf);
727
Elliott Hughes03181a82011-11-17 17:22:21 -0800728 LOG(VERBOSE) << StringPrintf(" Req for LocalVarTab in class=%s method=%s", Dbg::GetClassDescriptor(classId).c_str(), Dbg::GetMethodName(classId, methodId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700729
730 /*
731 * We could return ERR_ABSENT_INFORMATION here if the DEX file was
732 * built without local variable information. That will cause Eclipse
733 * to make a best-effort attempt at displaying local variables
734 * anonymously. However, the attempt isn't very good, so we're probably
735 * better off just not showing anything.
736 */
737 Dbg::OutputVariableTable(classId, methodId, true, pReply);
738 return ERR_NONE;
739}
740
741/*
742 * Given an object reference, return the runtime type of the object
743 * (class or array).
744 *
745 * This can get called on different things, e.g. threadId gets
746 * passed in here.
747 */
748static JdwpError handleOR_ReferenceType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
749 ObjectId objectId = ReadObjectId(&buf);
750 LOG(VERBOSE) << StringPrintf(" Req for type of objectId=0x%llx", objectId);
751
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800752 JDWP::JdwpTypeTag refTypeTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700753 RefTypeId typeId;
754 Dbg::GetObjectType(objectId, &refTypeTag, &typeId);
755
756 expandBufAdd1(pReply, refTypeTag);
757 expandBufAddRefTypeId(pReply, typeId);
758
759 return ERR_NONE;
760}
761
762/*
763 * Get values from the fields of an object.
764 */
765static JdwpError handleOR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
766 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700767 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700768
769 LOG(VERBOSE) << StringPrintf(" Req for %d fields from objectId=0x%llx", numFields, objectId);
770
771 expandBufAdd4BE(pReply, numFields);
772
773 for (uint32_t i = 0; i < numFields; i++) {
774 FieldId fieldId = ReadFieldId(&buf);
775 Dbg::GetFieldValue(objectId, fieldId, pReply);
776 }
777
778 return ERR_NONE;
779}
780
781/*
782 * Set values in the fields of an object.
783 */
784static JdwpError handleOR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
785 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700786 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700787
788 LOG(VERBOSE) << StringPrintf(" Req to set %d fields in objectId=0x%llx", numFields, objectId);
789
790 for (uint32_t i = 0; i < numFields; i++) {
791 FieldId fieldId = ReadFieldId(&buf);
792
Elliott Hughesaed4be92011-12-02 16:16:23 -0800793 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800794 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700795 uint64_t value = jdwpReadValue(&buf, width);
796
797 LOG(VERBOSE) << StringPrintf(" --> fieldId=%x tag='%c'(%d) value=%lld", fieldId, fieldTag, width, value);
798
799 Dbg::SetFieldValue(objectId, fieldId, value, width);
800 }
801
802 return ERR_NONE;
803}
804
805/*
806 * Invoke an instance method. The invocation must occur in the specified
807 * thread, which must have been suspended by an event.
808 *
809 * The call is synchronous. All threads in the VM are resumed, unless the
810 * SINGLE_THREADED flag is set.
811 *
812 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
813 * object), it will try to invoke the object's toString() function. This
814 * feature becomes crucial when examining ArrayLists with Eclipse.
815 */
816static JdwpError handleOR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
817 ObjectId objectId = ReadObjectId(&buf);
818 ObjectId threadId = ReadObjectId(&buf);
819 RefTypeId classId = ReadRefTypeId(&buf);
820 MethodId methodId = ReadMethodId(&buf);
821
822 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, false);
823}
824
825/*
826 * Disable garbage collection of the specified object.
827 */
828static JdwpError handleOR_DisableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
829 // this is currently a no-op
830 return ERR_NONE;
831}
832
833/*
834 * Enable garbage collection of the specified object.
835 */
836static JdwpError handleOR_EnableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
837 // this is currently a no-op
838 return ERR_NONE;
839}
840
841/*
842 * Determine whether an object has been garbage collected.
843 */
844static JdwpError handleOR_IsCollected(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
845 ObjectId objectId;
846
847 objectId = ReadObjectId(&buf);
848 LOG(VERBOSE) << StringPrintf(" Req IsCollected(0x%llx)", objectId);
849
850 // TODO: currently returning false; must integrate with GC
851 expandBufAdd1(pReply, 0);
852
853 return ERR_NONE;
854}
855
856/*
857 * Return the string value in a string object.
858 */
859static JdwpError handleSR_Value(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
860 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800861 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700862
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800863 LOG(VERBOSE) << StringPrintf(" Req for str %llx --> '%s'", stringObject, str.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700864
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800865 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700866
867 return ERR_NONE;
868}
869
870/*
871 * Return a thread's name.
872 */
873static JdwpError handleTR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
874 ObjectId threadId = ReadObjectId(&buf);
875
876 LOG(VERBOSE) << StringPrintf(" Req for name of thread 0x%llx", threadId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800877 std::string name;
878 if (!Dbg::GetThreadName(threadId, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700879 return ERR_INVALID_THREAD;
880 }
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800881 LOG(VERBOSE) << StringPrintf(" Name of thread 0x%llx is \"%s\"", threadId, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800882 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700883
884 return ERR_NONE;
885}
886
887/*
888 * Suspend the specified thread.
889 *
890 * It's supposed to remain suspended even if interpreted code wants to
891 * resume it; only the JDI is allowed to resume it.
892 */
893static JdwpError handleTR_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
894 ObjectId threadId = ReadObjectId(&buf);
895
896 if (threadId == Dbg::GetThreadSelfId()) {
897 LOG(INFO) << " Warning: ignoring request to suspend self";
898 return ERR_THREAD_NOT_SUSPENDED;
899 }
900 LOG(VERBOSE) << StringPrintf(" Req to suspend thread 0x%llx", threadId);
901 Dbg::SuspendThread(threadId);
902 return ERR_NONE;
903}
904
905/*
906 * Resume the specified thread.
907 */
908static JdwpError handleTR_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
909 ObjectId threadId = ReadObjectId(&buf);
910
911 if (threadId == Dbg::GetThreadSelfId()) {
912 LOG(INFO) << " Warning: ignoring request to resume self";
913 return ERR_NONE;
914 }
915 LOG(VERBOSE) << StringPrintf(" Req to resume thread 0x%llx", threadId);
916 Dbg::ResumeThread(threadId);
917 return ERR_NONE;
918}
919
920/*
921 * Return status of specified thread.
922 */
923static JdwpError handleTR_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
924 ObjectId threadId = ReadObjectId(&buf);
925
926 LOG(VERBOSE) << StringPrintf(" Req for status of thread 0x%llx", threadId);
927
928 uint32_t threadStatus;
929 uint32_t suspendStatus;
930 if (!Dbg::GetThreadStatus(threadId, &threadStatus, &suspendStatus)) {
931 return ERR_INVALID_THREAD;
932 }
933
934 LOG(VERBOSE) << " --> " << JdwpThreadStatus(threadStatus) << ", " << JdwpSuspendStatus(suspendStatus);
935
936 expandBufAdd4BE(pReply, threadStatus);
937 expandBufAdd4BE(pReply, suspendStatus);
938
939 return ERR_NONE;
940}
941
942/*
943 * Return the thread group that the specified thread is a member of.
944 */
945static JdwpError handleTR_ThreadGroup(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
946 ObjectId threadId = ReadObjectId(&buf);
947
948 /* currently not handling these */
949 ObjectId threadGroupId = Dbg::GetThreadGroup(threadId);
950 expandBufAddObjectId(pReply, threadGroupId);
951
952 return ERR_NONE;
953}
954
955/*
956 * Return the current call stack of a suspended thread.
957 *
958 * If the thread isn't suspended, the error code isn't defined, but should
959 * be THREAD_NOT_SUSPENDED.
960 */
961static JdwpError handleTR_Frames(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
962 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700963 uint32_t startFrame = Read4BE(&buf);
964 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700965
966 if (!Dbg::ThreadExists(threadId)) {
967 return ERR_INVALID_THREAD;
968 }
969 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800970 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700971 return ERR_THREAD_NOT_SUSPENDED;
972 }
973
Elliott Hughes761928d2011-11-16 18:33:03 -0800974 size_t frameCount = Dbg::GetThreadFrameCount(threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700975
976 LOG(VERBOSE) << StringPrintf(" Request for frames: threadId=%llx start=%d length=%d [count=%d]", threadId, startFrame, length, frameCount);
977 if (frameCount <= 0) {
978 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
979 }
980 if (length == (uint32_t) -1) {
981 length = frameCount;
982 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800983 CHECK_GE(startFrame, 0U);
984 CHECK_LT(startFrame, frameCount);
985 CHECK_LE(startFrame + length, frameCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700986
987 uint32_t frames = length;
988 expandBufAdd4BE(pReply, frames);
989 for (uint32_t i = startFrame; i < (startFrame+length); i++) {
990 FrameId frameId;
991 JdwpLocation loc;
992
993 Dbg::GetThreadFrame(threadId, i, &frameId, &loc);
994
995 expandBufAdd8BE(pReply, frameId);
996 AddLocation(pReply, &loc);
997
998 LOG(VERBOSE) << StringPrintf(" Frame %d: id=%llx loc={type=%d cls=%llx mth=%x loc=%llx}", i, frameId, loc.typeTag, loc.classId, loc.methodId, loc.idx);
999 }
1000
1001 return ERR_NONE;
1002}
1003
1004/*
1005 * Returns the #of frames on the specified thread, which must be suspended.
1006 */
1007static JdwpError handleTR_FrameCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1008 ObjectId threadId = ReadObjectId(&buf);
1009
1010 if (!Dbg::ThreadExists(threadId)) {
1011 return ERR_INVALID_THREAD;
1012 }
1013 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001014 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001015 return ERR_THREAD_NOT_SUSPENDED;
1016 }
1017
1018 int frameCount = Dbg::GetThreadFrameCount(threadId);
1019 if (frameCount < 0) {
1020 return ERR_INVALID_THREAD;
1021 }
1022 expandBufAdd4BE(pReply, (uint32_t)frameCount);
1023
1024 return ERR_NONE;
1025}
1026
1027/*
1028 * Get the monitor that the thread is waiting on.
1029 */
1030static JdwpError handleTR_CurrentContendedMonitor(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1031 ObjectId threadId;
1032
1033 threadId = ReadObjectId(&buf);
1034
1035 // TODO: create an Object to represent the monitor (we're currently
1036 // just using a raw Monitor struct in the VM)
1037
1038 return ERR_NOT_IMPLEMENTED;
1039}
1040
1041/*
1042 * Return the suspend count for the specified thread.
1043 *
1044 * (The thread *might* still be running -- it might not have examined
1045 * its suspend count recently.)
1046 */
1047static JdwpError handleTR_SuspendCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1048 ObjectId threadId = ReadObjectId(&buf);
1049
1050 uint32_t suspendCount = Dbg::GetThreadSuspendCount(threadId);
1051 expandBufAdd4BE(pReply, suspendCount);
1052
1053 return ERR_NONE;
1054}
1055
1056/*
1057 * Return the name of a thread group.
1058 *
1059 * The Eclipse debugger recognizes "main" and "system" as special.
1060 */
1061static JdwpError handleTGR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1062 ObjectId threadGroupId = ReadObjectId(&buf);
1063 LOG(VERBOSE) << StringPrintf(" Req for name of threadGroupId=0x%llx", threadGroupId);
1064
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001065 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(threadGroupId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001066
1067 return ERR_NONE;
1068}
1069
1070/*
1071 * Returns the thread group -- if any -- that contains the specified
1072 * thread group.
1073 */
1074static JdwpError handleTGR_Parent(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1075 ObjectId groupId = ReadObjectId(&buf);
1076
1077 ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId);
1078 expandBufAddObjectId(pReply, parentGroup);
1079
1080 return ERR_NONE;
1081}
1082
1083/*
1084 * Return the active threads and thread groups that are part of the
1085 * specified thread group.
1086 */
1087static JdwpError handleTGR_Children(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1088 ObjectId threadGroupId = ReadObjectId(&buf);
1089 LOG(VERBOSE) << StringPrintf(" Req for threads in threadGroupId=0x%llx", threadGroupId);
1090
1091 ObjectId* pThreadIds;
1092 uint32_t threadCount;
1093 Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount);
1094
1095 expandBufAdd4BE(pReply, threadCount);
1096
1097 for (uint32_t i = 0; i < threadCount; i++) {
1098 expandBufAddObjectId(pReply, pThreadIds[i]);
1099 }
1100 free(pThreadIds);
1101
1102 /*
1103 * TODO: finish support for child groups
1104 *
1105 * For now, just show that "main" is a child of "system".
1106 */
1107 if (threadGroupId == Dbg::GetSystemThreadGroupId()) {
1108 expandBufAdd4BE(pReply, 1);
1109 expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId());
1110 } else {
1111 expandBufAdd4BE(pReply, 0);
1112 }
1113
1114 return ERR_NONE;
1115}
1116
1117/*
1118 * Return the #of components in the array.
1119 */
1120static JdwpError handleAR_Length(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1121 ObjectId arrayId = ReadObjectId(&buf);
1122 LOG(VERBOSE) << StringPrintf(" Req for length of array 0x%llx", arrayId);
1123
1124 uint32_t arrayLength = Dbg::GetArrayLength(arrayId);
1125
1126 LOG(VERBOSE) << StringPrintf(" --> %d", arrayLength);
1127
1128 expandBufAdd4BE(pReply, arrayLength);
1129
1130 return ERR_NONE;
1131}
1132
1133/*
1134 * Return the values from an array.
1135 */
1136static JdwpError handleAR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1137 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001138 uint32_t firstIndex = Read4BE(&buf);
1139 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001140
1141 uint8_t tag = Dbg::GetArrayElementTag(arrayId);
1142 LOG(VERBOSE) << StringPrintf(" Req for array values 0x%llx first=%d len=%d (elem tag=%c)", arrayId, firstIndex, length, tag);
1143
1144 expandBufAdd1(pReply, tag);
1145 expandBufAdd4BE(pReply, length);
1146
1147 if (!Dbg::OutputArray(arrayId, firstIndex, length, pReply)) {
1148 return ERR_INVALID_LENGTH;
1149 }
1150
1151 return ERR_NONE;
1152}
1153
1154/*
1155 * Set values in an array.
1156 */
1157static JdwpError handleAR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1158 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001159 uint32_t firstIndex = Read4BE(&buf);
1160 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001161
1162 LOG(VERBOSE) << StringPrintf(" Req to set array values 0x%llx first=%d count=%d", arrayId, firstIndex, values);
1163
1164 if (!Dbg::SetArrayElements(arrayId, firstIndex, values, buf)) {
1165 return ERR_INVALID_LENGTH;
1166 }
1167
1168 return ERR_NONE;
1169}
1170
1171/*
1172 * Return the set of classes visible to a class loader. All classes which
1173 * have the class loader as a defining or initiating loader are returned.
1174 */
1175static JdwpError handleCLR_VisibleClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1176 ObjectId classLoaderObject;
1177 uint32_t numClasses = 0;
1178 RefTypeId* classRefBuf = NULL;
1179 int i;
1180
1181 classLoaderObject = ReadObjectId(&buf);
1182
1183 Dbg::GetVisibleClassList(classLoaderObject, &numClasses, &classRefBuf);
1184
1185 expandBufAdd4BE(pReply, numClasses);
1186 for (i = 0; i < (int) numClasses; i++) {
1187 uint8_t refTypeTag = Dbg::GetClassObjectType(classRefBuf[i]);
1188
1189 expandBufAdd1(pReply, refTypeTag);
1190 expandBufAddRefTypeId(pReply, classRefBuf[i]);
1191 }
1192
1193 return ERR_NONE;
1194}
1195
1196/*
1197 * Return a newly-allocated string in which all occurrences of '.' have
1198 * been changed to '/'. If we find a '/' in the original string, NULL
1199 * is returned to avoid ambiguity.
1200 */
1201char* dvmDotToSlash(const char* str) {
1202 char* newStr = strdup(str);
1203 char* cp = newStr;
1204
1205 if (newStr == NULL) {
1206 return NULL;
1207 }
1208
1209 while (*cp != '\0') {
1210 if (*cp == '/') {
1211 CHECK(false);
1212 return NULL;
1213 }
1214 if (*cp == '.') {
1215 *cp = '/';
1216 }
1217 cp++;
1218 }
1219
1220 return newStr;
1221}
1222
1223/*
1224 * Set an event trigger.
1225 *
1226 * Reply with a requestID.
1227 */
1228static JdwpError handleER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1229 const uint8_t* origBuf = buf;
1230
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001231 uint8_t eventKind = Read1(&buf);
1232 uint8_t suspendPolicy = Read1(&buf);
1233 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001234
1235 LOG(VERBOSE) << " Set(kind=" << JdwpEventKind(eventKind)
1236 << " suspend=" << JdwpSuspendPolicy(suspendPolicy)
1237 << " mods=" << modifierCount << ")";
1238
1239 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1240
1241 JdwpEvent* pEvent = EventAlloc(modifierCount);
1242 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
1243 pEvent->suspendPolicy = static_cast<JdwpSuspendPolicy>(suspendPolicy);
1244 pEvent->modCount = modifierCount;
1245
1246 /*
1247 * Read modifiers. Ordering may be significant (see explanation of Count
1248 * mods in JDWP doc).
1249 */
1250 for (uint32_t idx = 0; idx < modifierCount; idx++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001251 uint8_t modKind = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001252
1253 pEvent->mods[idx].modKind = modKind;
1254
1255 switch (modKind) {
1256 case MK_COUNT: /* report once, when "--count" reaches 0 */
1257 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001258 uint32_t count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001259 LOG(VERBOSE) << " Count: " << count;
1260 if (count == 0) {
1261 return ERR_INVALID_COUNT;
1262 }
1263 pEvent->mods[idx].count.count = count;
1264 }
1265 break;
1266 case MK_CONDITIONAL: /* conditional on expression) */
1267 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001268 uint32_t exprId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001269 LOG(VERBOSE) << " Conditional: " << exprId;
1270 pEvent->mods[idx].conditional.exprId = exprId;
1271 }
1272 break;
1273 case MK_THREAD_ONLY: /* only report events in specified thread */
1274 {
1275 ObjectId threadId = ReadObjectId(&buf);
1276 LOG(VERBOSE) << StringPrintf(" ThreadOnly: %llx", threadId);
1277 pEvent->mods[idx].threadOnly.threadId = threadId;
1278 }
1279 break;
1280 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1281 {
1282 RefTypeId clazzId = ReadRefTypeId(&buf);
Elliott Hughesa2155262011-11-16 16:26:58 -08001283 LOG(VERBOSE) << StringPrintf(" ClassOnly: %llx (%s)", clazzId, Dbg::GetClassDescriptor(clazzId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001284 pEvent->mods[idx].classOnly.refTypeId = clazzId;
1285 }
1286 break;
1287 case MK_CLASS_MATCH: /* restrict events to matching classes */
1288 {
1289 char* pattern;
1290 size_t strLen;
1291
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001292 pattern = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001293 LOG(VERBOSE) << StringPrintf(" ClassMatch: '%s'", pattern);
1294 /* pattern is "java.foo.*", we want "java/foo/ *" */
1295 pEvent->mods[idx].classMatch.classPattern = dvmDotToSlash(pattern);
1296 free(pattern);
1297 }
1298 break;
1299 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1300 {
1301 char* pattern;
1302 size_t strLen;
1303
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001304 pattern = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001305 LOG(VERBOSE) << StringPrintf(" ClassExclude: '%s'", pattern);
1306 pEvent->mods[idx].classExclude.classPattern = dvmDotToSlash(pattern);
1307 free(pattern);
1308 }
1309 break;
1310 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1311 {
1312 JdwpLocation loc;
1313
1314 jdwpReadLocation(&buf, &loc);
1315 LOG(VERBOSE) << StringPrintf(" LocationOnly: typeTag=%d classId=%llx methodId=%x idx=%llx",
1316 loc.typeTag, loc.classId, loc.methodId, loc.idx);
1317 pEvent->mods[idx].locationOnly.loc = loc;
1318 }
1319 break;
1320 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1321 {
1322 RefTypeId exceptionOrNull; /* null == all exceptions */
1323 uint8_t caught, uncaught;
1324
1325 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001326 caught = Read1(&buf);
1327 uncaught = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001328 LOG(VERBOSE) << StringPrintf(" ExceptionOnly: type=%llx(%s) caught=%d uncaught=%d",
Elliott Hughesa2155262011-11-16 16:26:58 -08001329 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassDescriptor(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001330
1331 pEvent->mods[idx].exceptionOnly.refTypeId = exceptionOrNull;
1332 pEvent->mods[idx].exceptionOnly.caught = caught;
1333 pEvent->mods[idx].exceptionOnly.uncaught = uncaught;
1334 }
1335 break;
1336 case MK_FIELD_ONLY: /* for field access/mod events */
1337 {
1338 RefTypeId declaring = ReadRefTypeId(&buf);
1339 FieldId fieldId = ReadFieldId(&buf);
1340 LOG(VERBOSE) << StringPrintf(" FieldOnly: %llx %x", declaring, fieldId);
1341 pEvent->mods[idx].fieldOnly.refTypeId = declaring;
1342 pEvent->mods[idx].fieldOnly.fieldId = fieldId;
1343 }
1344 break;
1345 case MK_STEP: /* for use with EK_SINGLE_STEP */
1346 {
1347 ObjectId threadId;
1348 uint32_t size, depth;
1349
1350 threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001351 size = Read4BE(&buf);
1352 depth = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001353 LOG(VERBOSE) << StringPrintf(" Step: thread=%llx", threadId)
1354 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1355
1356 pEvent->mods[idx].step.threadId = threadId;
1357 pEvent->mods[idx].step.size = size;
1358 pEvent->mods[idx].step.depth = depth;
1359 }
1360 break;
1361 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1362 {
1363 ObjectId instance = ReadObjectId(&buf);
1364 LOG(VERBOSE) << StringPrintf(" InstanceOnly: %llx", instance);
1365 pEvent->mods[idx].instanceOnly.objectId = instance;
1366 }
1367 break;
1368 default:
1369 LOG(WARNING) << "GLITCH: unsupported modKind=" << modKind;
1370 break;
1371 }
1372 }
1373
1374 /*
1375 * Make sure we consumed all data. It is possible that the remote side
1376 * has sent us bad stuff, but for now we blame ourselves.
1377 */
1378 if (buf != origBuf + dataLen) {
1379 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1380 }
1381
1382 /*
1383 * We reply with an integer "requestID".
1384 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001385 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001386 expandBufAdd4BE(pReply, requestId);
1387
1388 pEvent->requestId = requestId;
1389
1390 LOG(VERBOSE) << StringPrintf(" --> event requestId=%#x", requestId);
1391
1392 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001393 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001394 if (err != ERR_NONE) {
1395 /* registration failed, probably because event is bogus */
1396 EventFree(pEvent);
1397 LOG(WARNING) << "WARNING: event request rejected";
1398 }
1399 return err;
1400}
1401
1402/*
1403 * Clear an event. Failure to find an event with a matching ID is a no-op
1404 * and does not return an error.
1405 */
1406static JdwpError handleER_Clear(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1407 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001408 eventKind = Read1(&buf);
1409 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001410
1411 LOG(VERBOSE) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
1412
Elliott Hughes761928d2011-11-16 18:33:03 -08001413 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001414
1415 return ERR_NONE;
1416}
1417
1418/*
1419 * Return the values of arguments and local variables.
1420 */
1421static JdwpError handleSF_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1422 ObjectId threadId = ReadObjectId(&buf);
1423 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001424 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001425
1426 LOG(VERBOSE) << StringPrintf(" Req for %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
1427
1428 expandBufAdd4BE(pReply, slots); /* "int values" */
1429 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001430 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001431 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001432
1433 LOG(VERBOSE) << StringPrintf(" --> slot %d '%c'", slot, reqSigByte);
1434
Elliott Hughesdbb40792011-11-18 17:05:22 -08001435 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001436 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
1437 Dbg::GetLocalValue(threadId, frameId, slot, reqSigByte, ptr, width);
1438 }
1439
1440 return ERR_NONE;
1441}
1442
1443/*
1444 * Set the values of arguments and local variables.
1445 */
1446static JdwpError handleSF_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1447 ObjectId threadId = ReadObjectId(&buf);
1448 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001449 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001450
1451 LOG(VERBOSE) << StringPrintf(" Req to set %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
1452
1453 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001454 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001455 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001456 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001457 uint64_t value = jdwpReadValue(&buf, width);
1458
1459 LOG(VERBOSE) << StringPrintf(" --> slot %d '%c' %llx", slot, sigByte, value);
1460 Dbg::SetLocalValue(threadId, frameId, slot, sigByte, value, width);
1461 }
1462
1463 return ERR_NONE;
1464}
1465
1466/*
1467 * Returns the value of "this" for the specified frame.
1468 */
1469static JdwpError handleSF_ThisObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesd07986f2011-12-06 18:27:45 -08001470 ReadObjectId(&buf); // Skip thread id.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001471 FrameId frameId = ReadFrameId(&buf);
1472
1473 ObjectId objectId;
Elliott Hughesd07986f2011-12-06 18:27:45 -08001474 if (!Dbg::GetThisObject(frameId, &objectId)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001475 return ERR_INVALID_FRAMEID;
1476 }
1477
1478 uint8_t objectTag = Dbg::GetObjectTag(objectId);
Elliott Hughesd07986f2011-12-06 18:27:45 -08001479 LOG(VERBOSE) << StringPrintf(" Req for 'this' in frame=%llx --> %llx '%c'", frameId, objectId, (char)objectTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001480
1481 expandBufAdd1(pReply, objectTag);
1482 expandBufAddObjectId(pReply, objectId);
1483
1484 return ERR_NONE;
1485}
1486
1487/*
1488 * Return the reference type reflected by this class object.
1489 *
1490 * This appears to be required because ReferenceTypeId values are NEVER
1491 * reused, whereas ClassIds can be recycled like any other object. (Either
1492 * that, or I have no idea what this is for.)
1493 */
1494static JdwpError handleCOR_ReflectedType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1495 RefTypeId classObjectId = ReadRefTypeId(&buf);
1496
Elliott Hughesa2155262011-11-16 16:26:58 -08001497 LOG(VERBOSE) << StringPrintf(" Req for refTypeId for class=%llx (%s)", classObjectId, Dbg::GetClassDescriptor(classObjectId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001498
1499 /* just hand the type back to them */
1500 if (Dbg::IsInterface(classObjectId)) {
1501 expandBufAdd1(pReply, TT_INTERFACE);
1502 } else {
1503 expandBufAdd1(pReply, TT_CLASS);
1504 }
1505 expandBufAddRefTypeId(pReply, classObjectId);
1506
1507 return ERR_NONE;
1508}
1509
1510/*
1511 * Handle a DDM packet with a single chunk in it.
1512 */
1513static JdwpError handleDDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1514 uint8_t* replyBuf = NULL;
1515 int replyLen = -1;
1516
1517 LOG(VERBOSE) << StringPrintf(" Handling DDM packet (%.4s)", buf);
1518
1519 /*
1520 * On first DDM packet, notify all handlers that DDM is running.
1521 */
1522 if (!state->ddmActive) {
1523 state->ddmActive = true;
1524 Dbg::DdmConnected();
1525 }
1526
1527 /*
1528 * If they want to send something back, we copy it into the buffer.
1529 * A no-copy approach would be nicer.
1530 *
1531 * TODO: consider altering the JDWP stuff to hold the packet header
1532 * in a separate buffer. That would allow us to writev() DDM traffic
1533 * instead of copying it into the expanding buffer. The reduction in
1534 * heap requirements is probably more valuable than the efficiency.
1535 */
1536 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1537 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1538 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1539 free(replyBuf);
1540 }
1541 return ERR_NONE;
1542}
1543
1544/*
1545 * Handler map decl.
1546 */
1547typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1548
1549struct JdwpHandlerMap {
1550 uint8_t cmdSet;
1551 uint8_t cmd;
1552 JdwpRequestHandler func;
1553 const char* descr;
1554};
1555
1556/*
1557 * Map commands to functions.
1558 *
1559 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1560 * and 128-256 are vendor-defined.
1561 */
1562static const JdwpHandlerMap gHandlerMap[] = {
1563 /* VirtualMachine command set (1) */
1564 { 1, 1, handleVM_Version, "VirtualMachine.Version" },
1565 { 1, 2, handleVM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1566 //1, 3, VirtualMachine.AllClasses
1567 { 1, 4, handleVM_AllThreads, "VirtualMachine.AllThreads" },
1568 { 1, 5, handleVM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1569 { 1, 6, handleVM_Dispose, "VirtualMachine.Dispose" },
1570 { 1, 7, handleVM_IDSizes, "VirtualMachine.IDSizes" },
1571 { 1, 8, handleVM_Suspend, "VirtualMachine.Suspend" },
1572 { 1, 9, handleVM_Resume, "VirtualMachine.Resume" },
1573 { 1, 10, handleVM_Exit, "VirtualMachine.Exit" },
1574 { 1, 11, handleVM_CreateString, "VirtualMachine.CreateString" },
1575 { 1, 12, handleVM_Capabilities, "VirtualMachine.Capabilities" },
1576 { 1, 13, handleVM_ClassPaths, "VirtualMachine.ClassPaths" },
1577 { 1, 14, HandleVM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1578 //1, 15, HoldEvents
1579 //1, 16, ReleaseEvents
1580 { 1, 17, handleVM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1581 //1, 18, RedefineClasses
1582 //1, 19, SetDefaultStratum
1583 { 1, 20, handleVM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric"},
1584 //1, 21, InstanceCounts
1585
1586 /* ReferenceType command set (2) */
1587 { 2, 1, handleRT_Signature, "ReferenceType.Signature" },
1588 { 2, 2, handleRT_ClassLoader, "ReferenceType.ClassLoader" },
1589 { 2, 3, handleRT_Modifiers, "ReferenceType.Modifiers" },
1590 //2, 4, Fields
1591 //2, 5, Methods
1592 { 2, 6, handleRT_GetValues, "ReferenceType.GetValues" },
1593 { 2, 7, handleRT_SourceFile, "ReferenceType.SourceFile" },
1594 //2, 8, NestedTypes
1595 { 2, 9, handleRT_Status, "ReferenceType.Status" },
1596 { 2, 10, handleRT_Interfaces, "ReferenceType.Interfaces" },
1597 { 2, 11, handleRT_ClassObject, "ReferenceType.ClassObject" },
1598 { 2, 12, handleRT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1599 { 2, 13, handleRT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1600 { 2, 14, handleRT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1601 { 2, 15, handleRT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
1602 //2, 16, Instances
1603 //2, 17, ClassFileVersion
1604 //2, 18, ConstantPool
1605
1606 /* ClassType command set (3) */
1607 { 3, 1, handleCT_Superclass, "ClassType.Superclass" },
1608 { 3, 2, handleCT_SetValues, "ClassType.SetValues" },
1609 { 3, 3, handleCT_InvokeMethod, "ClassType.InvokeMethod" },
1610 { 3, 4, handleCT_NewInstance, "ClassType.NewInstance" },
1611
1612 /* ArrayType command set (4) */
1613 { 4, 1, handleAT_newInstance, "ArrayType.NewInstance" },
1614
1615 /* InterfaceType command set (5) */
1616
1617 /* Method command set (6) */
1618 { 6, 1, handleM_LineTable, "Method.LineTable" },
1619 //6, 2, VariableTable
1620 //6, 3, Bytecodes
1621 //6, 4, IsObsolete
1622 { 6, 5, handleM_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
1623
1624 /* Field command set (8) */
1625
1626 /* ObjectReference command set (9) */
1627 { 9, 1, handleOR_ReferenceType, "ObjectReference.ReferenceType" },
1628 { 9, 2, handleOR_GetValues, "ObjectReference.GetValues" },
1629 { 9, 3, handleOR_SetValues, "ObjectReference.SetValues" },
1630 //9, 4, (not defined)
1631 //9, 5, MonitorInfo
1632 { 9, 6, handleOR_InvokeMethod, "ObjectReference.InvokeMethod" },
1633 { 9, 7, handleOR_DisableCollection, "ObjectReference.DisableCollection" },
1634 { 9, 8, handleOR_EnableCollection, "ObjectReference.EnableCollection" },
1635 { 9, 9, handleOR_IsCollected, "ObjectReference.IsCollected" },
1636 //9, 10, ReferringObjects
1637
1638 /* StringReference command set (10) */
1639 { 10, 1, handleSR_Value, "StringReference.Value" },
1640
1641 /* ThreadReference command set (11) */
1642 { 11, 1, handleTR_Name, "ThreadReference.Name" },
1643 { 11, 2, handleTR_Suspend, "ThreadReference.Suspend" },
1644 { 11, 3, handleTR_Resume, "ThreadReference.Resume" },
1645 { 11, 4, handleTR_Status, "ThreadReference.Status" },
1646 { 11, 5, handleTR_ThreadGroup, "ThreadReference.ThreadGroup" },
1647 { 11, 6, handleTR_Frames, "ThreadReference.Frames" },
1648 { 11, 7, handleTR_FrameCount, "ThreadReference.FrameCount" },
1649 //11, 8, OwnedMonitors
1650 { 11, 9, handleTR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1651 //11, 10, Stop
1652 //11, 11, Interrupt
1653 { 11, 12, handleTR_SuspendCount, "ThreadReference.SuspendCount" },
1654 //11, 13, OwnedMonitorsStackDepthInfo
1655 //11, 14, ForceEarlyReturn
1656
1657 /* ThreadGroupReference command set (12) */
1658 { 12, 1, handleTGR_Name, "ThreadGroupReference.Name" },
1659 { 12, 2, handleTGR_Parent, "ThreadGroupReference.Parent" },
1660 { 12, 3, handleTGR_Children, "ThreadGroupReference.Children" },
1661
1662 /* ArrayReference command set (13) */
1663 { 13, 1, handleAR_Length, "ArrayReference.Length" },
1664 { 13, 2, handleAR_GetValues, "ArrayReference.GetValues" },
1665 { 13, 3, handleAR_SetValues, "ArrayReference.SetValues" },
1666
1667 /* ClassLoaderReference command set (14) */
1668 { 14, 1, handleCLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
1669
1670 /* EventRequest command set (15) */
1671 { 15, 1, handleER_Set, "EventRequest.Set" },
1672 { 15, 2, handleER_Clear, "EventRequest.Clear" },
1673 //15, 3, ClearAllBreakpoints
1674
1675 /* StackFrame command set (16) */
1676 { 16, 1, handleSF_GetValues, "StackFrame.GetValues" },
1677 { 16, 2, handleSF_SetValues, "StackFrame.SetValues" },
1678 { 16, 3, handleSF_ThisObject, "StackFrame.ThisObject" },
1679 //16, 4, PopFrames
1680
1681 /* ClassObjectReference command set (17) */
1682 { 17, 1, handleCOR_ReflectedType,"ClassObjectReference.ReflectedType" },
1683
1684 /* Event command set (64) */
1685 //64, 100, Composite <-- sent from VM to debugger, never received by VM
1686
1687 { 199, 1, handleDDM_Chunk, "DDM.Chunk" },
1688};
1689
1690/*
1691 * Process a request from the debugger.
1692 *
1693 * On entry, the JDWP thread is in VMWAIT.
1694 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001695void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001696 JdwpError result = ERR_NONE;
1697 int i, respLen;
1698
1699 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1700 /*
1701 * Activity from a debugger, not merely ddms. Mark us as having an
1702 * active debugger session, and zero out the last-activity timestamp
1703 * so waitForDebugger() doesn't return if we stall for a bit here.
1704 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001705 Dbg::GoActive();
Elliott Hughes376a7a02011-10-24 18:35:55 -07001706 QuasiAtomicSwap64(0, &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001707 }
1708
1709 /*
1710 * If a debugger event has fired in another thread, wait until the
1711 * initiating thread has suspended itself before processing messages
1712 * from the debugger. Otherwise we (the JDWP thread) could be told to
1713 * resume the thread before it has suspended.
1714 *
1715 * We call with an argument of zero to wait for the current event
1716 * thread to finish, and then clear the block. Depending on the thread
1717 * suspend policy, this may allow events in other threads to fire,
1718 * but those events have no bearing on what the debugger has sent us
1719 * in the current request.
1720 *
1721 * Note that we MUST clear the event token before waking the event
1722 * thread up, or risk waiting for the thread to suspend after we've
1723 * told it to resume.
1724 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001725 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001726
1727 /*
1728 * Tell the VM that we're running and shouldn't be interrupted by GC.
1729 * Do this after anything that can stall indefinitely.
1730 */
1731 Dbg::ThreadRunning();
1732
1733 expandBufAddSpace(pReply, kJDWPHeaderLen);
1734
1735 for (i = 0; i < (int) arraysize(gHandlerMap); i++) {
1736 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd) {
1737 LOG(VERBOSE) << StringPrintf("REQ: %s (cmd=%d/%d dataLen=%d id=0x%06x)", gHandlerMap[i].descr, pHeader->cmdSet, pHeader->cmd, dataLen, pHeader->id);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001738 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001739 break;
1740 }
1741 }
1742 if (i == arraysize(gHandlerMap)) {
1743 LOG(ERROR) << StringPrintf("REQ: UNSUPPORTED (cmd=%d/%d dataLen=%d id=0x%06x)", pHeader->cmdSet, pHeader->cmd, dataLen, pHeader->id);
1744 if (dataLen > 0) {
1745 HexDump(buf, dataLen);
1746 }
1747 LOG(FATAL) << "command not implemented"; // make it *really* obvious
1748 result = ERR_NOT_IMPLEMENTED;
1749 }
1750
1751 /*
1752 * Set up the reply header.
1753 *
1754 * If we encountered an error, only send the header back.
1755 */
1756 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001757 Set4BE(replyBuf + 4, pHeader->id);
1758 Set1(replyBuf + 8, kJDWPFlagReply);
1759 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001760 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001761 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001762 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001763 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001764 }
1765
1766 respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
1767 if (false) {
1768 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
1769 if (respLen > 0) {
1770 HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
1771 }
1772 }
1773
1774 /*
1775 * Update last-activity timestamp. We really only need this during
1776 * the initial setup. Only update if this is a non-DDMS packet.
1777 */
1778 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07001779 QuasiAtomicSwap64(MilliTime(), &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001780 }
1781
1782 /* tell the VM that GC is okay again */
1783 Dbg::ThreadWaiting();
1784}
1785
1786} // namespace JDWP
1787
1788} // namespace art