blob: fbe9192ee25759e484cd640e65c9633f5e96977e [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 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070050static void JdwpReadLocation(const uint8_t** pBuf, JdwpLocation* pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070051 memset(pLoc, 0, sizeof(*pLoc)); /* allows memcmp() later */
Elliott Hughes74847412012-06-20 18:10:21 -070052 pLoc->type_tag = ReadTypeTag(pBuf);
53 pLoc->class_id = ReadObjectId(pBuf);
54 pLoc->method_id = ReadMethodId(pBuf);
Elliott Hughes972a47b2012-02-21 18:16:06 -080055 pLoc->dex_pc = Read8BE(pBuf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070056}
57
58/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -070059 * Helper function: read a variable-width value from the input buffer.
60 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070061static uint64_t JdwpReadValue(const uint8_t** pBuf, size_t width) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070062 uint64_t value = -1;
63 switch (width) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070064 case 1: value = Read1(pBuf); break;
65 case 2: value = Read2BE(pBuf); break;
66 case 4: value = Read4BE(pBuf); break;
67 case 8: value = Read8BE(pBuf); break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070068 default: LOG(FATAL) << width; break;
69 }
70 return value;
71}
72
73/*
74 * Helper function: write a variable-width value into the output input buffer.
75 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070076static void JdwpWriteValue(ExpandBuf* pReply, int width, uint64_t value) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070077 switch (width) {
78 case 1: expandBufAdd1(pReply, value); break;
79 case 2: expandBufAdd2BE(pReply, value); break;
80 case 4: expandBufAdd4BE(pReply, value); break;
81 case 8: expandBufAdd8BE(pReply, value); break;
82 default: LOG(FATAL) << width; break;
83 }
84}
85
86/*
87 * Common code for *_InvokeMethod requests.
88 *
Elliott Hughes74847412012-06-20 18:10:21 -070089 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -070090 * expected-to-be-void return value of the called function.
91 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070092static JdwpError FinishInvoke(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
Elliott Hughes74847412012-06-20 18:10:21 -070093 ObjectId thread_id, ObjectId object_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 RefTypeId class_id, MethodId method_id, bool is_constructor)
95 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -070096 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070097
Elliott Hughes45651fd2012-02-21 15:48:20 -080098 uint32_t arg_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070099
Elliott Hughes74847412012-06-20 18:10:21 -0700100 VLOG(jdwp) << StringPrintf(" --> thread_id=%#llx object_id=%#llx", thread_id, object_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 VLOG(jdwp) << StringPrintf(" class_id=%#llx method_id=%x %s.%s", class_id,
102 method_id, Dbg::GetClassName(class_id).c_str(),
103 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800104 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700105
Elliott Hughes45651fd2012-02-21 15:48:20 -0800106 UniquePtr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
107 UniquePtr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
108 for (uint32_t i = 0; i < arg_count; ++i) {
109 argTypes[i] = ReadTag(&buf);
110 size_t width = Dbg::GetTagWidth(argTypes[i]);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700111 argValues[i] = JdwpReadValue(&buf, width);
Elliott Hughes229feb72012-02-23 13:33:29 -0800112 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#llx", width, argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700113 }
114
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700115 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
117 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
118 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700119
Elliott Hughes45651fd2012-02-21 15:48:20 -0800120 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121 uint64_t resultValue;
122 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700123 JdwpError err = Dbg::InvokeMethod(thread_id, object_id, class_id, method_id, arg_count, argValues.get(), argTypes.get(), options, &resultTag, &resultValue, &exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800125 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 }
127
128 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800129 if (is_constructor) {
130 // If we invoked a constructor (which actually returns void), return the receiver,
131 // unless we threw, in which case we return NULL.
132 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700133 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800134 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700135
Elliott Hughes45651fd2012-02-21 15:48:20 -0800136 size_t width = Dbg::GetTagWidth(resultTag);
137 expandBufAdd1(pReply, resultTag);
138 if (width != 0) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700139 JdwpWriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140 }
141 expandBufAdd1(pReply, JT_OBJECT);
142 expandBufAddObjectId(pReply, exceptObjId);
143
Elliott Hughes229feb72012-02-23 13:33:29 -0800144 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" %#llx (except=%#llx)", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700145
146 /* show detailed debug output */
147 if (resultTag == JT_STRING && exceptObjId == 0) {
148 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800149 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700150 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800151 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152 }
153 }
154 }
155
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700156 return err;
157}
158
159
160/*
161 * Request for version info.
162 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163static JdwpError VM_Version(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
164 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700165 /* text information on runtime version */
166 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800167 expandBufAddUtf8String(pReply, version);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700168 /* JDWP version numbers */
169 expandBufAdd4BE(pReply, 1); // major
170 expandBufAdd4BE(pReply, 5); // minor
171 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800172 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700173 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800174 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700175
176 return ERR_NONE;
177}
178
179/*
180 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
181 * referenceTypeID. We need to send back more than one if the class has
182 * been loaded by multiple class loaders.
183 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700184static JdwpError VM_ClassesBySignature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
185 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800186 std::string classDescriptor(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800187 VLOG(jdwp) << " Req for class by signature '" << classDescriptor << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700188
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800189 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800190 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700191
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800192 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700193
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800194 for (size_t i = 0; i < ids.size(); ++i) {
195 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800196 JDWP::JdwpTypeTag type_tag;
197 uint32_t class_status;
198 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
199 if (status != ERR_NONE) {
200 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800201 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202
Elliott Hughes436e3722012-02-17 20:01:47 -0800203 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800204 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800205 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700206 }
207
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700208 return ERR_NONE;
209}
210
211/*
212 * Handle request for the thread IDs of all running threads.
213 *
214 * We exclude ourselves from the list, because we don't allow ourselves
215 * to be suspended, and that violates some JDWP expectations.
216 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700217static JdwpError VM_AllThreads(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
218 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700219 std::vector<ObjectId> thread_ids;
Elliott Hughes026b1462012-06-28 20:43:49 -0700220 Dbg::GetThreads(0, thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700221
Elliott Hughescaf76542012-06-28 16:08:22 -0700222 expandBufAdd4BE(pReply, thread_ids.size());
223 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
224 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700225 }
226
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700227 return ERR_NONE;
228}
229
230/*
231 * List all thread groups that do not have a parent.
232 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233static JdwpError VM_TopLevelThreadGroups(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
234 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235 /*
236 * TODO: maintain a list of parentless thread groups in the VM.
237 *
238 * For now, just return "system". Application threads are created
239 * in "main", which is a child of "system".
240 */
241 uint32_t groups = 1;
242 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700243 //thread_group_id = debugGetMainThreadGroup();
244 //expandBufAdd8BE(pReply, thread_group_id);
245 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
246 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700247
248 return ERR_NONE;
249}
250
251/*
252 * Respond with the sizes of the basic debugger types.
253 *
254 * All IDs are 8 bytes.
255 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256static JdwpError VM_IDSizes(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
257 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700258 expandBufAdd4BE(pReply, sizeof(FieldId));
259 expandBufAdd4BE(pReply, sizeof(MethodId));
260 expandBufAdd4BE(pReply, sizeof(ObjectId));
261 expandBufAdd4BE(pReply, sizeof(RefTypeId));
262 expandBufAdd4BE(pReply, sizeof(FrameId));
263 return ERR_NONE;
264}
265
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266static JdwpError VM_Dispose(JdwpState*, const uint8_t*, int, ExpandBuf*)
267 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes86964332012-02-15 19:37:42 -0800268 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700269 return ERR_NONE;
270}
271
272/*
273 * Suspend the execution of the application running in the VM (i.e. suspend
274 * all threads).
275 *
276 * This needs to increment the "suspend count" on all threads.
277 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278static JdwpError VM_Suspend(JdwpState*, const uint8_t*, int, ExpandBuf*)
279 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700280 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700281 return ERR_NONE;
282}
283
284/*
285 * Resume execution. Decrements the "suspend count" of all threads.
286 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287static JdwpError VM_Resume(JdwpState*, const uint8_t*, int, ExpandBuf*)
288 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700289 Dbg::ResumeVM();
290 return ERR_NONE;
291}
292
293/*
294 * The debugger wants the entire VM to exit.
295 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296static JdwpError VM_Exit(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
297 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700298 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700299
300 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
301
302 Dbg::Exit(exitCode);
303 return ERR_NOT_IMPLEMENTED; // shouldn't get here
304}
305
306/*
307 * Create a new string in the VM and return its ID.
308 *
309 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
310 * string "java.util.Arrays".)
311 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312static JdwpError VM_CreateString(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
313 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800314 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800315 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700316 ObjectId stringId = Dbg::CreateString(str);
317 if (stringId == 0) {
318 return ERR_OUT_OF_MEMORY;
319 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700320 expandBufAddObjectId(pReply, stringId);
321 return ERR_NONE;
322}
323
324/*
325 * Tell the debugger what we are capable of.
326 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700327static JdwpError VM_Capabilities(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
328 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700329 expandBufAdd1(pReply, false); /* canWatchFieldModification */
330 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
331 expandBufAdd1(pReply, false); /* canGetBytecodes */
332 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
333 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
334 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
335 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
336 return ERR_NONE;
337}
338
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339static JdwpError VM_ClassPaths(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
340 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800341 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700342
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800343 std::vector<std::string> class_path;
344 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
345 expandBufAdd4BE(pReply, class_path.size());
346 for (size_t i = 0; i < class_path.size(); ++i) {
347 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700348 }
349
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800350 std::vector<std::string> boot_class_path;
351 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
352 expandBufAdd4BE(pReply, boot_class_path.size());
353 for (size_t i = 0; i < boot_class_path.size(); ++i) {
354 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700355 }
356
357 return ERR_NONE;
358}
359
360/*
361 * Release a list of object IDs. (Seen in jdb.)
362 *
363 * Currently does nothing.
364 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700365static JdwpError VM_DisposeObjects(JdwpState*, const uint8_t*, int, ExpandBuf*)
366 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700367 return ERR_NONE;
368}
369
370/*
371 * Tell the debugger what we are capable of.
372 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700373static JdwpError VM_CapabilitiesNew(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
374 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700375 expandBufAdd1(pReply, false); /* canWatchFieldModification */
376 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
377 expandBufAdd1(pReply, false); /* canGetBytecodes */
378 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
379 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
380 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
381 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
382 expandBufAdd1(pReply, false); /* canRedefineClasses */
383 expandBufAdd1(pReply, false); /* canAddMethod */
384 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
385 expandBufAdd1(pReply, false); /* canPopFrames */
386 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
387 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
388 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
389 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
390 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
391 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
392 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
393 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
394 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
395 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
396
397 /* fill in reserved22 through reserved32; note count started at 1 */
398 for (int i = 22; i <= 32; i++) {
399 expandBufAdd1(pReply, false); /* reservedN */
400 }
401 return ERR_NONE;
402}
403
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
405 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800406 std::vector<JDWP::RefTypeId> classes;
407 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700408
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800409 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700410
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800411 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800412 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800413 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800414 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800415 uint32_t class_status;
416 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
417 if (status != ERR_NONE) {
418 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800419 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420
Elliott Hughes436e3722012-02-17 20:01:47 -0800421 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800422 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800423 if (descriptor_and_status) {
424 expandBufAddUtf8String(pReply, descriptor);
425 if (generic) {
426 expandBufAddUtf8String(pReply, genericSignature);
427 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800428 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800429 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700430 }
431
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700432 return ERR_NONE;
433}
434
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700435static JdwpError VM_AllClasses(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
436 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700437 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800438}
439
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700440static JdwpError VM_AllClassesWithGeneric(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
441 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700442 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800443}
444
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700445static JdwpError RT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
446 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700447 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800448 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700449}
450
451/*
452 * Get values from static fields in a reference type.
453 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700454static JdwpError RT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
455 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800456 RefTypeId refTypeId = ReadRefTypeId(&buf);
457 uint32_t field_count = Read4BE(&buf);
458 expandBufAdd4BE(pReply, field_count);
459 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700460 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800461 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800462 if (status != ERR_NONE) {
463 return status;
464 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700465 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700466 return ERR_NONE;
467}
468
469/*
470 * Get the name of the source file in which a reference type was declared.
471 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700472static JdwpError RT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
473 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700474 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800475 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800476 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
477 if (status != ERR_NONE) {
478 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700479 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800480 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800481 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700482}
483
484/*
485 * Return the current status of the reference type.
486 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700487static JdwpError RT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
488 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700489 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800490 JDWP::JdwpTypeTag type_tag;
491 uint32_t class_status;
492 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
493 if (status != ERR_NONE) {
494 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800495 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800496 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700497 return ERR_NONE;
498}
499
500/*
501 * Return interfaces implemented directly by this class.
502 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700503static JdwpError RT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
504 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700505 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800506 VLOG(jdwp) << StringPrintf(" Req for interfaces in %#llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800507 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700508}
509
510/*
511 * Return the class object corresponding to this type.
512 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700513static JdwpError RT_ClassObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
514 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700515 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800516 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800517 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
518 if (status != ERR_NONE) {
519 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800520 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800521 VLOG(jdwp) << StringPrintf(" RefTypeId %#llx -> ObjectId %#llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800522 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700523 return ERR_NONE;
524}
525
526/*
527 * Returns the value of the SourceDebugExtension attribute.
528 *
529 * JDB seems interested, but DEX files don't currently support this.
530 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700531static JdwpError RT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*)
532 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700533 /* referenceTypeId in, string out */
534 return ERR_ABSENT_INFORMATION;
535}
536
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700537static JdwpError RT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
538 bool with_generic)
539 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700540 RefTypeId refTypeId = ReadRefTypeId(&buf);
541
Elliott Hughes229feb72012-02-23 13:33:29 -0800542 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=%#llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800543 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800544
545 JdwpError status = Dbg::GetSignature(refTypeId, signature);
546 if (status != ERR_NONE) {
547 return status;
548 }
549 expandBufAddUtf8String(pReply, signature);
550 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800551 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700552 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700553 return ERR_NONE;
554}
555
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700556static JdwpError RT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
557 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700558 return RT_Signature(state, buf, dataLen, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800559}
560
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700561static JdwpError RT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
562 ExpandBuf* pReply)
563 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700564 return RT_Signature(state, buf, dataLen, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800565}
566
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700567/*
568 * Return the instance of java.lang.ClassLoader that loaded the specified
569 * reference type, or null if it was loaded by the system loader.
570 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700571static JdwpError RT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
572 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700573 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800574 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700575}
576
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700577static std::string Describe(const RefTypeId& refTypeId)
578 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800579 std::string signature("unknown");
580 Dbg::GetSignature(refTypeId, signature);
Elliott Hughes229feb72012-02-23 13:33:29 -0800581 return StringPrintf("refTypeId=%#llx (%s)", refTypeId, signature.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800582}
583
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700584/*
585 * Given a referenceTypeId, return a block of stuff that describes the
586 * fields declared by a class.
587 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700588static JdwpError RT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
589 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700590 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800591 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800592 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800593}
594
595// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700596static JdwpError RT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
597 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800598 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800599 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800600 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700601}
602
603/*
604 * Given a referenceTypeID, return a block of goodies describing the
605 * methods declared by a class.
606 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607static JdwpError RT_MethodsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
608 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700609 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800610 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800611 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800612}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700613
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800614// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615static JdwpError RT_Methods(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
616 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800617 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800618 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800619 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700620}
621
622/*
623 * Return the immediate superclass of a class.
624 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700625static JdwpError CT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
626 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700627 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800628 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700629 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800630 if (status != ERR_NONE) {
631 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800632 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700633 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700634 return ERR_NONE;
635}
636
637/*
638 * Set static class values.
639 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700640static JdwpError CT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*)
641 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700642 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700643 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700644
Elliott Hughes74847412012-06-20 18:10:21 -0700645 VLOG(jdwp) << StringPrintf(" Req to set %d values in class_id=%#llx", values, class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700646
647 for (uint32_t i = 0; i < values; i++) {
648 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800649 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800650 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700651 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700652
Elliott Hughes2435a572012-02-17 16:07:41 -0800653 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800654 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
655 if (status != ERR_NONE) {
656 return status;
657 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700658 }
659
660 return ERR_NONE;
661}
662
663/*
664 * Invoke a static method.
665 *
666 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
667 * values in the "variables" display.
668 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669static JdwpError CT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
670 ExpandBuf* pReply)
671 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700672 RefTypeId class_id = ReadRefTypeId(&buf);
673 ObjectId thread_id = ReadObjectId(&buf);
674 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700675
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700676 return FinishInvoke(state, buf, dataLen, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700677}
678
679/*
680 * Create a new object of the requested type, and invoke the specified
681 * constructor.
682 *
683 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
684 * see the contents of a byte[] as a string.
685 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700686static JdwpError CT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen,
687 ExpandBuf* pReply)
688 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700689 RefTypeId class_id = ReadRefTypeId(&buf);
690 ObjectId thread_id = ReadObjectId(&buf);
691 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700692
Elliott Hughes74847412012-06-20 18:10:21 -0700693 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(class_id);
694 ObjectId object_id;
695 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800696 if (status != ERR_NONE) {
697 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800698 }
Elliott Hughes74847412012-06-20 18:10:21 -0700699 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700700 return ERR_OUT_OF_MEMORY;
701 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700702 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700703}
704
705/*
706 * Create a new array object of the requested type and length.
707 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708static JdwpError AT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
709 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700710 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700711 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700712
Elliott Hughes2435a572012-02-17 16:07:41 -0800713 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes74847412012-06-20 18:10:21 -0700714 ObjectId object_id;
715 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800716 if (status != ERR_NONE) {
717 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800718 }
Elliott Hughes74847412012-06-20 18:10:21 -0700719 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700720 return ERR_OUT_OF_MEMORY;
721 }
722 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700723 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700724 return ERR_NONE;
725}
726
727/*
728 * Return line number information for the method, if present.
729 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730static JdwpError M_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
731 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700732 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700733 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700734
Elliott Hughes74847412012-06-20 18:10:21 -0700735 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId, method_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700736
Elliott Hughes74847412012-06-20 18:10:21 -0700737 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700738
739 return ERR_NONE;
740}
741
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700742static JdwpError M_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
743 bool generic)
744 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700745 RefTypeId class_id = ReadRefTypeId(&buf);
746 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700747
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700748 VLOG(jdwp) << StringPrintf(" Req for LocalVarTab in class=%s method=%s",
749 Dbg::GetClassName(class_id).c_str(),
750 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700751
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800752 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
753 // information. That will cause Eclipse to make a best-effort attempt at displaying local
754 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
755 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700756 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700757 return ERR_NONE;
758}
759
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700760static JdwpError M_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen,
761 ExpandBuf* pReply)
762 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700763 return M_VariableTable(state, buf, dataLen, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800764}
765
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766static JdwpError M_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
767 ExpandBuf* pReply)
768 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700769 return M_VariableTable(state, buf, dataLen, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800770}
771
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700772/*
773 * Given an object reference, return the runtime type of the object
774 * (class or array).
775 *
Elliott Hughes74847412012-06-20 18:10:21 -0700776 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700777 * passed in here.
778 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700779static JdwpError OR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
780 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700781 ObjectId object_id = ReadObjectId(&buf);
782 VLOG(jdwp) << StringPrintf(" Req for type of object_id=%#llx", object_id);
783 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700784}
785
786/*
787 * Get values from the fields of an object.
788 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700789static JdwpError OR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
790 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700791 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800792 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700793
Elliott Hughes74847412012-06-20 18:10:21 -0700794 VLOG(jdwp) << StringPrintf(" Req for %d fields from object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700795
Elliott Hughes0cf74332012-02-23 23:14:00 -0800796 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700797
Elliott Hughes0cf74332012-02-23 23:14:00 -0800798 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700799 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700800 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800801 if (status != ERR_NONE) {
802 return status;
803 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700804 }
805
806 return ERR_NONE;
807}
808
809/*
810 * Set values in the fields of an object.
811 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812static JdwpError OR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
813 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700814 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800815 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700816
Elliott Hughes74847412012-06-20 18:10:21 -0700817 VLOG(jdwp) << StringPrintf(" Req to set %d fields in object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700818
Elliott Hughes0cf74332012-02-23 23:14:00 -0800819 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700820 FieldId fieldId = ReadFieldId(&buf);
821
Elliott Hughesaed4be92011-12-02 16:16:23 -0800822 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800823 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700824 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700825
Elliott Hughes2435a572012-02-17 16:07:41 -0800826 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700827 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800828 if (status != ERR_NONE) {
829 return status;
830 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700831 }
832
833 return ERR_NONE;
834}
835
836/*
837 * Invoke an instance method. The invocation must occur in the specified
838 * thread, which must have been suspended by an event.
839 *
840 * The call is synchronous. All threads in the VM are resumed, unless the
841 * SINGLE_THREADED flag is set.
842 *
843 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
844 * object), it will try to invoke the object's toString() function. This
845 * feature becomes crucial when examining ArrayLists with Eclipse.
846 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700847static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
848 ExpandBuf* pReply)
849 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700850 ObjectId object_id = ReadObjectId(&buf);
851 ObjectId thread_id = ReadObjectId(&buf);
852 RefTypeId class_id = ReadRefTypeId(&buf);
853 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700854
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700855 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700856}
857
858/*
859 * Disable garbage collection of the specified object.
860 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700861static JdwpError OR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
862 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700863 // this is currently a no-op
864 return ERR_NONE;
865}
866
867/*
868 * Enable garbage collection of the specified object.
869 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700870static JdwpError OR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
871 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700872 // this is currently a no-op
873 return ERR_NONE;
874}
875
876/*
877 * Determine whether an object has been garbage collected.
878 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700879static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
880 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700881 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700882
Elliott Hughes74847412012-06-20 18:10:21 -0700883 object_id = ReadObjectId(&buf);
884 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700885
886 // TODO: currently returning false; must integrate with GC
887 expandBufAdd1(pReply, 0);
888
889 return ERR_NONE;
890}
891
892/*
893 * Return the string value in a string object.
894 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700895static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
896 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700897 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800898 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700899
Elliott Hughes82914b62012-04-09 15:56:29 -0700900 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700901
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800902 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700903
904 return ERR_NONE;
905}
906
907/*
908 * Return a thread's name.
909 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700910static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
911 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700912 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700913
Elliott Hughes74847412012-06-20 18:10:21 -0700914 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800915 std::string name;
Elliott Hughes74847412012-06-20 18:10:21 -0700916 if (!Dbg::GetThreadName(thread_id, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700917 return ERR_INVALID_THREAD;
918 }
Elliott Hughes74847412012-06-20 18:10:21 -0700919 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800920 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700921
922 return ERR_NONE;
923}
924
925/*
926 * Suspend the specified thread.
927 *
928 * It's supposed to remain suspended even if interpreted code wants to
929 * resume it; only the JDI is allowed to resume it.
930 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700931static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
932 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700933 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700934
Elliott Hughes74847412012-06-20 18:10:21 -0700935 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700936 LOG(INFO) << " Warning: ignoring request to suspend self";
937 return ERR_THREAD_NOT_SUSPENDED;
938 }
Elliott Hughes74847412012-06-20 18:10:21 -0700939 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940 Thread* self = Thread::Current();
941 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
942 JdwpError result = Dbg::SuspendThread(thread_id);
943 self->TransitionFromSuspendedToRunnable();
944 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700945}
946
947/*
948 * Resume the specified thread.
949 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700950static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
951 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700952 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700953
Elliott Hughes74847412012-06-20 18:10:21 -0700954 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700955 LOG(INFO) << " Warning: ignoring request to resume self";
956 return ERR_NONE;
957 }
Elliott Hughes74847412012-06-20 18:10:21 -0700958 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
959 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700960 return ERR_NONE;
961}
962
963/*
964 * Return status of specified thread.
965 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
967 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700968 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700969
Elliott Hughes74847412012-06-20 18:10:21 -0700970 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700971
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800972 JDWP::JdwpThreadStatus threadStatus;
973 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes74847412012-06-20 18:10:21 -0700974 if (!Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700975 return ERR_INVALID_THREAD;
976 }
977
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800978 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700979
980 expandBufAdd4BE(pReply, threadStatus);
981 expandBufAdd4BE(pReply, suspendStatus);
982
983 return ERR_NONE;
984}
985
986/*
987 * Return the thread group that the specified thread is a member of.
988 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
990 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700991 ObjectId thread_id = ReadObjectId(&buf);
992 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700993}
994
995/*
996 * Return the current call stack of a suspended thread.
997 *
998 * If the thread isn't suspended, the error code isn't defined, but should
999 * be THREAD_NOT_SUSPENDED.
1000 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001001static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1002 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001003 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001004 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001005 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001006
Elliott Hughes74847412012-06-20 18:10:21 -07001007 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001008 return ERR_INVALID_THREAD;
1009 }
Elliott Hughes74847412012-06-20 18:10:21 -07001010 if (!Dbg::IsSuspended(thread_id)) {
1011 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001012 return ERR_THREAD_NOT_SUSPENDED;
1013 }
1014
Elliott Hughes74847412012-06-20 18:10:21 -07001015 size_t actual_frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001016
Elliott Hughes74847412012-06-20 18:10:21 -07001017 VLOG(jdwp) << StringPrintf(" Request for frames: thread_id=%#llx start=%d length=%d [count=%zd]", thread_id, start_frame, length, actual_frame_count);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001018 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001019 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1020 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001021
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001022 if (start_frame > actual_frame_count) {
1023 return ERR_INVALID_INDEX;
1024 }
1025 if (length == static_cast<uint32_t>(-1)) {
1026 length = actual_frame_count - start_frame;
1027 }
1028 if (start_frame + length > actual_frame_count) {
1029 return ERR_INVALID_LENGTH;
1030 }
1031
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001032 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001033}
1034
1035/*
1036 * Returns the #of frames on the specified thread, which must be suspended.
1037 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1039 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001040 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001041
Elliott Hughes74847412012-06-20 18:10:21 -07001042 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001043 return ERR_INVALID_THREAD;
1044 }
Elliott Hughes74847412012-06-20 18:10:21 -07001045 if (!Dbg::IsSuspended(thread_id)) {
1046 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001047 return ERR_THREAD_NOT_SUSPENDED;
1048 }
1049
Elliott Hughes74847412012-06-20 18:10:21 -07001050 int frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001051 if (frame_count < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001052 return ERR_INVALID_THREAD;
1053 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001054 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001055
1056 return ERR_NONE;
1057}
1058
1059/*
1060 * Get the monitor that the thread is waiting on.
1061 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
1063 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001064 ReadObjectId(&buf); // thread_id
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001065
1066 // TODO: create an Object to represent the monitor (we're currently
1067 // just using a raw Monitor struct in the VM)
1068
1069 return ERR_NOT_IMPLEMENTED;
1070}
1071
1072/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001074 *
1075 * (The thread *might* still be running -- it might not have examined
1076 * its suspend count recently.)
1077 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078static JdwpError TR_DebugSuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1079 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001080 ObjectId thread_id = ReadObjectId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001082}
1083
1084/*
1085 * Return the name of a thread group.
1086 *
1087 * The Eclipse debugger recognizes "main" and "system" as special.
1088 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1090 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001091 ObjectId thread_group_id = ReadObjectId(&buf);
1092 VLOG(jdwp) << StringPrintf(" Req for name of thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001093
Elliott Hughescaf76542012-06-28 16:08:22 -07001094 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001095
1096 return ERR_NONE;
1097}
1098
1099/*
1100 * Returns the thread group -- if any -- that contains the specified
1101 * thread group.
1102 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1104 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001105 ObjectId thread_group_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001106
Elliott Hughescaf76542012-06-28 16:08:22 -07001107 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001108 expandBufAddObjectId(pReply, parentGroup);
1109
1110 return ERR_NONE;
1111}
1112
1113/*
1114 * Return the active threads and thread groups that are part of the
1115 * specified thread group.
1116 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001117static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1118 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001119 ObjectId thread_group_id = ReadObjectId(&buf);
1120 VLOG(jdwp) << StringPrintf(" Req for threads in thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001121
Elliott Hughescaf76542012-06-28 16:08:22 -07001122 std::vector<ObjectId> thread_ids;
1123 Dbg::GetThreads(thread_group_id, thread_ids);
1124 expandBufAdd4BE(pReply, thread_ids.size());
1125 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1126 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001127 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001128
Elliott Hughescaf76542012-06-28 16:08:22 -07001129 std::vector<ObjectId> child_thread_groups_ids;
1130 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1131 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1132 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1133 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001134 }
1135
1136 return ERR_NONE;
1137}
1138
1139/*
1140 * Return the #of components in the array.
1141 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1143 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001144 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001145 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001146
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001147 int length;
1148 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1149 if (status != ERR_NONE) {
1150 return status;
1151 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001152 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001153
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001154 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001155
1156 return ERR_NONE;
1157}
1158
1159/*
1160 * Return the values from an array.
1161 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1163 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001164 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001165 uint32_t firstIndex = Read4BE(&buf);
1166 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001167 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001168
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001169 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001170}
1171
1172/*
1173 * Set values in an array.
1174 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001175static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
1176 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001177 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001178 uint32_t firstIndex = Read4BE(&buf);
1179 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001180
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001181 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId,
1182 firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001183
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001184 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001185}
1186
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001187static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1188 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001189 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001190 // TODO: we should only return classes which have the given class loader as a defining or
1191 // initiating loader. The former would be easy; the latter is hard, because we don't have
1192 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001193 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001194}
1195
1196/*
1197 * Set an event trigger.
1198 *
1199 * Reply with a requestID.
1200 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001201static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
1202 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001203 const uint8_t* origBuf = buf;
1204
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001205 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001206 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001207 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001208
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001209 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001210 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001211 << " mods=" << modifierCount << ")";
1212
1213 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1214
1215 JdwpEvent* pEvent = EventAlloc(modifierCount);
1216 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001217 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001218 pEvent->modCount = modifierCount;
1219
1220 /*
1221 * Read modifiers. Ordering may be significant (see explanation of Count
1222 * mods in JDWP doc).
1223 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001224 for (uint32_t i = 0; i < modifierCount; ++i) {
1225 JdwpEventMod& mod = pEvent->mods[i];
1226 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1227 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001228 case MK_COUNT: /* report once, when "--count" reaches 0 */
1229 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001230 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001231 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001232 if (count == 0) {
1233 return ERR_INVALID_COUNT;
1234 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001235 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001236 }
1237 break;
1238 case MK_CONDITIONAL: /* conditional on expression) */
1239 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001240 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001241 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001242 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001243 }
1244 break;
1245 case MK_THREAD_ONLY: /* only report events in specified thread */
1246 {
Elliott Hughes74847412012-06-20 18:10:21 -07001247 ObjectId thread_id = ReadObjectId(&buf);
1248 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1249 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001250 }
1251 break;
1252 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1253 {
Elliott Hughes74847412012-06-20 18:10:21 -07001254 RefTypeId class_id = ReadRefTypeId(&buf);
1255 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1256 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001257 }
1258 break;
1259 case MK_CLASS_MATCH: /* restrict events to matching classes */
1260 {
Elliott Hughes86964332012-02-15 19:37:42 -08001261 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001262 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001263 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001264 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001265 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001266 }
1267 break;
1268 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1269 {
Elliott Hughes86964332012-02-15 19:37:42 -08001270 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001271 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001272 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001273 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001274 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001275 }
1276 break;
1277 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1278 {
1279 JdwpLocation loc;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001280 JdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001281 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001282 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001283 }
1284 break;
1285 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1286 {
1287 RefTypeId exceptionOrNull; /* null == all exceptions */
1288 uint8_t caught, uncaught;
1289
1290 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001291 caught = Read1(&buf);
1292 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001293 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001294 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001295
Elliott Hughes972a47b2012-02-21 18:16:06 -08001296 mod.exceptionOnly.refTypeId = exceptionOrNull;
1297 mod.exceptionOnly.caught = caught;
1298 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001299 }
1300 break;
1301 case MK_FIELD_ONLY: /* for field access/mod events */
1302 {
1303 RefTypeId declaring = ReadRefTypeId(&buf);
1304 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001305 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001306 mod.fieldOnly.refTypeId = declaring;
1307 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001308 }
1309 break;
1310 case MK_STEP: /* for use with EK_SINGLE_STEP */
1311 {
Elliott Hughes74847412012-06-20 18:10:21 -07001312 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001313 uint32_t size, depth;
1314
Elliott Hughes74847412012-06-20 18:10:21 -07001315 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001316 size = Read4BE(&buf);
1317 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001318 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001319 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1320
Elliott Hughes74847412012-06-20 18:10:21 -07001321 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001322 mod.step.size = size;
1323 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001324 }
1325 break;
1326 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1327 {
1328 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001329 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001330 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001331 }
1332 break;
1333 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001334 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001335 break;
1336 }
1337 }
1338
1339 /*
1340 * Make sure we consumed all data. It is possible that the remote side
1341 * has sent us bad stuff, but for now we blame ourselves.
1342 */
1343 if (buf != origBuf + dataLen) {
1344 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1345 }
1346
1347 /*
1348 * We reply with an integer "requestID".
1349 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001350 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001351 expandBufAdd4BE(pReply, requestId);
1352
1353 pEvent->requestId = requestId;
1354
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001355 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001356
1357 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001358 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001359 if (err != ERR_NONE) {
1360 /* registration failed, probably because event is bogus */
1361 EventFree(pEvent);
1362 LOG(WARNING) << "WARNING: event request rejected";
1363 }
1364 return err;
1365}
1366
1367/*
1368 * Clear an event. Failure to find an event with a matching ID is a no-op
1369 * and does not return an error.
1370 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001371static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
1372 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001373 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001374 eventKind = Read1(&buf);
1375 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001376
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001377 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001378
Elliott Hughes761928d2011-11-16 18:33:03 -08001379 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001380
1381 return ERR_NONE;
1382}
1383
1384/*
1385 * Return the values of arguments and local variables.
1386 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001387static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1388 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001389 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001390 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001391 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001392
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001393 VLOG(jdwp) << StringPrintf(" Req for %d slots in thread_id=%#llx frame_id=%lld", slots, thread_id, frame_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001394
1395 expandBufAdd4BE(pReply, slots); /* "int values" */
1396 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001397 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001398 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001399
Elliott Hughes2435a572012-02-17 16:07:41 -08001400 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001401
Elliott Hughesdbb40792011-11-18 17:05:22 -08001402 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001403 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001404 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001405 }
1406
1407 return ERR_NONE;
1408}
1409
1410/*
1411 * Set the values of arguments and local variables.
1412 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001413static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
1414 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001415 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001416 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001417 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001418
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001419 VLOG(jdwp) << StringPrintf(" Req to set %d slots in thread_id=%#llx frame_id=%lld", slots, thread_id, frame_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001420
1421 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001422 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001423 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001424 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001425 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001426
Elliott Hughes2435a572012-02-17 16:07:41 -08001427 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001428 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001429 }
1430
1431 return ERR_NONE;
1432}
1433
1434/*
1435 * Returns the value of "this" for the specified frame.
1436 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001437static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1438 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001439 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001440 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001441
Elliott Hughes546b9862012-06-20 16:06:13 -07001442 ObjectId id;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001443 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &id);
1444 if (rc != ERR_NONE) {
1445 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001446 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001447
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001448 uint8_t tag;
1449 rc = Dbg::GetObjectTag(id, tag);
1450 if (rc != ERR_NONE) {
1451 return rc;
1452 }
1453
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001454 VLOG(jdwp) << StringPrintf(" Req for 'this' in thread_id=%#llx frame=%lld --> %#llx '%c'",
1455 thread_id, frame_id, id, static_cast<char>(tag));
Elliott Hughes546b9862012-06-20 16:06:13 -07001456 expandBufAdd1(pReply, tag);
1457 expandBufAddObjectId(pReply, id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001458
1459 return ERR_NONE;
1460}
1461
1462/*
1463 * Return the reference type reflected by this class object.
1464 *
1465 * This appears to be required because ReferenceTypeId values are NEVER
1466 * reused, whereas ClassIds can be recycled like any other object. (Either
1467 * that, or I have no idea what this is for.)
1468 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001469static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
1470 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001471 RefTypeId classObjectId = ReadRefTypeId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001472 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId,
1473 Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001474 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001475}
1476
1477/*
1478 * Handle a DDM packet with a single chunk in it.
1479 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001480static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
1481 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001482 uint8_t* replyBuf = NULL;
1483 int replyLen = -1;
1484
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001485 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001486
Elliott Hughesa21039c2012-06-21 12:09:25 -07001487 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001488
1489 /*
1490 * If they want to send something back, we copy it into the buffer.
1491 * A no-copy approach would be nicer.
1492 *
1493 * TODO: consider altering the JDWP stuff to hold the packet header
1494 * in a separate buffer. That would allow us to writev() DDM traffic
1495 * instead of copying it into the expanding buffer. The reduction in
1496 * heap requirements is probably more valuable than the efficiency.
1497 */
1498 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1499 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1500 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1501 free(replyBuf);
1502 }
1503 return ERR_NONE;
1504}
1505
1506/*
1507 * Handler map decl.
1508 */
1509typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1510
1511struct JdwpHandlerMap {
1512 uint8_t cmdSet;
1513 uint8_t cmd;
1514 JdwpRequestHandler func;
1515 const char* descr;
1516};
1517
1518/*
1519 * Map commands to functions.
1520 *
1521 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1522 * and 128-256 are vendor-defined.
1523 */
1524static const JdwpHandlerMap gHandlerMap[] = {
1525 /* VirtualMachine command set (1) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001526 { 1, 1, VM_Version, "VirtualMachine.Version" },
1527 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1528 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1529 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1530 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1531 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1532 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1533 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1534 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1535 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1536 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1537 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1538 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1539 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001540 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1541 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001542 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001543 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1544 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001545 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001546 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001547
1548 /* ReferenceType command set (2) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001549 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1550 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1551 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1552 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1553 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1554 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1555 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001556 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001557 { 2, 9, RT_Status, "ReferenceType.Status" },
1558 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1559 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
1560 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1561 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1562 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1563 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001564 { 2, 16, NULL, "ReferenceType.Instances" },
1565 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1566 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001567
1568 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001569 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1570 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1571 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1572 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001573
1574 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001575 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001576
1577 /* InterfaceType command set (5) */
1578
1579 /* Method command set (6) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001580 { 6, 1, M_LineTable, "Method.LineTable" },
1581 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001582 { 6, 3, NULL, "Method.Bytecodes" },
1583 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001584 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001585
1586 /* Field command set (8) */
1587
1588 /* ObjectReference command set (9) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001589 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1590 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1591 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001592 { 9, 4, NULL, "ObjectReference.UNUSED" },
1593 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001594 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
1595 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
1596 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1597 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001598 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001599
1600 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001601 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001602
1603 /* ThreadReference command set (11) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001604 { 11, 1, TR_Name, "ThreadReference.Name" },
1605 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1606 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1607 { 11, 4, TR_Status, "ThreadReference.Status" },
1608 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1609 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1610 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001611 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001612 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001613 { 11, 10, NULL, "ThreadReference.Stop" },
Elliott Hughes74847412012-06-20 18:10:21 -07001614 { 11, 11, NULL, "ThreadReference.Interrupt" },
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001615 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001616 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1617 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001618
1619 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001620 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1621 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1622 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001623
1624 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001625 { 13, 1, AR_Length, "ArrayReference.Length" },
1626 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1627 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001628
1629 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001630 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001631
1632 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001633 { 15, 1, ER_Set, "EventRequest.Set" },
1634 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001635 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001636
1637 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001638 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1639 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1640 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001641 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001642
1643 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001644 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001645
1646 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001647 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001648
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001649 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001650};
1651
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001652static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001653 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001654 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1655 return gHandlerMap[i].descr;
1656 }
1657 }
1658 return "?UNKNOWN?";
1659}
1660
1661static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1662 std::string result;
1663 result += "REQ: ";
1664 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1665 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1666 return result;
1667}
1668
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001669/*
1670 * Process a request from the debugger.
1671 *
1672 * On entry, the JDWP thread is in VMWAIT.
1673 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001674void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001675 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001676
1677 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1678 /*
1679 * Activity from a debugger, not merely ddms. Mark us as having an
1680 * active debugger session, and zero out the last-activity timestamp
1681 * so waitForDebugger() doesn't return if we stall for a bit here.
1682 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001683 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001684 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001685 }
1686
1687 /*
1688 * If a debugger event has fired in another thread, wait until the
1689 * initiating thread has suspended itself before processing messages
1690 * from the debugger. Otherwise we (the JDWP thread) could be told to
1691 * resume the thread before it has suspended.
1692 *
1693 * We call with an argument of zero to wait for the current event
1694 * thread to finish, and then clear the block. Depending on the thread
1695 * suspend policy, this may allow events in other threads to fire,
1696 * but those events have no bearing on what the debugger has sent us
1697 * in the current request.
1698 *
1699 * Note that we MUST clear the event token before waking the event
1700 * thread up, or risk waiting for the thread to suspend after we've
1701 * told it to resume.
1702 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001703 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001704
1705 /*
1706 * Tell the VM that we're running and shouldn't be interrupted by GC.
1707 * Do this after anything that can stall indefinitely.
1708 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001709 Thread* self = Thread::Current();
1710 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001711
1712 expandBufAddSpace(pReply, kJDWPHeaderLen);
1713
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001714 size_t i;
1715 for (i = 0; i < arraysize(gHandlerMap); i++) {
1716 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001717 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001718 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001719 break;
1720 }
1721 }
1722 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001723 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1724 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001725 result = ERR_NOT_IMPLEMENTED;
1726 }
1727
1728 /*
1729 * Set up the reply header.
1730 *
1731 * If we encountered an error, only send the header back.
1732 */
1733 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001734 Set4BE(replyBuf + 4, pHeader->id);
1735 Set1(replyBuf + 8, kJDWPFlagReply);
1736 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001737 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001738 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001739 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001740 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001741 }
1742
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001743 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001744 if (false) {
1745 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001746 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001747 }
1748
1749 /*
1750 * Update last-activity timestamp. We really only need this during
1751 * the initial setup. Only update if this is a non-DDMS packet.
1752 */
1753 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001754 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001755 }
1756
1757 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001758 self->TransitionFromRunnableToSuspended(old_state);
1759
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001760}
1761
1762} // namespace JDWP
1763
1764} // namespace art