blob: 964c5679d735c6770acbd4d558dd2bf31f109210 [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "atomic.h"
Ian Rogers43b2e0f2014-01-30 16:58:39 -080026#include "base/hex_dump.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
28#include "base/macros.h"
29#include "debugger.h"
30#include "jdwp/jdwp_constants.h"
31#include "jdwp/jdwp_event.h"
32#include "jdwp/jdwp_expand_buf.h"
33#include "jdwp/jdwp_priv.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070035#include "scoped_thread_state_change-inl.h"
Ian Rogers693ff612013-02-01 10:56:12 -080036#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010037#include "utils.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080038
Elliott Hughes872d4ec2011-10-21 17:07:15 -070039namespace art {
40
41namespace JDWP {
42
Andreas Gampe46ee31b2016-12-14 10:11:49 -080043using android::base::StringPrintf;
44
Elliott Hughes4b9702c2013-02-20 18:13:24 -080045std::string DescribeField(const FieldId& field_id) {
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070046 return StringPrintf("%#" PRIx64 " (%s)", field_id, Dbg::GetFieldName(field_id).c_str());
Elliott Hughesa96836a2013-01-17 12:27:49 -080047}
48
Elliott Hughes4b9702c2013-02-20 18:13:24 -080049std::string DescribeMethod(const MethodId& method_id) {
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070050 return StringPrintf("%#" PRIx64 " (%s)", method_id, Dbg::GetMethodName(method_id).c_str());
Elliott Hughesa96836a2013-01-17 12:27:49 -080051}
52
Elliott Hughes4b9702c2013-02-20 18:13:24 -080053std::string DescribeRefTypeId(const RefTypeId& ref_type_id) {
Elliott Hughesa96836a2013-01-17 12:27:49 -080054 std::string signature("unknown");
Ian Rogersfc0e94b2013-09-23 23:51:32 -070055 Dbg::GetSignature(ref_type_id, &signature);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -080056 return StringPrintf("%#" PRIx64 " (%s)", ref_type_id, signature.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -070057}
58
Elliott Hughes4993bbc2013-01-10 15:41:25 -080059static JdwpError WriteTaggedObject(ExpandBuf* reply, ObjectId object_id)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070060 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -080061 uint8_t tag;
Ian Rogersc0542af2014-09-03 16:16:56 -070062 JdwpError rc = Dbg::GetObjectTag(object_id, &tag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -080063 if (rc == ERR_NONE) {
64 expandBufAdd1(reply, tag);
65 expandBufAddObjectId(reply, object_id);
66 }
67 return rc;
68}
69
Elliott Hughes0cbaff52013-01-16 15:28:01 -080070static JdwpError WriteTaggedObjectList(ExpandBuf* reply, const std::vector<ObjectId>& objects)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes0cbaff52013-01-16 15:28:01 -080072 expandBufAdd4BE(reply, objects.size());
73 for (size_t i = 0; i < objects.size(); ++i) {
74 JdwpError rc = WriteTaggedObject(reply, objects[i]);
75 if (rc != ERR_NONE) {
76 return rc;
77 }
78 }
79 return ERR_NONE;
80}
81
Elliott Hughes872d4ec2011-10-21 17:07:15 -070082/*
83 * Common code for *_InvokeMethod requests.
84 *
Elliott Hughes74847412012-06-20 18:10:21 -070085 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -070086 * expected-to-be-void return value of the called function.
87 */
Sebastien Hertzcbc50642015-06-01 17:33:12 +020088static JdwpError RequestInvoke(JdwpState*, Request* request,
Sebastien Hertz1558b572015-02-25 15:05:59 +010089 ObjectId thread_id, ObjectId object_id,
90 RefTypeId class_id, MethodId method_id, bool is_constructor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070091 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -070092 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070093
Ian Rogersc0542af2014-09-03 16:16:56 -070094 int32_t arg_count = request->ReadSigned32("argument count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -070095
Ian Rogersd9e4e0c2014-01-23 20:11:40 -080096 VLOG(jdwp) << StringPrintf(" --> thread_id=%#" PRIx64 " object_id=%#" PRIx64,
97 thread_id, object_id);
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070098 VLOG(jdwp) << StringPrintf(" class_id=%#" PRIx64 " method_id=%#" PRIx64 " %s.%s",
99 class_id, method_id, Dbg::GetClassName(class_id).c_str(),
Elliott Hughesa96836a2013-01-17 12:27:49 -0800100 Dbg::GetMethodName(method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800101 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700102
Sebastien Hertz7d955652014-10-22 10:57:10 +0200103 std::unique_ptr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : nullptr);
104 std::unique_ptr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : nullptr);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800105 for (int32_t i = 0; i < arg_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700106 argTypes[i] = request->ReadTag();
Elliott Hughes45651fd2012-02-21 15:48:20 -0800107 size_t width = Dbg::GetTagWidth(argTypes[i]);
Ian Rogersc0542af2014-09-03 16:16:56 -0700108 argValues[i] = request->ReadValue(width);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800109 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#" PRIx64, width,
110 argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700111 }
112
Ian Rogersc0542af2014-09-03 16:16:56 -0700113 uint32_t options = request->ReadUnsigned32("InvokeOptions bit flags");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700114 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
115 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
116 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700117
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200118 JDWP::JdwpError error = Dbg::PrepareInvokeMethod(request->GetId(), thread_id, object_id,
119 class_id, method_id, arg_count,
120 argValues.get(), argTypes.get(), options);
121 if (error == JDWP::ERR_NONE) {
122 // We successfully requested the invoke. The event thread now owns the arguments array in its
123 // DebugInvokeReq mailbox.
124 argValues.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125 }
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200126 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700127}
128
Ian Rogersc0542af2014-09-03 16:16:56 -0700129static JdwpError VM_Version(JdwpState*, Request*, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700130 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes927bd292013-01-17 10:40:13 -0800131 // Text information on runtime version.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700132 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800133 expandBufAddUtf8String(pReply, version);
Elliott Hughes927bd292013-01-17 10:40:13 -0800134
135 // JDWP version numbers, major and minor.
136 expandBufAdd4BE(pReply, 1);
137 expandBufAdd4BE(pReply, 6);
138
139 // "java.version".
140 expandBufAddUtf8String(pReply, "1.6.0");
141
142 // "java.vm.name".
143 expandBufAddUtf8String(pReply, "Dalvik");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700144
145 return ERR_NONE;
146}
147
148/*
149 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
150 * referenceTypeID. We need to send back more than one if the class has
151 * been loaded by multiple class loaders.
152 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700153static JdwpError VM_ClassesBySignature(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700154 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700155 std::string classDescriptor(request->ReadUtf8String());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700156
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800157 std::vector<RefTypeId> ids;
Ian Rogersc0542af2014-09-03 16:16:56 -0700158 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), &ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700159
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800160 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700161
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800162 for (size_t i = 0; i < ids.size(); ++i) {
163 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800164 JDWP::JdwpTypeTag type_tag;
165 uint32_t class_status;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200166 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, nullptr);
Elliott Hughes436e3722012-02-17 20:01:47 -0800167 if (status != ERR_NONE) {
168 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800169 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700170
Elliott Hughes436e3722012-02-17 20:01:47 -0800171 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800172 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800173 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700174 }
175
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176 return ERR_NONE;
177}
178
179/*
180 * Handle request for the thread IDs of all running threads.
181 *
182 * We exclude ourselves from the list, because we don't allow ourselves
183 * to be suspended, and that violates some JDWP expectations.
184 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700185static JdwpError VM_AllThreads(JdwpState*, Request*, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700187 std::vector<ObjectId> thread_ids;
Sebastien Hertza06430c2014-09-15 19:21:30 +0200188 Dbg::GetThreads(nullptr /* all thread groups */, &thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189
Elliott Hughescaf76542012-06-28 16:08:22 -0700190 expandBufAdd4BE(pReply, thread_ids.size());
191 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
192 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700193 }
194
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700195 return ERR_NONE;
196}
197
198/*
199 * List all thread groups that do not have a parent.
200 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700201static JdwpError VM_TopLevelThreadGroups(JdwpState*, Request*, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700202 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700203 /*
204 * TODO: maintain a list of parentless thread groups in the VM.
205 *
206 * For now, just return "system". Application threads are created
207 * in "main", which is a child of "system".
208 */
209 uint32_t groups = 1;
210 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700211 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
212 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700213
214 return ERR_NONE;
215}
216
217/*
218 * Respond with the sizes of the basic debugger types.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700219 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700220static JdwpError VM_IDSizes(JdwpState*, Request*, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700222 expandBufAdd4BE(pReply, sizeof(FieldId));
223 expandBufAdd4BE(pReply, sizeof(MethodId));
224 expandBufAdd4BE(pReply, sizeof(ObjectId));
225 expandBufAdd4BE(pReply, sizeof(RefTypeId));
226 expandBufAdd4BE(pReply, sizeof(FrameId));
227 return ERR_NONE;
228}
229
Ian Rogersc0542af2014-09-03 16:16:56 -0700230static JdwpError VM_Dispose(JdwpState*, Request*, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100232 Dbg::Dispose();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700233 return ERR_NONE;
234}
235
236/*
237 * Suspend the execution of the application running in the VM (i.e. suspend
238 * all threads).
239 *
240 * This needs to increment the "suspend count" on all threads.
241 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700242static JdwpError VM_Suspend(JdwpState*, Request*, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700243 REQUIRES_SHARED(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800244 Thread* self = Thread::Current();
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700245 ScopedThreadSuspension sts(self, kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700246 Dbg::SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700247 return ERR_NONE;
248}
249
250/*
251 * Resume execution. Decrements the "suspend count" of all threads.
252 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700253static JdwpError VM_Resume(JdwpState*, Request*, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700255 Dbg::ResumeVM();
256 return ERR_NONE;
257}
258
Ian Rogersc0542af2014-09-03 16:16:56 -0700259static JdwpError VM_Exit(JdwpState* state, Request* request, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700260 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700261 uint32_t exit_status = request->ReadUnsigned32("exit_status");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800262 state->ExitAfterReplying(exit_status);
263 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700264}
265
266/*
267 * Create a new string in the VM and return its ID.
268 *
269 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
270 * string "java.util.Arrays".)
271 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700272static JdwpError VM_CreateString(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700273 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700274 std::string str(request->ReadUtf8String());
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +0200275 ObjectId string_id;
276 JdwpError status = Dbg::CreateString(str, &string_id);
277 if (status != ERR_NONE) {
278 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700279 }
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +0200280 expandBufAddObjectId(pReply, string_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700281 return ERR_NONE;
282}
283
Ian Rogersc0542af2014-09-03 16:16:56 -0700284static JdwpError VM_ClassPaths(JdwpState*, Request*, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700285 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800286 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700287
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800288 std::vector<std::string> class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700289 Split(Runtime::Current()->GetClassPathString(), ':', &class_path);
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800290 expandBufAdd4BE(pReply, class_path.size());
Sebastien Hertze6c143f2015-01-13 10:10:40 +0100291 for (const std::string& str : class_path) {
292 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700293 }
294
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800295 std::vector<std::string> boot_class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700296 Split(Runtime::Current()->GetBootClassPathString(), ':', &boot_class_path);
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800297 expandBufAdd4BE(pReply, boot_class_path.size());
Sebastien Hertze6c143f2015-01-13 10:10:40 +0100298 for (const std::string& str : boot_class_path) {
299 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700300 }
301
302 return ERR_NONE;
303}
304
Ian Rogersc0542af2014-09-03 16:16:56 -0700305static JdwpError VM_DisposeObjects(JdwpState*, Request* request, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700306 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700307 size_t object_count = request->ReadUnsigned32("object_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800308 for (size_t i = 0; i < object_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700309 ObjectId object_id = request->ReadObjectId();
310 uint32_t reference_count = request->ReadUnsigned32("reference_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800311 Dbg::DisposeObject(object_id, reference_count);
312 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700313 return ERR_NONE;
314}
315
Ian Rogersc0542af2014-09-03 16:16:56 -0700316static JdwpError VM_Capabilities(JdwpState*, Request*, ExpandBuf* reply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700317 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200318 expandBufAdd1(reply, true); // canWatchFieldModification
319 expandBufAdd1(reply, true); // canWatchFieldAccess
Elliott Hughes9777ba22013-01-17 09:04:19 -0800320 expandBufAdd1(reply, true); // canGetBytecodes
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800321 expandBufAdd1(reply, true); // canGetSyntheticAttribute
322 expandBufAdd1(reply, true); // canGetOwnedMonitorInfo
Elliott Hughesf9501702013-01-11 11:22:27 -0800323 expandBufAdd1(reply, true); // canGetCurrentContendedMonitor
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800324 expandBufAdd1(reply, true); // canGetMonitorInfo
325 return ERR_NONE;
326}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700327
Ian Rogersc0542af2014-09-03 16:16:56 -0700328static JdwpError VM_CapabilitiesNew(JdwpState*, Request* request, ExpandBuf* reply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700329 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800330 // The first few capabilities are the same as those reported by the older call.
Sebastien Hertz7d955652014-10-22 10:57:10 +0200331 VM_Capabilities(nullptr, request, reply);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800332
333 expandBufAdd1(reply, false); // canRedefineClasses
334 expandBufAdd1(reply, false); // canAddMethod
335 expandBufAdd1(reply, false); // canUnrestrictedlyRedefineClasses
336 expandBufAdd1(reply, false); // canPopFrames
Sebastien Hertz5f4e6f52014-04-11 12:07:41 +0200337 expandBufAdd1(reply, true); // canUseInstanceFilters
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800338 expandBufAdd1(reply, false); // canGetSourceDebugExtension
339 expandBufAdd1(reply, false); // canRequestVMDeathEvent
340 expandBufAdd1(reply, false); // canSetDefaultStratum
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800341 expandBufAdd1(reply, true); // 1.6: canGetInstanceInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800342 expandBufAdd1(reply, false); // 1.6: canRequestMonitorEvents
Elliott Hughes734b8c62013-01-11 15:32:45 -0800343 expandBufAdd1(reply, true); // 1.6: canGetMonitorFrameInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800344 expandBufAdd1(reply, false); // 1.6: canUseSourceNameFilters
345 expandBufAdd1(reply, false); // 1.6: canGetConstantPool
346 expandBufAdd1(reply, false); // 1.6: canForceEarlyReturn
347
348 // Fill in reserved22 through reserved32; note count started at 1.
349 for (size_t i = 22; i <= 32; ++i) {
350 expandBufAdd1(reply, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700351 }
352 return ERR_NONE;
353}
354
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700355static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700356 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800357 std::vector<JDWP::RefTypeId> classes;
Ian Rogersc0542af2014-09-03 16:16:56 -0700358 Dbg::GetClassList(&classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700359
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800360 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700361
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800362 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800363 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800364 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800365 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800366 uint32_t class_status;
367 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
368 if (status != ERR_NONE) {
369 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800370 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700371
Elliott Hughes436e3722012-02-17 20:01:47 -0800372 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800373 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800374 if (descriptor_and_status) {
375 expandBufAddUtf8String(pReply, descriptor);
376 if (generic) {
377 expandBufAddUtf8String(pReply, genericSignature);
378 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800379 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800380 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700381 }
382
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700383 return ERR_NONE;
384}
385
Ian Rogersc0542af2014-09-03 16:16:56 -0700386static JdwpError VM_AllClasses(JdwpState*, Request*, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700387 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700388 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800389}
390
Ian Rogersc0542af2014-09-03 16:16:56 -0700391static JdwpError VM_AllClassesWithGeneric(JdwpState*, Request*, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700392 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700393 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800394}
395
Ian Rogersc0542af2014-09-03 16:16:56 -0700396static JdwpError VM_InstanceCounts(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700397 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700398 int32_t class_count = request->ReadSigned32("class count");
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800399 if (class_count < 0) {
400 return ERR_ILLEGAL_ARGUMENT;
401 }
402 std::vector<RefTypeId> class_ids;
403 for (int32_t i = 0; i < class_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700404 class_ids.push_back(request->ReadRefTypeId());
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800405 }
406
407 std::vector<uint64_t> counts;
Ian Rogersc0542af2014-09-03 16:16:56 -0700408 JdwpError rc = Dbg::GetInstanceCounts(class_ids, &counts);
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800409 if (rc != ERR_NONE) {
410 return rc;
411 }
412
413 expandBufAdd4BE(pReply, counts.size());
414 for (size_t i = 0; i < counts.size(); ++i) {
415 expandBufAdd8BE(pReply, counts[i]);
416 }
417 return ERR_NONE;
418}
419
Ian Rogersc0542af2014-09-03 16:16:56 -0700420static JdwpError RT_Modifiers(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700421 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700422 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800423 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424}
425
426/*
427 * Get values from static fields in a reference type.
428 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700429static JdwpError RT_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700430 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700431 RefTypeId refTypeId = request->ReadRefTypeId();
432 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes0cf74332012-02-23 23:14:00 -0800433 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800434 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700435 FieldId fieldId = request->ReadFieldId();
Elliott Hughes0cf74332012-02-23 23:14:00 -0800436 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800437 if (status != ERR_NONE) {
438 return status;
439 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700440 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700441 return ERR_NONE;
442}
443
444/*
445 * Get the name of the source file in which a reference type was declared.
446 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700447static JdwpError RT_SourceFile(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700448 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700449 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes03181a82011-11-17 17:22:21 -0800450 std::string source_file;
Ian Rogersc0542af2014-09-03 16:16:56 -0700451 JdwpError status = Dbg::GetSourceFile(refTypeId, &source_file);
Elliott Hughes436e3722012-02-17 20:01:47 -0800452 if (status != ERR_NONE) {
453 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800455 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800456 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700457}
458
459/*
460 * Return the current status of the reference type.
461 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700462static JdwpError RT_Status(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700463 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700464 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800465 JDWP::JdwpTypeTag type_tag;
466 uint32_t class_status;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200467 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, nullptr);
Elliott Hughes436e3722012-02-17 20:01:47 -0800468 if (status != ERR_NONE) {
469 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800470 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800471 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700472 return ERR_NONE;
473}
474
475/*
476 * Return interfaces implemented directly by this class.
477 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700478static JdwpError RT_Interfaces(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700479 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700480 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800481 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700482}
483
484/*
485 * Return the class object corresponding to this type.
486 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700487static JdwpError RT_ClassObject(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700488 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700489 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -0800490 ObjectId class_object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700491 JdwpError status = Dbg::GetClassObject(refTypeId, &class_object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800492 if (status != ERR_NONE) {
493 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800494 }
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800495 VLOG(jdwp) << StringPrintf(" --> ObjectId %#" PRIx64, class_object_id);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800496 expandBufAddObjectId(pReply, class_object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700497 return ERR_NONE;
498}
499
500/*
501 * Returns the value of the SourceDebugExtension attribute.
502 *
503 * JDB seems interested, but DEX files don't currently support this.
504 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700505static JdwpError RT_SourceDebugExtension(JdwpState*, Request*, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700506 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700507 /* referenceTypeId in, string out */
508 return ERR_ABSENT_INFORMATION;
509}
510
Ian Rogersc0542af2014-09-03 16:16:56 -0700511static JdwpError RT_Signature(JdwpState*, Request* request, ExpandBuf* pReply, bool with_generic)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700512 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700513 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700514
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800515 std::string signature;
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700516 JdwpError status = Dbg::GetSignature(refTypeId, &signature);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800517 if (status != ERR_NONE) {
518 return status;
519 }
520 expandBufAddUtf8String(pReply, signature);
521 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800522 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700523 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700524 return ERR_NONE;
525}
526
Ian Rogersc0542af2014-09-03 16:16:56 -0700527static JdwpError RT_Signature(JdwpState* state, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700528 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800529 return RT_Signature(state, request, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800530}
531
Ian Rogersc0542af2014-09-03 16:16:56 -0700532static JdwpError RT_SignatureWithGeneric(JdwpState* state, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700533 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800534 return RT_Signature(state, request, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800535}
536
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700537/*
538 * Return the instance of java.lang.ClassLoader that loaded the specified
539 * reference type, or null if it was loaded by the system loader.
540 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700541static JdwpError RT_ClassLoader(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700542 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700543 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800544 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700545}
546
547/*
548 * Given a referenceTypeId, return a block of stuff that describes the
549 * fields declared by a class.
550 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700551static JdwpError RT_FieldsWithGeneric(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700552 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700553 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800554 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800555}
556
557// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogersc0542af2014-09-03 16:16:56 -0700558static JdwpError RT_Fields(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700559 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700560 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800561 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700562}
563
564/*
565 * Given a referenceTypeID, return a block of goodies describing the
566 * methods declared by a class.
567 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700568static JdwpError RT_MethodsWithGeneric(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700569 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700570 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800571 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800572}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700573
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800574// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogersc0542af2014-09-03 16:16:56 -0700575static JdwpError RT_Methods(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700576 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700577 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800578 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700579}
580
Ian Rogersc0542af2014-09-03 16:16:56 -0700581static JdwpError RT_Instances(JdwpState*, Request* request, ExpandBuf* reply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700582 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700583 RefTypeId class_id = request->ReadRefTypeId();
584 int32_t max_count = request->ReadSigned32("max count");
Elliott Hughes3b78c942013-01-15 17:35:41 -0800585 if (max_count < 0) {
586 return ERR_ILLEGAL_ARGUMENT;
587 }
588
589 std::vector<ObjectId> instances;
Ian Rogersc0542af2014-09-03 16:16:56 -0700590 JdwpError rc = Dbg::GetInstances(class_id, max_count, &instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800591 if (rc != ERR_NONE) {
592 return rc;
593 }
594
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800595 return WriteTaggedObjectList(reply, instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800596}
597
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700598/*
599 * Return the immediate superclass of a class.
600 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700601static JdwpError CT_Superclass(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700602 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700603 RefTypeId class_id = request->ReadRefTypeId();
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800604 RefTypeId superClassId;
Ian Rogersc0542af2014-09-03 16:16:56 -0700605 JdwpError status = Dbg::GetSuperclass(class_id, &superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800606 if (status != ERR_NONE) {
607 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800608 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700609 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700610 return ERR_NONE;
611}
612
613/*
614 * Set static class values.
615 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700616static JdwpError CT_SetValues(JdwpState* , Request* request, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700617 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700618 RefTypeId class_id = request->ReadRefTypeId();
619 int32_t values_count = request->ReadSigned32("values count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700620
Elliott Hughesa96836a2013-01-17 12:27:49 -0800621 UNUSED(class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622
Elliott Hughesa96836a2013-01-17 12:27:49 -0800623 for (int32_t i = 0; i < values_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700624 FieldId fieldId = request->ReadFieldId();
Elliott Hughesaed4be92011-12-02 16:16:23 -0800625 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800626 size_t width = Dbg::GetTagWidth(fieldTag);
Ian Rogersc0542af2014-09-03 16:16:56 -0700627 uint64_t value = request->ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700628
Elliott Hughesa96836a2013-01-17 12:27:49 -0800629 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " --> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800630 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
631 if (status != ERR_NONE) {
632 return status;
633 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700634 }
635
636 return ERR_NONE;
637}
638
639/*
640 * Invoke a static method.
641 *
642 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
643 * values in the "variables" display.
644 */
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200645static JdwpError CT_InvokeMethod(JdwpState* state, Request* request,
646 ExpandBuf* pReply ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700647 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700648 RefTypeId class_id = request->ReadRefTypeId();
649 ObjectId thread_id = request->ReadThreadId();
650 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700651
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200652 return RequestInvoke(state, request, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700653}
654
655/*
656 * Create a new object of the requested type, and invoke the specified
657 * constructor.
658 *
659 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
660 * see the contents of a byte[] as a string.
661 */
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200662static JdwpError CT_NewInstance(JdwpState* state, Request* request,
663 ExpandBuf* pReply ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700664 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700665 RefTypeId class_id = request->ReadRefTypeId();
666 ObjectId thread_id = request->ReadThreadId();
667 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700668
Elliott Hughes74847412012-06-20 18:10:21 -0700669 ObjectId object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700670 JdwpError status = Dbg::CreateObject(class_id, &object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800671 if (status != ERR_NONE) {
672 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800673 }
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200674 return RequestInvoke(state, request, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700675}
676
677/*
678 * Create a new array object of the requested type and length.
679 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700680static JdwpError AT_newInstance(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700681 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700682 RefTypeId arrayTypeId = request->ReadRefTypeId();
683 int32_t length = request->ReadSigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700684
Elliott Hughes74847412012-06-20 18:10:21 -0700685 ObjectId object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700686 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, &object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800687 if (status != ERR_NONE) {
688 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800689 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700690 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700691 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700692 return ERR_NONE;
693}
694
695/*
Alex Light70fa1a52016-02-24 16:35:59 -0800696 * Invoke a static method on an interface.
697 */
698static JdwpError IT_InvokeMethod(JdwpState* state, Request* request,
699 ExpandBuf* pReply ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700700 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light70fa1a52016-02-24 16:35:59 -0800701 RefTypeId class_id = request->ReadRefTypeId();
702 ObjectId thread_id = request->ReadThreadId();
703 MethodId method_id = request->ReadMethodId();
704
705 return RequestInvoke(state, request, thread_id, 0, class_id, method_id, false);
706}
707
708/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700709 * Return line number information for the method, if present.
710 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700711static JdwpError M_LineTable(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700712 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700713 RefTypeId refTypeId = request->ReadRefTypeId();
714 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700715
Elliott Hughes74847412012-06-20 18:10:21 -0700716 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700717
718 return ERR_NONE;
719}
720
Ian Rogersc0542af2014-09-03 16:16:56 -0700721static JdwpError M_VariableTable(JdwpState*, Request* request, ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 bool generic)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700723 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700724 RefTypeId class_id = request->ReadRefTypeId();
725 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700726
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800727 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
728 // information. That will cause Eclipse to make a best-effort attempt at displaying local
729 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
730 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700731 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700732 return ERR_NONE;
733}
734
Ian Rogersc0542af2014-09-03 16:16:56 -0700735static JdwpError M_VariableTable(JdwpState* state, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700736 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800737 return M_VariableTable(state, request, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800738}
739
Ian Rogersc0542af2014-09-03 16:16:56 -0700740static JdwpError M_VariableTableWithGeneric(JdwpState* state, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700741 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800742 return M_VariableTable(state, request, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800743}
744
Ian Rogersc0542af2014-09-03 16:16:56 -0700745static JdwpError M_Bytecodes(JdwpState*, Request* request, ExpandBuf* reply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700746 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700747 RefTypeId class_id = request->ReadRefTypeId();
748 MethodId method_id = request->ReadMethodId();
Elliott Hughes9777ba22013-01-17 09:04:19 -0800749
750 std::vector<uint8_t> bytecodes;
Ian Rogersc0542af2014-09-03 16:16:56 -0700751 JdwpError rc = Dbg::GetBytecodes(class_id, method_id, &bytecodes);
Elliott Hughes9777ba22013-01-17 09:04:19 -0800752 if (rc != ERR_NONE) {
753 return rc;
754 }
755
756 expandBufAdd4BE(reply, bytecodes.size());
757 for (size_t i = 0; i < bytecodes.size(); ++i) {
758 expandBufAdd1(reply, bytecodes[i]);
759 }
760
761 return ERR_NONE;
762}
763
Sebastien Hertz0a97fc62015-11-09 12:18:06 +0100764// Default implementation for IDEs relying on this command.
765static JdwpError M_IsObsolete(JdwpState*, Request* request, ExpandBuf* reply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700766 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0a97fc62015-11-09 12:18:06 +0100767 request->ReadRefTypeId(); // unused reference type ID
768 request->ReadMethodId(); // unused method ID
769 expandBufAdd1(reply, false); // a method is never obsolete.
770 return ERR_NONE;
771}
772
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700773/*
774 * Given an object reference, return the runtime type of the object
775 * (class or array).
776 *
Elliott Hughes74847412012-06-20 18:10:21 -0700777 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700778 * passed in here.
779 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700780static JdwpError OR_ReferenceType(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700781 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700782 ObjectId object_id = request->ReadObjectId();
Elliott Hughes74847412012-06-20 18:10:21 -0700783 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700784}
785
786/*
787 * Get values from the fields of an object.
788 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700789static JdwpError OR_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700790 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700791 ObjectId object_id = request->ReadObjectId();
792 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700793
Elliott Hughes0cf74332012-02-23 23:14:00 -0800794 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800795 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700796 FieldId fieldId = request->ReadFieldId();
Elliott Hughes74847412012-06-20 18:10:21 -0700797 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800798 if (status != ERR_NONE) {
799 return status;
800 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801 }
802
803 return ERR_NONE;
804}
805
806/*
807 * Set values in the fields of an object.
808 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700809static JdwpError OR_SetValues(JdwpState*, Request* request, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700810 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700811 ObjectId object_id = request->ReadObjectId();
812 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700813
Elliott Hughesa96836a2013-01-17 12:27:49 -0800814 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700815 FieldId fieldId = request->ReadFieldId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700816
Elliott Hughesaed4be92011-12-02 16:16:23 -0800817 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800818 size_t width = Dbg::GetTagWidth(fieldTag);
Ian Rogersc0542af2014-09-03 16:16:56 -0700819 uint64_t value = request->ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700820
Elliott Hughes2435a572012-02-17 16:07:41 -0800821 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700822 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800823 if (status != ERR_NONE) {
824 return status;
825 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700826 }
827
828 return ERR_NONE;
829}
830
Ian Rogersc0542af2014-09-03 16:16:56 -0700831static JdwpError OR_MonitorInfo(JdwpState*, Request* request, ExpandBuf* reply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700832 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700833 ObjectId object_id = request->ReadObjectId();
Elliott Hughesf327e072013-01-09 16:01:26 -0800834 return Dbg::GetMonitorInfo(object_id, reply);
835}
836
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700837/*
838 * Invoke an instance method. The invocation must occur in the specified
839 * thread, which must have been suspended by an event.
840 *
841 * The call is synchronous. All threads in the VM are resumed, unless the
842 * SINGLE_THREADED flag is set.
843 *
844 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
845 * object), it will try to invoke the object's toString() function. This
846 * feature becomes crucial when examining ArrayLists with Eclipse.
847 */
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200848static JdwpError OR_InvokeMethod(JdwpState* state, Request* request,
849 ExpandBuf* pReply ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700850 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700851 ObjectId object_id = request->ReadObjectId();
852 ObjectId thread_id = request->ReadThreadId();
853 RefTypeId class_id = request->ReadRefTypeId();
854 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700855
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200856 return RequestInvoke(state, request, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700857}
858
Ian Rogersc0542af2014-09-03 16:16:56 -0700859static JdwpError OR_DisableCollection(JdwpState*, Request* request, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700860 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700861 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800862 return Dbg::DisableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700863}
864
Ian Rogersc0542af2014-09-03 16:16:56 -0700865static JdwpError OR_EnableCollection(JdwpState*, Request* request, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700866 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700867 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800868 return Dbg::EnableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700869}
870
Ian Rogersc0542af2014-09-03 16:16:56 -0700871static JdwpError OR_IsCollected(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700872 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700873 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800874 bool is_collected;
Ian Rogersc0542af2014-09-03 16:16:56 -0700875 JdwpError rc = Dbg::IsCollected(object_id, &is_collected);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800876 expandBufAdd1(pReply, is_collected ? 1 : 0);
877 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700878}
879
Ian Rogersc0542af2014-09-03 16:16:56 -0700880static JdwpError OR_ReferringObjects(JdwpState*, Request* request, ExpandBuf* reply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700881 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700882 ObjectId object_id = request->ReadObjectId();
883 int32_t max_count = request->ReadSigned32("max count");
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800884 if (max_count < 0) {
885 return ERR_ILLEGAL_ARGUMENT;
886 }
887
888 std::vector<ObjectId> referring_objects;
Ian Rogersc0542af2014-09-03 16:16:56 -0700889 JdwpError rc = Dbg::GetReferringObjects(object_id, max_count, &referring_objects);
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800890 if (rc != ERR_NONE) {
891 return rc;
892 }
893
894 return WriteTaggedObjectList(reply, referring_objects);
895}
896
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700897/*
898 * Return the string value in a string object.
899 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700900static JdwpError SR_Value(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700901 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700902 ObjectId stringObject = request->ReadObjectId();
Sebastien Hertzb0b0b492014-09-15 11:27:27 +0200903 std::string str;
904 JDWP::JdwpError error = Dbg::StringToUtf8(stringObject, &str);
905 if (error != JDWP::ERR_NONE) {
906 return error;
907 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700908
Ian Rogers68b56852014-08-29 20:19:11 -0700909 VLOG(jdwp) << StringPrintf(" --> %s", PrintableString(str.c_str()).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700910
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800911 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700912
913 return ERR_NONE;
914}
915
916/*
917 * Return a thread's name.
918 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700919static JdwpError TR_Name(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700920 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700921 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700922
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800923 std::string name;
Ian Rogersc0542af2014-09-03 16:16:56 -0700924 JdwpError error = Dbg::GetThreadName(thread_id, &name);
Elliott Hughes221229c2013-01-08 18:17:50 -0800925 if (error != ERR_NONE) {
926 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700927 }
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800928 VLOG(jdwp) << StringPrintf(" Name of thread %#" PRIx64 " is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800929 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700930
931 return ERR_NONE;
932}
933
934/*
935 * Suspend the specified thread.
936 *
937 * It's supposed to remain suspended even if interpreted code wants to
938 * resume it; only the JDI is allowed to resume it.
939 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700940static JdwpError TR_Suspend(JdwpState*, Request* request, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700941 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700942 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700943
Elliott Hughes74847412012-06-20 18:10:21 -0700944 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700945 LOG(INFO) << " Warning: ignoring request to suspend self";
946 return ERR_THREAD_NOT_SUSPENDED;
947 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800948
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700949 Thread* self = Thread::Current();
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700950 ScopedThreadSuspension sts(self, kWaitingForDebuggerSend);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700951 JdwpError result = Dbg::SuspendThread(thread_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700952 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700953}
954
955/*
956 * Resume the specified thread.
957 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700958static JdwpError TR_Resume(JdwpState*, Request* request, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700959 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700960 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700961
Elliott Hughes74847412012-06-20 18:10:21 -0700962 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700963 LOG(INFO) << " Warning: ignoring request to resume self";
964 return ERR_NONE;
965 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800966
Elliott Hughes74847412012-06-20 18:10:21 -0700967 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700968 return ERR_NONE;
969}
970
971/*
972 * Return status of specified thread.
973 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700974static JdwpError TR_Status(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700975 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700976 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700977
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800978 JDWP::JdwpThreadStatus threadStatus;
979 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -0800980 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
981 if (error != ERR_NONE) {
982 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700983 }
984
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800985 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700986
987 expandBufAdd4BE(pReply, threadStatus);
988 expandBufAdd4BE(pReply, suspendStatus);
989
990 return ERR_NONE;
991}
992
993/*
994 * Return the thread group that the specified thread is a member of.
995 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700996static JdwpError TR_ThreadGroup(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700997 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700998 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -0700999 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001000}
1001
1002/*
1003 * Return the current call stack of a suspended thread.
1004 *
1005 * If the thread isn't suspended, the error code isn't defined, but should
1006 * be THREAD_NOT_SUSPENDED.
1007 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001008static JdwpError TR_Frames(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001009 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001010 ObjectId thread_id = request->ReadThreadId();
1011 uint32_t start_frame = request->ReadUnsigned32("start frame");
1012 uint32_t length = request->ReadUnsigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001013
Elliott Hughes221229c2013-01-08 18:17:50 -08001014 size_t actual_frame_count;
Ian Rogersc0542af2014-09-03 16:16:56 -07001015 JdwpError error = Dbg::GetThreadFrameCount(thread_id, &actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001016 if (error != ERR_NONE) {
1017 return error;
1018 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001019
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001020 if (actual_frame_count <= 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001021 return ERR_THREAD_NOT_SUSPENDED; // 0 means no managed frames (which means "in native").
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001022 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001023
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001024 if (start_frame > actual_frame_count) {
1025 return ERR_INVALID_INDEX;
1026 }
1027 if (length == static_cast<uint32_t>(-1)) {
1028 length = actual_frame_count - start_frame;
1029 }
1030 if (start_frame + length > actual_frame_count) {
1031 return ERR_INVALID_LENGTH;
1032 }
1033
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001034 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001035}
1036
1037/*
1038 * Returns the #of frames on the specified thread, which must be suspended.
1039 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001040static JdwpError TR_FrameCount(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001041 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001042 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001043
Elliott Hughes221229c2013-01-08 18:17:50 -08001044 size_t frame_count;
Ian Rogersc0542af2014-09-03 16:16:56 -07001045 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, &frame_count);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001046 if (rc != ERR_NONE) {
1047 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001048 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001049 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001050
1051 return ERR_NONE;
1052}
1053
Ian Rogersc0542af2014-09-03 16:16:56 -07001054static JdwpError TR_OwnedMonitors(Request* request, ExpandBuf* reply, bool with_stack_depths)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001055 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001056 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001057
1058 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001059 std::vector<uint32_t> stack_depths;
Ian Rogersc0542af2014-09-03 16:16:56 -07001060 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, &monitors, &stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001061 if (rc != ERR_NONE) {
1062 return rc;
1063 }
1064
1065 expandBufAdd4BE(reply, monitors.size());
1066 for (size_t i = 0; i < monitors.size(); ++i) {
1067 rc = WriteTaggedObject(reply, monitors[i]);
1068 if (rc != ERR_NONE) {
1069 return rc;
1070 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001071 if (with_stack_depths) {
1072 expandBufAdd4BE(reply, stack_depths[i]);
1073 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001074 }
1075 return ERR_NONE;
1076}
1077
Ian Rogersc0542af2014-09-03 16:16:56 -07001078static JdwpError TR_OwnedMonitors(JdwpState*, Request* request, ExpandBuf* reply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001079 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001080 return TR_OwnedMonitors(request, reply, false);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001081}
1082
Ian Rogersc0542af2014-09-03 16:16:56 -07001083static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, Request* request, ExpandBuf* reply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001084 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001085 return TR_OwnedMonitors(request, reply, true);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001086}
1087
Ian Rogersc0542af2014-09-03 16:16:56 -07001088static JdwpError TR_CurrentContendedMonitor(JdwpState*, Request* request, ExpandBuf* reply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001089 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001090 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001091
Elliott Hughesf9501702013-01-11 11:22:27 -08001092 ObjectId contended_monitor;
Ian Rogersc0542af2014-09-03 16:16:56 -07001093 JdwpError rc = Dbg::GetContendedMonitor(thread_id, &contended_monitor);
Elliott Hughesf9501702013-01-11 11:22:27 -08001094 if (rc != ERR_NONE) {
1095 return rc;
1096 }
1097 return WriteTaggedObject(reply, contended_monitor);
1098}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001099
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001100static JdwpError TR_Interrupt(JdwpState*, Request* request, ExpandBuf* reply ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001101 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001102 ObjectId thread_id = request->ReadThreadId();
Elliott Hughesf9501702013-01-11 11:22:27 -08001103 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001104}
1105
1106/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001107 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001108 *
1109 * (The thread *might* still be running -- it might not have examined
1110 * its suspend count recently.)
1111 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001112static JdwpError TR_DebugSuspendCount(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001113 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001114 ObjectId thread_id = request->ReadThreadId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001116}
1117
1118/*
1119 * Return the name of a thread group.
1120 *
1121 * The Eclipse debugger recognizes "main" and "system" as special.
1122 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001123static JdwpError TGR_Name(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001124 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001125 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001126 return Dbg::GetThreadGroupName(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001127}
1128
1129/*
1130 * Returns the thread group -- if any -- that contains the specified
1131 * thread group.
1132 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001133static JdwpError TGR_Parent(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001134 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001135 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001136 return Dbg::GetThreadGroupParent(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001137}
1138
1139/*
1140 * Return the active threads and thread groups that are part of the
1141 * specified thread group.
1142 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001143static JdwpError TGR_Children(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001144 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001145 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001146 return Dbg::GetThreadGroupChildren(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001147}
1148
1149/*
1150 * Return the #of components in the array.
1151 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001152static JdwpError AR_Length(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001153 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001154 ObjectId array_id = request->ReadArrayId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001155
Ian Rogersc0542af2014-09-03 16:16:56 -07001156 int32_t length;
1157 JdwpError status = Dbg::GetArrayLength(array_id, &length);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001158 if (status != ERR_NONE) {
1159 return status;
1160 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001161 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001162
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001163 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001164
1165 return ERR_NONE;
1166}
1167
1168/*
1169 * Return the values from an array.
1170 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001171static JdwpError AR_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001172 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001173 ObjectId array_id = request->ReadArrayId();
1174 uint32_t offset = request->ReadUnsigned32("offset");
1175 uint32_t length = request->ReadUnsigned32("length");
Elliott Hughesa96836a2013-01-17 12:27:49 -08001176 return Dbg::OutputArray(array_id, offset, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001177}
1178
1179/*
1180 * Set values in an array.
1181 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001182static JdwpError AR_SetValues(JdwpState*, Request* request, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001183 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001184 ObjectId array_id = request->ReadArrayId();
1185 uint32_t offset = request->ReadUnsigned32("offset");
1186 uint32_t count = request->ReadUnsigned32("count");
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001187 return Dbg::SetArrayElements(array_id, offset, count, request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001188}
1189
Ian Rogersc0542af2014-09-03 16:16:56 -07001190static JdwpError CLR_VisibleClasses(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001191 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001192 request->ReadObjectId(); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001193 // TODO: we should only return classes which have the given class loader as a defining or
1194 // initiating loader. The former would be easy; the latter is hard, because we don't have
1195 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001196 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001197}
1198
Sebastien Hertz358bcaf2015-10-21 16:18:58 +02001199// Delete function class to use std::unique_ptr with JdwpEvent.
1200struct JdwpEventDeleter {
1201 void operator()(JdwpEvent* event) {
1202 EventFree(event);
1203 }
1204};
1205
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001206/*
1207 * Set an event trigger.
1208 *
1209 * Reply with a requestID.
1210 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001211static JdwpError ER_Set(JdwpState* state, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001212 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001213 JdwpEventKind event_kind = request->ReadEnum1<JdwpEventKind>("event kind");
1214 JdwpSuspendPolicy suspend_policy = request->ReadEnum1<JdwpSuspendPolicy>("suspend policy");
1215 int32_t modifier_count = request->ReadSigned32("modifier count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001216
Elliott Hughesa96836a2013-01-17 12:27:49 -08001217 CHECK_LT(modifier_count, 256); /* reasonableness check */
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001218
Sebastien Hertz358bcaf2015-10-21 16:18:58 +02001219 std::unique_ptr<JDWP::JdwpEvent, JdwpEventDeleter> pEvent(EventAlloc(modifier_count));
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001220 pEvent->eventKind = event_kind;
1221 pEvent->suspend_policy = suspend_policy;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001222 pEvent->modCount = modifier_count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001223
1224 /*
1225 * Read modifiers. Ordering may be significant (see explanation of Count
1226 * mods in JDWP doc).
1227 */
Elliott Hughesa96836a2013-01-17 12:27:49 -08001228 for (int32_t i = 0; i < modifier_count; ++i) {
Elliott Hughes972a47b2012-02-21 18:16:06 -08001229 JdwpEventMod& mod = pEvent->mods[i];
Ian Rogersc0542af2014-09-03 16:16:56 -07001230 mod.modKind = request->ReadModKind();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001231 switch (mod.modKind) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001232 case MK_COUNT:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001233 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001234 // Report once, when "--count" reaches 0.
Ian Rogersc0542af2014-09-03 16:16:56 -07001235 uint32_t count = request->ReadUnsigned32("count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001236 if (count == 0) {
1237 return ERR_INVALID_COUNT;
1238 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001239 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001240 }
1241 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001242 case MK_CONDITIONAL:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001243 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001244 // Conditional on expression.
Ian Rogersc0542af2014-09-03 16:16:56 -07001245 uint32_t exprId = request->ReadUnsigned32("expr id");
Elliott Hughes972a47b2012-02-21 18:16:06 -08001246 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001247 }
1248 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001249 case MK_THREAD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001250 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001251 // Only report events in specified thread.
Ian Rogersc0542af2014-09-03 16:16:56 -07001252 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001253 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001254 }
1255 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001256 case MK_CLASS_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001257 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001258 // For ClassPrepare, MethodEntry.
Ian Rogersc0542af2014-09-03 16:16:56 -07001259 RefTypeId class_id = request->ReadRefTypeId();
Elliott Hughes74847412012-06-20 18:10:21 -07001260 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001261 }
1262 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001263 case MK_CLASS_MATCH:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001264 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001265 // Restrict events to matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001266 // pattern is "java.foo.*", we want "java/foo/*".
Ian Rogersc0542af2014-09-03 16:16:56 -07001267 std::string pattern(request->ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001268 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001269 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001270 }
1271 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001272 case MK_CLASS_EXCLUDE:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001273 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001274 // Restrict events to non-matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001275 // pattern is "java.foo.*", we want "java/foo/*".
Ian Rogersc0542af2014-09-03 16:16:56 -07001276 std::string pattern(request->ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001277 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001278 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001279 }
1280 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001281 case MK_LOCATION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001282 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001283 // Restrict certain events based on location.
Ian Rogersc0542af2014-09-03 16:16:56 -07001284 JdwpLocation location = request->ReadLocation();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001285 mod.locationOnly.loc = location;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001286 }
1287 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001288 case MK_EXCEPTION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001289 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001290 // Modifies EK_EXCEPTION events,
Ian Rogersc0542af2014-09-03 16:16:56 -07001291 mod.exceptionOnly.refTypeId = request->ReadRefTypeId(); // null => all exceptions.
1292 mod.exceptionOnly.caught = request->ReadEnum1<uint8_t>("caught");
1293 mod.exceptionOnly.uncaught = request->ReadEnum1<uint8_t>("uncaught");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001294 }
1295 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001296 case MK_FIELD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001297 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001298 // For field access/modification events.
Ian Rogersc0542af2014-09-03 16:16:56 -07001299 RefTypeId declaring = request->ReadRefTypeId();
1300 FieldId fieldId = request->ReadFieldId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001301 mod.fieldOnly.refTypeId = declaring;
1302 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001303 }
1304 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001305 case MK_STEP:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001306 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001307 // For use with EK_SINGLE_STEP.
Ian Rogersc0542af2014-09-03 16:16:56 -07001308 ObjectId thread_id = request->ReadThreadId();
1309 uint32_t size = request->ReadUnsigned32("step size");
1310 uint32_t depth = request->ReadUnsigned32("step depth");
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001311 VLOG(jdwp) << StringPrintf(" Step: thread=%#" PRIx64, thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001312 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1313
Elliott Hughes74847412012-06-20 18:10:21 -07001314 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001315 mod.step.size = size;
1316 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001317 }
1318 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001319 case MK_INSTANCE_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001320 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001321 // Report events related to a specific object.
Ian Rogersc0542af2014-09-03 16:16:56 -07001322 ObjectId instance = request->ReadObjectId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001323 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001324 }
1325 break;
1326 default:
Sebastien Hertz6d7839e2014-12-05 10:52:15 +01001327 LOG(WARNING) << "Unsupported modifier " << mod.modKind << " for event " << pEvent->eventKind;
Sebastien Hertz6d7839e2014-12-05 10:52:15 +01001328 return JDWP::ERR_NOT_IMPLEMENTED;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001329 }
1330 }
1331
1332 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001333 * We reply with an integer "requestID".
1334 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001335 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001336 expandBufAdd4BE(pReply, requestId);
1337
1338 pEvent->requestId = requestId;
1339
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001340 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001341
1342 /* add it to the list */
Sebastien Hertz358bcaf2015-10-21 16:18:58 +02001343 JdwpError err = state->RegisterEvent(pEvent.get());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001344 if (err != ERR_NONE) {
1345 /* registration failed, probably because event is bogus */
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001346 LOG(WARNING) << "WARNING: event request rejected";
Sebastien Hertz358bcaf2015-10-21 16:18:58 +02001347 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001348 }
Sebastien Hertz358bcaf2015-10-21 16:18:58 +02001349 pEvent.release();
1350 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001351}
1352
Ian Rogersc0542af2014-09-03 16:16:56 -07001353static JdwpError ER_Clear(JdwpState* state, Request* request, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001354 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001355 request->ReadEnum1<JdwpEventKind>("event kind");
1356 uint32_t requestId = request->ReadUnsigned32("request id");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001357
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001358 // Failure to find an event with a matching ID is a no-op
1359 // and does not return an error.
Elliott Hughes761928d2011-11-16 18:33:03 -08001360 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001361 return ERR_NONE;
1362}
1363
1364/*
1365 * Return the values of arguments and local variables.
1366 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001367static JdwpError SF_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001368 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz8009f392014-09-01 17:07:11 +02001369 return Dbg::GetLocalValues(request, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001370}
1371
1372/*
1373 * Set the values of arguments and local variables.
1374 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001375static JdwpError SF_SetValues(JdwpState*, Request* request, ExpandBuf*)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001376 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz8009f392014-09-01 17:07:11 +02001377 return Dbg::SetLocalValues(request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001378}
1379
Ian Rogersc0542af2014-09-03 16:16:56 -07001380static JdwpError SF_ThisObject(JdwpState*, Request* request, ExpandBuf* reply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001381 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001382 ObjectId thread_id = request->ReadThreadId();
1383 FrameId frame_id = request->ReadFrameId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001384
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001385 ObjectId object_id;
1386 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001387 if (rc != ERR_NONE) {
1388 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001389 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001390
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001391 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001392}
1393
1394/*
1395 * Return the reference type reflected by this class object.
1396 *
1397 * This appears to be required because ReferenceTypeId values are NEVER
1398 * reused, whereas ClassIds can be recycled like any other object. (Either
1399 * that, or I have no idea what this is for.)
1400 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001401static JdwpError COR_ReflectedType(JdwpState*, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001402 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001403 RefTypeId class_object_id = request->ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001404 return Dbg::GetReflectedType(class_object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001405}
1406
1407/*
1408 * Handle a DDM packet with a single chunk in it.
1409 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001410static JdwpError DDM_Chunk(JdwpState* state, Request* request, ExpandBuf* pReply)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001411 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001412 state->NotifyDdmsActive();
Sebastien Hertz7d955652014-10-22 10:57:10 +02001413 uint8_t* replyBuf = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001414 int replyLen = -1;
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001415 if (Dbg::DdmHandlePacket(request, &replyBuf, &replyLen)) {
1416 // If they want to send something back, we copy it into the buffer.
1417 // TODO: consider altering the JDWP stuff to hold the packet header
1418 // in a separate buffer. That would allow us to writev() DDM traffic
1419 // instead of copying it into the expanding buffer. The reduction in
1420 // heap requirements is probably more valuable than the efficiency.
1421 CHECK_GT(replyLen, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001422 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
Sebastien Hertz3901bbc2015-08-06 11:37:02 +02001423 delete[] replyBuf;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001424 }
1425 return ERR_NONE;
1426}
1427
1428/*
1429 * Handler map decl.
1430 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001431typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, Request* request, ExpandBuf* reply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001432
1433struct JdwpHandlerMap {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001434 uint8_t cmdSet;
1435 uint8_t cmd;
1436 JdwpRequestHandler func;
1437 const char* name;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001438};
1439
1440/*
1441 * Map commands to functions.
1442 *
1443 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1444 * and 128-256 are vendor-defined.
1445 */
Elliott Hughescb693062013-02-21 09:48:08 -08001446static const JdwpHandlerMap gHandlers[] = {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001447 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001448 { 1, 1, VM_Version, "VirtualMachine.Version" },
1449 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1450 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1451 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1452 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1453 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1454 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1455 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1456 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1457 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1458 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1459 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1460 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1461 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001462 { 1, 15, nullptr, "VirtualMachine.HoldEvents" },
1463 { 1, 16, nullptr, "VirtualMachine.ReleaseEvents" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001464 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001465 { 1, 18, nullptr, "VirtualMachine.RedefineClasses" },
1466 { 1, 19, nullptr, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001467 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001468 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001469
1470 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001471 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1472 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1473 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1474 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1475 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1476 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1477 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001478 { 2, 8, nullptr, "ReferenceType.NestedTypes" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001479 { 2, 9, RT_Status, "ReferenceType.Status" },
1480 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1481 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001482 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1483 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001484 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1485 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughes3b78c942013-01-15 17:35:41 -08001486 { 2, 16, RT_Instances, "ReferenceType.Instances" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001487 { 2, 17, nullptr, "ReferenceType.ClassFileVersion" },
1488 { 2, 18, nullptr, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001489
1490 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001491 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1492 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1493 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1494 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001495
1496 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001497 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001498
1499 /* InterfaceType command set (5) */
Alex Light70fa1a52016-02-24 16:35:59 -08001500 { 5, 1, IT_InvokeMethod, "InterfaceType.InvokeMethod" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001501
1502 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001503 { 6, 1, M_LineTable, "Method.LineTable" },
1504 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughes9777ba22013-01-17 09:04:19 -08001505 { 6, 3, M_Bytecodes, "Method.Bytecodes" },
Sebastien Hertz0a97fc62015-11-09 12:18:06 +01001506 { 6, 4, M_IsObsolete, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001507 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001508
1509 /* Field command set (8) */
1510
1511 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001512 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1513 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1514 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001515 { 9, 4, nullptr, "ObjectReference.UNUSED" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001516 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1517 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001518 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001519 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1520 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001521 { 9, 10, OR_ReferringObjects, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001522
1523 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001524 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001525
1526 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001527 { 11, 1, TR_Name, "ThreadReference.Name" },
1528 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1529 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1530 { 11, 4, TR_Status, "ThreadReference.Status" },
1531 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1532 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1533 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1534 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1535 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001536 { 11, 10, nullptr, "ThreadReference.Stop" },
Elliott Hughes734b8c62013-01-11 15:32:45 -08001537 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1538 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1539 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001540 { 11, 14, nullptr, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001541
1542 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001543 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1544 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1545 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001546
1547 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001548 { 13, 1, AR_Length, "ArrayReference.Length" },
1549 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1550 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001551
1552 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001553 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001554
1555 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001556 { 15, 1, ER_Set, "EventRequest.Set" },
1557 { 15, 2, ER_Clear, "EventRequest.Clear" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001558 { 15, 3, nullptr, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001559
1560 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001561 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1562 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1563 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001564 { 16, 4, nullptr, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001565
1566 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001567 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001568
1569 /* Event command set (64) */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001570 { 64, 100, nullptr, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001571
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001572 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001573};
1574
Ian Rogersc0542af2014-09-03 16:16:56 -07001575static const char* GetCommandName(Request* request) {
Elliott Hughescb693062013-02-21 09:48:08 -08001576 for (size_t i = 0; i < arraysize(gHandlers); ++i) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001577 if (gHandlers[i].cmdSet == request->GetCommandSet() &&
1578 gHandlers[i].cmd == request->GetCommand()) {
Elliott Hughescb693062013-02-21 09:48:08 -08001579 return gHandlers[i].name;
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001580 }
1581 }
1582 return "?UNKNOWN?";
1583}
1584
Ian Rogersc0542af2014-09-03 16:16:56 -07001585static std::string DescribeCommand(Request* request) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001586 std::string result;
Elliott Hughes64f574f2013-02-20 14:57:12 -08001587 result += "REQUEST: ";
Elliott Hughescb693062013-02-21 09:48:08 -08001588 result += GetCommandName(request);
Ian Rogersc0542af2014-09-03 16:16:56 -07001589 result += StringPrintf(" (length=%zu id=0x%06x)", request->GetLength(), request->GetId());
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001590 return result;
1591}
1592
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001593// Returns true if the given command_set and command identify an "invoke" command.
1594static bool IsInvokeCommand(uint8_t command_set, uint8_t command) {
1595 if (command_set == kJDWPClassTypeCmdSet) {
1596 return command == kJDWPClassTypeInvokeMethodCmd || command == kJDWPClassTypeNewInstanceCmd;
1597 } else if (command_set == kJDWPObjectReferenceCmdSet) {
1598 return command == kJDWPObjectReferenceInvokeCmd;
Alex Light70fa1a52016-02-24 16:35:59 -08001599 } else if (command_set == kJDWPInterfaceTypeCmdSet) {
1600 return command == kJDWPInterfaceTypeInvokeMethodCmd;
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001601 } else {
1602 return false;
1603 }
1604}
1605
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001606/*
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001607 * Process a request from the debugger. The skip_reply flag is set to true to indicate to the
1608 * caller the reply must not be sent to the debugger. This is used for invoke commands where the
1609 * reply is sent by the event thread after completing the invoke.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001610 *
1611 * On entry, the JDWP thread is in VMWAIT.
1612 */
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001613size_t JdwpState::ProcessRequest(Request* request, ExpandBuf* pReply, bool* skip_reply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001614 JdwpError result = ERR_NONE;
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001615 *skip_reply = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001616
Ian Rogersc0542af2014-09-03 16:16:56 -07001617 if (request->GetCommandSet() != kJDWPDdmCmdSet) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001618 /*
1619 * Activity from a debugger, not merely ddms. Mark us as having an
1620 * active debugger session, and zero out the last-activity timestamp
1621 * so waitForDebugger() doesn't return if we stall for a bit here.
1622 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001623 Dbg::GoActive();
Ian Rogers37f3c962014-07-17 11:25:30 -07001624 last_activity_time_ms_.StoreSequentiallyConsistent(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001625 }
1626
1627 /*
1628 * If a debugger event has fired in another thread, wait until the
Sebastien Hertz2bf93f42015-01-09 18:44:05 +01001629 * initiating thread has suspended itself before processing commands
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001630 * from the debugger. Otherwise we (the JDWP thread) could be told to
1631 * resume the thread before it has suspended.
1632 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001633 * Note that we MUST clear the event token before waking the event
1634 * thread up, or risk waiting for the thread to suspend after we've
1635 * told it to resume.
1636 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +01001637 AcquireJdwpTokenForCommand();
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001638
1639 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001640 * Tell the VM that we're running and shouldn't be interrupted by GC.
1641 * Do this after anything that can stall indefinitely.
1642 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001643 Thread* self = Thread::Current();
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001644 ScopedObjectAccess soa(self);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001645
1646 expandBufAddSpace(pReply, kJDWPHeaderLen);
1647
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001648 size_t i;
Elliott Hughescb693062013-02-21 09:48:08 -08001649 for (i = 0; i < arraysize(gHandlers); ++i) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001650 if (gHandlers[i].cmdSet == request->GetCommandSet() &&
1651 gHandlers[i].cmd == request->GetCommand() &&
1652 gHandlers[i].func != nullptr) {
Elliott Hughescb693062013-02-21 09:48:08 -08001653 VLOG(jdwp) << DescribeCommand(request);
1654 result = (*gHandlers[i].func)(this, request, pReply);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001655 if (result == ERR_NONE) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001656 request->CheckConsumed();
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001657 }
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001658 self->AssertNoPendingException();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001659 break;
1660 }
1661 }
Elliott Hughescb693062013-02-21 09:48:08 -08001662 if (i == arraysize(gHandlers)) {
1663 LOG(ERROR) << "Command not implemented: " << DescribeCommand(request);
Ian Rogersc0542af2014-09-03 16:16:56 -07001664 LOG(ERROR) << HexDump(request->data(), request->size(), false, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001665 result = ERR_NOT_IMPLEMENTED;
1666 }
1667
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001668 size_t replyLength = 0U;
1669 if (result == ERR_NONE && IsInvokeCommand(request->GetCommandSet(), request->GetCommand())) {
1670 // We successfully request an invoke in the event thread. It will send the reply once the
1671 // invoke completes so we must not send it now.
1672 *skip_reply = true;
1673 } else {
1674 /*
1675 * Set up the reply header.
1676 *
1677 * If we encountered an error, only send the header back.
1678 */
1679 uint8_t* replyBuf = expandBufGetBuffer(pReply);
1680 replyLength = (result == ERR_NONE) ? expandBufGetLength(pReply) : kJDWPHeaderLen;
1681 Set4BE(replyBuf + kJDWPHeaderSizeOffset, replyLength);
1682 Set4BE(replyBuf + kJDWPHeaderIdOffset, request->GetId());
1683 Set1(replyBuf + kJDWPHeaderFlagsOffset, kJDWPFlagReply);
1684 Set2BE(replyBuf + kJDWPHeaderErrorCodeOffset, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001685
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001686 CHECK_GT(expandBufGetLength(pReply), 0U) << GetCommandName(request) << " " << request->GetId();
Elliott Hughescb693062013-02-21 09:48:08 -08001687
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001688 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
1689 VLOG(jdwp) << "REPLY: " << GetCommandName(request) << " " << result << " (length=" << respLen << ")";
1690 if (false) {
1691 VLOG(jdwp) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen, false, "");
1692 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001693 }
1694
Elliott Hughescb693062013-02-21 09:48:08 -08001695 VLOG(jdwp) << "----------";
1696
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001697 /*
1698 * Update last-activity timestamp. We really only need this during
1699 * the initial setup. Only update if this is a non-DDMS packet.
1700 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001701 if (request->GetCommandSet() != kJDWPDdmCmdSet) {
Ian Rogers37f3c962014-07-17 11:25:30 -07001702 last_activity_time_ms_.StoreSequentiallyConsistent(MilliTime());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001703 }
1704
Sebastien Hertz43c8d722014-03-18 12:19:52 +01001705 return replyLength;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001706}
1707
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001708} // namespace JDWP
1709
1710} // namespace art