blob: 8ef146c0962fe4127a37844d3c59289e61c1be9c [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>
20
Elliott Hughesa96836a2013-01-17 12:27:49 -080021#include <string>
22
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "atomic.h"
24#include "base/logging.h"
25#include "base/macros.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080026#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "debugger.h"
28#include "jdwp/jdwp_constants.h"
29#include "jdwp/jdwp_event.h"
30#include "jdwp/jdwp_expand_buf.h"
31#include "jdwp/jdwp_priv.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "runtime.h"
Ian Rogers693ff612013-02-01 10:56:12 -080033#include "thread-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "UniquePtr.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080035
Elliott Hughes872d4ec2011-10-21 17:07:15 -070036namespace art {
37
38namespace JDWP {
39
Elliott Hughes4b9702c2013-02-20 18:13:24 -080040std::string DescribeField(const FieldId& field_id) {
Elliott Hughesa96836a2013-01-17 12:27:49 -080041 return StringPrintf("%#x (%s)", field_id, Dbg::GetFieldName(field_id).c_str());
42}
43
Elliott Hughes4b9702c2013-02-20 18:13:24 -080044std::string DescribeMethod(const MethodId& method_id) {
Elliott Hughesa96836a2013-01-17 12:27:49 -080045 return StringPrintf("%#x (%s)", method_id, Dbg::GetMethodName(method_id).c_str());
46}
47
Elliott Hughes4b9702c2013-02-20 18:13:24 -080048std::string DescribeRefTypeId(const RefTypeId& ref_type_id) {
Elliott Hughesa96836a2013-01-17 12:27:49 -080049 std::string signature("unknown");
50 Dbg::GetSignature(ref_type_id, signature);
51 return StringPrintf("%#llx (%s)", ref_type_id, signature.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -070052}
53
Elliott Hughes4b9702c2013-02-20 18:13:24 -080054// Helper function: write a variable-width value into the output input buffer.
Elliott Hughes4993bbc2013-01-10 15:41:25 -080055static void WriteValue(ExpandBuf* pReply, int width, uint64_t value) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070056 switch (width) {
57 case 1: expandBufAdd1(pReply, value); break;
58 case 2: expandBufAdd2BE(pReply, value); break;
59 case 4: expandBufAdd4BE(pReply, value); break;
60 case 8: expandBufAdd8BE(pReply, value); break;
61 default: LOG(FATAL) << width; break;
62 }
63}
64
Elliott Hughes4993bbc2013-01-10 15:41:25 -080065static JdwpError WriteTaggedObject(ExpandBuf* reply, ObjectId object_id)
66 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
67 uint8_t tag;
68 JdwpError rc = Dbg::GetObjectTag(object_id, tag);
69 if (rc == ERR_NONE) {
70 expandBufAdd1(reply, tag);
71 expandBufAddObjectId(reply, object_id);
72 }
73 return rc;
74}
75
Elliott Hughes0cbaff52013-01-16 15:28:01 -080076static JdwpError WriteTaggedObjectList(ExpandBuf* reply, const std::vector<ObjectId>& objects)
77 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
78 expandBufAdd4BE(reply, objects.size());
79 for (size_t i = 0; i < objects.size(); ++i) {
80 JdwpError rc = WriteTaggedObject(reply, objects[i]);
81 if (rc != ERR_NONE) {
82 return rc;
83 }
84 }
85 return ERR_NONE;
86}
87
Elliott Hughes872d4ec2011-10-21 17:07:15 -070088/*
89 * Common code for *_InvokeMethod requests.
90 *
Elliott Hughes74847412012-06-20 18:10:21 -070091 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -070092 * expected-to-be-void return value of the called function.
93 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -080094static JdwpError FinishInvoke(JdwpState*, Request& request, ExpandBuf* pReply,
Elliott Hughes74847412012-06-20 18:10:21 -070095 ObjectId thread_id, ObjectId object_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 RefTypeId class_id, MethodId method_id, bool is_constructor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -070098 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070099
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800100 int32_t arg_count = request.ReadSigned32("argument count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700101
Elliott Hughes74847412012-06-20 18:10:21 -0700102 VLOG(jdwp) << StringPrintf(" --> thread_id=%#llx object_id=%#llx", thread_id, object_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103 VLOG(jdwp) << StringPrintf(" class_id=%#llx method_id=%x %s.%s", class_id,
104 method_id, Dbg::GetClassName(class_id).c_str(),
Elliott Hughesa96836a2013-01-17 12:27:49 -0800105 Dbg::GetMethodName(method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800106 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700107
Elliott Hughes45651fd2012-02-21 15:48:20 -0800108 UniquePtr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
109 UniquePtr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800110 for (int32_t i = 0; i < arg_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800111 argTypes[i] = request.ReadTag();
Elliott Hughes45651fd2012-02-21 15:48:20 -0800112 size_t width = Dbg::GetTagWidth(argTypes[i]);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800113 argValues[i] = request.ReadValue(width);
Elliott Hughes229feb72012-02-23 13:33:29 -0800114 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#llx", width, argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700115 }
116
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800117 uint32_t options = request.ReadUnsigned32("InvokeOptions bit flags");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
119 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
120 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121
Elliott Hughes45651fd2012-02-21 15:48:20 -0800122 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123 uint64_t resultValue;
124 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700125 JdwpError err = Dbg::InvokeMethod(thread_id, object_id, class_id, method_id, arg_count, argValues.get(), argTypes.get(), options, &resultTag, &resultValue, &exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800127 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700128 }
129
130 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800131 if (is_constructor) {
132 // If we invoked a constructor (which actually returns void), return the receiver,
133 // unless we threw, in which case we return NULL.
134 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700135 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800136 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700137
Elliott Hughes45651fd2012-02-21 15:48:20 -0800138 size_t width = Dbg::GetTagWidth(resultTag);
139 expandBufAdd1(pReply, resultTag);
140 if (width != 0) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800141 WriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700142 }
143 expandBufAdd1(pReply, JT_OBJECT);
144 expandBufAddObjectId(pReply, exceptObjId);
145
Elliott Hughes229feb72012-02-23 13:33:29 -0800146 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" %#llx (except=%#llx)", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700147
148 /* show detailed debug output */
149 if (resultTag == JT_STRING && exceptObjId == 0) {
150 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800151 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800153 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700154 }
155 }
156 }
157
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700158 return err;
159}
160
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800161static JdwpError VM_Version(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes927bd292013-01-17 10:40:13 -0800163 // Text information on runtime version.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700164 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800165 expandBufAddUtf8String(pReply, version);
Elliott Hughes927bd292013-01-17 10:40:13 -0800166
167 // JDWP version numbers, major and minor.
168 expandBufAdd4BE(pReply, 1);
169 expandBufAdd4BE(pReply, 6);
170
171 // "java.version".
172 expandBufAddUtf8String(pReply, "1.6.0");
173
174 // "java.vm.name".
175 expandBufAddUtf8String(pReply, "Dalvik");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176
177 return ERR_NONE;
178}
179
180/*
181 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
182 * referenceTypeID. We need to send back more than one if the class has
183 * been loaded by multiple class loaders.
184 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800185static JdwpError VM_ClassesBySignature(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800187 std::string classDescriptor(request.ReadUtf8String());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700188
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800189 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800190 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700191
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800192 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700193
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800194 for (size_t i = 0; i < ids.size(); ++i) {
195 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800196 JDWP::JdwpTypeTag type_tag;
197 uint32_t class_status;
198 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
199 if (status != ERR_NONE) {
200 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800201 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202
Elliott Hughes436e3722012-02-17 20:01:47 -0800203 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800204 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800205 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700206 }
207
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700208 return ERR_NONE;
209}
210
211/*
212 * Handle request for the thread IDs of all running threads.
213 *
214 * We exclude ourselves from the list, because we don't allow ourselves
215 * to be suspended, and that violates some JDWP expectations.
216 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800217static JdwpError VM_AllThreads(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700219 std::vector<ObjectId> thread_ids;
Elliott Hughes026b1462012-06-28 20:43:49 -0700220 Dbg::GetThreads(0, thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700221
Elliott Hughescaf76542012-06-28 16:08:22 -0700222 expandBufAdd4BE(pReply, thread_ids.size());
223 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
224 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700225 }
226
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700227 return ERR_NONE;
228}
229
230/*
231 * List all thread groups that do not have a parent.
232 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800233static JdwpError VM_TopLevelThreadGroups(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235 /*
236 * TODO: maintain a list of parentless thread groups in the VM.
237 *
238 * For now, just return "system". Application threads are created
239 * in "main", which is a child of "system".
240 */
241 uint32_t groups = 1;
242 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700243 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
244 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700245
246 return ERR_NONE;
247}
248
249/*
250 * Respond with the sizes of the basic debugger types.
251 *
252 * All IDs are 8 bytes.
253 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800254static JdwpError VM_IDSizes(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700256 expandBufAdd4BE(pReply, sizeof(FieldId));
257 expandBufAdd4BE(pReply, sizeof(MethodId));
258 expandBufAdd4BE(pReply, sizeof(ObjectId));
259 expandBufAdd4BE(pReply, sizeof(RefTypeId));
260 expandBufAdd4BE(pReply, sizeof(FrameId));
261 return ERR_NONE;
262}
263
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800264static JdwpError VM_Dispose(JdwpState*, Request&, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700265 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes86964332012-02-15 19:37:42 -0800266 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700267 return ERR_NONE;
268}
269
270/*
271 * Suspend the execution of the application running in the VM (i.e. suspend
272 * all threads).
273 *
274 * This needs to increment the "suspend count" on all threads.
275 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800276static JdwpError VM_Suspend(JdwpState*, Request&, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700277 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800278 Thread* self = Thread::Current();
279 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700280 Dbg::SuspendVM();
jeffhaoa77f0f62012-12-05 17:19:31 -0800281 self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700282 return ERR_NONE;
283}
284
285/*
286 * Resume execution. Decrements the "suspend count" of all threads.
287 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800288static JdwpError VM_Resume(JdwpState*, Request&, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700289 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700290 Dbg::ResumeVM();
291 return ERR_NONE;
292}
293
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800294static JdwpError VM_Exit(JdwpState* state, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700295 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800296 uint32_t exit_status = request.ReadUnsigned32("exit_status");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800297 state->ExitAfterReplying(exit_status);
298 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700299}
300
301/*
302 * Create a new string in the VM and return its ID.
303 *
304 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
305 * string "java.util.Arrays".)
306 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800307static JdwpError VM_CreateString(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800309 std::string str(request.ReadUtf8String());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700310 ObjectId stringId = Dbg::CreateString(str);
311 if (stringId == 0) {
312 return ERR_OUT_OF_MEMORY;
313 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700314 expandBufAddObjectId(pReply, stringId);
315 return ERR_NONE;
316}
317
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800318static JdwpError VM_ClassPaths(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800320 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700321
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800322 std::vector<std::string> class_path;
323 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
324 expandBufAdd4BE(pReply, class_path.size());
325 for (size_t i = 0; i < class_path.size(); ++i) {
326 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700327 }
328
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800329 std::vector<std::string> boot_class_path;
330 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
331 expandBufAdd4BE(pReply, boot_class_path.size());
332 for (size_t i = 0; i < boot_class_path.size(); ++i) {
333 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700334 }
335
336 return ERR_NONE;
337}
338
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800339static JdwpError VM_DisposeObjects(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700340 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800341 size_t object_count = request.ReadUnsigned32("object_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800342 for (size_t i = 0; i < object_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800343 ObjectId object_id = request.ReadObjectId();
344 uint32_t reference_count = request.ReadUnsigned32("reference_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800345 Dbg::DisposeObject(object_id, reference_count);
346 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700347 return ERR_NONE;
348}
349
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800350static JdwpError VM_Capabilities(JdwpState*, Request&, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700351 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800352 expandBufAdd1(reply, false); // canWatchFieldModification
353 expandBufAdd1(reply, false); // canWatchFieldAccess
Elliott Hughes9777ba22013-01-17 09:04:19 -0800354 expandBufAdd1(reply, true); // canGetBytecodes
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800355 expandBufAdd1(reply, true); // canGetSyntheticAttribute
356 expandBufAdd1(reply, true); // canGetOwnedMonitorInfo
Elliott Hughesf9501702013-01-11 11:22:27 -0800357 expandBufAdd1(reply, true); // canGetCurrentContendedMonitor
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800358 expandBufAdd1(reply, true); // canGetMonitorInfo
359 return ERR_NONE;
360}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700361
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800362static JdwpError VM_CapabilitiesNew(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800363 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
364
365 // The first few capabilities are the same as those reported by the older call.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800366 VM_Capabilities(NULL, request, reply);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800367
368 expandBufAdd1(reply, false); // canRedefineClasses
369 expandBufAdd1(reply, false); // canAddMethod
370 expandBufAdd1(reply, false); // canUnrestrictedlyRedefineClasses
371 expandBufAdd1(reply, false); // canPopFrames
372 expandBufAdd1(reply, false); // canUseInstanceFilters
373 expandBufAdd1(reply, false); // canGetSourceDebugExtension
374 expandBufAdd1(reply, false); // canRequestVMDeathEvent
375 expandBufAdd1(reply, false); // canSetDefaultStratum
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800376 expandBufAdd1(reply, true); // 1.6: canGetInstanceInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800377 expandBufAdd1(reply, false); // 1.6: canRequestMonitorEvents
Elliott Hughes734b8c62013-01-11 15:32:45 -0800378 expandBufAdd1(reply, true); // 1.6: canGetMonitorFrameInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800379 expandBufAdd1(reply, false); // 1.6: canUseSourceNameFilters
380 expandBufAdd1(reply, false); // 1.6: canGetConstantPool
381 expandBufAdd1(reply, false); // 1.6: canForceEarlyReturn
382
383 // Fill in reserved22 through reserved32; note count started at 1.
384 for (size_t i = 22; i <= 32; ++i) {
385 expandBufAdd1(reply, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700386 }
387 return ERR_NONE;
388}
389
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700391 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800392 std::vector<JDWP::RefTypeId> classes;
393 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700394
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800395 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700396
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800397 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800398 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800399 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800400 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800401 uint32_t class_status;
402 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
403 if (status != ERR_NONE) {
404 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800405 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700406
Elliott Hughes436e3722012-02-17 20:01:47 -0800407 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800408 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800409 if (descriptor_and_status) {
410 expandBufAddUtf8String(pReply, descriptor);
411 if (generic) {
412 expandBufAddUtf8String(pReply, genericSignature);
413 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800414 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800415 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700416 }
417
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700418 return ERR_NONE;
419}
420
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800421static JdwpError VM_AllClasses(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700422 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700423 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800424}
425
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800426static JdwpError VM_AllClassesWithGeneric(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700427 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700428 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800429}
430
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800431static JdwpError VM_InstanceCounts(JdwpState*, Request& request, ExpandBuf* pReply)
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800432 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800433 int32_t class_count = request.ReadSigned32("class count");
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800434 if (class_count < 0) {
435 return ERR_ILLEGAL_ARGUMENT;
436 }
437 std::vector<RefTypeId> class_ids;
438 for (int32_t i = 0; i < class_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800439 class_ids.push_back(request.ReadRefTypeId());
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800440 }
441
442 std::vector<uint64_t> counts;
443 JdwpError rc = Dbg::GetInstanceCounts(class_ids, counts);
444 if (rc != ERR_NONE) {
445 return rc;
446 }
447
448 expandBufAdd4BE(pReply, counts.size());
449 for (size_t i = 0; i < counts.size(); ++i) {
450 expandBufAdd8BE(pReply, counts[i]);
451 }
452 return ERR_NONE;
453}
454
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800455static JdwpError RT_Modifiers(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700456 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800457 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800458 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700459}
460
461/*
462 * Get values from static fields in a reference type.
463 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800464static JdwpError RT_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700465 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800466 RefTypeId refTypeId = request.ReadRefTypeId();
467 int32_t field_count = request.ReadSigned32("field count");
Elliott Hughes0cf74332012-02-23 23:14:00 -0800468 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800469 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800470 FieldId fieldId = request.ReadFieldId();
Elliott Hughes0cf74332012-02-23 23:14:00 -0800471 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800472 if (status != ERR_NONE) {
473 return status;
474 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700475 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700476 return ERR_NONE;
477}
478
479/*
480 * Get the name of the source file in which a reference type was declared.
481 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800482static JdwpError RT_SourceFile(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700483 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800484 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes03181a82011-11-17 17:22:21 -0800485 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800486 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
487 if (status != ERR_NONE) {
488 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700489 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800490 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800491 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700492}
493
494/*
495 * Return the current status of the reference type.
496 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800497static JdwpError RT_Status(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700498 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800499 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800500 JDWP::JdwpTypeTag type_tag;
501 uint32_t class_status;
502 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
503 if (status != ERR_NONE) {
504 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800505 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800506 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700507 return ERR_NONE;
508}
509
510/*
511 * Return interfaces implemented directly by this class.
512 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800513static JdwpError RT_Interfaces(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700514 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800515 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800516 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700517}
518
519/*
520 * Return the class object corresponding to this type.
521 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800522static JdwpError RT_ClassObject(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700523 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800524 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -0800525 ObjectId class_object_id;
526 JdwpError status = Dbg::GetClassObject(refTypeId, class_object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800527 if (status != ERR_NONE) {
528 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800529 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800530 VLOG(jdwp) << StringPrintf(" --> ObjectId %#llx", class_object_id);
531 expandBufAddObjectId(pReply, class_object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700532 return ERR_NONE;
533}
534
535/*
536 * Returns the value of the SourceDebugExtension attribute.
537 *
538 * JDB seems interested, but DEX files don't currently support this.
539 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800540static JdwpError RT_SourceDebugExtension(JdwpState*, Request&, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700541 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700542 /* referenceTypeId in, string out */
543 return ERR_ABSENT_INFORMATION;
544}
545
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800546static JdwpError RT_Signature(JdwpState*, Request& request, ExpandBuf* pReply, bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700547 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800548 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700549
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800550 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800551 JdwpError status = Dbg::GetSignature(refTypeId, signature);
552 if (status != ERR_NONE) {
553 return status;
554 }
555 expandBufAddUtf8String(pReply, signature);
556 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800557 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700558 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700559 return ERR_NONE;
560}
561
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800562static JdwpError RT_Signature(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700563 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800564 return RT_Signature(state, request, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800565}
566
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800567static JdwpError RT_SignatureWithGeneric(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700568 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800569 return RT_Signature(state, request, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800570}
571
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700572/*
573 * Return the instance of java.lang.ClassLoader that loaded the specified
574 * reference type, or null if it was loaded by the system loader.
575 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800576static JdwpError RT_ClassLoader(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700577 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800578 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800579 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700580}
581
582/*
583 * Given a referenceTypeId, return a block of stuff that describes the
584 * fields declared by a class.
585 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800586static JdwpError RT_FieldsWithGeneric(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700587 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800588 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800589 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800590}
591
592// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800593static JdwpError RT_Fields(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700594 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800595 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800596 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700597}
598
599/*
600 * Given a referenceTypeID, return a block of goodies describing the
601 * methods declared by a class.
602 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800603static JdwpError RT_MethodsWithGeneric(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700604 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800605 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800606 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800607}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700608
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800609// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800610static JdwpError RT_Methods(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700611 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800612 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800613 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700614}
615
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800616static JdwpError RT_Instances(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes3b78c942013-01-15 17:35:41 -0800617 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800618 RefTypeId class_id = request.ReadRefTypeId();
619 int32_t max_count = request.ReadSigned32("max count");
Elliott Hughes3b78c942013-01-15 17:35:41 -0800620 if (max_count < 0) {
621 return ERR_ILLEGAL_ARGUMENT;
622 }
623
624 std::vector<ObjectId> instances;
625 JdwpError rc = Dbg::GetInstances(class_id, max_count, instances);
626 if (rc != ERR_NONE) {
627 return rc;
628 }
629
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800630 return WriteTaggedObjectList(reply, instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800631}
632
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700633/*
634 * Return the immediate superclass of a class.
635 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800636static JdwpError CT_Superclass(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700637 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800638 RefTypeId class_id = request.ReadRefTypeId();
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800639 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700640 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800641 if (status != ERR_NONE) {
642 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800643 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700644 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700645 return ERR_NONE;
646}
647
648/*
649 * Set static class values.
650 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800651static JdwpError CT_SetValues(JdwpState* , Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700652 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800653 RefTypeId class_id = request.ReadRefTypeId();
654 int32_t values_count = request.ReadSigned32("values count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700655
Elliott Hughesa96836a2013-01-17 12:27:49 -0800656 UNUSED(class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700657
Elliott Hughesa96836a2013-01-17 12:27:49 -0800658 for (int32_t i = 0; i < values_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800659 FieldId fieldId = request.ReadFieldId();
Elliott Hughesaed4be92011-12-02 16:16:23 -0800660 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800661 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800662 uint64_t value = request.ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700663
Elliott Hughesa96836a2013-01-17 12:27:49 -0800664 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " --> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800665 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
666 if (status != ERR_NONE) {
667 return status;
668 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700669 }
670
671 return ERR_NONE;
672}
673
674/*
675 * Invoke a static method.
676 *
677 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
678 * values in the "variables" display.
679 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800680static JdwpError CT_InvokeMethod(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700681 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800682 RefTypeId class_id = request.ReadRefTypeId();
683 ObjectId thread_id = request.ReadThreadId();
684 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700685
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800686 return FinishInvoke(state, request, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700687}
688
689/*
690 * Create a new object of the requested type, and invoke the specified
691 * constructor.
692 *
693 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
694 * see the contents of a byte[] as a string.
695 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800696static JdwpError CT_NewInstance(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700697 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800698 RefTypeId class_id = request.ReadRefTypeId();
699 ObjectId thread_id = request.ReadThreadId();
700 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700701
Elliott Hughes74847412012-06-20 18:10:21 -0700702 ObjectId object_id;
703 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800704 if (status != ERR_NONE) {
705 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800706 }
Elliott Hughes74847412012-06-20 18:10:21 -0700707 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700708 return ERR_OUT_OF_MEMORY;
709 }
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800710 return FinishInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700711}
712
713/*
714 * Create a new array object of the requested type and length.
715 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800716static JdwpError AT_newInstance(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700717 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800718 RefTypeId arrayTypeId = request.ReadRefTypeId();
719 int32_t length = request.ReadSigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700720
Elliott Hughes74847412012-06-20 18:10:21 -0700721 ObjectId object_id;
722 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800723 if (status != ERR_NONE) {
724 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800725 }
Elliott Hughes74847412012-06-20 18:10:21 -0700726 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700727 return ERR_OUT_OF_MEMORY;
728 }
729 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700730 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700731 return ERR_NONE;
732}
733
734/*
735 * Return line number information for the method, if present.
736 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800737static JdwpError M_LineTable(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700738 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800739 RefTypeId refTypeId = request.ReadRefTypeId();
740 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700741
Elliott Hughes74847412012-06-20 18:10:21 -0700742 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700743
744 return ERR_NONE;
745}
746
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800747static JdwpError M_VariableTable(JdwpState*, Request& request, ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700748 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700749 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800750 RefTypeId class_id = request.ReadRefTypeId();
751 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700752
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800753 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
754 // information. That will cause Eclipse to make a best-effort attempt at displaying local
755 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
756 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700757 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700758 return ERR_NONE;
759}
760
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800761static JdwpError M_VariableTable(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700762 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800763 return M_VariableTable(state, request, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800764}
765
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800766static JdwpError M_VariableTableWithGeneric(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700767 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800768 return M_VariableTable(state, request, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800769}
770
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800771static JdwpError M_Bytecodes(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes9777ba22013-01-17 09:04:19 -0800772 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800773 RefTypeId class_id = request.ReadRefTypeId();
774 MethodId method_id = request.ReadMethodId();
Elliott Hughes9777ba22013-01-17 09:04:19 -0800775
776 std::vector<uint8_t> bytecodes;
777 JdwpError rc = Dbg::GetBytecodes(class_id, method_id, bytecodes);
778 if (rc != ERR_NONE) {
779 return rc;
780 }
781
782 expandBufAdd4BE(reply, bytecodes.size());
783 for (size_t i = 0; i < bytecodes.size(); ++i) {
784 expandBufAdd1(reply, bytecodes[i]);
785 }
786
787 return ERR_NONE;
788}
789
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700790/*
791 * Given an object reference, return the runtime type of the object
792 * (class or array).
793 *
Elliott Hughes74847412012-06-20 18:10:21 -0700794 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700795 * passed in here.
796 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800797static JdwpError OR_ReferenceType(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700798 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800799 ObjectId object_id = request.ReadObjectId();
Elliott Hughes74847412012-06-20 18:10:21 -0700800 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801}
802
803/*
804 * Get values from the fields of an object.
805 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800806static JdwpError OR_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700807 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800808 ObjectId object_id = request.ReadObjectId();
809 int32_t field_count = request.ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700810
Elliott Hughes0cf74332012-02-23 23:14:00 -0800811 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800812 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800813 FieldId fieldId = request.ReadFieldId();
Elliott Hughes74847412012-06-20 18:10:21 -0700814 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800815 if (status != ERR_NONE) {
816 return status;
817 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700818 }
819
820 return ERR_NONE;
821}
822
823/*
824 * Set values in the fields of an object.
825 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800826static JdwpError OR_SetValues(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700827 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800828 ObjectId object_id = request.ReadObjectId();
829 int32_t field_count = request.ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700830
Elliott Hughesa96836a2013-01-17 12:27:49 -0800831 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800832 FieldId fieldId = request.ReadFieldId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700833
Elliott Hughesaed4be92011-12-02 16:16:23 -0800834 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800835 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800836 uint64_t value = request.ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700837
Elliott Hughes2435a572012-02-17 16:07:41 -0800838 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700839 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800840 if (status != ERR_NONE) {
841 return status;
842 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700843 }
844
845 return ERR_NONE;
846}
847
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800848static JdwpError OR_MonitorInfo(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughesf327e072013-01-09 16:01:26 -0800849 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800850 ObjectId object_id = request.ReadObjectId();
Elliott Hughesf327e072013-01-09 16:01:26 -0800851 return Dbg::GetMonitorInfo(object_id, reply);
852}
853
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700854/*
855 * Invoke an instance method. The invocation must occur in the specified
856 * thread, which must have been suspended by an event.
857 *
858 * The call is synchronous. All threads in the VM are resumed, unless the
859 * SINGLE_THREADED flag is set.
860 *
861 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
862 * object), it will try to invoke the object's toString() function. This
863 * feature becomes crucial when examining ArrayLists with Eclipse.
864 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800865static JdwpError OR_InvokeMethod(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700866 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800867 ObjectId object_id = request.ReadObjectId();
868 ObjectId thread_id = request.ReadThreadId();
869 RefTypeId class_id = request.ReadRefTypeId();
870 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700871
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800872 return FinishInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700873}
874
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800875static JdwpError OR_DisableCollection(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700876 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800877 ObjectId object_id = request.ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800878 return Dbg::DisableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700879}
880
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800881static JdwpError OR_EnableCollection(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700882 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800883 ObjectId object_id = request.ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800884 return Dbg::EnableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700885}
886
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800887static JdwpError OR_IsCollected(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700888 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800889 ObjectId object_id = request.ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800890 bool is_collected;
891 JdwpError rc = Dbg::IsCollected(object_id, is_collected);
892 expandBufAdd1(pReply, is_collected ? 1 : 0);
893 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700894}
895
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800896static JdwpError OR_ReferringObjects(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800897 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800898 ObjectId object_id = request.ReadObjectId();
899 int32_t max_count = request.ReadSigned32("max count");
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800900 if (max_count < 0) {
901 return ERR_ILLEGAL_ARGUMENT;
902 }
903
904 std::vector<ObjectId> referring_objects;
905 JdwpError rc = Dbg::GetReferringObjects(object_id, max_count, referring_objects);
906 if (rc != ERR_NONE) {
907 return rc;
908 }
909
910 return WriteTaggedObjectList(reply, referring_objects);
911}
912
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700913/*
914 * Return the string value in a string object.
915 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800916static JdwpError SR_Value(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700917 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800918 ObjectId stringObject = request.ReadObjectId();
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800919 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700920
Elliott Hughesa96836a2013-01-17 12:27:49 -0800921 VLOG(jdwp) << StringPrintf(" --> %s", PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700922
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800923 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700924
925 return ERR_NONE;
926}
927
928/*
929 * Return a thread's name.
930 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800931static JdwpError TR_Name(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700932 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800933 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700934
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800935 std::string name;
Elliott Hughes221229c2013-01-08 18:17:50 -0800936 JdwpError error = Dbg::GetThreadName(thread_id, name);
937 if (error != ERR_NONE) {
938 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700939 }
Elliott Hughes74847412012-06-20 18:10:21 -0700940 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800941 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700942
943 return ERR_NONE;
944}
945
946/*
947 * Suspend the specified thread.
948 *
949 * It's supposed to remain suspended even if interpreted code wants to
950 * resume it; only the JDI is allowed to resume it.
951 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800952static JdwpError TR_Suspend(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700953 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800954 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700955
Elliott Hughes74847412012-06-20 18:10:21 -0700956 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700957 LOG(INFO) << " Warning: ignoring request to suspend self";
958 return ERR_THREAD_NOT_SUSPENDED;
959 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800960
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 Thread* self = Thread::Current();
962 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
963 JdwpError result = Dbg::SuspendThread(thread_id);
964 self->TransitionFromSuspendedToRunnable();
965 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700966}
967
968/*
969 * Resume the specified thread.
970 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800971static JdwpError TR_Resume(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700972 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800973 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700974
Elliott Hughes74847412012-06-20 18:10:21 -0700975 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700976 LOG(INFO) << " Warning: ignoring request to resume self";
977 return ERR_NONE;
978 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800979
Elliott Hughes74847412012-06-20 18:10:21 -0700980 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700981 return ERR_NONE;
982}
983
984/*
985 * Return status of specified thread.
986 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800987static JdwpError TR_Status(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700988 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800989 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700990
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800991 JDWP::JdwpThreadStatus threadStatus;
992 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -0800993 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
994 if (error != ERR_NONE) {
995 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700996 }
997
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800998 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700999
1000 expandBufAdd4BE(pReply, threadStatus);
1001 expandBufAdd4BE(pReply, suspendStatus);
1002
1003 return ERR_NONE;
1004}
1005
1006/*
1007 * Return the thread group that the specified thread is a member of.
1008 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001009static JdwpError TR_ThreadGroup(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001010 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001011 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001012 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001013}
1014
1015/*
1016 * Return the current call stack of a suspended thread.
1017 *
1018 * If the thread isn't suspended, the error code isn't defined, but should
1019 * be THREAD_NOT_SUSPENDED.
1020 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001021static JdwpError TR_Frames(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001022 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001023 ObjectId thread_id = request.ReadThreadId();
1024 uint32_t start_frame = request.ReadUnsigned32("start frame");
1025 uint32_t length = request.ReadUnsigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001026
Elliott Hughes221229c2013-01-08 18:17:50 -08001027 size_t actual_frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001028 JdwpError error = Dbg::GetThreadFrameCount(thread_id, actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001029 if (error != ERR_NONE) {
1030 return error;
1031 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001032
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001033 if (actual_frame_count <= 0) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001034 return ERR_THREAD_NOT_SUSPENDED; // 0 means no managed frames (which means "in native").
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001035 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001036
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001037 if (start_frame > actual_frame_count) {
1038 return ERR_INVALID_INDEX;
1039 }
1040 if (length == static_cast<uint32_t>(-1)) {
1041 length = actual_frame_count - start_frame;
1042 }
1043 if (start_frame + length > actual_frame_count) {
1044 return ERR_INVALID_LENGTH;
1045 }
1046
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001047 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001048}
1049
1050/*
1051 * Returns the #of frames on the specified thread, which must be suspended.
1052 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001053static JdwpError TR_FrameCount(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001054 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001055 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001056
Elliott Hughes221229c2013-01-08 18:17:50 -08001057 size_t frame_count;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001058 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, frame_count);
1059 if (rc != ERR_NONE) {
1060 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001061 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001062 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001063
1064 return ERR_NONE;
1065}
1066
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001067static JdwpError TR_OwnedMonitors(Request& request, ExpandBuf* reply, bool with_stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001068 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001069 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001070
1071 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001072 std::vector<uint32_t> stack_depths;
1073 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, monitors, stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001074 if (rc != ERR_NONE) {
1075 return rc;
1076 }
1077
1078 expandBufAdd4BE(reply, monitors.size());
1079 for (size_t i = 0; i < monitors.size(); ++i) {
1080 rc = WriteTaggedObject(reply, monitors[i]);
1081 if (rc != ERR_NONE) {
1082 return rc;
1083 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001084 if (with_stack_depths) {
1085 expandBufAdd4BE(reply, stack_depths[i]);
1086 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001087 }
1088 return ERR_NONE;
1089}
1090
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001091static JdwpError TR_OwnedMonitors(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001093 return TR_OwnedMonitors(request, reply, false);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001094}
1095
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001096static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001098 return TR_OwnedMonitors(request, reply, true);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001099}
1100
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001101static JdwpError TR_CurrentContendedMonitor(JdwpState*, Request& request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001103 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001104
Elliott Hughesf9501702013-01-11 11:22:27 -08001105 ObjectId contended_monitor;
1106 JdwpError rc = Dbg::GetContendedMonitor(thread_id, contended_monitor);
1107 if (rc != ERR_NONE) {
1108 return rc;
1109 }
1110 return WriteTaggedObject(reply, contended_monitor);
1111}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001112
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001113static JdwpError TR_Interrupt(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughesf9501702013-01-11 11:22:27 -08001114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001115 ObjectId thread_id = request.ReadThreadId();
Elliott Hughesf9501702013-01-11 11:22:27 -08001116 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001117}
1118
1119/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001120 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001121 *
1122 * (The thread *might* still be running -- it might not have examined
1123 * its suspend count recently.)
1124 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001125static JdwpError TR_DebugSuspendCount(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001127 ObjectId thread_id = request.ReadThreadId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001128 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001129}
1130
1131/*
1132 * Return the name of a thread group.
1133 *
1134 * The Eclipse debugger recognizes "main" and "system" as special.
1135 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001136static JdwpError TGR_Name(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001138 ObjectId thread_group_id = request.ReadThreadGroupId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001139
Elliott Hughescaf76542012-06-28 16:08:22 -07001140 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001141
1142 return ERR_NONE;
1143}
1144
1145/*
1146 * Returns the thread group -- if any -- that contains the specified
1147 * thread group.
1148 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001149static JdwpError TGR_Parent(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001151 ObjectId thread_group_id = request.ReadThreadGroupId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001152
Elliott Hughescaf76542012-06-28 16:08:22 -07001153 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001154 expandBufAddObjectId(pReply, parentGroup);
1155
1156 return ERR_NONE;
1157}
1158
1159/*
1160 * Return the active threads and thread groups that are part of the
1161 * specified thread group.
1162 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001163static JdwpError TGR_Children(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001165 ObjectId thread_group_id = request.ReadThreadGroupId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001166
Elliott Hughescaf76542012-06-28 16:08:22 -07001167 std::vector<ObjectId> thread_ids;
1168 Dbg::GetThreads(thread_group_id, thread_ids);
1169 expandBufAdd4BE(pReply, thread_ids.size());
1170 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1171 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001172 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001173
Elliott Hughescaf76542012-06-28 16:08:22 -07001174 std::vector<ObjectId> child_thread_groups_ids;
1175 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1176 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1177 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1178 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001179 }
1180
1181 return ERR_NONE;
1182}
1183
1184/*
1185 * Return the #of components in the array.
1186 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001187static JdwpError AR_Length(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001189 ObjectId array_id = request.ReadArrayId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001190
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001191 int length;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001192 JdwpError status = Dbg::GetArrayLength(array_id, length);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001193 if (status != ERR_NONE) {
1194 return status;
1195 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001196 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001197
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001198 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001199
1200 return ERR_NONE;
1201}
1202
1203/*
1204 * Return the values from an array.
1205 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001206static JdwpError AR_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001208 ObjectId array_id = request.ReadArrayId();
1209 uint32_t offset = request.ReadUnsigned32("offset");
1210 uint32_t length = request.ReadUnsigned32("length");
Elliott Hughesa96836a2013-01-17 12:27:49 -08001211 return Dbg::OutputArray(array_id, offset, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001212}
1213
1214/*
1215 * Set values in an array.
1216 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001217static JdwpError AR_SetValues(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001219 ObjectId array_id = request.ReadArrayId();
1220 uint32_t offset = request.ReadUnsigned32("offset");
1221 uint32_t count = request.ReadUnsigned32("count");
1222 return Dbg::SetArrayElements(array_id, offset, count, request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001223}
1224
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001225static JdwpError CLR_VisibleClasses(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001226 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001227 request.ReadObjectId(); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001228 // TODO: we should only return classes which have the given class loader as a defining or
1229 // initiating loader. The former would be easy; the latter is hard, because we don't have
1230 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001231 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001232}
1233
1234/*
1235 * Set an event trigger.
1236 *
1237 * Reply with a requestID.
1238 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001239static JdwpError ER_Set(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001241 JdwpEventKind event_kind = request.ReadEnum1<JdwpEventKind>("event kind");
1242 JdwpSuspendPolicy suspend_policy = request.ReadEnum1<JdwpSuspendPolicy>("suspend policy");
1243 int32_t modifier_count = request.ReadSigned32("modifier count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001244
Elliott Hughesa96836a2013-01-17 12:27:49 -08001245 CHECK_LT(modifier_count, 256); /* reasonableness check */
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001246
Elliott Hughesa96836a2013-01-17 12:27:49 -08001247 JdwpEvent* pEvent = EventAlloc(modifier_count);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001248 pEvent->eventKind = event_kind;
1249 pEvent->suspend_policy = suspend_policy;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001250 pEvent->modCount = modifier_count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001251
1252 /*
1253 * Read modifiers. Ordering may be significant (see explanation of Count
1254 * mods in JDWP doc).
1255 */
Elliott Hughesa96836a2013-01-17 12:27:49 -08001256 for (int32_t i = 0; i < modifier_count; ++i) {
Elliott Hughes972a47b2012-02-21 18:16:06 -08001257 JdwpEventMod& mod = pEvent->mods[i];
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001258 mod.modKind = request.ReadModKind();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001259 switch (mod.modKind) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001260 case MK_COUNT:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001261 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001262 // Report once, when "--count" reaches 0.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001263 uint32_t count = request.ReadUnsigned32("count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001264 if (count == 0) {
1265 return ERR_INVALID_COUNT;
1266 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001267 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001268 }
1269 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001270 case MK_CONDITIONAL:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001271 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001272 // Conditional on expression.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001273 uint32_t exprId = request.ReadUnsigned32("expr id");
Elliott Hughes972a47b2012-02-21 18:16:06 -08001274 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001275 }
1276 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001277 case MK_THREAD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001278 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001279 // Only report events in specified thread.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001280 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001281 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001282 }
1283 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001284 case MK_CLASS_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001285 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001286 // For ClassPrepare, MethodEntry.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001287 RefTypeId class_id = request.ReadRefTypeId();
Elliott Hughes74847412012-06-20 18:10:21 -07001288 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001289 }
1290 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001291 case MK_CLASS_MATCH:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001292 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001293 // Restrict events to matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001294 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001295 std::string pattern(request.ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001296 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001297 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001298 }
1299 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001300 case MK_CLASS_EXCLUDE:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001301 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001302 // Restrict events to non-matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001303 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001304 std::string pattern(request.ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001305 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001306 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001307 }
1308 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001309 case MK_LOCATION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001310 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001311 // Restrict certain events based on location.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001312 JdwpLocation location = request.ReadLocation();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001313 mod.locationOnly.loc = location;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001314 }
1315 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001316 case MK_EXCEPTION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001317 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001318 // Modifies EK_EXCEPTION events,
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001319 mod.exceptionOnly.refTypeId = request.ReadRefTypeId(); // null => all exceptions.
1320 mod.exceptionOnly.caught = request.ReadEnum1<uint8_t>("caught");
1321 mod.exceptionOnly.uncaught = request.ReadEnum1<uint8_t>("uncaught");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001322 }
1323 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001324 case MK_FIELD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001325 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001326 // For field access/modification events.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001327 RefTypeId declaring = request.ReadRefTypeId();
1328 FieldId fieldId = request.ReadFieldId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001329 mod.fieldOnly.refTypeId = declaring;
1330 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001331 }
1332 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001333 case MK_STEP:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001334 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001335 // For use with EK_SINGLE_STEP.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001336 ObjectId thread_id = request.ReadThreadId();
1337 uint32_t size = request.ReadUnsigned32("step size");
1338 uint32_t depth = request.ReadUnsigned32("step depth");
Elliott Hughes74847412012-06-20 18:10:21 -07001339 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001340 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1341
Elliott Hughes74847412012-06-20 18:10:21 -07001342 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001343 mod.step.size = size;
1344 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001345 }
1346 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001347 case MK_INSTANCE_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001348 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001349 // Report events related to a specific object.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001350 ObjectId instance = request.ReadObjectId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001351 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001352 }
1353 break;
1354 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001355 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001356 break;
1357 }
1358 }
1359
1360 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001361 * We reply with an integer "requestID".
1362 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001363 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001364 expandBufAdd4BE(pReply, requestId);
1365
1366 pEvent->requestId = requestId;
1367
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001368 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001369
1370 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001371 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001372 if (err != ERR_NONE) {
1373 /* registration failed, probably because event is bogus */
1374 EventFree(pEvent);
1375 LOG(WARNING) << "WARNING: event request rejected";
1376 }
1377 return err;
1378}
1379
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001380static JdwpError ER_Clear(JdwpState* state, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001381 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001382 request.ReadEnum1<JdwpEventKind>("event kind");
1383 uint32_t requestId = request.ReadUnsigned32("request id");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001384
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001385 // Failure to find an event with a matching ID is a no-op
1386 // and does not return an error.
Elliott Hughes761928d2011-11-16 18:33:03 -08001387 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001388 return ERR_NONE;
1389}
1390
1391/*
1392 * Return the values of arguments and local variables.
1393 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001394static JdwpError SF_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001396 ObjectId thread_id = request.ReadThreadId();
1397 FrameId frame_id = request.ReadFrameId();
1398 int32_t slot_count = request.ReadSigned32("slot count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001399
Elliott Hughesa96836a2013-01-17 12:27:49 -08001400 expandBufAdd4BE(pReply, slot_count); /* "int values" */
1401 for (int32_t i = 0; i < slot_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001402 uint32_t slot = request.ReadUnsigned32("slot");
1403 JDWP::JdwpTag reqSigByte = request.ReadTag();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001404
Elliott Hughes2435a572012-02-17 16:07:41 -08001405 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001406
Elliott Hughesdbb40792011-11-18 17:05:22 -08001407 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001408 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001409 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001410 }
1411
1412 return ERR_NONE;
1413}
1414
1415/*
1416 * Set the values of arguments and local variables.
1417 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001418static JdwpError SF_SetValues(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001419 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001420 ObjectId thread_id = request.ReadThreadId();
1421 FrameId frame_id = request.ReadFrameId();
1422 int32_t slot_count = request.ReadSigned32("slot count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001423
Elliott Hughesa96836a2013-01-17 12:27:49 -08001424 for (int32_t i = 0; i < slot_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001425 uint32_t slot = request.ReadUnsigned32("slot");
1426 JDWP::JdwpTag sigByte = request.ReadTag();
Elliott Hughesdbb40792011-11-18 17:05:22 -08001427 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001428 uint64_t value = request.ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001429
Elliott Hughes2435a572012-02-17 16:07:41 -08001430 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001431 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001432 }
1433
1434 return ERR_NONE;
1435}
1436
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001437static JdwpError SF_ThisObject(JdwpState*, Request& request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001438 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001439 ObjectId thread_id = request.ReadThreadId();
1440 FrameId frame_id = request.ReadFrameId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001441
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001442 ObjectId object_id;
1443 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001444 if (rc != ERR_NONE) {
1445 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001446 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001447
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001448 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001449}
1450
1451/*
1452 * Return the reference type reflected by this class object.
1453 *
1454 * This appears to be required because ReferenceTypeId values are NEVER
1455 * reused, whereas ClassIds can be recycled like any other object. (Either
1456 * that, or I have no idea what this is for.)
1457 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001458static JdwpError COR_ReflectedType(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001460 RefTypeId class_object_id = request.ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001461 return Dbg::GetReflectedType(class_object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001462}
1463
1464/*
1465 * Handle a DDM packet with a single chunk in it.
1466 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001467static JdwpError DDM_Chunk(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001468 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001469 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001470 uint8_t* replyBuf = NULL;
1471 int replyLen = -1;
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001472 if (Dbg::DdmHandlePacket(request, &replyBuf, &replyLen)) {
1473 // If they want to send something back, we copy it into the buffer.
1474 // TODO: consider altering the JDWP stuff to hold the packet header
1475 // in a separate buffer. That would allow us to writev() DDM traffic
1476 // instead of copying it into the expanding buffer. The reduction in
1477 // heap requirements is probably more valuable than the efficiency.
1478 CHECK_GT(replyLen, 0);
1479 CHECK_LT(replyLen, 1*1024*1024);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001480 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1481 free(replyBuf);
1482 }
1483 return ERR_NONE;
1484}
1485
1486/*
1487 * Handler map decl.
1488 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001489typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, Request& request, ExpandBuf* reply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001490
1491struct JdwpHandlerMap {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001492 uint8_t cmdSet;
1493 uint8_t cmd;
1494 JdwpRequestHandler func;
1495 const char* name;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001496};
1497
1498/*
1499 * Map commands to functions.
1500 *
1501 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1502 * and 128-256 are vendor-defined.
1503 */
Elliott Hughescb693062013-02-21 09:48:08 -08001504static const JdwpHandlerMap gHandlers[] = {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001505 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001506 { 1, 1, VM_Version, "VirtualMachine.Version" },
1507 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1508 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1509 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1510 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1511 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1512 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1513 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1514 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1515 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1516 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1517 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1518 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1519 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1520 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1521 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
1522 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1523 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1524 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001525 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001526 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001527
1528 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001529 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1530 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1531 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1532 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1533 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1534 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1535 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
1536 { 2, 8, NULL, "ReferenceType.NestedTypes" },
1537 { 2, 9, RT_Status, "ReferenceType.Status" },
1538 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1539 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001540 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1541 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001542 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1543 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughes3b78c942013-01-15 17:35:41 -08001544 { 2, 16, RT_Instances, "ReferenceType.Instances" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001545 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1546 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001547
1548 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001549 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1550 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1551 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1552 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001553
1554 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001555 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001556
1557 /* InterfaceType command set (5) */
1558
1559 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001560 { 6, 1, M_LineTable, "Method.LineTable" },
1561 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughes9777ba22013-01-17 09:04:19 -08001562 { 6, 3, M_Bytecodes, "Method.Bytecodes" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001563 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001564 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001565
1566 /* Field command set (8) */
1567
1568 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001569 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1570 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1571 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
1572 { 9, 4, NULL, "ObjectReference.UNUSED" },
1573 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1574 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001575 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001576 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1577 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001578 { 9, 10, OR_ReferringObjects, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001579
1580 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001581 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001582
1583 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001584 { 11, 1, TR_Name, "ThreadReference.Name" },
1585 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1586 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1587 { 11, 4, TR_Status, "ThreadReference.Status" },
1588 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1589 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1590 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1591 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1592 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1593 { 11, 10, NULL, "ThreadReference.Stop" },
1594 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1595 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1596 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1597 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001598
1599 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001600 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1601 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1602 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001603
1604 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001605 { 13, 1, AR_Length, "ArrayReference.Length" },
1606 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1607 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001608
1609 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001610 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001611
1612 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001613 { 15, 1, ER_Set, "EventRequest.Set" },
1614 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001615 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001616
1617 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001618 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1619 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1620 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001621 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001622
1623 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001624 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001625
1626 /* Event command set (64) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001627 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001628
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001629 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001630};
1631
Elliott Hughescb693062013-02-21 09:48:08 -08001632static const char* GetCommandName(Request& request) {
1633 for (size_t i = 0; i < arraysize(gHandlers); ++i) {
1634 if (gHandlers[i].cmdSet == request.GetCommandSet() && gHandlers[i].cmd == request.GetCommand()) {
1635 return gHandlers[i].name;
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001636 }
1637 }
1638 return "?UNKNOWN?";
1639}
1640
Elliott Hughescb693062013-02-21 09:48:08 -08001641static std::string DescribeCommand(Request& request) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001642 std::string result;
Elliott Hughes64f574f2013-02-20 14:57:12 -08001643 result += "REQUEST: ";
Elliott Hughescb693062013-02-21 09:48:08 -08001644 result += GetCommandName(request);
1645 result += StringPrintf(" (length=%d id=0x%06x)", request.GetLength(), request.GetId());
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001646 return result;
1647}
1648
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001649/*
1650 * Process a request from the debugger.
1651 *
1652 * On entry, the JDWP thread is in VMWAIT.
1653 */
Elliott Hughescb693062013-02-21 09:48:08 -08001654void JdwpState::ProcessRequest(Request& request, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001655 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001656
Elliott Hughescb693062013-02-21 09:48:08 -08001657 if (request.GetCommandSet() != kJDWPDdmCmdSet) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001658 /*
1659 * Activity from a debugger, not merely ddms. Mark us as having an
1660 * active debugger session, and zero out the last-activity timestamp
1661 * so waitForDebugger() doesn't return if we stall for a bit here.
1662 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001663 Dbg::GoActive();
Ian Rogers9adbff52013-01-23 18:19:03 -08001664 QuasiAtomic::Write64(&last_activity_time_ms_, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001665 }
1666
1667 /*
1668 * If a debugger event has fired in another thread, wait until the
1669 * initiating thread has suspended itself before processing messages
1670 * from the debugger. Otherwise we (the JDWP thread) could be told to
1671 * resume the thread before it has suspended.
1672 *
1673 * We call with an argument of zero to wait for the current event
1674 * thread to finish, and then clear the block. Depending on the thread
1675 * suspend policy, this may allow events in other threads to fire,
1676 * but those events have no bearing on what the debugger has sent us
1677 * in the current request.
1678 *
1679 * Note that we MUST clear the event token before waking the event
1680 * thread up, or risk waiting for the thread to suspend after we've
1681 * told it to resume.
1682 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001683 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001684
1685 /*
1686 * Tell the VM that we're running and shouldn't be interrupted by GC.
1687 * Do this after anything that can stall indefinitely.
1688 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 Thread* self = Thread::Current();
1690 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001691
1692 expandBufAddSpace(pReply, kJDWPHeaderLen);
1693
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001694 size_t i;
Elliott Hughescb693062013-02-21 09:48:08 -08001695 for (i = 0; i < arraysize(gHandlers); ++i) {
1696 if (gHandlers[i].cmdSet == request.GetCommandSet() && gHandlers[i].cmd == request.GetCommand() && gHandlers[i].func != NULL) {
1697 VLOG(jdwp) << DescribeCommand(request);
1698 result = (*gHandlers[i].func)(this, request, pReply);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001699 if (result == ERR_NONE) {
1700 request.CheckConsumed();
1701 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001702 break;
1703 }
1704 }
Elliott Hughescb693062013-02-21 09:48:08 -08001705 if (i == arraysize(gHandlers)) {
1706 LOG(ERROR) << "Command not implemented: " << DescribeCommand(request);
1707 LOG(ERROR) << HexDump(request.data(), request.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001708 result = ERR_NOT_IMPLEMENTED;
1709 }
1710
1711 /*
1712 * Set up the reply header.
1713 *
1714 * If we encountered an error, only send the header back.
1715 */
1716 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughescb693062013-02-21 09:48:08 -08001717 Set4BE(replyBuf + 4, request.GetId());
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001718 Set1(replyBuf + 8, kJDWPFlagReply);
1719 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001720 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001721 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001722 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001723 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001724 }
1725
Elliott Hughescb693062013-02-21 09:48:08 -08001726 CHECK_GT(expandBufGetLength(pReply), 0U) << GetCommandName(request) << " " << request.GetId();
1727
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001728 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughescb693062013-02-21 09:48:08 -08001729 VLOG(jdwp) << "REPLY: " << GetCommandName(request) << " " << result << " (length=" << respLen << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001730 if (false) {
Elliott Hughes64f574f2013-02-20 14:57:12 -08001731 VLOG(jdwp) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001732 }
1733
Elliott Hughescb693062013-02-21 09:48:08 -08001734 VLOG(jdwp) << "----------";
1735
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001736 /*
1737 * Update last-activity timestamp. We really only need this during
1738 * the initial setup. Only update if this is a non-DDMS packet.
1739 */
Elliott Hughescb693062013-02-21 09:48:08 -08001740 if (request.GetCommandSet() != kJDWPDdmCmdSet) {
Ian Rogers9adbff52013-01-23 18:19:03 -08001741 QuasiAtomic::Write64(&last_activity_time_ms_, MilliTime());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001742 }
1743
1744 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001745 self->TransitionFromRunnableToSuspended(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001746}
1747
1748} // namespace JDWP
1749
1750} // namespace art