blob: f7f70f6ed7996dfeca9308e0c56af1831d2e6f66 [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"
Vladimir Marko80afd022015-05-19 18:08:00 +010035#include "utils.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080036
Elliott Hughes872d4ec2011-10-21 17:07:15 -070037namespace art {
38
39namespace JDWP {
40
Elliott Hughes4b9702c2013-02-20 18:13:24 -080041std::string DescribeField(const FieldId& field_id) {
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070042 return StringPrintf("%#" PRIx64 " (%s)", field_id, Dbg::GetFieldName(field_id).c_str());
Elliott Hughesa96836a2013-01-17 12:27:49 -080043}
44
Elliott Hughes4b9702c2013-02-20 18:13:24 -080045std::string DescribeMethod(const MethodId& method_id) {
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070046 return StringPrintf("%#" PRIx64 " (%s)", method_id, Dbg::GetMethodName(method_id).c_str());
Elliott Hughesa96836a2013-01-17 12:27:49 -080047}
48
Elliott Hughes4b9702c2013-02-20 18:13:24 -080049std::string DescribeRefTypeId(const RefTypeId& ref_type_id) {
Elliott Hughesa96836a2013-01-17 12:27:49 -080050 std::string signature("unknown");
Ian Rogersfc0e94b2013-09-23 23:51:32 -070051 Dbg::GetSignature(ref_type_id, &signature);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -080052 return StringPrintf("%#" PRIx64 " (%s)", ref_type_id, signature.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -070053}
54
Elliott Hughes4b9702c2013-02-20 18:13:24 -080055// Helper function: write a variable-width value into the output input buffer.
Elliott Hughes4993bbc2013-01-10 15:41:25 -080056static void WriteValue(ExpandBuf* pReply, int width, uint64_t value) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070057 switch (width) {
58 case 1: expandBufAdd1(pReply, value); break;
59 case 2: expandBufAdd2BE(pReply, value); break;
60 case 4: expandBufAdd4BE(pReply, value); break;
61 case 8: expandBufAdd8BE(pReply, value); break;
62 default: LOG(FATAL) << width; break;
63 }
64}
65
Elliott Hughes4993bbc2013-01-10 15:41:25 -080066static JdwpError WriteTaggedObject(ExpandBuf* reply, ObjectId object_id)
67 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
68 uint8_t tag;
Ian Rogersc0542af2014-09-03 16:16:56 -070069 JdwpError rc = Dbg::GetObjectTag(object_id, &tag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -080070 if (rc == ERR_NONE) {
71 expandBufAdd1(reply, tag);
72 expandBufAddObjectId(reply, object_id);
73 }
74 return rc;
75}
76
Elliott Hughes0cbaff52013-01-16 15:28:01 -080077static JdwpError WriteTaggedObjectList(ExpandBuf* reply, const std::vector<ObjectId>& objects)
78 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
79 expandBufAdd4BE(reply, objects.size());
80 for (size_t i = 0; i < objects.size(); ++i) {
81 JdwpError rc = WriteTaggedObject(reply, objects[i]);
82 if (rc != ERR_NONE) {
83 return rc;
84 }
85 }
86 return ERR_NONE;
87}
88
Elliott Hughes872d4ec2011-10-21 17:07:15 -070089/*
90 * Common code for *_InvokeMethod requests.
91 *
Elliott Hughes74847412012-06-20 18:10:21 -070092 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -070093 * expected-to-be-void return value of the called function.
94 */
Sebastien Hertz1558b572015-02-25 15:05:59 +010095static JdwpError RequestInvoke(JdwpState*, Request* request, ExpandBuf* pReply,
96 ObjectId thread_id, ObjectId object_id,
97 RefTypeId class_id, MethodId method_id, bool is_constructor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -070099 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700100
Ian Rogersc0542af2014-09-03 16:16:56 -0700101 int32_t arg_count = request->ReadSigned32("argument count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700102
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800103 VLOG(jdwp) << StringPrintf(" --> thread_id=%#" PRIx64 " object_id=%#" PRIx64,
104 thread_id, object_id);
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -0700105 VLOG(jdwp) << StringPrintf(" class_id=%#" PRIx64 " method_id=%#" PRIx64 " %s.%s",
106 class_id, method_id, Dbg::GetClassName(class_id).c_str(),
Elliott Hughesa96836a2013-01-17 12:27:49 -0800107 Dbg::GetMethodName(method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800108 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700109
Sebastien Hertz7d955652014-10-22 10:57:10 +0200110 std::unique_ptr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : nullptr);
111 std::unique_ptr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : nullptr);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800112 for (int32_t i = 0; i < arg_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700113 argTypes[i] = request->ReadTag();
Elliott Hughes45651fd2012-02-21 15:48:20 -0800114 size_t width = Dbg::GetTagWidth(argTypes[i]);
Ian Rogersc0542af2014-09-03 16:16:56 -0700115 argValues[i] = request->ReadValue(width);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800116 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#" PRIx64, width,
117 argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700118 }
119
Ian Rogersc0542af2014-09-03 16:16:56 -0700120 uint32_t options = request->ReadUnsigned32("InvokeOptions bit flags");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
122 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
123 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124
Elliott Hughes45651fd2012-02-21 15:48:20 -0800125 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 uint64_t resultValue;
127 ObjectId exceptObjId;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200128 JdwpError err = Dbg::InvokeMethod(thread_id, object_id, class_id, method_id, arg_count,
129 argValues.get(), argTypes.get(), options, &resultTag,
130 &resultValue, &exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800132 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700133 }
134
Sebastien Hertz1558b572015-02-25 15:05:59 +0100135 if (is_constructor) {
136 // If we invoked a constructor (which actually returns void), return the receiver,
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700137 // unless we threw, in which case we return null.
Sebastien Hertz1558b572015-02-25 15:05:59 +0100138 resultTag = JT_OBJECT;
139 resultValue = (exceptObjId == 0) ? object_id : 0;
140 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700141
Sebastien Hertz1558b572015-02-25 15:05:59 +0100142 size_t width = Dbg::GetTagWidth(resultTag);
143 expandBufAdd1(pReply, resultTag);
144 if (width != 0) {
145 WriteValue(pReply, width, resultValue);
146 }
147 expandBufAdd1(pReply, JT_OBJECT);
148 expandBufAddObjectId(pReply, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700149
Sebastien Hertz1558b572015-02-25 15:05:59 +0100150 VLOG(jdwp) << " --> returned " << resultTag
151 << StringPrintf(" %#" PRIx64 " (except=%#" PRIx64 ")", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152
Sebastien Hertz1558b572015-02-25 15:05:59 +0100153 /* show detailed debug output */
154 if (resultTag == JT_STRING && exceptObjId == 0) {
155 if (resultValue != 0) {
156 if (VLOG_IS_ON(jdwp)) {
157 std::string result_string;
158 JDWP::JdwpError error = Dbg::StringToUtf8(resultValue, &result_string);
159 CHECK_EQ(error, JDWP::ERR_NONE);
160 VLOG(jdwp) << " string '" << result_string << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700161 }
Sebastien Hertz1558b572015-02-25 15:05:59 +0100162 } else {
163 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700164 }
165 }
166
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700167 return err;
168}
169
Ian Rogersc0542af2014-09-03 16:16:56 -0700170static JdwpError VM_Version(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes927bd292013-01-17 10:40:13 -0800172 // Text information on runtime version.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700173 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800174 expandBufAddUtf8String(pReply, version);
Elliott Hughes927bd292013-01-17 10:40:13 -0800175
176 // JDWP version numbers, major and minor.
177 expandBufAdd4BE(pReply, 1);
178 expandBufAdd4BE(pReply, 6);
179
180 // "java.version".
181 expandBufAddUtf8String(pReply, "1.6.0");
182
183 // "java.vm.name".
184 expandBufAddUtf8String(pReply, "Dalvik");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700185
186 return ERR_NONE;
187}
188
189/*
190 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
191 * referenceTypeID. We need to send back more than one if the class has
192 * been loaded by multiple class loaders.
193 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700194static JdwpError VM_ClassesBySignature(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700195 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700196 std::string classDescriptor(request->ReadUtf8String());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700197
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800198 std::vector<RefTypeId> ids;
Ian Rogersc0542af2014-09-03 16:16:56 -0700199 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), &ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700200
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800201 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800203 for (size_t i = 0; i < ids.size(); ++i) {
204 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800205 JDWP::JdwpTypeTag type_tag;
206 uint32_t class_status;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200207 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, nullptr);
Elliott Hughes436e3722012-02-17 20:01:47 -0800208 if (status != ERR_NONE) {
209 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800210 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700211
Elliott Hughes436e3722012-02-17 20:01:47 -0800212 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800213 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800214 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700215 }
216
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700217 return ERR_NONE;
218}
219
220/*
221 * Handle request for the thread IDs of all running threads.
222 *
223 * We exclude ourselves from the list, because we don't allow ourselves
224 * to be suspended, and that violates some JDWP expectations.
225 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700226static JdwpError VM_AllThreads(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700228 std::vector<ObjectId> thread_ids;
Sebastien Hertza06430c2014-09-15 19:21:30 +0200229 Dbg::GetThreads(nullptr /* all thread groups */, &thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700230
Elliott Hughescaf76542012-06-28 16:08:22 -0700231 expandBufAdd4BE(pReply, thread_ids.size());
232 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
233 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700234 }
235
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700236 return ERR_NONE;
237}
238
239/*
240 * List all thread groups that do not have a parent.
241 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700242static JdwpError VM_TopLevelThreadGroups(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700244 /*
245 * TODO: maintain a list of parentless thread groups in the VM.
246 *
247 * For now, just return "system". Application threads are created
248 * in "main", which is a child of "system".
249 */
250 uint32_t groups = 1;
251 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700252 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
253 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700254
255 return ERR_NONE;
256}
257
258/*
259 * Respond with the sizes of the basic debugger types.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700261static JdwpError VM_IDSizes(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700262 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700263 expandBufAdd4BE(pReply, sizeof(FieldId));
264 expandBufAdd4BE(pReply, sizeof(MethodId));
265 expandBufAdd4BE(pReply, sizeof(ObjectId));
266 expandBufAdd4BE(pReply, sizeof(RefTypeId));
267 expandBufAdd4BE(pReply, sizeof(FrameId));
268 return ERR_NONE;
269}
270
Ian Rogersc0542af2014-09-03 16:16:56 -0700271static JdwpError VM_Dispose(JdwpState*, Request*, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700272 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100273 Dbg::Dispose();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700274 return ERR_NONE;
275}
276
277/*
278 * Suspend the execution of the application running in the VM (i.e. suspend
279 * all threads).
280 *
281 * This needs to increment the "suspend count" on all threads.
282 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700283static JdwpError VM_Suspend(JdwpState*, Request*, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700284 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800285 Thread* self = Thread::Current();
286 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700287 Dbg::SuspendVM();
jeffhaoa77f0f62012-12-05 17:19:31 -0800288 self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700289 return ERR_NONE;
290}
291
292/*
293 * Resume execution. Decrements the "suspend count" of all threads.
294 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700295static JdwpError VM_Resume(JdwpState*, Request*, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700297 Dbg::ResumeVM();
298 return ERR_NONE;
299}
300
Ian Rogersc0542af2014-09-03 16:16:56 -0700301static JdwpError VM_Exit(JdwpState* state, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700302 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700303 uint32_t exit_status = request->ReadUnsigned32("exit_status");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800304 state->ExitAfterReplying(exit_status);
305 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700306}
307
308/*
309 * Create a new string in the VM and return its ID.
310 *
311 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
312 * string "java.util.Arrays".)
313 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700314static JdwpError VM_CreateString(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700315 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700316 std::string str(request->ReadUtf8String());
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +0200317 ObjectId string_id;
318 JdwpError status = Dbg::CreateString(str, &string_id);
319 if (status != ERR_NONE) {
320 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700321 }
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +0200322 expandBufAddObjectId(pReply, string_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700323 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 }
Sebastien Hertz1558b572015-02-25 15:05:59 +0100714 return RequestInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700715}
716
717/*
718 * Create a new array object of the requested type and length.
719 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700720static JdwpError AT_newInstance(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700721 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700722 RefTypeId arrayTypeId = request->ReadRefTypeId();
723 int32_t length = request->ReadSigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700724
Elliott Hughes74847412012-06-20 18:10:21 -0700725 ObjectId object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700726 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, &object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800727 if (status != ERR_NONE) {
728 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800729 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700730 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700731 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700732 return ERR_NONE;
733}
734
735/*
736 * Return line number information for the method, if present.
737 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700738static JdwpError M_LineTable(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700739 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700740 RefTypeId refTypeId = request->ReadRefTypeId();
741 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742
Elliott Hughes74847412012-06-20 18:10:21 -0700743 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700744
745 return ERR_NONE;
746}
747
Ian Rogersc0542af2014-09-03 16:16:56 -0700748static JdwpError M_VariableTable(JdwpState*, Request* request, ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700749 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700750 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700751 RefTypeId class_id = request->ReadRefTypeId();
752 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700753
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800754 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
755 // information. That will cause Eclipse to make a best-effort attempt at displaying local
756 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
757 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700758 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700759 return ERR_NONE;
760}
761
Ian Rogersc0542af2014-09-03 16:16:56 -0700762static JdwpError M_VariableTable(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700763 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800764 return M_VariableTable(state, request, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800765}
766
Ian Rogersc0542af2014-09-03 16:16:56 -0700767static JdwpError M_VariableTableWithGeneric(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700768 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800769 return M_VariableTable(state, request, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800770}
771
Ian Rogersc0542af2014-09-03 16:16:56 -0700772static JdwpError M_Bytecodes(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes9777ba22013-01-17 09:04:19 -0800773 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700774 RefTypeId class_id = request->ReadRefTypeId();
775 MethodId method_id = request->ReadMethodId();
Elliott Hughes9777ba22013-01-17 09:04:19 -0800776
777 std::vector<uint8_t> bytecodes;
Ian Rogersc0542af2014-09-03 16:16:56 -0700778 JdwpError rc = Dbg::GetBytecodes(class_id, method_id, &bytecodes);
Elliott Hughes9777ba22013-01-17 09:04:19 -0800779 if (rc != ERR_NONE) {
780 return rc;
781 }
782
783 expandBufAdd4BE(reply, bytecodes.size());
784 for (size_t i = 0; i < bytecodes.size(); ++i) {
785 expandBufAdd1(reply, bytecodes[i]);
786 }
787
788 return ERR_NONE;
789}
790
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700791/*
792 * Given an object reference, return the runtime type of the object
793 * (class or array).
794 *
Elliott Hughes74847412012-06-20 18:10:21 -0700795 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700796 * passed in here.
797 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700798static JdwpError OR_ReferenceType(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700799 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700800 ObjectId object_id = request->ReadObjectId();
Elliott Hughes74847412012-06-20 18:10:21 -0700801 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700802}
803
804/*
805 * Get values from the fields of an object.
806 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700807static JdwpError OR_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700808 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700809 ObjectId object_id = request->ReadObjectId();
810 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700811
Elliott Hughes0cf74332012-02-23 23:14:00 -0800812 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800813 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700814 FieldId fieldId = request->ReadFieldId();
Elliott Hughes74847412012-06-20 18:10:21 -0700815 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800816 if (status != ERR_NONE) {
817 return status;
818 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700819 }
820
821 return ERR_NONE;
822}
823
824/*
825 * Set values in the fields of an object.
826 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700827static JdwpError OR_SetValues(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700828 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700829 ObjectId object_id = request->ReadObjectId();
830 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700831
Elliott Hughesa96836a2013-01-17 12:27:49 -0800832 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700833 FieldId fieldId = request->ReadFieldId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700834
Elliott Hughesaed4be92011-12-02 16:16:23 -0800835 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800836 size_t width = Dbg::GetTagWidth(fieldTag);
Ian Rogersc0542af2014-09-03 16:16:56 -0700837 uint64_t value = request->ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700838
Elliott Hughes2435a572012-02-17 16:07:41 -0800839 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700840 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800841 if (status != ERR_NONE) {
842 return status;
843 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700844 }
845
846 return ERR_NONE;
847}
848
Ian Rogersc0542af2014-09-03 16:16:56 -0700849static JdwpError OR_MonitorInfo(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughesf327e072013-01-09 16:01:26 -0800850 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700851 ObjectId object_id = request->ReadObjectId();
Elliott Hughesf327e072013-01-09 16:01:26 -0800852 return Dbg::GetMonitorInfo(object_id, reply);
853}
854
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700855/*
856 * Invoke an instance method. The invocation must occur in the specified
857 * thread, which must have been suspended by an event.
858 *
859 * The call is synchronous. All threads in the VM are resumed, unless the
860 * SINGLE_THREADED flag is set.
861 *
862 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
863 * object), it will try to invoke the object's toString() function. This
864 * feature becomes crucial when examining ArrayLists with Eclipse.
865 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700866static JdwpError OR_InvokeMethod(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700867 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700868 ObjectId object_id = request->ReadObjectId();
869 ObjectId thread_id = request->ReadThreadId();
870 RefTypeId class_id = request->ReadRefTypeId();
871 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700872
Sebastien Hertz1558b572015-02-25 15:05:59 +0100873 return RequestInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700874}
875
Ian Rogersc0542af2014-09-03 16:16:56 -0700876static JdwpError OR_DisableCollection(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700877 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700878 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800879 return Dbg::DisableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700880}
881
Ian Rogersc0542af2014-09-03 16:16:56 -0700882static JdwpError OR_EnableCollection(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::EnableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700886}
887
Ian Rogersc0542af2014-09-03 16:16:56 -0700888static JdwpError OR_IsCollected(JdwpState*, Request* request, ExpandBuf* pReply)
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 bool is_collected;
Ian Rogersc0542af2014-09-03 16:16:56 -0700892 JdwpError rc = Dbg::IsCollected(object_id, &is_collected);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800893 expandBufAdd1(pReply, is_collected ? 1 : 0);
894 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700895}
896
Ian Rogersc0542af2014-09-03 16:16:56 -0700897static JdwpError OR_ReferringObjects(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800898 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700899 ObjectId object_id = request->ReadObjectId();
900 int32_t max_count = request->ReadSigned32("max count");
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800901 if (max_count < 0) {
902 return ERR_ILLEGAL_ARGUMENT;
903 }
904
905 std::vector<ObjectId> referring_objects;
Ian Rogersc0542af2014-09-03 16:16:56 -0700906 JdwpError rc = Dbg::GetReferringObjects(object_id, max_count, &referring_objects);
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800907 if (rc != ERR_NONE) {
908 return rc;
909 }
910
911 return WriteTaggedObjectList(reply, referring_objects);
912}
913
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700914/*
915 * Return the string value in a string object.
916 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700917static JdwpError SR_Value(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700918 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700919 ObjectId stringObject = request->ReadObjectId();
Sebastien Hertzb0b0b492014-09-15 11:27:27 +0200920 std::string str;
921 JDWP::JdwpError error = Dbg::StringToUtf8(stringObject, &str);
922 if (error != JDWP::ERR_NONE) {
923 return error;
924 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700925
Ian Rogers68b56852014-08-29 20:19:11 -0700926 VLOG(jdwp) << StringPrintf(" --> %s", PrintableString(str.c_str()).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700927
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800928 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700929
930 return ERR_NONE;
931}
932
933/*
934 * Return a thread's name.
935 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700936static JdwpError TR_Name(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700937 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700938 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700939
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800940 std::string name;
Ian Rogersc0542af2014-09-03 16:16:56 -0700941 JdwpError error = Dbg::GetThreadName(thread_id, &name);
Elliott Hughes221229c2013-01-08 18:17:50 -0800942 if (error != ERR_NONE) {
943 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700944 }
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800945 VLOG(jdwp) << StringPrintf(" Name of thread %#" PRIx64 " is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800946 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700947
948 return ERR_NONE;
949}
950
951/*
952 * Suspend the specified thread.
953 *
954 * It's supposed to remain suspended even if interpreted code wants to
955 * resume it; only the JDI is allowed to resume it.
956 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700957static JdwpError TR_Suspend(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700958 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700959 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700960
Elliott Hughes74847412012-06-20 18:10:21 -0700961 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700962 LOG(INFO) << " Warning: ignoring request to suspend self";
963 return ERR_THREAD_NOT_SUSPENDED;
964 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800965
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 Thread* self = Thread::Current();
967 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
968 JdwpError result = Dbg::SuspendThread(thread_id);
969 self->TransitionFromSuspendedToRunnable();
970 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700971}
972
973/*
974 * Resume the specified thread.
975 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700976static JdwpError TR_Resume(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700977 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700978 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700979
Elliott Hughes74847412012-06-20 18:10:21 -0700980 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700981 LOG(INFO) << " Warning: ignoring request to resume self";
982 return ERR_NONE;
983 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800984
Elliott Hughes74847412012-06-20 18:10:21 -0700985 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700986 return ERR_NONE;
987}
988
989/*
990 * Return status of specified thread.
991 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700992static JdwpError TR_Status(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700993 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700994 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800996 JDWP::JdwpThreadStatus threadStatus;
997 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -0800998 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
999 if (error != ERR_NONE) {
1000 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001001 }
1002
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001003 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001004
1005 expandBufAdd4BE(pReply, threadStatus);
1006 expandBufAdd4BE(pReply, suspendStatus);
1007
1008 return ERR_NONE;
1009}
1010
1011/*
1012 * Return the thread group that the specified thread is a member of.
1013 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001014static JdwpError TR_ThreadGroup(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001015 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001016 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001017 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001018}
1019
1020/*
1021 * Return the current call stack of a suspended thread.
1022 *
1023 * If the thread isn't suspended, the error code isn't defined, but should
1024 * be THREAD_NOT_SUSPENDED.
1025 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001026static JdwpError TR_Frames(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001027 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001028 ObjectId thread_id = request->ReadThreadId();
1029 uint32_t start_frame = request->ReadUnsigned32("start frame");
1030 uint32_t length = request->ReadUnsigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031
Elliott Hughes221229c2013-01-08 18:17:50 -08001032 size_t actual_frame_count;
Ian Rogersc0542af2014-09-03 16:16:56 -07001033 JdwpError error = Dbg::GetThreadFrameCount(thread_id, &actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001034 if (error != ERR_NONE) {
1035 return error;
1036 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001037
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001038 if (actual_frame_count <= 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001039 return ERR_THREAD_NOT_SUSPENDED; // 0 means no managed frames (which means "in native").
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001040 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001041
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001042 if (start_frame > actual_frame_count) {
1043 return ERR_INVALID_INDEX;
1044 }
1045 if (length == static_cast<uint32_t>(-1)) {
1046 length = actual_frame_count - start_frame;
1047 }
1048 if (start_frame + length > actual_frame_count) {
1049 return ERR_INVALID_LENGTH;
1050 }
1051
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001052 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001053}
1054
1055/*
1056 * Returns the #of frames on the specified thread, which must be suspended.
1057 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001058static JdwpError TR_FrameCount(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001059 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001060 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001061
Elliott Hughes221229c2013-01-08 18:17:50 -08001062 size_t frame_count;
Ian Rogersc0542af2014-09-03 16:16:56 -07001063 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, &frame_count);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001064 if (rc != ERR_NONE) {
1065 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001066 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001067 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001068
1069 return ERR_NONE;
1070}
1071
Ian Rogersc0542af2014-09-03 16:16:56 -07001072static JdwpError TR_OwnedMonitors(Request* request, ExpandBuf* reply, bool with_stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001074 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001075
1076 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001077 std::vector<uint32_t> stack_depths;
Ian Rogersc0542af2014-09-03 16:16:56 -07001078 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, &monitors, &stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001079 if (rc != ERR_NONE) {
1080 return rc;
1081 }
1082
1083 expandBufAdd4BE(reply, monitors.size());
1084 for (size_t i = 0; i < monitors.size(); ++i) {
1085 rc = WriteTaggedObject(reply, monitors[i]);
1086 if (rc != ERR_NONE) {
1087 return rc;
1088 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001089 if (with_stack_depths) {
1090 expandBufAdd4BE(reply, stack_depths[i]);
1091 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001092 }
1093 return ERR_NONE;
1094}
1095
Ian Rogersc0542af2014-09-03 16:16:56 -07001096static JdwpError TR_OwnedMonitors(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001098 return TR_OwnedMonitors(request, reply, false);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001099}
1100
Ian Rogersc0542af2014-09-03 16:16:56 -07001101static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001103 return TR_OwnedMonitors(request, reply, true);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001104}
1105
Ian Rogersc0542af2014-09-03 16:16:56 -07001106static JdwpError TR_CurrentContendedMonitor(JdwpState*, Request* request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001108 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001109
Elliott Hughesf9501702013-01-11 11:22:27 -08001110 ObjectId contended_monitor;
Ian Rogersc0542af2014-09-03 16:16:56 -07001111 JdwpError rc = Dbg::GetContendedMonitor(thread_id, &contended_monitor);
Elliott Hughesf9501702013-01-11 11:22:27 -08001112 if (rc != ERR_NONE) {
1113 return rc;
1114 }
1115 return WriteTaggedObject(reply, contended_monitor);
1116}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001117
Ian Rogersc0542af2014-09-03 16:16:56 -07001118static JdwpError TR_Interrupt(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughesf9501702013-01-11 11:22:27 -08001119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001120 UNUSED(reply);
Ian Rogersc0542af2014-09-03 16:16:56 -07001121 ObjectId thread_id = request->ReadThreadId();
Elliott Hughesf9501702013-01-11 11:22:27 -08001122 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001123}
1124
1125/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001126 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001127 *
1128 * (The thread *might* still be running -- it might not have examined
1129 * its suspend count recently.)
1130 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001131static JdwpError TR_DebugSuspendCount(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001133 ObjectId thread_id = request->ReadThreadId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001134 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001135}
1136
1137/*
1138 * Return the name of a thread group.
1139 *
1140 * The Eclipse debugger recognizes "main" and "system" as special.
1141 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001142static JdwpError TGR_Name(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001144 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001145 return Dbg::GetThreadGroupName(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001146}
1147
1148/*
1149 * Returns the thread group -- if any -- that contains the specified
1150 * thread group.
1151 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001152static JdwpError TGR_Parent(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001154 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001155 return Dbg::GetThreadGroupParent(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001156}
1157
1158/*
1159 * Return the active threads and thread groups that are part of the
1160 * specified thread group.
1161 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001162static JdwpError TGR_Children(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001164 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001165 return Dbg::GetThreadGroupChildren(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001166}
1167
1168/*
1169 * Return the #of components in the array.
1170 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001171static JdwpError AR_Length(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001173 ObjectId array_id = request->ReadArrayId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001174
Ian Rogersc0542af2014-09-03 16:16:56 -07001175 int32_t length;
1176 JdwpError status = Dbg::GetArrayLength(array_id, &length);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001177 if (status != ERR_NONE) {
1178 return status;
1179 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001180 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001181
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001182 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001183
1184 return ERR_NONE;
1185}
1186
1187/*
1188 * Return the values from an array.
1189 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001190static JdwpError AR_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001191 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001192 ObjectId array_id = request->ReadArrayId();
1193 uint32_t offset = request->ReadUnsigned32("offset");
1194 uint32_t length = request->ReadUnsigned32("length");
Elliott Hughesa96836a2013-01-17 12:27:49 -08001195 return Dbg::OutputArray(array_id, offset, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001196}
1197
1198/*
1199 * Set values in an array.
1200 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001201static JdwpError AR_SetValues(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001203 ObjectId array_id = request->ReadArrayId();
1204 uint32_t offset = request->ReadUnsigned32("offset");
1205 uint32_t count = request->ReadUnsigned32("count");
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001206 return Dbg::SetArrayElements(array_id, offset, count, request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001207}
1208
Ian Rogersc0542af2014-09-03 16:16:56 -07001209static JdwpError CLR_VisibleClasses(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001211 request->ReadObjectId(); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001212 // TODO: we should only return classes which have the given class loader as a defining or
1213 // initiating loader. The former would be easy; the latter is hard, because we don't have
1214 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001215 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001216}
1217
1218/*
1219 * Set an event trigger.
1220 *
1221 * Reply with a requestID.
1222 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001223static JdwpError ER_Set(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001225 JdwpEventKind event_kind = request->ReadEnum1<JdwpEventKind>("event kind");
1226 JdwpSuspendPolicy suspend_policy = request->ReadEnum1<JdwpSuspendPolicy>("suspend policy");
1227 int32_t modifier_count = request->ReadSigned32("modifier count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001228
Elliott Hughesa96836a2013-01-17 12:27:49 -08001229 CHECK_LT(modifier_count, 256); /* reasonableness check */
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001230
Elliott Hughesa96836a2013-01-17 12:27:49 -08001231 JdwpEvent* pEvent = EventAlloc(modifier_count);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001232 pEvent->eventKind = event_kind;
1233 pEvent->suspend_policy = suspend_policy;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001234 pEvent->modCount = modifier_count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001235
1236 /*
1237 * Read modifiers. Ordering may be significant (see explanation of Count
1238 * mods in JDWP doc).
1239 */
Elliott Hughesa96836a2013-01-17 12:27:49 -08001240 for (int32_t i = 0; i < modifier_count; ++i) {
Elliott Hughes972a47b2012-02-21 18:16:06 -08001241 JdwpEventMod& mod = pEvent->mods[i];
Ian Rogersc0542af2014-09-03 16:16:56 -07001242 mod.modKind = request->ReadModKind();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001243 switch (mod.modKind) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001244 case MK_COUNT:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001245 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001246 // Report once, when "--count" reaches 0.
Ian Rogersc0542af2014-09-03 16:16:56 -07001247 uint32_t count = request->ReadUnsigned32("count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001248 if (count == 0) {
1249 return ERR_INVALID_COUNT;
1250 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001251 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001252 }
1253 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001254 case MK_CONDITIONAL:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001255 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001256 // Conditional on expression.
Ian Rogersc0542af2014-09-03 16:16:56 -07001257 uint32_t exprId = request->ReadUnsigned32("expr id");
Elliott Hughes972a47b2012-02-21 18:16:06 -08001258 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001259 }
1260 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001261 case MK_THREAD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001262 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001263 // Only report events in specified thread.
Ian Rogersc0542af2014-09-03 16:16:56 -07001264 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001265 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001266 }
1267 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001268 case MK_CLASS_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001269 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001270 // For ClassPrepare, MethodEntry.
Ian Rogersc0542af2014-09-03 16:16:56 -07001271 RefTypeId class_id = request->ReadRefTypeId();
Elliott Hughes74847412012-06-20 18:10:21 -07001272 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001273 }
1274 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001275 case MK_CLASS_MATCH:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001276 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001277 // Restrict events to matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001278 // pattern is "java.foo.*", we want "java/foo/*".
Ian Rogersc0542af2014-09-03 16:16:56 -07001279 std::string pattern(request->ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001280 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001281 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001282 }
1283 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001284 case MK_CLASS_EXCLUDE:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001285 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001286 // Restrict events to non-matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001287 // pattern is "java.foo.*", we want "java/foo/*".
Ian Rogersc0542af2014-09-03 16:16:56 -07001288 std::string pattern(request->ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001289 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001290 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001291 }
1292 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001293 case MK_LOCATION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001294 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001295 // Restrict certain events based on location.
Ian Rogersc0542af2014-09-03 16:16:56 -07001296 JdwpLocation location = request->ReadLocation();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001297 mod.locationOnly.loc = location;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001298 }
1299 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001300 case MK_EXCEPTION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001301 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001302 // Modifies EK_EXCEPTION events,
Ian Rogersc0542af2014-09-03 16:16:56 -07001303 mod.exceptionOnly.refTypeId = request->ReadRefTypeId(); // null => all exceptions.
1304 mod.exceptionOnly.caught = request->ReadEnum1<uint8_t>("caught");
1305 mod.exceptionOnly.uncaught = request->ReadEnum1<uint8_t>("uncaught");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001306 }
1307 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001308 case MK_FIELD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001309 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001310 // For field access/modification events.
Ian Rogersc0542af2014-09-03 16:16:56 -07001311 RefTypeId declaring = request->ReadRefTypeId();
1312 FieldId fieldId = request->ReadFieldId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001313 mod.fieldOnly.refTypeId = declaring;
1314 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001315 }
1316 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001317 case MK_STEP:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001318 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001319 // For use with EK_SINGLE_STEP.
Ian Rogersc0542af2014-09-03 16:16:56 -07001320 ObjectId thread_id = request->ReadThreadId();
1321 uint32_t size = request->ReadUnsigned32("step size");
1322 uint32_t depth = request->ReadUnsigned32("step depth");
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001323 VLOG(jdwp) << StringPrintf(" Step: thread=%#" PRIx64, thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001324 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1325
Elliott Hughes74847412012-06-20 18:10:21 -07001326 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001327 mod.step.size = size;
1328 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001329 }
1330 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001331 case MK_INSTANCE_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001332 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001333 // Report events related to a specific object.
Ian Rogersc0542af2014-09-03 16:16:56 -07001334 ObjectId instance = request->ReadObjectId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001335 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001336 }
1337 break;
1338 default:
Sebastien Hertz6d7839e2014-12-05 10:52:15 +01001339 LOG(WARNING) << "Unsupported modifier " << mod.modKind << " for event " << pEvent->eventKind;
1340 // Free allocated event to avoid leak before leaving.
1341 EventFree(pEvent);
1342 return JDWP::ERR_NOT_IMPLEMENTED;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001343 }
1344 }
1345
1346 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001347 * We reply with an integer "requestID".
1348 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001349 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001350 expandBufAdd4BE(pReply, requestId);
1351
1352 pEvent->requestId = requestId;
1353
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001354 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001355
1356 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001357 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001358 if (err != ERR_NONE) {
1359 /* registration failed, probably because event is bogus */
1360 EventFree(pEvent);
1361 LOG(WARNING) << "WARNING: event request rejected";
1362 }
1363 return err;
1364}
1365
Ian Rogersc0542af2014-09-03 16:16:56 -07001366static JdwpError ER_Clear(JdwpState* state, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001367 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001368 request->ReadEnum1<JdwpEventKind>("event kind");
1369 uint32_t requestId = request->ReadUnsigned32("request id");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001370
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001371 // Failure to find an event with a matching ID is a no-op
1372 // and does not return an error.
Elliott Hughes761928d2011-11-16 18:33:03 -08001373 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001374 return ERR_NONE;
1375}
1376
1377/*
1378 * Return the values of arguments and local variables.
1379 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001380static JdwpError SF_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001381 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz8009f392014-09-01 17:07:11 +02001382 return Dbg::GetLocalValues(request, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001383}
1384
1385/*
1386 * Set the values of arguments and local variables.
1387 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001388static JdwpError SF_SetValues(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz8009f392014-09-01 17:07:11 +02001390 return Dbg::SetLocalValues(request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001391}
1392
Ian Rogersc0542af2014-09-03 16:16:56 -07001393static JdwpError SF_ThisObject(JdwpState*, Request* request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001394 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001395 ObjectId thread_id = request->ReadThreadId();
1396 FrameId frame_id = request->ReadFrameId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001397
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001398 ObjectId object_id;
1399 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001400 if (rc != ERR_NONE) {
1401 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001402 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001403
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001404 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001405}
1406
1407/*
1408 * Return the reference type reflected by this class object.
1409 *
1410 * This appears to be required because ReferenceTypeId values are NEVER
1411 * reused, whereas ClassIds can be recycled like any other object. (Either
1412 * that, or I have no idea what this is for.)
1413 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001414static JdwpError COR_ReflectedType(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001415 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001416 RefTypeId class_object_id = request->ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001417 return Dbg::GetReflectedType(class_object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001418}
1419
1420/*
1421 * Handle a DDM packet with a single chunk in it.
1422 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001423static JdwpError DDM_Chunk(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001424 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001425 state->NotifyDdmsActive();
Sebastien Hertz7d955652014-10-22 10:57:10 +02001426 uint8_t* replyBuf = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001427 int replyLen = -1;
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001428 if (Dbg::DdmHandlePacket(request, &replyBuf, &replyLen)) {
1429 // If they want to send something back, we copy it into the buffer.
1430 // TODO: consider altering the JDWP stuff to hold the packet header
1431 // in a separate buffer. That would allow us to writev() DDM traffic
1432 // instead of copying it into the expanding buffer. The reduction in
1433 // heap requirements is probably more valuable than the efficiency.
1434 CHECK_GT(replyLen, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001435 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1436 free(replyBuf);
1437 }
1438 return ERR_NONE;
1439}
1440
1441/*
1442 * Handler map decl.
1443 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001444typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, Request* request, ExpandBuf* reply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001445
1446struct JdwpHandlerMap {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001447 uint8_t cmdSet;
1448 uint8_t cmd;
1449 JdwpRequestHandler func;
1450 const char* name;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001451};
1452
1453/*
1454 * Map commands to functions.
1455 *
1456 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1457 * and 128-256 are vendor-defined.
1458 */
Elliott Hughescb693062013-02-21 09:48:08 -08001459static const JdwpHandlerMap gHandlers[] = {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001460 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001461 { 1, 1, VM_Version, "VirtualMachine.Version" },
1462 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1463 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1464 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1465 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1466 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1467 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1468 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1469 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1470 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1471 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1472 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1473 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1474 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001475 { 1, 15, nullptr, "VirtualMachine.HoldEvents" },
1476 { 1, 16, nullptr, "VirtualMachine.ReleaseEvents" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001477 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001478 { 1, 18, nullptr, "VirtualMachine.RedefineClasses" },
1479 { 1, 19, nullptr, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001480 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001481 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001482
1483 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001484 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1485 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1486 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1487 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1488 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1489 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1490 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001491 { 2, 8, nullptr, "ReferenceType.NestedTypes" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001492 { 2, 9, RT_Status, "ReferenceType.Status" },
1493 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1494 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001495 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1496 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001497 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1498 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughes3b78c942013-01-15 17:35:41 -08001499 { 2, 16, RT_Instances, "ReferenceType.Instances" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001500 { 2, 17, nullptr, "ReferenceType.ClassFileVersion" },
1501 { 2, 18, nullptr, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001502
1503 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001504 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1505 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1506 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1507 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001508
1509 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001510 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001511
1512 /* InterfaceType command set (5) */
1513
1514 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001515 { 6, 1, M_LineTable, "Method.LineTable" },
1516 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughes9777ba22013-01-17 09:04:19 -08001517 { 6, 3, M_Bytecodes, "Method.Bytecodes" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001518 { 6, 4, nullptr, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001519 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001520
1521 /* Field command set (8) */
1522
1523 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001524 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1525 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1526 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001527 { 9, 4, nullptr, "ObjectReference.UNUSED" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001528 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1529 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001530 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001531 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1532 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001533 { 9, 10, OR_ReferringObjects, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001534
1535 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001536 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001537
1538 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001539 { 11, 1, TR_Name, "ThreadReference.Name" },
1540 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1541 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1542 { 11, 4, TR_Status, "ThreadReference.Status" },
1543 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1544 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1545 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1546 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1547 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001548 { 11, 10, nullptr, "ThreadReference.Stop" },
Elliott Hughes734b8c62013-01-11 15:32:45 -08001549 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1550 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1551 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001552 { 11, 14, nullptr, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001553
1554 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001555 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1556 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1557 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001558
1559 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001560 { 13, 1, AR_Length, "ArrayReference.Length" },
1561 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1562 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001563
1564 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001565 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001566
1567 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001568 { 15, 1, ER_Set, "EventRequest.Set" },
1569 { 15, 2, ER_Clear, "EventRequest.Clear" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001570 { 15, 3, nullptr, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001571
1572 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001573 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1574 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1575 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001576 { 16, 4, nullptr, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001577
1578 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001579 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001580
1581 /* Event command set (64) */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001582 { 64, 100, nullptr, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001583
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001584 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001585};
1586
Ian Rogersc0542af2014-09-03 16:16:56 -07001587static const char* GetCommandName(Request* request) {
Elliott Hughescb693062013-02-21 09:48:08 -08001588 for (size_t i = 0; i < arraysize(gHandlers); ++i) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001589 if (gHandlers[i].cmdSet == request->GetCommandSet() &&
1590 gHandlers[i].cmd == request->GetCommand()) {
Elliott Hughescb693062013-02-21 09:48:08 -08001591 return gHandlers[i].name;
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001592 }
1593 }
1594 return "?UNKNOWN?";
1595}
1596
Ian Rogersc0542af2014-09-03 16:16:56 -07001597static std::string DescribeCommand(Request* request) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001598 std::string result;
Elliott Hughes64f574f2013-02-20 14:57:12 -08001599 result += "REQUEST: ";
Elliott Hughescb693062013-02-21 09:48:08 -08001600 result += GetCommandName(request);
Ian Rogersc0542af2014-09-03 16:16:56 -07001601 result += StringPrintf(" (length=%zu id=0x%06x)", request->GetLength(), request->GetId());
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001602 return result;
1603}
1604
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001605/*
1606 * Process a request from the debugger.
1607 *
1608 * On entry, the JDWP thread is in VMWAIT.
1609 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001610size_t JdwpState::ProcessRequest(Request* request, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001611 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001612
Ian Rogersc0542af2014-09-03 16:16:56 -07001613 if (request->GetCommandSet() != kJDWPDdmCmdSet) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001614 /*
1615 * Activity from a debugger, not merely ddms. Mark us as having an
1616 * active debugger session, and zero out the last-activity timestamp
1617 * so waitForDebugger() doesn't return if we stall for a bit here.
1618 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001619 Dbg::GoActive();
Ian Rogers37f3c962014-07-17 11:25:30 -07001620 last_activity_time_ms_.StoreSequentiallyConsistent(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001621 }
1622
1623 /*
1624 * If a debugger event has fired in another thread, wait until the
Sebastien Hertz2bf93f42015-01-09 18:44:05 +01001625 * initiating thread has suspended itself before processing commands
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001626 * from the debugger. Otherwise we (the JDWP thread) could be told to
1627 * resume the thread before it has suspended.
1628 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001629 * Note that we MUST clear the event token before waking the event
1630 * thread up, or risk waiting for the thread to suspend after we've
1631 * told it to resume.
1632 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +01001633 AcquireJdwpTokenForCommand();
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001634
1635 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001636 * Tell the VM that we're running and shouldn't be interrupted by GC.
1637 * Do this after anything that can stall indefinitely.
1638 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001639 Thread* self = Thread::Current();
1640 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001641
1642 expandBufAddSpace(pReply, kJDWPHeaderLen);
1643
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001644 size_t i;
Elliott Hughescb693062013-02-21 09:48:08 -08001645 for (i = 0; i < arraysize(gHandlers); ++i) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001646 if (gHandlers[i].cmdSet == request->GetCommandSet() &&
1647 gHandlers[i].cmd == request->GetCommand() &&
1648 gHandlers[i].func != nullptr) {
Elliott Hughescb693062013-02-21 09:48:08 -08001649 VLOG(jdwp) << DescribeCommand(request);
1650 result = (*gHandlers[i].func)(this, request, pReply);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001651 if (result == ERR_NONE) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001652 request->CheckConsumed();
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001653 }
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001654 self->AssertNoPendingException();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001655 break;
1656 }
1657 }
Elliott Hughescb693062013-02-21 09:48:08 -08001658 if (i == arraysize(gHandlers)) {
1659 LOG(ERROR) << "Command not implemented: " << DescribeCommand(request);
Ian Rogersc0542af2014-09-03 16:16:56 -07001660 LOG(ERROR) << HexDump(request->data(), request->size(), false, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001661 result = ERR_NOT_IMPLEMENTED;
1662 }
1663
1664 /*
1665 * Set up the reply header.
1666 *
1667 * If we encountered an error, only send the header back.
1668 */
1669 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Sebastien Hertz43c8d722014-03-18 12:19:52 +01001670 size_t replyLength = (result == ERR_NONE) ? expandBufGetLength(pReply) : kJDWPHeaderLen;
1671 Set4BE(replyBuf + 0, replyLength);
Ian Rogersc0542af2014-09-03 16:16:56 -07001672 Set4BE(replyBuf + 4, request->GetId());
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001673 Set1(replyBuf + 8, kJDWPFlagReply);
1674 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001675
Ian Rogersc0542af2014-09-03 16:16:56 -07001676 CHECK_GT(expandBufGetLength(pReply), 0U) << GetCommandName(request) << " " << request->GetId();
Elliott Hughescb693062013-02-21 09:48:08 -08001677
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001678 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughescb693062013-02-21 09:48:08 -08001679 VLOG(jdwp) << "REPLY: " << GetCommandName(request) << " " << result << " (length=" << respLen << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001680 if (false) {
Ian Rogers43b2e0f2014-01-30 16:58:39 -08001681 VLOG(jdwp) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen, false, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001682 }
1683
Elliott Hughescb693062013-02-21 09:48:08 -08001684 VLOG(jdwp) << "----------";
1685
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001686 /*
1687 * Update last-activity timestamp. We really only need this during
1688 * the initial setup. Only update if this is a non-DDMS packet.
1689 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001690 if (request->GetCommandSet() != kJDWPDdmCmdSet) {
Ian Rogers37f3c962014-07-17 11:25:30 -07001691 last_activity_time_ms_.StoreSequentiallyConsistent(MilliTime());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001692 }
1693
1694 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001695 self->TransitionFromRunnableToSuspended(old_state);
Sebastien Hertz43c8d722014-03-18 12:19:52 +01001696
1697 return replyLength;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001698}
1699
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001700} // namespace JDWP
1701
1702} // namespace art