blob: add1394f2d1f8f203f3845fda1d2d268066b929e [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;
Ian Rogersc0542af2014-09-03 16:16:56 -070068 JdwpError rc = Dbg::GetObjectTag(object_id, &tag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -080069 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 */
Sebastien Hertz1558b572015-02-25 15:05:59 +010094static JdwpError RequestInvoke(JdwpState*, Request* request, ExpandBuf* pReply,
95 ObjectId thread_id, ObjectId object_id,
96 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
Ian Rogersc0542af2014-09-03 16:16:56 -0700100 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
Sebastien Hertz7d955652014-10-22 10:57:10 +0200109 std::unique_ptr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : nullptr);
110 std::unique_ptr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : nullptr);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800111 for (int32_t i = 0; i < arg_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700112 argTypes[i] = request->ReadTag();
Elliott Hughes45651fd2012-02-21 15:48:20 -0800113 size_t width = Dbg::GetTagWidth(argTypes[i]);
Ian Rogersc0542af2014-09-03 16:16:56 -0700114 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
Ian Rogersc0542af2014-09-03 16:16:56 -0700119 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;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200127 JdwpError err = Dbg::InvokeMethod(thread_id, object_id, class_id, method_id, arg_count,
128 argValues.get(), argTypes.get(), options, &resultTag,
129 &resultValue, &exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800131 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700132 }
133
Sebastien Hertz1558b572015-02-25 15:05:59 +0100134 if (is_constructor) {
135 // If we invoked a constructor (which actually returns void), return the receiver,
136 // unless we threw, in which case we return NULL.
137 resultTag = JT_OBJECT;
138 resultValue = (exceptObjId == 0) ? object_id : 0;
139 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140
Sebastien Hertz1558b572015-02-25 15:05:59 +0100141 size_t width = Dbg::GetTagWidth(resultTag);
142 expandBufAdd1(pReply, resultTag);
143 if (width != 0) {
144 WriteValue(pReply, width, resultValue);
145 }
146 expandBufAdd1(pReply, JT_OBJECT);
147 expandBufAddObjectId(pReply, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700148
Sebastien Hertz1558b572015-02-25 15:05:59 +0100149 VLOG(jdwp) << " --> returned " << resultTag
150 << StringPrintf(" %#" PRIx64 " (except=%#" PRIx64 ")", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700151
Sebastien Hertz1558b572015-02-25 15:05:59 +0100152 /* show detailed debug output */
153 if (resultTag == JT_STRING && exceptObjId == 0) {
154 if (resultValue != 0) {
155 if (VLOG_IS_ON(jdwp)) {
156 std::string result_string;
157 JDWP::JdwpError error = Dbg::StringToUtf8(resultValue, &result_string);
158 CHECK_EQ(error, JDWP::ERR_NONE);
159 VLOG(jdwp) << " string '" << result_string << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700160 }
Sebastien Hertz1558b572015-02-25 15:05:59 +0100161 } else {
162 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700163 }
164 }
165
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700166 return err;
167}
168
Ian Rogersc0542af2014-09-03 16:16:56 -0700169static 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 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700193static JdwpError VM_ClassesBySignature(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700195 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;
Ian Rogersc0542af2014-09-03 16:16:56 -0700198 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;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200206 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, nullptr);
Elliott Hughes436e3722012-02-17 20:01:47 -0800207 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 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700225static 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;
Sebastien Hertza06430c2014-09-15 19:21:30 +0200228 Dbg::GetThreads(nullptr /* all thread groups */, &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 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700241static 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 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700262static 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
Ian Rogersc0542af2014-09-03 16:16:56 -0700272static 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 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700284static 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 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700296static JdwpError VM_Resume(JdwpState*, Request*, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700297 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700298 Dbg::ResumeVM();
299 return ERR_NONE;
300}
301
Ian Rogersc0542af2014-09-03 16:16:56 -0700302static JdwpError VM_Exit(JdwpState* state, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700303 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700304 uint32_t exit_status = request->ReadUnsigned32("exit_status");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800305 state->ExitAfterReplying(exit_status);
306 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700307}
308
309/*
310 * Create a new string in the VM and return its ID.
311 *
312 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
313 * string "java.util.Arrays".)
314 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700315static JdwpError VM_CreateString(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700316 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700317 std::string str(request->ReadUtf8String());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700318 ObjectId stringId = Dbg::CreateString(str);
319 if (stringId == 0) {
320 return ERR_OUT_OF_MEMORY;
321 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700322 expandBufAddObjectId(pReply, stringId);
323 return ERR_NONE;
324}
325
Ian Rogersc0542af2014-09-03 16:16:56 -0700326static JdwpError VM_ClassPaths(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800328 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700329
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800330 std::vector<std::string> class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700331 Split(Runtime::Current()->GetClassPathString(), ':', &class_path);
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800332 expandBufAdd4BE(pReply, class_path.size());
Sebastien Hertze6c143f2015-01-13 10:10:40 +0100333 for (const std::string& str : class_path) {
334 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700335 }
336
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800337 std::vector<std::string> boot_class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700338 Split(Runtime::Current()->GetBootClassPathString(), ':', &boot_class_path);
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800339 expandBufAdd4BE(pReply, boot_class_path.size());
Sebastien Hertze6c143f2015-01-13 10:10:40 +0100340 for (const std::string& str : boot_class_path) {
341 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700342 }
343
344 return ERR_NONE;
345}
346
Ian Rogersc0542af2014-09-03 16:16:56 -0700347static JdwpError VM_DisposeObjects(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700349 size_t object_count = request->ReadUnsigned32("object_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800350 for (size_t i = 0; i < object_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700351 ObjectId object_id = request->ReadObjectId();
352 uint32_t reference_count = request->ReadUnsigned32("reference_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800353 Dbg::DisposeObject(object_id, reference_count);
354 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700355 return ERR_NONE;
356}
357
Ian Rogersc0542af2014-09-03 16:16:56 -0700358static JdwpError VM_Capabilities(JdwpState*, Request*, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700359 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200360 expandBufAdd1(reply, true); // canWatchFieldModification
361 expandBufAdd1(reply, true); // canWatchFieldAccess
Elliott Hughes9777ba22013-01-17 09:04:19 -0800362 expandBufAdd1(reply, true); // canGetBytecodes
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800363 expandBufAdd1(reply, true); // canGetSyntheticAttribute
364 expandBufAdd1(reply, true); // canGetOwnedMonitorInfo
Elliott Hughesf9501702013-01-11 11:22:27 -0800365 expandBufAdd1(reply, true); // canGetCurrentContendedMonitor
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800366 expandBufAdd1(reply, true); // canGetMonitorInfo
367 return ERR_NONE;
368}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700369
Ian Rogersc0542af2014-09-03 16:16:56 -0700370static JdwpError VM_CapabilitiesNew(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800371 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800372 // The first few capabilities are the same as those reported by the older call.
Sebastien Hertz7d955652014-10-22 10:57:10 +0200373 VM_Capabilities(nullptr, request, reply);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800374
375 expandBufAdd1(reply, false); // canRedefineClasses
376 expandBufAdd1(reply, false); // canAddMethod
377 expandBufAdd1(reply, false); // canUnrestrictedlyRedefineClasses
378 expandBufAdd1(reply, false); // canPopFrames
Sebastien Hertz5f4e6f52014-04-11 12:07:41 +0200379 expandBufAdd1(reply, true); // canUseInstanceFilters
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800380 expandBufAdd1(reply, false); // canGetSourceDebugExtension
381 expandBufAdd1(reply, false); // canRequestVMDeathEvent
382 expandBufAdd1(reply, false); // canSetDefaultStratum
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800383 expandBufAdd1(reply, true); // 1.6: canGetInstanceInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800384 expandBufAdd1(reply, false); // 1.6: canRequestMonitorEvents
Elliott Hughes734b8c62013-01-11 15:32:45 -0800385 expandBufAdd1(reply, true); // 1.6: canGetMonitorFrameInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800386 expandBufAdd1(reply, false); // 1.6: canUseSourceNameFilters
387 expandBufAdd1(reply, false); // 1.6: canGetConstantPool
388 expandBufAdd1(reply, false); // 1.6: canForceEarlyReturn
389
390 // Fill in reserved22 through reserved32; note count started at 1.
391 for (size_t i = 22; i <= 32; ++i) {
392 expandBufAdd1(reply, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700393 }
394 return ERR_NONE;
395}
396
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700397static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700398 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800399 std::vector<JDWP::RefTypeId> classes;
Ian Rogersc0542af2014-09-03 16:16:56 -0700400 Dbg::GetClassList(&classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700401
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800402 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700403
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800404 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800405 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800406 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800407 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800408 uint32_t class_status;
409 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
410 if (status != ERR_NONE) {
411 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800412 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700413
Elliott Hughes436e3722012-02-17 20:01:47 -0800414 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800415 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800416 if (descriptor_and_status) {
417 expandBufAddUtf8String(pReply, descriptor);
418 if (generic) {
419 expandBufAddUtf8String(pReply, genericSignature);
420 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800421 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800422 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700423 }
424
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700425 return ERR_NONE;
426}
427
Ian Rogersc0542af2014-09-03 16:16:56 -0700428static JdwpError VM_AllClasses(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700429 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700430 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800431}
432
Ian Rogersc0542af2014-09-03 16:16:56 -0700433static JdwpError VM_AllClassesWithGeneric(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700434 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700435 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800436}
437
Ian Rogersc0542af2014-09-03 16:16:56 -0700438static JdwpError VM_InstanceCounts(JdwpState*, Request* request, ExpandBuf* pReply)
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800439 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700440 int32_t class_count = request->ReadSigned32("class count");
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800441 if (class_count < 0) {
442 return ERR_ILLEGAL_ARGUMENT;
443 }
444 std::vector<RefTypeId> class_ids;
445 for (int32_t i = 0; i < class_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700446 class_ids.push_back(request->ReadRefTypeId());
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800447 }
448
449 std::vector<uint64_t> counts;
Ian Rogersc0542af2014-09-03 16:16:56 -0700450 JdwpError rc = Dbg::GetInstanceCounts(class_ids, &counts);
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800451 if (rc != ERR_NONE) {
452 return rc;
453 }
454
455 expandBufAdd4BE(pReply, counts.size());
456 for (size_t i = 0; i < counts.size(); ++i) {
457 expandBufAdd8BE(pReply, counts[i]);
458 }
459 return ERR_NONE;
460}
461
Ian Rogersc0542af2014-09-03 16:16:56 -0700462static JdwpError RT_Modifiers(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700463 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700464 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800465 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700466}
467
468/*
469 * Get values from static fields in a reference type.
470 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700471static JdwpError RT_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700472 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700473 RefTypeId refTypeId = request->ReadRefTypeId();
474 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes0cf74332012-02-23 23:14:00 -0800475 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800476 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700477 FieldId fieldId = request->ReadFieldId();
Elliott Hughes0cf74332012-02-23 23:14:00 -0800478 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800479 if (status != ERR_NONE) {
480 return status;
481 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700482 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700483 return ERR_NONE;
484}
485
486/*
487 * Get the name of the source file in which a reference type was declared.
488 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700489static JdwpError RT_SourceFile(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700490 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700491 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes03181a82011-11-17 17:22:21 -0800492 std::string source_file;
Ian Rogersc0542af2014-09-03 16:16:56 -0700493 JdwpError status = Dbg::GetSourceFile(refTypeId, &source_file);
Elliott Hughes436e3722012-02-17 20:01:47 -0800494 if (status != ERR_NONE) {
495 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700496 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800497 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800498 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700499}
500
501/*
502 * Return the current status of the reference type.
503 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700504static JdwpError RT_Status(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700505 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700506 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800507 JDWP::JdwpTypeTag type_tag;
508 uint32_t class_status;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200509 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, nullptr);
Elliott Hughes436e3722012-02-17 20:01:47 -0800510 if (status != ERR_NONE) {
511 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800512 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800513 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700514 return ERR_NONE;
515}
516
517/*
518 * Return interfaces implemented directly by this class.
519 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700520static JdwpError RT_Interfaces(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700521 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700522 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800523 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700524}
525
526/*
527 * Return the class object corresponding to this type.
528 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700529static JdwpError RT_ClassObject(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700530 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700531 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -0800532 ObjectId class_object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700533 JdwpError status = Dbg::GetClassObject(refTypeId, &class_object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800534 if (status != ERR_NONE) {
535 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800536 }
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800537 VLOG(jdwp) << StringPrintf(" --> ObjectId %#" PRIx64, class_object_id);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800538 expandBufAddObjectId(pReply, class_object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700539 return ERR_NONE;
540}
541
542/*
543 * Returns the value of the SourceDebugExtension attribute.
544 *
545 * JDB seems interested, but DEX files don't currently support this.
546 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700547static JdwpError RT_SourceDebugExtension(JdwpState*, Request*, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700548 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700549 /* referenceTypeId in, string out */
550 return ERR_ABSENT_INFORMATION;
551}
552
Ian Rogersc0542af2014-09-03 16:16:56 -0700553static JdwpError RT_Signature(JdwpState*, Request* request, ExpandBuf* pReply, bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700554 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700555 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700556
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800557 std::string signature;
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700558 JdwpError status = Dbg::GetSignature(refTypeId, &signature);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800559 if (status != ERR_NONE) {
560 return status;
561 }
562 expandBufAddUtf8String(pReply, signature);
563 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800564 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700565 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700566 return ERR_NONE;
567}
568
Ian Rogersc0542af2014-09-03 16:16:56 -0700569static JdwpError RT_Signature(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700570 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800571 return RT_Signature(state, request, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800572}
573
Ian Rogersc0542af2014-09-03 16:16:56 -0700574static JdwpError RT_SignatureWithGeneric(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800576 return RT_Signature(state, request, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800577}
578
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700579/*
580 * Return the instance of java.lang.ClassLoader that loaded the specified
581 * reference type, or null if it was loaded by the system loader.
582 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700583static JdwpError RT_ClassLoader(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700584 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700585 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800586 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700587}
588
589/*
590 * Given a referenceTypeId, return a block of stuff that describes the
591 * fields declared by a class.
592 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700593static JdwpError RT_FieldsWithGeneric(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700594 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700595 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800596 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800597}
598
599// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogersc0542af2014-09-03 16:16:56 -0700600static JdwpError RT_Fields(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700601 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700602 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800603 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700604}
605
606/*
607 * Given a referenceTypeID, return a block of goodies describing the
608 * methods declared by a class.
609 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700610static JdwpError RT_MethodsWithGeneric(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700611 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700612 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800613 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800614}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700615
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800616// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogersc0542af2014-09-03 16:16:56 -0700617static JdwpError RT_Methods(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700618 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700619 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800620 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700621}
622
Ian Rogersc0542af2014-09-03 16:16:56 -0700623static JdwpError RT_Instances(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes3b78c942013-01-15 17:35:41 -0800624 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700625 RefTypeId class_id = request->ReadRefTypeId();
626 int32_t max_count = request->ReadSigned32("max count");
Elliott Hughes3b78c942013-01-15 17:35:41 -0800627 if (max_count < 0) {
628 return ERR_ILLEGAL_ARGUMENT;
629 }
630
631 std::vector<ObjectId> instances;
Ian Rogersc0542af2014-09-03 16:16:56 -0700632 JdwpError rc = Dbg::GetInstances(class_id, max_count, &instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800633 if (rc != ERR_NONE) {
634 return rc;
635 }
636
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800637 return WriteTaggedObjectList(reply, instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800638}
639
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700640/*
641 * Return the immediate superclass of a class.
642 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700643static JdwpError CT_Superclass(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700644 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700645 RefTypeId class_id = request->ReadRefTypeId();
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800646 RefTypeId superClassId;
Ian Rogersc0542af2014-09-03 16:16:56 -0700647 JdwpError status = Dbg::GetSuperclass(class_id, &superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800648 if (status != ERR_NONE) {
649 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800650 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700651 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700652 return ERR_NONE;
653}
654
655/*
656 * Set static class values.
657 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700658static JdwpError CT_SetValues(JdwpState* , Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700659 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700660 RefTypeId class_id = request->ReadRefTypeId();
661 int32_t values_count = request->ReadSigned32("values count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700662
Elliott Hughesa96836a2013-01-17 12:27:49 -0800663 UNUSED(class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700664
Elliott Hughesa96836a2013-01-17 12:27:49 -0800665 for (int32_t i = 0; i < values_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700666 FieldId fieldId = request->ReadFieldId();
Elliott Hughesaed4be92011-12-02 16:16:23 -0800667 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800668 size_t width = Dbg::GetTagWidth(fieldTag);
Ian Rogersc0542af2014-09-03 16:16:56 -0700669 uint64_t value = request->ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700670
Elliott Hughesa96836a2013-01-17 12:27:49 -0800671 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " --> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800672 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
673 if (status != ERR_NONE) {
674 return status;
675 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700676 }
677
678 return ERR_NONE;
679}
680
681/*
682 * Invoke a static method.
683 *
684 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
685 * values in the "variables" display.
686 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700687static JdwpError CT_InvokeMethod(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700688 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700689 RefTypeId class_id = request->ReadRefTypeId();
690 ObjectId thread_id = request->ReadThreadId();
691 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700692
Sebastien Hertz1558b572015-02-25 15:05:59 +0100693 return RequestInvoke(state, request, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700694}
695
696/*
697 * Create a new object of the requested type, and invoke the specified
698 * constructor.
699 *
700 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
701 * see the contents of a byte[] as a string.
702 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700703static JdwpError CT_NewInstance(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700704 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700705 RefTypeId class_id = request->ReadRefTypeId();
706 ObjectId thread_id = request->ReadThreadId();
707 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700708
Elliott Hughes74847412012-06-20 18:10:21 -0700709 ObjectId object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700710 JdwpError status = Dbg::CreateObject(class_id, &object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800711 if (status != ERR_NONE) {
712 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800713 }
Elliott Hughes74847412012-06-20 18:10:21 -0700714 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700715 return ERR_OUT_OF_MEMORY;
716 }
Sebastien Hertz1558b572015-02-25 15:05:59 +0100717 return RequestInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700718}
719
720/*
721 * Create a new array object of the requested type and length.
722 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700723static JdwpError AT_newInstance(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700724 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700725 RefTypeId arrayTypeId = request->ReadRefTypeId();
726 int32_t length = request->ReadSigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700727
Elliott Hughes74847412012-06-20 18:10:21 -0700728 ObjectId object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700729 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, &object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800730 if (status != ERR_NONE) {
731 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800732 }
Elliott Hughes74847412012-06-20 18:10:21 -0700733 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700734 return ERR_OUT_OF_MEMORY;
735 }
736 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700737 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700738 return ERR_NONE;
739}
740
741/*
742 * Return line number information for the method, if present.
743 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700744static JdwpError M_LineTable(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700745 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700746 RefTypeId refTypeId = request->ReadRefTypeId();
747 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700748
Elliott Hughes74847412012-06-20 18:10:21 -0700749 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700750
751 return ERR_NONE;
752}
753
Ian Rogersc0542af2014-09-03 16:16:56 -0700754static JdwpError M_VariableTable(JdwpState*, Request* request, ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700755 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700756 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700757 RefTypeId class_id = request->ReadRefTypeId();
758 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700759
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800760 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
761 // information. That will cause Eclipse to make a best-effort attempt at displaying local
762 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
763 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700764 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700765 return ERR_NONE;
766}
767
Ian Rogersc0542af2014-09-03 16:16:56 -0700768static JdwpError M_VariableTable(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700769 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800770 return M_VariableTable(state, request, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800771}
772
Ian Rogersc0542af2014-09-03 16:16:56 -0700773static JdwpError M_VariableTableWithGeneric(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700774 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800775 return M_VariableTable(state, request, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800776}
777
Ian Rogersc0542af2014-09-03 16:16:56 -0700778static JdwpError M_Bytecodes(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes9777ba22013-01-17 09:04:19 -0800779 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700780 RefTypeId class_id = request->ReadRefTypeId();
781 MethodId method_id = request->ReadMethodId();
Elliott Hughes9777ba22013-01-17 09:04:19 -0800782
783 std::vector<uint8_t> bytecodes;
Ian Rogersc0542af2014-09-03 16:16:56 -0700784 JdwpError rc = Dbg::GetBytecodes(class_id, method_id, &bytecodes);
Elliott Hughes9777ba22013-01-17 09:04:19 -0800785 if (rc != ERR_NONE) {
786 return rc;
787 }
788
789 expandBufAdd4BE(reply, bytecodes.size());
790 for (size_t i = 0; i < bytecodes.size(); ++i) {
791 expandBufAdd1(reply, bytecodes[i]);
792 }
793
794 return ERR_NONE;
795}
796
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700797/*
798 * Given an object reference, return the runtime type of the object
799 * (class or array).
800 *
Elliott Hughes74847412012-06-20 18:10:21 -0700801 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700802 * passed in here.
803 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700804static JdwpError OR_ReferenceType(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700805 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700806 ObjectId object_id = request->ReadObjectId();
Elliott Hughes74847412012-06-20 18:10:21 -0700807 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700808}
809
810/*
811 * Get values from the fields of an object.
812 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700813static JdwpError OR_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700814 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700815 ObjectId object_id = request->ReadObjectId();
816 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700817
Elliott Hughes0cf74332012-02-23 23:14:00 -0800818 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800819 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700820 FieldId fieldId = request->ReadFieldId();
Elliott Hughes74847412012-06-20 18:10:21 -0700821 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800822 if (status != ERR_NONE) {
823 return status;
824 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700825 }
826
827 return ERR_NONE;
828}
829
830/*
831 * Set values in the fields of an object.
832 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700833static JdwpError OR_SetValues(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700834 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700835 ObjectId object_id = request->ReadObjectId();
836 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700837
Elliott Hughesa96836a2013-01-17 12:27:49 -0800838 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700839 FieldId fieldId = request->ReadFieldId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700840
Elliott Hughesaed4be92011-12-02 16:16:23 -0800841 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800842 size_t width = Dbg::GetTagWidth(fieldTag);
Ian Rogersc0542af2014-09-03 16:16:56 -0700843 uint64_t value = request->ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700844
Elliott Hughes2435a572012-02-17 16:07:41 -0800845 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700846 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800847 if (status != ERR_NONE) {
848 return status;
849 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700850 }
851
852 return ERR_NONE;
853}
854
Ian Rogersc0542af2014-09-03 16:16:56 -0700855static JdwpError OR_MonitorInfo(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughesf327e072013-01-09 16:01:26 -0800856 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700857 ObjectId object_id = request->ReadObjectId();
Elliott Hughesf327e072013-01-09 16:01:26 -0800858 return Dbg::GetMonitorInfo(object_id, reply);
859}
860
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700861/*
862 * Invoke an instance method. The invocation must occur in the specified
863 * thread, which must have been suspended by an event.
864 *
865 * The call is synchronous. All threads in the VM are resumed, unless the
866 * SINGLE_THREADED flag is set.
867 *
868 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
869 * object), it will try to invoke the object's toString() function. This
870 * feature becomes crucial when examining ArrayLists with Eclipse.
871 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700872static JdwpError OR_InvokeMethod(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700873 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700874 ObjectId object_id = request->ReadObjectId();
875 ObjectId thread_id = request->ReadThreadId();
876 RefTypeId class_id = request->ReadRefTypeId();
877 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700878
Sebastien Hertz1558b572015-02-25 15:05:59 +0100879 return RequestInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700880}
881
Ian Rogersc0542af2014-09-03 16:16:56 -0700882static JdwpError OR_DisableCollection(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700883 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700884 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800885 return Dbg::DisableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700886}
887
Ian Rogersc0542af2014-09-03 16:16:56 -0700888static JdwpError OR_EnableCollection(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700889 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700890 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800891 return Dbg::EnableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700892}
893
Ian Rogersc0542af2014-09-03 16:16:56 -0700894static JdwpError OR_IsCollected(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700895 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700896 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800897 bool is_collected;
Ian Rogersc0542af2014-09-03 16:16:56 -0700898 JdwpError rc = Dbg::IsCollected(object_id, &is_collected);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800899 expandBufAdd1(pReply, is_collected ? 1 : 0);
900 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700901}
902
Ian Rogersc0542af2014-09-03 16:16:56 -0700903static JdwpError OR_ReferringObjects(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800904 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700905 ObjectId object_id = request->ReadObjectId();
906 int32_t max_count = request->ReadSigned32("max count");
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800907 if (max_count < 0) {
908 return ERR_ILLEGAL_ARGUMENT;
909 }
910
911 std::vector<ObjectId> referring_objects;
Ian Rogersc0542af2014-09-03 16:16:56 -0700912 JdwpError rc = Dbg::GetReferringObjects(object_id, max_count, &referring_objects);
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800913 if (rc != ERR_NONE) {
914 return rc;
915 }
916
917 return WriteTaggedObjectList(reply, referring_objects);
918}
919
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700920/*
921 * Return the string value in a string object.
922 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700923static JdwpError SR_Value(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700924 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700925 ObjectId stringObject = request->ReadObjectId();
Sebastien Hertzb0b0b492014-09-15 11:27:27 +0200926 std::string str;
927 JDWP::JdwpError error = Dbg::StringToUtf8(stringObject, &str);
928 if (error != JDWP::ERR_NONE) {
929 return error;
930 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700931
Ian Rogers68b56852014-08-29 20:19:11 -0700932 VLOG(jdwp) << StringPrintf(" --> %s", PrintableString(str.c_str()).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700933
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800934 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700935
936 return ERR_NONE;
937}
938
939/*
940 * Return a thread's name.
941 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700942static JdwpError TR_Name(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700943 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700944 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700945
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800946 std::string name;
Ian Rogersc0542af2014-09-03 16:16:56 -0700947 JdwpError error = Dbg::GetThreadName(thread_id, &name);
Elliott Hughes221229c2013-01-08 18:17:50 -0800948 if (error != ERR_NONE) {
949 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700950 }
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800951 VLOG(jdwp) << StringPrintf(" Name of thread %#" PRIx64 " is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800952 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700953
954 return ERR_NONE;
955}
956
957/*
958 * Suspend the specified thread.
959 *
960 * It's supposed to remain suspended even if interpreted code wants to
961 * resume it; only the JDI is allowed to resume it.
962 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700963static JdwpError TR_Suspend(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700964 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700965 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700966
Elliott Hughes74847412012-06-20 18:10:21 -0700967 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700968 LOG(INFO) << " Warning: ignoring request to suspend self";
969 return ERR_THREAD_NOT_SUSPENDED;
970 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800971
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700972 Thread* self = Thread::Current();
973 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
974 JdwpError result = Dbg::SuspendThread(thread_id);
975 self->TransitionFromSuspendedToRunnable();
976 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700977}
978
979/*
980 * Resume the specified thread.
981 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700982static JdwpError TR_Resume(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700983 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700984 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700985
Elliott Hughes74847412012-06-20 18:10:21 -0700986 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700987 LOG(INFO) << " Warning: ignoring request to resume self";
988 return ERR_NONE;
989 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800990
Elliott Hughes74847412012-06-20 18:10:21 -0700991 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700992 return ERR_NONE;
993}
994
995/*
996 * Return status of specified thread.
997 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700998static JdwpError TR_Status(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700999 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001000 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001001
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08001002 JDWP::JdwpThreadStatus threadStatus;
1003 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -08001004 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
1005 if (error != ERR_NONE) {
1006 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001007 }
1008
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001009 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001010
1011 expandBufAdd4BE(pReply, threadStatus);
1012 expandBufAdd4BE(pReply, suspendStatus);
1013
1014 return ERR_NONE;
1015}
1016
1017/*
1018 * Return the thread group that the specified thread is a member of.
1019 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001020static JdwpError TR_ThreadGroup(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001021 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001022 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001023 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001024}
1025
1026/*
1027 * Return the current call stack of a suspended thread.
1028 *
1029 * If the thread isn't suspended, the error code isn't defined, but should
1030 * be THREAD_NOT_SUSPENDED.
1031 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001032static JdwpError TR_Frames(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001034 ObjectId thread_id = request->ReadThreadId();
1035 uint32_t start_frame = request->ReadUnsigned32("start frame");
1036 uint32_t length = request->ReadUnsigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001037
Elliott Hughes221229c2013-01-08 18:17:50 -08001038 size_t actual_frame_count;
Ian Rogersc0542af2014-09-03 16:16:56 -07001039 JdwpError error = Dbg::GetThreadFrameCount(thread_id, &actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001040 if (error != ERR_NONE) {
1041 return error;
1042 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001043
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001044 if (actual_frame_count <= 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001045 return ERR_THREAD_NOT_SUSPENDED; // 0 means no managed frames (which means "in native").
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001046 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001047
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001048 if (start_frame > actual_frame_count) {
1049 return ERR_INVALID_INDEX;
1050 }
1051 if (length == static_cast<uint32_t>(-1)) {
1052 length = actual_frame_count - start_frame;
1053 }
1054 if (start_frame + length > actual_frame_count) {
1055 return ERR_INVALID_LENGTH;
1056 }
1057
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001058 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001059}
1060
1061/*
1062 * Returns the #of frames on the specified thread, which must be suspended.
1063 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001064static JdwpError TR_FrameCount(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001065 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001066 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001067
Elliott Hughes221229c2013-01-08 18:17:50 -08001068 size_t frame_count;
Ian Rogersc0542af2014-09-03 16:16:56 -07001069 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, &frame_count);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001070 if (rc != ERR_NONE) {
1071 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001072 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001073 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001074
1075 return ERR_NONE;
1076}
1077
Ian Rogersc0542af2014-09-03 16:16:56 -07001078static JdwpError TR_OwnedMonitors(Request* request, ExpandBuf* reply, bool with_stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001079 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001080 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001081
1082 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001083 std::vector<uint32_t> stack_depths;
Ian Rogersc0542af2014-09-03 16:16:56 -07001084 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, &monitors, &stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001085 if (rc != ERR_NONE) {
1086 return rc;
1087 }
1088
1089 expandBufAdd4BE(reply, monitors.size());
1090 for (size_t i = 0; i < monitors.size(); ++i) {
1091 rc = WriteTaggedObject(reply, monitors[i]);
1092 if (rc != ERR_NONE) {
1093 return rc;
1094 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001095 if (with_stack_depths) {
1096 expandBufAdd4BE(reply, stack_depths[i]);
1097 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001098 }
1099 return ERR_NONE;
1100}
1101
Ian Rogersc0542af2014-09-03 16:16:56 -07001102static JdwpError TR_OwnedMonitors(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001104 return TR_OwnedMonitors(request, reply, false);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001105}
1106
Ian Rogersc0542af2014-09-03 16:16:56 -07001107static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001109 return TR_OwnedMonitors(request, reply, true);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001110}
1111
Ian Rogersc0542af2014-09-03 16:16:56 -07001112static JdwpError TR_CurrentContendedMonitor(JdwpState*, Request* request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001114 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001115
Elliott Hughesf9501702013-01-11 11:22:27 -08001116 ObjectId contended_monitor;
Ian Rogersc0542af2014-09-03 16:16:56 -07001117 JdwpError rc = Dbg::GetContendedMonitor(thread_id, &contended_monitor);
Elliott Hughesf9501702013-01-11 11:22:27 -08001118 if (rc != ERR_NONE) {
1119 return rc;
1120 }
1121 return WriteTaggedObject(reply, contended_monitor);
1122}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001123
Ian Rogersc0542af2014-09-03 16:16:56 -07001124static JdwpError TR_Interrupt(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughesf9501702013-01-11 11:22:27 -08001125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001126 UNUSED(reply);
Ian Rogersc0542af2014-09-03 16:16:56 -07001127 ObjectId thread_id = request->ReadThreadId();
Elliott Hughesf9501702013-01-11 11:22:27 -08001128 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001129}
1130
1131/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001133 *
1134 * (The thread *might* still be running -- it might not have examined
1135 * its suspend count recently.)
1136 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001137static JdwpError TR_DebugSuspendCount(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001139 ObjectId thread_id = request->ReadThreadId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001140 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001141}
1142
1143/*
1144 * Return the name of a thread group.
1145 *
1146 * The Eclipse debugger recognizes "main" and "system" as special.
1147 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001148static JdwpError TGR_Name(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001150 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001151 return Dbg::GetThreadGroupName(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001152}
1153
1154/*
1155 * Returns the thread group -- if any -- that contains the specified
1156 * thread group.
1157 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001158static JdwpError TGR_Parent(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001160 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001161 return Dbg::GetThreadGroupParent(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001162}
1163
1164/*
1165 * Return the active threads and thread groups that are part of the
1166 * specified thread group.
1167 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001168static JdwpError TGR_Children(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001170 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001171 return Dbg::GetThreadGroupChildren(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001172}
1173
1174/*
1175 * Return the #of components in the array.
1176 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001177static JdwpError AR_Length(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001179 ObjectId array_id = request->ReadArrayId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001180
Ian Rogersc0542af2014-09-03 16:16:56 -07001181 int32_t length;
1182 JdwpError status = Dbg::GetArrayLength(array_id, &length);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001183 if (status != ERR_NONE) {
1184 return status;
1185 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001186 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001187
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001188 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001189
1190 return ERR_NONE;
1191}
1192
1193/*
1194 * Return the values from an array.
1195 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001196static JdwpError AR_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001198 ObjectId array_id = request->ReadArrayId();
1199 uint32_t offset = request->ReadUnsigned32("offset");
1200 uint32_t length = request->ReadUnsigned32("length");
Elliott Hughesa96836a2013-01-17 12:27:49 -08001201 return Dbg::OutputArray(array_id, offset, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001202}
1203
1204/*
1205 * Set values in an array.
1206 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001207static JdwpError AR_SetValues(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001208 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001209 ObjectId array_id = request->ReadArrayId();
1210 uint32_t offset = request->ReadUnsigned32("offset");
1211 uint32_t count = request->ReadUnsigned32("count");
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001212 return Dbg::SetArrayElements(array_id, offset, count, request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001213}
1214
Ian Rogersc0542af2014-09-03 16:16:56 -07001215static JdwpError CLR_VisibleClasses(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001216 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001217 request->ReadObjectId(); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001218 // TODO: we should only return classes which have the given class loader as a defining or
1219 // initiating loader. The former would be easy; the latter is hard, because we don't have
1220 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001221 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001222}
1223
1224/*
1225 * Set an event trigger.
1226 *
1227 * Reply with a requestID.
1228 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001229static JdwpError ER_Set(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001231 JdwpEventKind event_kind = request->ReadEnum1<JdwpEventKind>("event kind");
1232 JdwpSuspendPolicy suspend_policy = request->ReadEnum1<JdwpSuspendPolicy>("suspend policy");
1233 int32_t modifier_count = request->ReadSigned32("modifier count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001234
Elliott Hughesa96836a2013-01-17 12:27:49 -08001235 CHECK_LT(modifier_count, 256); /* reasonableness check */
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001236
Elliott Hughesa96836a2013-01-17 12:27:49 -08001237 JdwpEvent* pEvent = EventAlloc(modifier_count);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001238 pEvent->eventKind = event_kind;
1239 pEvent->suspend_policy = suspend_policy;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001240 pEvent->modCount = modifier_count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001241
1242 /*
1243 * Read modifiers. Ordering may be significant (see explanation of Count
1244 * mods in JDWP doc).
1245 */
Elliott Hughesa96836a2013-01-17 12:27:49 -08001246 for (int32_t i = 0; i < modifier_count; ++i) {
Elliott Hughes972a47b2012-02-21 18:16:06 -08001247 JdwpEventMod& mod = pEvent->mods[i];
Ian Rogersc0542af2014-09-03 16:16:56 -07001248 mod.modKind = request->ReadModKind();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001249 switch (mod.modKind) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001250 case MK_COUNT:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001251 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001252 // Report once, when "--count" reaches 0.
Ian Rogersc0542af2014-09-03 16:16:56 -07001253 uint32_t count = request->ReadUnsigned32("count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001254 if (count == 0) {
1255 return ERR_INVALID_COUNT;
1256 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001257 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001258 }
1259 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001260 case MK_CONDITIONAL:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001261 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001262 // Conditional on expression.
Ian Rogersc0542af2014-09-03 16:16:56 -07001263 uint32_t exprId = request->ReadUnsigned32("expr id");
Elliott Hughes972a47b2012-02-21 18:16:06 -08001264 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001265 }
1266 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001267 case MK_THREAD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001268 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001269 // Only report events in specified thread.
Ian Rogersc0542af2014-09-03 16:16:56 -07001270 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001271 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001272 }
1273 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001274 case MK_CLASS_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001275 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001276 // For ClassPrepare, MethodEntry.
Ian Rogersc0542af2014-09-03 16:16:56 -07001277 RefTypeId class_id = request->ReadRefTypeId();
Elliott Hughes74847412012-06-20 18:10:21 -07001278 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001279 }
1280 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001281 case MK_CLASS_MATCH:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001282 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001283 // Restrict events to matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001284 // pattern is "java.foo.*", we want "java/foo/*".
Ian Rogersc0542af2014-09-03 16:16:56 -07001285 std::string pattern(request->ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001286 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001287 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001288 }
1289 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001290 case MK_CLASS_EXCLUDE:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001291 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001292 // Restrict events to non-matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001293 // pattern is "java.foo.*", we want "java/foo/*".
Ian Rogersc0542af2014-09-03 16:16:56 -07001294 std::string pattern(request->ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001295 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001296 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001297 }
1298 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001299 case MK_LOCATION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001300 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001301 // Restrict certain events based on location.
Ian Rogersc0542af2014-09-03 16:16:56 -07001302 JdwpLocation location = request->ReadLocation();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001303 mod.locationOnly.loc = location;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001304 }
1305 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001306 case MK_EXCEPTION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001307 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001308 // Modifies EK_EXCEPTION events,
Ian Rogersc0542af2014-09-03 16:16:56 -07001309 mod.exceptionOnly.refTypeId = request->ReadRefTypeId(); // null => all exceptions.
1310 mod.exceptionOnly.caught = request->ReadEnum1<uint8_t>("caught");
1311 mod.exceptionOnly.uncaught = request->ReadEnum1<uint8_t>("uncaught");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001312 }
1313 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001314 case MK_FIELD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001315 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001316 // For field access/modification events.
Ian Rogersc0542af2014-09-03 16:16:56 -07001317 RefTypeId declaring = request->ReadRefTypeId();
1318 FieldId fieldId = request->ReadFieldId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001319 mod.fieldOnly.refTypeId = declaring;
1320 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001321 }
1322 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001323 case MK_STEP:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001324 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001325 // For use with EK_SINGLE_STEP.
Ian Rogersc0542af2014-09-03 16:16:56 -07001326 ObjectId thread_id = request->ReadThreadId();
1327 uint32_t size = request->ReadUnsigned32("step size");
1328 uint32_t depth = request->ReadUnsigned32("step depth");
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001329 VLOG(jdwp) << StringPrintf(" Step: thread=%#" PRIx64, thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001330 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1331
Elliott Hughes74847412012-06-20 18:10:21 -07001332 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001333 mod.step.size = size;
1334 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001335 }
1336 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001337 case MK_INSTANCE_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001338 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001339 // Report events related to a specific object.
Ian Rogersc0542af2014-09-03 16:16:56 -07001340 ObjectId instance = request->ReadObjectId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001341 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001342 }
1343 break;
1344 default:
Sebastien Hertz6d7839e2014-12-05 10:52:15 +01001345 LOG(WARNING) << "Unsupported modifier " << mod.modKind << " for event " << pEvent->eventKind;
1346 // Free allocated event to avoid leak before leaving.
1347 EventFree(pEvent);
1348 return JDWP::ERR_NOT_IMPLEMENTED;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001349 }
1350 }
1351
1352 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001353 * We reply with an integer "requestID".
1354 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001355 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001356 expandBufAdd4BE(pReply, requestId);
1357
1358 pEvent->requestId = requestId;
1359
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001360 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001361
1362 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001363 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001364 if (err != ERR_NONE) {
1365 /* registration failed, probably because event is bogus */
1366 EventFree(pEvent);
1367 LOG(WARNING) << "WARNING: event request rejected";
1368 }
1369 return err;
1370}
1371
Ian Rogersc0542af2014-09-03 16:16:56 -07001372static JdwpError ER_Clear(JdwpState* state, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001373 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001374 request->ReadEnum1<JdwpEventKind>("event kind");
1375 uint32_t requestId = request->ReadUnsigned32("request id");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001376
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001377 // Failure to find an event with a matching ID is a no-op
1378 // and does not return an error.
Elliott Hughes761928d2011-11-16 18:33:03 -08001379 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001380 return ERR_NONE;
1381}
1382
1383/*
1384 * Return the values of arguments and local variables.
1385 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001386static JdwpError SF_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001387 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz8009f392014-09-01 17:07:11 +02001388 return Dbg::GetLocalValues(request, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001389}
1390
1391/*
1392 * Set the values of arguments and local variables.
1393 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001394static JdwpError SF_SetValues(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz8009f392014-09-01 17:07:11 +02001396 return Dbg::SetLocalValues(request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001397}
1398
Ian Rogersc0542af2014-09-03 16:16:56 -07001399static JdwpError SF_ThisObject(JdwpState*, Request* request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001400 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001401 ObjectId thread_id = request->ReadThreadId();
1402 FrameId frame_id = request->ReadFrameId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001403
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001404 ObjectId object_id;
1405 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001406 if (rc != ERR_NONE) {
1407 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001408 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001409
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001410 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001411}
1412
1413/*
1414 * Return the reference type reflected by this class object.
1415 *
1416 * This appears to be required because ReferenceTypeId values are NEVER
1417 * reused, whereas ClassIds can be recycled like any other object. (Either
1418 * that, or I have no idea what this is for.)
1419 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001420static JdwpError COR_ReflectedType(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001421 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001422 RefTypeId class_object_id = request->ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001423 return Dbg::GetReflectedType(class_object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001424}
1425
1426/*
1427 * Handle a DDM packet with a single chunk in it.
1428 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001429static JdwpError DDM_Chunk(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001430 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001431 state->NotifyDdmsActive();
Sebastien Hertz7d955652014-10-22 10:57:10 +02001432 uint8_t* replyBuf = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001433 int replyLen = -1;
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001434 if (Dbg::DdmHandlePacket(request, &replyBuf, &replyLen)) {
1435 // If they want to send something back, we copy it into the buffer.
1436 // TODO: consider altering the JDWP stuff to hold the packet header
1437 // in a separate buffer. That would allow us to writev() DDM traffic
1438 // instead of copying it into the expanding buffer. The reduction in
1439 // heap requirements is probably more valuable than the efficiency.
1440 CHECK_GT(replyLen, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001441 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1442 free(replyBuf);
1443 }
1444 return ERR_NONE;
1445}
1446
1447/*
1448 * Handler map decl.
1449 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001450typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, Request* request, ExpandBuf* reply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001451
1452struct JdwpHandlerMap {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001453 uint8_t cmdSet;
1454 uint8_t cmd;
1455 JdwpRequestHandler func;
1456 const char* name;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001457};
1458
1459/*
1460 * Map commands to functions.
1461 *
1462 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1463 * and 128-256 are vendor-defined.
1464 */
Elliott Hughescb693062013-02-21 09:48:08 -08001465static const JdwpHandlerMap gHandlers[] = {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001466 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001467 { 1, 1, VM_Version, "VirtualMachine.Version" },
1468 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1469 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1470 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1471 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1472 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1473 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1474 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1475 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1476 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1477 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1478 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1479 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1480 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001481 { 1, 15, nullptr, "VirtualMachine.HoldEvents" },
1482 { 1, 16, nullptr, "VirtualMachine.ReleaseEvents" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001483 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001484 { 1, 18, nullptr, "VirtualMachine.RedefineClasses" },
1485 { 1, 19, nullptr, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001486 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001487 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001488
1489 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001490 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1491 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1492 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1493 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1494 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1495 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1496 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001497 { 2, 8, nullptr, "ReferenceType.NestedTypes" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001498 { 2, 9, RT_Status, "ReferenceType.Status" },
1499 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1500 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001501 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1502 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001503 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1504 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughes3b78c942013-01-15 17:35:41 -08001505 { 2, 16, RT_Instances, "ReferenceType.Instances" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001506 { 2, 17, nullptr, "ReferenceType.ClassFileVersion" },
1507 { 2, 18, nullptr, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001508
1509 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001510 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1511 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1512 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1513 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001514
1515 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001516 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001517
1518 /* InterfaceType command set (5) */
1519
1520 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001521 { 6, 1, M_LineTable, "Method.LineTable" },
1522 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughes9777ba22013-01-17 09:04:19 -08001523 { 6, 3, M_Bytecodes, "Method.Bytecodes" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001524 { 6, 4, nullptr, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001525 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001526
1527 /* Field command set (8) */
1528
1529 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001530 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1531 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1532 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001533 { 9, 4, nullptr, "ObjectReference.UNUSED" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001534 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1535 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001536 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001537 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1538 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001539 { 9, 10, OR_ReferringObjects, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001540
1541 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001542 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001543
1544 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001545 { 11, 1, TR_Name, "ThreadReference.Name" },
1546 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1547 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1548 { 11, 4, TR_Status, "ThreadReference.Status" },
1549 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1550 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1551 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1552 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1553 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001554 { 11, 10, nullptr, "ThreadReference.Stop" },
Elliott Hughes734b8c62013-01-11 15:32:45 -08001555 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1556 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1557 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001558 { 11, 14, nullptr, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001559
1560 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001561 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1562 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1563 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001564
1565 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001566 { 13, 1, AR_Length, "ArrayReference.Length" },
1567 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1568 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001569
1570 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001571 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001572
1573 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001574 { 15, 1, ER_Set, "EventRequest.Set" },
1575 { 15, 2, ER_Clear, "EventRequest.Clear" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001576 { 15, 3, nullptr, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001577
1578 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001579 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1580 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1581 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001582 { 16, 4, nullptr, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001583
1584 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001585 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001586
1587 /* Event command set (64) */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001588 { 64, 100, nullptr, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001589
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001590 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001591};
1592
Ian Rogersc0542af2014-09-03 16:16:56 -07001593static const char* GetCommandName(Request* request) {
Elliott Hughescb693062013-02-21 09:48:08 -08001594 for (size_t i = 0; i < arraysize(gHandlers); ++i) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001595 if (gHandlers[i].cmdSet == request->GetCommandSet() &&
1596 gHandlers[i].cmd == request->GetCommand()) {
Elliott Hughescb693062013-02-21 09:48:08 -08001597 return gHandlers[i].name;
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001598 }
1599 }
1600 return "?UNKNOWN?";
1601}
1602
Ian Rogersc0542af2014-09-03 16:16:56 -07001603static std::string DescribeCommand(Request* request) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001604 std::string result;
Elliott Hughes64f574f2013-02-20 14:57:12 -08001605 result += "REQUEST: ";
Elliott Hughescb693062013-02-21 09:48:08 -08001606 result += GetCommandName(request);
Ian Rogersc0542af2014-09-03 16:16:56 -07001607 result += StringPrintf(" (length=%zu id=0x%06x)", request->GetLength(), request->GetId());
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001608 return result;
1609}
1610
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001611/*
1612 * Process a request from the debugger.
1613 *
1614 * On entry, the JDWP thread is in VMWAIT.
1615 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001616size_t JdwpState::ProcessRequest(Request* request, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001617 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001618
Ian Rogersc0542af2014-09-03 16:16:56 -07001619 if (request->GetCommandSet() != kJDWPDdmCmdSet) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001620 /*
1621 * Activity from a debugger, not merely ddms. Mark us as having an
1622 * active debugger session, and zero out the last-activity timestamp
1623 * so waitForDebugger() doesn't return if we stall for a bit here.
1624 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001625 Dbg::GoActive();
Ian Rogers37f3c962014-07-17 11:25:30 -07001626 last_activity_time_ms_.StoreSequentiallyConsistent(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001627 }
1628
1629 /*
1630 * If a debugger event has fired in another thread, wait until the
Sebastien Hertz2bf93f42015-01-09 18:44:05 +01001631 * initiating thread has suspended itself before processing commands
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001632 * from the debugger. Otherwise we (the JDWP thread) could be told to
1633 * resume the thread before it has suspended.
1634 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001635 * Note that we MUST clear the event token before waking the event
1636 * thread up, or risk waiting for the thread to suspend after we've
1637 * told it to resume.
1638 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +01001639 AcquireJdwpTokenForCommand();
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001640
1641 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001642 * Tell the VM that we're running and shouldn't be interrupted by GC.
1643 * Do this after anything that can stall indefinitely.
1644 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001645 Thread* self = Thread::Current();
1646 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001647
1648 expandBufAddSpace(pReply, kJDWPHeaderLen);
1649
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001650 size_t i;
Elliott Hughescb693062013-02-21 09:48:08 -08001651 for (i = 0; i < arraysize(gHandlers); ++i) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001652 if (gHandlers[i].cmdSet == request->GetCommandSet() &&
1653 gHandlers[i].cmd == request->GetCommand() &&
1654 gHandlers[i].func != nullptr) {
Elliott Hughescb693062013-02-21 09:48:08 -08001655 VLOG(jdwp) << DescribeCommand(request);
1656 result = (*gHandlers[i].func)(this, request, pReply);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001657 if (result == ERR_NONE) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001658 request->CheckConsumed();
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001659 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001660 break;
1661 }
1662 }
Elliott Hughescb693062013-02-21 09:48:08 -08001663 if (i == arraysize(gHandlers)) {
1664 LOG(ERROR) << "Command not implemented: " << DescribeCommand(request);
Ian Rogersc0542af2014-09-03 16:16:56 -07001665 LOG(ERROR) << HexDump(request->data(), request->size(), false, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001666 result = ERR_NOT_IMPLEMENTED;
1667 }
1668
1669 /*
1670 * Set up the reply header.
1671 *
1672 * If we encountered an error, only send the header back.
1673 */
1674 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Sebastien Hertz43c8d722014-03-18 12:19:52 +01001675 size_t replyLength = (result == ERR_NONE) ? expandBufGetLength(pReply) : kJDWPHeaderLen;
1676 Set4BE(replyBuf + 0, replyLength);
Ian Rogersc0542af2014-09-03 16:16:56 -07001677 Set4BE(replyBuf + 4, request->GetId());
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001678 Set1(replyBuf + 8, kJDWPFlagReply);
1679 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001680
Ian Rogersc0542af2014-09-03 16:16:56 -07001681 CHECK_GT(expandBufGetLength(pReply), 0U) << GetCommandName(request) << " " << request->GetId();
Elliott Hughescb693062013-02-21 09:48:08 -08001682
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001683 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughescb693062013-02-21 09:48:08 -08001684 VLOG(jdwp) << "REPLY: " << GetCommandName(request) << " " << result << " (length=" << respLen << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001685 if (false) {
Ian Rogers43b2e0f2014-01-30 16:58:39 -08001686 VLOG(jdwp) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen, false, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001687 }
1688
Elliott Hughescb693062013-02-21 09:48:08 -08001689 VLOG(jdwp) << "----------";
1690
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001691 /*
1692 * Update last-activity timestamp. We really only need this during
1693 * the initial setup. Only update if this is a non-DDMS packet.
1694 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001695 if (request->GetCommandSet() != kJDWPDdmCmdSet) {
Ian Rogers37f3c962014-07-17 11:25:30 -07001696 last_activity_time_ms_.StoreSequentiallyConsistent(MilliTime());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001697 }
1698
1699 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001700 self->TransitionFromRunnableToSuspended(old_state);
Sebastien Hertz43c8d722014-03-18 12:19:52 +01001701
1702 return replyLength;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001703}
1704
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001705} // namespace JDWP
1706
1707} // namespace art