blob: 23637db8e60215b25362f7d5ca5a580c8c764c18 [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
Elliott Hughes761928d2011-11-16 18:33:03 -0800994 size_t frameCount = Dbg::GetThreadFrameCount(threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995
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 }
Elliott Hughes761928d2011-11-16 18:33:03 -08001003 CHECK_GE(startFrame, 0U);
1004 CHECK_LT(startFrame, frameCount);
1005 CHECK_LE(startFrame + length, frameCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001006
1007 uint32_t frames = length;
1008 expandBufAdd4BE(pReply, frames);
1009 for (uint32_t i = startFrame; i < (startFrame+length); i++) {
1010 FrameId frameId;
1011 JdwpLocation loc;
1012
1013 Dbg::GetThreadFrame(threadId, i, &frameId, &loc);
1014
1015 expandBufAdd8BE(pReply, frameId);
1016 AddLocation(pReply, &loc);
1017
1018 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);
1019 }
1020
1021 return ERR_NONE;
1022}
1023
1024/*
1025 * Returns the #of frames on the specified thread, which must be suspended.
1026 */
1027static JdwpError handleTR_FrameCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1028 ObjectId threadId = ReadObjectId(&buf);
1029
1030 if (!Dbg::ThreadExists(threadId)) {
1031 return ERR_INVALID_THREAD;
1032 }
1033 if (!Dbg::IsSuspended(threadId)) {
1034 LOG(VERBOSE) << StringPrintf(" Rejecting req for frames in running thread '%s' (%llx)", Dbg::GetThreadName(threadId), threadId);
1035 return ERR_THREAD_NOT_SUSPENDED;
1036 }
1037
1038 int frameCount = Dbg::GetThreadFrameCount(threadId);
1039 if (frameCount < 0) {
1040 return ERR_INVALID_THREAD;
1041 }
1042 expandBufAdd4BE(pReply, (uint32_t)frameCount);
1043
1044 return ERR_NONE;
1045}
1046
1047/*
1048 * Get the monitor that the thread is waiting on.
1049 */
1050static JdwpError handleTR_CurrentContendedMonitor(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1051 ObjectId threadId;
1052
1053 threadId = ReadObjectId(&buf);
1054
1055 // TODO: create an Object to represent the monitor (we're currently
1056 // just using a raw Monitor struct in the VM)
1057
1058 return ERR_NOT_IMPLEMENTED;
1059}
1060
1061/*
1062 * Return the suspend count for the specified thread.
1063 *
1064 * (The thread *might* still be running -- it might not have examined
1065 * its suspend count recently.)
1066 */
1067static JdwpError handleTR_SuspendCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1068 ObjectId threadId = ReadObjectId(&buf);
1069
1070 uint32_t suspendCount = Dbg::GetThreadSuspendCount(threadId);
1071 expandBufAdd4BE(pReply, suspendCount);
1072
1073 return ERR_NONE;
1074}
1075
1076/*
1077 * Return the name of a thread group.
1078 *
1079 * The Eclipse debugger recognizes "main" and "system" as special.
1080 */
1081static JdwpError handleTGR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1082 ObjectId threadGroupId = ReadObjectId(&buf);
1083 LOG(VERBOSE) << StringPrintf(" Req for name of threadGroupId=0x%llx", threadGroupId);
1084
1085 char* name = Dbg::GetThreadGroupName(threadGroupId);
1086 if (name != NULL) {
Elliott Hughesa2155262011-11-16 16:26:58 -08001087 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001088 } else {
Elliott Hughesa2155262011-11-16 16:26:58 -08001089 expandBufAddUtf8String(pReply, "BAD-GROUP-ID");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001090 LOG(VERBOSE) << StringPrintf("bad thread group ID");
1091 }
1092
1093 free(name);
1094
1095 return ERR_NONE;
1096}
1097
1098/*
1099 * Returns the thread group -- if any -- that contains the specified
1100 * thread group.
1101 */
1102static JdwpError handleTGR_Parent(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1103 ObjectId groupId = ReadObjectId(&buf);
1104
1105 ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId);
1106 expandBufAddObjectId(pReply, parentGroup);
1107
1108 return ERR_NONE;
1109}
1110
1111/*
1112 * Return the active threads and thread groups that are part of the
1113 * specified thread group.
1114 */
1115static JdwpError handleTGR_Children(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1116 ObjectId threadGroupId = ReadObjectId(&buf);
1117 LOG(VERBOSE) << StringPrintf(" Req for threads in threadGroupId=0x%llx", threadGroupId);
1118
1119 ObjectId* pThreadIds;
1120 uint32_t threadCount;
1121 Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount);
1122
1123 expandBufAdd4BE(pReply, threadCount);
1124
1125 for (uint32_t i = 0; i < threadCount; i++) {
1126 expandBufAddObjectId(pReply, pThreadIds[i]);
1127 }
1128 free(pThreadIds);
1129
1130 /*
1131 * TODO: finish support for child groups
1132 *
1133 * For now, just show that "main" is a child of "system".
1134 */
1135 if (threadGroupId == Dbg::GetSystemThreadGroupId()) {
1136 expandBufAdd4BE(pReply, 1);
1137 expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId());
1138 } else {
1139 expandBufAdd4BE(pReply, 0);
1140 }
1141
1142 return ERR_NONE;
1143}
1144
1145/*
1146 * Return the #of components in the array.
1147 */
1148static JdwpError handleAR_Length(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1149 ObjectId arrayId = ReadObjectId(&buf);
1150 LOG(VERBOSE) << StringPrintf(" Req for length of array 0x%llx", arrayId);
1151
1152 uint32_t arrayLength = Dbg::GetArrayLength(arrayId);
1153
1154 LOG(VERBOSE) << StringPrintf(" --> %d", arrayLength);
1155
1156 expandBufAdd4BE(pReply, arrayLength);
1157
1158 return ERR_NONE;
1159}
1160
1161/*
1162 * Return the values from an array.
1163 */
1164static JdwpError handleAR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1165 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001166 uint32_t firstIndex = Read4BE(&buf);
1167 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001168
1169 uint8_t tag = Dbg::GetArrayElementTag(arrayId);
1170 LOG(VERBOSE) << StringPrintf(" Req for array values 0x%llx first=%d len=%d (elem tag=%c)", arrayId, firstIndex, length, tag);
1171
1172 expandBufAdd1(pReply, tag);
1173 expandBufAdd4BE(pReply, length);
1174
1175 if (!Dbg::OutputArray(arrayId, firstIndex, length, pReply)) {
1176 return ERR_INVALID_LENGTH;
1177 }
1178
1179 return ERR_NONE;
1180}
1181
1182/*
1183 * Set values in an array.
1184 */
1185static JdwpError handleAR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1186 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001187 uint32_t firstIndex = Read4BE(&buf);
1188 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001189
1190 LOG(VERBOSE) << StringPrintf(" Req to set array values 0x%llx first=%d count=%d", arrayId, firstIndex, values);
1191
1192 if (!Dbg::SetArrayElements(arrayId, firstIndex, values, buf)) {
1193 return ERR_INVALID_LENGTH;
1194 }
1195
1196 return ERR_NONE;
1197}
1198
1199/*
1200 * Return the set of classes visible to a class loader. All classes which
1201 * have the class loader as a defining or initiating loader are returned.
1202 */
1203static JdwpError handleCLR_VisibleClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1204 ObjectId classLoaderObject;
1205 uint32_t numClasses = 0;
1206 RefTypeId* classRefBuf = NULL;
1207 int i;
1208
1209 classLoaderObject = ReadObjectId(&buf);
1210
1211 Dbg::GetVisibleClassList(classLoaderObject, &numClasses, &classRefBuf);
1212
1213 expandBufAdd4BE(pReply, numClasses);
1214 for (i = 0; i < (int) numClasses; i++) {
1215 uint8_t refTypeTag = Dbg::GetClassObjectType(classRefBuf[i]);
1216
1217 expandBufAdd1(pReply, refTypeTag);
1218 expandBufAddRefTypeId(pReply, classRefBuf[i]);
1219 }
1220
1221 return ERR_NONE;
1222}
1223
1224/*
1225 * Return a newly-allocated string in which all occurrences of '.' have
1226 * been changed to '/'. If we find a '/' in the original string, NULL
1227 * is returned to avoid ambiguity.
1228 */
1229char* dvmDotToSlash(const char* str) {
1230 char* newStr = strdup(str);
1231 char* cp = newStr;
1232
1233 if (newStr == NULL) {
1234 return NULL;
1235 }
1236
1237 while (*cp != '\0') {
1238 if (*cp == '/') {
1239 CHECK(false);
1240 return NULL;
1241 }
1242 if (*cp == '.') {
1243 *cp = '/';
1244 }
1245 cp++;
1246 }
1247
1248 return newStr;
1249}
1250
1251/*
1252 * Set an event trigger.
1253 *
1254 * Reply with a requestID.
1255 */
1256static JdwpError handleER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1257 const uint8_t* origBuf = buf;
1258
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001259 uint8_t eventKind = Read1(&buf);
1260 uint8_t suspendPolicy = Read1(&buf);
1261 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001262
1263 LOG(VERBOSE) << " Set(kind=" << JdwpEventKind(eventKind)
1264 << " suspend=" << JdwpSuspendPolicy(suspendPolicy)
1265 << " mods=" << modifierCount << ")";
1266
1267 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1268
1269 JdwpEvent* pEvent = EventAlloc(modifierCount);
1270 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
1271 pEvent->suspendPolicy = static_cast<JdwpSuspendPolicy>(suspendPolicy);
1272 pEvent->modCount = modifierCount;
1273
1274 /*
1275 * Read modifiers. Ordering may be significant (see explanation of Count
1276 * mods in JDWP doc).
1277 */
1278 for (uint32_t idx = 0; idx < modifierCount; idx++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001279 uint8_t modKind = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001280
1281 pEvent->mods[idx].modKind = modKind;
1282
1283 switch (modKind) {
1284 case MK_COUNT: /* report once, when "--count" reaches 0 */
1285 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001286 uint32_t count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001287 LOG(VERBOSE) << " Count: " << count;
1288 if (count == 0) {
1289 return ERR_INVALID_COUNT;
1290 }
1291 pEvent->mods[idx].count.count = count;
1292 }
1293 break;
1294 case MK_CONDITIONAL: /* conditional on expression) */
1295 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001296 uint32_t exprId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001297 LOG(VERBOSE) << " Conditional: " << exprId;
1298 pEvent->mods[idx].conditional.exprId = exprId;
1299 }
1300 break;
1301 case MK_THREAD_ONLY: /* only report events in specified thread */
1302 {
1303 ObjectId threadId = ReadObjectId(&buf);
1304 LOG(VERBOSE) << StringPrintf(" ThreadOnly: %llx", threadId);
1305 pEvent->mods[idx].threadOnly.threadId = threadId;
1306 }
1307 break;
1308 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1309 {
1310 RefTypeId clazzId = ReadRefTypeId(&buf);
Elliott Hughesa2155262011-11-16 16:26:58 -08001311 LOG(VERBOSE) << StringPrintf(" ClassOnly: %llx (%s)", clazzId, Dbg::GetClassDescriptor(clazzId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001312 pEvent->mods[idx].classOnly.refTypeId = clazzId;
1313 }
1314 break;
1315 case MK_CLASS_MATCH: /* restrict events to matching classes */
1316 {
1317 char* pattern;
1318 size_t strLen;
1319
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001320 pattern = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001321 LOG(VERBOSE) << StringPrintf(" ClassMatch: '%s'", pattern);
1322 /* pattern is "java.foo.*", we want "java/foo/ *" */
1323 pEvent->mods[idx].classMatch.classPattern = dvmDotToSlash(pattern);
1324 free(pattern);
1325 }
1326 break;
1327 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1328 {
1329 char* pattern;
1330 size_t strLen;
1331
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001332 pattern = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001333 LOG(VERBOSE) << StringPrintf(" ClassExclude: '%s'", pattern);
1334 pEvent->mods[idx].classExclude.classPattern = dvmDotToSlash(pattern);
1335 free(pattern);
1336 }
1337 break;
1338 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1339 {
1340 JdwpLocation loc;
1341
1342 jdwpReadLocation(&buf, &loc);
1343 LOG(VERBOSE) << StringPrintf(" LocationOnly: typeTag=%d classId=%llx methodId=%x idx=%llx",
1344 loc.typeTag, loc.classId, loc.methodId, loc.idx);
1345 pEvent->mods[idx].locationOnly.loc = loc;
1346 }
1347 break;
1348 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1349 {
1350 RefTypeId exceptionOrNull; /* null == all exceptions */
1351 uint8_t caught, uncaught;
1352
1353 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001354 caught = Read1(&buf);
1355 uncaught = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001356 LOG(VERBOSE) << StringPrintf(" ExceptionOnly: type=%llx(%s) caught=%d uncaught=%d",
Elliott Hughesa2155262011-11-16 16:26:58 -08001357 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassDescriptor(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001358
1359 pEvent->mods[idx].exceptionOnly.refTypeId = exceptionOrNull;
1360 pEvent->mods[idx].exceptionOnly.caught = caught;
1361 pEvent->mods[idx].exceptionOnly.uncaught = uncaught;
1362 }
1363 break;
1364 case MK_FIELD_ONLY: /* for field access/mod events */
1365 {
1366 RefTypeId declaring = ReadRefTypeId(&buf);
1367 FieldId fieldId = ReadFieldId(&buf);
1368 LOG(VERBOSE) << StringPrintf(" FieldOnly: %llx %x", declaring, fieldId);
1369 pEvent->mods[idx].fieldOnly.refTypeId = declaring;
1370 pEvent->mods[idx].fieldOnly.fieldId = fieldId;
1371 }
1372 break;
1373 case MK_STEP: /* for use with EK_SINGLE_STEP */
1374 {
1375 ObjectId threadId;
1376 uint32_t size, depth;
1377
1378 threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001379 size = Read4BE(&buf);
1380 depth = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001381 LOG(VERBOSE) << StringPrintf(" Step: thread=%llx", threadId)
1382 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1383
1384 pEvent->mods[idx].step.threadId = threadId;
1385 pEvent->mods[idx].step.size = size;
1386 pEvent->mods[idx].step.depth = depth;
1387 }
1388 break;
1389 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1390 {
1391 ObjectId instance = ReadObjectId(&buf);
1392 LOG(VERBOSE) << StringPrintf(" InstanceOnly: %llx", instance);
1393 pEvent->mods[idx].instanceOnly.objectId = instance;
1394 }
1395 break;
1396 default:
1397 LOG(WARNING) << "GLITCH: unsupported modKind=" << modKind;
1398 break;
1399 }
1400 }
1401
1402 /*
1403 * Make sure we consumed all data. It is possible that the remote side
1404 * has sent us bad stuff, but for now we blame ourselves.
1405 */
1406 if (buf != origBuf + dataLen) {
1407 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1408 }
1409
1410 /*
1411 * We reply with an integer "requestID".
1412 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001413 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001414 expandBufAdd4BE(pReply, requestId);
1415
1416 pEvent->requestId = requestId;
1417
1418 LOG(VERBOSE) << StringPrintf(" --> event requestId=%#x", requestId);
1419
1420 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001421 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001422 if (err != ERR_NONE) {
1423 /* registration failed, probably because event is bogus */
1424 EventFree(pEvent);
1425 LOG(WARNING) << "WARNING: event request rejected";
1426 }
1427 return err;
1428}
1429
1430/*
1431 * Clear an event. Failure to find an event with a matching ID is a no-op
1432 * and does not return an error.
1433 */
1434static JdwpError handleER_Clear(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1435 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001436 eventKind = Read1(&buf);
1437 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001438
1439 LOG(VERBOSE) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
1440
Elliott Hughes761928d2011-11-16 18:33:03 -08001441 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001442
1443 return ERR_NONE;
1444}
1445
1446/*
1447 * Return the values of arguments and local variables.
1448 */
1449static JdwpError handleSF_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1450 ObjectId threadId = ReadObjectId(&buf);
1451 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001452 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001453
1454 LOG(VERBOSE) << StringPrintf(" Req for %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
1455
1456 expandBufAdd4BE(pReply, slots); /* "int values" */
1457 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001458 uint32_t slot = Read4BE(&buf);
1459 uint8_t reqSigByte = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001460
1461 LOG(VERBOSE) << StringPrintf(" --> slot %d '%c'", slot, reqSigByte);
1462
1463 int width = Dbg::GetTagWidth(reqSigByte);
1464 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
1465 Dbg::GetLocalValue(threadId, frameId, slot, reqSigByte, ptr, width);
1466 }
1467
1468 return ERR_NONE;
1469}
1470
1471/*
1472 * Set the values of arguments and local variables.
1473 */
1474static JdwpError handleSF_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1475 ObjectId threadId = ReadObjectId(&buf);
1476 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001477 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001478
1479 LOG(VERBOSE) << StringPrintf(" Req to set %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
1480
1481 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001482 uint32_t slot = Read4BE(&buf);
1483 uint8_t sigByte = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001484 int width = Dbg::GetTagWidth(sigByte);
1485 uint64_t value = jdwpReadValue(&buf, width);
1486
1487 LOG(VERBOSE) << StringPrintf(" --> slot %d '%c' %llx", slot, sigByte, value);
1488 Dbg::SetLocalValue(threadId, frameId, slot, sigByte, value, width);
1489 }
1490
1491 return ERR_NONE;
1492}
1493
1494/*
1495 * Returns the value of "this" for the specified frame.
1496 */
1497static JdwpError handleSF_ThisObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1498 ObjectId threadId = ReadObjectId(&buf);
1499 FrameId frameId = ReadFrameId(&buf);
1500
1501 ObjectId objectId;
1502 if (!Dbg::GetThisObject(threadId, frameId, &objectId)) {
1503 return ERR_INVALID_FRAMEID;
1504 }
1505
1506 uint8_t objectTag = Dbg::GetObjectTag(objectId);
1507 LOG(VERBOSE) << StringPrintf(" Req for 'this' in thread=%llx frame=%llx --> %llx %s '%c'", threadId, frameId, objectId, Dbg::GetObjectTypeName(objectId), (char)objectTag);
1508
1509 expandBufAdd1(pReply, objectTag);
1510 expandBufAddObjectId(pReply, objectId);
1511
1512 return ERR_NONE;
1513}
1514
1515/*
1516 * Return the reference type reflected by this class object.
1517 *
1518 * This appears to be required because ReferenceTypeId values are NEVER
1519 * reused, whereas ClassIds can be recycled like any other object. (Either
1520 * that, or I have no idea what this is for.)
1521 */
1522static JdwpError handleCOR_ReflectedType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1523 RefTypeId classObjectId = ReadRefTypeId(&buf);
1524
Elliott Hughesa2155262011-11-16 16:26:58 -08001525 LOG(VERBOSE) << StringPrintf(" Req for refTypeId for class=%llx (%s)", classObjectId, Dbg::GetClassDescriptor(classObjectId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001526
1527 /* just hand the type back to them */
1528 if (Dbg::IsInterface(classObjectId)) {
1529 expandBufAdd1(pReply, TT_INTERFACE);
1530 } else {
1531 expandBufAdd1(pReply, TT_CLASS);
1532 }
1533 expandBufAddRefTypeId(pReply, classObjectId);
1534
1535 return ERR_NONE;
1536}
1537
1538/*
1539 * Handle a DDM packet with a single chunk in it.
1540 */
1541static JdwpError handleDDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1542 uint8_t* replyBuf = NULL;
1543 int replyLen = -1;
1544
1545 LOG(VERBOSE) << StringPrintf(" Handling DDM packet (%.4s)", buf);
1546
1547 /*
1548 * On first DDM packet, notify all handlers that DDM is running.
1549 */
1550 if (!state->ddmActive) {
1551 state->ddmActive = true;
1552 Dbg::DdmConnected();
1553 }
1554
1555 /*
1556 * If they want to send something back, we copy it into the buffer.
1557 * A no-copy approach would be nicer.
1558 *
1559 * TODO: consider altering the JDWP stuff to hold the packet header
1560 * in a separate buffer. That would allow us to writev() DDM traffic
1561 * instead of copying it into the expanding buffer. The reduction in
1562 * heap requirements is probably more valuable than the efficiency.
1563 */
1564 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1565 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1566 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1567 free(replyBuf);
1568 }
1569 return ERR_NONE;
1570}
1571
1572/*
1573 * Handler map decl.
1574 */
1575typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1576
1577struct JdwpHandlerMap {
1578 uint8_t cmdSet;
1579 uint8_t cmd;
1580 JdwpRequestHandler func;
1581 const char* descr;
1582};
1583
1584/*
1585 * Map commands to functions.
1586 *
1587 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1588 * and 128-256 are vendor-defined.
1589 */
1590static const JdwpHandlerMap gHandlerMap[] = {
1591 /* VirtualMachine command set (1) */
1592 { 1, 1, handleVM_Version, "VirtualMachine.Version" },
1593 { 1, 2, handleVM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1594 //1, 3, VirtualMachine.AllClasses
1595 { 1, 4, handleVM_AllThreads, "VirtualMachine.AllThreads" },
1596 { 1, 5, handleVM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1597 { 1, 6, handleVM_Dispose, "VirtualMachine.Dispose" },
1598 { 1, 7, handleVM_IDSizes, "VirtualMachine.IDSizes" },
1599 { 1, 8, handleVM_Suspend, "VirtualMachine.Suspend" },
1600 { 1, 9, handleVM_Resume, "VirtualMachine.Resume" },
1601 { 1, 10, handleVM_Exit, "VirtualMachine.Exit" },
1602 { 1, 11, handleVM_CreateString, "VirtualMachine.CreateString" },
1603 { 1, 12, handleVM_Capabilities, "VirtualMachine.Capabilities" },
1604 { 1, 13, handleVM_ClassPaths, "VirtualMachine.ClassPaths" },
1605 { 1, 14, HandleVM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1606 //1, 15, HoldEvents
1607 //1, 16, ReleaseEvents
1608 { 1, 17, handleVM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1609 //1, 18, RedefineClasses
1610 //1, 19, SetDefaultStratum
1611 { 1, 20, handleVM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric"},
1612 //1, 21, InstanceCounts
1613
1614 /* ReferenceType command set (2) */
1615 { 2, 1, handleRT_Signature, "ReferenceType.Signature" },
1616 { 2, 2, handleRT_ClassLoader, "ReferenceType.ClassLoader" },
1617 { 2, 3, handleRT_Modifiers, "ReferenceType.Modifiers" },
1618 //2, 4, Fields
1619 //2, 5, Methods
1620 { 2, 6, handleRT_GetValues, "ReferenceType.GetValues" },
1621 { 2, 7, handleRT_SourceFile, "ReferenceType.SourceFile" },
1622 //2, 8, NestedTypes
1623 { 2, 9, handleRT_Status, "ReferenceType.Status" },
1624 { 2, 10, handleRT_Interfaces, "ReferenceType.Interfaces" },
1625 { 2, 11, handleRT_ClassObject, "ReferenceType.ClassObject" },
1626 { 2, 12, handleRT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1627 { 2, 13, handleRT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1628 { 2, 14, handleRT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1629 { 2, 15, handleRT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
1630 //2, 16, Instances
1631 //2, 17, ClassFileVersion
1632 //2, 18, ConstantPool
1633
1634 /* ClassType command set (3) */
1635 { 3, 1, handleCT_Superclass, "ClassType.Superclass" },
1636 { 3, 2, handleCT_SetValues, "ClassType.SetValues" },
1637 { 3, 3, handleCT_InvokeMethod, "ClassType.InvokeMethod" },
1638 { 3, 4, handleCT_NewInstance, "ClassType.NewInstance" },
1639
1640 /* ArrayType command set (4) */
1641 { 4, 1, handleAT_newInstance, "ArrayType.NewInstance" },
1642
1643 /* InterfaceType command set (5) */
1644
1645 /* Method command set (6) */
1646 { 6, 1, handleM_LineTable, "Method.LineTable" },
1647 //6, 2, VariableTable
1648 //6, 3, Bytecodes
1649 //6, 4, IsObsolete
1650 { 6, 5, handleM_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
1651
1652 /* Field command set (8) */
1653
1654 /* ObjectReference command set (9) */
1655 { 9, 1, handleOR_ReferenceType, "ObjectReference.ReferenceType" },
1656 { 9, 2, handleOR_GetValues, "ObjectReference.GetValues" },
1657 { 9, 3, handleOR_SetValues, "ObjectReference.SetValues" },
1658 //9, 4, (not defined)
1659 //9, 5, MonitorInfo
1660 { 9, 6, handleOR_InvokeMethod, "ObjectReference.InvokeMethod" },
1661 { 9, 7, handleOR_DisableCollection, "ObjectReference.DisableCollection" },
1662 { 9, 8, handleOR_EnableCollection, "ObjectReference.EnableCollection" },
1663 { 9, 9, handleOR_IsCollected, "ObjectReference.IsCollected" },
1664 //9, 10, ReferringObjects
1665
1666 /* StringReference command set (10) */
1667 { 10, 1, handleSR_Value, "StringReference.Value" },
1668
1669 /* ThreadReference command set (11) */
1670 { 11, 1, handleTR_Name, "ThreadReference.Name" },
1671 { 11, 2, handleTR_Suspend, "ThreadReference.Suspend" },
1672 { 11, 3, handleTR_Resume, "ThreadReference.Resume" },
1673 { 11, 4, handleTR_Status, "ThreadReference.Status" },
1674 { 11, 5, handleTR_ThreadGroup, "ThreadReference.ThreadGroup" },
1675 { 11, 6, handleTR_Frames, "ThreadReference.Frames" },
1676 { 11, 7, handleTR_FrameCount, "ThreadReference.FrameCount" },
1677 //11, 8, OwnedMonitors
1678 { 11, 9, handleTR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1679 //11, 10, Stop
1680 //11, 11, Interrupt
1681 { 11, 12, handleTR_SuspendCount, "ThreadReference.SuspendCount" },
1682 //11, 13, OwnedMonitorsStackDepthInfo
1683 //11, 14, ForceEarlyReturn
1684
1685 /* ThreadGroupReference command set (12) */
1686 { 12, 1, handleTGR_Name, "ThreadGroupReference.Name" },
1687 { 12, 2, handleTGR_Parent, "ThreadGroupReference.Parent" },
1688 { 12, 3, handleTGR_Children, "ThreadGroupReference.Children" },
1689
1690 /* ArrayReference command set (13) */
1691 { 13, 1, handleAR_Length, "ArrayReference.Length" },
1692 { 13, 2, handleAR_GetValues, "ArrayReference.GetValues" },
1693 { 13, 3, handleAR_SetValues, "ArrayReference.SetValues" },
1694
1695 /* ClassLoaderReference command set (14) */
1696 { 14, 1, handleCLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
1697
1698 /* EventRequest command set (15) */
1699 { 15, 1, handleER_Set, "EventRequest.Set" },
1700 { 15, 2, handleER_Clear, "EventRequest.Clear" },
1701 //15, 3, ClearAllBreakpoints
1702
1703 /* StackFrame command set (16) */
1704 { 16, 1, handleSF_GetValues, "StackFrame.GetValues" },
1705 { 16, 2, handleSF_SetValues, "StackFrame.SetValues" },
1706 { 16, 3, handleSF_ThisObject, "StackFrame.ThisObject" },
1707 //16, 4, PopFrames
1708
1709 /* ClassObjectReference command set (17) */
1710 { 17, 1, handleCOR_ReflectedType,"ClassObjectReference.ReflectedType" },
1711
1712 /* Event command set (64) */
1713 //64, 100, Composite <-- sent from VM to debugger, never received by VM
1714
1715 { 199, 1, handleDDM_Chunk, "DDM.Chunk" },
1716};
1717
1718/*
1719 * Process a request from the debugger.
1720 *
1721 * On entry, the JDWP thread is in VMWAIT.
1722 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001723void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001724 JdwpError result = ERR_NONE;
1725 int i, respLen;
1726
1727 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1728 /*
1729 * Activity from a debugger, not merely ddms. Mark us as having an
1730 * active debugger session, and zero out the last-activity timestamp
1731 * so waitForDebugger() doesn't return if we stall for a bit here.
1732 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001733 Dbg::GoActive();
Elliott Hughes376a7a02011-10-24 18:35:55 -07001734 QuasiAtomicSwap64(0, &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001735 }
1736
1737 /*
1738 * If a debugger event has fired in another thread, wait until the
1739 * initiating thread has suspended itself before processing messages
1740 * from the debugger. Otherwise we (the JDWP thread) could be told to
1741 * resume the thread before it has suspended.
1742 *
1743 * We call with an argument of zero to wait for the current event
1744 * thread to finish, and then clear the block. Depending on the thread
1745 * suspend policy, this may allow events in other threads to fire,
1746 * but those events have no bearing on what the debugger has sent us
1747 * in the current request.
1748 *
1749 * Note that we MUST clear the event token before waking the event
1750 * thread up, or risk waiting for the thread to suspend after we've
1751 * told it to resume.
1752 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001753 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001754
1755 /*
1756 * Tell the VM that we're running and shouldn't be interrupted by GC.
1757 * Do this after anything that can stall indefinitely.
1758 */
1759 Dbg::ThreadRunning();
1760
1761 expandBufAddSpace(pReply, kJDWPHeaderLen);
1762
1763 for (i = 0; i < (int) arraysize(gHandlerMap); i++) {
1764 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd) {
1765 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 -07001766 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001767 break;
1768 }
1769 }
1770 if (i == arraysize(gHandlerMap)) {
1771 LOG(ERROR) << StringPrintf("REQ: UNSUPPORTED (cmd=%d/%d dataLen=%d id=0x%06x)", pHeader->cmdSet, pHeader->cmd, dataLen, pHeader->id);
1772 if (dataLen > 0) {
1773 HexDump(buf, dataLen);
1774 }
1775 LOG(FATAL) << "command not implemented"; // make it *really* obvious
1776 result = ERR_NOT_IMPLEMENTED;
1777 }
1778
1779 /*
1780 * Set up the reply header.
1781 *
1782 * If we encountered an error, only send the header back.
1783 */
1784 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001785 Set4BE(replyBuf + 4, pHeader->id);
1786 Set1(replyBuf + 8, kJDWPFlagReply);
1787 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001788 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001789 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001790 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001791 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001792 }
1793
1794 respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
1795 if (false) {
1796 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
1797 if (respLen > 0) {
1798 HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
1799 }
1800 }
1801
1802 /*
1803 * Update last-activity timestamp. We really only need this during
1804 * the initial setup. Only update if this is a non-DDMS packet.
1805 */
1806 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07001807 QuasiAtomicSwap64(MilliTime(), &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001808 }
1809
1810 /* tell the VM that GC is okay again */
1811 Dbg::ThreadWaiting();
1812}
1813
1814} // namespace JDWP
1815
1816} // namespace art