blob: 7776f8fad31012ffe6162be015f5f31045a8e280 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes872d4ec2011-10-21 17:07:15 -070017#include <stdlib.h>
18#include <string.h>
19#include <unistd.h>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Elliott Hughesa96836a2013-01-17 12:27:49 -080021#include <string>
22
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "atomic.h"
Ian Rogers43b2e0f2014-01-30 16:58:39 -080024#include "base/hex_dump.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
26#include "base/macros.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080027#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "debugger.h"
29#include "jdwp/jdwp_constants.h"
30#include "jdwp/jdwp_event.h"
31#include "jdwp/jdwp_expand_buf.h"
32#include "jdwp/jdwp_priv.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "runtime.h"
Ian Rogers693ff612013-02-01 10:56:12 -080034#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010035#include "utils.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080036
Elliott Hughes872d4ec2011-10-21 17:07:15 -070037namespace art {
38
39namespace JDWP {
40
Elliott Hughes4b9702c2013-02-20 18:13:24 -080041std::string DescribeField(const FieldId& field_id) {
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070042 return StringPrintf("%#" PRIx64 " (%s)", field_id, Dbg::GetFieldName(field_id).c_str());
Elliott Hughesa96836a2013-01-17 12:27:49 -080043}
44
Elliott Hughes4b9702c2013-02-20 18:13:24 -080045std::string DescribeMethod(const MethodId& method_id) {
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070046 return StringPrintf("%#" PRIx64 " (%s)", method_id, Dbg::GetMethodName(method_id).c_str());
Elliott Hughesa96836a2013-01-17 12:27:49 -080047}
48
Elliott Hughes4b9702c2013-02-20 18:13:24 -080049std::string DescribeRefTypeId(const RefTypeId& ref_type_id) {
Elliott Hughesa96836a2013-01-17 12:27:49 -080050 std::string signature("unknown");
Ian Rogersfc0e94b2013-09-23 23:51:32 -070051 Dbg::GetSignature(ref_type_id, &signature);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -080052 return StringPrintf("%#" PRIx64 " (%s)", ref_type_id, signature.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -070053}
54
Elliott Hughes4993bbc2013-01-10 15:41:25 -080055static JdwpError WriteTaggedObject(ExpandBuf* reply, ObjectId object_id)
Mathieu Chartier90443472015-07-16 20:32:27 -070056 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -080057 uint8_t tag;
Ian Rogersc0542af2014-09-03 16:16:56 -070058 JdwpError rc = Dbg::GetObjectTag(object_id, &tag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -080059 if (rc == ERR_NONE) {
60 expandBufAdd1(reply, tag);
61 expandBufAddObjectId(reply, object_id);
62 }
63 return rc;
64}
65
Elliott Hughes0cbaff52013-01-16 15:28:01 -080066static JdwpError WriteTaggedObjectList(ExpandBuf* reply, const std::vector<ObjectId>& objects)
Mathieu Chartier90443472015-07-16 20:32:27 -070067 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes0cbaff52013-01-16 15:28:01 -080068 expandBufAdd4BE(reply, objects.size());
69 for (size_t i = 0; i < objects.size(); ++i) {
70 JdwpError rc = WriteTaggedObject(reply, objects[i]);
71 if (rc != ERR_NONE) {
72 return rc;
73 }
74 }
75 return ERR_NONE;
76}
77
Elliott Hughes872d4ec2011-10-21 17:07:15 -070078/*
79 * Common code for *_InvokeMethod requests.
80 *
Elliott Hughes74847412012-06-20 18:10:21 -070081 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -070082 * expected-to-be-void return value of the called function.
83 */
Sebastien Hertzcbc50642015-06-01 17:33:12 +020084static JdwpError RequestInvoke(JdwpState*, Request* request,
Sebastien Hertz1558b572015-02-25 15:05:59 +010085 ObjectId thread_id, ObjectId object_id,
86 RefTypeId class_id, MethodId method_id, bool is_constructor)
Mathieu Chartier90443472015-07-16 20:32:27 -070087 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -070088 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070089
Ian Rogersc0542af2014-09-03 16:16:56 -070090 int32_t arg_count = request->ReadSigned32("argument count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -070091
Ian Rogersd9e4e0c2014-01-23 20:11:40 -080092 VLOG(jdwp) << StringPrintf(" --> thread_id=%#" PRIx64 " object_id=%#" PRIx64,
93 thread_id, object_id);
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070094 VLOG(jdwp) << StringPrintf(" class_id=%#" PRIx64 " method_id=%#" PRIx64 " %s.%s",
95 class_id, method_id, Dbg::GetClassName(class_id).c_str(),
Elliott Hughesa96836a2013-01-17 12:27:49 -080096 Dbg::GetMethodName(method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -080097 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070098
Sebastien Hertz7d955652014-10-22 10:57:10 +020099 std::unique_ptr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : nullptr);
100 std::unique_ptr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : nullptr);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800101 for (int32_t i = 0; i < arg_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700102 argTypes[i] = request->ReadTag();
Elliott Hughes45651fd2012-02-21 15:48:20 -0800103 size_t width = Dbg::GetTagWidth(argTypes[i]);
Ian Rogersc0542af2014-09-03 16:16:56 -0700104 argValues[i] = request->ReadValue(width);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800105 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#" PRIx64, width,
106 argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700107 }
108
Ian Rogersc0542af2014-09-03 16:16:56 -0700109 uint32_t options = request->ReadUnsigned32("InvokeOptions bit flags");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700110 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
111 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
112 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700113
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200114 JDWP::JdwpError error = Dbg::PrepareInvokeMethod(request->GetId(), thread_id, object_id,
115 class_id, method_id, arg_count,
116 argValues.get(), argTypes.get(), options);
117 if (error == JDWP::ERR_NONE) {
118 // We successfully requested the invoke. The event thread now owns the arguments array in its
119 // DebugInvokeReq mailbox.
120 argValues.release();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121 }
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200122 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123}
124
Ian Rogersc0542af2014-09-03 16:16:56 -0700125static JdwpError VM_Version(JdwpState*, Request*, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700126 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes927bd292013-01-17 10:40:13 -0800127 // Text information on runtime version.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700128 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800129 expandBufAddUtf8String(pReply, version);
Elliott Hughes927bd292013-01-17 10:40:13 -0800130
131 // JDWP version numbers, major and minor.
132 expandBufAdd4BE(pReply, 1);
133 expandBufAdd4BE(pReply, 6);
134
135 // "java.version".
136 expandBufAddUtf8String(pReply, "1.6.0");
137
138 // "java.vm.name".
139 expandBufAddUtf8String(pReply, "Dalvik");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140
141 return ERR_NONE;
142}
143
144/*
145 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
146 * referenceTypeID. We need to send back more than one if the class has
147 * been loaded by multiple class loaders.
148 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700149static JdwpError VM_ClassesBySignature(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700150 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700151 std::string classDescriptor(request->ReadUtf8String());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800153 std::vector<RefTypeId> ids;
Ian Rogersc0542af2014-09-03 16:16:56 -0700154 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), &ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700155
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800156 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700157
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800158 for (size_t i = 0; i < ids.size(); ++i) {
159 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800160 JDWP::JdwpTypeTag type_tag;
161 uint32_t class_status;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200162 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, nullptr);
Elliott Hughes436e3722012-02-17 20:01:47 -0800163 if (status != ERR_NONE) {
164 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800165 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700166
Elliott Hughes436e3722012-02-17 20:01:47 -0800167 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800168 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800169 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700170 }
171
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700172 return ERR_NONE;
173}
174
175/*
176 * Handle request for the thread IDs of all running threads.
177 *
178 * We exclude ourselves from the list, because we don't allow ourselves
179 * to be suspended, and that violates some JDWP expectations.
180 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700181static JdwpError VM_AllThreads(JdwpState*, Request*, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700182 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700183 std::vector<ObjectId> thread_ids;
Sebastien Hertza06430c2014-09-15 19:21:30 +0200184 Dbg::GetThreads(nullptr /* all thread groups */, &thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700185
Elliott Hughescaf76542012-06-28 16:08:22 -0700186 expandBufAdd4BE(pReply, thread_ids.size());
187 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
188 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189 }
190
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700191 return ERR_NONE;
192}
193
194/*
195 * List all thread groups that do not have a parent.
196 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700197static JdwpError VM_TopLevelThreadGroups(JdwpState*, Request*, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700198 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199 /*
200 * TODO: maintain a list of parentless thread groups in the VM.
201 *
202 * For now, just return "system". Application threads are created
203 * in "main", which is a child of "system".
204 */
205 uint32_t groups = 1;
206 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700207 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
208 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700209
210 return ERR_NONE;
211}
212
213/*
214 * Respond with the sizes of the basic debugger types.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700215 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700216static JdwpError VM_IDSizes(JdwpState*, Request*, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700217 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700218 expandBufAdd4BE(pReply, sizeof(FieldId));
219 expandBufAdd4BE(pReply, sizeof(MethodId));
220 expandBufAdd4BE(pReply, sizeof(ObjectId));
221 expandBufAdd4BE(pReply, sizeof(RefTypeId));
222 expandBufAdd4BE(pReply, sizeof(FrameId));
223 return ERR_NONE;
224}
225
Ian Rogersc0542af2014-09-03 16:16:56 -0700226static JdwpError VM_Dispose(JdwpState*, Request*, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700227 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100228 Dbg::Dispose();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700229 return ERR_NONE;
230}
231
232/*
233 * Suspend the execution of the application running in the VM (i.e. suspend
234 * all threads).
235 *
236 * This needs to increment the "suspend count" on all threads.
237 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700238static JdwpError VM_Suspend(JdwpState*, Request*, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700239 SHARED_REQUIRES(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800240 Thread* self = Thread::Current();
241 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700242 Dbg::SuspendVM();
jeffhaoa77f0f62012-12-05 17:19:31 -0800243 self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700244 return ERR_NONE;
245}
246
247/*
248 * Resume execution. Decrements the "suspend count" of all threads.
249 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700250static JdwpError VM_Resume(JdwpState*, Request*, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700251 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700252 Dbg::ResumeVM();
253 return ERR_NONE;
254}
255
Ian Rogersc0542af2014-09-03 16:16:56 -0700256static JdwpError VM_Exit(JdwpState* state, Request* request, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700257 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700258 uint32_t exit_status = request->ReadUnsigned32("exit_status");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800259 state->ExitAfterReplying(exit_status);
260 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700261}
262
263/*
264 * Create a new string in the VM and return its ID.
265 *
266 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
267 * string "java.util.Arrays".)
268 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700269static JdwpError VM_CreateString(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700270 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700271 std::string str(request->ReadUtf8String());
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +0200272 ObjectId string_id;
273 JdwpError status = Dbg::CreateString(str, &string_id);
274 if (status != ERR_NONE) {
275 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700276 }
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +0200277 expandBufAddObjectId(pReply, string_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700278 return ERR_NONE;
279}
280
Ian Rogersc0542af2014-09-03 16:16:56 -0700281static JdwpError VM_ClassPaths(JdwpState*, Request*, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700282 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800283 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700284
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800285 std::vector<std::string> class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700286 Split(Runtime::Current()->GetClassPathString(), ':', &class_path);
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800287 expandBufAdd4BE(pReply, class_path.size());
Sebastien Hertze6c143f2015-01-13 10:10:40 +0100288 for (const std::string& str : class_path) {
289 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700290 }
291
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800292 std::vector<std::string> boot_class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700293 Split(Runtime::Current()->GetBootClassPathString(), ':', &boot_class_path);
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800294 expandBufAdd4BE(pReply, boot_class_path.size());
Sebastien Hertze6c143f2015-01-13 10:10:40 +0100295 for (const std::string& str : boot_class_path) {
296 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700297 }
298
299 return ERR_NONE;
300}
301
Ian Rogersc0542af2014-09-03 16:16:56 -0700302static JdwpError VM_DisposeObjects(JdwpState*, Request* request, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700303 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700304 size_t object_count = request->ReadUnsigned32("object_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800305 for (size_t i = 0; i < object_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700306 ObjectId object_id = request->ReadObjectId();
307 uint32_t reference_count = request->ReadUnsigned32("reference_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800308 Dbg::DisposeObject(object_id, reference_count);
309 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700310 return ERR_NONE;
311}
312
Ian Rogersc0542af2014-09-03 16:16:56 -0700313static JdwpError VM_Capabilities(JdwpState*, Request*, ExpandBuf* reply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700314 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200315 expandBufAdd1(reply, true); // canWatchFieldModification
316 expandBufAdd1(reply, true); // canWatchFieldAccess
Elliott Hughes9777ba22013-01-17 09:04:19 -0800317 expandBufAdd1(reply, true); // canGetBytecodes
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800318 expandBufAdd1(reply, true); // canGetSyntheticAttribute
319 expandBufAdd1(reply, true); // canGetOwnedMonitorInfo
Elliott Hughesf9501702013-01-11 11:22:27 -0800320 expandBufAdd1(reply, true); // canGetCurrentContendedMonitor
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800321 expandBufAdd1(reply, true); // canGetMonitorInfo
322 return ERR_NONE;
323}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700324
Ian Rogersc0542af2014-09-03 16:16:56 -0700325static JdwpError VM_CapabilitiesNew(JdwpState*, Request* request, ExpandBuf* reply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700326 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800327 // The first few capabilities are the same as those reported by the older call.
Sebastien Hertz7d955652014-10-22 10:57:10 +0200328 VM_Capabilities(nullptr, request, reply);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800329
330 expandBufAdd1(reply, false); // canRedefineClasses
331 expandBufAdd1(reply, false); // canAddMethod
332 expandBufAdd1(reply, false); // canUnrestrictedlyRedefineClasses
333 expandBufAdd1(reply, false); // canPopFrames
Sebastien Hertz5f4e6f52014-04-11 12:07:41 +0200334 expandBufAdd1(reply, true); // canUseInstanceFilters
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800335 expandBufAdd1(reply, false); // canGetSourceDebugExtension
336 expandBufAdd1(reply, false); // canRequestVMDeathEvent
337 expandBufAdd1(reply, false); // canSetDefaultStratum
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800338 expandBufAdd1(reply, true); // 1.6: canGetInstanceInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800339 expandBufAdd1(reply, false); // 1.6: canRequestMonitorEvents
Elliott Hughes734b8c62013-01-11 15:32:45 -0800340 expandBufAdd1(reply, true); // 1.6: canGetMonitorFrameInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800341 expandBufAdd1(reply, false); // 1.6: canUseSourceNameFilters
342 expandBufAdd1(reply, false); // 1.6: canGetConstantPool
343 expandBufAdd1(reply, false); // 1.6: canForceEarlyReturn
344
345 // Fill in reserved22 through reserved32; note count started at 1.
346 for (size_t i = 22; i <= 32; ++i) {
347 expandBufAdd1(reply, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700348 }
349 return ERR_NONE;
350}
351
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Mathieu Chartier90443472015-07-16 20:32:27 -0700353 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800354 std::vector<JDWP::RefTypeId> classes;
Ian Rogersc0542af2014-09-03 16:16:56 -0700355 Dbg::GetClassList(&classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700356
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800357 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700358
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800359 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800360 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800361 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800362 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800363 uint32_t class_status;
364 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
365 if (status != ERR_NONE) {
366 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800367 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700368
Elliott Hughes436e3722012-02-17 20:01:47 -0800369 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800370 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800371 if (descriptor_and_status) {
372 expandBufAddUtf8String(pReply, descriptor);
373 if (generic) {
374 expandBufAddUtf8String(pReply, genericSignature);
375 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800376 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800377 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700378 }
379
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700380 return ERR_NONE;
381}
382
Ian Rogersc0542af2014-09-03 16:16:56 -0700383static JdwpError VM_AllClasses(JdwpState*, Request*, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700384 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700385 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800386}
387
Ian Rogersc0542af2014-09-03 16:16:56 -0700388static JdwpError VM_AllClassesWithGeneric(JdwpState*, Request*, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700389 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700390 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800391}
392
Ian Rogersc0542af2014-09-03 16:16:56 -0700393static JdwpError VM_InstanceCounts(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700394 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700395 int32_t class_count = request->ReadSigned32("class count");
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800396 if (class_count < 0) {
397 return ERR_ILLEGAL_ARGUMENT;
398 }
399 std::vector<RefTypeId> class_ids;
400 for (int32_t i = 0; i < class_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700401 class_ids.push_back(request->ReadRefTypeId());
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800402 }
403
404 std::vector<uint64_t> counts;
Ian Rogersc0542af2014-09-03 16:16:56 -0700405 JdwpError rc = Dbg::GetInstanceCounts(class_ids, &counts);
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800406 if (rc != ERR_NONE) {
407 return rc;
408 }
409
410 expandBufAdd4BE(pReply, counts.size());
411 for (size_t i = 0; i < counts.size(); ++i) {
412 expandBufAdd8BE(pReply, counts[i]);
413 }
414 return ERR_NONE;
415}
416
Ian Rogersc0542af2014-09-03 16:16:56 -0700417static JdwpError RT_Modifiers(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700418 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700419 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800420 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700421}
422
423/*
424 * Get values from static fields in a reference type.
425 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700426static JdwpError RT_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700427 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700428 RefTypeId refTypeId = request->ReadRefTypeId();
429 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes0cf74332012-02-23 23:14:00 -0800430 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800431 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700432 FieldId fieldId = request->ReadFieldId();
Elliott Hughes0cf74332012-02-23 23:14:00 -0800433 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800434 if (status != ERR_NONE) {
435 return status;
436 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700437 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700438 return ERR_NONE;
439}
440
441/*
442 * Get the name of the source file in which a reference type was declared.
443 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700444static JdwpError RT_SourceFile(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700445 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700446 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes03181a82011-11-17 17:22:21 -0800447 std::string source_file;
Ian Rogersc0542af2014-09-03 16:16:56 -0700448 JdwpError status = Dbg::GetSourceFile(refTypeId, &source_file);
Elliott Hughes436e3722012-02-17 20:01:47 -0800449 if (status != ERR_NONE) {
450 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800452 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800453 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700454}
455
456/*
457 * Return the current status of the reference type.
458 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700459static JdwpError RT_Status(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700460 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700461 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800462 JDWP::JdwpTypeTag type_tag;
463 uint32_t class_status;
Sebastien Hertz7d955652014-10-22 10:57:10 +0200464 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, nullptr);
Elliott Hughes436e3722012-02-17 20:01:47 -0800465 if (status != ERR_NONE) {
466 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800467 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800468 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700469 return ERR_NONE;
470}
471
472/*
473 * Return interfaces implemented directly by this class.
474 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700475static JdwpError RT_Interfaces(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700476 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700477 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800478 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700479}
480
481/*
482 * Return the class object corresponding to this type.
483 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700484static JdwpError RT_ClassObject(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700485 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700486 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -0800487 ObjectId class_object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700488 JdwpError status = Dbg::GetClassObject(refTypeId, &class_object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800489 if (status != ERR_NONE) {
490 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800491 }
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800492 VLOG(jdwp) << StringPrintf(" --> ObjectId %#" PRIx64, class_object_id);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800493 expandBufAddObjectId(pReply, class_object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700494 return ERR_NONE;
495}
496
497/*
498 * Returns the value of the SourceDebugExtension attribute.
499 *
500 * JDB seems interested, but DEX files don't currently support this.
501 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700502static JdwpError RT_SourceDebugExtension(JdwpState*, Request*, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700503 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700504 /* referenceTypeId in, string out */
505 return ERR_ABSENT_INFORMATION;
506}
507
Ian Rogersc0542af2014-09-03 16:16:56 -0700508static JdwpError RT_Signature(JdwpState*, Request* request, ExpandBuf* pReply, bool with_generic)
Mathieu Chartier90443472015-07-16 20:32:27 -0700509 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700510 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700511
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800512 std::string signature;
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700513 JdwpError status = Dbg::GetSignature(refTypeId, &signature);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800514 if (status != ERR_NONE) {
515 return status;
516 }
517 expandBufAddUtf8String(pReply, signature);
518 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800519 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700520 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700521 return ERR_NONE;
522}
523
Ian Rogersc0542af2014-09-03 16:16:56 -0700524static JdwpError RT_Signature(JdwpState* state, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700525 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800526 return RT_Signature(state, request, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800527}
528
Ian Rogersc0542af2014-09-03 16:16:56 -0700529static JdwpError RT_SignatureWithGeneric(JdwpState* state, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700530 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800531 return RT_Signature(state, request, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800532}
533
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700534/*
535 * Return the instance of java.lang.ClassLoader that loaded the specified
536 * reference type, or null if it was loaded by the system loader.
537 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700538static JdwpError RT_ClassLoader(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700539 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700540 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800541 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700542}
543
544/*
545 * Given a referenceTypeId, return a block of stuff that describes the
546 * fields declared by a class.
547 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700548static JdwpError RT_FieldsWithGeneric(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700549 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700550 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800551 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800552}
553
554// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogersc0542af2014-09-03 16:16:56 -0700555static JdwpError RT_Fields(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700556 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700557 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800558 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700559}
560
561/*
562 * Given a referenceTypeID, return a block of goodies describing the
563 * methods declared by a class.
564 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700565static JdwpError RT_MethodsWithGeneric(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700566 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700567 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800568 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800569}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700570
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800571// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogersc0542af2014-09-03 16:16:56 -0700572static JdwpError RT_Methods(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700573 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700574 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800575 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700576}
577
Ian Rogersc0542af2014-09-03 16:16:56 -0700578static JdwpError RT_Instances(JdwpState*, Request* request, ExpandBuf* reply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700579 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700580 RefTypeId class_id = request->ReadRefTypeId();
581 int32_t max_count = request->ReadSigned32("max count");
Elliott Hughes3b78c942013-01-15 17:35:41 -0800582 if (max_count < 0) {
583 return ERR_ILLEGAL_ARGUMENT;
584 }
585
586 std::vector<ObjectId> instances;
Ian Rogersc0542af2014-09-03 16:16:56 -0700587 JdwpError rc = Dbg::GetInstances(class_id, max_count, &instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800588 if (rc != ERR_NONE) {
589 return rc;
590 }
591
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800592 return WriteTaggedObjectList(reply, instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800593}
594
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700595/*
596 * Return the immediate superclass of a class.
597 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700598static JdwpError CT_Superclass(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700599 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700600 RefTypeId class_id = request->ReadRefTypeId();
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800601 RefTypeId superClassId;
Ian Rogersc0542af2014-09-03 16:16:56 -0700602 JdwpError status = Dbg::GetSuperclass(class_id, &superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800603 if (status != ERR_NONE) {
604 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800605 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700606 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700607 return ERR_NONE;
608}
609
610/*
611 * Set static class values.
612 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700613static JdwpError CT_SetValues(JdwpState* , Request* request, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700614 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700615 RefTypeId class_id = request->ReadRefTypeId();
616 int32_t values_count = request->ReadSigned32("values count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617
Elliott Hughesa96836a2013-01-17 12:27:49 -0800618 UNUSED(class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700619
Elliott Hughesa96836a2013-01-17 12:27:49 -0800620 for (int32_t i = 0; i < values_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700621 FieldId fieldId = request->ReadFieldId();
Elliott Hughesaed4be92011-12-02 16:16:23 -0800622 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800623 size_t width = Dbg::GetTagWidth(fieldTag);
Ian Rogersc0542af2014-09-03 16:16:56 -0700624 uint64_t value = request->ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700625
Elliott Hughesa96836a2013-01-17 12:27:49 -0800626 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " --> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800627 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
628 if (status != ERR_NONE) {
629 return status;
630 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700631 }
632
633 return ERR_NONE;
634}
635
636/*
637 * Invoke a static method.
638 *
639 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
640 * values in the "variables" display.
641 */
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200642static JdwpError CT_InvokeMethod(JdwpState* state, Request* request,
643 ExpandBuf* pReply ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700644 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700645 RefTypeId class_id = request->ReadRefTypeId();
646 ObjectId thread_id = request->ReadThreadId();
647 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700648
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200649 return RequestInvoke(state, request, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700650}
651
652/*
653 * Create a new object of the requested type, and invoke the specified
654 * constructor.
655 *
656 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
657 * see the contents of a byte[] as a string.
658 */
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200659static JdwpError CT_NewInstance(JdwpState* state, Request* request,
660 ExpandBuf* pReply ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700661 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700662 RefTypeId class_id = request->ReadRefTypeId();
663 ObjectId thread_id = request->ReadThreadId();
664 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700665
Elliott Hughes74847412012-06-20 18:10:21 -0700666 ObjectId object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700667 JdwpError status = Dbg::CreateObject(class_id, &object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800668 if (status != ERR_NONE) {
669 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800670 }
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200671 return RequestInvoke(state, request, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700672}
673
674/*
675 * Create a new array object of the requested type and length.
676 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700677static JdwpError AT_newInstance(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700678 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700679 RefTypeId arrayTypeId = request->ReadRefTypeId();
680 int32_t length = request->ReadSigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700681
Elliott Hughes74847412012-06-20 18:10:21 -0700682 ObjectId object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700683 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, &object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800684 if (status != ERR_NONE) {
685 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800686 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700687 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700688 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700689 return ERR_NONE;
690}
691
692/*
693 * Return line number information for the method, if present.
694 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700695static JdwpError M_LineTable(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700696 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700697 RefTypeId refTypeId = request->ReadRefTypeId();
698 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700699
Elliott Hughes74847412012-06-20 18:10:21 -0700700 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700701
702 return ERR_NONE;
703}
704
Ian Rogersc0542af2014-09-03 16:16:56 -0700705static JdwpError M_VariableTable(JdwpState*, Request* request, ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700706 bool generic)
Mathieu Chartier90443472015-07-16 20:32:27 -0700707 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700708 RefTypeId class_id = request->ReadRefTypeId();
709 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700710
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800711 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
712 // information. That will cause Eclipse to make a best-effort attempt at displaying local
713 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
714 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700715 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700716 return ERR_NONE;
717}
718
Ian Rogersc0542af2014-09-03 16:16:56 -0700719static JdwpError M_VariableTable(JdwpState* state, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700720 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800721 return M_VariableTable(state, request, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800722}
723
Ian Rogersc0542af2014-09-03 16:16:56 -0700724static JdwpError M_VariableTableWithGeneric(JdwpState* state, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700725 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800726 return M_VariableTable(state, request, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800727}
728
Ian Rogersc0542af2014-09-03 16:16:56 -0700729static JdwpError M_Bytecodes(JdwpState*, Request* request, ExpandBuf* reply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700730 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700731 RefTypeId class_id = request->ReadRefTypeId();
732 MethodId method_id = request->ReadMethodId();
Elliott Hughes9777ba22013-01-17 09:04:19 -0800733
734 std::vector<uint8_t> bytecodes;
Ian Rogersc0542af2014-09-03 16:16:56 -0700735 JdwpError rc = Dbg::GetBytecodes(class_id, method_id, &bytecodes);
Elliott Hughes9777ba22013-01-17 09:04:19 -0800736 if (rc != ERR_NONE) {
737 return rc;
738 }
739
740 expandBufAdd4BE(reply, bytecodes.size());
741 for (size_t i = 0; i < bytecodes.size(); ++i) {
742 expandBufAdd1(reply, bytecodes[i]);
743 }
744
745 return ERR_NONE;
746}
747
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700748/*
749 * Given an object reference, return the runtime type of the object
750 * (class or array).
751 *
Elliott Hughes74847412012-06-20 18:10:21 -0700752 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700753 * passed in here.
754 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700755static JdwpError OR_ReferenceType(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700756 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700757 ObjectId object_id = request->ReadObjectId();
Elliott Hughes74847412012-06-20 18:10:21 -0700758 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700759}
760
761/*
762 * Get values from the fields of an object.
763 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700764static JdwpError OR_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700765 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700766 ObjectId object_id = request->ReadObjectId();
767 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700768
Elliott Hughes0cf74332012-02-23 23:14:00 -0800769 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800770 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700771 FieldId fieldId = request->ReadFieldId();
Elliott Hughes74847412012-06-20 18:10:21 -0700772 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800773 if (status != ERR_NONE) {
774 return status;
775 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700776 }
777
778 return ERR_NONE;
779}
780
781/*
782 * Set values in the fields of an object.
783 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700784static JdwpError OR_SetValues(JdwpState*, Request* request, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700785 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700786 ObjectId object_id = request->ReadObjectId();
787 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700788
Elliott Hughesa96836a2013-01-17 12:27:49 -0800789 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700790 FieldId fieldId = request->ReadFieldId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700791
Elliott Hughesaed4be92011-12-02 16:16:23 -0800792 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800793 size_t width = Dbg::GetTagWidth(fieldTag);
Ian Rogersc0542af2014-09-03 16:16:56 -0700794 uint64_t value = request->ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700795
Elliott Hughes2435a572012-02-17 16:07:41 -0800796 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700797 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
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
Ian Rogersc0542af2014-09-03 16:16:56 -0700806static JdwpError OR_MonitorInfo(JdwpState*, Request* request, ExpandBuf* reply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700807 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700808 ObjectId object_id = request->ReadObjectId();
Elliott Hughesf327e072013-01-09 16:01:26 -0800809 return Dbg::GetMonitorInfo(object_id, reply);
810}
811
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700812/*
813 * Invoke an instance method. The invocation must occur in the specified
814 * thread, which must have been suspended by an event.
815 *
816 * The call is synchronous. All threads in the VM are resumed, unless the
817 * SINGLE_THREADED flag is set.
818 *
819 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
820 * object), it will try to invoke the object's toString() function. This
821 * feature becomes crucial when examining ArrayLists with Eclipse.
822 */
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200823static JdwpError OR_InvokeMethod(JdwpState* state, Request* request,
824 ExpandBuf* pReply ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700825 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700826 ObjectId object_id = request->ReadObjectId();
827 ObjectId thread_id = request->ReadThreadId();
828 RefTypeId class_id = request->ReadRefTypeId();
829 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700830
Sebastien Hertzcbc50642015-06-01 17:33:12 +0200831 return RequestInvoke(state, request, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700832}
833
Ian Rogersc0542af2014-09-03 16:16:56 -0700834static JdwpError OR_DisableCollection(JdwpState*, Request* request, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700835 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700836 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800837 return Dbg::DisableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700838}
839
Ian Rogersc0542af2014-09-03 16:16:56 -0700840static JdwpError OR_EnableCollection(JdwpState*, Request* request, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700841 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700842 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800843 return Dbg::EnableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700844}
845
Ian Rogersc0542af2014-09-03 16:16:56 -0700846static JdwpError OR_IsCollected(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700847 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700848 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800849 bool is_collected;
Ian Rogersc0542af2014-09-03 16:16:56 -0700850 JdwpError rc = Dbg::IsCollected(object_id, &is_collected);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800851 expandBufAdd1(pReply, is_collected ? 1 : 0);
852 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700853}
854
Ian Rogersc0542af2014-09-03 16:16:56 -0700855static JdwpError OR_ReferringObjects(JdwpState*, Request* request, ExpandBuf* reply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700856 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700857 ObjectId object_id = request->ReadObjectId();
858 int32_t max_count = request->ReadSigned32("max count");
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800859 if (max_count < 0) {
860 return ERR_ILLEGAL_ARGUMENT;
861 }
862
863 std::vector<ObjectId> referring_objects;
Ian Rogersc0542af2014-09-03 16:16:56 -0700864 JdwpError rc = Dbg::GetReferringObjects(object_id, max_count, &referring_objects);
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800865 if (rc != ERR_NONE) {
866 return rc;
867 }
868
869 return WriteTaggedObjectList(reply, referring_objects);
870}
871
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700872/*
873 * Return the string value in a string object.
874 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700875static JdwpError SR_Value(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700876 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700877 ObjectId stringObject = request->ReadObjectId();
Sebastien Hertzb0b0b492014-09-15 11:27:27 +0200878 std::string str;
879 JDWP::JdwpError error = Dbg::StringToUtf8(stringObject, &str);
880 if (error != JDWP::ERR_NONE) {
881 return error;
882 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700883
Ian Rogers68b56852014-08-29 20:19:11 -0700884 VLOG(jdwp) << StringPrintf(" --> %s", PrintableString(str.c_str()).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700885
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800886 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700887
888 return ERR_NONE;
889}
890
891/*
892 * Return a thread's name.
893 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700894static JdwpError TR_Name(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700895 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700896 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700897
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800898 std::string name;
Ian Rogersc0542af2014-09-03 16:16:56 -0700899 JdwpError error = Dbg::GetThreadName(thread_id, &name);
Elliott Hughes221229c2013-01-08 18:17:50 -0800900 if (error != ERR_NONE) {
901 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700902 }
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800903 VLOG(jdwp) << StringPrintf(" Name of thread %#" PRIx64 " is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800904 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700905
906 return ERR_NONE;
907}
908
909/*
910 * Suspend the specified thread.
911 *
912 * It's supposed to remain suspended even if interpreted code wants to
913 * resume it; only the JDI is allowed to resume it.
914 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700915static JdwpError TR_Suspend(JdwpState*, Request* request, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700916 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700917 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700918
Elliott Hughes74847412012-06-20 18:10:21 -0700919 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700920 LOG(INFO) << " Warning: ignoring request to suspend self";
921 return ERR_THREAD_NOT_SUSPENDED;
922 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800923
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700924 Thread* self = Thread::Current();
925 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
926 JdwpError result = Dbg::SuspendThread(thread_id);
927 self->TransitionFromSuspendedToRunnable();
928 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700929}
930
931/*
932 * Resume the specified thread.
933 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700934static JdwpError TR_Resume(JdwpState*, Request* request, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -0700935 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700936 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700937
Elliott Hughes74847412012-06-20 18:10:21 -0700938 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700939 LOG(INFO) << " Warning: ignoring request to resume self";
940 return ERR_NONE;
941 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800942
Elliott Hughes74847412012-06-20 18:10:21 -0700943 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700944 return ERR_NONE;
945}
946
947/*
948 * Return status of specified thread.
949 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700950static JdwpError TR_Status(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700951 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700952 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700953
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800954 JDWP::JdwpThreadStatus threadStatus;
955 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -0800956 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
957 if (error != ERR_NONE) {
958 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700959 }
960
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800961 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700962
963 expandBufAdd4BE(pReply, threadStatus);
964 expandBufAdd4BE(pReply, suspendStatus);
965
966 return ERR_NONE;
967}
968
969/*
970 * Return the thread group that the specified thread is a member of.
971 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700972static JdwpError TR_ThreadGroup(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700973 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700974 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -0700975 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700976}
977
978/*
979 * Return the current call stack of a suspended thread.
980 *
981 * If the thread isn't suspended, the error code isn't defined, but should
982 * be THREAD_NOT_SUSPENDED.
983 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700984static JdwpError TR_Frames(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -0700985 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700986 ObjectId thread_id = request->ReadThreadId();
987 uint32_t start_frame = request->ReadUnsigned32("start frame");
988 uint32_t length = request->ReadUnsigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700989
Elliott Hughes221229c2013-01-08 18:17:50 -0800990 size_t actual_frame_count;
Ian Rogersc0542af2014-09-03 16:16:56 -0700991 JdwpError error = Dbg::GetThreadFrameCount(thread_id, &actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -0800992 if (error != ERR_NONE) {
993 return error;
994 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800996 if (actual_frame_count <= 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700997 return ERR_THREAD_NOT_SUSPENDED; // 0 means no managed frames (which means "in native").
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700998 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700999
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001000 if (start_frame > actual_frame_count) {
1001 return ERR_INVALID_INDEX;
1002 }
1003 if (length == static_cast<uint32_t>(-1)) {
1004 length = actual_frame_count - start_frame;
1005 }
1006 if (start_frame + length > actual_frame_count) {
1007 return ERR_INVALID_LENGTH;
1008 }
1009
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001010 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001011}
1012
1013/*
1014 * Returns the #of frames on the specified thread, which must be suspended.
1015 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001016static JdwpError TR_FrameCount(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001017 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001018 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001019
Elliott Hughes221229c2013-01-08 18:17:50 -08001020 size_t frame_count;
Ian Rogersc0542af2014-09-03 16:16:56 -07001021 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, &frame_count);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001022 if (rc != ERR_NONE) {
1023 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001024 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001025 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001026
1027 return ERR_NONE;
1028}
1029
Ian Rogersc0542af2014-09-03 16:16:56 -07001030static JdwpError TR_OwnedMonitors(Request* request, ExpandBuf* reply, bool with_stack_depths)
Mathieu Chartier90443472015-07-16 20:32:27 -07001031 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001032 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001033
1034 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001035 std::vector<uint32_t> stack_depths;
Ian Rogersc0542af2014-09-03 16:16:56 -07001036 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, &monitors, &stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001037 if (rc != ERR_NONE) {
1038 return rc;
1039 }
1040
1041 expandBufAdd4BE(reply, monitors.size());
1042 for (size_t i = 0; i < monitors.size(); ++i) {
1043 rc = WriteTaggedObject(reply, monitors[i]);
1044 if (rc != ERR_NONE) {
1045 return rc;
1046 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001047 if (with_stack_depths) {
1048 expandBufAdd4BE(reply, stack_depths[i]);
1049 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001050 }
1051 return ERR_NONE;
1052}
1053
Ian Rogersc0542af2014-09-03 16:16:56 -07001054static JdwpError TR_OwnedMonitors(JdwpState*, Request* request, ExpandBuf* reply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001055 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001056 return TR_OwnedMonitors(request, reply, false);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001057}
1058
Ian Rogersc0542af2014-09-03 16:16:56 -07001059static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, Request* request, ExpandBuf* reply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001060 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001061 return TR_OwnedMonitors(request, reply, true);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001062}
1063
Ian Rogersc0542af2014-09-03 16:16:56 -07001064static JdwpError TR_CurrentContendedMonitor(JdwpState*, Request* request, ExpandBuf* reply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001065 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001066 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001067
Elliott Hughesf9501702013-01-11 11:22:27 -08001068 ObjectId contended_monitor;
Ian Rogersc0542af2014-09-03 16:16:56 -07001069 JdwpError rc = Dbg::GetContendedMonitor(thread_id, &contended_monitor);
Elliott Hughesf9501702013-01-11 11:22:27 -08001070 if (rc != ERR_NONE) {
1071 return rc;
1072 }
1073 return WriteTaggedObject(reply, contended_monitor);
1074}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001075
Ian Rogersc0542af2014-09-03 16:16:56 -07001076static JdwpError TR_Interrupt(JdwpState*, Request* request, ExpandBuf* reply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001077 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001078 UNUSED(reply);
Ian Rogersc0542af2014-09-03 16:16:56 -07001079 ObjectId thread_id = request->ReadThreadId();
Elliott Hughesf9501702013-01-11 11:22:27 -08001080 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001081}
1082
1083/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001085 *
1086 * (The thread *might* still be running -- it might not have examined
1087 * its suspend count recently.)
1088 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001089static JdwpError TR_DebugSuspendCount(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001090 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001091 ObjectId thread_id = request->ReadThreadId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001093}
1094
1095/*
1096 * Return the name of a thread group.
1097 *
1098 * The Eclipse debugger recognizes "main" and "system" as special.
1099 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001100static JdwpError TGR_Name(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001101 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001102 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001103 return Dbg::GetThreadGroupName(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001104}
1105
1106/*
1107 * Returns the thread group -- if any -- that contains the specified
1108 * thread group.
1109 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001110static JdwpError TGR_Parent(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001111 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001112 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001113 return Dbg::GetThreadGroupParent(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001114}
1115
1116/*
1117 * Return the active threads and thread groups that are part of the
1118 * specified thread group.
1119 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001120static JdwpError TGR_Children(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001121 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001122 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001123 return Dbg::GetThreadGroupChildren(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001124}
1125
1126/*
1127 * Return the #of components in the array.
1128 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001129static JdwpError AR_Length(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001130 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001131 ObjectId array_id = request->ReadArrayId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001132
Ian Rogersc0542af2014-09-03 16:16:56 -07001133 int32_t length;
1134 JdwpError status = Dbg::GetArrayLength(array_id, &length);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001135 if (status != ERR_NONE) {
1136 return status;
1137 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001138 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001139
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001140 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001141
1142 return ERR_NONE;
1143}
1144
1145/*
1146 * Return the values from an array.
1147 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001148static JdwpError AR_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001149 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001150 ObjectId array_id = request->ReadArrayId();
1151 uint32_t offset = request->ReadUnsigned32("offset");
1152 uint32_t length = request->ReadUnsigned32("length");
Elliott Hughesa96836a2013-01-17 12:27:49 -08001153 return Dbg::OutputArray(array_id, offset, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001154}
1155
1156/*
1157 * Set values in an array.
1158 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001159static JdwpError AR_SetValues(JdwpState*, Request* request, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -07001160 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001161 ObjectId array_id = request->ReadArrayId();
1162 uint32_t offset = request->ReadUnsigned32("offset");
1163 uint32_t count = request->ReadUnsigned32("count");
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001164 return Dbg::SetArrayElements(array_id, offset, count, request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001165}
1166
Ian Rogersc0542af2014-09-03 16:16:56 -07001167static JdwpError CLR_VisibleClasses(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001168 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001169 request->ReadObjectId(); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001170 // TODO: we should only return classes which have the given class loader as a defining or
1171 // initiating loader. The former would be easy; the latter is hard, because we don't have
1172 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001173 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001174}
1175
1176/*
1177 * Set an event trigger.
1178 *
1179 * Reply with a requestID.
1180 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001181static JdwpError ER_Set(JdwpState* state, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001182 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001183 JdwpEventKind event_kind = request->ReadEnum1<JdwpEventKind>("event kind");
1184 JdwpSuspendPolicy suspend_policy = request->ReadEnum1<JdwpSuspendPolicy>("suspend policy");
1185 int32_t modifier_count = request->ReadSigned32("modifier count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001186
Elliott Hughesa96836a2013-01-17 12:27:49 -08001187 CHECK_LT(modifier_count, 256); /* reasonableness check */
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001188
Elliott Hughesa96836a2013-01-17 12:27:49 -08001189 JdwpEvent* pEvent = EventAlloc(modifier_count);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001190 pEvent->eventKind = event_kind;
1191 pEvent->suspend_policy = suspend_policy;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001192 pEvent->modCount = modifier_count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001193
1194 /*
1195 * Read modifiers. Ordering may be significant (see explanation of Count
1196 * mods in JDWP doc).
1197 */
Elliott Hughesa96836a2013-01-17 12:27:49 -08001198 for (int32_t i = 0; i < modifier_count; ++i) {
Elliott Hughes972a47b2012-02-21 18:16:06 -08001199 JdwpEventMod& mod = pEvent->mods[i];
Ian Rogersc0542af2014-09-03 16:16:56 -07001200 mod.modKind = request->ReadModKind();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001201 switch (mod.modKind) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001202 case MK_COUNT:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001203 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001204 // Report once, when "--count" reaches 0.
Ian Rogersc0542af2014-09-03 16:16:56 -07001205 uint32_t count = request->ReadUnsigned32("count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001206 if (count == 0) {
1207 return ERR_INVALID_COUNT;
1208 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001209 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001210 }
1211 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001212 case MK_CONDITIONAL:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001213 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001214 // Conditional on expression.
Ian Rogersc0542af2014-09-03 16:16:56 -07001215 uint32_t exprId = request->ReadUnsigned32("expr id");
Elliott Hughes972a47b2012-02-21 18:16:06 -08001216 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001217 }
1218 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001219 case MK_THREAD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001220 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001221 // Only report events in specified thread.
Ian Rogersc0542af2014-09-03 16:16:56 -07001222 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001223 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001224 }
1225 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001226 case MK_CLASS_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001227 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001228 // For ClassPrepare, MethodEntry.
Ian Rogersc0542af2014-09-03 16:16:56 -07001229 RefTypeId class_id = request->ReadRefTypeId();
Elliott Hughes74847412012-06-20 18:10:21 -07001230 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001231 }
1232 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001233 case MK_CLASS_MATCH:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001234 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001235 // Restrict events to matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001236 // pattern is "java.foo.*", we want "java/foo/*".
Ian Rogersc0542af2014-09-03 16:16:56 -07001237 std::string pattern(request->ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001238 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001239 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001240 }
1241 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001242 case MK_CLASS_EXCLUDE:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001243 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001244 // Restrict events to non-matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001245 // pattern is "java.foo.*", we want "java/foo/*".
Ian Rogersc0542af2014-09-03 16:16:56 -07001246 std::string pattern(request->ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001247 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001248 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001249 }
1250 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001251 case MK_LOCATION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001252 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001253 // Restrict certain events based on location.
Ian Rogersc0542af2014-09-03 16:16:56 -07001254 JdwpLocation location = request->ReadLocation();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001255 mod.locationOnly.loc = location;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001256 }
1257 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001258 case MK_EXCEPTION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001259 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001260 // Modifies EK_EXCEPTION events,
Ian Rogersc0542af2014-09-03 16:16:56 -07001261 mod.exceptionOnly.refTypeId = request->ReadRefTypeId(); // null => all exceptions.
1262 mod.exceptionOnly.caught = request->ReadEnum1<uint8_t>("caught");
1263 mod.exceptionOnly.uncaught = request->ReadEnum1<uint8_t>("uncaught");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001264 }
1265 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001266 case MK_FIELD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001267 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001268 // For field access/modification events.
Ian Rogersc0542af2014-09-03 16:16:56 -07001269 RefTypeId declaring = request->ReadRefTypeId();
1270 FieldId fieldId = request->ReadFieldId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001271 mod.fieldOnly.refTypeId = declaring;
1272 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001273 }
1274 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001275 case MK_STEP:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001276 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001277 // For use with EK_SINGLE_STEP.
Ian Rogersc0542af2014-09-03 16:16:56 -07001278 ObjectId thread_id = request->ReadThreadId();
1279 uint32_t size = request->ReadUnsigned32("step size");
1280 uint32_t depth = request->ReadUnsigned32("step depth");
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001281 VLOG(jdwp) << StringPrintf(" Step: thread=%#" PRIx64, thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001282 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1283
Elliott Hughes74847412012-06-20 18:10:21 -07001284 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001285 mod.step.size = size;
1286 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001287 }
1288 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001289 case MK_INSTANCE_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001290 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001291 // Report events related to a specific object.
Ian Rogersc0542af2014-09-03 16:16:56 -07001292 ObjectId instance = request->ReadObjectId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001293 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001294 }
1295 break;
1296 default:
Sebastien Hertz6d7839e2014-12-05 10:52:15 +01001297 LOG(WARNING) << "Unsupported modifier " << mod.modKind << " for event " << pEvent->eventKind;
1298 // Free allocated event to avoid leak before leaving.
1299 EventFree(pEvent);
1300 return JDWP::ERR_NOT_IMPLEMENTED;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001301 }
1302 }
1303
1304 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001305 * We reply with an integer "requestID".
1306 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001307 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001308 expandBufAdd4BE(pReply, requestId);
1309
1310 pEvent->requestId = requestId;
1311
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001312 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001313
1314 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001315 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001316 if (err != ERR_NONE) {
1317 /* registration failed, probably because event is bogus */
1318 EventFree(pEvent);
1319 LOG(WARNING) << "WARNING: event request rejected";
1320 }
1321 return err;
1322}
1323
Ian Rogersc0542af2014-09-03 16:16:56 -07001324static JdwpError ER_Clear(JdwpState* state, Request* request, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -07001325 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001326 request->ReadEnum1<JdwpEventKind>("event kind");
1327 uint32_t requestId = request->ReadUnsigned32("request id");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001328
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001329 // Failure to find an event with a matching ID is a no-op
1330 // and does not return an error.
Elliott Hughes761928d2011-11-16 18:33:03 -08001331 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001332 return ERR_NONE;
1333}
1334
1335/*
1336 * Return the values of arguments and local variables.
1337 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001338static JdwpError SF_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001339 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz8009f392014-09-01 17:07:11 +02001340 return Dbg::GetLocalValues(request, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001341}
1342
1343/*
1344 * Set the values of arguments and local variables.
1345 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001346static JdwpError SF_SetValues(JdwpState*, Request* request, ExpandBuf*)
Mathieu Chartier90443472015-07-16 20:32:27 -07001347 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz8009f392014-09-01 17:07:11 +02001348 return Dbg::SetLocalValues(request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001349}
1350
Ian Rogersc0542af2014-09-03 16:16:56 -07001351static JdwpError SF_ThisObject(JdwpState*, Request* request, ExpandBuf* reply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001352 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001353 ObjectId thread_id = request->ReadThreadId();
1354 FrameId frame_id = request->ReadFrameId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001355
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001356 ObjectId object_id;
1357 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001358 if (rc != ERR_NONE) {
1359 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001360 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001361
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001362 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001363}
1364
1365/*
1366 * Return the reference type reflected by this class object.
1367 *
1368 * This appears to be required because ReferenceTypeId values are NEVER
1369 * reused, whereas ClassIds can be recycled like any other object. (Either
1370 * that, or I have no idea what this is for.)
1371 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001372static JdwpError COR_ReflectedType(JdwpState*, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001373 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001374 RefTypeId class_object_id = request->ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001375 return Dbg::GetReflectedType(class_object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001376}
1377
1378/*
1379 * Handle a DDM packet with a single chunk in it.
1380 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001381static JdwpError DDM_Chunk(JdwpState* state, Request* request, ExpandBuf* pReply)
Mathieu Chartier90443472015-07-16 20:32:27 -07001382 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001383 state->NotifyDdmsActive();
Sebastien Hertz7d955652014-10-22 10:57:10 +02001384 uint8_t* replyBuf = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001385 int replyLen = -1;
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001386 if (Dbg::DdmHandlePacket(request, &replyBuf, &replyLen)) {
1387 // If they want to send something back, we copy it into the buffer.
1388 // TODO: consider altering the JDWP stuff to hold the packet header
1389 // in a separate buffer. That would allow us to writev() DDM traffic
1390 // instead of copying it into the expanding buffer. The reduction in
1391 // heap requirements is probably more valuable than the efficiency.
1392 CHECK_GT(replyLen, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001393 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
Sebastien Hertz3901bbc2015-08-06 11:37:02 +02001394 delete[] replyBuf;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001395 }
1396 return ERR_NONE;
1397}
1398
1399/*
1400 * Handler map decl.
1401 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001402typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, Request* request, ExpandBuf* reply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001403
1404struct JdwpHandlerMap {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001405 uint8_t cmdSet;
1406 uint8_t cmd;
1407 JdwpRequestHandler func;
1408 const char* name;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001409};
1410
1411/*
1412 * Map commands to functions.
1413 *
1414 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1415 * and 128-256 are vendor-defined.
1416 */
Elliott Hughescb693062013-02-21 09:48:08 -08001417static const JdwpHandlerMap gHandlers[] = {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001418 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001419 { 1, 1, VM_Version, "VirtualMachine.Version" },
1420 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1421 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1422 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1423 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1424 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1425 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1426 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1427 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1428 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1429 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1430 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1431 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1432 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001433 { 1, 15, nullptr, "VirtualMachine.HoldEvents" },
1434 { 1, 16, nullptr, "VirtualMachine.ReleaseEvents" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001435 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001436 { 1, 18, nullptr, "VirtualMachine.RedefineClasses" },
1437 { 1, 19, nullptr, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001438 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001439 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001440
1441 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001442 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1443 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1444 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1445 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1446 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1447 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1448 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001449 { 2, 8, nullptr, "ReferenceType.NestedTypes" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001450 { 2, 9, RT_Status, "ReferenceType.Status" },
1451 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1452 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001453 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1454 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001455 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1456 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughes3b78c942013-01-15 17:35:41 -08001457 { 2, 16, RT_Instances, "ReferenceType.Instances" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001458 { 2, 17, nullptr, "ReferenceType.ClassFileVersion" },
1459 { 2, 18, nullptr, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001460
1461 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001462 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1463 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1464 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1465 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001466
1467 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001468 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001469
1470 /* InterfaceType command set (5) */
1471
1472 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001473 { 6, 1, M_LineTable, "Method.LineTable" },
1474 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughes9777ba22013-01-17 09:04:19 -08001475 { 6, 3, M_Bytecodes, "Method.Bytecodes" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001476 { 6, 4, nullptr, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001477 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001478
1479 /* Field command set (8) */
1480
1481 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001482 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1483 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1484 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001485 { 9, 4, nullptr, "ObjectReference.UNUSED" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001486 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1487 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001488 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001489 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1490 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001491 { 9, 10, OR_ReferringObjects, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001492
1493 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001494 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001495
1496 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001497 { 11, 1, TR_Name, "ThreadReference.Name" },
1498 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1499 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1500 { 11, 4, TR_Status, "ThreadReference.Status" },
1501 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1502 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1503 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1504 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1505 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001506 { 11, 10, nullptr, "ThreadReference.Stop" },
Elliott Hughes734b8c62013-01-11 15:32:45 -08001507 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1508 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1509 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001510 { 11, 14, nullptr, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001511
1512 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001513 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1514 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1515 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001516
1517 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001518 { 13, 1, AR_Length, "ArrayReference.Length" },
1519 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1520 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001521
1522 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001523 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001524
1525 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001526 { 15, 1, ER_Set, "EventRequest.Set" },
1527 { 15, 2, ER_Clear, "EventRequest.Clear" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001528 { 15, 3, nullptr, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001529
1530 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001531 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1532 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1533 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Sebastien Hertz7d955652014-10-22 10:57:10 +02001534 { 16, 4, nullptr, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001535
1536 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001537 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001538
1539 /* Event command set (64) */
Sebastien Hertz7d955652014-10-22 10:57:10 +02001540 { 64, 100, nullptr, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001541
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001542 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001543};
1544
Ian Rogersc0542af2014-09-03 16:16:56 -07001545static const char* GetCommandName(Request* request) {
Elliott Hughescb693062013-02-21 09:48:08 -08001546 for (size_t i = 0; i < arraysize(gHandlers); ++i) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001547 if (gHandlers[i].cmdSet == request->GetCommandSet() &&
1548 gHandlers[i].cmd == request->GetCommand()) {
Elliott Hughescb693062013-02-21 09:48:08 -08001549 return gHandlers[i].name;
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001550 }
1551 }
1552 return "?UNKNOWN?";
1553}
1554
Ian Rogersc0542af2014-09-03 16:16:56 -07001555static std::string DescribeCommand(Request* request) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001556 std::string result;
Elliott Hughes64f574f2013-02-20 14:57:12 -08001557 result += "REQUEST: ";
Elliott Hughescb693062013-02-21 09:48:08 -08001558 result += GetCommandName(request);
Ian Rogersc0542af2014-09-03 16:16:56 -07001559 result += StringPrintf(" (length=%zu id=0x%06x)", request->GetLength(), request->GetId());
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001560 return result;
1561}
1562
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001563// Returns true if the given command_set and command identify an "invoke" command.
1564static bool IsInvokeCommand(uint8_t command_set, uint8_t command) {
1565 if (command_set == kJDWPClassTypeCmdSet) {
1566 return command == kJDWPClassTypeInvokeMethodCmd || command == kJDWPClassTypeNewInstanceCmd;
1567 } else if (command_set == kJDWPObjectReferenceCmdSet) {
1568 return command == kJDWPObjectReferenceInvokeCmd;
1569 } else {
1570 return false;
1571 }
1572}
1573
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001574/*
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001575 * Process a request from the debugger. The skip_reply flag is set to true to indicate to the
1576 * caller the reply must not be sent to the debugger. This is used for invoke commands where the
1577 * reply is sent by the event thread after completing the invoke.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001578 *
1579 * On entry, the JDWP thread is in VMWAIT.
1580 */
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001581size_t JdwpState::ProcessRequest(Request* request, ExpandBuf* pReply, bool* skip_reply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001582 JdwpError result = ERR_NONE;
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001583 *skip_reply = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001584
Ian Rogersc0542af2014-09-03 16:16:56 -07001585 if (request->GetCommandSet() != kJDWPDdmCmdSet) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001586 /*
1587 * Activity from a debugger, not merely ddms. Mark us as having an
1588 * active debugger session, and zero out the last-activity timestamp
1589 * so waitForDebugger() doesn't return if we stall for a bit here.
1590 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001591 Dbg::GoActive();
Ian Rogers37f3c962014-07-17 11:25:30 -07001592 last_activity_time_ms_.StoreSequentiallyConsistent(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001593 }
1594
1595 /*
1596 * If a debugger event has fired in another thread, wait until the
Sebastien Hertz2bf93f42015-01-09 18:44:05 +01001597 * initiating thread has suspended itself before processing commands
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001598 * from the debugger. Otherwise we (the JDWP thread) could be told to
1599 * resume the thread before it has suspended.
1600 *
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001601 * Note that we MUST clear the event token before waking the event
1602 * thread up, or risk waiting for the thread to suspend after we've
1603 * told it to resume.
1604 */
Sebastien Hertz2bf93f42015-01-09 18:44:05 +01001605 AcquireJdwpTokenForCommand();
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001606
1607 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001608 * Tell the VM that we're running and shouldn't be interrupted by GC.
1609 * Do this after anything that can stall indefinitely.
1610 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001611 Thread* self = Thread::Current();
1612 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001613
1614 expandBufAddSpace(pReply, kJDWPHeaderLen);
1615
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001616 size_t i;
Elliott Hughescb693062013-02-21 09:48:08 -08001617 for (i = 0; i < arraysize(gHandlers); ++i) {
Sebastien Hertz7d955652014-10-22 10:57:10 +02001618 if (gHandlers[i].cmdSet == request->GetCommandSet() &&
1619 gHandlers[i].cmd == request->GetCommand() &&
1620 gHandlers[i].func != nullptr) {
Elliott Hughescb693062013-02-21 09:48:08 -08001621 VLOG(jdwp) << DescribeCommand(request);
1622 result = (*gHandlers[i].func)(this, request, pReply);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001623 if (result == ERR_NONE) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001624 request->CheckConsumed();
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001625 }
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001626 self->AssertNoPendingException();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001627 break;
1628 }
1629 }
Elliott Hughescb693062013-02-21 09:48:08 -08001630 if (i == arraysize(gHandlers)) {
1631 LOG(ERROR) << "Command not implemented: " << DescribeCommand(request);
Ian Rogersc0542af2014-09-03 16:16:56 -07001632 LOG(ERROR) << HexDump(request->data(), request->size(), false, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001633 result = ERR_NOT_IMPLEMENTED;
1634 }
1635
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001636 size_t replyLength = 0U;
1637 if (result == ERR_NONE && IsInvokeCommand(request->GetCommandSet(), request->GetCommand())) {
1638 // We successfully request an invoke in the event thread. It will send the reply once the
1639 // invoke completes so we must not send it now.
1640 *skip_reply = true;
1641 } else {
1642 /*
1643 * Set up the reply header.
1644 *
1645 * If we encountered an error, only send the header back.
1646 */
1647 uint8_t* replyBuf = expandBufGetBuffer(pReply);
1648 replyLength = (result == ERR_NONE) ? expandBufGetLength(pReply) : kJDWPHeaderLen;
1649 Set4BE(replyBuf + kJDWPHeaderSizeOffset, replyLength);
1650 Set4BE(replyBuf + kJDWPHeaderIdOffset, request->GetId());
1651 Set1(replyBuf + kJDWPHeaderFlagsOffset, kJDWPFlagReply);
1652 Set2BE(replyBuf + kJDWPHeaderErrorCodeOffset, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001653
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001654 CHECK_GT(expandBufGetLength(pReply), 0U) << GetCommandName(request) << " " << request->GetId();
Elliott Hughescb693062013-02-21 09:48:08 -08001655
Sebastien Hertzcbc50642015-06-01 17:33:12 +02001656 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
1657 VLOG(jdwp) << "REPLY: " << GetCommandName(request) << " " << result << " (length=" << respLen << ")";
1658 if (false) {
1659 VLOG(jdwp) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen, false, "");
1660 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001661 }
1662
Elliott Hughescb693062013-02-21 09:48:08 -08001663 VLOG(jdwp) << "----------";
1664
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001665 /*
1666 * Update last-activity timestamp. We really only need this during
1667 * the initial setup. Only update if this is a non-DDMS packet.
1668 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001669 if (request->GetCommandSet() != kJDWPDdmCmdSet) {
Ian Rogers37f3c962014-07-17 11:25:30 -07001670 last_activity_time_ms_.StoreSequentiallyConsistent(MilliTime());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001671 }
1672
1673 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001674 self->TransitionFromRunnableToSuspended(old_state);
Sebastien Hertz43c8d722014-03-18 12:19:52 +01001675
1676 return replyLength;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001677}
1678
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001679} // namespace JDWP
1680
1681} // namespace art