blob: 523d89278a55cdb19b05a417b6d3e50821a5e84d [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");
Ian Rogersfc0e94b2013-09-23 23:51:32 -070050 Dbg::GetSignature(ref_type_id, &signature);
Elliott Hughesa96836a2013-01-17 12:27:49 -080051 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_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800364 // The first few capabilities are the same as those reported by the older call.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800365 VM_Capabilities(NULL, request, reply);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800366
367 expandBufAdd1(reply, false); // canRedefineClasses
368 expandBufAdd1(reply, false); // canAddMethod
369 expandBufAdd1(reply, false); // canUnrestrictedlyRedefineClasses
370 expandBufAdd1(reply, false); // canPopFrames
371 expandBufAdd1(reply, false); // canUseInstanceFilters
372 expandBufAdd1(reply, false); // canGetSourceDebugExtension
373 expandBufAdd1(reply, false); // canRequestVMDeathEvent
374 expandBufAdd1(reply, false); // canSetDefaultStratum
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800375 expandBufAdd1(reply, true); // 1.6: canGetInstanceInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800376 expandBufAdd1(reply, false); // 1.6: canRequestMonitorEvents
Elliott Hughes734b8c62013-01-11 15:32:45 -0800377 expandBufAdd1(reply, true); // 1.6: canGetMonitorFrameInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800378 expandBufAdd1(reply, false); // 1.6: canUseSourceNameFilters
379 expandBufAdd1(reply, false); // 1.6: canGetConstantPool
380 expandBufAdd1(reply, false); // 1.6: canForceEarlyReturn
381
382 // Fill in reserved22 through reserved32; note count started at 1.
383 for (size_t i = 22; i <= 32; ++i) {
384 expandBufAdd1(reply, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700385 }
386 return ERR_NONE;
387}
388
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700390 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800391 std::vector<JDWP::RefTypeId> classes;
392 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700393
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800394 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700395
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800396 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800397 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800398 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800399 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800400 uint32_t class_status;
401 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
402 if (status != ERR_NONE) {
403 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800404 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700405
Elliott Hughes436e3722012-02-17 20:01:47 -0800406 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800407 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800408 if (descriptor_and_status) {
409 expandBufAddUtf8String(pReply, descriptor);
410 if (generic) {
411 expandBufAddUtf8String(pReply, genericSignature);
412 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800413 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800414 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700415 }
416
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700417 return ERR_NONE;
418}
419
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800420static JdwpError VM_AllClasses(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700421 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700422 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800423}
424
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800425static JdwpError VM_AllClassesWithGeneric(JdwpState*, Request&, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700426 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700427 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800428}
429
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800430static JdwpError VM_InstanceCounts(JdwpState*, Request& request, ExpandBuf* pReply)
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800431 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800432 int32_t class_count = request.ReadSigned32("class count");
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800433 if (class_count < 0) {
434 return ERR_ILLEGAL_ARGUMENT;
435 }
436 std::vector<RefTypeId> class_ids;
437 for (int32_t i = 0; i < class_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800438 class_ids.push_back(request.ReadRefTypeId());
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800439 }
440
441 std::vector<uint64_t> counts;
442 JdwpError rc = Dbg::GetInstanceCounts(class_ids, counts);
443 if (rc != ERR_NONE) {
444 return rc;
445 }
446
447 expandBufAdd4BE(pReply, counts.size());
448 for (size_t i = 0; i < counts.size(); ++i) {
449 expandBufAdd8BE(pReply, counts[i]);
450 }
451 return ERR_NONE;
452}
453
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800454static JdwpError RT_Modifiers(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700455 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800456 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800457 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700458}
459
460/*
461 * Get values from static fields in a reference type.
462 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800463static JdwpError RT_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700464 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800465 RefTypeId refTypeId = request.ReadRefTypeId();
466 int32_t field_count = request.ReadSigned32("field count");
Elliott Hughes0cf74332012-02-23 23:14:00 -0800467 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800468 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800469 FieldId fieldId = request.ReadFieldId();
Elliott Hughes0cf74332012-02-23 23:14:00 -0800470 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800471 if (status != ERR_NONE) {
472 return status;
473 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700474 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700475 return ERR_NONE;
476}
477
478/*
479 * Get the name of the source file in which a reference type was declared.
480 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800481static JdwpError RT_SourceFile(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700482 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800483 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes03181a82011-11-17 17:22:21 -0800484 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800485 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
486 if (status != ERR_NONE) {
487 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700488 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800489 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800490 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700491}
492
493/*
494 * Return the current status of the reference type.
495 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800496static JdwpError RT_Status(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700497 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800498 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800499 JDWP::JdwpTypeTag type_tag;
500 uint32_t class_status;
501 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
502 if (status != ERR_NONE) {
503 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800504 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800505 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700506 return ERR_NONE;
507}
508
509/*
510 * Return interfaces implemented directly by this class.
511 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800512static JdwpError RT_Interfaces(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700513 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800514 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800515 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700516}
517
518/*
519 * Return the class object corresponding to this type.
520 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800521static JdwpError RT_ClassObject(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700522 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800523 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -0800524 ObjectId class_object_id;
525 JdwpError status = Dbg::GetClassObject(refTypeId, class_object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800526 if (status != ERR_NONE) {
527 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800528 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800529 VLOG(jdwp) << StringPrintf(" --> ObjectId %#llx", class_object_id);
530 expandBufAddObjectId(pReply, class_object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700531 return ERR_NONE;
532}
533
534/*
535 * Returns the value of the SourceDebugExtension attribute.
536 *
537 * JDB seems interested, but DEX files don't currently support this.
538 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800539static JdwpError RT_SourceDebugExtension(JdwpState*, Request&, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700540 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700541 /* referenceTypeId in, string out */
542 return ERR_ABSENT_INFORMATION;
543}
544
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800545static JdwpError RT_Signature(JdwpState*, Request& request, ExpandBuf* pReply, bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700546 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800547 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700548
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800549 std::string signature;
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700550 JdwpError status = Dbg::GetSignature(refTypeId, &signature);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800551 if (status != ERR_NONE) {
552 return status;
553 }
554 expandBufAddUtf8String(pReply, signature);
555 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800556 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700557 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700558 return ERR_NONE;
559}
560
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800561static JdwpError RT_Signature(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700562 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800563 return RT_Signature(state, request, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800564}
565
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800566static JdwpError RT_SignatureWithGeneric(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700567 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800568 return RT_Signature(state, request, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800569}
570
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700571/*
572 * Return the instance of java.lang.ClassLoader that loaded the specified
573 * reference type, or null if it was loaded by the system loader.
574 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800575static JdwpError RT_ClassLoader(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700576 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800577 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800578 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700579}
580
581/*
582 * Given a referenceTypeId, return a block of stuff that describes the
583 * fields declared by a class.
584 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800585static JdwpError RT_FieldsWithGeneric(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700586 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800587 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800588 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800589}
590
591// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800592static JdwpError RT_Fields(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700593 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800594 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800595 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700596}
597
598/*
599 * Given a referenceTypeID, return a block of goodies describing the
600 * methods declared by a class.
601 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800602static JdwpError RT_MethodsWithGeneric(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700603 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800604 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800605 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800606}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700607
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800608// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800609static JdwpError RT_Methods(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700610 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800611 RefTypeId refTypeId = request.ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800612 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700613}
614
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800615static JdwpError RT_Instances(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes3b78c942013-01-15 17:35:41 -0800616 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800617 RefTypeId class_id = request.ReadRefTypeId();
618 int32_t max_count = request.ReadSigned32("max count");
Elliott Hughes3b78c942013-01-15 17:35:41 -0800619 if (max_count < 0) {
620 return ERR_ILLEGAL_ARGUMENT;
621 }
622
623 std::vector<ObjectId> instances;
624 JdwpError rc = Dbg::GetInstances(class_id, max_count, instances);
625 if (rc != ERR_NONE) {
626 return rc;
627 }
628
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800629 return WriteTaggedObjectList(reply, instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800630}
631
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700632/*
633 * Return the immediate superclass of a class.
634 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800635static JdwpError CT_Superclass(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700636 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800637 RefTypeId class_id = request.ReadRefTypeId();
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800638 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700639 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800640 if (status != ERR_NONE) {
641 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800642 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700643 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700644 return ERR_NONE;
645}
646
647/*
648 * Set static class values.
649 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800650static JdwpError CT_SetValues(JdwpState* , Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700651 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800652 RefTypeId class_id = request.ReadRefTypeId();
653 int32_t values_count = request.ReadSigned32("values count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700654
Elliott Hughesa96836a2013-01-17 12:27:49 -0800655 UNUSED(class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700656
Elliott Hughesa96836a2013-01-17 12:27:49 -0800657 for (int32_t i = 0; i < values_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800658 FieldId fieldId = request.ReadFieldId();
Elliott Hughesaed4be92011-12-02 16:16:23 -0800659 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800660 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800661 uint64_t value = request.ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700662
Elliott Hughesa96836a2013-01-17 12:27:49 -0800663 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " --> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800664 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
665 if (status != ERR_NONE) {
666 return status;
667 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700668 }
669
670 return ERR_NONE;
671}
672
673/*
674 * Invoke a static method.
675 *
676 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
677 * values in the "variables" display.
678 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800679static JdwpError CT_InvokeMethod(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700680 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800681 RefTypeId class_id = request.ReadRefTypeId();
682 ObjectId thread_id = request.ReadThreadId();
683 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700684
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800685 return FinishInvoke(state, request, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700686}
687
688/*
689 * Create a new object of the requested type, and invoke the specified
690 * constructor.
691 *
692 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
693 * see the contents of a byte[] as a string.
694 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800695static JdwpError CT_NewInstance(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700696 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800697 RefTypeId class_id = request.ReadRefTypeId();
698 ObjectId thread_id = request.ReadThreadId();
699 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700700
Elliott Hughes74847412012-06-20 18:10:21 -0700701 ObjectId object_id;
702 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800703 if (status != ERR_NONE) {
704 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800705 }
Elliott Hughes74847412012-06-20 18:10:21 -0700706 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700707 return ERR_OUT_OF_MEMORY;
708 }
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800709 return FinishInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700710}
711
712/*
713 * Create a new array object of the requested type and length.
714 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800715static JdwpError AT_newInstance(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700716 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800717 RefTypeId arrayTypeId = request.ReadRefTypeId();
718 int32_t length = request.ReadSigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700719
Elliott Hughes74847412012-06-20 18:10:21 -0700720 ObjectId object_id;
721 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800722 if (status != ERR_NONE) {
723 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800724 }
Elliott Hughes74847412012-06-20 18:10:21 -0700725 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700726 return ERR_OUT_OF_MEMORY;
727 }
728 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700729 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700730 return ERR_NONE;
731}
732
733/*
734 * Return line number information for the method, if present.
735 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800736static JdwpError M_LineTable(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700737 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800738 RefTypeId refTypeId = request.ReadRefTypeId();
739 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700740
Elliott Hughes74847412012-06-20 18:10:21 -0700741 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742
743 return ERR_NONE;
744}
745
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800746static JdwpError M_VariableTable(JdwpState*, Request& request, ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700747 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700748 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800749 RefTypeId class_id = request.ReadRefTypeId();
750 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700751
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800752 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
753 // information. That will cause Eclipse to make a best-effort attempt at displaying local
754 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
755 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700756 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700757 return ERR_NONE;
758}
759
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800760static JdwpError M_VariableTable(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700761 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800762 return M_VariableTable(state, request, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800763}
764
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800765static JdwpError M_VariableTableWithGeneric(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700766 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800767 return M_VariableTable(state, request, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800768}
769
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800770static JdwpError M_Bytecodes(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes9777ba22013-01-17 09:04:19 -0800771 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800772 RefTypeId class_id = request.ReadRefTypeId();
773 MethodId method_id = request.ReadMethodId();
Elliott Hughes9777ba22013-01-17 09:04:19 -0800774
775 std::vector<uint8_t> bytecodes;
776 JdwpError rc = Dbg::GetBytecodes(class_id, method_id, bytecodes);
777 if (rc != ERR_NONE) {
778 return rc;
779 }
780
781 expandBufAdd4BE(reply, bytecodes.size());
782 for (size_t i = 0; i < bytecodes.size(); ++i) {
783 expandBufAdd1(reply, bytecodes[i]);
784 }
785
786 return ERR_NONE;
787}
788
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700789/*
790 * Given an object reference, return the runtime type of the object
791 * (class or array).
792 *
Elliott Hughes74847412012-06-20 18:10:21 -0700793 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700794 * passed in here.
795 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800796static JdwpError OR_ReferenceType(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700797 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800798 ObjectId object_id = request.ReadObjectId();
Elliott Hughes74847412012-06-20 18:10:21 -0700799 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700800}
801
802/*
803 * Get values from the fields of an object.
804 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800805static JdwpError OR_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700806 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800807 ObjectId object_id = request.ReadObjectId();
808 int32_t field_count = request.ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700809
Elliott Hughes0cf74332012-02-23 23:14:00 -0800810 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800811 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800812 FieldId fieldId = request.ReadFieldId();
Elliott Hughes74847412012-06-20 18:10:21 -0700813 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800814 if (status != ERR_NONE) {
815 return status;
816 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700817 }
818
819 return ERR_NONE;
820}
821
822/*
823 * Set values in the fields of an object.
824 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800825static JdwpError OR_SetValues(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700826 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800827 ObjectId object_id = request.ReadObjectId();
828 int32_t field_count = request.ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700829
Elliott Hughesa96836a2013-01-17 12:27:49 -0800830 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800831 FieldId fieldId = request.ReadFieldId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700832
Elliott Hughesaed4be92011-12-02 16:16:23 -0800833 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800834 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800835 uint64_t value = request.ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700836
Elliott Hughes2435a572012-02-17 16:07:41 -0800837 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700838 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800839 if (status != ERR_NONE) {
840 return status;
841 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700842 }
843
844 return ERR_NONE;
845}
846
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800847static JdwpError OR_MonitorInfo(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughesf327e072013-01-09 16:01:26 -0800848 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800849 ObjectId object_id = request.ReadObjectId();
Elliott Hughesf327e072013-01-09 16:01:26 -0800850 return Dbg::GetMonitorInfo(object_id, reply);
851}
852
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700853/*
854 * Invoke an instance method. The invocation must occur in the specified
855 * thread, which must have been suspended by an event.
856 *
857 * The call is synchronous. All threads in the VM are resumed, unless the
858 * SINGLE_THREADED flag is set.
859 *
860 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
861 * object), it will try to invoke the object's toString() function. This
862 * feature becomes crucial when examining ArrayLists with Eclipse.
863 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800864static JdwpError OR_InvokeMethod(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700865 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800866 ObjectId object_id = request.ReadObjectId();
867 ObjectId thread_id = request.ReadThreadId();
868 RefTypeId class_id = request.ReadRefTypeId();
869 MethodId method_id = request.ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700870
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800871 return FinishInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700872}
873
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800874static JdwpError OR_DisableCollection(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700875 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800876 ObjectId object_id = request.ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800877 return Dbg::DisableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700878}
879
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800880static JdwpError OR_EnableCollection(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700881 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800882 ObjectId object_id = request.ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800883 return Dbg::EnableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700884}
885
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800886static JdwpError OR_IsCollected(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700887 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800888 ObjectId object_id = request.ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800889 bool is_collected;
890 JdwpError rc = Dbg::IsCollected(object_id, is_collected);
891 expandBufAdd1(pReply, is_collected ? 1 : 0);
892 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700893}
894
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800895static JdwpError OR_ReferringObjects(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800896 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800897 ObjectId object_id = request.ReadObjectId();
898 int32_t max_count = request.ReadSigned32("max count");
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800899 if (max_count < 0) {
900 return ERR_ILLEGAL_ARGUMENT;
901 }
902
903 std::vector<ObjectId> referring_objects;
904 JdwpError rc = Dbg::GetReferringObjects(object_id, max_count, referring_objects);
905 if (rc != ERR_NONE) {
906 return rc;
907 }
908
909 return WriteTaggedObjectList(reply, referring_objects);
910}
911
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700912/*
913 * Return the string value in a string object.
914 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800915static JdwpError SR_Value(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700916 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800917 ObjectId stringObject = request.ReadObjectId();
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800918 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700919
Elliott Hughesa96836a2013-01-17 12:27:49 -0800920 VLOG(jdwp) << StringPrintf(" --> %s", PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700921
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800922 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700923
924 return ERR_NONE;
925}
926
927/*
928 * Return a thread's name.
929 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800930static JdwpError TR_Name(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700931 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800932 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700933
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800934 std::string name;
Elliott Hughes221229c2013-01-08 18:17:50 -0800935 JdwpError error = Dbg::GetThreadName(thread_id, name);
936 if (error != ERR_NONE) {
937 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700938 }
Elliott Hughes74847412012-06-20 18:10:21 -0700939 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800940 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700941
942 return ERR_NONE;
943}
944
945/*
946 * Suspend the specified thread.
947 *
948 * It's supposed to remain suspended even if interpreted code wants to
949 * resume it; only the JDI is allowed to resume it.
950 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800951static JdwpError TR_Suspend(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700952 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800953 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700954
Elliott Hughes74847412012-06-20 18:10:21 -0700955 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700956 LOG(INFO) << " Warning: ignoring request to suspend self";
957 return ERR_THREAD_NOT_SUSPENDED;
958 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800959
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 Thread* self = Thread::Current();
961 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
962 JdwpError result = Dbg::SuspendThread(thread_id);
963 self->TransitionFromSuspendedToRunnable();
964 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700965}
966
967/*
968 * Resume the specified thread.
969 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800970static JdwpError TR_Resume(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700971 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800972 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700973
Elliott Hughes74847412012-06-20 18:10:21 -0700974 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700975 LOG(INFO) << " Warning: ignoring request to resume self";
976 return ERR_NONE;
977 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800978
Elliott Hughes74847412012-06-20 18:10:21 -0700979 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700980 return ERR_NONE;
981}
982
983/*
984 * Return status of specified thread.
985 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800986static JdwpError TR_Status(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700987 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800988 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700989
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800990 JDWP::JdwpThreadStatus threadStatus;
991 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -0800992 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
993 if (error != ERR_NONE) {
994 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995 }
996
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800997 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700998
999 expandBufAdd4BE(pReply, threadStatus);
1000 expandBufAdd4BE(pReply, suspendStatus);
1001
1002 return ERR_NONE;
1003}
1004
1005/*
1006 * Return the thread group that the specified thread is a member of.
1007 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001008static JdwpError TR_ThreadGroup(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001009 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001010 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001011 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001012}
1013
1014/*
1015 * Return the current call stack of a suspended thread.
1016 *
1017 * If the thread isn't suspended, the error code isn't defined, but should
1018 * be THREAD_NOT_SUSPENDED.
1019 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001020static JdwpError TR_Frames(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001021 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001022 ObjectId thread_id = request.ReadThreadId();
1023 uint32_t start_frame = request.ReadUnsigned32("start frame");
1024 uint32_t length = request.ReadUnsigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001025
Elliott Hughes221229c2013-01-08 18:17:50 -08001026 size_t actual_frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001027 JdwpError error = Dbg::GetThreadFrameCount(thread_id, actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001028 if (error != ERR_NONE) {
1029 return error;
1030 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001032 if (actual_frame_count <= 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001033 return ERR_THREAD_NOT_SUSPENDED; // 0 means no managed frames (which means "in native").
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001034 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001035
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001036 if (start_frame > actual_frame_count) {
1037 return ERR_INVALID_INDEX;
1038 }
1039 if (length == static_cast<uint32_t>(-1)) {
1040 length = actual_frame_count - start_frame;
1041 }
1042 if (start_frame + length > actual_frame_count) {
1043 return ERR_INVALID_LENGTH;
1044 }
1045
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001046 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001047}
1048
1049/*
1050 * Returns the #of frames on the specified thread, which must be suspended.
1051 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001052static JdwpError TR_FrameCount(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001053 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001054 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001055
Elliott Hughes221229c2013-01-08 18:17:50 -08001056 size_t frame_count;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001057 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, frame_count);
1058 if (rc != ERR_NONE) {
1059 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001060 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001061 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001062
1063 return ERR_NONE;
1064}
1065
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001066static JdwpError TR_OwnedMonitors(Request& request, ExpandBuf* reply, bool with_stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001067 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001068 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001069
1070 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001071 std::vector<uint32_t> stack_depths;
1072 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, monitors, stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001073 if (rc != ERR_NONE) {
1074 return rc;
1075 }
1076
1077 expandBufAdd4BE(reply, monitors.size());
1078 for (size_t i = 0; i < monitors.size(); ++i) {
1079 rc = WriteTaggedObject(reply, monitors[i]);
1080 if (rc != ERR_NONE) {
1081 return rc;
1082 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001083 if (with_stack_depths) {
1084 expandBufAdd4BE(reply, stack_depths[i]);
1085 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001086 }
1087 return ERR_NONE;
1088}
1089
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001090static JdwpError TR_OwnedMonitors(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001091 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001092 return TR_OwnedMonitors(request, reply, false);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001093}
1094
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001095static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001097 return TR_OwnedMonitors(request, reply, true);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001098}
1099
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001100static JdwpError TR_CurrentContendedMonitor(JdwpState*, Request& request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001102 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001103
Elliott Hughesf9501702013-01-11 11:22:27 -08001104 ObjectId contended_monitor;
1105 JdwpError rc = Dbg::GetContendedMonitor(thread_id, contended_monitor);
1106 if (rc != ERR_NONE) {
1107 return rc;
1108 }
1109 return WriteTaggedObject(reply, contended_monitor);
1110}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001111
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001112static JdwpError TR_Interrupt(JdwpState*, Request& request, ExpandBuf* reply)
Elliott Hughesf9501702013-01-11 11:22:27 -08001113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001114 ObjectId thread_id = request.ReadThreadId();
Elliott Hughesf9501702013-01-11 11:22:27 -08001115 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001116}
1117
1118/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001120 *
1121 * (The thread *might* still be running -- it might not have examined
1122 * its suspend count recently.)
1123 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001124static JdwpError TR_DebugSuspendCount(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001126 ObjectId thread_id = request.ReadThreadId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001127 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001128}
1129
1130/*
1131 * Return the name of a thread group.
1132 *
1133 * The Eclipse debugger recognizes "main" and "system" as special.
1134 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001135static JdwpError TGR_Name(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001136 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001137 ObjectId thread_group_id = request.ReadThreadGroupId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001138
Elliott Hughescaf76542012-06-28 16:08:22 -07001139 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001140
1141 return ERR_NONE;
1142}
1143
1144/*
1145 * Returns the thread group -- if any -- that contains the specified
1146 * thread group.
1147 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001148static JdwpError TGR_Parent(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001150 ObjectId thread_group_id = request.ReadThreadGroupId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001151
Elliott Hughescaf76542012-06-28 16:08:22 -07001152 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001153 expandBufAddObjectId(pReply, parentGroup);
1154
1155 return ERR_NONE;
1156}
1157
1158/*
1159 * Return the active threads and thread groups that are part of the
1160 * specified thread group.
1161 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001162static JdwpError TGR_Children(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001164 ObjectId thread_group_id = request.ReadThreadGroupId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001165
Elliott Hughescaf76542012-06-28 16:08:22 -07001166 std::vector<ObjectId> thread_ids;
1167 Dbg::GetThreads(thread_group_id, thread_ids);
1168 expandBufAdd4BE(pReply, thread_ids.size());
1169 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1170 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001171 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001172
Elliott Hughescaf76542012-06-28 16:08:22 -07001173 std::vector<ObjectId> child_thread_groups_ids;
1174 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1175 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1176 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1177 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001178 }
1179
1180 return ERR_NONE;
1181}
1182
1183/*
1184 * Return the #of components in the array.
1185 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001186static JdwpError AR_Length(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001188 ObjectId array_id = request.ReadArrayId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001189
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001190 int length;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001191 JdwpError status = Dbg::GetArrayLength(array_id, length);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001192 if (status != ERR_NONE) {
1193 return status;
1194 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001195 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001196
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001197 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001198
1199 return ERR_NONE;
1200}
1201
1202/*
1203 * Return the values from an array.
1204 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001205static JdwpError AR_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001206 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001207 ObjectId array_id = request.ReadArrayId();
1208 uint32_t offset = request.ReadUnsigned32("offset");
1209 uint32_t length = request.ReadUnsigned32("length");
Elliott Hughesa96836a2013-01-17 12:27:49 -08001210 return Dbg::OutputArray(array_id, offset, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001211}
1212
1213/*
1214 * Set values in an array.
1215 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001216static JdwpError AR_SetValues(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001218 ObjectId array_id = request.ReadArrayId();
1219 uint32_t offset = request.ReadUnsigned32("offset");
1220 uint32_t count = request.ReadUnsigned32("count");
1221 return Dbg::SetArrayElements(array_id, offset, count, request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001222}
1223
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001224static JdwpError CLR_VisibleClasses(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001226 request.ReadObjectId(); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001227 // TODO: we should only return classes which have the given class loader as a defining or
1228 // initiating loader. The former would be easy; the latter is hard, because we don't have
1229 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001230 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001231}
1232
1233/*
1234 * Set an event trigger.
1235 *
1236 * Reply with a requestID.
1237 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001238static JdwpError ER_Set(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001240 JdwpEventKind event_kind = request.ReadEnum1<JdwpEventKind>("event kind");
1241 JdwpSuspendPolicy suspend_policy = request.ReadEnum1<JdwpSuspendPolicy>("suspend policy");
1242 int32_t modifier_count = request.ReadSigned32("modifier count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001243
Elliott Hughesa96836a2013-01-17 12:27:49 -08001244 CHECK_LT(modifier_count, 256); /* reasonableness check */
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001245
Elliott Hughesa96836a2013-01-17 12:27:49 -08001246 JdwpEvent* pEvent = EventAlloc(modifier_count);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001247 pEvent->eventKind = event_kind;
1248 pEvent->suspend_policy = suspend_policy;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001249 pEvent->modCount = modifier_count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001250
1251 /*
1252 * Read modifiers. Ordering may be significant (see explanation of Count
1253 * mods in JDWP doc).
1254 */
Elliott Hughesa96836a2013-01-17 12:27:49 -08001255 for (int32_t i = 0; i < modifier_count; ++i) {
Elliott Hughes972a47b2012-02-21 18:16:06 -08001256 JdwpEventMod& mod = pEvent->mods[i];
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001257 mod.modKind = request.ReadModKind();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001258 switch (mod.modKind) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001259 case MK_COUNT:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001260 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001261 // Report once, when "--count" reaches 0.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001262 uint32_t count = request.ReadUnsigned32("count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001263 if (count == 0) {
1264 return ERR_INVALID_COUNT;
1265 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001266 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001267 }
1268 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001269 case MK_CONDITIONAL:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001270 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001271 // Conditional on expression.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001272 uint32_t exprId = request.ReadUnsigned32("expr id");
Elliott Hughes972a47b2012-02-21 18:16:06 -08001273 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001274 }
1275 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001276 case MK_THREAD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001277 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001278 // Only report events in specified thread.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001279 ObjectId thread_id = request.ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001280 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001281 }
1282 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001283 case MK_CLASS_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001284 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001285 // For ClassPrepare, MethodEntry.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001286 RefTypeId class_id = request.ReadRefTypeId();
Elliott Hughes74847412012-06-20 18:10:21 -07001287 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001288 }
1289 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001290 case MK_CLASS_MATCH:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001291 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001292 // Restrict events to matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001293 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001294 std::string pattern(request.ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001295 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001296 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001297 }
1298 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001299 case MK_CLASS_EXCLUDE:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001300 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001301 // Restrict events to non-matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001302 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001303 std::string pattern(request.ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001304 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001305 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001306 }
1307 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001308 case MK_LOCATION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001309 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001310 // Restrict certain events based on location.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001311 JdwpLocation location = request.ReadLocation();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001312 mod.locationOnly.loc = location;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001313 }
1314 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001315 case MK_EXCEPTION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001316 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001317 // Modifies EK_EXCEPTION events,
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001318 mod.exceptionOnly.refTypeId = request.ReadRefTypeId(); // null => all exceptions.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001319 mod.exceptionOnly.caught = request.ReadEnum1<uint8_t>("caught");
1320 mod.exceptionOnly.uncaught = request.ReadEnum1<uint8_t>("uncaught");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001321 }
1322 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001323 case MK_FIELD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001324 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001325 // For field access/modification events.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001326 RefTypeId declaring = request.ReadRefTypeId();
1327 FieldId fieldId = request.ReadFieldId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001328 mod.fieldOnly.refTypeId = declaring;
1329 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001330 }
1331 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001332 case MK_STEP:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001333 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001334 // For use with EK_SINGLE_STEP.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001335 ObjectId thread_id = request.ReadThreadId();
1336 uint32_t size = request.ReadUnsigned32("step size");
1337 uint32_t depth = request.ReadUnsigned32("step depth");
Elliott Hughes74847412012-06-20 18:10:21 -07001338 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001339 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1340
Elliott Hughes74847412012-06-20 18:10:21 -07001341 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001342 mod.step.size = size;
1343 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001344 }
1345 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001346 case MK_INSTANCE_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001347 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001348 // Report events related to a specific object.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001349 ObjectId instance = request.ReadObjectId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001350 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001351 }
1352 break;
1353 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001354 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001355 break;
1356 }
1357 }
1358
1359 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001360 * We reply with an integer "requestID".
1361 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001362 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001363 expandBufAdd4BE(pReply, requestId);
1364
1365 pEvent->requestId = requestId;
1366
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001367 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001368
1369 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001370 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001371 if (err != ERR_NONE) {
1372 /* registration failed, probably because event is bogus */
1373 EventFree(pEvent);
1374 LOG(WARNING) << "WARNING: event request rejected";
1375 }
1376 return err;
1377}
1378
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001379static JdwpError ER_Clear(JdwpState* state, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001380 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001381 request.ReadEnum1<JdwpEventKind>("event kind");
1382 uint32_t requestId = request.ReadUnsigned32("request id");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001383
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001384 // Failure to find an event with a matching ID is a no-op
1385 // and does not return an error.
Elliott Hughes761928d2011-11-16 18:33:03 -08001386 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001387 return ERR_NONE;
1388}
1389
1390/*
1391 * Return the values of arguments and local variables.
1392 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001393static JdwpError SF_GetValues(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001394 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001395 ObjectId thread_id = request.ReadThreadId();
1396 FrameId frame_id = request.ReadFrameId();
1397 int32_t slot_count = request.ReadSigned32("slot count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001398
Elliott Hughesa96836a2013-01-17 12:27:49 -08001399 expandBufAdd4BE(pReply, slot_count); /* "int values" */
1400 for (int32_t i = 0; i < slot_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001401 uint32_t slot = request.ReadUnsigned32("slot");
1402 JDWP::JdwpTag reqSigByte = request.ReadTag();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001403
Elliott Hughes2435a572012-02-17 16:07:41 -08001404 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001405
Elliott Hughesdbb40792011-11-18 17:05:22 -08001406 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001407 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001408 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001409 }
1410
1411 return ERR_NONE;
1412}
1413
1414/*
1415 * Set the values of arguments and local variables.
1416 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001417static JdwpError SF_SetValues(JdwpState*, Request& request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001418 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001419 ObjectId thread_id = request.ReadThreadId();
1420 FrameId frame_id = request.ReadFrameId();
1421 int32_t slot_count = request.ReadSigned32("slot count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001422
Elliott Hughesa96836a2013-01-17 12:27:49 -08001423 for (int32_t i = 0; i < slot_count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001424 uint32_t slot = request.ReadUnsigned32("slot");
1425 JDWP::JdwpTag sigByte = request.ReadTag();
Elliott Hughesdbb40792011-11-18 17:05:22 -08001426 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001427 uint64_t value = request.ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001428
Elliott Hughes2435a572012-02-17 16:07:41 -08001429 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001430 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001431 }
1432
1433 return ERR_NONE;
1434}
1435
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001436static JdwpError SF_ThisObject(JdwpState*, Request& request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001437 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001438 ObjectId thread_id = request.ReadThreadId();
1439 FrameId frame_id = request.ReadFrameId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001440
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001441 ObjectId object_id;
1442 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001443 if (rc != ERR_NONE) {
1444 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001445 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001446
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001447 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001448}
1449
1450/*
1451 * Return the reference type reflected by this class object.
1452 *
1453 * This appears to be required because ReferenceTypeId values are NEVER
1454 * reused, whereas ClassIds can be recycled like any other object. (Either
1455 * that, or I have no idea what this is for.)
1456 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001457static JdwpError COR_ReflectedType(JdwpState*, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001458 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001459 RefTypeId class_object_id = request.ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001460 return Dbg::GetReflectedType(class_object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001461}
1462
1463/*
1464 * Handle a DDM packet with a single chunk in it.
1465 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001466static JdwpError DDM_Chunk(JdwpState* state, Request& request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001467 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001468 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001469 uint8_t* replyBuf = NULL;
1470 int replyLen = -1;
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001471 if (Dbg::DdmHandlePacket(request, &replyBuf, &replyLen)) {
1472 // If they want to send something back, we copy it into the buffer.
1473 // TODO: consider altering the JDWP stuff to hold the packet header
1474 // in a separate buffer. That would allow us to writev() DDM traffic
1475 // instead of copying it into the expanding buffer. The reduction in
1476 // heap requirements is probably more valuable than the efficiency.
1477 CHECK_GT(replyLen, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001478 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1479 free(replyBuf);
1480 }
1481 return ERR_NONE;
1482}
1483
1484/*
1485 * Handler map decl.
1486 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001487typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, Request& request, ExpandBuf* reply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001488
1489struct JdwpHandlerMap {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001490 uint8_t cmdSet;
1491 uint8_t cmd;
1492 JdwpRequestHandler func;
1493 const char* name;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001494};
1495
1496/*
1497 * Map commands to functions.
1498 *
1499 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1500 * and 128-256 are vendor-defined.
1501 */
Elliott Hughescb693062013-02-21 09:48:08 -08001502static const JdwpHandlerMap gHandlers[] = {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001503 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001504 { 1, 1, VM_Version, "VirtualMachine.Version" },
1505 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1506 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1507 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1508 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1509 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1510 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1511 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1512 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1513 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1514 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1515 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1516 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1517 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1518 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1519 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
1520 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1521 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1522 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001523 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001524 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001525
1526 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001527 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1528 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1529 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1530 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1531 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1532 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1533 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
1534 { 2, 8, NULL, "ReferenceType.NestedTypes" },
1535 { 2, 9, RT_Status, "ReferenceType.Status" },
1536 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1537 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001538 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1539 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001540 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1541 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughes3b78c942013-01-15 17:35:41 -08001542 { 2, 16, RT_Instances, "ReferenceType.Instances" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001543 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1544 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001545
1546 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001547 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1548 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1549 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1550 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001551
1552 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001553 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001554
1555 /* InterfaceType command set (5) */
1556
1557 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001558 { 6, 1, M_LineTable, "Method.LineTable" },
1559 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughes9777ba22013-01-17 09:04:19 -08001560 { 6, 3, M_Bytecodes, "Method.Bytecodes" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001561 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001562 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001563
1564 /* Field command set (8) */
1565
1566 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001567 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1568 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1569 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
1570 { 9, 4, NULL, "ObjectReference.UNUSED" },
1571 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1572 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001573 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001574 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1575 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001576 { 9, 10, OR_ReferringObjects, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001577
1578 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001579 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001580
1581 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001582 { 11, 1, TR_Name, "ThreadReference.Name" },
1583 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1584 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1585 { 11, 4, TR_Status, "ThreadReference.Status" },
1586 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1587 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1588 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1589 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1590 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1591 { 11, 10, NULL, "ThreadReference.Stop" },
1592 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1593 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1594 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1595 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001596
1597 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001598 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1599 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1600 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001601
1602 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001603 { 13, 1, AR_Length, "ArrayReference.Length" },
1604 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1605 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001606
1607 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001608 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001609
1610 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001611 { 15, 1, ER_Set, "EventRequest.Set" },
1612 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001613 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001614
1615 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001616 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1617 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1618 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001619 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001620
1621 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001622 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001623
1624 /* Event command set (64) */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001625 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001626
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001627 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001628};
1629
Elliott Hughescb693062013-02-21 09:48:08 -08001630static const char* GetCommandName(Request& request) {
1631 for (size_t i = 0; i < arraysize(gHandlers); ++i) {
1632 if (gHandlers[i].cmdSet == request.GetCommandSet() && gHandlers[i].cmd == request.GetCommand()) {
1633 return gHandlers[i].name;
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001634 }
1635 }
1636 return "?UNKNOWN?";
1637}
1638
Elliott Hughescb693062013-02-21 09:48:08 -08001639static std::string DescribeCommand(Request& request) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001640 std::string result;
Elliott Hughes64f574f2013-02-20 14:57:12 -08001641 result += "REQUEST: ";
Elliott Hughescb693062013-02-21 09:48:08 -08001642 result += GetCommandName(request);
1643 result += StringPrintf(" (length=%d id=0x%06x)", request.GetLength(), request.GetId());
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001644 return result;
1645}
1646
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001647/*
1648 * Process a request from the debugger.
1649 *
1650 * On entry, the JDWP thread is in VMWAIT.
1651 */
Elliott Hughescb693062013-02-21 09:48:08 -08001652void JdwpState::ProcessRequest(Request& request, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001653 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001654
Elliott Hughescb693062013-02-21 09:48:08 -08001655 if (request.GetCommandSet() != kJDWPDdmCmdSet) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001656 /*
1657 * Activity from a debugger, not merely ddms. Mark us as having an
1658 * active debugger session, and zero out the last-activity timestamp
1659 * so waitForDebugger() doesn't return if we stall for a bit here.
1660 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001661 Dbg::GoActive();
Ian Rogers9adbff52013-01-23 18:19:03 -08001662 QuasiAtomic::Write64(&last_activity_time_ms_, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001663 }
1664
1665 /*
1666 * If a debugger event has fired in another thread, wait until the
1667 * initiating thread has suspended itself before processing messages
1668 * from the debugger. Otherwise we (the JDWP thread) could be told to
1669 * resume the thread before it has suspended.
1670 *
1671 * We call with an argument of zero to wait for the current event
1672 * thread to finish, and then clear the block. Depending on the thread
1673 * suspend policy, this may allow events in other threads to fire,
1674 * but those events have no bearing on what the debugger has sent us
1675 * in the current request.
1676 *
1677 * Note that we MUST clear the event token before waking the event
1678 * thread up, or risk waiting for the thread to suspend after we've
1679 * told it to resume.
1680 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001681 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001682
1683 /*
1684 * Tell the VM that we're running and shouldn't be interrupted by GC.
1685 * Do this after anything that can stall indefinitely.
1686 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 Thread* self = Thread::Current();
1688 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001689
1690 expandBufAddSpace(pReply, kJDWPHeaderLen);
1691
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001692 size_t i;
Elliott Hughescb693062013-02-21 09:48:08 -08001693 for (i = 0; i < arraysize(gHandlers); ++i) {
1694 if (gHandlers[i].cmdSet == request.GetCommandSet() && gHandlers[i].cmd == request.GetCommand() && gHandlers[i].func != NULL) {
1695 VLOG(jdwp) << DescribeCommand(request);
1696 result = (*gHandlers[i].func)(this, request, pReply);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001697 if (result == ERR_NONE) {
1698 request.CheckConsumed();
1699 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001700 break;
1701 }
1702 }
Elliott Hughescb693062013-02-21 09:48:08 -08001703 if (i == arraysize(gHandlers)) {
1704 LOG(ERROR) << "Command not implemented: " << DescribeCommand(request);
1705 LOG(ERROR) << HexDump(request.data(), request.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001706 result = ERR_NOT_IMPLEMENTED;
1707 }
1708
1709 /*
1710 * Set up the reply header.
1711 *
1712 * If we encountered an error, only send the header back.
1713 */
1714 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughescb693062013-02-21 09:48:08 -08001715 Set4BE(replyBuf + 4, request.GetId());
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001716 Set1(replyBuf + 8, kJDWPFlagReply);
1717 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001718 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001719 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001720 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001721 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001722 }
1723
Elliott Hughescb693062013-02-21 09:48:08 -08001724 CHECK_GT(expandBufGetLength(pReply), 0U) << GetCommandName(request) << " " << request.GetId();
1725
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001726 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughescb693062013-02-21 09:48:08 -08001727 VLOG(jdwp) << "REPLY: " << GetCommandName(request) << " " << result << " (length=" << respLen << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001728 if (false) {
Elliott Hughes64f574f2013-02-20 14:57:12 -08001729 VLOG(jdwp) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001730 }
1731
Elliott Hughescb693062013-02-21 09:48:08 -08001732 VLOG(jdwp) << "----------";
1733
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001734 /*
1735 * Update last-activity timestamp. We really only need this during
1736 * the initial setup. Only update if this is a non-DDMS packet.
1737 */
Elliott Hughescb693062013-02-21 09:48:08 -08001738 if (request.GetCommandSet() != kJDWPDdmCmdSet) {
Ian Rogers9adbff52013-01-23 18:19:03 -08001739 QuasiAtomic::Write64(&last_activity_time_ms_, MilliTime());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001740 }
1741
1742 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001743 self->TransitionFromRunnableToSuspended(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001744}
1745
1746} // namespace JDWP
1747
1748} // namespace art