blob: 04c4734f45ae362ac6c410ee2d2f0baeeea9d08c [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);
112 LOG(VERBOSE) << StringPrintf(" classId=%llx methodId=%x %s.%s", classId, methodId, Dbg::GetClassDescriptor(classId), Dbg::GetMethodName(classId, methodId));
113 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()));
181 expandBufAddUtf8String(pReply, (const uint8_t*) version.c_str());
182 /* JDWP version numbers */
183 expandBufAdd4BE(pReply, 1); // major
184 expandBufAdd4BE(pReply, 5); // minor
185 /* VM JRE version */
186 expandBufAddUtf8String(pReply, (const uint8_t*) "1.6.0"); /* e.g. 1.6.0_22 */
187 /* target VM name */
188 expandBufAddUtf8String(pReply, (const uint8_t*) "DalvikVM");
189
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
385 expandBufAddUtf8String(pReply, (const uint8_t*) baseDir);
386 expandBufAdd4BE(pReply, classPaths);
387 for (uint32_t i = 0; i < classPaths; i++) {
388 expandBufAddUtf8String(pReply, (const uint8_t*) ".");
389 }
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++) {
453 static const uint8_t genericSignature[1] = "";
454 uint8_t refTypeTag;
455 const char* signature;
456 uint32_t status;
457
458 Dbg::GetClassInfo(classRefBuf[i], &refTypeTag, &status, &signature);
459
460 expandBufAdd1(pReply, refTypeTag);
461 expandBufAddRefTypeId(pReply, classRefBuf[i]);
462 expandBufAddUtf8String(pReply, (const uint8_t*) signature);
463 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);
481 expandBufAddUtf8String(pReply, (const uint8_t*) signature);
482
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) {
522 expandBufAddUtf8String(pReply, (const uint8_t*) fileName);
523 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
549 LOG(VERBOSE) << StringPrintf(" Req for interfaces in %llx (%s)", refTypeId,
550 Dbg::GetClassDescriptor(refTypeId));
551
552 Dbg::OutputAllInterfaces(refTypeId, pReply);
553
554 return ERR_NONE;
555}
556
557/*
558 * Return the class object corresponding to this type.
559 */
560static JdwpError handleRT_ClassObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
561 RefTypeId refTypeId = ReadRefTypeId(&buf);
562 ObjectId classObjId = Dbg::GetClassObject(refTypeId);
563
564 LOG(VERBOSE) << StringPrintf(" RefTypeId %llx -> ObjectId %llx", refTypeId, classObjId);
565
566 expandBufAddObjectId(pReply, classObjId);
567
568 return ERR_NONE;
569}
570
571/*
572 * Returns the value of the SourceDebugExtension attribute.
573 *
574 * JDB seems interested, but DEX files don't currently support this.
575 */
576static JdwpError handleRT_SourceDebugExtension(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
577 /* referenceTypeId in, string out */
578 return ERR_ABSENT_INFORMATION;
579}
580
581/*
582 * Like RT_Signature but with the possibility of a "generic signature".
583 */
584static JdwpError handleRT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
585 static const uint8_t genericSignature[1] = "";
586
587 RefTypeId refTypeId = ReadRefTypeId(&buf);
588
589 LOG(VERBOSE) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
590 const char* signature = Dbg::GetSignature(refTypeId);
591 if (signature != NULL) {
592 expandBufAddUtf8String(pReply, (const uint8_t*) signature);
593 } else {
594 LOG(WARNING) << StringPrintf("No signature for refTypeId=0x%llx", refTypeId);
595 expandBufAddUtf8String(pReply, (const uint8_t*) "Lunknown;");
596 }
597 expandBufAddUtf8String(pReply, genericSignature);
598
599 return ERR_NONE;
600}
601
602/*
603 * Return the instance of java.lang.ClassLoader that loaded the specified
604 * reference type, or null if it was loaded by the system loader.
605 */
606static JdwpError handleRT_ClassLoader(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
607 RefTypeId refTypeId = ReadRefTypeId(&buf);
608
609 expandBufAddObjectId(pReply, Dbg::GetClassLoader(refTypeId));
610
611 return ERR_NONE;
612}
613
614/*
615 * Given a referenceTypeId, return a block of stuff that describes the
616 * fields declared by a class.
617 */
618static JdwpError handleRT_FieldsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
619 RefTypeId refTypeId = ReadRefTypeId(&buf);
620 LOG(VERBOSE) << StringPrintf(" Req for fields in refTypeId=0x%llx", refTypeId);
621 LOG(VERBOSE) << StringPrintf(" --> '%s'", Dbg::GetSignature(refTypeId));
622 Dbg::OutputAllFields(refTypeId, true, pReply);
623 return ERR_NONE;
624}
625
626/*
627 * Given a referenceTypeID, return a block of goodies describing the
628 * methods declared by a class.
629 */
630static JdwpError handleRT_MethodsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
631 RefTypeId refTypeId = ReadRefTypeId(&buf);
632
633 LOG(VERBOSE) << StringPrintf(" Req for methods in refTypeId=0x%llx", refTypeId);
634 LOG(VERBOSE) << StringPrintf(" --> '%s'", Dbg::GetSignature(refTypeId));
635
636 Dbg::OutputAllMethods(refTypeId, true, pReply);
637
638 return ERR_NONE;
639}
640
641/*
642 * Return the immediate superclass of a class.
643 */
644static JdwpError handleCT_Superclass(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
645 RefTypeId classId = ReadRefTypeId(&buf);
646
647 RefTypeId superClassId = Dbg::GetSuperclass(classId);
648
649 expandBufAddRefTypeId(pReply, superClassId);
650
651 return ERR_NONE;
652}
653
654/*
655 * Set static class values.
656 */
657static JdwpError handleCT_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
658 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700659 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700660
661 LOG(VERBOSE) << StringPrintf(" Req to set %d values in classId=%llx", values, classId);
662
663 for (uint32_t i = 0; i < values; i++) {
664 FieldId fieldId = ReadFieldId(&buf);
665 uint8_t fieldTag = Dbg::GetStaticFieldBasicTag(classId, fieldId);
666 int width = Dbg::GetTagWidth(fieldTag);
667 uint64_t value = jdwpReadValue(&buf, width);
668
669 LOG(VERBOSE) << StringPrintf(" --> field=%x tag=%c -> %lld", fieldId, fieldTag, value);
670 Dbg::SetStaticFieldValue(classId, fieldId, value, width);
671 }
672
673 return ERR_NONE;
674}
675
676/*
677 * Invoke a static method.
678 *
679 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
680 * values in the "variables" display.
681 */
682static JdwpError handleCT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
683 RefTypeId classId = ReadRefTypeId(&buf);
684 ObjectId threadId = ReadObjectId(&buf);
685 MethodId methodId = ReadMethodId(&buf);
686
687 return finishInvoke(state, buf, dataLen, pReply, threadId, 0, classId, methodId, false);
688}
689
690/*
691 * Create a new object of the requested type, and invoke the specified
692 * constructor.
693 *
694 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
695 * see the contents of a byte[] as a string.
696 */
697static JdwpError handleCT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
698 RefTypeId classId = ReadRefTypeId(&buf);
699 ObjectId threadId = ReadObjectId(&buf);
700 MethodId methodId = ReadMethodId(&buf);
701
702 LOG(VERBOSE) << "Creating instance of " << Dbg::GetClassDescriptor(classId);
703 ObjectId objectId = Dbg::CreateObject(classId);
704 if (objectId == 0) {
705 return ERR_OUT_OF_MEMORY;
706 }
707 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, true);
708}
709
710/*
711 * Create a new array object of the requested type and length.
712 */
713static JdwpError handleAT_newInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
714 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700715 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700716
717 LOG(VERBOSE) << StringPrintf("Creating array %s[%u]", Dbg::GetClassDescriptor(arrayTypeId), length);
718 ObjectId objectId = Dbg::CreateArrayObject(arrayTypeId, length);
719 if (objectId == 0) {
720 return ERR_OUT_OF_MEMORY;
721 }
722 expandBufAdd1(pReply, JT_ARRAY);
723 expandBufAddObjectId(pReply, objectId);
724 return ERR_NONE;
725}
726
727/*
728 * Return line number information for the method, if present.
729 */
730static JdwpError handleM_LineTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
731 RefTypeId refTypeId = ReadRefTypeId(&buf);
732 MethodId methodId = ReadMethodId(&buf);
733
734 LOG(VERBOSE) << StringPrintf(" Req for line table in %s.%s", Dbg::GetClassDescriptor(refTypeId), Dbg::GetMethodName(refTypeId,methodId));
735
736 Dbg::OutputLineTable(refTypeId, methodId, pReply);
737
738 return ERR_NONE;
739}
740
741/*
742 * Pull out the LocalVariableTable goodies.
743 */
744static JdwpError handleM_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
745 RefTypeId classId = ReadRefTypeId(&buf);
746 MethodId methodId = ReadMethodId(&buf);
747
748 LOG(VERBOSE) << StringPrintf(" Req for LocalVarTab in class=%s method=%s",
749 Dbg::GetClassDescriptor(classId),
750 Dbg::GetMethodName(classId, methodId));
751
752 /*
753 * We could return ERR_ABSENT_INFORMATION here if the DEX file was
754 * built without local variable information. That will cause Eclipse
755 * to make a best-effort attempt at displaying local variables
756 * anonymously. However, the attempt isn't very good, so we're probably
757 * better off just not showing anything.
758 */
759 Dbg::OutputVariableTable(classId, methodId, true, pReply);
760 return ERR_NONE;
761}
762
763/*
764 * Given an object reference, return the runtime type of the object
765 * (class or array).
766 *
767 * This can get called on different things, e.g. threadId gets
768 * passed in here.
769 */
770static JdwpError handleOR_ReferenceType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
771 ObjectId objectId = ReadObjectId(&buf);
772 LOG(VERBOSE) << StringPrintf(" Req for type of objectId=0x%llx", objectId);
773
774 uint8_t refTypeTag;
775 RefTypeId typeId;
776 Dbg::GetObjectType(objectId, &refTypeTag, &typeId);
777
778 expandBufAdd1(pReply, refTypeTag);
779 expandBufAddRefTypeId(pReply, typeId);
780
781 return ERR_NONE;
782}
783
784/*
785 * Get values from the fields of an object.
786 */
787static JdwpError handleOR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
788 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700789 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700790
791 LOG(VERBOSE) << StringPrintf(" Req for %d fields from objectId=0x%llx", numFields, objectId);
792
793 expandBufAdd4BE(pReply, numFields);
794
795 for (uint32_t i = 0; i < numFields; i++) {
796 FieldId fieldId = ReadFieldId(&buf);
797 Dbg::GetFieldValue(objectId, fieldId, pReply);
798 }
799
800 return ERR_NONE;
801}
802
803/*
804 * Set values in the fields of an object.
805 */
806static JdwpError handleOR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
807 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700808 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700809
810 LOG(VERBOSE) << StringPrintf(" Req to set %d fields in objectId=0x%llx", numFields, objectId);
811
812 for (uint32_t i = 0; i < numFields; i++) {
813 FieldId fieldId = ReadFieldId(&buf);
814
815 uint8_t fieldTag = Dbg::GetFieldBasicTag(objectId, fieldId);
816 int width = Dbg::GetTagWidth(fieldTag);
817 uint64_t value = jdwpReadValue(&buf, width);
818
819 LOG(VERBOSE) << StringPrintf(" --> fieldId=%x tag='%c'(%d) value=%lld", fieldId, fieldTag, width, value);
820
821 Dbg::SetFieldValue(objectId, fieldId, value, width);
822 }
823
824 return ERR_NONE;
825}
826
827/*
828 * Invoke an instance method. The invocation must occur in the specified
829 * thread, which must have been suspended by an event.
830 *
831 * The call is synchronous. All threads in the VM are resumed, unless the
832 * SINGLE_THREADED flag is set.
833 *
834 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
835 * object), it will try to invoke the object's toString() function. This
836 * feature becomes crucial when examining ArrayLists with Eclipse.
837 */
838static JdwpError handleOR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
839 ObjectId objectId = ReadObjectId(&buf);
840 ObjectId threadId = ReadObjectId(&buf);
841 RefTypeId classId = ReadRefTypeId(&buf);
842 MethodId methodId = ReadMethodId(&buf);
843
844 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, false);
845}
846
847/*
848 * Disable garbage collection of the specified object.
849 */
850static JdwpError handleOR_DisableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
851 // this is currently a no-op
852 return ERR_NONE;
853}
854
855/*
856 * Enable garbage collection of the specified object.
857 */
858static JdwpError handleOR_EnableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
859 // this is currently a no-op
860 return ERR_NONE;
861}
862
863/*
864 * Determine whether an object has been garbage collected.
865 */
866static JdwpError handleOR_IsCollected(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
867 ObjectId objectId;
868
869 objectId = ReadObjectId(&buf);
870 LOG(VERBOSE) << StringPrintf(" Req IsCollected(0x%llx)", objectId);
871
872 // TODO: currently returning false; must integrate with GC
873 expandBufAdd1(pReply, 0);
874
875 return ERR_NONE;
876}
877
878/*
879 * Return the string value in a string object.
880 */
881static JdwpError handleSR_Value(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
882 ObjectId stringObject = ReadObjectId(&buf);
883 char* str = Dbg::StringToUtf8(stringObject);
884
885 LOG(VERBOSE) << StringPrintf(" Req for str %llx --> '%s'", stringObject, str);
886
887 expandBufAddUtf8String(pReply, (uint8_t*) str);
888 free(str);
889
890 return ERR_NONE;
891}
892
893/*
894 * Return a thread's name.
895 */
896static JdwpError handleTR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
897 ObjectId threadId = ReadObjectId(&buf);
898
899 LOG(VERBOSE) << StringPrintf(" Req for name of thread 0x%llx", threadId);
900 char* name = Dbg::GetThreadName(threadId);
901 if (name == NULL) {
902 return ERR_INVALID_THREAD;
903 }
904 expandBufAddUtf8String(pReply, (uint8_t*) name);
905 free(name);
906
907 return ERR_NONE;
908}
909
910/*
911 * Suspend the specified thread.
912 *
913 * It's supposed to remain suspended even if interpreted code wants to
914 * resume it; only the JDI is allowed to resume it.
915 */
916static JdwpError handleTR_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
917 ObjectId threadId = ReadObjectId(&buf);
918
919 if (threadId == Dbg::GetThreadSelfId()) {
920 LOG(INFO) << " Warning: ignoring request to suspend self";
921 return ERR_THREAD_NOT_SUSPENDED;
922 }
923 LOG(VERBOSE) << StringPrintf(" Req to suspend thread 0x%llx", threadId);
924 Dbg::SuspendThread(threadId);
925 return ERR_NONE;
926}
927
928/*
929 * Resume the specified thread.
930 */
931static JdwpError handleTR_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
932 ObjectId threadId = ReadObjectId(&buf);
933
934 if (threadId == Dbg::GetThreadSelfId()) {
935 LOG(INFO) << " Warning: ignoring request to resume self";
936 return ERR_NONE;
937 }
938 LOG(VERBOSE) << StringPrintf(" Req to resume thread 0x%llx", threadId);
939 Dbg::ResumeThread(threadId);
940 return ERR_NONE;
941}
942
943/*
944 * Return status of specified thread.
945 */
946static JdwpError handleTR_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
947 ObjectId threadId = ReadObjectId(&buf);
948
949 LOG(VERBOSE) << StringPrintf(" Req for status of thread 0x%llx", threadId);
950
951 uint32_t threadStatus;
952 uint32_t suspendStatus;
953 if (!Dbg::GetThreadStatus(threadId, &threadStatus, &suspendStatus)) {
954 return ERR_INVALID_THREAD;
955 }
956
957 LOG(VERBOSE) << " --> " << JdwpThreadStatus(threadStatus) << ", " << JdwpSuspendStatus(suspendStatus);
958
959 expandBufAdd4BE(pReply, threadStatus);
960 expandBufAdd4BE(pReply, suspendStatus);
961
962 return ERR_NONE;
963}
964
965/*
966 * Return the thread group that the specified thread is a member of.
967 */
968static JdwpError handleTR_ThreadGroup(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
969 ObjectId threadId = ReadObjectId(&buf);
970
971 /* currently not handling these */
972 ObjectId threadGroupId = Dbg::GetThreadGroup(threadId);
973 expandBufAddObjectId(pReply, threadGroupId);
974
975 return ERR_NONE;
976}
977
978/*
979 * Return the current call stack of a suspended thread.
980 *
981 * If the thread isn't suspended, the error code isn't defined, but should
982 * be THREAD_NOT_SUSPENDED.
983 */
984static JdwpError handleTR_Frames(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
985 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700986 uint32_t startFrame = Read4BE(&buf);
987 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700988
989 if (!Dbg::ThreadExists(threadId)) {
990 return ERR_INVALID_THREAD;
991 }
992 if (!Dbg::IsSuspended(threadId)) {
993 LOG(VERBOSE) << StringPrintf(" Rejecting req for frames in running thread '%s' (%llx)", Dbg::GetThreadName(threadId), threadId);
994 return ERR_THREAD_NOT_SUSPENDED;
995 }
996
997 int frameCount = Dbg::GetThreadFrameCount(threadId);
998
999 LOG(VERBOSE) << StringPrintf(" Request for frames: threadId=%llx start=%d length=%d [count=%d]", threadId, startFrame, length, frameCount);
1000 if (frameCount <= 0) {
1001 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1002 }
1003 if (length == (uint32_t) -1) {
1004 length = frameCount;
1005 }
1006 CHECK((int) startFrame >= 0 && (int) startFrame < frameCount);
1007 CHECK_LE((int) (startFrame + length), frameCount);
1008
1009 uint32_t frames = length;
1010 expandBufAdd4BE(pReply, frames);
1011 for (uint32_t i = startFrame; i < (startFrame+length); i++) {
1012 FrameId frameId;
1013 JdwpLocation loc;
1014
1015 Dbg::GetThreadFrame(threadId, i, &frameId, &loc);
1016
1017 expandBufAdd8BE(pReply, frameId);
1018 AddLocation(pReply, &loc);
1019
1020 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);
1021 }
1022
1023 return ERR_NONE;
1024}
1025
1026/*
1027 * Returns the #of frames on the specified thread, which must be suspended.
1028 */
1029static JdwpError handleTR_FrameCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1030 ObjectId threadId = ReadObjectId(&buf);
1031
1032 if (!Dbg::ThreadExists(threadId)) {
1033 return ERR_INVALID_THREAD;
1034 }
1035 if (!Dbg::IsSuspended(threadId)) {
1036 LOG(VERBOSE) << StringPrintf(" Rejecting req for frames in running thread '%s' (%llx)", Dbg::GetThreadName(threadId), threadId);
1037 return ERR_THREAD_NOT_SUSPENDED;
1038 }
1039
1040 int frameCount = Dbg::GetThreadFrameCount(threadId);
1041 if (frameCount < 0) {
1042 return ERR_INVALID_THREAD;
1043 }
1044 expandBufAdd4BE(pReply, (uint32_t)frameCount);
1045
1046 return ERR_NONE;
1047}
1048
1049/*
1050 * Get the monitor that the thread is waiting on.
1051 */
1052static JdwpError handleTR_CurrentContendedMonitor(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1053 ObjectId threadId;
1054
1055 threadId = ReadObjectId(&buf);
1056
1057 // TODO: create an Object to represent the monitor (we're currently
1058 // just using a raw Monitor struct in the VM)
1059
1060 return ERR_NOT_IMPLEMENTED;
1061}
1062
1063/*
1064 * Return the suspend count for the specified thread.
1065 *
1066 * (The thread *might* still be running -- it might not have examined
1067 * its suspend count recently.)
1068 */
1069static JdwpError handleTR_SuspendCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1070 ObjectId threadId = ReadObjectId(&buf);
1071
1072 uint32_t suspendCount = Dbg::GetThreadSuspendCount(threadId);
1073 expandBufAdd4BE(pReply, suspendCount);
1074
1075 return ERR_NONE;
1076}
1077
1078/*
1079 * Return the name of a thread group.
1080 *
1081 * The Eclipse debugger recognizes "main" and "system" as special.
1082 */
1083static JdwpError handleTGR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1084 ObjectId threadGroupId = ReadObjectId(&buf);
1085 LOG(VERBOSE) << StringPrintf(" Req for name of threadGroupId=0x%llx", threadGroupId);
1086
1087 char* name = Dbg::GetThreadGroupName(threadGroupId);
1088 if (name != NULL) {
1089 expandBufAddUtf8String(pReply, (uint8_t*) name);
1090 } else {
1091 expandBufAddUtf8String(pReply, (uint8_t*) "BAD-GROUP-ID");
1092 LOG(VERBOSE) << StringPrintf("bad thread group ID");
1093 }
1094
1095 free(name);
1096
1097 return ERR_NONE;
1098}
1099
1100/*
1101 * Returns the thread group -- if any -- that contains the specified
1102 * thread group.
1103 */
1104static JdwpError handleTGR_Parent(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1105 ObjectId groupId = ReadObjectId(&buf);
1106
1107 ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId);
1108 expandBufAddObjectId(pReply, parentGroup);
1109
1110 return ERR_NONE;
1111}
1112
1113/*
1114 * Return the active threads and thread groups that are part of the
1115 * specified thread group.
1116 */
1117static JdwpError handleTGR_Children(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1118 ObjectId threadGroupId = ReadObjectId(&buf);
1119 LOG(VERBOSE) << StringPrintf(" Req for threads in threadGroupId=0x%llx", threadGroupId);
1120
1121 ObjectId* pThreadIds;
1122 uint32_t threadCount;
1123 Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount);
1124
1125 expandBufAdd4BE(pReply, threadCount);
1126
1127 for (uint32_t i = 0; i < threadCount; i++) {
1128 expandBufAddObjectId(pReply, pThreadIds[i]);
1129 }
1130 free(pThreadIds);
1131
1132 /*
1133 * TODO: finish support for child groups
1134 *
1135 * For now, just show that "main" is a child of "system".
1136 */
1137 if (threadGroupId == Dbg::GetSystemThreadGroupId()) {
1138 expandBufAdd4BE(pReply, 1);
1139 expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId());
1140 } else {
1141 expandBufAdd4BE(pReply, 0);
1142 }
1143
1144 return ERR_NONE;
1145}
1146
1147/*
1148 * Return the #of components in the array.
1149 */
1150static JdwpError handleAR_Length(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1151 ObjectId arrayId = ReadObjectId(&buf);
1152 LOG(VERBOSE) << StringPrintf(" Req for length of array 0x%llx", arrayId);
1153
1154 uint32_t arrayLength = Dbg::GetArrayLength(arrayId);
1155
1156 LOG(VERBOSE) << StringPrintf(" --> %d", arrayLength);
1157
1158 expandBufAdd4BE(pReply, arrayLength);
1159
1160 return ERR_NONE;
1161}
1162
1163/*
1164 * Return the values from an array.
1165 */
1166static JdwpError handleAR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1167 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001168 uint32_t firstIndex = Read4BE(&buf);
1169 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001170
1171 uint8_t tag = Dbg::GetArrayElementTag(arrayId);
1172 LOG(VERBOSE) << StringPrintf(" Req for array values 0x%llx first=%d len=%d (elem tag=%c)", arrayId, firstIndex, length, tag);
1173
1174 expandBufAdd1(pReply, tag);
1175 expandBufAdd4BE(pReply, length);
1176
1177 if (!Dbg::OutputArray(arrayId, firstIndex, length, pReply)) {
1178 return ERR_INVALID_LENGTH;
1179 }
1180
1181 return ERR_NONE;
1182}
1183
1184/*
1185 * Set values in an array.
1186 */
1187static JdwpError handleAR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1188 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001189 uint32_t firstIndex = Read4BE(&buf);
1190 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001191
1192 LOG(VERBOSE) << StringPrintf(" Req to set array values 0x%llx first=%d count=%d", arrayId, firstIndex, values);
1193
1194 if (!Dbg::SetArrayElements(arrayId, firstIndex, values, buf)) {
1195 return ERR_INVALID_LENGTH;
1196 }
1197
1198 return ERR_NONE;
1199}
1200
1201/*
1202 * Return the set of classes visible to a class loader. All classes which
1203 * have the class loader as a defining or initiating loader are returned.
1204 */
1205static JdwpError handleCLR_VisibleClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1206 ObjectId classLoaderObject;
1207 uint32_t numClasses = 0;
1208 RefTypeId* classRefBuf = NULL;
1209 int i;
1210
1211 classLoaderObject = ReadObjectId(&buf);
1212
1213 Dbg::GetVisibleClassList(classLoaderObject, &numClasses, &classRefBuf);
1214
1215 expandBufAdd4BE(pReply, numClasses);
1216 for (i = 0; i < (int) numClasses; i++) {
1217 uint8_t refTypeTag = Dbg::GetClassObjectType(classRefBuf[i]);
1218
1219 expandBufAdd1(pReply, refTypeTag);
1220 expandBufAddRefTypeId(pReply, classRefBuf[i]);
1221 }
1222
1223 return ERR_NONE;
1224}
1225
1226/*
1227 * Return a newly-allocated string in which all occurrences of '.' have
1228 * been changed to '/'. If we find a '/' in the original string, NULL
1229 * is returned to avoid ambiguity.
1230 */
1231char* dvmDotToSlash(const char* str) {
1232 char* newStr = strdup(str);
1233 char* cp = newStr;
1234
1235 if (newStr == NULL) {
1236 return NULL;
1237 }
1238
1239 while (*cp != '\0') {
1240 if (*cp == '/') {
1241 CHECK(false);
1242 return NULL;
1243 }
1244 if (*cp == '.') {
1245 *cp = '/';
1246 }
1247 cp++;
1248 }
1249
1250 return newStr;
1251}
1252
1253/*
1254 * Set an event trigger.
1255 *
1256 * Reply with a requestID.
1257 */
1258static JdwpError handleER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1259 const uint8_t* origBuf = buf;
1260
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001261 uint8_t eventKind = Read1(&buf);
1262 uint8_t suspendPolicy = Read1(&buf);
1263 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001264
1265 LOG(VERBOSE) << " Set(kind=" << JdwpEventKind(eventKind)
1266 << " suspend=" << JdwpSuspendPolicy(suspendPolicy)
1267 << " mods=" << modifierCount << ")";
1268
1269 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1270
1271 JdwpEvent* pEvent = EventAlloc(modifierCount);
1272 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
1273 pEvent->suspendPolicy = static_cast<JdwpSuspendPolicy>(suspendPolicy);
1274 pEvent->modCount = modifierCount;
1275
1276 /*
1277 * Read modifiers. Ordering may be significant (see explanation of Count
1278 * mods in JDWP doc).
1279 */
1280 for (uint32_t idx = 0; idx < modifierCount; idx++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001281 uint8_t modKind = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001282
1283 pEvent->mods[idx].modKind = modKind;
1284
1285 switch (modKind) {
1286 case MK_COUNT: /* report once, when "--count" reaches 0 */
1287 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001288 uint32_t count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001289 LOG(VERBOSE) << " Count: " << count;
1290 if (count == 0) {
1291 return ERR_INVALID_COUNT;
1292 }
1293 pEvent->mods[idx].count.count = count;
1294 }
1295 break;
1296 case MK_CONDITIONAL: /* conditional on expression) */
1297 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001298 uint32_t exprId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001299 LOG(VERBOSE) << " Conditional: " << exprId;
1300 pEvent->mods[idx].conditional.exprId = exprId;
1301 }
1302 break;
1303 case MK_THREAD_ONLY: /* only report events in specified thread */
1304 {
1305 ObjectId threadId = ReadObjectId(&buf);
1306 LOG(VERBOSE) << StringPrintf(" ThreadOnly: %llx", threadId);
1307 pEvent->mods[idx].threadOnly.threadId = threadId;
1308 }
1309 break;
1310 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1311 {
1312 RefTypeId clazzId = ReadRefTypeId(&buf);
1313 LOG(VERBOSE) << StringPrintf(" ClassOnly: %llx (%s)", clazzId, Dbg::GetClassDescriptor(clazzId));
1314 pEvent->mods[idx].classOnly.refTypeId = clazzId;
1315 }
1316 break;
1317 case MK_CLASS_MATCH: /* restrict events to matching classes */
1318 {
1319 char* pattern;
1320 size_t strLen;
1321
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001322 pattern = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001323 LOG(VERBOSE) << StringPrintf(" ClassMatch: '%s'", pattern);
1324 /* pattern is "java.foo.*", we want "java/foo/ *" */
1325 pEvent->mods[idx].classMatch.classPattern = dvmDotToSlash(pattern);
1326 free(pattern);
1327 }
1328 break;
1329 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1330 {
1331 char* pattern;
1332 size_t strLen;
1333
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001334 pattern = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001335 LOG(VERBOSE) << StringPrintf(" ClassExclude: '%s'", pattern);
1336 pEvent->mods[idx].classExclude.classPattern = dvmDotToSlash(pattern);
1337 free(pattern);
1338 }
1339 break;
1340 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1341 {
1342 JdwpLocation loc;
1343
1344 jdwpReadLocation(&buf, &loc);
1345 LOG(VERBOSE) << StringPrintf(" LocationOnly: typeTag=%d classId=%llx methodId=%x idx=%llx",
1346 loc.typeTag, loc.classId, loc.methodId, loc.idx);
1347 pEvent->mods[idx].locationOnly.loc = loc;
1348 }
1349 break;
1350 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1351 {
1352 RefTypeId exceptionOrNull; /* null == all exceptions */
1353 uint8_t caught, uncaught;
1354
1355 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001356 caught = Read1(&buf);
1357 uncaught = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001358 LOG(VERBOSE) << StringPrintf(" ExceptionOnly: type=%llx(%s) caught=%d uncaught=%d",
1359 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassDescriptor(exceptionOrNull), caught, uncaught);
1360
1361 pEvent->mods[idx].exceptionOnly.refTypeId = exceptionOrNull;
1362 pEvent->mods[idx].exceptionOnly.caught = caught;
1363 pEvent->mods[idx].exceptionOnly.uncaught = uncaught;
1364 }
1365 break;
1366 case MK_FIELD_ONLY: /* for field access/mod events */
1367 {
1368 RefTypeId declaring = ReadRefTypeId(&buf);
1369 FieldId fieldId = ReadFieldId(&buf);
1370 LOG(VERBOSE) << StringPrintf(" FieldOnly: %llx %x", declaring, fieldId);
1371 pEvent->mods[idx].fieldOnly.refTypeId = declaring;
1372 pEvent->mods[idx].fieldOnly.fieldId = fieldId;
1373 }
1374 break;
1375 case MK_STEP: /* for use with EK_SINGLE_STEP */
1376 {
1377 ObjectId threadId;
1378 uint32_t size, depth;
1379
1380 threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001381 size = Read4BE(&buf);
1382 depth = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001383 LOG(VERBOSE) << StringPrintf(" Step: thread=%llx", threadId)
1384 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1385
1386 pEvent->mods[idx].step.threadId = threadId;
1387 pEvent->mods[idx].step.size = size;
1388 pEvent->mods[idx].step.depth = depth;
1389 }
1390 break;
1391 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1392 {
1393 ObjectId instance = ReadObjectId(&buf);
1394 LOG(VERBOSE) << StringPrintf(" InstanceOnly: %llx", instance);
1395 pEvent->mods[idx].instanceOnly.objectId = instance;
1396 }
1397 break;
1398 default:
1399 LOG(WARNING) << "GLITCH: unsupported modKind=" << modKind;
1400 break;
1401 }
1402 }
1403
1404 /*
1405 * Make sure we consumed all data. It is possible that the remote side
1406 * has sent us bad stuff, but for now we blame ourselves.
1407 */
1408 if (buf != origBuf + dataLen) {
1409 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1410 }
1411
1412 /*
1413 * We reply with an integer "requestID".
1414 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001415 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001416 expandBufAdd4BE(pReply, requestId);
1417
1418 pEvent->requestId = requestId;
1419
1420 LOG(VERBOSE) << StringPrintf(" --> event requestId=%#x", requestId);
1421
1422 /* add it to the list */
1423 JdwpError err = RegisterEvent(state, pEvent);
1424 if (err != ERR_NONE) {
1425 /* registration failed, probably because event is bogus */
1426 EventFree(pEvent);
1427 LOG(WARNING) << "WARNING: event request rejected";
1428 }
1429 return err;
1430}
1431
1432/*
1433 * Clear an event. Failure to find an event with a matching ID is a no-op
1434 * and does not return an error.
1435 */
1436static JdwpError handleER_Clear(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1437 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001438 eventKind = Read1(&buf);
1439 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001440
1441 LOG(VERBOSE) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
1442
1443 UnregisterEventById(state, requestId);
1444
1445 return ERR_NONE;
1446}
1447
1448/*
1449 * Return the values of arguments and local variables.
1450 */
1451static JdwpError handleSF_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1452 ObjectId threadId = ReadObjectId(&buf);
1453 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001454 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001455
1456 LOG(VERBOSE) << StringPrintf(" Req for %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
1457
1458 expandBufAdd4BE(pReply, slots); /* "int values" */
1459 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001460 uint32_t slot = Read4BE(&buf);
1461 uint8_t reqSigByte = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001462
1463 LOG(VERBOSE) << StringPrintf(" --> slot %d '%c'", slot, reqSigByte);
1464
1465 int width = Dbg::GetTagWidth(reqSigByte);
1466 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
1467 Dbg::GetLocalValue(threadId, frameId, slot, reqSigByte, ptr, width);
1468 }
1469
1470 return ERR_NONE;
1471}
1472
1473/*
1474 * Set the values of arguments and local variables.
1475 */
1476static JdwpError handleSF_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1477 ObjectId threadId = ReadObjectId(&buf);
1478 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001479 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001480
1481 LOG(VERBOSE) << StringPrintf(" Req to set %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
1482
1483 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001484 uint32_t slot = Read4BE(&buf);
1485 uint8_t sigByte = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001486 int width = Dbg::GetTagWidth(sigByte);
1487 uint64_t value = jdwpReadValue(&buf, width);
1488
1489 LOG(VERBOSE) << StringPrintf(" --> slot %d '%c' %llx", slot, sigByte, value);
1490 Dbg::SetLocalValue(threadId, frameId, slot, sigByte, value, width);
1491 }
1492
1493 return ERR_NONE;
1494}
1495
1496/*
1497 * Returns the value of "this" for the specified frame.
1498 */
1499static JdwpError handleSF_ThisObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1500 ObjectId threadId = ReadObjectId(&buf);
1501 FrameId frameId = ReadFrameId(&buf);
1502
1503 ObjectId objectId;
1504 if (!Dbg::GetThisObject(threadId, frameId, &objectId)) {
1505 return ERR_INVALID_FRAMEID;
1506 }
1507
1508 uint8_t objectTag = Dbg::GetObjectTag(objectId);
1509 LOG(VERBOSE) << StringPrintf(" Req for 'this' in thread=%llx frame=%llx --> %llx %s '%c'", threadId, frameId, objectId, Dbg::GetObjectTypeName(objectId), (char)objectTag);
1510
1511 expandBufAdd1(pReply, objectTag);
1512 expandBufAddObjectId(pReply, objectId);
1513
1514 return ERR_NONE;
1515}
1516
1517/*
1518 * Return the reference type reflected by this class object.
1519 *
1520 * This appears to be required because ReferenceTypeId values are NEVER
1521 * reused, whereas ClassIds can be recycled like any other object. (Either
1522 * that, or I have no idea what this is for.)
1523 */
1524static JdwpError handleCOR_ReflectedType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1525 RefTypeId classObjectId = ReadRefTypeId(&buf);
1526
1527 LOG(VERBOSE) << StringPrintf(" Req for refTypeId for class=%llx (%s)", classObjectId, Dbg::GetClassDescriptor(classObjectId));
1528
1529 /* just hand the type back to them */
1530 if (Dbg::IsInterface(classObjectId)) {
1531 expandBufAdd1(pReply, TT_INTERFACE);
1532 } else {
1533 expandBufAdd1(pReply, TT_CLASS);
1534 }
1535 expandBufAddRefTypeId(pReply, classObjectId);
1536
1537 return ERR_NONE;
1538}
1539
1540/*
1541 * Handle a DDM packet with a single chunk in it.
1542 */
1543static JdwpError handleDDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1544 uint8_t* replyBuf = NULL;
1545 int replyLen = -1;
1546
1547 LOG(VERBOSE) << StringPrintf(" Handling DDM packet (%.4s)", buf);
1548
1549 /*
1550 * On first DDM packet, notify all handlers that DDM is running.
1551 */
1552 if (!state->ddmActive) {
1553 state->ddmActive = true;
1554 Dbg::DdmConnected();
1555 }
1556
1557 /*
1558 * If they want to send something back, we copy it into the buffer.
1559 * A no-copy approach would be nicer.
1560 *
1561 * TODO: consider altering the JDWP stuff to hold the packet header
1562 * in a separate buffer. That would allow us to writev() DDM traffic
1563 * instead of copying it into the expanding buffer. The reduction in
1564 * heap requirements is probably more valuable than the efficiency.
1565 */
1566 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1567 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1568 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1569 free(replyBuf);
1570 }
1571 return ERR_NONE;
1572}
1573
1574/*
1575 * Handler map decl.
1576 */
1577typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1578
1579struct JdwpHandlerMap {
1580 uint8_t cmdSet;
1581 uint8_t cmd;
1582 JdwpRequestHandler func;
1583 const char* descr;
1584};
1585
1586/*
1587 * Map commands to functions.
1588 *
1589 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1590 * and 128-256 are vendor-defined.
1591 */
1592static const JdwpHandlerMap gHandlerMap[] = {
1593 /* VirtualMachine command set (1) */
1594 { 1, 1, handleVM_Version, "VirtualMachine.Version" },
1595 { 1, 2, handleVM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1596 //1, 3, VirtualMachine.AllClasses
1597 { 1, 4, handleVM_AllThreads, "VirtualMachine.AllThreads" },
1598 { 1, 5, handleVM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1599 { 1, 6, handleVM_Dispose, "VirtualMachine.Dispose" },
1600 { 1, 7, handleVM_IDSizes, "VirtualMachine.IDSizes" },
1601 { 1, 8, handleVM_Suspend, "VirtualMachine.Suspend" },
1602 { 1, 9, handleVM_Resume, "VirtualMachine.Resume" },
1603 { 1, 10, handleVM_Exit, "VirtualMachine.Exit" },
1604 { 1, 11, handleVM_CreateString, "VirtualMachine.CreateString" },
1605 { 1, 12, handleVM_Capabilities, "VirtualMachine.Capabilities" },
1606 { 1, 13, handleVM_ClassPaths, "VirtualMachine.ClassPaths" },
1607 { 1, 14, HandleVM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1608 //1, 15, HoldEvents
1609 //1, 16, ReleaseEvents
1610 { 1, 17, handleVM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1611 //1, 18, RedefineClasses
1612 //1, 19, SetDefaultStratum
1613 { 1, 20, handleVM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric"},
1614 //1, 21, InstanceCounts
1615
1616 /* ReferenceType command set (2) */
1617 { 2, 1, handleRT_Signature, "ReferenceType.Signature" },
1618 { 2, 2, handleRT_ClassLoader, "ReferenceType.ClassLoader" },
1619 { 2, 3, handleRT_Modifiers, "ReferenceType.Modifiers" },
1620 //2, 4, Fields
1621 //2, 5, Methods
1622 { 2, 6, handleRT_GetValues, "ReferenceType.GetValues" },
1623 { 2, 7, handleRT_SourceFile, "ReferenceType.SourceFile" },
1624 //2, 8, NestedTypes
1625 { 2, 9, handleRT_Status, "ReferenceType.Status" },
1626 { 2, 10, handleRT_Interfaces, "ReferenceType.Interfaces" },
1627 { 2, 11, handleRT_ClassObject, "ReferenceType.ClassObject" },
1628 { 2, 12, handleRT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1629 { 2, 13, handleRT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1630 { 2, 14, handleRT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1631 { 2, 15, handleRT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
1632 //2, 16, Instances
1633 //2, 17, ClassFileVersion
1634 //2, 18, ConstantPool
1635
1636 /* ClassType command set (3) */
1637 { 3, 1, handleCT_Superclass, "ClassType.Superclass" },
1638 { 3, 2, handleCT_SetValues, "ClassType.SetValues" },
1639 { 3, 3, handleCT_InvokeMethod, "ClassType.InvokeMethod" },
1640 { 3, 4, handleCT_NewInstance, "ClassType.NewInstance" },
1641
1642 /* ArrayType command set (4) */
1643 { 4, 1, handleAT_newInstance, "ArrayType.NewInstance" },
1644
1645 /* InterfaceType command set (5) */
1646
1647 /* Method command set (6) */
1648 { 6, 1, handleM_LineTable, "Method.LineTable" },
1649 //6, 2, VariableTable
1650 //6, 3, Bytecodes
1651 //6, 4, IsObsolete
1652 { 6, 5, handleM_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
1653
1654 /* Field command set (8) */
1655
1656 /* ObjectReference command set (9) */
1657 { 9, 1, handleOR_ReferenceType, "ObjectReference.ReferenceType" },
1658 { 9, 2, handleOR_GetValues, "ObjectReference.GetValues" },
1659 { 9, 3, handleOR_SetValues, "ObjectReference.SetValues" },
1660 //9, 4, (not defined)
1661 //9, 5, MonitorInfo
1662 { 9, 6, handleOR_InvokeMethod, "ObjectReference.InvokeMethod" },
1663 { 9, 7, handleOR_DisableCollection, "ObjectReference.DisableCollection" },
1664 { 9, 8, handleOR_EnableCollection, "ObjectReference.EnableCollection" },
1665 { 9, 9, handleOR_IsCollected, "ObjectReference.IsCollected" },
1666 //9, 10, ReferringObjects
1667
1668 /* StringReference command set (10) */
1669 { 10, 1, handleSR_Value, "StringReference.Value" },
1670
1671 /* ThreadReference command set (11) */
1672 { 11, 1, handleTR_Name, "ThreadReference.Name" },
1673 { 11, 2, handleTR_Suspend, "ThreadReference.Suspend" },
1674 { 11, 3, handleTR_Resume, "ThreadReference.Resume" },
1675 { 11, 4, handleTR_Status, "ThreadReference.Status" },
1676 { 11, 5, handleTR_ThreadGroup, "ThreadReference.ThreadGroup" },
1677 { 11, 6, handleTR_Frames, "ThreadReference.Frames" },
1678 { 11, 7, handleTR_FrameCount, "ThreadReference.FrameCount" },
1679 //11, 8, OwnedMonitors
1680 { 11, 9, handleTR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1681 //11, 10, Stop
1682 //11, 11, Interrupt
1683 { 11, 12, handleTR_SuspendCount, "ThreadReference.SuspendCount" },
1684 //11, 13, OwnedMonitorsStackDepthInfo
1685 //11, 14, ForceEarlyReturn
1686
1687 /* ThreadGroupReference command set (12) */
1688 { 12, 1, handleTGR_Name, "ThreadGroupReference.Name" },
1689 { 12, 2, handleTGR_Parent, "ThreadGroupReference.Parent" },
1690 { 12, 3, handleTGR_Children, "ThreadGroupReference.Children" },
1691
1692 /* ArrayReference command set (13) */
1693 { 13, 1, handleAR_Length, "ArrayReference.Length" },
1694 { 13, 2, handleAR_GetValues, "ArrayReference.GetValues" },
1695 { 13, 3, handleAR_SetValues, "ArrayReference.SetValues" },
1696
1697 /* ClassLoaderReference command set (14) */
1698 { 14, 1, handleCLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
1699
1700 /* EventRequest command set (15) */
1701 { 15, 1, handleER_Set, "EventRequest.Set" },
1702 { 15, 2, handleER_Clear, "EventRequest.Clear" },
1703 //15, 3, ClearAllBreakpoints
1704
1705 /* StackFrame command set (16) */
1706 { 16, 1, handleSF_GetValues, "StackFrame.GetValues" },
1707 { 16, 2, handleSF_SetValues, "StackFrame.SetValues" },
1708 { 16, 3, handleSF_ThisObject, "StackFrame.ThisObject" },
1709 //16, 4, PopFrames
1710
1711 /* ClassObjectReference command set (17) */
1712 { 17, 1, handleCOR_ReflectedType,"ClassObjectReference.ReflectedType" },
1713
1714 /* Event command set (64) */
1715 //64, 100, Composite <-- sent from VM to debugger, never received by VM
1716
1717 { 199, 1, handleDDM_Chunk, "DDM.Chunk" },
1718};
1719
1720/*
1721 * Process a request from the debugger.
1722 *
1723 * On entry, the JDWP thread is in VMWAIT.
1724 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001725void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001726 JdwpError result = ERR_NONE;
1727 int i, respLen;
1728
1729 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1730 /*
1731 * Activity from a debugger, not merely ddms. Mark us as having an
1732 * active debugger session, and zero out the last-activity timestamp
1733 * so waitForDebugger() doesn't return if we stall for a bit here.
1734 */
1735 Dbg::Active();
Elliott Hughes376a7a02011-10-24 18:35:55 -07001736 QuasiAtomicSwap64(0, &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001737 }
1738
1739 /*
1740 * If a debugger event has fired in another thread, wait until the
1741 * initiating thread has suspended itself before processing messages
1742 * from the debugger. Otherwise we (the JDWP thread) could be told to
1743 * resume the thread before it has suspended.
1744 *
1745 * We call with an argument of zero to wait for the current event
1746 * thread to finish, and then clear the block. Depending on the thread
1747 * suspend policy, this may allow events in other threads to fire,
1748 * but those events have no bearing on what the debugger has sent us
1749 * in the current request.
1750 *
1751 * Note that we MUST clear the event token before waking the event
1752 * thread up, or risk waiting for the thread to suspend after we've
1753 * told it to resume.
1754 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001755 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001756
1757 /*
1758 * Tell the VM that we're running and shouldn't be interrupted by GC.
1759 * Do this after anything that can stall indefinitely.
1760 */
1761 Dbg::ThreadRunning();
1762
1763 expandBufAddSpace(pReply, kJDWPHeaderLen);
1764
1765 for (i = 0; i < (int) arraysize(gHandlerMap); i++) {
1766 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd) {
1767 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 -07001768 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001769 break;
1770 }
1771 }
1772 if (i == arraysize(gHandlerMap)) {
1773 LOG(ERROR) << StringPrintf("REQ: UNSUPPORTED (cmd=%d/%d dataLen=%d id=0x%06x)", pHeader->cmdSet, pHeader->cmd, dataLen, pHeader->id);
1774 if (dataLen > 0) {
1775 HexDump(buf, dataLen);
1776 }
1777 LOG(FATAL) << "command not implemented"; // make it *really* obvious
1778 result = ERR_NOT_IMPLEMENTED;
1779 }
1780
1781 /*
1782 * Set up the reply header.
1783 *
1784 * If we encountered an error, only send the header back.
1785 */
1786 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001787 Set4BE(replyBuf + 4, pHeader->id);
1788 Set1(replyBuf + 8, kJDWPFlagReply);
1789 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001790 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001791 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001792 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001793 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001794 }
1795
1796 respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
1797 if (false) {
1798 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
1799 if (respLen > 0) {
1800 HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
1801 }
1802 }
1803
1804 /*
1805 * Update last-activity timestamp. We really only need this during
1806 * the initial setup. Only update if this is a non-DDMS packet.
1807 */
1808 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07001809 QuasiAtomicSwap64(MilliTime(), &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001810 }
1811
1812 /* tell the VM that GC is okay again */
1813 Dbg::ThreadWaiting();
1814}
1815
1816} // namespace JDWP
1817
1818} // namespace art