blob: c0a44e07309b58ab08ce035d43fed2f3ff2d6b13 [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 */
Elliott Hughesdbb40792011-11-18 17:05:22 -080071static uint64_t jdwpReadValue(const uint8_t** pBuf, size_t width) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070072 uint64_t value = -1;
73 switch (width) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070074 case 1: value = Read1(pBuf); break;
75 case 2: value = Read2BE(pBuf); break;
76 case 4: value = Read4BE(pBuf); break;
77 case 8: value = Read8BE(pBuf); break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070078 default: LOG(FATAL) << width; break;
79 }
80 return value;
81}
82
83/*
84 * Helper function: write a variable-width value into the output input buffer.
85 */
86static void jdwpWriteValue(ExpandBuf* pReply, int width, uint64_t value) {
87 switch (width) {
88 case 1: expandBufAdd1(pReply, value); break;
89 case 2: expandBufAdd2BE(pReply, value); break;
90 case 4: expandBufAdd4BE(pReply, value); break;
91 case 8: expandBufAdd8BE(pReply, value); break;
92 default: LOG(FATAL) << width; break;
93 }
94}
95
96/*
97 * Common code for *_InvokeMethod requests.
98 *
99 * If "isConstructor" is set, this returns "objectId" rather than the
100 * expected-to-be-void return value of the called function.
101 */
102static JdwpError finishInvoke(JdwpState* state,
103 const uint8_t* buf, int dataLen, ExpandBuf* pReply,
104 ObjectId threadId, ObjectId objectId, RefTypeId classId, MethodId methodId,
105 bool isConstructor)
106{
107 CHECK(!isConstructor || objectId != 0);
108
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700109 uint32_t numArgs = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700110
111 LOG(VERBOSE) << StringPrintf(" --> threadId=%llx objectId=%llx", threadId, objectId);
Elliott Hughes03181a82011-11-17 17:22:21 -0800112 LOG(VERBOSE) << StringPrintf(" classId=%llx methodId=%x %s.%s", classId, methodId, Dbg::GetClassDescriptor(classId).c_str(), Dbg::GetMethodName(classId, methodId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700113 LOG(VERBOSE) << StringPrintf(" %d args:", numArgs);
114
115 uint64_t* argArray = NULL;
116 if (numArgs > 0) {
117 argArray = (ObjectId*) malloc(sizeof(ObjectId) * numArgs);
118 }
119
120 for (uint32_t i = 0; i < numArgs; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700121 uint8_t typeTag = Read1(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800122 size_t width = Dbg::GetTagWidth(typeTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123 uint64_t value = jdwpReadValue(&buf, width);
124
125 LOG(VERBOSE) << StringPrintf(" '%c'(%d): 0x%llx", typeTag, width, value);
126 argArray[i] = value;
127 }
128
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700129 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130 LOG(VERBOSE) << StringPrintf(" options=0x%04x%s%s", options, (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "", (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
131
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 {
Elliott Hughesdbb40792011-11-18 17:05:22 -0800145 size_t width = Dbg::GetTagWidth(resultTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700146
147 expandBufAdd1(pReply, resultTag);
148 if (width != 0) {
149 jdwpWriteValue(pReply, width, resultValue);
150 }
151 }
152 expandBufAdd1(pReply, JT_OBJECT);
153 expandBufAddObjectId(pReply, exceptObjId);
154
155 LOG(VERBOSE) << StringPrintf(" --> returned '%c' 0x%llx (except=%08llx)", resultTag, resultValue, exceptObjId);
156
157 /* show detailed debug output */
158 if (resultTag == JT_STRING && exceptObjId == 0) {
159 if (resultValue != 0) {
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800160 LOG(VERBOSE) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700161 } else {
162 LOG(VERBOSE) << " string (null)";
163 }
164 }
165 }
166
167bail:
168 free(argArray);
169 return err;
170}
171
172
173/*
174 * Request for version info.
175 */
176static JdwpError handleVM_Version(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
177 /* text information on runtime version */
178 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughesa2155262011-11-16 16:26:58 -0800179 expandBufAddUtf8String(pReply, version.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700180 /* JDWP version numbers */
181 expandBufAdd4BE(pReply, 1); // major
182 expandBufAdd4BE(pReply, 5); // minor
183 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800184 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700185 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800186 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700187
188 return ERR_NONE;
189}
190
191/*
192 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
193 * referenceTypeID. We need to send back more than one if the class has
194 * been loaded by multiple class loaders.
195 */
196static JdwpError handleVM_ClassesBySignature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
197 size_t strLen;
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700198 char* classDescriptor = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199 LOG(VERBOSE) << " Req for class by signature '" << classDescriptor << "'";
200
201 /*
202 * TODO: if a class with the same name has been loaded multiple times
203 * (by different class loaders), we're supposed to return each of them.
204 *
205 * NOTE: this may mangle "className".
206 */
207 uint32_t numClasses;
208 RefTypeId refTypeId;
209 if (!Dbg::FindLoadedClassBySignature(classDescriptor, &refTypeId)) {
210 /* not currently loaded */
211 LOG(VERBOSE) << " --> no match!";
212 numClasses = 0;
213 } else {
214 /* just the one */
215 numClasses = 1;
216 }
217
218 expandBufAdd4BE(pReply, numClasses);
219
220 if (numClasses > 0) {
221 uint8_t typeTag;
222 uint32_t status;
223
224 /* get class vs. interface and status flags */
225 Dbg::GetClassInfo(refTypeId, &typeTag, &status, NULL);
226
227 expandBufAdd1(pReply, typeTag);
228 expandBufAddRefTypeId(pReply, refTypeId);
229 expandBufAdd4BE(pReply, status);
230 }
231
232 free(classDescriptor);
233
234 return ERR_NONE;
235}
236
237/*
238 * Handle request for the thread IDs of all running threads.
239 *
240 * We exclude ourselves from the list, because we don't allow ourselves
241 * to be suspended, and that violates some JDWP expectations.
242 */
243static JdwpError handleVM_AllThreads(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
244 ObjectId* pThreadIds;
245 uint32_t threadCount;
246 Dbg::GetAllThreads(&pThreadIds, &threadCount);
247
248 expandBufAdd4BE(pReply, threadCount);
249
250 ObjectId* walker = pThreadIds;
251 for (uint32_t i = 0; i < threadCount; i++) {
252 expandBufAddObjectId(pReply, *walker++);
253 }
254
255 free(pThreadIds);
256
257 return ERR_NONE;
258}
259
260/*
261 * List all thread groups that do not have a parent.
262 */
263static JdwpError handleVM_TopLevelThreadGroups(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
264 /*
265 * TODO: maintain a list of parentless thread groups in the VM.
266 *
267 * For now, just return "system". Application threads are created
268 * in "main", which is a child of "system".
269 */
270 uint32_t groups = 1;
271 expandBufAdd4BE(pReply, groups);
272 //threadGroupId = debugGetMainThreadGroup();
273 //expandBufAdd8BE(pReply, threadGroupId);
274 ObjectId threadGroupId = Dbg::GetSystemThreadGroupId();
275 expandBufAddObjectId(pReply, threadGroupId);
276
277 return ERR_NONE;
278}
279
280/*
281 * Respond with the sizes of the basic debugger types.
282 *
283 * All IDs are 8 bytes.
284 */
285static JdwpError handleVM_IDSizes(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
286 expandBufAdd4BE(pReply, sizeof(FieldId));
287 expandBufAdd4BE(pReply, sizeof(MethodId));
288 expandBufAdd4BE(pReply, sizeof(ObjectId));
289 expandBufAdd4BE(pReply, sizeof(RefTypeId));
290 expandBufAdd4BE(pReply, sizeof(FrameId));
291 return ERR_NONE;
292}
293
294/*
295 * The debugger is politely asking to disconnect. We're good with that.
296 *
297 * We could resume threads and clean up pinned references, but we can do
298 * that when the TCP connection drops.
299 */
300static JdwpError handleVM_Dispose(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
301 return ERR_NONE;
302}
303
304/*
305 * Suspend the execution of the application running in the VM (i.e. suspend
306 * all threads).
307 *
308 * This needs to increment the "suspend count" on all threads.
309 */
310static JdwpError handleVM_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700311 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700312 return ERR_NONE;
313}
314
315/*
316 * Resume execution. Decrements the "suspend count" of all threads.
317 */
318static JdwpError handleVM_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
319 Dbg::ResumeVM();
320 return ERR_NONE;
321}
322
323/*
324 * The debugger wants the entire VM to exit.
325 */
326static JdwpError handleVM_Exit(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700327 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700328
329 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
330
331 Dbg::Exit(exitCode);
332 return ERR_NOT_IMPLEMENTED; // shouldn't get here
333}
334
335/*
336 * Create a new string in the VM and return its ID.
337 *
338 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
339 * string "java.util.Arrays".)
340 */
341static JdwpError handleVM_CreateString(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
342 size_t strLen;
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700343 char* str = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700344
345 LOG(VERBOSE) << " Req to create string '" << str << "'";
346
347 ObjectId stringId = Dbg::CreateString(str);
348 if (stringId == 0) {
349 return ERR_OUT_OF_MEMORY;
350 }
351
352 expandBufAddObjectId(pReply, stringId);
353 return ERR_NONE;
354}
355
356/*
357 * Tell the debugger what we are capable of.
358 */
359static JdwpError handleVM_Capabilities(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
360 expandBufAdd1(pReply, false); /* canWatchFieldModification */
361 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
362 expandBufAdd1(pReply, false); /* canGetBytecodes */
363 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
364 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
365 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
366 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
367 return ERR_NONE;
368}
369
370/*
371 * Return classpath and bootclasspath.
372 */
373static JdwpError handleVM_ClassPaths(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
374 char baseDir[2] = "/";
375
376 /*
377 * TODO: make this real. Not important for remote debugging, but
378 * might be useful for local debugging.
379 */
380 uint32_t classPaths = 1;
381 uint32_t bootClassPaths = 0;
382
Elliott Hughesa2155262011-11-16 16:26:58 -0800383 expandBufAddUtf8String(pReply, baseDir);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700384 expandBufAdd4BE(pReply, classPaths);
385 for (uint32_t i = 0; i < classPaths; i++) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800386 expandBufAddUtf8String(pReply, ".");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700387 }
388
389 expandBufAdd4BE(pReply, bootClassPaths);
390 for (uint32_t i = 0; i < classPaths; i++) {
391 /* add bootclasspath components as strings */
392 }
393
394 return ERR_NONE;
395}
396
397/*
398 * Release a list of object IDs. (Seen in jdb.)
399 *
400 * Currently does nothing.
401 */
402static JdwpError HandleVM_DisposeObjects(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
403 return ERR_NONE;
404}
405
406/*
407 * Tell the debugger what we are capable of.
408 */
409static JdwpError handleVM_CapabilitiesNew(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
410 expandBufAdd1(pReply, false); /* canWatchFieldModification */
411 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
412 expandBufAdd1(pReply, false); /* canGetBytecodes */
413 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
414 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
415 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
416 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
417 expandBufAdd1(pReply, false); /* canRedefineClasses */
418 expandBufAdd1(pReply, false); /* canAddMethod */
419 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
420 expandBufAdd1(pReply, false); /* canPopFrames */
421 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
422 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
423 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
424 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
425 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
426 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
427 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
428 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
429 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
430 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
431
432 /* fill in reserved22 through reserved32; note count started at 1 */
433 for (int i = 22; i <= 32; i++) {
434 expandBufAdd1(pReply, false); /* reservedN */
435 }
436 return ERR_NONE;
437}
438
439/*
440 * Cough up the complete list of classes.
441 */
442static JdwpError handleVM_AllClassesWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
443 uint32_t numClasses = 0;
444 RefTypeId* classRefBuf = NULL;
445
446 Dbg::GetClassList(&numClasses, &classRefBuf);
447
448 expandBufAdd4BE(pReply, numClasses);
449
450 for (uint32_t i = 0; i < numClasses; i++) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800451 static const char genericSignature[1] = "";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700452 uint8_t refTypeTag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800453 std::string descriptor;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454 uint32_t status;
455
Elliott Hughesa2155262011-11-16 16:26:58 -0800456 Dbg::GetClassInfo(classRefBuf[i], &refTypeTag, &status, &descriptor);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700457
458 expandBufAdd1(pReply, refTypeTag);
459 expandBufAddRefTypeId(pReply, classRefBuf[i]);
Elliott Hughesa2155262011-11-16 16:26:58 -0800460 expandBufAddUtf8String(pReply, descriptor.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700461 expandBufAddUtf8String(pReply, genericSignature);
462 expandBufAdd4BE(pReply, status);
463 }
464
465 free(classRefBuf);
466
467 return ERR_NONE;
468}
469
470/*
471 * Given a referenceTypeID, return a string with the JNI reference type
472 * signature (e.g. "Ljava/lang/Error;").
473 */
474static JdwpError handleRT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
475 RefTypeId refTypeId = ReadRefTypeId(&buf);
476
477 LOG(VERBOSE) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800478 std::string signature(Dbg::GetSignature(refTypeId));
479 expandBufAddUtf8String(pReply, signature.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700480
481 return ERR_NONE;
482}
483
484/*
485 * Return the modifiers (a/k/a access flags) for a reference type.
486 */
487static JdwpError handleRT_Modifiers(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
488 RefTypeId refTypeId = ReadRefTypeId(&buf);
489 uint32_t modBits = Dbg::GetAccessFlags(refTypeId);
490 expandBufAdd4BE(pReply, modBits);
491 return ERR_NONE;
492}
493
494/*
495 * Get values from static fields in a reference type.
496 */
497static JdwpError handleRT_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
498 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700499 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500
501 LOG(VERBOSE) << " RT_GetValues " << numFields << ":";
502
503 expandBufAdd4BE(pReply, numFields);
504 for (uint32_t i = 0; i < numFields; i++) {
505 FieldId fieldId = ReadFieldId(&buf);
506 Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
507 }
508
509 return ERR_NONE;
510}
511
512/*
513 * Get the name of the source file in which a reference type was declared.
514 */
515static JdwpError handleRT_SourceFile(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
516 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800517 std::string source_file;
518 if (!Dbg::GetSourceFile(refTypeId, source_file)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700519 return ERR_ABSENT_INFORMATION;
520 }
Elliott Hughes03181a82011-11-17 17:22:21 -0800521 expandBufAddUtf8String(pReply, source_file.c_str());
522 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700523}
524
525/*
526 * Return the current status of the reference type.
527 */
528static JdwpError handleRT_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
529 RefTypeId refTypeId = ReadRefTypeId(&buf);
530
531 /* get status flags */
532 uint8_t typeTag;
533 uint32_t status;
534 Dbg::GetClassInfo(refTypeId, &typeTag, &status, NULL);
535 expandBufAdd4BE(pReply, status);
536 return ERR_NONE;
537}
538
539/*
540 * Return interfaces implemented directly by this class.
541 */
542static JdwpError handleRT_Interfaces(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
543 RefTypeId refTypeId = ReadRefTypeId(&buf);
544
Elliott Hughesa2155262011-11-16 16:26:58 -0800545 LOG(VERBOSE) << StringPrintf(" Req for interfaces in %llx (%s)", refTypeId, Dbg::GetClassDescriptor(refTypeId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700546
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800547 Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700548
549 return ERR_NONE;
550}
551
552/*
553 * Return the class object corresponding to this type.
554 */
555static JdwpError handleRT_ClassObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
556 RefTypeId refTypeId = ReadRefTypeId(&buf);
557 ObjectId classObjId = Dbg::GetClassObject(refTypeId);
558
559 LOG(VERBOSE) << StringPrintf(" RefTypeId %llx -> ObjectId %llx", refTypeId, classObjId);
560
561 expandBufAddObjectId(pReply, classObjId);
562
563 return ERR_NONE;
564}
565
566/*
567 * Returns the value of the SourceDebugExtension attribute.
568 *
569 * JDB seems interested, but DEX files don't currently support this.
570 */
571static JdwpError handleRT_SourceDebugExtension(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
572 /* referenceTypeId in, string out */
573 return ERR_ABSENT_INFORMATION;
574}
575
576/*
577 * Like RT_Signature but with the possibility of a "generic signature".
578 */
579static JdwpError handleRT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800580 static const char genericSignature[1] = "";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700581
582 RefTypeId refTypeId = ReadRefTypeId(&buf);
583
584 LOG(VERBOSE) << StringPrintf(" Req for signature of refTypeId=0x%llx", refTypeId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800585 std::string signature(Dbg::GetSignature(refTypeId));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700586 if (signature != NULL) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800587 expandBufAddUtf8String(pReply, signature.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700588 } else {
589 LOG(WARNING) << StringPrintf("No signature for refTypeId=0x%llx", refTypeId);
Elliott Hughesa2155262011-11-16 16:26:58 -0800590 expandBufAddUtf8String(pReply, "Lunknown;");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700591 }
592 expandBufAddUtf8String(pReply, genericSignature);
593
594 return ERR_NONE;
595}
596
597/*
598 * Return the instance of java.lang.ClassLoader that loaded the specified
599 * reference type, or null if it was loaded by the system loader.
600 */
601static JdwpError handleRT_ClassLoader(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
602 RefTypeId refTypeId = ReadRefTypeId(&buf);
603
604 expandBufAddObjectId(pReply, Dbg::GetClassLoader(refTypeId));
605
606 return ERR_NONE;
607}
608
609/*
610 * Given a referenceTypeId, return a block of stuff that describes the
611 * fields declared by a class.
612 */
613static JdwpError handleRT_FieldsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
614 RefTypeId refTypeId = ReadRefTypeId(&buf);
615 LOG(VERBOSE) << StringPrintf(" Req for fields in refTypeId=0x%llx", refTypeId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800616 LOG(VERBOSE) << StringPrintf(" --> '%s'", Dbg::GetSignature(refTypeId).c_str());
617 Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700618 return ERR_NONE;
619}
620
621/*
622 * Given a referenceTypeID, return a block of goodies describing the
623 * methods declared by a class.
624 */
625static JdwpError handleRT_MethodsWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
626 RefTypeId refTypeId = ReadRefTypeId(&buf);
627
628 LOG(VERBOSE) << StringPrintf(" Req for methods in refTypeId=0x%llx", refTypeId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800629 LOG(VERBOSE) << StringPrintf(" --> '%s'", Dbg::GetSignature(refTypeId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700630
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800631 Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700632
633 return ERR_NONE;
634}
635
636/*
637 * Return the immediate superclass of a class.
638 */
639static JdwpError handleCT_Superclass(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
640 RefTypeId classId = ReadRefTypeId(&buf);
641
642 RefTypeId superClassId = Dbg::GetSuperclass(classId);
643
644 expandBufAddRefTypeId(pReply, superClassId);
645
646 return ERR_NONE;
647}
648
649/*
650 * Set static class values.
651 */
652static JdwpError handleCT_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
653 RefTypeId classId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700654 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700655
656 LOG(VERBOSE) << StringPrintf(" Req to set %d values in classId=%llx", values, classId);
657
658 for (uint32_t i = 0; i < values; i++) {
659 FieldId fieldId = ReadFieldId(&buf);
660 uint8_t fieldTag = Dbg::GetStaticFieldBasicTag(classId, fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800661 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700662 uint64_t value = jdwpReadValue(&buf, width);
663
664 LOG(VERBOSE) << StringPrintf(" --> field=%x tag=%c -> %lld", fieldId, fieldTag, value);
665 Dbg::SetStaticFieldValue(classId, fieldId, value, width);
666 }
667
668 return ERR_NONE;
669}
670
671/*
672 * Invoke a static method.
673 *
674 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
675 * values in the "variables" display.
676 */
677static JdwpError handleCT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
678 RefTypeId classId = ReadRefTypeId(&buf);
679 ObjectId threadId = ReadObjectId(&buf);
680 MethodId methodId = ReadMethodId(&buf);
681
682 return finishInvoke(state, buf, dataLen, pReply, threadId, 0, classId, methodId, false);
683}
684
685/*
686 * Create a new object of the requested type, and invoke the specified
687 * constructor.
688 *
689 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
690 * see the contents of a byte[] as a string.
691 */
692static JdwpError handleCT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
693 RefTypeId classId = ReadRefTypeId(&buf);
694 ObjectId threadId = ReadObjectId(&buf);
695 MethodId methodId = ReadMethodId(&buf);
696
697 LOG(VERBOSE) << "Creating instance of " << Dbg::GetClassDescriptor(classId);
698 ObjectId objectId = Dbg::CreateObject(classId);
699 if (objectId == 0) {
700 return ERR_OUT_OF_MEMORY;
701 }
702 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, true);
703}
704
705/*
706 * Create a new array object of the requested type and length.
707 */
708static JdwpError handleAT_newInstance(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
709 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700710 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700711
Elliott Hughesa2155262011-11-16 16:26:58 -0800712 LOG(VERBOSE) << StringPrintf("Creating array %s[%u]", Dbg::GetClassDescriptor(arrayTypeId).c_str(), length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700713 ObjectId objectId = Dbg::CreateArrayObject(arrayTypeId, length);
714 if (objectId == 0) {
715 return ERR_OUT_OF_MEMORY;
716 }
717 expandBufAdd1(pReply, JT_ARRAY);
718 expandBufAddObjectId(pReply, objectId);
719 return ERR_NONE;
720}
721
722/*
723 * Return line number information for the method, if present.
724 */
725static JdwpError handleM_LineTable(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
726 RefTypeId refTypeId = ReadRefTypeId(&buf);
727 MethodId methodId = ReadMethodId(&buf);
728
Elliott Hughes03181a82011-11-17 17:22:21 -0800729 LOG(VERBOSE) << StringPrintf(" Req for line table in %s.%s", Dbg::GetClassDescriptor(refTypeId).c_str(), Dbg::GetMethodName(refTypeId,methodId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700730
731 Dbg::OutputLineTable(refTypeId, methodId, pReply);
732
733 return ERR_NONE;
734}
735
736/*
737 * Pull out the LocalVariableTable goodies.
738 */
739static JdwpError handleM_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
740 RefTypeId classId = ReadRefTypeId(&buf);
741 MethodId methodId = ReadMethodId(&buf);
742
Elliott Hughes03181a82011-11-17 17:22:21 -0800743 LOG(VERBOSE) << StringPrintf(" Req for LocalVarTab in class=%s method=%s", Dbg::GetClassDescriptor(classId).c_str(), Dbg::GetMethodName(classId, methodId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700744
745 /*
746 * We could return ERR_ABSENT_INFORMATION here if the DEX file was
747 * built without local variable information. That will cause Eclipse
748 * to make a best-effort attempt at displaying local variables
749 * anonymously. However, the attempt isn't very good, so we're probably
750 * better off just not showing anything.
751 */
752 Dbg::OutputVariableTable(classId, methodId, true, pReply);
753 return ERR_NONE;
754}
755
756/*
757 * Given an object reference, return the runtime type of the object
758 * (class or array).
759 *
760 * This can get called on different things, e.g. threadId gets
761 * passed in here.
762 */
763static JdwpError handleOR_ReferenceType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
764 ObjectId objectId = ReadObjectId(&buf);
765 LOG(VERBOSE) << StringPrintf(" Req for type of objectId=0x%llx", objectId);
766
767 uint8_t refTypeTag;
768 RefTypeId typeId;
769 Dbg::GetObjectType(objectId, &refTypeTag, &typeId);
770
771 expandBufAdd1(pReply, refTypeTag);
772 expandBufAddRefTypeId(pReply, typeId);
773
774 return ERR_NONE;
775}
776
777/*
778 * Get values from the fields of an object.
779 */
780static JdwpError handleOR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
781 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700782 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700783
784 LOG(VERBOSE) << StringPrintf(" Req for %d fields from objectId=0x%llx", numFields, objectId);
785
786 expandBufAdd4BE(pReply, numFields);
787
788 for (uint32_t i = 0; i < numFields; i++) {
789 FieldId fieldId = ReadFieldId(&buf);
790 Dbg::GetFieldValue(objectId, fieldId, pReply);
791 }
792
793 return ERR_NONE;
794}
795
796/*
797 * Set values in the fields of an object.
798 */
799static JdwpError handleOR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
800 ObjectId objectId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700801 uint32_t numFields = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700802
803 LOG(VERBOSE) << StringPrintf(" Req to set %d fields in objectId=0x%llx", numFields, objectId);
804
805 for (uint32_t i = 0; i < numFields; i++) {
806 FieldId fieldId = ReadFieldId(&buf);
807
808 uint8_t fieldTag = Dbg::GetFieldBasicTag(objectId, fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800809 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700810 uint64_t value = jdwpReadValue(&buf, width);
811
812 LOG(VERBOSE) << StringPrintf(" --> fieldId=%x tag='%c'(%d) value=%lld", fieldId, fieldTag, width, value);
813
814 Dbg::SetFieldValue(objectId, fieldId, value, width);
815 }
816
817 return ERR_NONE;
818}
819
820/*
821 * Invoke an instance method. The invocation must occur in the specified
822 * thread, which must have been suspended by an event.
823 *
824 * The call is synchronous. All threads in the VM are resumed, unless the
825 * SINGLE_THREADED flag is set.
826 *
827 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
828 * object), it will try to invoke the object's toString() function. This
829 * feature becomes crucial when examining ArrayLists with Eclipse.
830 */
831static JdwpError handleOR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
832 ObjectId objectId = ReadObjectId(&buf);
833 ObjectId threadId = ReadObjectId(&buf);
834 RefTypeId classId = ReadRefTypeId(&buf);
835 MethodId methodId = ReadMethodId(&buf);
836
837 return finishInvoke(state, buf, dataLen, pReply, threadId, objectId, classId, methodId, false);
838}
839
840/*
841 * Disable garbage collection of the specified object.
842 */
843static JdwpError handleOR_DisableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
844 // this is currently a no-op
845 return ERR_NONE;
846}
847
848/*
849 * Enable garbage collection of the specified object.
850 */
851static JdwpError handleOR_EnableCollection(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
852 // this is currently a no-op
853 return ERR_NONE;
854}
855
856/*
857 * Determine whether an object has been garbage collected.
858 */
859static JdwpError handleOR_IsCollected(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
860 ObjectId objectId;
861
862 objectId = ReadObjectId(&buf);
863 LOG(VERBOSE) << StringPrintf(" Req IsCollected(0x%llx)", objectId);
864
865 // TODO: currently returning false; must integrate with GC
866 expandBufAdd1(pReply, 0);
867
868 return ERR_NONE;
869}
870
871/*
872 * Return the string value in a string object.
873 */
874static JdwpError handleSR_Value(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
875 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800876 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700877
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800878 LOG(VERBOSE) << StringPrintf(" Req for str %llx --> '%s'", stringObject, str.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700879
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800880 expandBufAddUtf8String(pReply, str.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700881
882 return ERR_NONE;
883}
884
885/*
886 * Return a thread's name.
887 */
888static JdwpError handleTR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
889 ObjectId threadId = ReadObjectId(&buf);
890
891 LOG(VERBOSE) << StringPrintf(" Req for name of thread 0x%llx", threadId);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800892 std::string name;
893 if (!Dbg::GetThreadName(threadId, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700894 return ERR_INVALID_THREAD;
895 }
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800896 LOG(VERBOSE) << StringPrintf(" Name of thread 0x%llx is \"%s\"", threadId, name.c_str());
897 expandBufAddUtf8String(pReply, name.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700898
899 return ERR_NONE;
900}
901
902/*
903 * Suspend the specified thread.
904 *
905 * It's supposed to remain suspended even if interpreted code wants to
906 * resume it; only the JDI is allowed to resume it.
907 */
908static JdwpError handleTR_Suspend(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
909 ObjectId threadId = ReadObjectId(&buf);
910
911 if (threadId == Dbg::GetThreadSelfId()) {
912 LOG(INFO) << " Warning: ignoring request to suspend self";
913 return ERR_THREAD_NOT_SUSPENDED;
914 }
915 LOG(VERBOSE) << StringPrintf(" Req to suspend thread 0x%llx", threadId);
916 Dbg::SuspendThread(threadId);
917 return ERR_NONE;
918}
919
920/*
921 * Resume the specified thread.
922 */
923static JdwpError handleTR_Resume(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
924 ObjectId threadId = ReadObjectId(&buf);
925
926 if (threadId == Dbg::GetThreadSelfId()) {
927 LOG(INFO) << " Warning: ignoring request to resume self";
928 return ERR_NONE;
929 }
930 LOG(VERBOSE) << StringPrintf(" Req to resume thread 0x%llx", threadId);
931 Dbg::ResumeThread(threadId);
932 return ERR_NONE;
933}
934
935/*
936 * Return status of specified thread.
937 */
938static JdwpError handleTR_Status(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
939 ObjectId threadId = ReadObjectId(&buf);
940
941 LOG(VERBOSE) << StringPrintf(" Req for status of thread 0x%llx", threadId);
942
943 uint32_t threadStatus;
944 uint32_t suspendStatus;
945 if (!Dbg::GetThreadStatus(threadId, &threadStatus, &suspendStatus)) {
946 return ERR_INVALID_THREAD;
947 }
948
949 LOG(VERBOSE) << " --> " << JdwpThreadStatus(threadStatus) << ", " << JdwpSuspendStatus(suspendStatus);
950
951 expandBufAdd4BE(pReply, threadStatus);
952 expandBufAdd4BE(pReply, suspendStatus);
953
954 return ERR_NONE;
955}
956
957/*
958 * Return the thread group that the specified thread is a member of.
959 */
960static JdwpError handleTR_ThreadGroup(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
961 ObjectId threadId = ReadObjectId(&buf);
962
963 /* currently not handling these */
964 ObjectId threadGroupId = Dbg::GetThreadGroup(threadId);
965 expandBufAddObjectId(pReply, threadGroupId);
966
967 return ERR_NONE;
968}
969
970/*
971 * Return the current call stack of a suspended thread.
972 *
973 * If the thread isn't suspended, the error code isn't defined, but should
974 * be THREAD_NOT_SUSPENDED.
975 */
976static JdwpError handleTR_Frames(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
977 ObjectId threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700978 uint32_t startFrame = Read4BE(&buf);
979 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700980
981 if (!Dbg::ThreadExists(threadId)) {
982 return ERR_INVALID_THREAD;
983 }
984 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800985 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700986 return ERR_THREAD_NOT_SUSPENDED;
987 }
988
Elliott Hughes761928d2011-11-16 18:33:03 -0800989 size_t frameCount = Dbg::GetThreadFrameCount(threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700990
991 LOG(VERBOSE) << StringPrintf(" Request for frames: threadId=%llx start=%d length=%d [count=%d]", threadId, startFrame, length, frameCount);
992 if (frameCount <= 0) {
993 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
994 }
995 if (length == (uint32_t) -1) {
996 length = frameCount;
997 }
Elliott Hughes761928d2011-11-16 18:33:03 -0800998 CHECK_GE(startFrame, 0U);
999 CHECK_LT(startFrame, frameCount);
1000 CHECK_LE(startFrame + length, frameCount);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001001
1002 uint32_t frames = length;
1003 expandBufAdd4BE(pReply, frames);
1004 for (uint32_t i = startFrame; i < (startFrame+length); i++) {
1005 FrameId frameId;
1006 JdwpLocation loc;
1007
1008 Dbg::GetThreadFrame(threadId, i, &frameId, &loc);
1009
1010 expandBufAdd8BE(pReply, frameId);
1011 AddLocation(pReply, &loc);
1012
1013 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);
1014 }
1015
1016 return ERR_NONE;
1017}
1018
1019/*
1020 * Returns the #of frames on the specified thread, which must be suspended.
1021 */
1022static JdwpError handleTR_FrameCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1023 ObjectId threadId = ReadObjectId(&buf);
1024
1025 if (!Dbg::ThreadExists(threadId)) {
1026 return ERR_INVALID_THREAD;
1027 }
1028 if (!Dbg::IsSuspended(threadId)) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001029 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %llx", threadId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001030 return ERR_THREAD_NOT_SUSPENDED;
1031 }
1032
1033 int frameCount = Dbg::GetThreadFrameCount(threadId);
1034 if (frameCount < 0) {
1035 return ERR_INVALID_THREAD;
1036 }
1037 expandBufAdd4BE(pReply, (uint32_t)frameCount);
1038
1039 return ERR_NONE;
1040}
1041
1042/*
1043 * Get the monitor that the thread is waiting on.
1044 */
1045static JdwpError handleTR_CurrentContendedMonitor(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1046 ObjectId threadId;
1047
1048 threadId = ReadObjectId(&buf);
1049
1050 // TODO: create an Object to represent the monitor (we're currently
1051 // just using a raw Monitor struct in the VM)
1052
1053 return ERR_NOT_IMPLEMENTED;
1054}
1055
1056/*
1057 * Return the suspend count for the specified thread.
1058 *
1059 * (The thread *might* still be running -- it might not have examined
1060 * its suspend count recently.)
1061 */
1062static JdwpError handleTR_SuspendCount(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1063 ObjectId threadId = ReadObjectId(&buf);
1064
1065 uint32_t suspendCount = Dbg::GetThreadSuspendCount(threadId);
1066 expandBufAdd4BE(pReply, suspendCount);
1067
1068 return ERR_NONE;
1069}
1070
1071/*
1072 * Return the name of a thread group.
1073 *
1074 * The Eclipse debugger recognizes "main" and "system" as special.
1075 */
1076static JdwpError handleTGR_Name(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1077 ObjectId threadGroupId = ReadObjectId(&buf);
1078 LOG(VERBOSE) << StringPrintf(" Req for name of threadGroupId=0x%llx", threadGroupId);
1079
Elliott Hughes499c5132011-11-17 14:55:11 -08001080 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(threadGroupId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001081
1082 return ERR_NONE;
1083}
1084
1085/*
1086 * Returns the thread group -- if any -- that contains the specified
1087 * thread group.
1088 */
1089static JdwpError handleTGR_Parent(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1090 ObjectId groupId = ReadObjectId(&buf);
1091
1092 ObjectId parentGroup = Dbg::GetThreadGroupParent(groupId);
1093 expandBufAddObjectId(pReply, parentGroup);
1094
1095 return ERR_NONE;
1096}
1097
1098/*
1099 * Return the active threads and thread groups that are part of the
1100 * specified thread group.
1101 */
1102static JdwpError handleTGR_Children(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1103 ObjectId threadGroupId = ReadObjectId(&buf);
1104 LOG(VERBOSE) << StringPrintf(" Req for threads in threadGroupId=0x%llx", threadGroupId);
1105
1106 ObjectId* pThreadIds;
1107 uint32_t threadCount;
1108 Dbg::GetThreadGroupThreads(threadGroupId, &pThreadIds, &threadCount);
1109
1110 expandBufAdd4BE(pReply, threadCount);
1111
1112 for (uint32_t i = 0; i < threadCount; i++) {
1113 expandBufAddObjectId(pReply, pThreadIds[i]);
1114 }
1115 free(pThreadIds);
1116
1117 /*
1118 * TODO: finish support for child groups
1119 *
1120 * For now, just show that "main" is a child of "system".
1121 */
1122 if (threadGroupId == Dbg::GetSystemThreadGroupId()) {
1123 expandBufAdd4BE(pReply, 1);
1124 expandBufAddObjectId(pReply, Dbg::GetMainThreadGroupId());
1125 } else {
1126 expandBufAdd4BE(pReply, 0);
1127 }
1128
1129 return ERR_NONE;
1130}
1131
1132/*
1133 * Return the #of components in the array.
1134 */
1135static JdwpError handleAR_Length(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1136 ObjectId arrayId = ReadObjectId(&buf);
1137 LOG(VERBOSE) << StringPrintf(" Req for length of array 0x%llx", arrayId);
1138
1139 uint32_t arrayLength = Dbg::GetArrayLength(arrayId);
1140
1141 LOG(VERBOSE) << StringPrintf(" --> %d", arrayLength);
1142
1143 expandBufAdd4BE(pReply, arrayLength);
1144
1145 return ERR_NONE;
1146}
1147
1148/*
1149 * Return the values from an array.
1150 */
1151static JdwpError handleAR_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1152 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001153 uint32_t firstIndex = Read4BE(&buf);
1154 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001155
1156 uint8_t tag = Dbg::GetArrayElementTag(arrayId);
1157 LOG(VERBOSE) << StringPrintf(" Req for array values 0x%llx first=%d len=%d (elem tag=%c)", arrayId, firstIndex, length, tag);
1158
1159 expandBufAdd1(pReply, tag);
1160 expandBufAdd4BE(pReply, length);
1161
1162 if (!Dbg::OutputArray(arrayId, firstIndex, length, pReply)) {
1163 return ERR_INVALID_LENGTH;
1164 }
1165
1166 return ERR_NONE;
1167}
1168
1169/*
1170 * Set values in an array.
1171 */
1172static JdwpError handleAR_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1173 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001174 uint32_t firstIndex = Read4BE(&buf);
1175 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001176
1177 LOG(VERBOSE) << StringPrintf(" Req to set array values 0x%llx first=%d count=%d", arrayId, firstIndex, values);
1178
1179 if (!Dbg::SetArrayElements(arrayId, firstIndex, values, buf)) {
1180 return ERR_INVALID_LENGTH;
1181 }
1182
1183 return ERR_NONE;
1184}
1185
1186/*
1187 * Return the set of classes visible to a class loader. All classes which
1188 * have the class loader as a defining or initiating loader are returned.
1189 */
1190static JdwpError handleCLR_VisibleClasses(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1191 ObjectId classLoaderObject;
1192 uint32_t numClasses = 0;
1193 RefTypeId* classRefBuf = NULL;
1194 int i;
1195
1196 classLoaderObject = ReadObjectId(&buf);
1197
1198 Dbg::GetVisibleClassList(classLoaderObject, &numClasses, &classRefBuf);
1199
1200 expandBufAdd4BE(pReply, numClasses);
1201 for (i = 0; i < (int) numClasses; i++) {
1202 uint8_t refTypeTag = Dbg::GetClassObjectType(classRefBuf[i]);
1203
1204 expandBufAdd1(pReply, refTypeTag);
1205 expandBufAddRefTypeId(pReply, classRefBuf[i]);
1206 }
1207
1208 return ERR_NONE;
1209}
1210
1211/*
1212 * Return a newly-allocated string in which all occurrences of '.' have
1213 * been changed to '/'. If we find a '/' in the original string, NULL
1214 * is returned to avoid ambiguity.
1215 */
1216char* dvmDotToSlash(const char* str) {
1217 char* newStr = strdup(str);
1218 char* cp = newStr;
1219
1220 if (newStr == NULL) {
1221 return NULL;
1222 }
1223
1224 while (*cp != '\0') {
1225 if (*cp == '/') {
1226 CHECK(false);
1227 return NULL;
1228 }
1229 if (*cp == '.') {
1230 *cp = '/';
1231 }
1232 cp++;
1233 }
1234
1235 return newStr;
1236}
1237
1238/*
1239 * Set an event trigger.
1240 *
1241 * Reply with a requestID.
1242 */
1243static JdwpError handleER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1244 const uint8_t* origBuf = buf;
1245
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001246 uint8_t eventKind = Read1(&buf);
1247 uint8_t suspendPolicy = Read1(&buf);
1248 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001249
1250 LOG(VERBOSE) << " Set(kind=" << JdwpEventKind(eventKind)
1251 << " suspend=" << JdwpSuspendPolicy(suspendPolicy)
1252 << " mods=" << modifierCount << ")";
1253
1254 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1255
1256 JdwpEvent* pEvent = EventAlloc(modifierCount);
1257 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
1258 pEvent->suspendPolicy = static_cast<JdwpSuspendPolicy>(suspendPolicy);
1259 pEvent->modCount = modifierCount;
1260
1261 /*
1262 * Read modifiers. Ordering may be significant (see explanation of Count
1263 * mods in JDWP doc).
1264 */
1265 for (uint32_t idx = 0; idx < modifierCount; idx++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001266 uint8_t modKind = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001267
1268 pEvent->mods[idx].modKind = modKind;
1269
1270 switch (modKind) {
1271 case MK_COUNT: /* report once, when "--count" reaches 0 */
1272 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001273 uint32_t count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001274 LOG(VERBOSE) << " Count: " << count;
1275 if (count == 0) {
1276 return ERR_INVALID_COUNT;
1277 }
1278 pEvent->mods[idx].count.count = count;
1279 }
1280 break;
1281 case MK_CONDITIONAL: /* conditional on expression) */
1282 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001283 uint32_t exprId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001284 LOG(VERBOSE) << " Conditional: " << exprId;
1285 pEvent->mods[idx].conditional.exprId = exprId;
1286 }
1287 break;
1288 case MK_THREAD_ONLY: /* only report events in specified thread */
1289 {
1290 ObjectId threadId = ReadObjectId(&buf);
1291 LOG(VERBOSE) << StringPrintf(" ThreadOnly: %llx", threadId);
1292 pEvent->mods[idx].threadOnly.threadId = threadId;
1293 }
1294 break;
1295 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1296 {
1297 RefTypeId clazzId = ReadRefTypeId(&buf);
Elliott Hughesa2155262011-11-16 16:26:58 -08001298 LOG(VERBOSE) << StringPrintf(" ClassOnly: %llx (%s)", clazzId, Dbg::GetClassDescriptor(clazzId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001299 pEvent->mods[idx].classOnly.refTypeId = clazzId;
1300 }
1301 break;
1302 case MK_CLASS_MATCH: /* restrict events to matching classes */
1303 {
1304 char* pattern;
1305 size_t strLen;
1306
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001307 pattern = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001308 LOG(VERBOSE) << StringPrintf(" ClassMatch: '%s'", pattern);
1309 /* pattern is "java.foo.*", we want "java/foo/ *" */
1310 pEvent->mods[idx].classMatch.classPattern = dvmDotToSlash(pattern);
1311 free(pattern);
1312 }
1313 break;
1314 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1315 {
1316 char* pattern;
1317 size_t strLen;
1318
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001319 pattern = ReadNewUtf8String(&buf, &strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001320 LOG(VERBOSE) << StringPrintf(" ClassExclude: '%s'", pattern);
1321 pEvent->mods[idx].classExclude.classPattern = dvmDotToSlash(pattern);
1322 free(pattern);
1323 }
1324 break;
1325 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1326 {
1327 JdwpLocation loc;
1328
1329 jdwpReadLocation(&buf, &loc);
1330 LOG(VERBOSE) << StringPrintf(" LocationOnly: typeTag=%d classId=%llx methodId=%x idx=%llx",
1331 loc.typeTag, loc.classId, loc.methodId, loc.idx);
1332 pEvent->mods[idx].locationOnly.loc = loc;
1333 }
1334 break;
1335 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1336 {
1337 RefTypeId exceptionOrNull; /* null == all exceptions */
1338 uint8_t caught, uncaught;
1339
1340 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001341 caught = Read1(&buf);
1342 uncaught = Read1(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001343 LOG(VERBOSE) << StringPrintf(" ExceptionOnly: type=%llx(%s) caught=%d uncaught=%d",
Elliott Hughesa2155262011-11-16 16:26:58 -08001344 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassDescriptor(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001345
1346 pEvent->mods[idx].exceptionOnly.refTypeId = exceptionOrNull;
1347 pEvent->mods[idx].exceptionOnly.caught = caught;
1348 pEvent->mods[idx].exceptionOnly.uncaught = uncaught;
1349 }
1350 break;
1351 case MK_FIELD_ONLY: /* for field access/mod events */
1352 {
1353 RefTypeId declaring = ReadRefTypeId(&buf);
1354 FieldId fieldId = ReadFieldId(&buf);
1355 LOG(VERBOSE) << StringPrintf(" FieldOnly: %llx %x", declaring, fieldId);
1356 pEvent->mods[idx].fieldOnly.refTypeId = declaring;
1357 pEvent->mods[idx].fieldOnly.fieldId = fieldId;
1358 }
1359 break;
1360 case MK_STEP: /* for use with EK_SINGLE_STEP */
1361 {
1362 ObjectId threadId;
1363 uint32_t size, depth;
1364
1365 threadId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001366 size = Read4BE(&buf);
1367 depth = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001368 LOG(VERBOSE) << StringPrintf(" Step: thread=%llx", threadId)
1369 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1370
1371 pEvent->mods[idx].step.threadId = threadId;
1372 pEvent->mods[idx].step.size = size;
1373 pEvent->mods[idx].step.depth = depth;
1374 }
1375 break;
1376 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1377 {
1378 ObjectId instance = ReadObjectId(&buf);
1379 LOG(VERBOSE) << StringPrintf(" InstanceOnly: %llx", instance);
1380 pEvent->mods[idx].instanceOnly.objectId = instance;
1381 }
1382 break;
1383 default:
1384 LOG(WARNING) << "GLITCH: unsupported modKind=" << modKind;
1385 break;
1386 }
1387 }
1388
1389 /*
1390 * Make sure we consumed all data. It is possible that the remote side
1391 * has sent us bad stuff, but for now we blame ourselves.
1392 */
1393 if (buf != origBuf + dataLen) {
1394 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1395 }
1396
1397 /*
1398 * We reply with an integer "requestID".
1399 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001400 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001401 expandBufAdd4BE(pReply, requestId);
1402
1403 pEvent->requestId = requestId;
1404
1405 LOG(VERBOSE) << StringPrintf(" --> event requestId=%#x", requestId);
1406
1407 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001408 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001409 if (err != ERR_NONE) {
1410 /* registration failed, probably because event is bogus */
1411 EventFree(pEvent);
1412 LOG(WARNING) << "WARNING: event request rejected";
1413 }
1414 return err;
1415}
1416
1417/*
1418 * Clear an event. Failure to find an event with a matching ID is a no-op
1419 * and does not return an error.
1420 */
1421static JdwpError handleER_Clear(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1422 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001423 eventKind = Read1(&buf);
1424 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001425
1426 LOG(VERBOSE) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
1427
Elliott Hughes761928d2011-11-16 18:33:03 -08001428 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001429
1430 return ERR_NONE;
1431}
1432
1433/*
1434 * Return the values of arguments and local variables.
1435 */
1436static JdwpError handleSF_GetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1437 ObjectId threadId = ReadObjectId(&buf);
1438 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001439 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001440
1441 LOG(VERBOSE) << StringPrintf(" Req for %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
1442
1443 expandBufAdd4BE(pReply, slots); /* "int values" */
1444 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001445 uint32_t slot = Read4BE(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001446 JDWP::JdwpTag reqSigByte = static_cast<JDWP::JdwpTag>(Read1(&buf));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001447
1448 LOG(VERBOSE) << StringPrintf(" --> slot %d '%c'", slot, reqSigByte);
1449
Elliott Hughesdbb40792011-11-18 17:05:22 -08001450 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001451 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
1452 Dbg::GetLocalValue(threadId, frameId, slot, reqSigByte, ptr, width);
1453 }
1454
1455 return ERR_NONE;
1456}
1457
1458/*
1459 * Set the values of arguments and local variables.
1460 */
1461static JdwpError handleSF_SetValues(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1462 ObjectId threadId = ReadObjectId(&buf);
1463 FrameId frameId = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001464 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001465
1466 LOG(VERBOSE) << StringPrintf(" Req to set %d slots in threadId=%llx frameId=%llx", slots, threadId, frameId);
1467
1468 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001469 uint32_t slot = Read4BE(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001470 JDWP::JdwpTag sigByte = static_cast<JDWP::JdwpTag>(Read1(&buf));
1471 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001472 uint64_t value = jdwpReadValue(&buf, width);
1473
1474 LOG(VERBOSE) << StringPrintf(" --> slot %d '%c' %llx", slot, sigByte, value);
1475 Dbg::SetLocalValue(threadId, frameId, slot, sigByte, value, width);
1476 }
1477
1478 return ERR_NONE;
1479}
1480
1481/*
1482 * Returns the value of "this" for the specified frame.
1483 */
1484static JdwpError handleSF_ThisObject(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1485 ObjectId threadId = ReadObjectId(&buf);
1486 FrameId frameId = ReadFrameId(&buf);
1487
1488 ObjectId objectId;
1489 if (!Dbg::GetThisObject(threadId, frameId, &objectId)) {
1490 return ERR_INVALID_FRAMEID;
1491 }
1492
1493 uint8_t objectTag = Dbg::GetObjectTag(objectId);
1494 LOG(VERBOSE) << StringPrintf(" Req for 'this' in thread=%llx frame=%llx --> %llx %s '%c'", threadId, frameId, objectId, Dbg::GetObjectTypeName(objectId), (char)objectTag);
1495
1496 expandBufAdd1(pReply, objectTag);
1497 expandBufAddObjectId(pReply, objectId);
1498
1499 return ERR_NONE;
1500}
1501
1502/*
1503 * Return the reference type reflected by this class object.
1504 *
1505 * This appears to be required because ReferenceTypeId values are NEVER
1506 * reused, whereas ClassIds can be recycled like any other object. (Either
1507 * that, or I have no idea what this is for.)
1508 */
1509static JdwpError handleCOR_ReflectedType(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1510 RefTypeId classObjectId = ReadRefTypeId(&buf);
1511
Elliott Hughesa2155262011-11-16 16:26:58 -08001512 LOG(VERBOSE) << StringPrintf(" Req for refTypeId for class=%llx (%s)", classObjectId, Dbg::GetClassDescriptor(classObjectId).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001513
1514 /* just hand the type back to them */
1515 if (Dbg::IsInterface(classObjectId)) {
1516 expandBufAdd1(pReply, TT_INTERFACE);
1517 } else {
1518 expandBufAdd1(pReply, TT_CLASS);
1519 }
1520 expandBufAddRefTypeId(pReply, classObjectId);
1521
1522 return ERR_NONE;
1523}
1524
1525/*
1526 * Handle a DDM packet with a single chunk in it.
1527 */
1528static JdwpError handleDDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
1529 uint8_t* replyBuf = NULL;
1530 int replyLen = -1;
1531
1532 LOG(VERBOSE) << StringPrintf(" Handling DDM packet (%.4s)", buf);
1533
1534 /*
1535 * On first DDM packet, notify all handlers that DDM is running.
1536 */
1537 if (!state->ddmActive) {
1538 state->ddmActive = true;
1539 Dbg::DdmConnected();
1540 }
1541
1542 /*
1543 * If they want to send something back, we copy it into the buffer.
1544 * A no-copy approach would be nicer.
1545 *
1546 * TODO: consider altering the JDWP stuff to hold the packet header
1547 * in a separate buffer. That would allow us to writev() DDM traffic
1548 * instead of copying it into the expanding buffer. The reduction in
1549 * heap requirements is probably more valuable than the efficiency.
1550 */
1551 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1552 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1553 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1554 free(replyBuf);
1555 }
1556 return ERR_NONE;
1557}
1558
1559/*
1560 * Handler map decl.
1561 */
1562typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1563
1564struct JdwpHandlerMap {
1565 uint8_t cmdSet;
1566 uint8_t cmd;
1567 JdwpRequestHandler func;
1568 const char* descr;
1569};
1570
1571/*
1572 * Map commands to functions.
1573 *
1574 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1575 * and 128-256 are vendor-defined.
1576 */
1577static const JdwpHandlerMap gHandlerMap[] = {
1578 /* VirtualMachine command set (1) */
1579 { 1, 1, handleVM_Version, "VirtualMachine.Version" },
1580 { 1, 2, handleVM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1581 //1, 3, VirtualMachine.AllClasses
1582 { 1, 4, handleVM_AllThreads, "VirtualMachine.AllThreads" },
1583 { 1, 5, handleVM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1584 { 1, 6, handleVM_Dispose, "VirtualMachine.Dispose" },
1585 { 1, 7, handleVM_IDSizes, "VirtualMachine.IDSizes" },
1586 { 1, 8, handleVM_Suspend, "VirtualMachine.Suspend" },
1587 { 1, 9, handleVM_Resume, "VirtualMachine.Resume" },
1588 { 1, 10, handleVM_Exit, "VirtualMachine.Exit" },
1589 { 1, 11, handleVM_CreateString, "VirtualMachine.CreateString" },
1590 { 1, 12, handleVM_Capabilities, "VirtualMachine.Capabilities" },
1591 { 1, 13, handleVM_ClassPaths, "VirtualMachine.ClassPaths" },
1592 { 1, 14, HandleVM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1593 //1, 15, HoldEvents
1594 //1, 16, ReleaseEvents
1595 { 1, 17, handleVM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1596 //1, 18, RedefineClasses
1597 //1, 19, SetDefaultStratum
1598 { 1, 20, handleVM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric"},
1599 //1, 21, InstanceCounts
1600
1601 /* ReferenceType command set (2) */
1602 { 2, 1, handleRT_Signature, "ReferenceType.Signature" },
1603 { 2, 2, handleRT_ClassLoader, "ReferenceType.ClassLoader" },
1604 { 2, 3, handleRT_Modifiers, "ReferenceType.Modifiers" },
1605 //2, 4, Fields
1606 //2, 5, Methods
1607 { 2, 6, handleRT_GetValues, "ReferenceType.GetValues" },
1608 { 2, 7, handleRT_SourceFile, "ReferenceType.SourceFile" },
1609 //2, 8, NestedTypes
1610 { 2, 9, handleRT_Status, "ReferenceType.Status" },
1611 { 2, 10, handleRT_Interfaces, "ReferenceType.Interfaces" },
1612 { 2, 11, handleRT_ClassObject, "ReferenceType.ClassObject" },
1613 { 2, 12, handleRT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1614 { 2, 13, handleRT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1615 { 2, 14, handleRT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1616 { 2, 15, handleRT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
1617 //2, 16, Instances
1618 //2, 17, ClassFileVersion
1619 //2, 18, ConstantPool
1620
1621 /* ClassType command set (3) */
1622 { 3, 1, handleCT_Superclass, "ClassType.Superclass" },
1623 { 3, 2, handleCT_SetValues, "ClassType.SetValues" },
1624 { 3, 3, handleCT_InvokeMethod, "ClassType.InvokeMethod" },
1625 { 3, 4, handleCT_NewInstance, "ClassType.NewInstance" },
1626
1627 /* ArrayType command set (4) */
1628 { 4, 1, handleAT_newInstance, "ArrayType.NewInstance" },
1629
1630 /* InterfaceType command set (5) */
1631
1632 /* Method command set (6) */
1633 { 6, 1, handleM_LineTable, "Method.LineTable" },
1634 //6, 2, VariableTable
1635 //6, 3, Bytecodes
1636 //6, 4, IsObsolete
1637 { 6, 5, handleM_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
1638
1639 /* Field command set (8) */
1640
1641 /* ObjectReference command set (9) */
1642 { 9, 1, handleOR_ReferenceType, "ObjectReference.ReferenceType" },
1643 { 9, 2, handleOR_GetValues, "ObjectReference.GetValues" },
1644 { 9, 3, handleOR_SetValues, "ObjectReference.SetValues" },
1645 //9, 4, (not defined)
1646 //9, 5, MonitorInfo
1647 { 9, 6, handleOR_InvokeMethod, "ObjectReference.InvokeMethod" },
1648 { 9, 7, handleOR_DisableCollection, "ObjectReference.DisableCollection" },
1649 { 9, 8, handleOR_EnableCollection, "ObjectReference.EnableCollection" },
1650 { 9, 9, handleOR_IsCollected, "ObjectReference.IsCollected" },
1651 //9, 10, ReferringObjects
1652
1653 /* StringReference command set (10) */
1654 { 10, 1, handleSR_Value, "StringReference.Value" },
1655
1656 /* ThreadReference command set (11) */
1657 { 11, 1, handleTR_Name, "ThreadReference.Name" },
1658 { 11, 2, handleTR_Suspend, "ThreadReference.Suspend" },
1659 { 11, 3, handleTR_Resume, "ThreadReference.Resume" },
1660 { 11, 4, handleTR_Status, "ThreadReference.Status" },
1661 { 11, 5, handleTR_ThreadGroup, "ThreadReference.ThreadGroup" },
1662 { 11, 6, handleTR_Frames, "ThreadReference.Frames" },
1663 { 11, 7, handleTR_FrameCount, "ThreadReference.FrameCount" },
1664 //11, 8, OwnedMonitors
1665 { 11, 9, handleTR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1666 //11, 10, Stop
1667 //11, 11, Interrupt
1668 { 11, 12, handleTR_SuspendCount, "ThreadReference.SuspendCount" },
1669 //11, 13, OwnedMonitorsStackDepthInfo
1670 //11, 14, ForceEarlyReturn
1671
1672 /* ThreadGroupReference command set (12) */
1673 { 12, 1, handleTGR_Name, "ThreadGroupReference.Name" },
1674 { 12, 2, handleTGR_Parent, "ThreadGroupReference.Parent" },
1675 { 12, 3, handleTGR_Children, "ThreadGroupReference.Children" },
1676
1677 /* ArrayReference command set (13) */
1678 { 13, 1, handleAR_Length, "ArrayReference.Length" },
1679 { 13, 2, handleAR_GetValues, "ArrayReference.GetValues" },
1680 { 13, 3, handleAR_SetValues, "ArrayReference.SetValues" },
1681
1682 /* ClassLoaderReference command set (14) */
1683 { 14, 1, handleCLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
1684
1685 /* EventRequest command set (15) */
1686 { 15, 1, handleER_Set, "EventRequest.Set" },
1687 { 15, 2, handleER_Clear, "EventRequest.Clear" },
1688 //15, 3, ClearAllBreakpoints
1689
1690 /* StackFrame command set (16) */
1691 { 16, 1, handleSF_GetValues, "StackFrame.GetValues" },
1692 { 16, 2, handleSF_SetValues, "StackFrame.SetValues" },
1693 { 16, 3, handleSF_ThisObject, "StackFrame.ThisObject" },
1694 //16, 4, PopFrames
1695
1696 /* ClassObjectReference command set (17) */
1697 { 17, 1, handleCOR_ReflectedType,"ClassObjectReference.ReflectedType" },
1698
1699 /* Event command set (64) */
1700 //64, 100, Composite <-- sent from VM to debugger, never received by VM
1701
1702 { 199, 1, handleDDM_Chunk, "DDM.Chunk" },
1703};
1704
1705/*
1706 * Process a request from the debugger.
1707 *
1708 * On entry, the JDWP thread is in VMWAIT.
1709 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001710void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001711 JdwpError result = ERR_NONE;
1712 int i, respLen;
1713
1714 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1715 /*
1716 * Activity from a debugger, not merely ddms. Mark us as having an
1717 * active debugger session, and zero out the last-activity timestamp
1718 * so waitForDebugger() doesn't return if we stall for a bit here.
1719 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001720 Dbg::GoActive();
Elliott Hughes376a7a02011-10-24 18:35:55 -07001721 QuasiAtomicSwap64(0, &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001722 }
1723
1724 /*
1725 * If a debugger event has fired in another thread, wait until the
1726 * initiating thread has suspended itself before processing messages
1727 * from the debugger. Otherwise we (the JDWP thread) could be told to
1728 * resume the thread before it has suspended.
1729 *
1730 * We call with an argument of zero to wait for the current event
1731 * thread to finish, and then clear the block. Depending on the thread
1732 * suspend policy, this may allow events in other threads to fire,
1733 * but those events have no bearing on what the debugger has sent us
1734 * in the current request.
1735 *
1736 * Note that we MUST clear the event token before waking the event
1737 * thread up, or risk waiting for the thread to suspend after we've
1738 * told it to resume.
1739 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001740 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001741
1742 /*
1743 * Tell the VM that we're running and shouldn't be interrupted by GC.
1744 * Do this after anything that can stall indefinitely.
1745 */
1746 Dbg::ThreadRunning();
1747
1748 expandBufAddSpace(pReply, kJDWPHeaderLen);
1749
1750 for (i = 0; i < (int) arraysize(gHandlerMap); i++) {
1751 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd) {
1752 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 -07001753 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001754 break;
1755 }
1756 }
1757 if (i == arraysize(gHandlerMap)) {
1758 LOG(ERROR) << StringPrintf("REQ: UNSUPPORTED (cmd=%d/%d dataLen=%d id=0x%06x)", pHeader->cmdSet, pHeader->cmd, dataLen, pHeader->id);
1759 if (dataLen > 0) {
1760 HexDump(buf, dataLen);
1761 }
1762 LOG(FATAL) << "command not implemented"; // make it *really* obvious
1763 result = ERR_NOT_IMPLEMENTED;
1764 }
1765
1766 /*
1767 * Set up the reply header.
1768 *
1769 * If we encountered an error, only send the header back.
1770 */
1771 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001772 Set4BE(replyBuf + 4, pHeader->id);
1773 Set1(replyBuf + 8, kJDWPFlagReply);
1774 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001775 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001776 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001777 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001778 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001779 }
1780
1781 respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
1782 if (false) {
1783 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
1784 if (respLen > 0) {
1785 HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
1786 }
1787 }
1788
1789 /*
1790 * Update last-activity timestamp. We really only need this during
1791 * the initial setup. Only update if this is a non-DDMS packet.
1792 */
1793 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07001794 QuasiAtomicSwap64(MilliTime(), &lastActivityWhen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001795 }
1796
1797 /* tell the VM that GC is okay again */
1798 Dbg::ThreadWaiting();
1799}
1800
1801} // namespace JDWP
1802
1803} // namespace art