blob: e5e18bb285eae8bf76b236095a4d02b504ebc97c [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 Hughesf7c3b662011-10-27 12:04:56 -070052 pLoc->typeTag = Read1(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 */
71static uint64_t jdwpReadValue(const uint8_t** pBuf, int width) {
72 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 Hughesa2155262011-11-16 16:26:58 -0800112 LOG(VERBOSE) << StringPrintf(" classId=%llx methodId=%x %s.%s", classId, methodId, Dbg::GetClassDescriptor(classId).c_str(), Dbg::GetMethodName(classId, methodId));
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 Hughesf7c3b662011-10-27 12:04:56 -0700121 uint8_t typeTag = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700122 int width = Dbg::GetTagWidth(typeTag);
123 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
132 uint8_t resultTag;
133 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 {
145 int width = Dbg::GetTagWidth(resultTag);
146
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) {
160 char* str = Dbg::StringToUtf8(resultValue);
161 LOG(VERBOSE) << StringPrintf(" string '%s'", str);
162 free(str);
163 } else {
164 LOG(VERBOSE) << " string (null)";
165 }
166 }
167 }
168
169bail:
170 free(argArray);
171 return err;
172}
173
174
175/*
176 * Request for version info.
177 */
178static JdwpError handleVM_Version(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
179 /* text information on runtime version */
180 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughesa2155262011-11-16 16:26:58 -0800181 expandBufAddUtf8String(pReply, version.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700182 /* JDWP version numbers */
183 expandBufAdd4BE(pReply, 1); // major
184 expandBufAdd4BE(pReply, 5); // minor
185 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800186 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700187 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800188 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189
190 return ERR_NONE;
191}
192
193/*
194 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
195 * referenceTypeID. We need to send back more than one if the class has
196 * been loaded by multiple class loaders.
197 */
198static JdwpError handleVM_ClassesBySignature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
199 size_t strLen;
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700200 char* classDescriptor = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700201 LOG(VERBOSE) << " Req for class by signature '" << classDescriptor << "'";
202
203 /*
204 * TODO: if a class with the same name has been loaded multiple times
205 * (by different class loaders), we're supposed to return each of them.
206 *
207 * NOTE: this may mangle "className".
208 */
209 uint32_t numClasses;
210 RefTypeId refTypeId;
211 if (!Dbg::FindLoadedClassBySignature(classDescriptor, &refTypeId)) {
212 /* not currently loaded */
213 LOG(VERBOSE) << " --> no match!";
214 numClasses = 0;
215 } else {
216 /* just the one */
217 numClasses = 1;
218 }
219
220 expandBufAdd4BE(pReply, numClasses);
221
222 if (numClasses > 0) {
223 uint8_t typeTag;
224 uint32_t status;
225
226 /* get class vs. interface and status flags */
227 Dbg::GetClassInfo(refTypeId, &typeTag, &status, NULL);
228
229 expandBufAdd1(pReply, typeTag);
230 expandBufAddRefTypeId(pReply, refTypeId);
231 expandBufAdd4BE(pReply, status);
232 }
233
234 free(classDescriptor);
235
236 return ERR_NONE;
237}
238
239/*
240 * Handle request for the thread IDs of all running threads.
241 *
242 * We exclude ourselves from the list, because we don't allow ourselves
243 * to be suspended, and that violates some JDWP expectations.
244 */
245static JdwpError handleVM_AllThreads(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
246 ObjectId* pThreadIds;
247 uint32_t threadCount;
248 Dbg::GetAllThreads(&pThreadIds, &threadCount);
249
250 expandBufAdd4BE(pReply, threadCount);
251
252 ObjectId* walker = pThreadIds;
253 for (uint32_t i = 0; i < threadCount; i++) {
254 expandBufAddObjectId(pReply, *walker++);
255 }
256
257 free(pThreadIds);
258
259 return ERR_NONE;
260}
261
262/*
263 * List all thread groups that do not have a parent.
264 */
265static JdwpError handleVM_TopLevelThreadGroups(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
266 /*
267 * TODO: maintain a list of parentless thread groups in the VM.
268 *
269 * For now, just return "system". Application threads are created
270 * in "main", which is a child of "system".
271 */
272 uint32_t groups = 1;
273 expandBufAdd4BE(pReply, groups);
274 //threadGroupId = debugGetMainThreadGroup();
275 //expandBufAdd8BE(pReply, threadGroupId);
276 ObjectId threadGroupId = Dbg::GetSystemThreadGroupId();
277 expandBufAddObjectId(pReply, threadGroupId);
278
279 return ERR_NONE;
280}
281
282/*
283 * Respond with the sizes of the basic debugger types.
284 *
285 * All IDs are 8 bytes.
286 */
287static JdwpError handleVM_IDSizes(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
288 expandBufAdd4BE(pReply, sizeof(FieldId));
289 expandBufAdd4BE(pReply, sizeof(MethodId));
290 expandBufAdd4BE(pReply, sizeof(ObjectId));
291 expandBufAdd4BE(pReply, sizeof(RefTypeId));
292 expandBufAdd4BE(pReply, sizeof(FrameId));
293 return ERR_NONE;
294}
295
296/*
297 * The debugger is politely asking to disconnect. We're good with that.
298 *
299 * We could resume threads and clean up pinned references, but we can do
300 * that when the TCP connection drops.
301 */
302static JdwpError handleVM_Dispose(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
303 return ERR_NONE;
304}
305
306/*
307 * Suspend the execution of the application running in the VM (i.e. suspend
308 * all threads).
309 *
310 * This needs to increment the "suspend count" on all threads.
311 */
312static JdwpError handleVM_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700313 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700314 return ERR_NONE;
315}
316
317/*
318 * Resume execution. Decrements the "suspend count" of all threads.
319 */
320static JdwpError handleVM_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
321 Dbg::ResumeVM();
322 return ERR_NONE;
323}
324
325/*
326 * The debugger wants the entire VM to exit.
327 */
328static JdwpError handleVM_Exit(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700329 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700330
331 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
332
333 Dbg::Exit(exitCode);
334 return ERR_NOT_IMPLEMENTED; // shouldn't get here
335}
336
337/*
338 * Create a new string in the VM and return its ID.
339 *
340 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
341 * string "java.util.Arrays".)
342 */
343static JdwpError handleVM_CreateString(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
344 size_t strLen;
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700345 char* str = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700346
347 LOG(VERBOSE) << " Req to create string '" << str << "'";
348
349 ObjectId stringId = Dbg::CreateString(str);
350 if (stringId == 0) {
351 return ERR_OUT_OF_MEMORY;
352 }
353
354 expandBufAddObjectId(pReply, stringId);
355 return ERR_NONE;
356}
357
358/*
359 * Tell the debugger what we are capable of.
360 */
361static JdwpError handleVM_Capabilities(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
362 expandBufAdd1(pReply, false); /* canWatchFieldModification */
363 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
364 expandBufAdd1(pReply, false); /* canGetBytecodes */
365 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
366 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
367 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
368 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
369 return ERR_NONE;
370}
371
372/*
373 * Return classpath and bootclasspath.
374 */
375static JdwpError handleVM_ClassPaths(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
376 char baseDir[2] = "/";
377
378 /*
379 * TODO: make this real. Not important for remote debugging, but
380 * might be useful for local debugging.
381 */
382 uint32_t classPaths = 1;
383 uint32_t bootClassPaths = 0;
384
Elliott Hughesa2155262011-11-16 16:26:58 -0800385 expandBufAddUtf8String(pReply, baseDir);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700386 expandBufAdd4BE(pReply, classPaths);
387 for (uint32_t i = 0; i < classPaths; i++) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800388 expandBufAddUtf8String(pReply, ".");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700389 }
390
391 expandBufAdd4BE(pReply, bootClassPaths);
392 for (uint32_t i = 0; i < classPaths; i++) {
393 /* add bootclasspath components as strings */
394 }
395
396 return ERR_NONE;
397}
398
399/*
400 * Release a list of object IDs. (Seen in jdb.)
401 *
402 * Currently does nothing.
403 */
404static JdwpError HandleVM_DisposeObjects(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
405 return ERR_NONE;
406}
407
408/*
409 * Tell the debugger what we are capable of.
410 */
411static JdwpError handleVM_CapabilitiesNew(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
412 expandBufAdd1(pReply, false); /* canWatchFieldModification */
413 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
414 expandBufAdd1(pReply, false); /* canGetBytecodes */
415 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
416 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
417 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
418 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
419 expandBufAdd1(pReply, false); /* canRedefineClasses */
420 expandBufAdd1(pReply, false); /* canAddMethod */
421 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
422 expandBufAdd1(pReply, false); /* canPopFrames */
423 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
424 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
425 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
426 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
427 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
428 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
429 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
430 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
431 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
432 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
433
434 /* fill in reserved22 through reserved32; note count started at 1 */
435 for (int i = 22; i <= 32; i++) {
436 expandBufAdd1(pReply, false); /* reservedN */
437 }
438 return ERR_NONE;
439}
440
441/*
442 * Cough up the complete list of classes.
443 */
444static JdwpError handleVM_AllClassesWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
445 uint32_t numClasses = 0;
446 RefTypeId* classRefBuf = NULL;
447
448 Dbg::GetClassList(&numClasses, &classRefBuf);
449
450 expandBufAdd4BE(pReply, numClasses);
451
452 for (uint32_t i = 0; i < numClasses; i++) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800453 static const char genericSignature[1] = "";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454 uint8_t refTypeTag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800455 std::string descriptor;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700456 uint32_t status;
457
Elliott Hughesa2155262011-11-16 16:26:58 -0800458 Dbg::GetClassInfo(classRefBuf[i], &refTypeTag, &status, &descriptor);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700459
460 expandBufAdd1(pReply, refTypeTag);
461 expandBufAddRefTypeId(pReply, classRefBuf[i]);
Elliott Hughesa2155262011-11-16 16:26:58 -0800462 expandBufAddUtf8String(pReply, descriptor.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700463 expandBufAddUtf8String(pReply, genericSignature);
464 expandBufAdd4BE(pReply, status);
465 }
466
467 free(classRefBuf);
468
469 return ERR_NONE;
470}
471
472/*
473 * Given a referenceTypeID, return a string with the JNI reference type
474 * signature (e.g. "Ljava/lang/Error;").
475 */
476static JdwpError handleRT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
477 RefTypeId refTypeId = ReadRefTypeId(&buf);
478
479 LOG(VERBOSE) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
480 const char* signature = Dbg::GetSignature(refTypeId);
Elliott Hughesa2155262011-11-16 16:26:58 -0800481 expandBufAddUtf8String(pReply, signature);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700482
483 return ERR_NONE;
484}
485
486/*
487 * Return the modifiers (a/k/a access flags) for a reference type.
488 */
489static JdwpError handleRT_Modifiers(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
490 RefTypeId refTypeId = ReadRefTypeId(&buf);
491 uint32_t modBits = Dbg::GetAccessFlags(refTypeId);
492 expandBufAdd4BE(pReply, modBits);
493 return ERR_NONE;
494}
495
496/*
497 * Get values from static fields in a reference type.
498 */
499static JdwpError handleRT_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
500 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700501 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700502
503 LOG(VERBOSE) << " RT_GetValues " << numFields << ":";
504
505 expandBufAdd4BE(pReply, numFields);
506 for (uint32_t i = 0; i < numFields; i++) {
507 FieldId fieldId = ReadFieldId(&buf);
508 Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
509 }
510
511 return ERR_NONE;
512}
513
514/*
515 * Get the name of the source file in which a reference type was declared.
516 */
517static JdwpError handleRT_SourceFile(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
518 RefTypeId refTypeId = ReadRefTypeId(&buf);
519
520 const char* fileName = Dbg::GetSourceFile(refTypeId);
521 if (fileName != NULL) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800522 expandBufAddUtf8String(pReply, fileName);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700523 return ERR_NONE;
524 } else {
525 return ERR_ABSENT_INFORMATION;
526 }
527}
528
529/*
530 * Return the current status of the reference type.
531 */
532static JdwpError handleRT_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
533 RefTypeId refTypeId = ReadRefTypeId(&buf);
534
535 /* get status flags */
536 uint8_t typeTag;
537 uint32_t status;
538 Dbg::GetClassInfo(refTypeId, &typeTag, &status, NULL);
539 expandBufAdd4BE(pReply, status);
540 return ERR_NONE;
541}
542
543/*
544 * Return interfaces implemented directly by this class.
545 */
546static JdwpError handleRT_Interfaces(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
547 RefTypeId refTypeId = ReadRefTypeId(&buf);
548
Elliott Hughesa2155262011-11-16 16:26:58 -0800549 LOG(VERBOSE) << StringPrintf(" Req for interfaces in %llx (%s)", refTypeId, Dbg::GetClassDescriptor(refTypeId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700550
551 Dbg::OutputAllInterfaces(refTypeId, pReply);
552
553 return ERR_NONE;
554}
555
556/*
557 * Return the class object corresponding to this type.
558 */
559static JdwpError handleRT_ClassObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
560 RefTypeId refTypeId = ReadRefTypeId(&buf);
561 ObjectId classObjId = Dbg::GetClassObject(refTypeId);
562
563 LOG(VERBOSE) << StringPrintf(" RefTypeId %llx -> ObjectId %llx", refTypeId, classObjId);
564
565 expandBufAddObjectId(pReply, classObjId);
566
567 return ERR_NONE;
568}
569
570/*
571 * Returns the value of the SourceDebugExtension attribute.
572 *
573 * JDB seems interested, but DEX files don't currently support this.
574 */
575static JdwpError handleRT_SourceDebugExtension(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
576 /* referenceTypeId in, string out */
577 return ERR_ABSENT_INFORMATION;
578}
579
580/*
581 * Like RT_Signature but with the possibility of a "generic signature".
582 */
583static JdwpError handleRT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800584 static const char genericSignature[1] = "";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700585
586 RefTypeId refTypeId = ReadRefTypeId(&buf);
587
588 LOG(VERBOSE) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
589 const char* signature = Dbg::GetSignature(refTypeId);
590 if (signature != NULL) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800591 expandBufAddUtf8String(pReply, signature);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700592 } else {
593 LOG(WARNING) << StringPrintf("No signature for refTypeId=0x%llx", refTypeId);
Elliott Hughesa2155262011-11-16 16:26:58 -0800594 expandBufAddUtf8String(pReply, "Lunknown;");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700595 }
596 expandBufAddUtf8String(pReply, genericSignature);
597
598 return ERR_NONE;
599}
600
601/*
602 * Return the instance of java.lang.ClassLoader that loaded the specified
603 * reference type, or null if it was loaded by the system loader.
604 */
605static JdwpError handleRT_ClassLoader(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
606 RefTypeId refTypeId = ReadRefTypeId(&buf);
607
608 expandBufAddObjectId(pReply, Dbg::GetClassLoader(refTypeId));
609
610 return ERR_NONE;
611}
612
613/*
614 * Given a referenceTypeId, return a block of stuff that describes the
615 * fields declared by a class.
616 */
617static JdwpError handleRT_FieldsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
618 RefTypeId refTypeId = ReadRefTypeId(&buf);
619 LOG(VERBOSE) << StringPrintf(" Req for fields in refTypeId=0x%llx", refTypeId);
620 LOG(VERBOSE) << StringPrintf(" --> '%s'", Dbg::GetSignature(refTypeId));
621 Dbg::OutputAllFields(refTypeId, true, pReply);
622 return ERR_NONE;
623}
624
625/*
626 * Given a referenceTypeID, return a block of goodies describing the
627 * methods declared by a class.
628 */
629static JdwpError handleRT_MethodsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
630 RefTypeId refTypeId = ReadRefTypeId(&buf);
631
632 LOG(VERBOSE) << StringPrintf(" Req for methods in refTypeId=0x%llx", refTypeId);
633 LOG(VERBOSE) << StringPrintf(" --> '%s'", Dbg::GetSignature(refTypeId));
634
635 Dbg::OutputAllMethods(refTypeId, true, pReply);
636
637 return ERR_NONE;
638}
639
640/*
641 * Return the immediate superclass of a class.
642 */
643static JdwpError handleCT_Superclass(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
644 RefTypeId classId = ReadRefTypeId(&buf);
645
646 RefTypeId superClassId = Dbg::GetSuperclass(classId);
647
648 expandBufAddRefTypeId(pReply, superClassId);
649
650 return ERR_NONE;
651}
652
653/*
654 * Set static class values.
655 */
656static JdwpError handleCT_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
657 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700658 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700659
660 LOG(VERBOSE) << StringPrintf(" Req to set %d values in classId=%llx", values, classId);
661
662 for (uint32_t i = 0; i < values; i++) {
663 FieldId fieldId = ReadFieldId(&buf);
664 uint8_t fieldTag = Dbg::GetStaticFieldBasicTag(classId, fieldId);
665 int width = Dbg::GetTagWidth(fieldTag);
666 uint64_t value = jdwpReadValue(&buf, width);
667
668 LOG(VERBOSE) << StringPrintf(" --> field=%x tag=%c -> %lld", fieldId, fieldTag, value);
669 Dbg::SetStaticFieldValue(classId, fieldId, value, width);
670 }
671
672 return ERR_NONE;
673}
674
675/*
676 * Invoke a static method.
677 *
678 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
679 * values in the "variables" display.
680 */
681static JdwpError handleCT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
682 RefTypeId classId = ReadRefTypeId(&buf);
683 ObjectId threadId = ReadObjectId(&buf);
684 MethodId methodId = ReadMethodId(&buf);
685
686 return finishInvoke(state, buf, dataLen, pReply, threadId, 0, classId, methodId, false);
687}
688
689/*
690 * Create a new object of the requested type, and invoke the specified
691 * constructor.
692 *
693 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
694 * see the contents of a byte[] as a string.
695 */
696static JdwpError handleCT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
697 RefTypeId classId = ReadRefTypeId(&buf);
698 ObjectId threadId = ReadObjectId(&buf);
699 MethodId methodId = ReadMethodId(&buf);
700
701 LOG(VERBOSE) << "Creating instance of " << Dbg::GetClassDescriptor(classId);
702 ObjectId objectId = Dbg::CreateObject(classId);
703 if (objectId == 0) {
704 return ERR_OUT_OF_MEMORY;
705 }
706 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, true);
707}
708
709/*
710 * Create a new array object of the requested type and length.
711 */
712static JdwpError handleAT_newInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
713 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700714 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700715
Elliott Hughesa2155262011-11-16 16:26:58 -0800716 LOG(VERBOSE) << StringPrintf("Creating array %s[%u]", Dbg::GetClassDescriptor(arrayTypeId).c_str(), length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700717 ObjectId objectId = Dbg::CreateArrayObject(arrayTypeId, length);
718 if (objectId == 0) {
719 return ERR_OUT_OF_MEMORY;
720 }
721 expandBufAdd1(pReply, JT_ARRAY);
722 expandBufAddObjectId(pReply, objectId);
723 return ERR_NONE;
724}
725
726/*
727 * Return line number information for the method, if present.
728 */
729static JdwpError handleM_LineTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
730 RefTypeId refTypeId = ReadRefTypeId(&buf);
731 MethodId methodId = ReadMethodId(&buf);
732
Elliott Hughesa2155262011-11-16 16:26:58 -0800733 LOG(VERBOSE) << StringPrintf(" Req for line table in %s.%s", Dbg::GetClassDescriptor(refTypeId).c_str(), Dbg::GetMethodName(refTypeId,methodId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700734
735 Dbg::OutputLineTable(refTypeId, methodId, pReply);
736
737 return ERR_NONE;
738}
739
740/*
741 * Pull out the LocalVariableTable goodies.
742 */
743static JdwpError handleM_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
744 RefTypeId classId = ReadRefTypeId(&buf);
745 MethodId methodId = ReadMethodId(&buf);
746
Elliott Hughesa2155262011-11-16 16:26:58 -0800747 LOG(VERBOSE) << StringPrintf(" Req for LocalVarTab in class=%s method=%s", Dbg::GetClassDescriptor(classId).c_str(), Dbg::GetMethodName(classId, methodId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700748
749 /*
750 * We could return ERR_ABSENT_INFORMATION here if the DEX file was
751 * built without local variable information. That will cause Eclipse
752 * to make a best-effort attempt at displaying local variables
753 * anonymously. However, the attempt isn't very good, so we're probably
754 * better off just not showing anything.
755 */
756 Dbg::OutputVariableTable(classId, methodId, true, pReply);
757 return ERR_NONE;
758}
759
760/*
761 * Given an object reference, return the runtime type of the object
762 * (class or array).
763 *
764 * This can get called on different things, e.g. threadId gets
765 * passed in here.
766 */
767static JdwpError handleOR_ReferenceType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
768 ObjectId objectId = ReadObjectId(&buf);
769 LOG(VERBOSE) << StringPrintf(" Req for type of objectId=0x%llx", objectId);
770
771 uint8_t refTypeTag;
772 RefTypeId typeId;
773 Dbg::GetObjectType(objectId, &refTypeTag, &typeId);
774
775 expandBufAdd1(pReply, refTypeTag);
776 expandBufAddRefTypeId(pReply, typeId);
777
778 return ERR_NONE;
779}
780
781/*
782 * Get values from the fields of an object.
783 */
784static JdwpError handleOR_GetValues(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 for %d fields from objectId=0x%llx", numFields, objectId);
789
790 expandBufAdd4BE(pReply, numFields);
791
792 for (uint32_t i = 0; i < numFields; i++) {
793 FieldId fieldId = ReadFieldId(&buf);
794 Dbg::GetFieldValue(objectId, fieldId, pReply);
795 }
796
797 return ERR_NONE;
798}
799
800/*
801 * Set values in the fields of an object.
802 */
803static JdwpError handleOR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
804 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700805 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700806
807 LOG(VERBOSE) << StringPrintf(" Req to set %d fields in objectId=0x%llx", numFields, objectId);
808
809 for (uint32_t i = 0; i < numFields; i++) {
810 FieldId fieldId = ReadFieldId(&buf);
811
812 uint8_t fieldTag = Dbg::GetFieldBasicTag(objectId, fieldId);
813 int width = Dbg::GetTagWidth(fieldTag);
814 uint64_t value = jdwpReadValue(&buf, width);
815
816 LOG(VERBOSE) << StringPrintf(" --> fieldId=%x tag='%c'(%d) value=%lld", fieldId, fieldTag, width, value);
817
818 Dbg::SetFieldValue(objectId, fieldId, value, width);
819 }
820
821 return ERR_NONE;
822}
823
824/*
825 * Invoke an instance method. The invocation must occur in the specified
826 * thread, which must have been suspended by an event.
827 *
828 * The call is synchronous. All threads in the VM are resumed, unless the
829 * SINGLE_THREADED flag is set.
830 *
831 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
832 * object), it will try to invoke the object's toString() function. This
833 * feature becomes crucial when examining ArrayLists with Eclipse.
834 */
835static JdwpError handleOR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
836 ObjectId objectId = ReadObjectId(&buf);
837 ObjectId threadId = ReadObjectId(&buf);
838 RefTypeId classId = ReadRefTypeId(&buf);
839 MethodId methodId = ReadMethodId(&buf);
840
841 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, false);
842}
843
844/*
845 * Disable garbage collection of the specified object.
846 */
847static JdwpError handleOR_DisableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
848 // this is currently a no-op
849 return ERR_NONE;
850}
851
852/*
853 * Enable garbage collection of the specified object.
854 */
855static JdwpError handleOR_EnableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
856 // this is currently a no-op
857 return ERR_NONE;
858}
859
860/*
861 * Determine whether an object has been garbage collected.
862 */
863static JdwpError handleOR_IsCollected(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
864 ObjectId objectId;
865
866 objectId = ReadObjectId(&buf);
867 LOG(VERBOSE) << StringPrintf(" Req IsCollected(0x%llx)", objectId);
868
869 // TODO: currently returning false; must integrate with GC
870 expandBufAdd1(pReply, 0);
871
872 return ERR_NONE;
873}
874
875/*
876 * Return the string value in a string object.
877 */
878static JdwpError handleSR_Value(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
879 ObjectId stringObject = ReadObjectId(&buf);
880 char* str = Dbg::StringToUtf8(stringObject);
881
882 LOG(VERBOSE) << StringPrintf(" Req for str %llx --> '%s'", stringObject, str);
883
Elliott Hughesa2155262011-11-16 16:26:58 -0800884 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700885 free(str);
886
887 return ERR_NONE;
888}
889
890/*
891 * Return a thread's name.
892 */
893static JdwpError handleTR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
894 ObjectId threadId = ReadObjectId(&buf);
895
896 LOG(VERBOSE) << StringPrintf(" Req for name of thread 0x%llx", threadId);
897 char* name = Dbg::GetThreadName(threadId);
898 if (name == NULL) {
899 return ERR_INVALID_THREAD;
900 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800901 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700902 free(name);
903
904 return ERR_NONE;
905}
906
907/*
908 * Suspend the specified thread.
909 *
910 * It's supposed to remain suspended even if interpreted code wants to
911 * resume it; only the JDI is allowed to resume it.
912 */
913static JdwpError handleTR_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
914 ObjectId threadId = ReadObjectId(&buf);
915
916 if (threadId == Dbg::GetThreadSelfId()) {
917 LOG(INFO) << " Warning: ignoring request to suspend self";
918 return ERR_THREAD_NOT_SUSPENDED;
919 }
920 LOG(VERBOSE) << StringPrintf(" Req to suspend thread 0x%llx", threadId);
921 Dbg::SuspendThread(threadId);
922 return ERR_NONE;
923}
924
925/*
926 * Resume the specified thread.
927 */
928static JdwpError handleTR_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
929 ObjectId threadId = ReadObjectId(&buf);
930
931 if (threadId == Dbg::GetThreadSelfId()) {
932 LOG(INFO) << " Warning: ignoring request to resume self";
933 return ERR_NONE;
934 }
935 LOG(VERBOSE) << StringPrintf(" Req to resume thread 0x%llx", threadId);
936 Dbg::ResumeThread(threadId);
937 return ERR_NONE;
938}
939
940/*
941 * Return status of specified thread.
942 */
943static JdwpError handleTR_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
944 ObjectId threadId = ReadObjectId(&buf);
945
946 LOG(VERBOSE) << StringPrintf(" Req for status of thread 0x%llx", threadId);
947
948 uint32_t threadStatus;
949 uint32_t suspendStatus;
950 if (!Dbg::GetThreadStatus(threadId, &threadStatus, &suspendStatus)) {
951 return ERR_INVALID_THREAD;
952 }
953
954 LOG(VERBOSE) << " --> " << JdwpThreadStatus(threadStatus) << ", " << JdwpSuspendStatus(suspendStatus);
955
956 expandBufAdd4BE(pReply, threadStatus);
957 expandBufAdd4BE(pReply, suspendStatus);
958
959 return ERR_NONE;
960}
961
962/*
963 * Return the thread group that the specified thread is a member of.
964 */
965static JdwpError handleTR_ThreadGroup(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
966 ObjectId threadId = ReadObjectId(&buf);
967
968 /* currently not handling these */
969 ObjectId threadGroupId = Dbg::GetThreadGroup(threadId);
970 expandBufAddObjectId(pReply, threadGroupId);
971
972 return ERR_NONE;
973}
974
975/*
976 * Return the current call stack of a suspended thread.
977 *
978 * If the thread isn't suspended, the error code isn't defined, but should
979 * be THREAD_NOT_SUSPENDED.
980 */
981static JdwpError handleTR_Frames(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
982 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700983 uint32_t startFrame = Read4BE(&buf);
984 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700985
986 if (!Dbg::ThreadExists(threadId)) {
987 return ERR_INVALID_THREAD;
988 }
989 if (!Dbg::IsSuspended(threadId)) {
990 LOG(VERBOSE) << StringPrintf(" Rejecting req for frames in running thread '%s' (%llx)", Dbg::GetThreadName(threadId), threadId);
991 return ERR_THREAD_NOT_SUSPENDED;
992 }
993
994 int frameCount = Dbg::GetThreadFrameCount(threadId);
995
996 LOG(VERBOSE) << StringPrintf(" Request for frames: threadId=%llx start=%d length=%d [count=%d]", threadId, startFrame, length, frameCount);
997 if (frameCount <= 0) {
998 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
999 }
1000 if (length == (uint32_t) -1) {
1001 length = frameCount;
1002 }
1003 CHECK((int) startFrame >= 0 && (int) startFrame < frameCount);
1004 CHECK_LE((int) (startFrame + length), frameCount);
1005
1006 uint32_t frames = length;
1007 expandBufAdd4BE(pReply, frames);
1008 for (uint32_t i = startFrame; i < (startFrame+length); i++) {
1009 FrameId frameId;
1010 JdwpLocation loc;
1011
1012 Dbg::GetThreadFrame(threadId, i, &frameId, &loc);
1013
1014 expandBufAdd8BE(pReply, frameId);
1015 AddLocation(pReply, &loc);
1016
1017 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);
1018 }
1019
1020 return ERR_NONE;
1021}
1022
1023/*
1024 * Returns the #of frames on the specified thread, which must be suspended.
1025 */
1026static JdwpError handleTR_FrameCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1027 ObjectId threadId = ReadObjectId(&buf);
1028
1029 if (!Dbg::ThreadExists(threadId)) {
1030 return ERR_INVALID_THREAD;
1031 }
1032 if (!Dbg::IsSuspended(threadId)) {
1033 LOG(VERBOSE) << StringPrintf(" Rejecting req for frames in running thread '%s' (%llx)", Dbg::GetThreadName(threadId), threadId);
1034 return ERR_THREAD_NOT_SUSPENDED;
1035 }
1036
1037 int frameCount = Dbg::GetThreadFrameCount(threadId);
1038 if (frameCount < 0) {
1039 return ERR_INVALID_THREAD;
1040 }
1041 expandBufAdd4BE(pReply, (uint32_t)frameCount);
1042
1043 return ERR_NONE;
1044}
1045
1046/*
1047 * Get the monitor that the thread is waiting on.
1048 */
1049static JdwpError handleTR_CurrentContendedMonitor(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1050 ObjectId threadId;
1051
1052 threadId = ReadObjectId(&buf);
1053
1054 // TODO: create an Object to represent the monitor (we're currently
1055 // just using a raw Monitor struct in the VM)
1056
1057 return ERR_NOT_IMPLEMENTED;
1058}
1059
1060/*
1061 * Return the suspend count for the specified thread.
1062 *
1063 * (The thread *might* still be running -- it might not have examined
1064 * its suspend count recently.)
1065 */
1066static JdwpError handleTR_SuspendCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1067 ObjectId threadId = ReadObjectId(&buf);
1068
1069 uint32_t suspendCount = Dbg::GetThreadSuspendCount(threadId);
1070 expandBufAdd4BE(pReply, suspendCount);
1071
1072 return ERR_NONE;
1073}
1074
1075/*
1076 * Return the name of a thread group.
1077 *
1078 * The Eclipse debugger recognizes "main" and "system" as special.
1079 */
1080static JdwpError handleTGR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1081 ObjectId threadGroupId = ReadObjectId(&buf);
1082 LOG(VERBOSE) << StringPrintf(" Req for name of threadGroupId=0x%llx", threadGroupId);
1083
1084 char* name = Dbg::GetThreadGroupName(threadGroupId);
1085 if (name != NULL) {
Elliott Hughesa2155262011-11-16 16:26:58 -08001086 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001087 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -08001088 expandBufAddUtf8String(pReply, "BAD-GROUP-ID");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001089 LOG(VERBOSE) << StringPrintf("bad thread group ID");
1090 }
1091
1092 free(name);
1093
1094 return ERR_NONE;
1095}
1096
1097/*
1098 * Returns the thread group -- if any -- that contains the specified
1099 * thread group.
1100 */
1101static JdwpError handleTGR_Parent(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1102 ObjectId groupId = ReadObjectId(&buf);
1103
1104 ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId);
1105 expandBufAddObjectId(pReply, parentGroup);
1106
1107 return ERR_NONE;
1108}
1109
1110/*
1111 * Return the active threads and thread groups that are part of the
1112 * specified thread group.
1113 */
1114static JdwpError handleTGR_Children(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1115 ObjectId threadGroupId = ReadObjectId(&buf);
1116 LOG(VERBOSE) << StringPrintf(" Req for threads in threadGroupId=0x%llx", threadGroupId);
1117
1118 ObjectId* pThreadIds;
1119 uint32_t threadCount;
1120 Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount);
1121
1122 expandBufAdd4BE(pReply, threadCount);
1123
1124 for (uint32_t i = 0; i < threadCount; i++) {
1125 expandBufAddObjectId(pReply, pThreadIds[i]);
1126 }
1127 free(pThreadIds);
1128
1129 /*
1130 * TODO: finish support for child groups
1131 *
1132 * For now, just show that "main" is a child of "system".
1133 */
1134 if (threadGroupId == Dbg::GetSystemThreadGroupId()) {
1135 expandBufAdd4BE(pReply, 1);
1136 expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId());
1137 } else {
1138 expandBufAdd4BE(pReply, 0);
1139 }
1140
1141 return ERR_NONE;
1142}
1143
1144/*
1145 * Return the #of components in the array.
1146 */
1147static JdwpError handleAR_Length(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1148 ObjectId arrayId = ReadObjectId(&buf);
1149 LOG(VERBOSE) << StringPrintf(" Req for length of array 0x%llx", arrayId);
1150
1151 uint32_t arrayLength = Dbg::GetArrayLength(arrayId);
1152
1153 LOG(VERBOSE) << StringPrintf(" --> %d", arrayLength);
1154
1155 expandBufAdd4BE(pReply, arrayLength);
1156
1157 return ERR_NONE;
1158}
1159
1160/*
1161 * Return the values from an array.
1162 */
1163static JdwpError handleAR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1164 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001165 uint32_t firstIndex = Read4BE(&buf);
1166 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001167
1168 uint8_t tag = Dbg::GetArrayElementTag(arrayId);
1169 LOG(VERBOSE) << StringPrintf(" Req for array values 0x%llx first=%d len=%d (elem tag=%c)", arrayId, firstIndex, length, tag);
1170
1171 expandBufAdd1(pReply, tag);
1172 expandBufAdd4BE(pReply, length);
1173
1174 if (!Dbg::OutputArray(arrayId, firstIndex, length, pReply)) {
1175 return ERR_INVALID_LENGTH;
1176 }
1177
1178 return ERR_NONE;
1179}
1180
1181/*
1182 * Set values in an array.
1183 */
1184static JdwpError handleAR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1185 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001186 uint32_t firstIndex = Read4BE(&buf);
1187 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001188
1189 LOG(VERBOSE) << StringPrintf(" Req to set array values 0x%llx first=%d count=%d", arrayId, firstIndex, values);
1190
1191 if (!Dbg::SetArrayElements(arrayId, firstIndex, values, buf)) {
1192 return ERR_INVALID_LENGTH;
1193 }
1194
1195 return ERR_NONE;
1196}
1197
1198/*
1199 * Return the set of classes visible to a class loader. All classes which
1200 * have the class loader as a defining or initiating loader are returned.
1201 */
1202static JdwpError handleCLR_VisibleClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1203 ObjectId classLoaderObject;
1204 uint32_t numClasses = 0;
1205 RefTypeId* classRefBuf = NULL;
1206 int i;
1207
1208 classLoaderObject = ReadObjectId(&buf);
1209
1210 Dbg::GetVisibleClassList(classLoaderObject, &numClasses, &classRefBuf);
1211
1212 expandBufAdd4BE(pReply, numClasses);
1213 for (i = 0; i < (int) numClasses; i++) {
1214 uint8_t refTypeTag = Dbg::GetClassObjectType(classRefBuf[i]);
1215
1216 expandBufAdd1(pReply, refTypeTag);
1217 expandBufAddRefTypeId(pReply, classRefBuf[i]);
1218 }
1219
1220 return ERR_NONE;
1221}
1222
1223/*
1224 * Return a newly-allocated string in which all occurrences of '.' have
1225 * been changed to '/'. If we find a '/' in the original string, NULL
1226 * is returned to avoid ambiguity.
1227 */
1228char* dvmDotToSlash(const char* str) {
1229 char* newStr = strdup(str);
1230 char* cp = newStr;
1231
1232 if (newStr == NULL) {
1233 return NULL;
1234 }
1235
1236 while (*cp != '\0') {
1237 if (*cp == '/') {
1238 CHECK(false);
1239 return NULL;
1240 }
1241 if (*cp == '.') {
1242 *cp = '/';
1243 }
1244 cp++;
1245 }
1246
1247 return newStr;
1248}
1249
1250/*
1251 * Set an event trigger.
1252 *
1253 * Reply with a requestID.
1254 */
1255static JdwpError handleER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1256 const uint8_t* origBuf = buf;
1257
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001258 uint8_t eventKind = Read1(&buf);
1259 uint8_t suspendPolicy = Read1(&buf);
1260 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001261
1262 LOG(VERBOSE) << " Set(kind=" << JdwpEventKind(eventKind)
1263 << " suspend=" << JdwpSuspendPolicy(suspendPolicy)
1264 << " mods=" << modifierCount << ")";
1265
1266 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1267
1268 JdwpEvent* pEvent = EventAlloc(modifierCount);
1269 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
1270 pEvent->suspendPolicy = static_cast<JdwpSuspendPolicy>(suspendPolicy);
1271 pEvent->modCount = modifierCount;
1272
1273 /*
1274 * Read modifiers. Ordering may be significant (see explanation of Count
1275 * mods in JDWP doc).
1276 */
1277 for (uint32_t idx = 0; idx < modifierCount; idx++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001278 uint8_t modKind = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001279
1280 pEvent->mods[idx].modKind = modKind;
1281
1282 switch (modKind) {
1283 case MK_COUNT: /* report once, when "--count" reaches 0 */
1284 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001285 uint32_t count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001286 LOG(VERBOSE) << " Count: " << count;
1287 if (count == 0) {
1288 return ERR_INVALID_COUNT;
1289 }
1290 pEvent->mods[idx].count.count = count;
1291 }
1292 break;
1293 case MK_CONDITIONAL: /* conditional on expression) */
1294 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001295 uint32_t exprId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001296 LOG(VERBOSE) << " Conditional: " << exprId;
1297 pEvent->mods[idx].conditional.exprId = exprId;
1298 }
1299 break;
1300 case MK_THREAD_ONLY: /* only report events in specified thread */
1301 {
1302 ObjectId threadId = ReadObjectId(&buf);
1303 LOG(VERBOSE) << StringPrintf(" ThreadOnly: %llx", threadId);
1304 pEvent->mods[idx].threadOnly.threadId = threadId;
1305 }
1306 break;
1307 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1308 {
1309 RefTypeId clazzId = ReadRefTypeId(&buf);
Elliott Hughesa2155262011-11-16 16:26:58 -08001310 LOG(VERBOSE) << StringPrintf(" ClassOnly: %llx (%s)", clazzId, Dbg::GetClassDescriptor(clazzId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001311 pEvent->mods[idx].classOnly.refTypeId = clazzId;
1312 }
1313 break;
1314 case MK_CLASS_MATCH: /* restrict events to matching classes */
1315 {
1316 char* pattern;
1317 size_t strLen;
1318
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001319 pattern = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001320 LOG(VERBOSE) << StringPrintf(" ClassMatch: '%s'", pattern);
1321 /* pattern is "java.foo.*", we want "java/foo/ *" */
1322 pEvent->mods[idx].classMatch.classPattern = dvmDotToSlash(pattern);
1323 free(pattern);
1324 }
1325 break;
1326 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1327 {
1328 char* pattern;
1329 size_t strLen;
1330
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001331 pattern = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001332 LOG(VERBOSE) << StringPrintf(" ClassExclude: '%s'", pattern);
1333 pEvent->mods[idx].classExclude.classPattern = dvmDotToSlash(pattern);
1334 free(pattern);
1335 }
1336 break;
1337 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1338 {
1339 JdwpLocation loc;
1340
1341 jdwpReadLocation(&buf, &loc);
1342 LOG(VERBOSE) << StringPrintf(" LocationOnly: typeTag=%d classId=%llx methodId=%x idx=%llx",
1343 loc.typeTag, loc.classId, loc.methodId, loc.idx);
1344 pEvent->mods[idx].locationOnly.loc = loc;
1345 }
1346 break;
1347 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1348 {
1349 RefTypeId exceptionOrNull; /* null == all exceptions */
1350 uint8_t caught, uncaught;
1351
1352 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001353 caught = Read1(&buf);
1354 uncaught = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001355 LOG(VERBOSE) << StringPrintf(" ExceptionOnly: type=%llx(%s) caught=%d uncaught=%d",
Elliott Hughesa2155262011-11-16 16:26:58 -08001356 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassDescriptor(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001357
1358 pEvent->mods[idx].exceptionOnly.refTypeId = exceptionOrNull;
1359 pEvent->mods[idx].exceptionOnly.caught = caught;
1360 pEvent->mods[idx].exceptionOnly.uncaught = uncaught;
1361 }
1362 break;
1363 case MK_FIELD_ONLY: /* for field access/mod events */
1364 {
1365 RefTypeId declaring = ReadRefTypeId(&buf);
1366 FieldId fieldId = ReadFieldId(&buf);
1367 LOG(VERBOSE) << StringPrintf(" FieldOnly: %llx %x", declaring, fieldId);
1368 pEvent->mods[idx].fieldOnly.refTypeId = declaring;
1369 pEvent->mods[idx].fieldOnly.fieldId = fieldId;
1370 }
1371 break;
1372 case MK_STEP: /* for use with EK_SINGLE_STEP */
1373 {
1374 ObjectId threadId;
1375 uint32_t size, depth;
1376
1377 threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001378 size = Read4BE(&buf);
1379 depth = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001380 LOG(VERBOSE) << StringPrintf(" Step: thread=%llx", threadId)
1381 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1382
1383 pEvent->mods[idx].step.threadId = threadId;
1384 pEvent->mods[idx].step.size = size;
1385 pEvent->mods[idx].step.depth = depth;
1386 }
1387 break;
1388 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1389 {
1390 ObjectId instance = ReadObjectId(&buf);
1391 LOG(VERBOSE) << StringPrintf(" InstanceOnly: %llx", instance);
1392 pEvent->mods[idx].instanceOnly.objectId = instance;
1393 }
1394 break;
1395 default:
1396 LOG(WARNING) << "GLITCH: unsupported modKind=" << modKind;
1397 break;
1398 }
1399 }
1400
1401 /*
1402 * Make sure we consumed all data. It is possible that the remote side
1403 * has sent us bad stuff, but for now we blame ourselves.
1404 */
1405 if (buf != origBuf + dataLen) {
1406 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1407 }
1408
1409 /*
1410 * We reply with an integer "requestID".
1411 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001412 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001413 expandBufAdd4BE(pReply, requestId);
1414
1415 pEvent->requestId = requestId;
1416
1417 LOG(VERBOSE) << StringPrintf(" --> event requestId=%#x", requestId);
1418
1419 /* add it to the list */
1420 JdwpError err = RegisterEvent(state, pEvent);
1421 if (err != ERR_NONE) {
1422 /* registration failed, probably because event is bogus */
1423 EventFree(pEvent);
1424 LOG(WARNING) << "WARNING: event request rejected";
1425 }
1426 return err;
1427}
1428
1429/*
1430 * Clear an event. Failure to find an event with a matching ID is a no-op
1431 * and does not return an error.
1432 */
1433static JdwpError handleER_Clear(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1434 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001435 eventKind = Read1(&buf);
1436 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001437
1438 LOG(VERBOSE) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
1439
1440 UnregisterEventById(state, requestId);
1441
1442 return ERR_NONE;
1443}
1444
1445/*
1446 * Return the values of arguments and local variables.
1447 */
1448static JdwpError handleSF_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1449 ObjectId threadId = ReadObjectId(&buf);
1450 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001451 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001452
1453 LOG(VERBOSE) << StringPrintf(" Req for %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
1454
1455 expandBufAdd4BE(pReply, slots); /* "int values" */
1456 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001457 uint32_t slot = Read4BE(&buf);
1458 uint8_t reqSigByte = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001459
1460 LOG(VERBOSE) << StringPrintf(" --> slot %d '%c'", slot, reqSigByte);
1461
1462 int width = Dbg::GetTagWidth(reqSigByte);
1463 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
1464 Dbg::GetLocalValue(threadId, frameId, slot, reqSigByte, ptr, width);
1465 }
1466
1467 return ERR_NONE;
1468}
1469
1470/*
1471 * Set the values of arguments and local variables.
1472 */
1473static JdwpError handleSF_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1474 ObjectId threadId = ReadObjectId(&buf);
1475 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001476 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001477
1478 LOG(VERBOSE) << StringPrintf(" Req to set %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
1479
1480 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001481 uint32_t slot = Read4BE(&buf);
1482 uint8_t sigByte = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001483 int width = Dbg::GetTagWidth(sigByte);
1484 uint64_t value = jdwpReadValue(&buf, width);
1485
1486 LOG(VERBOSE) << StringPrintf(" --> slot %d '%c' %llx", slot, sigByte, value);
1487 Dbg::SetLocalValue(threadId, frameId, slot, sigByte, value, width);
1488 }
1489
1490 return ERR_NONE;
1491}
1492
1493/*
1494 * Returns the value of "this" for the specified frame.
1495 */
1496static JdwpError handleSF_ThisObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1497 ObjectId threadId = ReadObjectId(&buf);
1498 FrameId frameId = ReadFrameId(&buf);
1499
1500 ObjectId objectId;
1501 if (!Dbg::GetThisObject(threadId, frameId, &objectId)) {
1502 return ERR_INVALID_FRAMEID;
1503 }
1504
1505 uint8_t objectTag = Dbg::GetObjectTag(objectId);
1506 LOG(VERBOSE) << StringPrintf(" Req for 'this' in thread=%llx frame=%llx --> %llx %s '%c'", threadId, frameId, objectId, Dbg::GetObjectTypeName(objectId), (char)objectTag);
1507
1508 expandBufAdd1(pReply, objectTag);
1509 expandBufAddObjectId(pReply, objectId);
1510
1511 return ERR_NONE;
1512}
1513
1514/*
1515 * Return the reference type reflected by this class object.
1516 *
1517 * This appears to be required because ReferenceTypeId values are NEVER
1518 * reused, whereas ClassIds can be recycled like any other object. (Either
1519 * that, or I have no idea what this is for.)
1520 */
1521static JdwpError handleCOR_ReflectedType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1522 RefTypeId classObjectId = ReadRefTypeId(&buf);
1523
Elliott Hughesa2155262011-11-16 16:26:58 -08001524 LOG(VERBOSE) << StringPrintf(" Req for refTypeId for class=%llx (%s)", classObjectId, Dbg::GetClassDescriptor(classObjectId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001525
1526 /* just hand the type back to them */
1527 if (Dbg::IsInterface(classObjectId)) {
1528 expandBufAdd1(pReply, TT_INTERFACE);
1529 } else {
1530 expandBufAdd1(pReply, TT_CLASS);
1531 }
1532 expandBufAddRefTypeId(pReply, classObjectId);
1533
1534 return ERR_NONE;
1535}
1536
1537/*
1538 * Handle a DDM packet with a single chunk in it.
1539 */
1540static JdwpError handleDDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1541 uint8_t* replyBuf = NULL;
1542 int replyLen = -1;
1543
1544 LOG(VERBOSE) << StringPrintf(" Handling DDM packet (%.4s)", buf);
1545
1546 /*
1547 * On first DDM packet, notify all handlers that DDM is running.
1548 */
1549 if (!state->ddmActive) {
1550 state->ddmActive = true;
1551 Dbg::DdmConnected();
1552 }
1553
1554 /*
1555 * If they want to send something back, we copy it into the buffer.
1556 * A no-copy approach would be nicer.
1557 *
1558 * TODO: consider altering the JDWP stuff to hold the packet header
1559 * in a separate buffer. That would allow us to writev() DDM traffic
1560 * instead of copying it into the expanding buffer. The reduction in
1561 * heap requirements is probably more valuable than the efficiency.
1562 */
1563 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1564 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1565 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1566 free(replyBuf);
1567 }
1568 return ERR_NONE;
1569}
1570
1571/*
1572 * Handler map decl.
1573 */
1574typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1575
1576struct JdwpHandlerMap {
1577 uint8_t cmdSet;
1578 uint8_t cmd;
1579 JdwpRequestHandler func;
1580 const char* descr;
1581};
1582
1583/*
1584 * Map commands to functions.
1585 *
1586 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1587 * and 128-256 are vendor-defined.
1588 */
1589static const JdwpHandlerMap gHandlerMap[] = {
1590 /* VirtualMachine command set (1) */
1591 { 1, 1, handleVM_Version, "VirtualMachine.Version" },
1592 { 1, 2, handleVM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1593 //1, 3, VirtualMachine.AllClasses
1594 { 1, 4, handleVM_AllThreads, "VirtualMachine.AllThreads" },
1595 { 1, 5, handleVM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1596 { 1, 6, handleVM_Dispose, "VirtualMachine.Dispose" },
1597 { 1, 7, handleVM_IDSizes, "VirtualMachine.IDSizes" },
1598 { 1, 8, handleVM_Suspend, "VirtualMachine.Suspend" },
1599 { 1, 9, handleVM_Resume, "VirtualMachine.Resume" },
1600 { 1, 10, handleVM_Exit, "VirtualMachine.Exit" },
1601 { 1, 11, handleVM_CreateString, "VirtualMachine.CreateString" },
1602 { 1, 12, handleVM_Capabilities, "VirtualMachine.Capabilities" },
1603 { 1, 13, handleVM_ClassPaths, "VirtualMachine.ClassPaths" },
1604 { 1, 14, HandleVM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1605 //1, 15, HoldEvents
1606 //1, 16, ReleaseEvents
1607 { 1, 17, handleVM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1608 //1, 18, RedefineClasses
1609 //1, 19, SetDefaultStratum
1610 { 1, 20, handleVM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric"},
1611 //1, 21, InstanceCounts
1612
1613 /* ReferenceType command set (2) */
1614 { 2, 1, handleRT_Signature, "ReferenceType.Signature" },
1615 { 2, 2, handleRT_ClassLoader, "ReferenceType.ClassLoader" },
1616 { 2, 3, handleRT_Modifiers, "ReferenceType.Modifiers" },
1617 //2, 4, Fields
1618 //2, 5, Methods
1619 { 2, 6, handleRT_GetValues, "ReferenceType.GetValues" },
1620 { 2, 7, handleRT_SourceFile, "ReferenceType.SourceFile" },
1621 //2, 8, NestedTypes
1622 { 2, 9, handleRT_Status, "ReferenceType.Status" },
1623 { 2, 10, handleRT_Interfaces, "ReferenceType.Interfaces" },
1624 { 2, 11, handleRT_ClassObject, "ReferenceType.ClassObject" },
1625 { 2, 12, handleRT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1626 { 2, 13, handleRT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1627 { 2, 14, handleRT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1628 { 2, 15, handleRT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
1629 //2, 16, Instances
1630 //2, 17, ClassFileVersion
1631 //2, 18, ConstantPool
1632
1633 /* ClassType command set (3) */
1634 { 3, 1, handleCT_Superclass, "ClassType.Superclass" },
1635 { 3, 2, handleCT_SetValues, "ClassType.SetValues" },
1636 { 3, 3, handleCT_InvokeMethod, "ClassType.InvokeMethod" },
1637 { 3, 4, handleCT_NewInstance, "ClassType.NewInstance" },
1638
1639 /* ArrayType command set (4) */
1640 { 4, 1, handleAT_newInstance, "ArrayType.NewInstance" },
1641
1642 /* InterfaceType command set (5) */
1643
1644 /* Method command set (6) */
1645 { 6, 1, handleM_LineTable, "Method.LineTable" },
1646 //6, 2, VariableTable
1647 //6, 3, Bytecodes
1648 //6, 4, IsObsolete
1649 { 6, 5, handleM_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
1650
1651 /* Field command set (8) */
1652
1653 /* ObjectReference command set (9) */
1654 { 9, 1, handleOR_ReferenceType, "ObjectReference.ReferenceType" },
1655 { 9, 2, handleOR_GetValues, "ObjectReference.GetValues" },
1656 { 9, 3, handleOR_SetValues, "ObjectReference.SetValues" },
1657 //9, 4, (not defined)
1658 //9, 5, MonitorInfo
1659 { 9, 6, handleOR_InvokeMethod, "ObjectReference.InvokeMethod" },
1660 { 9, 7, handleOR_DisableCollection, "ObjectReference.DisableCollection" },
1661 { 9, 8, handleOR_EnableCollection, "ObjectReference.EnableCollection" },
1662 { 9, 9, handleOR_IsCollected, "ObjectReference.IsCollected" },
1663 //9, 10, ReferringObjects
1664
1665 /* StringReference command set (10) */
1666 { 10, 1, handleSR_Value, "StringReference.Value" },
1667
1668 /* ThreadReference command set (11) */
1669 { 11, 1, handleTR_Name, "ThreadReference.Name" },
1670 { 11, 2, handleTR_Suspend, "ThreadReference.Suspend" },
1671 { 11, 3, handleTR_Resume, "ThreadReference.Resume" },
1672 { 11, 4, handleTR_Status, "ThreadReference.Status" },
1673 { 11, 5, handleTR_ThreadGroup, "ThreadReference.ThreadGroup" },
1674 { 11, 6, handleTR_Frames, "ThreadReference.Frames" },
1675 { 11, 7, handleTR_FrameCount, "ThreadReference.FrameCount" },
1676 //11, 8, OwnedMonitors
1677 { 11, 9, handleTR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1678 //11, 10, Stop
1679 //11, 11, Interrupt
1680 { 11, 12, handleTR_SuspendCount, "ThreadReference.SuspendCount" },
1681 //11, 13, OwnedMonitorsStackDepthInfo
1682 //11, 14, ForceEarlyReturn
1683
1684 /* ThreadGroupReference command set (12) */
1685 { 12, 1, handleTGR_Name, "ThreadGroupReference.Name" },
1686 { 12, 2, handleTGR_Parent, "ThreadGroupReference.Parent" },
1687 { 12, 3, handleTGR_Children, "ThreadGroupReference.Children" },
1688
1689 /* ArrayReference command set (13) */
1690 { 13, 1, handleAR_Length, "ArrayReference.Length" },
1691 { 13, 2, handleAR_GetValues, "ArrayReference.GetValues" },
1692 { 13, 3, handleAR_SetValues, "ArrayReference.SetValues" },
1693
1694 /* ClassLoaderReference command set (14) */
1695 { 14, 1, handleCLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
1696
1697 /* EventRequest command set (15) */
1698 { 15, 1, handleER_Set, "EventRequest.Set" },
1699 { 15, 2, handleER_Clear, "EventRequest.Clear" },
1700 //15, 3, ClearAllBreakpoints
1701
1702 /* StackFrame command set (16) */
1703 { 16, 1, handleSF_GetValues, "StackFrame.GetValues" },
1704 { 16, 2, handleSF_SetValues, "StackFrame.SetValues" },
1705 { 16, 3, handleSF_ThisObject, "StackFrame.ThisObject" },
1706 //16, 4, PopFrames
1707
1708 /* ClassObjectReference command set (17) */
1709 { 17, 1, handleCOR_ReflectedType,"ClassObjectReference.ReflectedType" },
1710
1711 /* Event command set (64) */
1712 //64, 100, Composite <-- sent from VM to debugger, never received by VM
1713
1714 { 199, 1, handleDDM_Chunk, "DDM.Chunk" },
1715};
1716
1717/*
1718 * Process a request from the debugger.
1719 *
1720 * On entry, the JDWP thread is in VMWAIT.
1721 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001722void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001723 JdwpError result = ERR_NONE;
1724 int i, respLen;
1725
1726 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1727 /*
1728 * Activity from a debugger, not merely ddms. Mark us as having an
1729 * active debugger session, and zero out the last-activity timestamp
1730 * so waitForDebugger() doesn't return if we stall for a bit here.
1731 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001732 Dbg::GoActive();
Elliott Hughes376a7a02011-10-24 18:35:55 -07001733 QuasiAtomicSwap64(0, &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001734 }
1735
1736 /*
1737 * If a debugger event has fired in another thread, wait until the
1738 * initiating thread has suspended itself before processing messages
1739 * from the debugger. Otherwise we (the JDWP thread) could be told to
1740 * resume the thread before it has suspended.
1741 *
1742 * We call with an argument of zero to wait for the current event
1743 * thread to finish, and then clear the block. Depending on the thread
1744 * suspend policy, this may allow events in other threads to fire,
1745 * but those events have no bearing on what the debugger has sent us
1746 * in the current request.
1747 *
1748 * Note that we MUST clear the event token before waking the event
1749 * thread up, or risk waiting for the thread to suspend after we've
1750 * told it to resume.
1751 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001752 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001753
1754 /*
1755 * Tell the VM that we're running and shouldn't be interrupted by GC.
1756 * Do this after anything that can stall indefinitely.
1757 */
1758 Dbg::ThreadRunning();
1759
1760 expandBufAddSpace(pReply, kJDWPHeaderLen);
1761
1762 for (i = 0; i < (int) arraysize(gHandlerMap); i++) {
1763 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd) {
1764 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 -07001765 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001766 break;
1767 }
1768 }
1769 if (i == arraysize(gHandlerMap)) {
1770 LOG(ERROR) << StringPrintf("REQ: UNSUPPORTED (cmd=%d/%d dataLen=%d id=0x%06x)", pHeader->cmdSet, pHeader->cmd, dataLen, pHeader->id);
1771 if (dataLen > 0) {
1772 HexDump(buf, dataLen);
1773 }
1774 LOG(FATAL) << "command not implemented"; // make it *really* obvious
1775 result = ERR_NOT_IMPLEMENTED;
1776 }
1777
1778 /*
1779 * Set up the reply header.
1780 *
1781 * If we encountered an error, only send the header back.
1782 */
1783 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001784 Set4BE(replyBuf + 4, pHeader->id);
1785 Set1(replyBuf + 8, kJDWPFlagReply);
1786 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001787 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001788 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001789 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001790 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001791 }
1792
1793 respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
1794 if (false) {
1795 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
1796 if (respLen > 0) {
1797 HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
1798 }
1799 }
1800
1801 /*
1802 * Update last-activity timestamp. We really only need this during
1803 * the initial setup. Only update if this is a non-DDMS packet.
1804 */
1805 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07001806 QuasiAtomicSwap64(MilliTime(), &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001807 }
1808
1809 /* tell the VM that GC is okay again */
1810 Dbg::ThreadWaiting();
1811}
1812
1813} // namespace JDWP
1814
1815} // namespace art