blob: 23a31308d823b9eebe1aaa9f3e1372a293cffcef [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
386 expandBufAdd1(reply, false); // canGetBytecodes
387 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 Hughes872d4ec2011-10-21 17:07:15 -0700831/*
832 * Given an object reference, return the runtime type of the object
833 * (class or array).
834 *
Elliott Hughes74847412012-06-20 18:10:21 -0700835 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700836 * passed in here.
837 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700838static JdwpError OR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700839 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700840 ObjectId object_id = ReadObjectId(&buf);
841 VLOG(jdwp) << StringPrintf(" Req for type of object_id=%#llx", object_id);
842 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700843}
844
845/*
846 * Get values from the fields of an object.
847 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700848static JdwpError OR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700849 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700850 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800851 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700852
Elliott Hughes74847412012-06-20 18:10:21 -0700853 VLOG(jdwp) << StringPrintf(" Req for %d fields from object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700854
Elliott Hughes0cf74332012-02-23 23:14:00 -0800855 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700856
Elliott Hughes0cf74332012-02-23 23:14:00 -0800857 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700858 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700859 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800860 if (status != ERR_NONE) {
861 return status;
862 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700863 }
864
865 return ERR_NONE;
866}
867
868/*
869 * Set values in the fields of an object.
870 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700871static JdwpError OR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700872 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700873 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800874 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700875
Elliott Hughes74847412012-06-20 18:10:21 -0700876 VLOG(jdwp) << StringPrintf(" Req to set %d fields in object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700877
Elliott Hughes0cf74332012-02-23 23:14:00 -0800878 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700879 FieldId fieldId = ReadFieldId(&buf);
880
Elliott Hughesaed4be92011-12-02 16:16:23 -0800881 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800882 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800883 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700884
Elliott Hughes2435a572012-02-17 16:07:41 -0800885 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700886 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800887 if (status != ERR_NONE) {
888 return status;
889 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700890 }
891
892 return ERR_NONE;
893}
894
Elliott Hughesf327e072013-01-09 16:01:26 -0800895static JdwpError OR_MonitorInfo(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
896 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
897 ObjectId object_id = ReadObjectId(&buf);
898 return Dbg::GetMonitorInfo(object_id, reply);
899}
900
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700901/*
902 * Invoke an instance method. The invocation must occur in the specified
903 * thread, which must have been suspended by an event.
904 *
905 * The call is synchronous. All threads in the VM are resumed, unless the
906 * SINGLE_THREADED flag is set.
907 *
908 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
909 * object), it will try to invoke the object's toString() function. This
910 * feature becomes crucial when examining ArrayLists with Eclipse.
911 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700912static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
913 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700914 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700915 ObjectId object_id = ReadObjectId(&buf);
916 ObjectId thread_id = ReadObjectId(&buf);
917 RefTypeId class_id = ReadRefTypeId(&buf);
918 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700919
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700920 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700921}
922
923/*
924 * Disable garbage collection of the specified object.
925 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926static JdwpError OR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700927 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700928 // this is currently a no-op
929 return ERR_NONE;
930}
931
932/*
933 * Enable garbage collection of the specified object.
934 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935static JdwpError OR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700936 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700937 // this is currently a no-op
938 return ERR_NONE;
939}
940
941/*
942 * Determine whether an object has been garbage collected.
943 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700945 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700946 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700947
Elliott Hughes74847412012-06-20 18:10:21 -0700948 object_id = ReadObjectId(&buf);
949 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700950
951 // TODO: currently returning false; must integrate with GC
952 expandBufAdd1(pReply, 0);
953
954 return ERR_NONE;
955}
956
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800957static JdwpError OR_ReferringObjects(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
958 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
959 ObjectId object_id = ReadObjectId(&buf);
960 int32_t max_count = Read4BE(&buf);
961 if (max_count < 0) {
962 return ERR_ILLEGAL_ARGUMENT;
963 }
964
965 std::vector<ObjectId> referring_objects;
966 JdwpError rc = Dbg::GetReferringObjects(object_id, max_count, referring_objects);
967 if (rc != ERR_NONE) {
968 return rc;
969 }
970
971 return WriteTaggedObjectList(reply, referring_objects);
972}
973
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700974/*
975 * Return the string value in a string object.
976 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700978 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700979 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800980 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700981
Elliott Hughes82914b62012-04-09 15:56:29 -0700982 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700983
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800984 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700985
986 return ERR_NONE;
987}
988
989/*
990 * Return a thread's name.
991 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700993 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700994 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995
Elliott Hughes74847412012-06-20 18:10:21 -0700996 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800997 std::string name;
Elliott Hughes221229c2013-01-08 18:17:50 -0800998 JdwpError error = Dbg::GetThreadName(thread_id, name);
999 if (error != ERR_NONE) {
1000 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001001 }
Elliott Hughes74847412012-06-20 18:10:21 -07001002 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001003 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001004
1005 return ERR_NONE;
1006}
1007
1008/*
1009 * Suspend the specified thread.
1010 *
1011 * It's supposed to remain suspended even if interpreted code wants to
1012 * resume it; only the JDI is allowed to resume it.
1013 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001014static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001015 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001016 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001017
Elliott Hughes74847412012-06-20 18:10:21 -07001018 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001019 LOG(INFO) << " Warning: ignoring request to suspend self";
1020 return ERR_THREAD_NOT_SUSPENDED;
1021 }
Elliott Hughes74847412012-06-20 18:10:21 -07001022 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001023 Thread* self = Thread::Current();
1024 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
1025 JdwpError result = Dbg::SuspendThread(thread_id);
1026 self->TransitionFromSuspendedToRunnable();
1027 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001028}
1029
1030/*
1031 * Resume the specified thread.
1032 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001033static JdwpError TR_Resume(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 resume self";
1039 return ERR_NONE;
1040 }
Elliott Hughes74847412012-06-20 18:10:21 -07001041 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
1042 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001043 return ERR_NONE;
1044}
1045
1046/*
1047 * Return status of specified thread.
1048 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001049static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001050 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001051 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001052
Elliott Hughes74847412012-06-20 18:10:21 -07001053 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001054
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08001055 JDWP::JdwpThreadStatus threadStatus;
1056 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -08001057 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
1058 if (error != ERR_NONE) {
1059 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001060 }
1061
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001062 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001063
1064 expandBufAdd4BE(pReply, threadStatus);
1065 expandBufAdd4BE(pReply, suspendStatus);
1066
1067 return ERR_NONE;
1068}
1069
1070/*
1071 * Return the thread group that the specified thread is a member of.
1072 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001074 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001075 ObjectId thread_id = ReadObjectId(&buf);
1076 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001077}
1078
1079/*
1080 * Return the current call stack of a suspended thread.
1081 *
1082 * If the thread isn't suspended, the error code isn't defined, but should
1083 * be THREAD_NOT_SUSPENDED.
1084 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001086 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001087 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001088 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001089 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001090
Elliott Hughes221229c2013-01-08 18:17:50 -08001091 size_t actual_frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001092 JdwpError error = Dbg::GetThreadFrameCount(thread_id, actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001093 if (error != ERR_NONE) {
1094 return error;
1095 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001096
Elliott Hughes74847412012-06-20 18:10:21 -07001097 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 -08001098 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001099 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1100 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001101
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001102 if (start_frame > actual_frame_count) {
1103 return ERR_INVALID_INDEX;
1104 }
1105 if (length == static_cast<uint32_t>(-1)) {
1106 length = actual_frame_count - start_frame;
1107 }
1108 if (start_frame + length > actual_frame_count) {
1109 return ERR_INVALID_LENGTH;
1110 }
1111
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001112 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001113}
1114
1115/*
1116 * Returns the #of frames on the specified thread, which must be suspended.
1117 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001120 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001121
Elliott Hughes221229c2013-01-08 18:17:50 -08001122 size_t frame_count;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001123 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, frame_count);
1124 if (rc != ERR_NONE) {
1125 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001126 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001127 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001128
1129 return ERR_NONE;
1130}
1131
Elliott Hughes734b8c62013-01-11 15:32:45 -08001132static JdwpError TR_OwnedMonitors(const uint8_t* buf, ExpandBuf* reply, bool with_stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1134 ObjectId thread_id = ReadObjectId(&buf);
1135
1136 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001137 std::vector<uint32_t> stack_depths;
1138 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, monitors, stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001139 if (rc != ERR_NONE) {
1140 return rc;
1141 }
1142
1143 expandBufAdd4BE(reply, monitors.size());
1144 for (size_t i = 0; i < monitors.size(); ++i) {
1145 rc = WriteTaggedObject(reply, monitors[i]);
1146 if (rc != ERR_NONE) {
1147 return rc;
1148 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001149 if (with_stack_depths) {
1150 expandBufAdd4BE(reply, stack_depths[i]);
1151 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001152 }
1153 return ERR_NONE;
1154}
1155
Elliott Hughes734b8c62013-01-11 15:32:45 -08001156static JdwpError TR_OwnedMonitors(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1158 return TR_OwnedMonitors(buf, reply, false);
1159}
1160
1161static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1163 return TR_OwnedMonitors(buf, reply, true);
1164}
1165
Elliott Hughesf9501702013-01-11 11:22:27 -08001166static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesf9501702013-01-11 11:22:27 -08001168 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001169
Elliott Hughesf9501702013-01-11 11:22:27 -08001170 ObjectId contended_monitor;
1171 JdwpError rc = Dbg::GetContendedMonitor(thread_id, contended_monitor);
1172 if (rc != ERR_NONE) {
1173 return rc;
1174 }
1175 return WriteTaggedObject(reply, contended_monitor);
1176}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001177
Elliott Hughesf9501702013-01-11 11:22:27 -08001178static JdwpError TR_Interrupt(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1180 ObjectId thread_id = ReadObjectId(&buf);
1181 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001182}
1183
1184/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001185 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001186 *
1187 * (The thread *might* still be running -- it might not have examined
1188 * its suspend count recently.)
1189 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190static JdwpError TR_DebugSuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001191 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001192 ObjectId thread_id = ReadObjectId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001194}
1195
1196/*
1197 * Return the name of a thread group.
1198 *
1199 * The Eclipse debugger recognizes "main" and "system" as special.
1200 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001201static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001203 ObjectId thread_group_id = ReadObjectId(&buf);
1204 VLOG(jdwp) << StringPrintf(" Req for name of thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001205
Elliott Hughescaf76542012-06-28 16:08:22 -07001206 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001207
1208 return ERR_NONE;
1209}
1210
1211/*
1212 * Returns the thread group -- if any -- that contains the specified
1213 * thread group.
1214 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001215static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001216 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001217 ObjectId thread_group_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001218
Elliott Hughescaf76542012-06-28 16:08:22 -07001219 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001220 expandBufAddObjectId(pReply, parentGroup);
1221
1222 return ERR_NONE;
1223}
1224
1225/*
1226 * Return the active threads and thread groups that are part of the
1227 * specified thread group.
1228 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001229static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001231 ObjectId thread_group_id = ReadObjectId(&buf);
1232 VLOG(jdwp) << StringPrintf(" Req for threads in thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001233
Elliott Hughescaf76542012-06-28 16:08:22 -07001234 std::vector<ObjectId> thread_ids;
1235 Dbg::GetThreads(thread_group_id, thread_ids);
1236 expandBufAdd4BE(pReply, thread_ids.size());
1237 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1238 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001239 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001240
Elliott Hughescaf76542012-06-28 16:08:22 -07001241 std::vector<ObjectId> child_thread_groups_ids;
1242 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1243 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1244 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1245 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001246 }
1247
1248 return ERR_NONE;
1249}
1250
1251/*
1252 * Return the #of components in the array.
1253 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001254static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001256 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001257 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001258
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001259 int length;
1260 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1261 if (status != ERR_NONE) {
1262 return status;
1263 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001264 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001265
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001266 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001267
1268 return ERR_NONE;
1269}
1270
1271/*
1272 * Return the values from an array.
1273 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001274static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001275 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001276 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001277 uint32_t firstIndex = Read4BE(&buf);
1278 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001279 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001280
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001281 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001282}
1283
1284/*
1285 * Set values in an array.
1286 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001287static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001288 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001289 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001290 uint32_t firstIndex = Read4BE(&buf);
1291 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001292
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId,
1294 firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001295
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001296 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001297}
1298
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001299static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001301 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001302 // TODO: we should only return classes which have the given class loader as a defining or
1303 // initiating loader. The former would be easy; the latter is hard, because we don't have
1304 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001305 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001306}
1307
1308/*
1309 * Set an event trigger.
1310 *
1311 * Reply with a requestID.
1312 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001313static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001314 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001315 const uint8_t* origBuf = buf;
1316
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001317 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001318 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001319 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001320
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001321 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001322 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001323 << " mods=" << modifierCount << ")";
1324
1325 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1326
1327 JdwpEvent* pEvent = EventAlloc(modifierCount);
1328 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001329 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001330 pEvent->modCount = modifierCount;
1331
1332 /*
1333 * Read modifiers. Ordering may be significant (see explanation of Count
1334 * mods in JDWP doc).
1335 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001336 for (uint32_t i = 0; i < modifierCount; ++i) {
1337 JdwpEventMod& mod = pEvent->mods[i];
1338 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1339 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001340 case MK_COUNT: /* report once, when "--count" reaches 0 */
1341 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001342 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001343 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001344 if (count == 0) {
1345 return ERR_INVALID_COUNT;
1346 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001347 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001348 }
1349 break;
1350 case MK_CONDITIONAL: /* conditional on expression) */
1351 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001352 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001353 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001354 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001355 }
1356 break;
1357 case MK_THREAD_ONLY: /* only report events in specified thread */
1358 {
Elliott Hughes74847412012-06-20 18:10:21 -07001359 ObjectId thread_id = ReadObjectId(&buf);
1360 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1361 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001362 }
1363 break;
1364 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1365 {
Elliott Hughes74847412012-06-20 18:10:21 -07001366 RefTypeId class_id = ReadRefTypeId(&buf);
1367 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1368 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001369 }
1370 break;
1371 case MK_CLASS_MATCH: /* restrict events to matching classes */
1372 {
Elliott Hughes86964332012-02-15 19:37:42 -08001373 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001374 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001375 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001376 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001377 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001378 }
1379 break;
1380 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1381 {
Elliott Hughes86964332012-02-15 19:37:42 -08001382 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001383 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001384 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001385 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001386 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001387 }
1388 break;
1389 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1390 {
1391 JdwpLocation loc;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001392 ReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001393 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001394 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001395 }
1396 break;
1397 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1398 {
1399 RefTypeId exceptionOrNull; /* null == all exceptions */
1400 uint8_t caught, uncaught;
1401
1402 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001403 caught = Read1(&buf);
1404 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001405 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001406 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001407
Elliott Hughes972a47b2012-02-21 18:16:06 -08001408 mod.exceptionOnly.refTypeId = exceptionOrNull;
1409 mod.exceptionOnly.caught = caught;
1410 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001411 }
1412 break;
1413 case MK_FIELD_ONLY: /* for field access/mod events */
1414 {
1415 RefTypeId declaring = ReadRefTypeId(&buf);
1416 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001417 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001418 mod.fieldOnly.refTypeId = declaring;
1419 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001420 }
1421 break;
1422 case MK_STEP: /* for use with EK_SINGLE_STEP */
1423 {
Elliott Hughes74847412012-06-20 18:10:21 -07001424 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001425 uint32_t size, depth;
1426
Elliott Hughes74847412012-06-20 18:10:21 -07001427 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001428 size = Read4BE(&buf);
1429 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001430 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001431 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1432
Elliott Hughes74847412012-06-20 18:10:21 -07001433 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001434 mod.step.size = size;
1435 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001436 }
1437 break;
1438 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1439 {
1440 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001441 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001442 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001443 }
1444 break;
1445 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001446 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001447 break;
1448 }
1449 }
1450
1451 /*
1452 * Make sure we consumed all data. It is possible that the remote side
1453 * has sent us bad stuff, but for now we blame ourselves.
1454 */
1455 if (buf != origBuf + dataLen) {
1456 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1457 }
1458
1459 /*
1460 * We reply with an integer "requestID".
1461 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001462 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001463 expandBufAdd4BE(pReply, requestId);
1464
1465 pEvent->requestId = requestId;
1466
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001467 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001468
1469 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001470 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001471 if (err != ERR_NONE) {
1472 /* registration failed, probably because event is bogus */
1473 EventFree(pEvent);
1474 LOG(WARNING) << "WARNING: event request rejected";
1475 }
1476 return err;
1477}
1478
1479/*
1480 * Clear an event. Failure to find an event with a matching ID is a no-op
1481 * and does not return an error.
1482 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001483static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001484 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001485 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001486 eventKind = Read1(&buf);
1487 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001488
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001489 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001490
Elliott Hughes761928d2011-11-16 18:33:03 -08001491 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001492
1493 return ERR_NONE;
1494}
1495
1496/*
1497 * Return the values of arguments and local variables.
1498 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001499static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001500 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001501 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001502 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001503 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001504
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001505 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 -07001506
1507 expandBufAdd4BE(pReply, slots); /* "int values" */
1508 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001509 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001510 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001511
Elliott Hughes2435a572012-02-17 16:07:41 -08001512 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001513
Elliott Hughesdbb40792011-11-18 17:05:22 -08001514 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001515 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001516 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001517 }
1518
1519 return ERR_NONE;
1520}
1521
1522/*
1523 * Set the values of arguments and local variables.
1524 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001525static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001526 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001527 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001528 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001529 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001530
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001531 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 -07001532
1533 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001534 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001535 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001536 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001537 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001538
Elliott Hughes2435a572012-02-17 16:07:41 -08001539 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001540 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001541 }
1542
1543 return ERR_NONE;
1544}
1545
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001546static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001547 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001548 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001549 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001550
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001551 ObjectId object_id;
1552 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001553 if (rc != ERR_NONE) {
1554 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001555 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001556
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001557 VLOG(jdwp) << StringPrintf(" Req for 'this' in thread_id=%#llx frame=%lld --> %#llx",
1558 thread_id, frame_id, object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001559
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001560 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001561}
1562
1563/*
1564 * Return the reference type reflected by this class object.
1565 *
1566 * This appears to be required because ReferenceTypeId values are NEVER
1567 * reused, whereas ClassIds can be recycled like any other object. (Either
1568 * that, or I have no idea what this is for.)
1569 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001570static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001571 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001572 RefTypeId classObjectId = ReadRefTypeId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001573 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId,
1574 Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001575 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001576}
1577
1578/*
1579 * Handle a DDM packet with a single chunk in it.
1580 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001581static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001582 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001583 uint8_t* replyBuf = NULL;
1584 int replyLen = -1;
1585
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001586 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001587
Elliott Hughesa21039c2012-06-21 12:09:25 -07001588 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001589
1590 /*
1591 * If they want to send something back, we copy it into the buffer.
1592 * A no-copy approach would be nicer.
1593 *
1594 * TODO: consider altering the JDWP stuff to hold the packet header
1595 * in a separate buffer. That would allow us to writev() DDM traffic
1596 * instead of copying it into the expanding buffer. The reduction in
1597 * heap requirements is probably more valuable than the efficiency.
1598 */
1599 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1600 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1601 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1602 free(replyBuf);
1603 }
1604 return ERR_NONE;
1605}
1606
1607/*
1608 * Handler map decl.
1609 */
1610typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1611
1612struct JdwpHandlerMap {
1613 uint8_t cmdSet;
1614 uint8_t cmd;
1615 JdwpRequestHandler func;
1616 const char* descr;
1617};
1618
1619/*
1620 * Map commands to functions.
1621 *
1622 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1623 * and 128-256 are vendor-defined.
1624 */
1625static const JdwpHandlerMap gHandlerMap[] = {
1626 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001627 { 1, 1, VM_Version, "VirtualMachine.Version" },
1628 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1629 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1630 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1631 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1632 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1633 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1634 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1635 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1636 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1637 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1638 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1639 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1640 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1641 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1642 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
1643 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1644 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1645 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001646 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001647 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001648
1649 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001650 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1651 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1652 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1653 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1654 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1655 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1656 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
1657 { 2, 8, NULL, "ReferenceType.NestedTypes" },
1658 { 2, 9, RT_Status, "ReferenceType.Status" },
1659 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1660 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001661 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1662 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001663 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1664 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughes3b78c942013-01-15 17:35:41 -08001665 { 2, 16, RT_Instances, "ReferenceType.Instances" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001666 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1667 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001668
1669 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001670 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1671 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1672 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1673 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001674
1675 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001676 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001677
1678 /* InterfaceType command set (5) */
1679
1680 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001681 { 6, 1, M_LineTable, "Method.LineTable" },
1682 { 6, 2, M_VariableTable, "Method.VariableTable" },
1683 { 6, 3, NULL, "Method.Bytecodes" },
1684 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001685 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001686
1687 /* Field command set (8) */
1688
1689 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001690 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1691 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1692 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
1693 { 9, 4, NULL, "ObjectReference.UNUSED" },
1694 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1695 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001696 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001697 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1698 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001699 { 9, 10, OR_ReferringObjects, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001700
1701 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001702 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001703
1704 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001705 { 11, 1, TR_Name, "ThreadReference.Name" },
1706 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1707 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1708 { 11, 4, TR_Status, "ThreadReference.Status" },
1709 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1710 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1711 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1712 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1713 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1714 { 11, 10, NULL, "ThreadReference.Stop" },
1715 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1716 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1717 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1718 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001719
1720 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001721 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1722 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1723 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001724
1725 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001726 { 13, 1, AR_Length, "ArrayReference.Length" },
1727 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1728 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001729
1730 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001731 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001732
1733 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001734 { 15, 1, ER_Set, "EventRequest.Set" },
1735 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001736 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001737
1738 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001739 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1740 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1741 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001742 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001743
1744 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001745 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001746
1747 /* Event command set (64) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001748 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001749
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001750 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001751};
1752
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001753static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001754 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001755 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1756 return gHandlerMap[i].descr;
1757 }
1758 }
1759 return "?UNKNOWN?";
1760}
1761
1762static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1763 std::string result;
1764 result += "REQ: ";
1765 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1766 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1767 return result;
1768}
1769
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001770/*
1771 * Process a request from the debugger.
1772 *
1773 * On entry, the JDWP thread is in VMWAIT.
1774 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001775void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001776 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001777
1778 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1779 /*
1780 * Activity from a debugger, not merely ddms. Mark us as having an
1781 * active debugger session, and zero out the last-activity timestamp
1782 * so waitForDebugger() doesn't return if we stall for a bit here.
1783 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001784 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001785 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001786 }
1787
1788 /*
1789 * If a debugger event has fired in another thread, wait until the
1790 * initiating thread has suspended itself before processing messages
1791 * from the debugger. Otherwise we (the JDWP thread) could be told to
1792 * resume the thread before it has suspended.
1793 *
1794 * We call with an argument of zero to wait for the current event
1795 * thread to finish, and then clear the block. Depending on the thread
1796 * suspend policy, this may allow events in other threads to fire,
1797 * but those events have no bearing on what the debugger has sent us
1798 * in the current request.
1799 *
1800 * Note that we MUST clear the event token before waking the event
1801 * thread up, or risk waiting for the thread to suspend after we've
1802 * told it to resume.
1803 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001804 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001805
1806 /*
1807 * Tell the VM that we're running and shouldn't be interrupted by GC.
1808 * Do this after anything that can stall indefinitely.
1809 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001810 Thread* self = Thread::Current();
1811 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001812
1813 expandBufAddSpace(pReply, kJDWPHeaderLen);
1814
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001815 size_t i;
1816 for (i = 0; i < arraysize(gHandlerMap); i++) {
1817 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001818 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001819 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001820 break;
1821 }
1822 }
1823 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001824 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1825 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001826 result = ERR_NOT_IMPLEMENTED;
1827 }
1828
1829 /*
1830 * Set up the reply header.
1831 *
1832 * If we encountered an error, only send the header back.
1833 */
1834 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001835 Set4BE(replyBuf + 4, pHeader->id);
1836 Set1(replyBuf + 8, kJDWPFlagReply);
1837 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001838 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001839 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001840 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001841 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001842 }
1843
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001844 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001845 if (false) {
1846 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001847 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001848 }
1849
1850 /*
1851 * Update last-activity timestamp. We really only need this during
1852 * the initial setup. Only update if this is a non-DDMS packet.
1853 */
1854 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001855 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001856 }
1857
1858 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001859 self->TransitionFromRunnableToSuspended(old_state);
1860
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001861}
1862
1863} // namespace JDWP
1864
1865} // namespace art