blob: 1d56fe7739a915ca69d2d34e7e99c32698388dbd [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
Elliott Hughes872d4ec2011-10-21 17:07:15 -070017#include <stdlib.h>
18#include <string.h>
19#include <unistd.h>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Elliott Hughesa96836a2013-01-17 12:27:49 -080021#include <string>
22
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "atomic.h"
Ian Rogers43b2e0f2014-01-30 16:58:39 -080024#include "base/hex_dump.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
26#include "base/macros.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080027#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "debugger.h"
29#include "jdwp/jdwp_constants.h"
30#include "jdwp/jdwp_event.h"
31#include "jdwp/jdwp_expand_buf.h"
32#include "jdwp/jdwp_priv.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "runtime.h"
Ian Rogers693ff612013-02-01 10:56:12 -080034#include "thread-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080035
Elliott Hughes872d4ec2011-10-21 17:07:15 -070036namespace art {
37
38namespace JDWP {
39
Elliott Hughes4b9702c2013-02-20 18:13:24 -080040std::string DescribeField(const FieldId& field_id) {
Elliott Hughesa96836a2013-01-17 12:27:49 -080041 return StringPrintf("%#x (%s)", field_id, Dbg::GetFieldName(field_id).c_str());
42}
43
Elliott Hughes4b9702c2013-02-20 18:13:24 -080044std::string DescribeMethod(const MethodId& method_id) {
Elliott Hughesa96836a2013-01-17 12:27:49 -080045 return StringPrintf("%#x (%s)", method_id, Dbg::GetMethodName(method_id).c_str());
46}
47
Elliott Hughes4b9702c2013-02-20 18:13:24 -080048std::string DescribeRefTypeId(const RefTypeId& ref_type_id) {
Elliott Hughesa96836a2013-01-17 12:27:49 -080049 std::string signature("unknown");
Ian Rogersfc0e94b2013-09-23 23:51:32 -070050 Dbg::GetSignature(ref_type_id, &signature);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -080051 return StringPrintf("%#" PRIx64 " (%s)", ref_type_id, signature.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -070052}
53
Elliott Hughes4b9702c2013-02-20 18:13:24 -080054// Helper function: write a variable-width value into the output input buffer.
Elliott Hughes4993bbc2013-01-10 15:41:25 -080055static void WriteValue(ExpandBuf* pReply, int width, uint64_t value) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070056 switch (width) {
57 case 1: expandBufAdd1(pReply, value); break;
58 case 2: expandBufAdd2BE(pReply, value); break;
59 case 4: expandBufAdd4BE(pReply, value); break;
60 case 8: expandBufAdd8BE(pReply, value); break;
61 default: LOG(FATAL) << width; break;
62 }
63}
64
Elliott Hughes4993bbc2013-01-10 15:41:25 -080065static JdwpError WriteTaggedObject(ExpandBuf* reply, ObjectId object_id)
66 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
67 uint8_t tag;
68 JdwpError rc = Dbg::GetObjectTag(object_id, tag);
69 if (rc == ERR_NONE) {
70 expandBufAdd1(reply, tag);
71 expandBufAddObjectId(reply, object_id);
72 }
73 return rc;
74}
75
Elliott Hughes0cbaff52013-01-16 15:28:01 -080076static JdwpError WriteTaggedObjectList(ExpandBuf* reply, const std::vector<ObjectId>& objects)
77 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
78 expandBufAdd4BE(reply, objects.size());
79 for (size_t i = 0; i < objects.size(); ++i) {
80 JdwpError rc = WriteTaggedObject(reply, objects[i]);
81 if (rc != ERR_NONE) {
82 return rc;
83 }
84 }
85 return ERR_NONE;
86}
87
Elliott Hughes872d4ec2011-10-21 17:07:15 -070088/*
89 * Common code for *_InvokeMethod requests.
90 *
Elliott Hughes74847412012-06-20 18:10:21 -070091 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -070092 * expected-to-be-void return value of the called function.
93 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -080094static JdwpError FinishInvoke(JdwpState*, Request& request, ExpandBuf* pReply,
Elliott Hughes74847412012-06-20 18:10:21 -070095 ObjectId thread_id, ObjectId object_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 RefTypeId class_id, MethodId method_id, bool is_constructor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -070098 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070099
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800100 int32_t arg_count = request.ReadSigned32("argument count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700101
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800102 VLOG(jdwp) << StringPrintf(" --> thread_id=%#" PRIx64 " object_id=%#" PRIx64,
103 thread_id, object_id);
104 VLOG(jdwp) << StringPrintf(" class_id=%#" PRIx64 " method_id=%x %s.%s", class_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 method_id, Dbg::GetClassName(class_id).c_str(),
Elliott Hughesa96836a2013-01-17 12:27:49 -0800106 Dbg::GetMethodName(method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800107 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108
Ian Rogers700a4022014-05-19 16:49:03 -0700109 std::unique_ptr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
110 std::unique_ptr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800111 for (int32_t i = 0; i < arg_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800112 argTypes[i] = request.ReadTag();
Elliott Hughes45651fd2012-02-21 15:48:20 -0800113 size_t width = Dbg::GetTagWidth(argTypes[i]);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800114 argValues[i] = request.ReadValue(width);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800115 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#" PRIx64, width,
116 argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700117 }
118
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800119 uint32_t options = request.ReadUnsigned32("InvokeOptions bit flags");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
121 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
122 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123
Elliott Hughes45651fd2012-02-21 15:48:20 -0800124 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125 uint64_t resultValue;
126 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700127 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 -0700128 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800129 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130 }
131
132 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800133 if (is_constructor) {
134 // If we invoked a constructor (which actually returns void), return the receiver,
135 // unless we threw, in which case we return NULL.
136 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700137 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800138 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700139
Elliott Hughes45651fd2012-02-21 15:48:20 -0800140 size_t width = Dbg::GetTagWidth(resultTag);
141 expandBufAdd1(pReply, resultTag);
142 if (width != 0) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800143 WriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700144 }
145 expandBufAdd1(pReply, JT_OBJECT);
146 expandBufAddObjectId(pReply, exceptObjId);
147
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800148 VLOG(jdwp) << " --> returned " << resultTag
149 << StringPrintf(" %#" PRIx64 " (except=%#" PRIx64 ")", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700150
151 /* show detailed debug output */
152 if (resultTag == JT_STRING && exceptObjId == 0) {
153 if (resultValue != 0) {
Sebastien Hertz29259fa2014-09-15 11:27:27 +0200154 if (VLOG_IS_ON(jdwp)) {
155 std::string result_string;
156 JDWP::JdwpError error = Dbg::StringToUtf8(resultValue, &result_string);
157 CHECK_EQ(error, JDWP::ERR_NONE);
158 VLOG(jdwp) << " string '" << result_string << "'";
159 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700160 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800161 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700162 }
163 }
164 }
165
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700166 return err;
167}
168
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800169static JdwpError VM_Version(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes927bd292013-01-17 10:40:13 -0800171 // Text information on runtime version.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700172 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800173 expandBufAddUtf8String(pReply, version);
Elliott Hughes927bd292013-01-17 10:40:13 -0800174
175 // JDWP version numbers, major and minor.
176 expandBufAdd4BE(pReply, 1);
177 expandBufAdd4BE(pReply, 6);
178
179 // "java.version".
180 expandBufAddUtf8String(pReply, "1.6.0");
181
182 // "java.vm.name".
183 expandBufAddUtf8String(pReply, "Dalvik");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700184
185 return ERR_NONE;
186}
187
188/*
189 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
190 * referenceTypeID. We need to send back more than one if the class has
191 * been loaded by multiple class loaders.
192 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800193static JdwpError VM_ClassesBySignature(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800195 std::string classDescriptor(request.ReadUtf8String());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700196
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800197 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800198 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800200 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700201
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800202 for (size_t i = 0; i < ids.size(); ++i) {
203 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800204 JDWP::JdwpTypeTag type_tag;
205 uint32_t class_status;
206 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
207 if (status != ERR_NONE) {
208 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800209 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700210
Elliott Hughes436e3722012-02-17 20:01:47 -0800211 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800212 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800213 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700214 }
215
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700216 return ERR_NONE;
217}
218
219/*
220 * Handle request for the thread IDs of all running threads.
221 *
222 * We exclude ourselves from the list, because we don't allow ourselves
223 * to be suspended, and that violates some JDWP expectations.
224 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800225static JdwpError VM_AllThreads(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700226 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700227 std::vector<ObjectId> thread_ids;
Elliott Hughes026b1462012-06-28 20:43:49 -0700228 Dbg::GetThreads(0, thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700229
Elliott Hughescaf76542012-06-28 16:08:22 -0700230 expandBufAdd4BE(pReply, thread_ids.size());
231 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
232 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700233 }
234
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235 return ERR_NONE;
236}
237
238/*
239 * List all thread groups that do not have a parent.
240 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800241static JdwpError VM_TopLevelThreadGroups(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700243 /*
244 * TODO: maintain a list of parentless thread groups in the VM.
245 *
246 * For now, just return "system". Application threads are created
247 * in "main", which is a child of "system".
248 */
249 uint32_t groups = 1;
250 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700251 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
252 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700253
254 return ERR_NONE;
255}
256
257/*
258 * Respond with the sizes of the basic debugger types.
259 *
260 * All IDs are 8 bytes.
261 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800262static JdwpError VM_IDSizes(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700264 expandBufAdd4BE(pReply, sizeof(FieldId));
265 expandBufAdd4BE(pReply, sizeof(MethodId));
266 expandBufAdd4BE(pReply, sizeof(ObjectId));
267 expandBufAdd4BE(pReply, sizeof(RefTypeId));
268 expandBufAdd4BE(pReply, sizeof(FrameId));
269 return ERR_NONE;
270}
271
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800272static JdwpError VM_Dispose(JdwpState*, Request&, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700273 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes86964332012-02-15 19:37:42 -0800274 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700275 return ERR_NONE;
276}
277
278/*
279 * Suspend the execution of the application running in the VM (i.e. suspend
280 * all threads).
281 *
282 * This needs to increment the "suspend count" on all threads.
283 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800284static JdwpError VM_Suspend(JdwpState*, Request&, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700285 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800286 Thread* self = Thread::Current();
287 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700288 Dbg::SuspendVM();
jeffhaoa77f0f62012-12-05 17:19:31 -0800289 self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700290 return ERR_NONE;
291}
292
293/*
294 * Resume execution. Decrements the "suspend count" of all threads.
295 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800296static JdwpError VM_Resume(JdwpState*, Request&, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700297 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100298 Dbg::ProcessDelayedFullUndeoptimizations();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700299 Dbg::ResumeVM();
300 return ERR_NONE;
301}
302
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800303static JdwpError VM_Exit(JdwpState* state, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700304 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800305 uint32_t exit_status = request.ReadUnsigned32("exit_status");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800306 state->ExitAfterReplying(exit_status);
307 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308}
309
310/*
311 * Create a new string in the VM and return its ID.
312 *
313 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
314 * string "java.util.Arrays".)
315 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800316static JdwpError VM_CreateString(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800318 std::string str(request.ReadUtf8String());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700319 ObjectId stringId = Dbg::CreateString(str);
320 if (stringId == 0) {
321 return ERR_OUT_OF_MEMORY;
322 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700323 expandBufAddObjectId(pReply, stringId);
324 return ERR_NONE;
325}
326
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800327static JdwpError VM_ClassPaths(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800329 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700330
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800331 std::vector<std::string> class_path;
332 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
333 expandBufAdd4BE(pReply, class_path.size());
334 for (size_t i = 0; i < class_path.size(); ++i) {
335 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700336 }
337
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800338 std::vector<std::string> boot_class_path;
339 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
340 expandBufAdd4BE(pReply, boot_class_path.size());
341 for (size_t i = 0; i < boot_class_path.size(); ++i) {
342 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700343 }
344
345 return ERR_NONE;
346}
347
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800348static JdwpError VM_DisposeObjects(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700349 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800350 size_t object_count = request.ReadUnsigned32("object_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800351 for (size_t i = 0; i < object_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800352 ObjectId object_id = request.ReadObjectId();
353 uint32_t reference_count = request.ReadUnsigned32("reference_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800354 Dbg::DisposeObject(object_id, reference_count);
355 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700356 return ERR_NONE;
357}
358
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800359static JdwpError VM_Capabilities(JdwpState*, Request&, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700360 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200361 expandBufAdd1(reply, true); // canWatchFieldModification
362 expandBufAdd1(reply, true); // canWatchFieldAccess
Elliott Hughes9777ba22013-01-17 09:04:19 -0800363 expandBufAdd1(reply, true); // canGetBytecodes
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800364 expandBufAdd1(reply, true); // canGetSyntheticAttribute
365 expandBufAdd1(reply, true); // canGetOwnedMonitorInfo
Elliott Hughesf9501702013-01-11 11:22:27 -0800366 expandBufAdd1(reply, true); // canGetCurrentContendedMonitor
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800367 expandBufAdd1(reply, true); // canGetMonitorInfo
368 return ERR_NONE;
369}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700370
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800371static JdwpError VM_CapabilitiesNew(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800372 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800373 // The first few capabilities are the same as those reported by the older call.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800374 VM_Capabilities(NULL, request, reply);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800375
376 expandBufAdd1(reply, false); // canRedefineClasses
377 expandBufAdd1(reply, false); // canAddMethod
378 expandBufAdd1(reply, false); // canUnrestrictedlyRedefineClasses
379 expandBufAdd1(reply, false); // canPopFrames
Sebastien Hertz5f4e6f52014-04-11 12:07:41 +0200380 expandBufAdd1(reply, true); // canUseInstanceFilters
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800381 expandBufAdd1(reply, false); // canGetSourceDebugExtension
382 expandBufAdd1(reply, false); // canRequestVMDeathEvent
383 expandBufAdd1(reply, false); // canSetDefaultStratum
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800384 expandBufAdd1(reply, true); // 1.6: canGetInstanceInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800385 expandBufAdd1(reply, false); // 1.6: canRequestMonitorEvents
Elliott Hughes734b8c62013-01-11 15:32:45 -0800386 expandBufAdd1(reply, true); // 1.6: canGetMonitorFrameInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800387 expandBufAdd1(reply, false); // 1.6: canUseSourceNameFilters
388 expandBufAdd1(reply, false); // 1.6: canGetConstantPool
389 expandBufAdd1(reply, false); // 1.6: canForceEarlyReturn
390
391 // Fill in reserved22 through reserved32; note count started at 1.
392 for (size_t i = 22; i <= 32; ++i) {
393 expandBufAdd1(reply, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700394 }
395 return ERR_NONE;
396}
397
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700398static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700399 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800400 std::vector<JDWP::RefTypeId> classes;
401 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700402
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800403 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700404
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800405 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800406 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800407 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800408 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800409 uint32_t class_status;
410 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
411 if (status != ERR_NONE) {
412 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800413 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700414
Elliott Hughes436e3722012-02-17 20:01:47 -0800415 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800416 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800417 if (descriptor_and_status) {
418 expandBufAddUtf8String(pReply, descriptor);
419 if (generic) {
420 expandBufAddUtf8String(pReply, genericSignature);
421 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800422 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800423 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424 }
425
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700426 return ERR_NONE;
427}
428
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800429static JdwpError VM_AllClasses(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700430 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700431 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800432}
433
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800434static JdwpError VM_AllClassesWithGeneric(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700435 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700436 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800437}
438
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800439static JdwpError VM_InstanceCounts(JdwpState*, Request& request, ExpandBuf* pReply)
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800440 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800441 int32_t class_count = request.ReadSigned32("class count");
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800442 if (class_count < 0) {
443 return ERR_ILLEGAL_ARGUMENT;
444 }
445 std::vector<RefTypeId> class_ids;
446 for (int32_t i = 0; i < class_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800447 class_ids.push_back(request.ReadRefTypeId());
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800448 }
449
450 std::vector<uint64_t> counts;
451 JdwpError rc = Dbg::GetInstanceCounts(class_ids, counts);
452 if (rc != ERR_NONE) {
453 return rc;
454 }
455
456 expandBufAdd4BE(pReply, counts.size());
457 for (size_t i = 0; i < counts.size(); ++i) {
458 expandBufAdd8BE(pReply, counts[i]);
459 }
460 return ERR_NONE;
461}
462
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800463static JdwpError RT_Modifiers(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700464 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800465 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800466 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700467}
468
469/*
470 * Get values from static fields in a reference type.
471 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800472static JdwpError RT_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700473 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800474 RefTypeId refTypeId = request.ReadRefTypeId();
475 int32_t field_count = request.ReadSigned32("field count");
Elliott Hughes0cf74332012-02-23 23:14:00 -0800476 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800477 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800478 FieldId fieldId = request.ReadFieldId();
Elliott Hughes0cf74332012-02-23 23:14:00 -0800479 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800480 if (status != ERR_NONE) {
481 return status;
482 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700483 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700484 return ERR_NONE;
485}
486
487/*
488 * Get the name of the source file in which a reference type was declared.
489 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800490static JdwpError RT_SourceFile(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700491 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800492 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes03181a82011-11-17 17:22:21 -0800493 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800494 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
495 if (status != ERR_NONE) {
496 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700497 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800498 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800499 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500}
501
502/*
503 * Return the current status of the reference type.
504 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800505static JdwpError RT_Status(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700506 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800507 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800508 JDWP::JdwpTypeTag type_tag;
509 uint32_t class_status;
510 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
511 if (status != ERR_NONE) {
512 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800513 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800514 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700515 return ERR_NONE;
516}
517
518/*
519 * Return interfaces implemented directly by this class.
520 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800521static JdwpError RT_Interfaces(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700522 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800523 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800524 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700525}
526
527/*
528 * Return the class object corresponding to this type.
529 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800530static JdwpError RT_ClassObject(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800532 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -0800533 ObjectId class_object_id;
534 JdwpError status = Dbg::GetClassObject(refTypeId, class_object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800535 if (status != ERR_NONE) {
536 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800537 }
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800538 VLOG(jdwp) << StringPrintf(" --> ObjectId %#" PRIx64, class_object_id);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800539 expandBufAddObjectId(pReply, class_object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700540 return ERR_NONE;
541}
542
543/*
544 * Returns the value of the SourceDebugExtension attribute.
545 *
546 * JDB seems interested, but DEX files don't currently support this.
547 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800548static JdwpError RT_SourceDebugExtension(JdwpState*, Request&, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700549 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700550 /* referenceTypeId in, string out */
551 return ERR_ABSENT_INFORMATION;
552}
553
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800554static JdwpError RT_Signature(JdwpState*, Request& request, ExpandBuf* pReply, bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700555 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800556 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700557
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800558 std::string signature;
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700559 JdwpError status = Dbg::GetSignature(refTypeId, &signature);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800560 if (status != ERR_NONE) {
561 return status;
562 }
563 expandBufAddUtf8String(pReply, signature);
564 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800565 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700566 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700567 return ERR_NONE;
568}
569
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800570static JdwpError RT_Signature(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700571 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800572 return RT_Signature(state, request, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800573}
574
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800575static JdwpError RT_SignatureWithGeneric(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700576 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800577 return RT_Signature(state, request, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800578}
579
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700580/*
581 * Return the instance of java.lang.ClassLoader that loaded the specified
582 * reference type, or null if it was loaded by the system loader.
583 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800584static JdwpError RT_ClassLoader(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700585 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800586 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800587 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700588}
589
590/*
591 * Given a referenceTypeId, return a block of stuff that describes the
592 * fields declared by a class.
593 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800594static JdwpError RT_FieldsWithGeneric(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700595 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800596 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800597 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800598}
599
600// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800601static JdwpError RT_Fields(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700602 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800603 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800604 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700605}
606
607/*
608 * Given a referenceTypeID, return a block of goodies describing the
609 * methods declared by a class.
610 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800611static JdwpError RT_MethodsWithGeneric(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700612 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800613 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800614 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800615}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700616
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800617// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800618static JdwpError RT_Methods(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700619 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800620 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800621 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622}
623
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800624static JdwpError RT_Instances(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes3b78c942013-01-15 17:35:41 -0800625 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800626 RefTypeId class_id = request.ReadRefTypeId();
627 int32_t max_count = request.ReadSigned32("max count");
Elliott Hughes3b78c942013-01-15 17:35:41 -0800628 if (max_count < 0) {
629 return ERR_ILLEGAL_ARGUMENT;
630 }
631
632 std::vector<ObjectId> instances;
633 JdwpError rc = Dbg::GetInstances(class_id, max_count, instances);
634 if (rc != ERR_NONE) {
635 return rc;
636 }
637
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800638 return WriteTaggedObjectList(reply, instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800639}
640
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700641/*
642 * Return the immediate superclass of a class.
643 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800644static JdwpError CT_Superclass(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700645 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800646 RefTypeId class_id = request.ReadRefTypeId();
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800647 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700648 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800649 if (status != ERR_NONE) {
650 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800651 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700652 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700653 return ERR_NONE;
654}
655
656/*
657 * Set static class values.
658 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800659static JdwpError CT_SetValues(JdwpState* , Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700660 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800661 RefTypeId class_id = request.ReadRefTypeId();
662 int32_t values_count = request.ReadSigned32("values count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700663
Elliott Hughesa96836a2013-01-17 12:27:49 -0800664 UNUSED(class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700665
Elliott Hughesa96836a2013-01-17 12:27:49 -0800666 for (int32_t i = 0; i < values_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800667 FieldId fieldId = request.ReadFieldId();
Elliott Hughesaed4be92011-12-02 16:16:23 -0800668 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800669 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800670 uint64_t value = request.ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700671
Elliott Hughesa96836a2013-01-17 12:27:49 -0800672 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " --> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800673 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
674 if (status != ERR_NONE) {
675 return status;
676 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700677 }
678
679 return ERR_NONE;
680}
681
682/*
683 * Invoke a static method.
684 *
685 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
686 * values in the "variables" display.
687 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800688static JdwpError CT_InvokeMethod(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700689 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800690 RefTypeId class_id = request.ReadRefTypeId();
691 ObjectId thread_id = request.ReadThreadId();
692 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700693
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800694 return FinishInvoke(state, request, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700695}
696
697/*
698 * Create a new object of the requested type, and invoke the specified
699 * constructor.
700 *
701 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
702 * see the contents of a byte[] as a string.
703 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800704static JdwpError CT_NewInstance(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700705 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800706 RefTypeId class_id = request.ReadRefTypeId();
707 ObjectId thread_id = request.ReadThreadId();
708 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700709
Elliott Hughes74847412012-06-20 18:10:21 -0700710 ObjectId object_id;
711 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800712 if (status != ERR_NONE) {
713 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800714 }
Elliott Hughes74847412012-06-20 18:10:21 -0700715 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700716 return ERR_OUT_OF_MEMORY;
717 }
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800718 return FinishInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700719}
720
721/*
722 * Create a new array object of the requested type and length.
723 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800724static JdwpError AT_newInstance(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700725 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800726 RefTypeId arrayTypeId = request.ReadRefTypeId();
727 int32_t length = request.ReadSigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700728
Elliott Hughes74847412012-06-20 18:10:21 -0700729 ObjectId object_id;
730 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800731 if (status != ERR_NONE) {
732 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800733 }
Elliott Hughes74847412012-06-20 18:10:21 -0700734 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700735 return ERR_OUT_OF_MEMORY;
736 }
737 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700738 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700739 return ERR_NONE;
740}
741
742/*
743 * Return line number information for the method, if present.
744 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800745static JdwpError M_LineTable(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700746 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800747 RefTypeId refTypeId = request.ReadRefTypeId();
748 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700749
Elliott Hughes74847412012-06-20 18:10:21 -0700750 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700751
752 return ERR_NONE;
753}
754
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800755static JdwpError M_VariableTable(JdwpState*, Request& request, ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700756 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700757 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800758 RefTypeId class_id = request.ReadRefTypeId();
759 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700760
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800761 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
762 // information. That will cause Eclipse to make a best-effort attempt at displaying local
763 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
764 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700765 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700766 return ERR_NONE;
767}
768
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800769static JdwpError M_VariableTable(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700770 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800771 return M_VariableTable(state, request, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800772}
773
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800774static JdwpError M_VariableTableWithGeneric(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700775 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800776 return M_VariableTable(state, request, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800777}
778
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800779static JdwpError M_Bytecodes(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes9777ba22013-01-17 09:04:19 -0800780 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800781 RefTypeId class_id = request.ReadRefTypeId();
782 MethodId method_id = request.ReadMethodId();
Elliott Hughes9777ba22013-01-17 09:04:19 -0800783
784 std::vector<uint8_t> bytecodes;
785 JdwpError rc = Dbg::GetBytecodes(class_id, method_id, bytecodes);
786 if (rc != ERR_NONE) {
787 return rc;
788 }
789
790 expandBufAdd4BE(reply, bytecodes.size());
791 for (size_t i = 0; i < bytecodes.size(); ++i) {
792 expandBufAdd1(reply, bytecodes[i]);
793 }
794
795 return ERR_NONE;
796}
797
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700798/*
799 * Given an object reference, return the runtime type of the object
800 * (class or array).
801 *
Elliott Hughes74847412012-06-20 18:10:21 -0700802 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700803 * passed in here.
804 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800805static JdwpError OR_ReferenceType(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700806 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800807 ObjectId object_id = request.ReadObjectId();
Elliott Hughes74847412012-06-20 18:10:21 -0700808 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700809}
810
811/*
812 * Get values from the fields of an object.
813 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800814static JdwpError OR_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700815 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800816 ObjectId object_id = request.ReadObjectId();
817 int32_t field_count = request.ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700818
Elliott Hughes0cf74332012-02-23 23:14:00 -0800819 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800820 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800821 FieldId fieldId = request.ReadFieldId();
Elliott Hughes74847412012-06-20 18:10:21 -0700822 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800823 if (status != ERR_NONE) {
824 return status;
825 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700826 }
827
828 return ERR_NONE;
829}
830
831/*
832 * Set values in the fields of an object.
833 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800834static JdwpError OR_SetValues(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700835 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800836 ObjectId object_id = request.ReadObjectId();
837 int32_t field_count = request.ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700838
Elliott Hughesa96836a2013-01-17 12:27:49 -0800839 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800840 FieldId fieldId = request.ReadFieldId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700841
Elliott Hughesaed4be92011-12-02 16:16:23 -0800842 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800843 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800844 uint64_t value = request.ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700845
Elliott Hughes2435a572012-02-17 16:07:41 -0800846 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700847 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800848 if (status != ERR_NONE) {
849 return status;
850 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700851 }
852
853 return ERR_NONE;
854}
855
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800856static JdwpError OR_MonitorInfo(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughesf327e072013-01-09 16:01:26 -0800857 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800858 ObjectId object_id = request.ReadObjectId();
Elliott Hughesf327e072013-01-09 16:01:26 -0800859 return Dbg::GetMonitorInfo(object_id, reply);
860}
861
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700862/*
863 * Invoke an instance method. The invocation must occur in the specified
864 * thread, which must have been suspended by an event.
865 *
866 * The call is synchronous. All threads in the VM are resumed, unless the
867 * SINGLE_THREADED flag is set.
868 *
869 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
870 * object), it will try to invoke the object's toString() function. This
871 * feature becomes crucial when examining ArrayLists with Eclipse.
872 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800873static JdwpError OR_InvokeMethod(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700874 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800875 ObjectId object_id = request.ReadObjectId();
876 ObjectId thread_id = request.ReadThreadId();
877 RefTypeId class_id = request.ReadRefTypeId();
878 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700879
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800880 return FinishInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700881}
882
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800883static JdwpError OR_DisableCollection(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700884 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800885 ObjectId object_id = request.ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800886 return Dbg::DisableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700887}
888
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800889static JdwpError OR_EnableCollection(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700890 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800891 ObjectId object_id = request.ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800892 return Dbg::EnableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700893}
894
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800895static JdwpError OR_IsCollected(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700896 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800897 ObjectId object_id = request.ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800898 bool is_collected;
899 JdwpError rc = Dbg::IsCollected(object_id, is_collected);
900 expandBufAdd1(pReply, is_collected ? 1 : 0);
901 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700902}
903
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800904static JdwpError OR_ReferringObjects(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800905 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800906 ObjectId object_id = request.ReadObjectId();
907 int32_t max_count = request.ReadSigned32("max count");
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800908 if (max_count < 0) {
909 return ERR_ILLEGAL_ARGUMENT;
910 }
911
912 std::vector<ObjectId> referring_objects;
913 JdwpError rc = Dbg::GetReferringObjects(object_id, max_count, referring_objects);
914 if (rc != ERR_NONE) {
915 return rc;
916 }
917
918 return WriteTaggedObjectList(reply, referring_objects);
919}
920
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700921/*
922 * Return the string value in a string object.
923 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800924static JdwpError SR_Value(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700925 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800926 ObjectId stringObject = request.ReadObjectId();
Sebastien Hertz29259fa2014-09-15 11:27:27 +0200927 std::string str;
928 JDWP::JdwpError error = Dbg::StringToUtf8(stringObject, &str);
929 if (error != JDWP::ERR_NONE) {
930 return error;
931 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700932
Ian Rogerscc2f2392014-08-29 20:19:11 -0700933 VLOG(jdwp) << StringPrintf(" --> %s", PrintableString(str.c_str()).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700934
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800935 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700936
937 return ERR_NONE;
938}
939
940/*
941 * Return a thread's name.
942 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800943static JdwpError TR_Name(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700944 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800945 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700946
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800947 std::string name;
Elliott Hughes221229c2013-01-08 18:17:50 -0800948 JdwpError error = Dbg::GetThreadName(thread_id, name);
949 if (error != ERR_NONE) {
950 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700951 }
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800952 VLOG(jdwp) << StringPrintf(" Name of thread %#" PRIx64 " is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800953 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700954
955 return ERR_NONE;
956}
957
958/*
959 * Suspend the specified thread.
960 *
961 * It's supposed to remain suspended even if interpreted code wants to
962 * resume it; only the JDI is allowed to resume it.
963 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800964static JdwpError TR_Suspend(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700965 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800966 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700967
Elliott Hughes74847412012-06-20 18:10:21 -0700968 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700969 LOG(INFO) << " Warning: ignoring request to suspend self";
970 return ERR_THREAD_NOT_SUSPENDED;
971 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800972
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700973 Thread* self = Thread::Current();
974 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
975 JdwpError result = Dbg::SuspendThread(thread_id);
976 self->TransitionFromSuspendedToRunnable();
977 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700978}
979
980/*
981 * Resume the specified thread.
982 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800983static JdwpError TR_Resume(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700984 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800985 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700986
Elliott Hughes74847412012-06-20 18:10:21 -0700987 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700988 LOG(INFO) << " Warning: ignoring request to resume self";
989 return ERR_NONE;
990 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800991
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100992 Dbg::ProcessDelayedFullUndeoptimizations();
993
Elliott Hughes74847412012-06-20 18:10:21 -0700994 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995 return ERR_NONE;
996}
997
998/*
999 * Return status of specified thread.
1000 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001001static JdwpError TR_Status(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001002 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001003 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001004
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08001005 JDWP::JdwpThreadStatus threadStatus;
1006 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -08001007 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
1008 if (error != ERR_NONE) {
1009 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001010 }
1011
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001012 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001013
1014 expandBufAdd4BE(pReply, threadStatus);
1015 expandBufAdd4BE(pReply, suspendStatus);
1016
1017 return ERR_NONE;
1018}
1019
1020/*
1021 * Return the thread group that the specified thread is a member of.
1022 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001023static JdwpError TR_ThreadGroup(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001024 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001025 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001026 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001027}
1028
1029/*
1030 * Return the current call stack of a suspended thread.
1031 *
1032 * If the thread isn't suspended, the error code isn't defined, but should
1033 * be THREAD_NOT_SUSPENDED.
1034 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001035static JdwpError TR_Frames(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001036 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001037 ObjectId thread_id = request.ReadThreadId();
1038 uint32_t start_frame = request.ReadUnsigned32("start frame");
1039 uint32_t length = request.ReadUnsigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001040
Elliott Hughes221229c2013-01-08 18:17:50 -08001041 size_t actual_frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001042 JdwpError error = Dbg::GetThreadFrameCount(thread_id, actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001043 if (error != ERR_NONE) {
1044 return error;
1045 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001046
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001047 if (actual_frame_count <= 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001048 return ERR_THREAD_NOT_SUSPENDED; // 0 means no managed frames (which means "in native").
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001049 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001050
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001051 if (start_frame > actual_frame_count) {
1052 return ERR_INVALID_INDEX;
1053 }
1054 if (length == static_cast<uint32_t>(-1)) {
1055 length = actual_frame_count - start_frame;
1056 }
1057 if (start_frame + length > actual_frame_count) {
1058 return ERR_INVALID_LENGTH;
1059 }
1060
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001061 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001062}
1063
1064/*
1065 * Returns the #of frames on the specified thread, which must be suspended.
1066 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001067static JdwpError TR_FrameCount(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001068 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001069 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001070
Elliott Hughes221229c2013-01-08 18:17:50 -08001071 size_t frame_count;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001072 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, frame_count);
1073 if (rc != ERR_NONE) {
1074 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001075 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001076 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001077
1078 return ERR_NONE;
1079}
1080
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001081static JdwpError TR_OwnedMonitors(Request& request, ExpandBuf* reply, bool with_stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001082 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001083 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001084
1085 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001086 std::vector<uint32_t> stack_depths;
1087 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, monitors, stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001088 if (rc != ERR_NONE) {
1089 return rc;
1090 }
1091
1092 expandBufAdd4BE(reply, monitors.size());
1093 for (size_t i = 0; i < monitors.size(); ++i) {
1094 rc = WriteTaggedObject(reply, monitors[i]);
1095 if (rc != ERR_NONE) {
1096 return rc;
1097 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001098 if (with_stack_depths) {
1099 expandBufAdd4BE(reply, stack_depths[i]);
1100 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001101 }
1102 return ERR_NONE;
1103}
1104
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001105static JdwpError TR_OwnedMonitors(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001107 return TR_OwnedMonitors(request, reply, false);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001108}
1109
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001110static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001112 return TR_OwnedMonitors(request, reply, true);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001113}
1114
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001115static JdwpError TR_CurrentContendedMonitor(JdwpState*, Request& request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001117 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001118
Elliott Hughesf9501702013-01-11 11:22:27 -08001119 ObjectId contended_monitor;
1120 JdwpError rc = Dbg::GetContendedMonitor(thread_id, contended_monitor);
1121 if (rc != ERR_NONE) {
1122 return rc;
1123 }
1124 return WriteTaggedObject(reply, contended_monitor);
1125}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001126
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001127static JdwpError TR_Interrupt(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughesf9501702013-01-11 11:22:27 -08001128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001129 ObjectId thread_id = request.ReadThreadId();
Elliott Hughesf9501702013-01-11 11:22:27 -08001130 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001131}
1132
1133/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001134 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001135 *
1136 * (The thread *might* still be running -- it might not have examined
1137 * its suspend count recently.)
1138 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001139static JdwpError TR_DebugSuspendCount(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001141 ObjectId thread_id = request.ReadThreadId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001143}
1144
1145/*
1146 * Return the name of a thread group.
1147 *
1148 * The Eclipse debugger recognizes "main" and "system" as special.
1149 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001150static JdwpError TGR_Name(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001152 ObjectId thread_group_id = request.ReadThreadGroupId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001153
Elliott Hughescaf76542012-06-28 16:08:22 -07001154 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001155
1156 return ERR_NONE;
1157}
1158
1159/*
1160 * Returns the thread group -- if any -- that contains the specified
1161 * thread group.
1162 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001163static JdwpError TGR_Parent(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001165 ObjectId thread_group_id = request.ReadThreadGroupId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001166
Elliott Hughescaf76542012-06-28 16:08:22 -07001167 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001168 expandBufAddObjectId(pReply, parentGroup);
1169
1170 return ERR_NONE;
1171}
1172
1173/*
1174 * Return the active threads and thread groups that are part of the
1175 * specified thread group.
1176 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001177static JdwpError TGR_Children(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001179 ObjectId thread_group_id = request.ReadThreadGroupId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001180
Elliott Hughescaf76542012-06-28 16:08:22 -07001181 std::vector<ObjectId> thread_ids;
1182 Dbg::GetThreads(thread_group_id, thread_ids);
1183 expandBufAdd4BE(pReply, thread_ids.size());
1184 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1185 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001186 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001187
Elliott Hughescaf76542012-06-28 16:08:22 -07001188 std::vector<ObjectId> child_thread_groups_ids;
1189 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1190 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1191 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1192 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001193 }
1194
1195 return ERR_NONE;
1196}
1197
1198/*
1199 * Return the #of components in the array.
1200 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001201static JdwpError AR_Length(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001203 ObjectId array_id = request.ReadArrayId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001204
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001205 int length;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001206 JdwpError status = Dbg::GetArrayLength(array_id, length);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001207 if (status != ERR_NONE) {
1208 return status;
1209 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001210 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001211
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001212 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001213
1214 return ERR_NONE;
1215}
1216
1217/*
1218 * Return the values from an array.
1219 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001220static JdwpError AR_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001222 ObjectId array_id = request.ReadArrayId();
1223 uint32_t offset = request.ReadUnsigned32("offset");
1224 uint32_t length = request.ReadUnsigned32("length");
Elliott Hughesa96836a2013-01-17 12:27:49 -08001225 return Dbg::OutputArray(array_id, offset, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001226}
1227
1228/*
1229 * Set values in an array.
1230 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001231static JdwpError AR_SetValues(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001233 ObjectId array_id = request.ReadArrayId();
1234 uint32_t offset = request.ReadUnsigned32("offset");
1235 uint32_t count = request.ReadUnsigned32("count");
1236 return Dbg::SetArrayElements(array_id, offset, count, request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001237}
1238
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001239static JdwpError CLR_VisibleClasses(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001241 request.ReadObjectId(); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001242 // TODO: we should only return classes which have the given class loader as a defining or
1243 // initiating loader. The former would be easy; the latter is hard, because we don't have
1244 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001245 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001246}
1247
1248/*
1249 * Set an event trigger.
1250 *
1251 * Reply with a requestID.
1252 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001253static JdwpError ER_Set(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001254 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001255 JdwpEventKind event_kind = request.ReadEnum1<JdwpEventKind>("event kind");
1256 JdwpSuspendPolicy suspend_policy = request.ReadEnum1<JdwpSuspendPolicy>("suspend policy");
1257 int32_t modifier_count = request.ReadSigned32("modifier count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001258
Elliott Hughesa96836a2013-01-17 12:27:49 -08001259 CHECK_LT(modifier_count, 256); /* reasonableness check */
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001260
Elliott Hughesa96836a2013-01-17 12:27:49 -08001261 JdwpEvent* pEvent = EventAlloc(modifier_count);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001262 pEvent->eventKind = event_kind;
1263 pEvent->suspend_policy = suspend_policy;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001264 pEvent->modCount = modifier_count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001265
1266 /*
1267 * Read modifiers. Ordering may be significant (see explanation of Count
1268 * mods in JDWP doc).
1269 */
Elliott Hughesa96836a2013-01-17 12:27:49 -08001270 for (int32_t i = 0; i < modifier_count; ++i) {
Elliott Hughes972a47b2012-02-21 18:16:06 -08001271 JdwpEventMod& mod = pEvent->mods[i];
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001272 mod.modKind = request.ReadModKind();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001273 switch (mod.modKind) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001274 case MK_COUNT:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001275 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001276 // Report once, when "--count" reaches 0.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001277 uint32_t count = request.ReadUnsigned32("count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001278 if (count == 0) {
1279 return ERR_INVALID_COUNT;
1280 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001281 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001282 }
1283 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001284 case MK_CONDITIONAL:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001285 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001286 // Conditional on expression.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001287 uint32_t exprId = request.ReadUnsigned32("expr id");
Elliott Hughes972a47b2012-02-21 18:16:06 -08001288 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001289 }
1290 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001291 case MK_THREAD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001292 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001293 // Only report events in specified thread.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001294 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001295 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001296 }
1297 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001298 case MK_CLASS_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001299 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001300 // For ClassPrepare, MethodEntry.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001301 RefTypeId class_id = request.ReadRefTypeId();
Elliott Hughes74847412012-06-20 18:10:21 -07001302 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001303 }
1304 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001305 case MK_CLASS_MATCH:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001306 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001307 // Restrict events to matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001308 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001309 std::string pattern(request.ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001310 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001311 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001312 }
1313 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001314 case MK_CLASS_EXCLUDE:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001315 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001316 // Restrict events to non-matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001317 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001318 std::string pattern(request.ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001319 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001320 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001321 }
1322 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001323 case MK_LOCATION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001324 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001325 // Restrict certain events based on location.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001326 JdwpLocation location = request.ReadLocation();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001327 mod.locationOnly.loc = location;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001328 }
1329 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001330 case MK_EXCEPTION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001331 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001332 // Modifies EK_EXCEPTION events,
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001333 mod.exceptionOnly.refTypeId = request.ReadRefTypeId(); // null => all exceptions.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001334 mod.exceptionOnly.caught = request.ReadEnum1<uint8_t>("caught");
1335 mod.exceptionOnly.uncaught = request.ReadEnum1<uint8_t>("uncaught");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001336 }
1337 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001338 case MK_FIELD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001339 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001340 // For field access/modification events.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001341 RefTypeId declaring = request.ReadRefTypeId();
1342 FieldId fieldId = request.ReadFieldId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001343 mod.fieldOnly.refTypeId = declaring;
1344 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001345 }
1346 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001347 case MK_STEP:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001348 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001349 // For use with EK_SINGLE_STEP.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001350 ObjectId thread_id = request.ReadThreadId();
1351 uint32_t size = request.ReadUnsigned32("step size");
1352 uint32_t depth = request.ReadUnsigned32("step depth");
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001353 VLOG(jdwp) << StringPrintf(" Step: thread=%#" PRIx64, thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001354 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1355
Elliott Hughes74847412012-06-20 18:10:21 -07001356 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001357 mod.step.size = size;
1358 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001359 }
1360 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001361 case MK_INSTANCE_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001362 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001363 // Report events related to a specific object.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001364 ObjectId instance = request.ReadObjectId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001365 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001366 }
1367 break;
1368 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001369 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001370 break;
1371 }
1372 }
1373
1374 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001375 * We reply with an integer "requestID".
1376 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001377 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001378 expandBufAdd4BE(pReply, requestId);
1379
1380 pEvent->requestId = requestId;
1381
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001382 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001383
1384 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001385 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001386 if (err != ERR_NONE) {
1387 /* registration failed, probably because event is bogus */
1388 EventFree(pEvent);
1389 LOG(WARNING) << "WARNING: event request rejected";
1390 }
1391 return err;
1392}
1393
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001394static JdwpError ER_Clear(JdwpState* state, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001396 request.ReadEnum1<JdwpEventKind>("event kind");
1397 uint32_t requestId = request.ReadUnsigned32("request id");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001398
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001399 // Failure to find an event with a matching ID is a no-op
1400 // and does not return an error.
Elliott Hughes761928d2011-11-16 18:33:03 -08001401 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001402 return ERR_NONE;
1403}
1404
1405/*
1406 * Return the values of arguments and local variables.
1407 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001408static JdwpError SF_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001409 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001410 ObjectId thread_id = request.ReadThreadId();
1411 FrameId frame_id = request.ReadFrameId();
1412 int32_t slot_count = request.ReadSigned32("slot count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001413
Elliott Hughesa96836a2013-01-17 12:27:49 -08001414 expandBufAdd4BE(pReply, slot_count); /* "int values" */
1415 for (int32_t i = 0; i < slot_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001416 uint32_t slot = request.ReadUnsigned32("slot");
1417 JDWP::JdwpTag reqSigByte = request.ReadTag();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001418
Elliott Hughes2435a572012-02-17 16:07:41 -08001419 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001420
Elliott Hughesdbb40792011-11-18 17:05:22 -08001421 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001422 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001423 JdwpError error = Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
1424 if (error != ERR_NONE) {
1425 return error;
1426 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001427 }
1428
1429 return ERR_NONE;
1430}
1431
1432/*
1433 * Set the values of arguments and local variables.
1434 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001435static JdwpError SF_SetValues(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001436 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001437 ObjectId thread_id = request.ReadThreadId();
1438 FrameId frame_id = request.ReadFrameId();
1439 int32_t slot_count = request.ReadSigned32("slot count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001440
Elliott Hughesa96836a2013-01-17 12:27:49 -08001441 for (int32_t i = 0; i < slot_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001442 uint32_t slot = request.ReadUnsigned32("slot");
1443 JDWP::JdwpTag sigByte = request.ReadTag();
Elliott Hughesdbb40792011-11-18 17:05:22 -08001444 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001445 uint64_t value = request.ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001446
Elliott Hughes2435a572012-02-17 16:07:41 -08001447 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001448 JdwpError error = Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
1449 if (error != ERR_NONE) {
1450 return error;
1451 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001452 }
1453
1454 return ERR_NONE;
1455}
1456
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001457static JdwpError SF_ThisObject(JdwpState*, Request& request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001458 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001459 ObjectId thread_id = request.ReadThreadId();
1460 FrameId frame_id = request.ReadFrameId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001461
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001462 ObjectId object_id;
1463 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001464 if (rc != ERR_NONE) {
1465 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001466 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001467
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001468 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001469}
1470
1471/*
1472 * Return the reference type reflected by this class object.
1473 *
1474 * This appears to be required because ReferenceTypeId values are NEVER
1475 * reused, whereas ClassIds can be recycled like any other object. (Either
1476 * that, or I have no idea what this is for.)
1477 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001478static JdwpError COR_ReflectedType(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001480 RefTypeId class_object_id = request.ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001481 return Dbg::GetReflectedType(class_object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001482}
1483
1484/*
1485 * Handle a DDM packet with a single chunk in it.
1486 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001487static JdwpError DDM_Chunk(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001488 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001489 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001490 uint8_t* replyBuf = NULL;
1491 int replyLen = -1;
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001492 if (Dbg::DdmHandlePacket(request, &replyBuf, &replyLen)) {
1493 // If they want to send something back, we copy it into the buffer.
1494 // TODO: consider altering the JDWP stuff to hold the packet header
1495 // in a separate buffer. That would allow us to writev() DDM traffic
1496 // instead of copying it into the expanding buffer. The reduction in
1497 // heap requirements is probably more valuable than the efficiency.
1498 CHECK_GT(replyLen, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001499 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1500 free(replyBuf);
1501 }
1502 return ERR_NONE;
1503}
1504
1505/*
1506 * Handler map decl.
1507 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001508typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, Request& request, ExpandBuf* reply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001509
1510struct JdwpHandlerMap {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001511 uint8_t cmdSet;
1512 uint8_t cmd;
1513 JdwpRequestHandler func;
1514 const char* name;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001515};
1516
1517/*
1518 * Map commands to functions.
1519 *
1520 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1521 * and 128-256 are vendor-defined.
1522 */
Elliott Hughescb693062013-02-21 09:48:08 -08001523static const JdwpHandlerMap gHandlers[] = {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001524 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001525 { 1, 1, VM_Version, "VirtualMachine.Version" },
1526 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1527 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1528 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1529 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1530 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1531 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1532 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1533 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1534 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1535 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1536 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1537 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1538 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1539 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1540 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
1541 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1542 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1543 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001544 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001545 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001546
1547 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001548 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1549 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1550 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1551 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1552 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1553 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1554 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
1555 { 2, 8, NULL, "ReferenceType.NestedTypes" },
1556 { 2, 9, RT_Status, "ReferenceType.Status" },
1557 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1558 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001559 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1560 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001561 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1562 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughes3b78c942013-01-15 17:35:41 -08001563 { 2, 16, RT_Instances, "ReferenceType.Instances" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001564 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1565 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001566
1567 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001568 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1569 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1570 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1571 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001572
1573 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001574 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001575
1576 /* InterfaceType command set (5) */
1577
1578 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001579 { 6, 1, M_LineTable, "Method.LineTable" },
1580 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughes9777ba22013-01-17 09:04:19 -08001581 { 6, 3, M_Bytecodes, "Method.Bytecodes" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001582 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001583 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001584
1585 /* Field command set (8) */
1586
1587 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001588 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1589 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1590 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
1591 { 9, 4, NULL, "ObjectReference.UNUSED" },
1592 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1593 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001594 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001595 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1596 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001597 { 9, 10, OR_ReferringObjects, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001598
1599 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001600 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001601
1602 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001603 { 11, 1, TR_Name, "ThreadReference.Name" },
1604 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1605 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1606 { 11, 4, TR_Status, "ThreadReference.Status" },
1607 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1608 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1609 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1610 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1611 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1612 { 11, 10, NULL, "ThreadReference.Stop" },
1613 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1614 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1615 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1616 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001617
1618 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001619 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1620 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1621 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001622
1623 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001624 { 13, 1, AR_Length, "ArrayReference.Length" },
1625 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1626 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001627
1628 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001629 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001630
1631 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001632 { 15, 1, ER_Set, "EventRequest.Set" },
1633 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001634 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001635
1636 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001637 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1638 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1639 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001640 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001641
1642 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001643 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001644
1645 /* Event command set (64) */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001646 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001647
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001648 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001649};
1650
Elliott Hughescb693062013-02-21 09:48:08 -08001651static const char* GetCommandName(Request& request) {
1652 for (size_t i = 0; i < arraysize(gHandlers); ++i) {
1653 if (gHandlers[i].cmdSet == request.GetCommandSet() && gHandlers[i].cmd == request.GetCommand()) {
1654 return gHandlers[i].name;
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001655 }
1656 }
1657 return "?UNKNOWN?";
1658}
1659
Elliott Hughescb693062013-02-21 09:48:08 -08001660static std::string DescribeCommand(Request& request) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001661 std::string result;
Elliott Hughes64f574f2013-02-20 14:57:12 -08001662 result += "REQUEST: ";
Elliott Hughescb693062013-02-21 09:48:08 -08001663 result += GetCommandName(request);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001664 result += StringPrintf(" (length=%zu id=0x%06x)", request.GetLength(), request.GetId());
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001665 return result;
1666}
1667
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001668/*
1669 * Process a request from the debugger.
1670 *
1671 * On entry, the JDWP thread is in VMWAIT.
1672 */
Sebastien Hertz43c8d722014-03-18 12:19:52 +01001673size_t JdwpState::ProcessRequest(Request& request, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001674 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001675
Elliott Hughescb693062013-02-21 09:48:08 -08001676 if (request.GetCommandSet() != kJDWPDdmCmdSet) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001677 /*
1678 * Activity from a debugger, not merely ddms. Mark us as having an
1679 * active debugger session, and zero out the last-activity timestamp
1680 * so waitForDebugger() doesn't return if we stall for a bit here.
1681 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001682 Dbg::GoActive();
Ian Rogers37f3c962014-07-17 11:25:30 -07001683 last_activity_time_ms_.StoreSequentiallyConsistent(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001684 }
1685
1686 /*
1687 * If a debugger event has fired in another thread, wait until the
1688 * initiating thread has suspended itself before processing messages
1689 * from the debugger. Otherwise we (the JDWP thread) could be told to
1690 * resume the thread before it has suspended.
1691 *
1692 * We call with an argument of zero to wait for the current event
1693 * thread to finish, and then clear the block. Depending on the thread
1694 * suspend policy, this may allow events in other threads to fire,
1695 * but those events have no bearing on what the debugger has sent us
1696 * in the current request.
1697 *
1698 * Note that we MUST clear the event token before waking the event
1699 * thread up, or risk waiting for the thread to suspend after we've
1700 * told it to resume.
1701 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001702 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001703
1704 /*
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001705 * We do not want events to be sent while we process a request. Indicate the JDWP thread starts
1706 * to process a request so other threads wait for it to finish before sending an event.
1707 */
1708 StartProcessingRequest();
1709
1710 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001711 * Tell the VM that we're running and shouldn't be interrupted by GC.
1712 * Do this after anything that can stall indefinitely.
1713 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001714 Thread* self = Thread::Current();
1715 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001716
1717 expandBufAddSpace(pReply, kJDWPHeaderLen);
1718
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001719 size_t i;
Elliott Hughescb693062013-02-21 09:48:08 -08001720 for (i = 0; i < arraysize(gHandlers); ++i) {
1721 if (gHandlers[i].cmdSet == request.GetCommandSet() && gHandlers[i].cmd == request.GetCommand() && gHandlers[i].func != NULL) {
1722 VLOG(jdwp) << DescribeCommand(request);
1723 result = (*gHandlers[i].func)(this, request, pReply);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001724 if (result == ERR_NONE) {
1725 request.CheckConsumed();
1726 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001727 break;
1728 }
1729 }
Elliott Hughescb693062013-02-21 09:48:08 -08001730 if (i == arraysize(gHandlers)) {
1731 LOG(ERROR) << "Command not implemented: " << DescribeCommand(request);
Ian Rogers43b2e0f2014-01-30 16:58:39 -08001732 LOG(ERROR) << HexDump(request.data(), request.size(), false, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001733 result = ERR_NOT_IMPLEMENTED;
1734 }
1735
1736 /*
1737 * Set up the reply header.
1738 *
1739 * If we encountered an error, only send the header back.
1740 */
1741 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Sebastien Hertz43c8d722014-03-18 12:19:52 +01001742 size_t replyLength = (result == ERR_NONE) ? expandBufGetLength(pReply) : kJDWPHeaderLen;
1743 Set4BE(replyBuf + 0, replyLength);
Elliott Hughescb693062013-02-21 09:48:08 -08001744 Set4BE(replyBuf + 4, request.GetId());
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001745 Set1(replyBuf + 8, kJDWPFlagReply);
1746 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001747
Elliott Hughescb693062013-02-21 09:48:08 -08001748 CHECK_GT(expandBufGetLength(pReply), 0U) << GetCommandName(request) << " " << request.GetId();
1749
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001750 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughescb693062013-02-21 09:48:08 -08001751 VLOG(jdwp) << "REPLY: " << GetCommandName(request) << " " << result << " (length=" << respLen << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001752 if (false) {
Ian Rogers43b2e0f2014-01-30 16:58:39 -08001753 VLOG(jdwp) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen, false, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001754 }
1755
Elliott Hughescb693062013-02-21 09:48:08 -08001756 VLOG(jdwp) << "----------";
1757
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001758 /*
1759 * Update last-activity timestamp. We really only need this during
1760 * the initial setup. Only update if this is a non-DDMS packet.
1761 */
Elliott Hughescb693062013-02-21 09:48:08 -08001762 if (request.GetCommandSet() != kJDWPDdmCmdSet) {
Ian Rogers37f3c962014-07-17 11:25:30 -07001763 last_activity_time_ms_.StoreSequentiallyConsistent(MilliTime());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001764 }
1765
1766 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001767 self->TransitionFromRunnableToSuspended(old_state);
Sebastien Hertz43c8d722014-03-18 12:19:52 +01001768
1769 return replyLength;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001770}
1771
Sebastien Hertz99660e12014-02-19 15:04:42 +01001772/*
1773 * Indicates a request is about to be processed. If a thread wants to send an event in the meantime,
1774 * it will need to wait until we processed this request (see EndProcessingRequest).
1775 */
1776void JdwpState::StartProcessingRequest() {
1777 Thread* self = Thread::Current();
1778 CHECK_EQ(self, GetDebugThread()) << "Requests are only processed by debug thread";
1779 MutexLock mu(self, process_request_lock_);
1780 CHECK_EQ(processing_request_, false);
1781 processing_request_ = true;
1782}
1783
1784/*
1785 * Indicates a request has been processed (and we sent its reply). All threads waiting for us (see
1786 * WaitForProcessingRequest) are waken up so they can send events again.
1787 */
1788void JdwpState::EndProcessingRequest() {
1789 Thread* self = Thread::Current();
1790 CHECK_EQ(self, GetDebugThread()) << "Requests are only processed by debug thread";
1791 MutexLock mu(self, process_request_lock_);
1792 CHECK_EQ(processing_request_, true);
1793 processing_request_ = false;
1794 process_request_cond_.Broadcast(self);
1795}
1796
1797/*
1798 * Waits for any request being processed so we do not send an event in the meantime.
1799 */
1800void JdwpState::WaitForProcessingRequest() {
1801 Thread* self = Thread::Current();
1802 CHECK_NE(self, GetDebugThread()) << "Events should not be posted by debug thread";
1803 MutexLock mu(self, process_request_lock_);
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001804 bool waited = false;
Sebastien Hertz99660e12014-02-19 15:04:42 +01001805 while (processing_request_) {
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001806 VLOG(jdwp) << StringPrintf("wait for processing request");
1807 waited = true;
Sebastien Hertz99660e12014-02-19 15:04:42 +01001808 process_request_cond_.Wait(self);
1809 }
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001810 if (waited) {
1811 VLOG(jdwp) << StringPrintf("finished waiting for processing request");
1812 }
Sebastien Hertz99660e12014-02-19 15:04:42 +01001813 CHECK_EQ(processing_request_, false);
1814}
1815
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001816} // namespace JDWP
1817
1818} // namespace art