blob: 14dd2a558b4a1ed583144c3ffdb410816dd2740e [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
Elliott Hughes872d4ec2011-10-21 17:07:15 -070028#include "jdwp/jdwp_handler.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070029
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
Elliott Hughes07ed66b2012-12-12 18:34:25 -080034#include "atomic.h"
35#include "base/logging.h"
36#include "base/macros.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080037#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080038#include "debugger.h"
39#include "jdwp/jdwp_constants.h"
40#include "jdwp/jdwp_event.h"
41#include "jdwp/jdwp_expand_buf.h"
42#include "jdwp/jdwp_priv.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080043
Elliott Hughes872d4ec2011-10-21 17:07:15 -070044namespace art {
45
46namespace JDWP {
47
48/*
49 * Helper function: read a "location" from an input buffer.
50 */
Elliott Hughes4993bbc2013-01-10 15:41:25 -080051static void ReadLocation(const uint8_t** pBuf, JdwpLocation* pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070052 memset(pLoc, 0, sizeof(*pLoc)); /* allows memcmp() later */
Elliott Hughes74847412012-06-20 18:10:21 -070053 pLoc->type_tag = ReadTypeTag(pBuf);
54 pLoc->class_id = ReadObjectId(pBuf);
55 pLoc->method_id = ReadMethodId(pBuf);
Elliott Hughes972a47b2012-02-21 18:16:06 -080056 pLoc->dex_pc = Read8BE(pBuf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070057}
58
59/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -070060 * Helper function: read a variable-width value from the input buffer.
61 */
Elliott Hughes4993bbc2013-01-10 15:41:25 -080062static uint64_t ReadValue(const uint8_t** pBuf, size_t width) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070063 uint64_t value = -1;
64 switch (width) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070065 case 1: value = Read1(pBuf); break;
66 case 2: value = Read2BE(pBuf); break;
67 case 4: value = Read4BE(pBuf); break;
68 case 8: value = Read8BE(pBuf); break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070069 default: LOG(FATAL) << width; break;
70 }
71 return value;
72}
73
74/*
75 * Helper function: write a variable-width value into the output input buffer.
76 */
Elliott Hughes4993bbc2013-01-10 15:41:25 -080077static void WriteValue(ExpandBuf* pReply, int width, uint64_t value) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070078 switch (width) {
79 case 1: expandBufAdd1(pReply, value); break;
80 case 2: expandBufAdd2BE(pReply, value); break;
81 case 4: expandBufAdd4BE(pReply, value); break;
82 case 8: expandBufAdd8BE(pReply, value); break;
83 default: LOG(FATAL) << width; break;
84 }
85}
86
Elliott Hughes4993bbc2013-01-10 15:41:25 -080087static JdwpError WriteTaggedObject(ExpandBuf* reply, ObjectId object_id)
88 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
89 uint8_t tag;
90 JdwpError rc = Dbg::GetObjectTag(object_id, tag);
91 if (rc == ERR_NONE) {
92 expandBufAdd1(reply, tag);
93 expandBufAddObjectId(reply, object_id);
94 }
95 return rc;
96}
97
Elliott Hughes0cbaff52013-01-16 15:28:01 -080098static JdwpError WriteTaggedObjectList(ExpandBuf* reply, const std::vector<ObjectId>& objects)
99 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
100 expandBufAdd4BE(reply, objects.size());
101 for (size_t i = 0; i < objects.size(); ++i) {
102 JdwpError rc = WriteTaggedObject(reply, objects[i]);
103 if (rc != ERR_NONE) {
104 return rc;
105 }
106 }
107 return ERR_NONE;
108}
109
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700110/*
111 * Common code for *_InvokeMethod requests.
112 *
Elliott Hughes74847412012-06-20 18:10:21 -0700113 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700114 * expected-to-be-void return value of the called function.
115 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700116static JdwpError FinishInvoke(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
Elliott Hughes74847412012-06-20 18:10:21 -0700117 ObjectId thread_id, ObjectId object_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118 RefTypeId class_id, MethodId method_id, bool is_constructor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700120 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121
Elliott Hughes45651fd2012-02-21 15:48:20 -0800122 uint32_t arg_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123
Elliott Hughes74847412012-06-20 18:10:21 -0700124 VLOG(jdwp) << StringPrintf(" --> thread_id=%#llx object_id=%#llx", thread_id, object_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 VLOG(jdwp) << StringPrintf(" class_id=%#llx method_id=%x %s.%s", class_id,
126 method_id, Dbg::GetClassName(class_id).c_str(),
127 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800128 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700129
Elliott Hughes45651fd2012-02-21 15:48:20 -0800130 UniquePtr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
131 UniquePtr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
132 for (uint32_t i = 0; i < arg_count; ++i) {
133 argTypes[i] = ReadTag(&buf);
134 size_t width = Dbg::GetTagWidth(argTypes[i]);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800135 argValues[i] = ReadValue(&buf, width);
Elliott Hughes229feb72012-02-23 13:33:29 -0800136 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#llx", width, argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700137 }
138
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700139 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
141 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
142 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700143
Elliott Hughes45651fd2012-02-21 15:48:20 -0800144 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700145 uint64_t resultValue;
146 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700147 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 -0700148 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800149 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700150 }
151
152 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800153 if (is_constructor) {
154 // If we invoked a constructor (which actually returns void), return the receiver,
155 // unless we threw, in which case we return NULL.
156 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700157 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800158 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700159
Elliott Hughes45651fd2012-02-21 15:48:20 -0800160 size_t width = Dbg::GetTagWidth(resultTag);
161 expandBufAdd1(pReply, resultTag);
162 if (width != 0) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800163 WriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700164 }
165 expandBufAdd1(pReply, JT_OBJECT);
166 expandBufAddObjectId(pReply, exceptObjId);
167
Elliott Hughes229feb72012-02-23 13:33:29 -0800168 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" %#llx (except=%#llx)", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169
170 /* show detailed debug output */
171 if (resultTag == JT_STRING && exceptObjId == 0) {
172 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800173 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700174 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800175 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176 }
177 }
178 }
179
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700180 return err;
181}
182
183
184/*
185 * Request for version info.
186 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700187static JdwpError VM_Version(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189 /* text information on runtime version */
190 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800191 expandBufAddUtf8String(pReply, version);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700192 /* JDWP version numbers */
193 expandBufAdd4BE(pReply, 1); // major
194 expandBufAdd4BE(pReply, 5); // minor
195 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800196 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700197 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800198 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199
200 return ERR_NONE;
201}
202
203/*
204 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
205 * referenceTypeID. We need to send back more than one if the class has
206 * been loaded by multiple class loaders.
207 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700208static JdwpError VM_ClassesBySignature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800210 std::string classDescriptor(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800211 VLOG(jdwp) << " Req for class by signature '" << classDescriptor << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700212
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800213 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800214 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700215
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800216 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700217
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800218 for (size_t i = 0; i < ids.size(); ++i) {
219 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800220 JDWP::JdwpTypeTag type_tag;
221 uint32_t class_status;
222 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
223 if (status != ERR_NONE) {
224 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800225 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226
Elliott Hughes436e3722012-02-17 20:01:47 -0800227 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800228 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800229 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700230 }
231
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700232 return ERR_NONE;
233}
234
235/*
236 * Handle request for the thread IDs of all running threads.
237 *
238 * We exclude ourselves from the list, because we don't allow ourselves
239 * to be suspended, and that violates some JDWP expectations.
240 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700241static JdwpError VM_AllThreads(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700243 std::vector<ObjectId> thread_ids;
Elliott Hughes026b1462012-06-28 20:43:49 -0700244 Dbg::GetThreads(0, thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700245
Elliott Hughescaf76542012-06-28 16:08:22 -0700246 expandBufAdd4BE(pReply, thread_ids.size());
247 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
248 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700249 }
250
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700251 return ERR_NONE;
252}
253
254/*
255 * List all thread groups that do not have a parent.
256 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257static JdwpError VM_TopLevelThreadGroups(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700259 /*
260 * TODO: maintain a list of parentless thread groups in the VM.
261 *
262 * For now, just return "system". Application threads are created
263 * in "main", which is a child of "system".
264 */
265 uint32_t groups = 1;
266 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700267 //thread_group_id = debugGetMainThreadGroup();
268 //expandBufAdd8BE(pReply, thread_group_id);
269 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
270 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700271
272 return ERR_NONE;
273}
274
275/*
276 * Respond with the sizes of the basic debugger types.
277 *
278 * All IDs are 8 bytes.
279 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280static JdwpError VM_IDSizes(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700281 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700282 expandBufAdd4BE(pReply, sizeof(FieldId));
283 expandBufAdd4BE(pReply, sizeof(MethodId));
284 expandBufAdd4BE(pReply, sizeof(ObjectId));
285 expandBufAdd4BE(pReply, sizeof(RefTypeId));
286 expandBufAdd4BE(pReply, sizeof(FrameId));
287 return ERR_NONE;
288}
289
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290static JdwpError VM_Dispose(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700291 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes86964332012-02-15 19:37:42 -0800292 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700293 return ERR_NONE;
294}
295
296/*
297 * Suspend the execution of the application running in the VM (i.e. suspend
298 * all threads).
299 *
300 * This needs to increment the "suspend count" on all threads.
301 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302static JdwpError VM_Suspend(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700303 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800304 Thread* self = Thread::Current();
305 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700306 Dbg::SuspendVM();
jeffhaoa77f0f62012-12-05 17:19:31 -0800307 self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308 return ERR_NONE;
309}
310
311/*
312 * Resume execution. Decrements the "suspend count" of all threads.
313 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314static JdwpError VM_Resume(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700315 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700316 Dbg::ResumeVM();
317 return ERR_NONE;
318}
319
320/*
321 * The debugger wants the entire VM to exit.
322 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700323static JdwpError VM_Exit(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700324 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700325 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700326
327 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
328
329 Dbg::Exit(exitCode);
330 return ERR_NOT_IMPLEMENTED; // shouldn't get here
331}
332
333/*
334 * Create a new string in the VM and return its ID.
335 *
336 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
337 * string "java.util.Arrays".)
338 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339static JdwpError VM_CreateString(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700340 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800341 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800342 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700343 ObjectId stringId = Dbg::CreateString(str);
344 if (stringId == 0) {
345 return ERR_OUT_OF_MEMORY;
346 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700347 expandBufAddObjectId(pReply, stringId);
348 return ERR_NONE;
349}
350
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700351static JdwpError VM_ClassPaths(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700352 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800353 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700354
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800355 std::vector<std::string> class_path;
356 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
357 expandBufAdd4BE(pReply, class_path.size());
358 for (size_t i = 0; i < class_path.size(); ++i) {
359 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700360 }
361
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800362 std::vector<std::string> boot_class_path;
363 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
364 expandBufAdd4BE(pReply, boot_class_path.size());
365 for (size_t i = 0; i < boot_class_path.size(); ++i) {
366 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700367 }
368
369 return ERR_NONE;
370}
371
372/*
373 * Release a list of object IDs. (Seen in jdb.)
374 *
375 * Currently does nothing.
376 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377static JdwpError VM_DisposeObjects(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700378 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700379 return ERR_NONE;
380}
381
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800382static JdwpError VM_Capabilities(JdwpState*, const uint8_t*, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800384 expandBufAdd1(reply, false); // canWatchFieldModification
385 expandBufAdd1(reply, false); // canWatchFieldAccess
Elliott Hughes9777ba22013-01-17 09:04:19 -0800386 expandBufAdd1(reply, true); // canGetBytecodes
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800387 expandBufAdd1(reply, true); // canGetSyntheticAttribute
388 expandBufAdd1(reply, true); // canGetOwnedMonitorInfo
Elliott Hughesf9501702013-01-11 11:22:27 -0800389 expandBufAdd1(reply, true); // canGetCurrentContendedMonitor
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800390 expandBufAdd1(reply, true); // canGetMonitorInfo
391 return ERR_NONE;
392}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700393
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800394static JdwpError VM_CapabilitiesNew(JdwpState*, const uint8_t*, int, ExpandBuf* reply)
395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
396
397 // The first few capabilities are the same as those reported by the older call.
398 VM_Capabilities(NULL, NULL, 0, reply);
399
400 expandBufAdd1(reply, false); // canRedefineClasses
401 expandBufAdd1(reply, false); // canAddMethod
402 expandBufAdd1(reply, false); // canUnrestrictedlyRedefineClasses
403 expandBufAdd1(reply, false); // canPopFrames
404 expandBufAdd1(reply, false); // canUseInstanceFilters
405 expandBufAdd1(reply, false); // canGetSourceDebugExtension
406 expandBufAdd1(reply, false); // canRequestVMDeathEvent
407 expandBufAdd1(reply, false); // canSetDefaultStratum
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800408 expandBufAdd1(reply, true); // 1.6: canGetInstanceInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800409 expandBufAdd1(reply, false); // 1.6: canRequestMonitorEvents
Elliott Hughes734b8c62013-01-11 15:32:45 -0800410 expandBufAdd1(reply, true); // 1.6: canGetMonitorFrameInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800411 expandBufAdd1(reply, false); // 1.6: canUseSourceNameFilters
412 expandBufAdd1(reply, false); // 1.6: canGetConstantPool
413 expandBufAdd1(reply, false); // 1.6: canForceEarlyReturn
414
415 // Fill in reserved22 through reserved32; note count started at 1.
416 for (size_t i = 22; i <= 32; ++i) {
417 expandBufAdd1(reply, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700418 }
419 return ERR_NONE;
420}
421
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700422static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700423 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800424 std::vector<JDWP::RefTypeId> classes;
425 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700426
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800427 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700428
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800429 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800430 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800431 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800432 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800433 uint32_t class_status;
434 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
435 if (status != ERR_NONE) {
436 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800437 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700438
Elliott Hughes436e3722012-02-17 20:01:47 -0800439 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800440 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800441 if (descriptor_and_status) {
442 expandBufAddUtf8String(pReply, descriptor);
443 if (generic) {
444 expandBufAddUtf8String(pReply, genericSignature);
445 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800446 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800447 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700448 }
449
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700450 return ERR_NONE;
451}
452
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700453static JdwpError VM_AllClasses(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700454 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700455 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800456}
457
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700458static JdwpError VM_AllClassesWithGeneric(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700460 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800461}
462
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800463static JdwpError VM_InstanceCounts(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
464 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
465 int32_t class_count = static_cast<int32_t>(Read4BE(&buf));
466 if (class_count < 0) {
467 return ERR_ILLEGAL_ARGUMENT;
468 }
469 std::vector<RefTypeId> class_ids;
470 for (int32_t i = 0; i < class_count; ++i) {
471 class_ids.push_back(ReadRefTypeId(&buf));
472 }
473
474 std::vector<uint64_t> counts;
475 JdwpError rc = Dbg::GetInstanceCounts(class_ids, counts);
476 if (rc != ERR_NONE) {
477 return rc;
478 }
479
480 expandBufAdd4BE(pReply, counts.size());
481 for (size_t i = 0; i < counts.size(); ++i) {
482 expandBufAdd8BE(pReply, counts[i]);
483 }
484 return ERR_NONE;
485}
486
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700487static JdwpError RT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700488 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700489 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800490 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700491}
492
493/*
494 * Get values from static fields in a reference type.
495 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700496static JdwpError RT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700497 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800498 RefTypeId refTypeId = ReadRefTypeId(&buf);
499 uint32_t field_count = Read4BE(&buf);
500 expandBufAdd4BE(pReply, field_count);
501 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700502 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800503 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800504 if (status != ERR_NONE) {
505 return status;
506 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700507 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700508 return ERR_NONE;
509}
510
511/*
512 * Get the name of the source file in which a reference type was declared.
513 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700514static JdwpError RT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700515 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700516 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800517 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800518 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
519 if (status != ERR_NONE) {
520 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700521 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800522 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800523 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700524}
525
526/*
527 * Return the current status of the reference type.
528 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700529static JdwpError RT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700530 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700531 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800532 JDWP::JdwpTypeTag type_tag;
533 uint32_t class_status;
534 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
535 if (status != ERR_NONE) {
536 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800537 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800538 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700539 return ERR_NONE;
540}
541
542/*
543 * Return interfaces implemented directly by this class.
544 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700545static JdwpError RT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700546 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700547 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800548 VLOG(jdwp) << StringPrintf(" Req for interfaces in %#llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800549 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700550}
551
552/*
553 * Return the class object corresponding to this type.
554 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700555static JdwpError RT_ClassObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700556 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700557 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800558 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800559 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
560 if (status != ERR_NONE) {
561 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800562 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800563 VLOG(jdwp) << StringPrintf(" RefTypeId %#llx -> ObjectId %#llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800564 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700565 return ERR_NONE;
566}
567
568/*
569 * Returns the value of the SourceDebugExtension attribute.
570 *
571 * JDB seems interested, but DEX files don't currently support this.
572 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700573static JdwpError RT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700574 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700575 /* referenceTypeId in, string out */
576 return ERR_ABSENT_INFORMATION;
577}
578
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700579static JdwpError RT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
580 bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700581 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700582 RefTypeId refTypeId = ReadRefTypeId(&buf);
583
Elliott Hughes229feb72012-02-23 13:33:29 -0800584 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=%#llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800585 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800586
587 JdwpError status = Dbg::GetSignature(refTypeId, signature);
588 if (status != ERR_NONE) {
589 return status;
590 }
591 expandBufAddUtf8String(pReply, signature);
592 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800593 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700594 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700595 return ERR_NONE;
596}
597
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598static JdwpError RT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700599 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700600 return RT_Signature(state, buf, dataLen, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800601}
602
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700603static JdwpError RT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
604 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700605 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700606 return RT_Signature(state, buf, dataLen, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800607}
608
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700609/*
610 * Return the instance of java.lang.ClassLoader that loaded the specified
611 * reference type, or null if it was loaded by the system loader.
612 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613static JdwpError RT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700614 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700615 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800616 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617}
618
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619static std::string Describe(const RefTypeId& refTypeId)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700620 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800621 std::string signature("unknown");
622 Dbg::GetSignature(refTypeId, signature);
Elliott Hughes229feb72012-02-23 13:33:29 -0800623 return StringPrintf("refTypeId=%#llx (%s)", refTypeId, signature.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800624}
625
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700626/*
627 * Given a referenceTypeId, return a block of stuff that describes the
628 * fields declared by a class.
629 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700630static JdwpError RT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700631 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700632 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800633 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800634 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800635}
636
637// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638static JdwpError RT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700639 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800640 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800641 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800642 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700643}
644
645/*
646 * Given a referenceTypeID, return a block of goodies describing the
647 * methods declared by a class.
648 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700649static JdwpError RT_MethodsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700650 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700651 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800652 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800653 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800654}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700655
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800656// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657static JdwpError RT_Methods(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700658 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800659 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800660 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800661 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700662}
663
Elliott Hughes3b78c942013-01-15 17:35:41 -0800664static JdwpError RT_Instances(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
665 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
666 RefTypeId class_id = ReadRefTypeId(&buf);
667 int32_t max_count = Read4BE(&buf);
668 if (max_count < 0) {
669 return ERR_ILLEGAL_ARGUMENT;
670 }
671
672 std::vector<ObjectId> instances;
673 JdwpError rc = Dbg::GetInstances(class_id, max_count, instances);
674 if (rc != ERR_NONE) {
675 return rc;
676 }
677
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800678 return WriteTaggedObjectList(reply, instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800679}
680
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700681/*
682 * Return the immediate superclass of a class.
683 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700684static JdwpError CT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700685 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700686 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800687 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700688 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800689 if (status != ERR_NONE) {
690 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800691 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700692 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700693 return ERR_NONE;
694}
695
696/*
697 * Set static class values.
698 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700699static JdwpError CT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700700 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700701 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700702 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700703
Elliott Hughes74847412012-06-20 18:10:21 -0700704 VLOG(jdwp) << StringPrintf(" Req to set %d values in class_id=%#llx", values, class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700705
706 for (uint32_t i = 0; i < values; i++) {
707 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800708 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800709 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800710 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700711
Elliott Hughes2435a572012-02-17 16:07:41 -0800712 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800713 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
714 if (status != ERR_NONE) {
715 return status;
716 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700717 }
718
719 return ERR_NONE;
720}
721
722/*
723 * Invoke a static method.
724 *
725 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
726 * values in the "variables" display.
727 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728static JdwpError CT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
729 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700730 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700731 RefTypeId class_id = ReadRefTypeId(&buf);
732 ObjectId thread_id = ReadObjectId(&buf);
733 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700734
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700735 return FinishInvoke(state, buf, dataLen, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700736}
737
738/*
739 * Create a new object of the requested type, and invoke the specified
740 * constructor.
741 *
742 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
743 * see the contents of a byte[] as a string.
744 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700745static JdwpError CT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen,
746 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700747 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700748 RefTypeId class_id = ReadRefTypeId(&buf);
749 ObjectId thread_id = ReadObjectId(&buf);
750 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700751
Elliott Hughes74847412012-06-20 18:10:21 -0700752 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(class_id);
753 ObjectId object_id;
754 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800755 if (status != ERR_NONE) {
756 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800757 }
Elliott Hughes74847412012-06-20 18:10:21 -0700758 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700759 return ERR_OUT_OF_MEMORY;
760 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700761 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700762}
763
764/*
765 * Create a new array object of the requested type and length.
766 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700767static JdwpError AT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700768 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700769 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700770 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700771
Elliott Hughes2435a572012-02-17 16:07:41 -0800772 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes74847412012-06-20 18:10:21 -0700773 ObjectId object_id;
774 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800775 if (status != ERR_NONE) {
776 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800777 }
Elliott Hughes74847412012-06-20 18:10:21 -0700778 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700779 return ERR_OUT_OF_MEMORY;
780 }
781 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700782 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700783 return ERR_NONE;
784}
785
786/*
787 * Return line number information for the method, if present.
788 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700789static JdwpError M_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700790 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700791 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700792 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700793
Elliott Hughes74847412012-06-20 18:10:21 -0700794 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId, method_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700795
Elliott Hughes74847412012-06-20 18:10:21 -0700796 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700797
798 return ERR_NONE;
799}
800
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700801static JdwpError M_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
802 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700803 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700804 RefTypeId class_id = ReadRefTypeId(&buf);
805 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700806
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700807 VLOG(jdwp) << StringPrintf(" Req for LocalVarTab in class=%s method=%s",
808 Dbg::GetClassName(class_id).c_str(),
809 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700810
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800811 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
812 // information. That will cause Eclipse to make a best-effort attempt at displaying local
813 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
814 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700815 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700816 return ERR_NONE;
817}
818
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700819static JdwpError M_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen,
820 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700821 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700822 return M_VariableTable(state, buf, dataLen, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800823}
824
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700825static JdwpError M_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
826 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700827 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700828 return M_VariableTable(state, buf, dataLen, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800829}
830
Elliott Hughes9777ba22013-01-17 09:04:19 -0800831static JdwpError M_Bytecodes(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
832 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
833 RefTypeId class_id = ReadRefTypeId(&buf);
834 MethodId method_id = ReadMethodId(&buf);
835
836 std::vector<uint8_t> bytecodes;
837 JdwpError rc = Dbg::GetBytecodes(class_id, method_id, bytecodes);
838 if (rc != ERR_NONE) {
839 return rc;
840 }
841
842 expandBufAdd4BE(reply, bytecodes.size());
843 for (size_t i = 0; i < bytecodes.size(); ++i) {
844 expandBufAdd1(reply, bytecodes[i]);
845 }
846
847 return ERR_NONE;
848}
849
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700850/*
851 * Given an object reference, return the runtime type of the object
852 * (class or array).
853 *
Elliott Hughes74847412012-06-20 18:10:21 -0700854 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700855 * passed in here.
856 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700857static JdwpError OR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700858 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700859 ObjectId object_id = ReadObjectId(&buf);
860 VLOG(jdwp) << StringPrintf(" Req for type of object_id=%#llx", object_id);
861 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700862}
863
864/*
865 * Get values from the fields of an object.
866 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700867static JdwpError OR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700868 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700869 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800870 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700871
Elliott Hughes74847412012-06-20 18:10:21 -0700872 VLOG(jdwp) << StringPrintf(" Req for %d fields from object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700873
Elliott Hughes0cf74332012-02-23 23:14:00 -0800874 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700875
Elliott Hughes0cf74332012-02-23 23:14:00 -0800876 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700877 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700878 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800879 if (status != ERR_NONE) {
880 return status;
881 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700882 }
883
884 return ERR_NONE;
885}
886
887/*
888 * Set values in the fields of an object.
889 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700890static JdwpError OR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700891 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700892 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800893 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700894
Elliott Hughes74847412012-06-20 18:10:21 -0700895 VLOG(jdwp) << StringPrintf(" Req to set %d fields in object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700896
Elliott Hughes0cf74332012-02-23 23:14:00 -0800897 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700898 FieldId fieldId = ReadFieldId(&buf);
899
Elliott Hughesaed4be92011-12-02 16:16:23 -0800900 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800901 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800902 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700903
Elliott Hughes2435a572012-02-17 16:07:41 -0800904 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700905 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800906 if (status != ERR_NONE) {
907 return status;
908 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700909 }
910
911 return ERR_NONE;
912}
913
Elliott Hughesf327e072013-01-09 16:01:26 -0800914static JdwpError OR_MonitorInfo(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
915 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
916 ObjectId object_id = ReadObjectId(&buf);
917 return Dbg::GetMonitorInfo(object_id, reply);
918}
919
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700920/*
921 * Invoke an instance method. The invocation must occur in the specified
922 * thread, which must have been suspended by an event.
923 *
924 * The call is synchronous. All threads in the VM are resumed, unless the
925 * SINGLE_THREADED flag is set.
926 *
927 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
928 * object), it will try to invoke the object's toString() function. This
929 * feature becomes crucial when examining ArrayLists with Eclipse.
930 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700931static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
932 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700933 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700934 ObjectId object_id = ReadObjectId(&buf);
935 ObjectId thread_id = ReadObjectId(&buf);
936 RefTypeId class_id = ReadRefTypeId(&buf);
937 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700938
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700939 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700940}
941
942/*
943 * Disable garbage collection of the specified object.
944 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945static JdwpError OR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700946 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700947 // this is currently a no-op
948 return ERR_NONE;
949}
950
951/*
952 * Enable garbage collection of the specified object.
953 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954static JdwpError OR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700955 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700956 // this is currently a no-op
957 return ERR_NONE;
958}
959
960/*
961 * Determine whether an object has been garbage collected.
962 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700964 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700965 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700966
Elliott Hughes74847412012-06-20 18:10:21 -0700967 object_id = ReadObjectId(&buf);
968 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700969
970 // TODO: currently returning false; must integrate with GC
971 expandBufAdd1(pReply, 0);
972
973 return ERR_NONE;
974}
975
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800976static JdwpError OR_ReferringObjects(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
977 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
978 ObjectId object_id = ReadObjectId(&buf);
979 int32_t max_count = Read4BE(&buf);
980 if (max_count < 0) {
981 return ERR_ILLEGAL_ARGUMENT;
982 }
983
984 std::vector<ObjectId> referring_objects;
985 JdwpError rc = Dbg::GetReferringObjects(object_id, max_count, referring_objects);
986 if (rc != ERR_NONE) {
987 return rc;
988 }
989
990 return WriteTaggedObjectList(reply, referring_objects);
991}
992
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700993/*
994 * Return the string value in a string object.
995 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700997 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700998 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800999 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001000
Elliott Hughes82914b62012-04-09 15:56:29 -07001001 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001002
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001003 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001004
1005 return ERR_NONE;
1006}
1007
1008/*
1009 * Return a thread's name.
1010 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001012 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001013 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001014
Elliott Hughes74847412012-06-20 18:10:21 -07001015 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001016 std::string name;
Elliott Hughes221229c2013-01-08 18:17:50 -08001017 JdwpError error = Dbg::GetThreadName(thread_id, name);
1018 if (error != ERR_NONE) {
1019 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001020 }
Elliott Hughes74847412012-06-20 18:10:21 -07001021 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001022 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001023
1024 return ERR_NONE;
1025}
1026
1027/*
1028 * Suspend the specified thread.
1029 *
1030 * It's supposed to remain suspended even if interpreted code wants to
1031 * resume it; only the JDI is allowed to resume it.
1032 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001033static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001034 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001035 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001036
Elliott Hughes74847412012-06-20 18:10:21 -07001037 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001038 LOG(INFO) << " Warning: ignoring request to suspend self";
1039 return ERR_THREAD_NOT_SUSPENDED;
1040 }
Elliott Hughes74847412012-06-20 18:10:21 -07001041 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042 Thread* self = Thread::Current();
1043 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
1044 JdwpError result = Dbg::SuspendThread(thread_id);
1045 self->TransitionFromSuspendedToRunnable();
1046 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001047}
1048
1049/*
1050 * Resume the specified thread.
1051 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001052static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001053 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001054 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001055
Elliott Hughes74847412012-06-20 18:10:21 -07001056 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001057 LOG(INFO) << " Warning: ignoring request to resume self";
1058 return ERR_NONE;
1059 }
Elliott Hughes74847412012-06-20 18:10:21 -07001060 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
1061 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001062 return ERR_NONE;
1063}
1064
1065/*
1066 * Return status of specified thread.
1067 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001069 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001070 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001071
Elliott Hughes74847412012-06-20 18:10:21 -07001072 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001073
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08001074 JDWP::JdwpThreadStatus threadStatus;
1075 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -08001076 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
1077 if (error != ERR_NONE) {
1078 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001079 }
1080
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001081 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001082
1083 expandBufAdd4BE(pReply, threadStatus);
1084 expandBufAdd4BE(pReply, suspendStatus);
1085
1086 return ERR_NONE;
1087}
1088
1089/*
1090 * Return the thread group that the specified thread is a member of.
1091 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001093 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001094 ObjectId thread_id = ReadObjectId(&buf);
1095 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001096}
1097
1098/*
1099 * Return the current call stack of a suspended thread.
1100 *
1101 * If the thread isn't suspended, the error code isn't defined, but should
1102 * be THREAD_NOT_SUSPENDED.
1103 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001104static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001106 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001107 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001108 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001109
Elliott Hughes221229c2013-01-08 18:17:50 -08001110 size_t actual_frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001111 JdwpError error = Dbg::GetThreadFrameCount(thread_id, actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001112 if (error != ERR_NONE) {
1113 return error;
1114 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001115
Elliott Hughes74847412012-06-20 18:10:21 -07001116 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 -08001117 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001118 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1119 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001120
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001121 if (start_frame > actual_frame_count) {
1122 return ERR_INVALID_INDEX;
1123 }
1124 if (length == static_cast<uint32_t>(-1)) {
1125 length = actual_frame_count - start_frame;
1126 }
1127 if (start_frame + length > actual_frame_count) {
1128 return ERR_INVALID_LENGTH;
1129 }
1130
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001131 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001132}
1133
1134/*
1135 * Returns the #of frames on the specified thread, which must be suspended.
1136 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001137static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001139 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001140
Elliott Hughes221229c2013-01-08 18:17:50 -08001141 size_t frame_count;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001142 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, frame_count);
1143 if (rc != ERR_NONE) {
1144 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001145 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001146 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001147
1148 return ERR_NONE;
1149}
1150
Elliott Hughes734b8c62013-01-11 15:32:45 -08001151static JdwpError TR_OwnedMonitors(const uint8_t* buf, ExpandBuf* reply, bool with_stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1153 ObjectId thread_id = ReadObjectId(&buf);
1154
1155 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001156 std::vector<uint32_t> stack_depths;
1157 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, monitors, stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001158 if (rc != ERR_NONE) {
1159 return rc;
1160 }
1161
1162 expandBufAdd4BE(reply, monitors.size());
1163 for (size_t i = 0; i < monitors.size(); ++i) {
1164 rc = WriteTaggedObject(reply, monitors[i]);
1165 if (rc != ERR_NONE) {
1166 return rc;
1167 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001168 if (with_stack_depths) {
1169 expandBufAdd4BE(reply, stack_depths[i]);
1170 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001171 }
1172 return ERR_NONE;
1173}
1174
Elliott Hughes734b8c62013-01-11 15:32:45 -08001175static JdwpError TR_OwnedMonitors(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1177 return TR_OwnedMonitors(buf, reply, false);
1178}
1179
1180static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1182 return TR_OwnedMonitors(buf, reply, true);
1183}
1184
Elliott Hughesf9501702013-01-11 11:22:27 -08001185static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesf9501702013-01-11 11:22:27 -08001187 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001188
Elliott Hughesf9501702013-01-11 11:22:27 -08001189 ObjectId contended_monitor;
1190 JdwpError rc = Dbg::GetContendedMonitor(thread_id, contended_monitor);
1191 if (rc != ERR_NONE) {
1192 return rc;
1193 }
1194 return WriteTaggedObject(reply, contended_monitor);
1195}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001196
Elliott Hughesf9501702013-01-11 11:22:27 -08001197static JdwpError TR_Interrupt(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1199 ObjectId thread_id = ReadObjectId(&buf);
1200 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001201}
1202
1203/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001204 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001205 *
1206 * (The thread *might* still be running -- it might not have examined
1207 * its suspend count recently.)
1208 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001209static JdwpError TR_DebugSuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001211 ObjectId thread_id = ReadObjectId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001213}
1214
1215/*
1216 * Return the name of a thread group.
1217 *
1218 * The Eclipse debugger recognizes "main" and "system" as special.
1219 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001220static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001222 ObjectId thread_group_id = ReadObjectId(&buf);
1223 VLOG(jdwp) << StringPrintf(" Req for name of thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001224
Elliott Hughescaf76542012-06-28 16:08:22 -07001225 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001226
1227 return ERR_NONE;
1228}
1229
1230/*
1231 * Returns the thread group -- if any -- that contains the specified
1232 * thread group.
1233 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001235 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001236 ObjectId thread_group_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001237
Elliott Hughescaf76542012-06-28 16:08:22 -07001238 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001239 expandBufAddObjectId(pReply, parentGroup);
1240
1241 return ERR_NONE;
1242}
1243
1244/*
1245 * Return the active threads and thread groups that are part of the
1246 * specified thread group.
1247 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001248static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001250 ObjectId thread_group_id = ReadObjectId(&buf);
1251 VLOG(jdwp) << StringPrintf(" Req for threads in thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001252
Elliott Hughescaf76542012-06-28 16:08:22 -07001253 std::vector<ObjectId> thread_ids;
1254 Dbg::GetThreads(thread_group_id, thread_ids);
1255 expandBufAdd4BE(pReply, thread_ids.size());
1256 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1257 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001258 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001259
Elliott Hughescaf76542012-06-28 16:08:22 -07001260 std::vector<ObjectId> child_thread_groups_ids;
1261 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1262 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1263 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1264 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001265 }
1266
1267 return ERR_NONE;
1268}
1269
1270/*
1271 * Return the #of components in the array.
1272 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001275 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001276 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001277
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001278 int length;
1279 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1280 if (status != ERR_NONE) {
1281 return status;
1282 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001283 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001284
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001285 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001286
1287 return ERR_NONE;
1288}
1289
1290/*
1291 * Return the values from an array.
1292 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001294 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001295 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001296 uint32_t firstIndex = Read4BE(&buf);
1297 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001298 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001299
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001300 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001301}
1302
1303/*
1304 * Set values in an array.
1305 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001306static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001307 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001308 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001309 uint32_t firstIndex = Read4BE(&buf);
1310 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001311
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId,
1313 firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001314
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001315 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001316}
1317
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001318static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001320 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001321 // TODO: we should only return classes which have the given class loader as a defining or
1322 // initiating loader. The former would be easy; the latter is hard, because we don't have
1323 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001324 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001325}
1326
1327/*
1328 * Set an event trigger.
1329 *
1330 * Reply with a requestID.
1331 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001332static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001334 const uint8_t* origBuf = buf;
1335
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001336 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001337 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001338 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001339
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001340 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001341 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001342 << " mods=" << modifierCount << ")";
1343
1344 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1345
1346 JdwpEvent* pEvent = EventAlloc(modifierCount);
1347 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001348 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001349 pEvent->modCount = modifierCount;
1350
1351 /*
1352 * Read modifiers. Ordering may be significant (see explanation of Count
1353 * mods in JDWP doc).
1354 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001355 for (uint32_t i = 0; i < modifierCount; ++i) {
1356 JdwpEventMod& mod = pEvent->mods[i];
1357 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1358 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001359 case MK_COUNT: /* report once, when "--count" reaches 0 */
1360 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001361 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001362 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001363 if (count == 0) {
1364 return ERR_INVALID_COUNT;
1365 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001366 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001367 }
1368 break;
1369 case MK_CONDITIONAL: /* conditional on expression) */
1370 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001371 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001372 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001373 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001374 }
1375 break;
1376 case MK_THREAD_ONLY: /* only report events in specified thread */
1377 {
Elliott Hughes74847412012-06-20 18:10:21 -07001378 ObjectId thread_id = ReadObjectId(&buf);
1379 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1380 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001381 }
1382 break;
1383 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1384 {
Elliott Hughes74847412012-06-20 18:10:21 -07001385 RefTypeId class_id = ReadRefTypeId(&buf);
1386 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1387 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001388 }
1389 break;
1390 case MK_CLASS_MATCH: /* restrict events to matching classes */
1391 {
Elliott Hughes86964332012-02-15 19:37:42 -08001392 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001393 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001394 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001395 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001396 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001397 }
1398 break;
1399 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1400 {
Elliott Hughes86964332012-02-15 19:37:42 -08001401 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001402 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001403 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001404 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001405 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001406 }
1407 break;
1408 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1409 {
1410 JdwpLocation loc;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001411 ReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001412 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001413 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001414 }
1415 break;
1416 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1417 {
1418 RefTypeId exceptionOrNull; /* null == all exceptions */
1419 uint8_t caught, uncaught;
1420
1421 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001422 caught = Read1(&buf);
1423 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001424 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001425 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001426
Elliott Hughes972a47b2012-02-21 18:16:06 -08001427 mod.exceptionOnly.refTypeId = exceptionOrNull;
1428 mod.exceptionOnly.caught = caught;
1429 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001430 }
1431 break;
1432 case MK_FIELD_ONLY: /* for field access/mod events */
1433 {
1434 RefTypeId declaring = ReadRefTypeId(&buf);
1435 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001436 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001437 mod.fieldOnly.refTypeId = declaring;
1438 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001439 }
1440 break;
1441 case MK_STEP: /* for use with EK_SINGLE_STEP */
1442 {
Elliott Hughes74847412012-06-20 18:10:21 -07001443 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001444 uint32_t size, depth;
1445
Elliott Hughes74847412012-06-20 18:10:21 -07001446 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001447 size = Read4BE(&buf);
1448 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001449 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001450 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1451
Elliott Hughes74847412012-06-20 18:10:21 -07001452 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001453 mod.step.size = size;
1454 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001455 }
1456 break;
1457 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1458 {
1459 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001460 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001461 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001462 }
1463 break;
1464 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001465 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001466 break;
1467 }
1468 }
1469
1470 /*
1471 * Make sure we consumed all data. It is possible that the remote side
1472 * has sent us bad stuff, but for now we blame ourselves.
1473 */
1474 if (buf != origBuf + dataLen) {
1475 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1476 }
1477
1478 /*
1479 * We reply with an integer "requestID".
1480 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001481 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001482 expandBufAdd4BE(pReply, requestId);
1483
1484 pEvent->requestId = requestId;
1485
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001486 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001487
1488 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001489 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001490 if (err != ERR_NONE) {
1491 /* registration failed, probably because event is bogus */
1492 EventFree(pEvent);
1493 LOG(WARNING) << "WARNING: event request rejected";
1494 }
1495 return err;
1496}
1497
1498/*
1499 * Clear an event. Failure to find an event with a matching ID is a no-op
1500 * and does not return an error.
1501 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001502static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001503 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001504 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001505 eventKind = Read1(&buf);
1506 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001507
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001508 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001509
Elliott Hughes761928d2011-11-16 18:33:03 -08001510 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001511
1512 return ERR_NONE;
1513}
1514
1515/*
1516 * Return the values of arguments and local variables.
1517 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001518static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001519 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001520 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001521 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001522 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001523
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001524 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 -07001525
1526 expandBufAdd4BE(pReply, slots); /* "int values" */
1527 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001528 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001529 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001530
Elliott Hughes2435a572012-02-17 16:07:41 -08001531 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001532
Elliott Hughesdbb40792011-11-18 17:05:22 -08001533 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001534 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001535 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001536 }
1537
1538 return ERR_NONE;
1539}
1540
1541/*
1542 * Set the values of arguments and local variables.
1543 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001544static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001545 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001546 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001547 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001548 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001549
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001550 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 -07001551
1552 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001553 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001554 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001555 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001556 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001557
Elliott Hughes2435a572012-02-17 16:07:41 -08001558 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001559 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001560 }
1561
1562 return ERR_NONE;
1563}
1564
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001565static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001566 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001567 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001568 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001569
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001570 ObjectId object_id;
1571 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001572 if (rc != ERR_NONE) {
1573 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001574 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001575
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001576 VLOG(jdwp) << StringPrintf(" Req for 'this' in thread_id=%#llx frame=%lld --> %#llx",
1577 thread_id, frame_id, object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001578
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001579 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001580}
1581
1582/*
1583 * Return the reference type reflected by this class object.
1584 *
1585 * This appears to be required because ReferenceTypeId values are NEVER
1586 * reused, whereas ClassIds can be recycled like any other object. (Either
1587 * that, or I have no idea what this is for.)
1588 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001589static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001590 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001591 RefTypeId classObjectId = ReadRefTypeId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001592 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId,
1593 Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001594 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001595}
1596
1597/*
1598 * Handle a DDM packet with a single chunk in it.
1599 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001600static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001601 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001602 uint8_t* replyBuf = NULL;
1603 int replyLen = -1;
1604
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001605 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001606
Elliott Hughesa21039c2012-06-21 12:09:25 -07001607 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001608
1609 /*
1610 * If they want to send something back, we copy it into the buffer.
1611 * A no-copy approach would be nicer.
1612 *
1613 * TODO: consider altering the JDWP stuff to hold the packet header
1614 * in a separate buffer. That would allow us to writev() DDM traffic
1615 * instead of copying it into the expanding buffer. The reduction in
1616 * heap requirements is probably more valuable than the efficiency.
1617 */
1618 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1619 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1620 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1621 free(replyBuf);
1622 }
1623 return ERR_NONE;
1624}
1625
1626/*
1627 * Handler map decl.
1628 */
1629typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1630
1631struct JdwpHandlerMap {
1632 uint8_t cmdSet;
1633 uint8_t cmd;
1634 JdwpRequestHandler func;
1635 const char* descr;
1636};
1637
1638/*
1639 * Map commands to functions.
1640 *
1641 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1642 * and 128-256 are vendor-defined.
1643 */
1644static const JdwpHandlerMap gHandlerMap[] = {
1645 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001646 { 1, 1, VM_Version, "VirtualMachine.Version" },
1647 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1648 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1649 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1650 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1651 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1652 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1653 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1654 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1655 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1656 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1657 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1658 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1659 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1660 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1661 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
1662 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1663 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1664 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001665 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001666 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001667
1668 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001669 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1670 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1671 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1672 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1673 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1674 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1675 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
1676 { 2, 8, NULL, "ReferenceType.NestedTypes" },
1677 { 2, 9, RT_Status, "ReferenceType.Status" },
1678 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1679 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001680 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1681 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001682 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1683 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughes3b78c942013-01-15 17:35:41 -08001684 { 2, 16, RT_Instances, "ReferenceType.Instances" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001685 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1686 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001687
1688 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001689 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1690 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1691 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1692 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001693
1694 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001695 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001696
1697 /* InterfaceType command set (5) */
1698
1699 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001700 { 6, 1, M_LineTable, "Method.LineTable" },
1701 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughes9777ba22013-01-17 09:04:19 -08001702 { 6, 3, M_Bytecodes, "Method.Bytecodes" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001703 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001704 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001705
1706 /* Field command set (8) */
1707
1708 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001709 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1710 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1711 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
1712 { 9, 4, NULL, "ObjectReference.UNUSED" },
1713 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1714 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001715 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001716 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1717 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001718 { 9, 10, OR_ReferringObjects, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001719
1720 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001721 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001722
1723 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001724 { 11, 1, TR_Name, "ThreadReference.Name" },
1725 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1726 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1727 { 11, 4, TR_Status, "ThreadReference.Status" },
1728 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1729 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1730 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1731 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1732 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1733 { 11, 10, NULL, "ThreadReference.Stop" },
1734 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1735 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1736 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1737 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001738
1739 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001740 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1741 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1742 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001743
1744 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001745 { 13, 1, AR_Length, "ArrayReference.Length" },
1746 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1747 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001748
1749 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001750 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001751
1752 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001753 { 15, 1, ER_Set, "EventRequest.Set" },
1754 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001755 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001756
1757 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001758 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1759 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1760 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001761 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001762
1763 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001764 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001765
1766 /* Event command set (64) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001767 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001768
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001769 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001770};
1771
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001772static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001773 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001774 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1775 return gHandlerMap[i].descr;
1776 }
1777 }
1778 return "?UNKNOWN?";
1779}
1780
1781static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1782 std::string result;
1783 result += "REQ: ";
1784 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1785 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1786 return result;
1787}
1788
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001789/*
1790 * Process a request from the debugger.
1791 *
1792 * On entry, the JDWP thread is in VMWAIT.
1793 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001794void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001795 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001796
1797 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1798 /*
1799 * Activity from a debugger, not merely ddms. Mark us as having an
1800 * active debugger session, and zero out the last-activity timestamp
1801 * so waitForDebugger() doesn't return if we stall for a bit here.
1802 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001803 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001804 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001805 }
1806
1807 /*
1808 * If a debugger event has fired in another thread, wait until the
1809 * initiating thread has suspended itself before processing messages
1810 * from the debugger. Otherwise we (the JDWP thread) could be told to
1811 * resume the thread before it has suspended.
1812 *
1813 * We call with an argument of zero to wait for the current event
1814 * thread to finish, and then clear the block. Depending on the thread
1815 * suspend policy, this may allow events in other threads to fire,
1816 * but those events have no bearing on what the debugger has sent us
1817 * in the current request.
1818 *
1819 * Note that we MUST clear the event token before waking the event
1820 * thread up, or risk waiting for the thread to suspend after we've
1821 * told it to resume.
1822 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001823 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001824
1825 /*
1826 * Tell the VM that we're running and shouldn't be interrupted by GC.
1827 * Do this after anything that can stall indefinitely.
1828 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001829 Thread* self = Thread::Current();
1830 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001831
1832 expandBufAddSpace(pReply, kJDWPHeaderLen);
1833
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001834 size_t i;
1835 for (i = 0; i < arraysize(gHandlerMap); i++) {
1836 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001837 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001838 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001839 break;
1840 }
1841 }
1842 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001843 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1844 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001845 result = ERR_NOT_IMPLEMENTED;
1846 }
1847
1848 /*
1849 * Set up the reply header.
1850 *
1851 * If we encountered an error, only send the header back.
1852 */
1853 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001854 Set4BE(replyBuf + 4, pHeader->id);
1855 Set1(replyBuf + 8, kJDWPFlagReply);
1856 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001857 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001858 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001859 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001860 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001861 }
1862
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001863 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001864 if (false) {
1865 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001866 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001867 }
1868
1869 /*
1870 * Update last-activity timestamp. We really only need this during
1871 * the initial setup. Only update if this is a non-DDMS packet.
1872 */
1873 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001874 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001875 }
1876
1877 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001878 self->TransitionFromRunnableToSuspended(old_state);
1879
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001880}
1881
1882} // namespace JDWP
1883
1884} // namespace art