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