blob: 8560cb5bc8c400aef9fb5bc488f758998cf60d63 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes872d4ec2011-10-21 17:07:15 -070017#include <stdlib.h>
18#include <string.h>
19#include <unistd.h>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Elliott Hughesa96836a2013-01-17 12:27:49 -080021#include <string>
22
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "atomic.h"
Ian Rogers43b2e0f2014-01-30 16:58:39 -080024#include "base/hex_dump.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
26#include "base/macros.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080027#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "debugger.h"
29#include "jdwp/jdwp_constants.h"
30#include "jdwp/jdwp_event.h"
31#include "jdwp/jdwp_expand_buf.h"
32#include "jdwp/jdwp_priv.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "runtime.h"
Ian Rogers693ff612013-02-01 10:56:12 -080034#include "thread-inl.h"
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);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -080051 return StringPrintf("%#" PRIx64 " (%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;
Ian Rogersc0542af2014-09-03 16:16:56 -070068 JdwpError rc = Dbg::GetObjectTag(object_id, &tag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -080069 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 */
Ian Rogersc0542af2014-09-03 16:16:56 -070094static 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
Ian Rogersc0542af2014-09-03 16:16:56 -0700100 int32_t arg_count = request->ReadSigned32("argument count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700101
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800102 VLOG(jdwp) << StringPrintf(" --> thread_id=%#" PRIx64 " object_id=%#" PRIx64,
103 thread_id, object_id);
104 VLOG(jdwp) << StringPrintf(" class_id=%#" PRIx64 " method_id=%x %s.%s", class_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 method_id, Dbg::GetClassName(class_id).c_str(),
Elliott Hughesa96836a2013-01-17 12:27:49 -0800106 Dbg::GetMethodName(method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800107 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108
Ian Rogers700a4022014-05-19 16:49:03 -0700109 std::unique_ptr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
110 std::unique_ptr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800111 for (int32_t i = 0; i < arg_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700112 argTypes[i] = request->ReadTag();
Elliott Hughes45651fd2012-02-21 15:48:20 -0800113 size_t width = Dbg::GetTagWidth(argTypes[i]);
Ian Rogersc0542af2014-09-03 16:16:56 -0700114 argValues[i] = request->ReadValue(width);
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800115 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#" PRIx64, width,
116 argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700117 }
118
Ian Rogersc0542af2014-09-03 16:16:56 -0700119 uint32_t options = request->ReadUnsigned32("InvokeOptions bit flags");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
121 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
122 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123
Elliott Hughes45651fd2012-02-21 15:48:20 -0800124 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125 uint64_t resultValue;
126 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700127 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 -0700128 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800129 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130 }
131
132 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800133 if (is_constructor) {
134 // If we invoked a constructor (which actually returns void), return the receiver,
135 // unless we threw, in which case we return NULL.
136 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700137 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800138 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700139
Elliott Hughes45651fd2012-02-21 15:48:20 -0800140 size_t width = Dbg::GetTagWidth(resultTag);
141 expandBufAdd1(pReply, resultTag);
142 if (width != 0) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800143 WriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700144 }
145 expandBufAdd1(pReply, JT_OBJECT);
146 expandBufAddObjectId(pReply, exceptObjId);
147
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800148 VLOG(jdwp) << " --> returned " << resultTag
149 << StringPrintf(" %#" PRIx64 " (except=%#" PRIx64 ")", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700150
151 /* show detailed debug output */
152 if (resultTag == JT_STRING && exceptObjId == 0) {
153 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800154 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700155 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800156 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700157 }
158 }
159 }
160
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700161 return err;
162}
163
Ian Rogersc0542af2014-09-03 16:16:56 -0700164static JdwpError VM_Version(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes927bd292013-01-17 10:40:13 -0800166 // Text information on runtime version.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700167 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800168 expandBufAddUtf8String(pReply, version);
Elliott Hughes927bd292013-01-17 10:40:13 -0800169
170 // JDWP version numbers, major and minor.
171 expandBufAdd4BE(pReply, 1);
172 expandBufAdd4BE(pReply, 6);
173
174 // "java.version".
175 expandBufAddUtf8String(pReply, "1.6.0");
176
177 // "java.vm.name".
178 expandBufAddUtf8String(pReply, "Dalvik");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179
180 return ERR_NONE;
181}
182
183/*
184 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
185 * referenceTypeID. We need to send back more than one if the class has
186 * been loaded by multiple class loaders.
187 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700188static JdwpError VM_ClassesBySignature(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700190 std::string classDescriptor(request->ReadUtf8String());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700191
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800192 std::vector<RefTypeId> ids;
Ian Rogersc0542af2014-09-03 16:16:56 -0700193 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), &ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700194
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800195 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700196
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800197 for (size_t i = 0; i < ids.size(); ++i) {
198 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800199 JDWP::JdwpTypeTag type_tag;
200 uint32_t class_status;
201 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
202 if (status != ERR_NONE) {
203 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800204 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205
Elliott Hughes436e3722012-02-17 20:01:47 -0800206 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800207 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800208 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700209 }
210
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700211 return ERR_NONE;
212}
213
214/*
215 * Handle request for the thread IDs of all running threads.
216 *
217 * We exclude ourselves from the list, because we don't allow ourselves
218 * to be suspended, and that violates some JDWP expectations.
219 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700220static JdwpError VM_AllThreads(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700222 std::vector<ObjectId> thread_ids;
Sebastien Hertza06430c2014-09-15 19:21:30 +0200223 Dbg::GetThreads(nullptr /* all thread groups */, &thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700224
Elliott Hughescaf76542012-06-28 16:08:22 -0700225 expandBufAdd4BE(pReply, thread_ids.size());
226 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
227 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700228 }
229
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700230 return ERR_NONE;
231}
232
233/*
234 * List all thread groups that do not have a parent.
235 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700236static JdwpError VM_TopLevelThreadGroups(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700238 /*
239 * TODO: maintain a list of parentless thread groups in the VM.
240 *
241 * For now, just return "system". Application threads are created
242 * in "main", which is a child of "system".
243 */
244 uint32_t groups = 1;
245 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700246 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
247 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700248
249 return ERR_NONE;
250}
251
252/*
253 * Respond with the sizes of the basic debugger types.
254 *
255 * All IDs are 8 bytes.
256 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700257static JdwpError VM_IDSizes(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700259 expandBufAdd4BE(pReply, sizeof(FieldId));
260 expandBufAdd4BE(pReply, sizeof(MethodId));
261 expandBufAdd4BE(pReply, sizeof(ObjectId));
262 expandBufAdd4BE(pReply, sizeof(RefTypeId));
263 expandBufAdd4BE(pReply, sizeof(FrameId));
264 return ERR_NONE;
265}
266
Ian Rogersc0542af2014-09-03 16:16:56 -0700267static JdwpError VM_Dispose(JdwpState*, Request*, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes86964332012-02-15 19:37:42 -0800269 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700270 return ERR_NONE;
271}
272
273/*
274 * Suspend the execution of the application running in the VM (i.e. suspend
275 * all threads).
276 *
277 * This needs to increment the "suspend count" on all threads.
278 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700279static JdwpError VM_Suspend(JdwpState*, Request*, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700280 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800281 Thread* self = Thread::Current();
282 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700283 Dbg::SuspendVM();
jeffhaoa77f0f62012-12-05 17:19:31 -0800284 self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700285 return ERR_NONE;
286}
287
288/*
289 * Resume execution. Decrements the "suspend count" of all threads.
290 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700291static JdwpError VM_Resume(JdwpState*, Request*, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100293 Dbg::ProcessDelayedFullUndeoptimizations();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700294 Dbg::ResumeVM();
295 return ERR_NONE;
296}
297
Ian Rogersc0542af2014-09-03 16:16:56 -0700298static JdwpError VM_Exit(JdwpState* state, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700299 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700300 uint32_t exit_status = request->ReadUnsigned32("exit_status");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800301 state->ExitAfterReplying(exit_status);
302 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700303}
304
305/*
306 * Create a new string in the VM and return its ID.
307 *
308 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
309 * string "java.util.Arrays".)
310 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700311static JdwpError VM_CreateString(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700312 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700313 std::string str(request->ReadUtf8String());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700314 ObjectId stringId = Dbg::CreateString(str);
315 if (stringId == 0) {
316 return ERR_OUT_OF_MEMORY;
317 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700318 expandBufAddObjectId(pReply, stringId);
319 return ERR_NONE;
320}
321
Ian Rogersc0542af2014-09-03 16:16:56 -0700322static JdwpError VM_ClassPaths(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700323 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800324 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700325
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800326 std::vector<std::string> class_path;
327 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
328 expandBufAdd4BE(pReply, class_path.size());
329 for (size_t i = 0; i < class_path.size(); ++i) {
330 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700331 }
332
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800333 std::vector<std::string> boot_class_path;
334 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
335 expandBufAdd4BE(pReply, boot_class_path.size());
336 for (size_t i = 0; i < boot_class_path.size(); ++i) {
337 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700338 }
339
340 return ERR_NONE;
341}
342
Ian Rogersc0542af2014-09-03 16:16:56 -0700343static JdwpError VM_DisposeObjects(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700345 size_t object_count = request->ReadUnsigned32("object_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800346 for (size_t i = 0; i < object_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700347 ObjectId object_id = request->ReadObjectId();
348 uint32_t reference_count = request->ReadUnsigned32("reference_count");
Elliott Hughes64f574f2013-02-20 14:57:12 -0800349 Dbg::DisposeObject(object_id, reference_count);
350 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700351 return ERR_NONE;
352}
353
Ian Rogersc0542af2014-09-03 16:16:56 -0700354static JdwpError VM_Capabilities(JdwpState*, Request*, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200356 expandBufAdd1(reply, true); // canWatchFieldModification
357 expandBufAdd1(reply, true); // canWatchFieldAccess
Elliott Hughes9777ba22013-01-17 09:04:19 -0800358 expandBufAdd1(reply, true); // canGetBytecodes
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800359 expandBufAdd1(reply, true); // canGetSyntheticAttribute
360 expandBufAdd1(reply, true); // canGetOwnedMonitorInfo
Elliott Hughesf9501702013-01-11 11:22:27 -0800361 expandBufAdd1(reply, true); // canGetCurrentContendedMonitor
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800362 expandBufAdd1(reply, true); // canGetMonitorInfo
363 return ERR_NONE;
364}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700365
Ian Rogersc0542af2014-09-03 16:16:56 -0700366static JdwpError VM_CapabilitiesNew(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800367 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800368 // The first few capabilities are the same as those reported by the older call.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800369 VM_Capabilities(NULL, request, reply);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800370
371 expandBufAdd1(reply, false); // canRedefineClasses
372 expandBufAdd1(reply, false); // canAddMethod
373 expandBufAdd1(reply, false); // canUnrestrictedlyRedefineClasses
374 expandBufAdd1(reply, false); // canPopFrames
Sebastien Hertz5f4e6f52014-04-11 12:07:41 +0200375 expandBufAdd1(reply, true); // canUseInstanceFilters
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800376 expandBufAdd1(reply, false); // canGetSourceDebugExtension
377 expandBufAdd1(reply, false); // canRequestVMDeathEvent
378 expandBufAdd1(reply, false); // canSetDefaultStratum
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800379 expandBufAdd1(reply, true); // 1.6: canGetInstanceInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800380 expandBufAdd1(reply, false); // 1.6: canRequestMonitorEvents
Elliott Hughes734b8c62013-01-11 15:32:45 -0800381 expandBufAdd1(reply, true); // 1.6: canGetMonitorFrameInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800382 expandBufAdd1(reply, false); // 1.6: canUseSourceNameFilters
383 expandBufAdd1(reply, false); // 1.6: canGetConstantPool
384 expandBufAdd1(reply, false); // 1.6: canForceEarlyReturn
385
386 // Fill in reserved22 through reserved32; note count started at 1.
387 for (size_t i = 22; i <= 32; ++i) {
388 expandBufAdd1(reply, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700389 }
390 return ERR_NONE;
391}
392
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700394 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800395 std::vector<JDWP::RefTypeId> classes;
Ian Rogersc0542af2014-09-03 16:16:56 -0700396 Dbg::GetClassList(&classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700397
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800398 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700399
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800400 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800401 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800402 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800403 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800404 uint32_t class_status;
405 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
406 if (status != ERR_NONE) {
407 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800408 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700409
Elliott Hughes436e3722012-02-17 20:01:47 -0800410 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800411 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800412 if (descriptor_and_status) {
413 expandBufAddUtf8String(pReply, descriptor);
414 if (generic) {
415 expandBufAddUtf8String(pReply, genericSignature);
416 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800417 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800418 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700419 }
420
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700421 return ERR_NONE;
422}
423
Ian Rogersc0542af2014-09-03 16:16:56 -0700424static JdwpError VM_AllClasses(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700425 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700426 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800427}
428
Ian Rogersc0542af2014-09-03 16:16:56 -0700429static JdwpError VM_AllClassesWithGeneric(JdwpState*, Request*, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700430 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700431 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800432}
433
Ian Rogersc0542af2014-09-03 16:16:56 -0700434static JdwpError VM_InstanceCounts(JdwpState*, Request* request, ExpandBuf* pReply)
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800435 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700436 int32_t class_count = request->ReadSigned32("class count");
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800437 if (class_count < 0) {
438 return ERR_ILLEGAL_ARGUMENT;
439 }
440 std::vector<RefTypeId> class_ids;
441 for (int32_t i = 0; i < class_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700442 class_ids.push_back(request->ReadRefTypeId());
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800443 }
444
445 std::vector<uint64_t> counts;
Ian Rogersc0542af2014-09-03 16:16:56 -0700446 JdwpError rc = Dbg::GetInstanceCounts(class_ids, &counts);
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800447 if (rc != ERR_NONE) {
448 return rc;
449 }
450
451 expandBufAdd4BE(pReply, counts.size());
452 for (size_t i = 0; i < counts.size(); ++i) {
453 expandBufAdd8BE(pReply, counts[i]);
454 }
455 return ERR_NONE;
456}
457
Ian Rogersc0542af2014-09-03 16:16:56 -0700458static JdwpError RT_Modifiers(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700460 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800461 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700462}
463
464/*
465 * Get values from static fields in a reference type.
466 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700467static JdwpError RT_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700468 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700469 RefTypeId refTypeId = request->ReadRefTypeId();
470 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes0cf74332012-02-23 23:14:00 -0800471 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800472 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700473 FieldId fieldId = request->ReadFieldId();
Elliott Hughes0cf74332012-02-23 23:14:00 -0800474 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800475 if (status != ERR_NONE) {
476 return status;
477 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700478 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700479 return ERR_NONE;
480}
481
482/*
483 * Get the name of the source file in which a reference type was declared.
484 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700485static JdwpError RT_SourceFile(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700486 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700487 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes03181a82011-11-17 17:22:21 -0800488 std::string source_file;
Ian Rogersc0542af2014-09-03 16:16:56 -0700489 JdwpError status = Dbg::GetSourceFile(refTypeId, &source_file);
Elliott Hughes436e3722012-02-17 20:01:47 -0800490 if (status != ERR_NONE) {
491 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700492 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800493 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800494 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700495}
496
497/*
498 * Return the current status of the reference type.
499 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700500static JdwpError RT_Status(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700501 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700502 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800503 JDWP::JdwpTypeTag type_tag;
504 uint32_t class_status;
505 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
506 if (status != ERR_NONE) {
507 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800508 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800509 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700510 return ERR_NONE;
511}
512
513/*
514 * Return interfaces implemented directly by this class.
515 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700516static JdwpError RT_Interfaces(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700517 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700518 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800519 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700520}
521
522/*
523 * Return the class object corresponding to this type.
524 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700525static JdwpError RT_ClassObject(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700526 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700527 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -0800528 ObjectId class_object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700529 JdwpError status = Dbg::GetClassObject(refTypeId, &class_object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800530 if (status != ERR_NONE) {
531 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800532 }
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800533 VLOG(jdwp) << StringPrintf(" --> ObjectId %#" PRIx64, class_object_id);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800534 expandBufAddObjectId(pReply, class_object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700535 return ERR_NONE;
536}
537
538/*
539 * Returns the value of the SourceDebugExtension attribute.
540 *
541 * JDB seems interested, but DEX files don't currently support this.
542 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700543static JdwpError RT_SourceDebugExtension(JdwpState*, Request*, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700544 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700545 /* referenceTypeId in, string out */
546 return ERR_ABSENT_INFORMATION;
547}
548
Ian Rogersc0542af2014-09-03 16:16:56 -0700549static JdwpError RT_Signature(JdwpState*, Request* request, ExpandBuf* pReply, bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700550 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700551 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700552
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800553 std::string signature;
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700554 JdwpError status = Dbg::GetSignature(refTypeId, &signature);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800555 if (status != ERR_NONE) {
556 return status;
557 }
558 expandBufAddUtf8String(pReply, signature);
559 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800560 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700561 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700562 return ERR_NONE;
563}
564
Ian Rogersc0542af2014-09-03 16:16:56 -0700565static JdwpError RT_Signature(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700566 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800567 return RT_Signature(state, request, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800568}
569
Ian Rogersc0542af2014-09-03 16:16:56 -0700570static JdwpError RT_SignatureWithGeneric(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700571 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800572 return RT_Signature(state, request, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800573}
574
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700575/*
576 * Return the instance of java.lang.ClassLoader that loaded the specified
577 * reference type, or null if it was loaded by the system loader.
578 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700579static JdwpError RT_ClassLoader(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700580 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700581 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800582 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700583}
584
585/*
586 * Given a referenceTypeId, return a block of stuff that describes the
587 * fields declared by a class.
588 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700589static JdwpError RT_FieldsWithGeneric(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700590 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700591 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800592 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800593}
594
595// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogersc0542af2014-09-03 16:16:56 -0700596static JdwpError RT_Fields(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700597 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700598 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800599 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700600}
601
602/*
603 * Given a referenceTypeID, return a block of goodies describing the
604 * methods declared by a class.
605 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700606static JdwpError RT_MethodsWithGeneric(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700607 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700608 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800609 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800610}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700611
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800612// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogersc0542af2014-09-03 16:16:56 -0700613static JdwpError RT_Methods(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700614 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700615 RefTypeId refTypeId = request->ReadRefTypeId();
Elliott Hughes436e3722012-02-17 20:01:47 -0800616 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617}
618
Ian Rogersc0542af2014-09-03 16:16:56 -0700619static JdwpError RT_Instances(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes3b78c942013-01-15 17:35:41 -0800620 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700621 RefTypeId class_id = request->ReadRefTypeId();
622 int32_t max_count = request->ReadSigned32("max count");
Elliott Hughes3b78c942013-01-15 17:35:41 -0800623 if (max_count < 0) {
624 return ERR_ILLEGAL_ARGUMENT;
625 }
626
627 std::vector<ObjectId> instances;
Ian Rogersc0542af2014-09-03 16:16:56 -0700628 JdwpError rc = Dbg::GetInstances(class_id, max_count, &instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800629 if (rc != ERR_NONE) {
630 return rc;
631 }
632
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800633 return WriteTaggedObjectList(reply, instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800634}
635
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700636/*
637 * Return the immediate superclass of a class.
638 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700639static JdwpError CT_Superclass(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700640 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700641 RefTypeId class_id = request->ReadRefTypeId();
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800642 RefTypeId superClassId;
Ian Rogersc0542af2014-09-03 16:16:56 -0700643 JdwpError status = Dbg::GetSuperclass(class_id, &superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800644 if (status != ERR_NONE) {
645 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800646 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700647 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700648 return ERR_NONE;
649}
650
651/*
652 * Set static class values.
653 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700654static JdwpError CT_SetValues(JdwpState* , Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700655 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700656 RefTypeId class_id = request->ReadRefTypeId();
657 int32_t values_count = request->ReadSigned32("values count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700658
Elliott Hughesa96836a2013-01-17 12:27:49 -0800659 UNUSED(class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700660
Elliott Hughesa96836a2013-01-17 12:27:49 -0800661 for (int32_t i = 0; i < values_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700662 FieldId fieldId = request->ReadFieldId();
Elliott Hughesaed4be92011-12-02 16:16:23 -0800663 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800664 size_t width = Dbg::GetTagWidth(fieldTag);
Ian Rogersc0542af2014-09-03 16:16:56 -0700665 uint64_t value = request->ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700666
Elliott Hughesa96836a2013-01-17 12:27:49 -0800667 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " --> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800668 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
669 if (status != ERR_NONE) {
670 return status;
671 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700672 }
673
674 return ERR_NONE;
675}
676
677/*
678 * Invoke a static method.
679 *
680 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
681 * values in the "variables" display.
682 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700683static JdwpError CT_InvokeMethod(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700684 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700685 RefTypeId class_id = request->ReadRefTypeId();
686 ObjectId thread_id = request->ReadThreadId();
687 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700688
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800689 return FinishInvoke(state, request, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700690}
691
692/*
693 * Create a new object of the requested type, and invoke the specified
694 * constructor.
695 *
696 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
697 * see the contents of a byte[] as a string.
698 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700699static JdwpError CT_NewInstance(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700700 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700701 RefTypeId class_id = request->ReadRefTypeId();
702 ObjectId thread_id = request->ReadThreadId();
703 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700704
Elliott Hughes74847412012-06-20 18:10:21 -0700705 ObjectId object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700706 JdwpError status = Dbg::CreateObject(class_id, &object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800707 if (status != ERR_NONE) {
708 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800709 }
Elliott Hughes74847412012-06-20 18:10:21 -0700710 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700711 return ERR_OUT_OF_MEMORY;
712 }
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800713 return FinishInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700714}
715
716/*
717 * Create a new array object of the requested type and length.
718 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700719static JdwpError AT_newInstance(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700720 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700721 RefTypeId arrayTypeId = request->ReadRefTypeId();
722 int32_t length = request->ReadSigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700723
Elliott Hughes74847412012-06-20 18:10:21 -0700724 ObjectId object_id;
Ian Rogersc0542af2014-09-03 16:16:56 -0700725 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, &object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800726 if (status != ERR_NONE) {
727 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800728 }
Elliott Hughes74847412012-06-20 18:10:21 -0700729 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700730 return ERR_OUT_OF_MEMORY;
731 }
732 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700733 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700734 return ERR_NONE;
735}
736
737/*
738 * Return line number information for the method, if present.
739 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700740static JdwpError M_LineTable(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700741 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700742 RefTypeId refTypeId = request->ReadRefTypeId();
743 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700744
Elliott Hughes74847412012-06-20 18:10:21 -0700745 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700746
747 return ERR_NONE;
748}
749
Ian Rogersc0542af2014-09-03 16:16:56 -0700750static JdwpError M_VariableTable(JdwpState*, Request* request, ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700751 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700752 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700753 RefTypeId class_id = request->ReadRefTypeId();
754 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700755
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800756 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
757 // information. That will cause Eclipse to make a best-effort attempt at displaying local
758 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
759 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700760 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700761 return ERR_NONE;
762}
763
Ian Rogersc0542af2014-09-03 16:16:56 -0700764static JdwpError M_VariableTable(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700765 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800766 return M_VariableTable(state, request, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800767}
768
Ian Rogersc0542af2014-09-03 16:16:56 -0700769static JdwpError M_VariableTableWithGeneric(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700770 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800771 return M_VariableTable(state, request, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800772}
773
Ian Rogersc0542af2014-09-03 16:16:56 -0700774static JdwpError M_Bytecodes(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes9777ba22013-01-17 09:04:19 -0800775 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700776 RefTypeId class_id = request->ReadRefTypeId();
777 MethodId method_id = request->ReadMethodId();
Elliott Hughes9777ba22013-01-17 09:04:19 -0800778
779 std::vector<uint8_t> bytecodes;
Ian Rogersc0542af2014-09-03 16:16:56 -0700780 JdwpError rc = Dbg::GetBytecodes(class_id, method_id, &bytecodes);
Elliott Hughes9777ba22013-01-17 09:04:19 -0800781 if (rc != ERR_NONE) {
782 return rc;
783 }
784
785 expandBufAdd4BE(reply, bytecodes.size());
786 for (size_t i = 0; i < bytecodes.size(); ++i) {
787 expandBufAdd1(reply, bytecodes[i]);
788 }
789
790 return ERR_NONE;
791}
792
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700793/*
794 * Given an object reference, return the runtime type of the object
795 * (class or array).
796 *
Elliott Hughes74847412012-06-20 18:10:21 -0700797 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700798 * passed in here.
799 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700800static JdwpError OR_ReferenceType(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700801 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700802 ObjectId object_id = request->ReadObjectId();
Elliott Hughes74847412012-06-20 18:10:21 -0700803 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700804}
805
806/*
807 * Get values from the fields of an object.
808 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700809static JdwpError OR_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700810 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700811 ObjectId object_id = request->ReadObjectId();
812 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700813
Elliott Hughes0cf74332012-02-23 23:14:00 -0800814 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800815 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700816 FieldId fieldId = request->ReadFieldId();
Elliott Hughes74847412012-06-20 18:10:21 -0700817 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800818 if (status != ERR_NONE) {
819 return status;
820 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700821 }
822
823 return ERR_NONE;
824}
825
826/*
827 * Set values in the fields of an object.
828 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700829static JdwpError OR_SetValues(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700830 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700831 ObjectId object_id = request->ReadObjectId();
832 int32_t field_count = request->ReadSigned32("field count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700833
Elliott Hughesa96836a2013-01-17 12:27:49 -0800834 for (int32_t i = 0; i < field_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700835 FieldId fieldId = request->ReadFieldId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700836
Elliott Hughesaed4be92011-12-02 16:16:23 -0800837 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800838 size_t width = Dbg::GetTagWidth(fieldTag);
Ian Rogersc0542af2014-09-03 16:16:56 -0700839 uint64_t value = request->ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700840
Elliott Hughes2435a572012-02-17 16:07:41 -0800841 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700842 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800843 if (status != ERR_NONE) {
844 return status;
845 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700846 }
847
848 return ERR_NONE;
849}
850
Ian Rogersc0542af2014-09-03 16:16:56 -0700851static JdwpError OR_MonitorInfo(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughesf327e072013-01-09 16:01:26 -0800852 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700853 ObjectId object_id = request->ReadObjectId();
Elliott Hughesf327e072013-01-09 16:01:26 -0800854 return Dbg::GetMonitorInfo(object_id, reply);
855}
856
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700857/*
858 * Invoke an instance method. The invocation must occur in the specified
859 * thread, which must have been suspended by an event.
860 *
861 * The call is synchronous. All threads in the VM are resumed, unless the
862 * SINGLE_THREADED flag is set.
863 *
864 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
865 * object), it will try to invoke the object's toString() function. This
866 * feature becomes crucial when examining ArrayLists with Eclipse.
867 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700868static JdwpError OR_InvokeMethod(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700869 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700870 ObjectId object_id = request->ReadObjectId();
871 ObjectId thread_id = request->ReadThreadId();
872 RefTypeId class_id = request->ReadRefTypeId();
873 MethodId method_id = request->ReadMethodId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700874
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800875 return FinishInvoke(state, request, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700876}
877
Ian Rogersc0542af2014-09-03 16:16:56 -0700878static JdwpError OR_DisableCollection(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700879 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700880 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800881 return Dbg::DisableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700882}
883
Ian Rogersc0542af2014-09-03 16:16:56 -0700884static JdwpError OR_EnableCollection(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700885 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700886 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800887 return Dbg::EnableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700888}
889
Ian Rogersc0542af2014-09-03 16:16:56 -0700890static JdwpError OR_IsCollected(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700891 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700892 ObjectId object_id = request->ReadObjectId();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800893 bool is_collected;
Ian Rogersc0542af2014-09-03 16:16:56 -0700894 JdwpError rc = Dbg::IsCollected(object_id, &is_collected);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800895 expandBufAdd1(pReply, is_collected ? 1 : 0);
896 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700897}
898
Ian Rogersc0542af2014-09-03 16:16:56 -0700899static JdwpError OR_ReferringObjects(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800900 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700901 ObjectId object_id = request->ReadObjectId();
902 int32_t max_count = request->ReadSigned32("max count");
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800903 if (max_count < 0) {
904 return ERR_ILLEGAL_ARGUMENT;
905 }
906
907 std::vector<ObjectId> referring_objects;
Ian Rogersc0542af2014-09-03 16:16:56 -0700908 JdwpError rc = Dbg::GetReferringObjects(object_id, max_count, &referring_objects);
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800909 if (rc != ERR_NONE) {
910 return rc;
911 }
912
913 return WriteTaggedObjectList(reply, referring_objects);
914}
915
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700916/*
917 * Return the string value in a string object.
918 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700919static JdwpError SR_Value(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700920 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700921 ObjectId stringObject = request->ReadObjectId();
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800922 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700923
Ian Rogers68b56852014-08-29 20:19:11 -0700924 VLOG(jdwp) << StringPrintf(" --> %s", PrintableString(str.c_str()).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700925
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800926 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700927
928 return ERR_NONE;
929}
930
931/*
932 * Return a thread's name.
933 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700934static JdwpError TR_Name(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700935 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700936 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700937
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800938 std::string name;
Ian Rogersc0542af2014-09-03 16:16:56 -0700939 JdwpError error = Dbg::GetThreadName(thread_id, &name);
Elliott Hughes221229c2013-01-08 18:17:50 -0800940 if (error != ERR_NONE) {
941 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700942 }
Ian Rogersd9e4e0c2014-01-23 20:11:40 -0800943 VLOG(jdwp) << StringPrintf(" Name of thread %#" PRIx64 " is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800944 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700945
946 return ERR_NONE;
947}
948
949/*
950 * Suspend the specified thread.
951 *
952 * It's supposed to remain suspended even if interpreted code wants to
953 * resume it; only the JDI is allowed to resume it.
954 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700955static JdwpError TR_Suspend(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700956 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700957 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700958
Elliott Hughes74847412012-06-20 18:10:21 -0700959 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700960 LOG(INFO) << " Warning: ignoring request to suspend self";
961 return ERR_THREAD_NOT_SUSPENDED;
962 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800963
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700964 Thread* self = Thread::Current();
965 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
966 JdwpError result = Dbg::SuspendThread(thread_id);
967 self->TransitionFromSuspendedToRunnable();
968 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700969}
970
971/*
972 * Resume the specified thread.
973 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700974static JdwpError TR_Resume(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700975 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700976 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700977
Elliott Hughes74847412012-06-20 18:10:21 -0700978 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700979 LOG(INFO) << " Warning: ignoring request to resume self";
980 return ERR_NONE;
981 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800982
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100983 Dbg::ProcessDelayedFullUndeoptimizations();
984
Elliott Hughes74847412012-06-20 18:10:21 -0700985 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700986 return ERR_NONE;
987}
988
989/*
990 * Return status of specified thread.
991 */
Ian Rogersc0542af2014-09-03 16:16:56 -0700992static JdwpError TR_Status(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700993 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700994 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700995
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800996 JDWP::JdwpThreadStatus threadStatus;
997 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -0800998 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
999 if (error != ERR_NONE) {
1000 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001001 }
1002
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001003 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001004
1005 expandBufAdd4BE(pReply, threadStatus);
1006 expandBufAdd4BE(pReply, suspendStatus);
1007
1008 return ERR_NONE;
1009}
1010
1011/*
1012 * Return the thread group that the specified thread is a member of.
1013 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001014static JdwpError TR_ThreadGroup(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001015 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001016 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001017 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001018}
1019
1020/*
1021 * Return the current call stack of a suspended thread.
1022 *
1023 * If the thread isn't suspended, the error code isn't defined, but should
1024 * be THREAD_NOT_SUSPENDED.
1025 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001026static JdwpError TR_Frames(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001027 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001028 ObjectId thread_id = request->ReadThreadId();
1029 uint32_t start_frame = request->ReadUnsigned32("start frame");
1030 uint32_t length = request->ReadUnsigned32("length");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031
Elliott Hughes221229c2013-01-08 18:17:50 -08001032 size_t actual_frame_count;
Ian Rogersc0542af2014-09-03 16:16:56 -07001033 JdwpError error = Dbg::GetThreadFrameCount(thread_id, &actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001034 if (error != ERR_NONE) {
1035 return error;
1036 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001037
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001038 if (actual_frame_count <= 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001039 return ERR_THREAD_NOT_SUSPENDED; // 0 means no managed frames (which means "in native").
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001040 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001041
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001042 if (start_frame > actual_frame_count) {
1043 return ERR_INVALID_INDEX;
1044 }
1045 if (length == static_cast<uint32_t>(-1)) {
1046 length = actual_frame_count - start_frame;
1047 }
1048 if (start_frame + length > actual_frame_count) {
1049 return ERR_INVALID_LENGTH;
1050 }
1051
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001052 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001053}
1054
1055/*
1056 * Returns the #of frames on the specified thread, which must be suspended.
1057 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001058static JdwpError TR_FrameCount(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001059 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001060 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001061
Elliott Hughes221229c2013-01-08 18:17:50 -08001062 size_t frame_count;
Ian Rogersc0542af2014-09-03 16:16:56 -07001063 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, &frame_count);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001064 if (rc != ERR_NONE) {
1065 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001066 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001067 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001068
1069 return ERR_NONE;
1070}
1071
Ian Rogersc0542af2014-09-03 16:16:56 -07001072static JdwpError TR_OwnedMonitors(Request* request, ExpandBuf* reply, bool with_stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001074 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001075
1076 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001077 std::vector<uint32_t> stack_depths;
Ian Rogersc0542af2014-09-03 16:16:56 -07001078 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, &monitors, &stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001079 if (rc != ERR_NONE) {
1080 return rc;
1081 }
1082
1083 expandBufAdd4BE(reply, monitors.size());
1084 for (size_t i = 0; i < monitors.size(); ++i) {
1085 rc = WriteTaggedObject(reply, monitors[i]);
1086 if (rc != ERR_NONE) {
1087 return rc;
1088 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001089 if (with_stack_depths) {
1090 expandBufAdd4BE(reply, stack_depths[i]);
1091 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001092 }
1093 return ERR_NONE;
1094}
1095
Ian Rogersc0542af2014-09-03 16:16:56 -07001096static JdwpError TR_OwnedMonitors(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001098 return TR_OwnedMonitors(request, reply, false);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001099}
1100
Ian Rogersc0542af2014-09-03 16:16:56 -07001101static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughes734b8c62013-01-11 15:32:45 -08001102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001103 return TR_OwnedMonitors(request, reply, true);
Elliott Hughes734b8c62013-01-11 15:32:45 -08001104}
1105
Ian Rogersc0542af2014-09-03 16:16:56 -07001106static JdwpError TR_CurrentContendedMonitor(JdwpState*, Request* request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001108 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001109
Elliott Hughesf9501702013-01-11 11:22:27 -08001110 ObjectId contended_monitor;
Ian Rogersc0542af2014-09-03 16:16:56 -07001111 JdwpError rc = Dbg::GetContendedMonitor(thread_id, &contended_monitor);
Elliott Hughesf9501702013-01-11 11:22:27 -08001112 if (rc != ERR_NONE) {
1113 return rc;
1114 }
1115 return WriteTaggedObject(reply, contended_monitor);
1116}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001117
Ian Rogersc0542af2014-09-03 16:16:56 -07001118static JdwpError TR_Interrupt(JdwpState*, Request* request, ExpandBuf* reply)
Elliott Hughesf9501702013-01-11 11:22:27 -08001119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001120 ObjectId thread_id = request->ReadThreadId();
Elliott Hughesf9501702013-01-11 11:22:27 -08001121 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001122}
1123
1124/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001125 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001126 *
1127 * (The thread *might* still be running -- it might not have examined
1128 * its suspend count recently.)
1129 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001130static JdwpError TR_DebugSuspendCount(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001131 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001132 ObjectId thread_id = request->ReadThreadId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001133 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001134}
1135
1136/*
1137 * Return the name of a thread group.
1138 *
1139 * The Eclipse debugger recognizes "main" and "system" as special.
1140 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001141static JdwpError TGR_Name(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001143 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001144 return Dbg::GetThreadGroupName(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001145}
1146
1147/*
1148 * Returns the thread group -- if any -- that contains the specified
1149 * thread group.
1150 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001151static JdwpError TGR_Parent(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001153 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001154 return Dbg::GetThreadGroupParent(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001155}
1156
1157/*
1158 * Return the active threads and thread groups that are part of the
1159 * specified thread group.
1160 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001161static JdwpError TGR_Children(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001163 ObjectId thread_group_id = request->ReadThreadGroupId();
Sebastien Hertza06430c2014-09-15 19:21:30 +02001164 return Dbg::GetThreadGroupChildren(thread_group_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001165}
1166
1167/*
1168 * Return the #of components in the array.
1169 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001170static JdwpError AR_Length(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001172 ObjectId array_id = request->ReadArrayId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001173
Ian Rogersc0542af2014-09-03 16:16:56 -07001174 int32_t length;
1175 JdwpError status = Dbg::GetArrayLength(array_id, &length);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001176 if (status != ERR_NONE) {
1177 return status;
1178 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001179 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001180
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001181 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001182
1183 return ERR_NONE;
1184}
1185
1186/*
1187 * Return the values from an array.
1188 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001189static JdwpError AR_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001191 ObjectId array_id = request->ReadArrayId();
1192 uint32_t offset = request->ReadUnsigned32("offset");
1193 uint32_t length = request->ReadUnsigned32("length");
Elliott Hughesa96836a2013-01-17 12:27:49 -08001194 return Dbg::OutputArray(array_id, offset, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001195}
1196
1197/*
1198 * Set values in an array.
1199 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001200static JdwpError AR_SetValues(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001201 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001202 ObjectId array_id = request->ReadArrayId();
1203 uint32_t offset = request->ReadUnsigned32("offset");
1204 uint32_t count = request->ReadUnsigned32("count");
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001205 return Dbg::SetArrayElements(array_id, offset, count, request);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001206}
1207
Ian Rogersc0542af2014-09-03 16:16:56 -07001208static JdwpError CLR_VisibleClasses(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001210 request->ReadObjectId(); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001211 // TODO: we should only return classes which have the given class loader as a defining or
1212 // initiating loader. The former would be easy; the latter is hard, because we don't have
1213 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001214 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001215}
1216
1217/*
1218 * Set an event trigger.
1219 *
1220 * Reply with a requestID.
1221 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001222static JdwpError ER_Set(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001224 JdwpEventKind event_kind = request->ReadEnum1<JdwpEventKind>("event kind");
1225 JdwpSuspendPolicy suspend_policy = request->ReadEnum1<JdwpSuspendPolicy>("suspend policy");
1226 int32_t modifier_count = request->ReadSigned32("modifier count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001227
Elliott Hughesa96836a2013-01-17 12:27:49 -08001228 CHECK_LT(modifier_count, 256); /* reasonableness check */
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001229
Elliott Hughesa96836a2013-01-17 12:27:49 -08001230 JdwpEvent* pEvent = EventAlloc(modifier_count);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001231 pEvent->eventKind = event_kind;
1232 pEvent->suspend_policy = suspend_policy;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001233 pEvent->modCount = modifier_count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001234
1235 /*
1236 * Read modifiers. Ordering may be significant (see explanation of Count
1237 * mods in JDWP doc).
1238 */
Elliott Hughesa96836a2013-01-17 12:27:49 -08001239 for (int32_t i = 0; i < modifier_count; ++i) {
Elliott Hughes972a47b2012-02-21 18:16:06 -08001240 JdwpEventMod& mod = pEvent->mods[i];
Ian Rogersc0542af2014-09-03 16:16:56 -07001241 mod.modKind = request->ReadModKind();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001242 switch (mod.modKind) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001243 case MK_COUNT:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001244 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001245 // Report once, when "--count" reaches 0.
Ian Rogersc0542af2014-09-03 16:16:56 -07001246 uint32_t count = request->ReadUnsigned32("count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001247 if (count == 0) {
1248 return ERR_INVALID_COUNT;
1249 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001250 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001251 }
1252 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001253 case MK_CONDITIONAL:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001254 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001255 // Conditional on expression.
Ian Rogersc0542af2014-09-03 16:16:56 -07001256 uint32_t exprId = request->ReadUnsigned32("expr id");
Elliott Hughes972a47b2012-02-21 18:16:06 -08001257 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001258 }
1259 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001260 case MK_THREAD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001261 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001262 // Only report events in specified thread.
Ian Rogersc0542af2014-09-03 16:16:56 -07001263 ObjectId thread_id = request->ReadThreadId();
Elliott Hughes74847412012-06-20 18:10:21 -07001264 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001265 }
1266 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001267 case MK_CLASS_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001268 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001269 // For ClassPrepare, MethodEntry.
Ian Rogersc0542af2014-09-03 16:16:56 -07001270 RefTypeId class_id = request->ReadRefTypeId();
Elliott Hughes74847412012-06-20 18:10:21 -07001271 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001272 }
1273 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001274 case MK_CLASS_MATCH:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001275 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001276 // Restrict events to matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001277 // pattern is "java.foo.*", we want "java/foo/*".
Ian Rogersc0542af2014-09-03 16:16:56 -07001278 std::string pattern(request->ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001279 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001280 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001281 }
1282 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001283 case MK_CLASS_EXCLUDE:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001284 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001285 // Restrict events to non-matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001286 // pattern is "java.foo.*", we want "java/foo/*".
Ian Rogersc0542af2014-09-03 16:16:56 -07001287 std::string pattern(request->ReadUtf8String());
Elliott Hughes86964332012-02-15 19:37:42 -08001288 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001289 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001290 }
1291 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001292 case MK_LOCATION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001293 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001294 // Restrict certain events based on location.
Ian Rogersc0542af2014-09-03 16:16:56 -07001295 JdwpLocation location = request->ReadLocation();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001296 mod.locationOnly.loc = location;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001297 }
1298 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001299 case MK_EXCEPTION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001300 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001301 // Modifies EK_EXCEPTION events,
Ian Rogersc0542af2014-09-03 16:16:56 -07001302 mod.exceptionOnly.refTypeId = request->ReadRefTypeId(); // null => all exceptions.
1303 mod.exceptionOnly.caught = request->ReadEnum1<uint8_t>("caught");
1304 mod.exceptionOnly.uncaught = request->ReadEnum1<uint8_t>("uncaught");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001305 }
1306 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001307 case MK_FIELD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001308 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001309 // For field access/modification events.
Ian Rogersc0542af2014-09-03 16:16:56 -07001310 RefTypeId declaring = request->ReadRefTypeId();
1311 FieldId fieldId = request->ReadFieldId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001312 mod.fieldOnly.refTypeId = declaring;
1313 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001314 }
1315 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001316 case MK_STEP:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001317 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001318 // For use with EK_SINGLE_STEP.
Ian Rogersc0542af2014-09-03 16:16:56 -07001319 ObjectId thread_id = request->ReadThreadId();
1320 uint32_t size = request->ReadUnsigned32("step size");
1321 uint32_t depth = request->ReadUnsigned32("step depth");
Ian Rogersd9e4e0c2014-01-23 20:11:40 -08001322 VLOG(jdwp) << StringPrintf(" Step: thread=%#" PRIx64, thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001323 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1324
Elliott Hughes74847412012-06-20 18:10:21 -07001325 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001326 mod.step.size = size;
1327 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001328 }
1329 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001330 case MK_INSTANCE_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001331 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001332 // Report events related to a specific object.
Ian Rogersc0542af2014-09-03 16:16:56 -07001333 ObjectId instance = request->ReadObjectId();
Elliott Hughes972a47b2012-02-21 18:16:06 -08001334 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001335 }
1336 break;
1337 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001338 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001339 break;
1340 }
1341 }
1342
1343 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001344 * We reply with an integer "requestID".
1345 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001346 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001347 expandBufAdd4BE(pReply, requestId);
1348
1349 pEvent->requestId = requestId;
1350
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001351 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001352
1353 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001354 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001355 if (err != ERR_NONE) {
1356 /* registration failed, probably because event is bogus */
1357 EventFree(pEvent);
1358 LOG(WARNING) << "WARNING: event request rejected";
1359 }
1360 return err;
1361}
1362
Ian Rogersc0542af2014-09-03 16:16:56 -07001363static JdwpError ER_Clear(JdwpState* state, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001364 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001365 request->ReadEnum1<JdwpEventKind>("event kind");
1366 uint32_t requestId = request->ReadUnsigned32("request id");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001367
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001368 // Failure to find an event with a matching ID is a no-op
1369 // and does not return an error.
Elliott Hughes761928d2011-11-16 18:33:03 -08001370 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001371 return ERR_NONE;
1372}
1373
1374/*
1375 * Return the values of arguments and local variables.
1376 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001377static JdwpError SF_GetValues(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001378 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001379 ObjectId thread_id = request->ReadThreadId();
1380 FrameId frame_id = request->ReadFrameId();
1381 int32_t slot_count = request->ReadSigned32("slot count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001382
Elliott Hughesa96836a2013-01-17 12:27:49 -08001383 expandBufAdd4BE(pReply, slot_count); /* "int values" */
1384 for (int32_t i = 0; i < slot_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001385 uint32_t slot = request->ReadUnsigned32("slot");
1386 JDWP::JdwpTag reqSigByte = request->ReadTag();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001387
Elliott Hughes2435a572012-02-17 16:07:41 -08001388 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001389
Elliott Hughesdbb40792011-11-18 17:05:22 -08001390 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001391 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001392 JdwpError error = Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
1393 if (error != ERR_NONE) {
1394 return error;
1395 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001396 }
1397
1398 return ERR_NONE;
1399}
1400
1401/*
1402 * Set the values of arguments and local variables.
1403 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001404static JdwpError SF_SetValues(JdwpState*, Request* request, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001405 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001406 ObjectId thread_id = request->ReadThreadId();
1407 FrameId frame_id = request->ReadFrameId();
1408 int32_t slot_count = request->ReadSigned32("slot count");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001409
Elliott Hughesa96836a2013-01-17 12:27:49 -08001410 for (int32_t i = 0; i < slot_count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001411 uint32_t slot = request->ReadUnsigned32("slot");
1412 JDWP::JdwpTag sigByte = request->ReadTag();
Elliott Hughesdbb40792011-11-18 17:05:22 -08001413 size_t width = Dbg::GetTagWidth(sigByte);
Ian Rogersc0542af2014-09-03 16:16:56 -07001414 uint64_t value = request->ReadValue(width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001415
Elliott Hughes2435a572012-02-17 16:07:41 -08001416 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001417 JdwpError error = Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
1418 if (error != ERR_NONE) {
1419 return error;
1420 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001421 }
1422
1423 return ERR_NONE;
1424}
1425
Ian Rogersc0542af2014-09-03 16:16:56 -07001426static JdwpError SF_ThisObject(JdwpState*, Request* request, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001427 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001428 ObjectId thread_id = request->ReadThreadId();
1429 FrameId frame_id = request->ReadFrameId();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001430
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001431 ObjectId object_id;
1432 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001433 if (rc != ERR_NONE) {
1434 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001435 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001436
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001437 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001438}
1439
1440/*
1441 * Return the reference type reflected by this class object.
1442 *
1443 * This appears to be required because ReferenceTypeId values are NEVER
1444 * reused, whereas ClassIds can be recycled like any other object. (Either
1445 * that, or I have no idea what this is for.)
1446 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001447static JdwpError COR_ReflectedType(JdwpState*, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001448 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001449 RefTypeId class_object_id = request->ReadRefTypeId();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001450 return Dbg::GetReflectedType(class_object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001451}
1452
1453/*
1454 * Handle a DDM packet with a single chunk in it.
1455 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001456static JdwpError DDM_Chunk(JdwpState* state, Request* request, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001457 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001458 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001459 uint8_t* replyBuf = NULL;
1460 int replyLen = -1;
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001461 if (Dbg::DdmHandlePacket(request, &replyBuf, &replyLen)) {
1462 // If they want to send something back, we copy it into the buffer.
1463 // TODO: consider altering the JDWP stuff to hold the packet header
1464 // in a separate buffer. That would allow us to writev() DDM traffic
1465 // instead of copying it into the expanding buffer. The reduction in
1466 // heap requirements is probably more valuable than the efficiency.
1467 CHECK_GT(replyLen, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001468 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1469 free(replyBuf);
1470 }
1471 return ERR_NONE;
1472}
1473
1474/*
1475 * Handler map decl.
1476 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001477typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, Request* request, ExpandBuf* reply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001478
1479struct JdwpHandlerMap {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001480 uint8_t cmdSet;
1481 uint8_t cmd;
1482 JdwpRequestHandler func;
1483 const char* name;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001484};
1485
1486/*
1487 * Map commands to functions.
1488 *
1489 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1490 * and 128-256 are vendor-defined.
1491 */
Elliott Hughescb693062013-02-21 09:48:08 -08001492static const JdwpHandlerMap gHandlers[] = {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001493 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001494 { 1, 1, VM_Version, "VirtualMachine.Version" },
1495 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1496 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1497 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1498 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1499 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1500 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1501 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1502 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1503 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1504 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1505 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1506 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1507 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1508 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1509 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
1510 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1511 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1512 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001513 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001514 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001515
1516 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001517 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1518 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1519 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1520 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1521 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1522 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1523 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
1524 { 2, 8, NULL, "ReferenceType.NestedTypes" },
1525 { 2, 9, RT_Status, "ReferenceType.Status" },
1526 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1527 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001528 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1529 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001530 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1531 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughes3b78c942013-01-15 17:35:41 -08001532 { 2, 16, RT_Instances, "ReferenceType.Instances" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001533 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1534 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001535
1536 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001537 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1538 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1539 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1540 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001541
1542 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001543 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001544
1545 /* InterfaceType command set (5) */
1546
1547 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001548 { 6, 1, M_LineTable, "Method.LineTable" },
1549 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughes9777ba22013-01-17 09:04:19 -08001550 { 6, 3, M_Bytecodes, "Method.Bytecodes" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001551 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001552 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001553
1554 /* Field command set (8) */
1555
1556 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001557 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1558 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1559 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
1560 { 9, 4, NULL, "ObjectReference.UNUSED" },
1561 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1562 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001563 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001564 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1565 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001566 { 9, 10, OR_ReferringObjects, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001567
1568 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001569 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001570
1571 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001572 { 11, 1, TR_Name, "ThreadReference.Name" },
1573 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1574 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1575 { 11, 4, TR_Status, "ThreadReference.Status" },
1576 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1577 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1578 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1579 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1580 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1581 { 11, 10, NULL, "ThreadReference.Stop" },
1582 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1583 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1584 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1585 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001586
1587 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001588 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1589 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1590 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001591
1592 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001593 { 13, 1, AR_Length, "ArrayReference.Length" },
1594 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1595 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001596
1597 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001598 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001599
1600 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001601 { 15, 1, ER_Set, "EventRequest.Set" },
1602 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001603 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001604
1605 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001606 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1607 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1608 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001609 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001610
1611 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001612 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001613
1614 /* Event command set (64) */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001615 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001616
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001617 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001618};
1619
Ian Rogersc0542af2014-09-03 16:16:56 -07001620static const char* GetCommandName(Request* request) {
Elliott Hughescb693062013-02-21 09:48:08 -08001621 for (size_t i = 0; i < arraysize(gHandlers); ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001622 if (gHandlers[i].cmdSet == request->GetCommandSet() && gHandlers[i].cmd == request->GetCommand()) {
Elliott Hughescb693062013-02-21 09:48:08 -08001623 return gHandlers[i].name;
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001624 }
1625 }
1626 return "?UNKNOWN?";
1627}
1628
Ian Rogersc0542af2014-09-03 16:16:56 -07001629static std::string DescribeCommand(Request* request) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001630 std::string result;
Elliott Hughes64f574f2013-02-20 14:57:12 -08001631 result += "REQUEST: ";
Elliott Hughescb693062013-02-21 09:48:08 -08001632 result += GetCommandName(request);
Ian Rogersc0542af2014-09-03 16:16:56 -07001633 result += StringPrintf(" (length=%zu id=0x%06x)", request->GetLength(), request->GetId());
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001634 return result;
1635}
1636
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001637/*
1638 * Process a request from the debugger.
1639 *
1640 * On entry, the JDWP thread is in VMWAIT.
1641 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001642size_t JdwpState::ProcessRequest(Request* request, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001643 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001644
Ian Rogersc0542af2014-09-03 16:16:56 -07001645 if (request->GetCommandSet() != kJDWPDdmCmdSet) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001646 /*
1647 * Activity from a debugger, not merely ddms. Mark us as having an
1648 * active debugger session, and zero out the last-activity timestamp
1649 * so waitForDebugger() doesn't return if we stall for a bit here.
1650 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001651 Dbg::GoActive();
Ian Rogers37f3c962014-07-17 11:25:30 -07001652 last_activity_time_ms_.StoreSequentiallyConsistent(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001653 }
1654
1655 /*
1656 * If a debugger event has fired in another thread, wait until the
1657 * initiating thread has suspended itself before processing messages
1658 * from the debugger. Otherwise we (the JDWP thread) could be told to
1659 * resume the thread before it has suspended.
1660 *
1661 * We call with an argument of zero to wait for the current event
1662 * thread to finish, and then clear the block. Depending on the thread
1663 * suspend policy, this may allow events in other threads to fire,
1664 * but those events have no bearing on what the debugger has sent us
Ian Rogersc0542af2014-09-03 16:16:56 -07001665 * in the current request->
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001666 *
1667 * Note that we MUST clear the event token before waking the event
1668 * thread up, or risk waiting for the thread to suspend after we've
1669 * told it to resume.
1670 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001671 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001672
1673 /*
Ian Rogersc0542af2014-09-03 16:16:56 -07001674 * We do not want events to be sent while we process a request-> Indicate the JDWP thread starts
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001675 * to process a request so other threads wait for it to finish before sending an event.
1676 */
1677 StartProcessingRequest();
1678
1679 /*
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001680 * Tell the VM that we're running and shouldn't be interrupted by GC.
1681 * Do this after anything that can stall indefinitely.
1682 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001683 Thread* self = Thread::Current();
1684 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001685
1686 expandBufAddSpace(pReply, kJDWPHeaderLen);
1687
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001688 size_t i;
Elliott Hughescb693062013-02-21 09:48:08 -08001689 for (i = 0; i < arraysize(gHandlers); ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001690 if (gHandlers[i].cmdSet == request->GetCommandSet() && gHandlers[i].cmd == request->GetCommand() && gHandlers[i].func != NULL) {
Elliott Hughescb693062013-02-21 09:48:08 -08001691 VLOG(jdwp) << DescribeCommand(request);
1692 result = (*gHandlers[i].func)(this, request, pReply);
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001693 if (result == ERR_NONE) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001694 request->CheckConsumed();
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001695 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001696 break;
1697 }
1698 }
Elliott Hughescb693062013-02-21 09:48:08 -08001699 if (i == arraysize(gHandlers)) {
1700 LOG(ERROR) << "Command not implemented: " << DescribeCommand(request);
Ian Rogersc0542af2014-09-03 16:16:56 -07001701 LOG(ERROR) << HexDump(request->data(), request->size(), false, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001702 result = ERR_NOT_IMPLEMENTED;
1703 }
1704
1705 /*
1706 * Set up the reply header.
1707 *
1708 * If we encountered an error, only send the header back.
1709 */
1710 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Sebastien Hertz43c8d722014-03-18 12:19:52 +01001711 size_t replyLength = (result == ERR_NONE) ? expandBufGetLength(pReply) : kJDWPHeaderLen;
1712 Set4BE(replyBuf + 0, replyLength);
Ian Rogersc0542af2014-09-03 16:16:56 -07001713 Set4BE(replyBuf + 4, request->GetId());
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001714 Set1(replyBuf + 8, kJDWPFlagReply);
1715 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001716
Ian Rogersc0542af2014-09-03 16:16:56 -07001717 CHECK_GT(expandBufGetLength(pReply), 0U) << GetCommandName(request) << " " << request->GetId();
Elliott Hughescb693062013-02-21 09:48:08 -08001718
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001719 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughescb693062013-02-21 09:48:08 -08001720 VLOG(jdwp) << "REPLY: " << GetCommandName(request) << " " << result << " (length=" << respLen << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001721 if (false) {
Ian Rogers43b2e0f2014-01-30 16:58:39 -08001722 VLOG(jdwp) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen, false, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001723 }
1724
Elliott Hughescb693062013-02-21 09:48:08 -08001725 VLOG(jdwp) << "----------";
1726
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001727 /*
1728 * Update last-activity timestamp. We really only need this during
1729 * the initial setup. Only update if this is a non-DDMS packet.
1730 */
Ian Rogersc0542af2014-09-03 16:16:56 -07001731 if (request->GetCommandSet() != kJDWPDdmCmdSet) {
Ian Rogers37f3c962014-07-17 11:25:30 -07001732 last_activity_time_ms_.StoreSequentiallyConsistent(MilliTime());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001733 }
1734
1735 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001736 self->TransitionFromRunnableToSuspended(old_state);
Sebastien Hertz43c8d722014-03-18 12:19:52 +01001737
1738 return replyLength;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001739}
1740
Sebastien Hertz99660e12014-02-19 15:04:42 +01001741/*
1742 * Indicates a request is about to be processed. If a thread wants to send an event in the meantime,
1743 * it will need to wait until we processed this request (see EndProcessingRequest).
1744 */
1745void JdwpState::StartProcessingRequest() {
1746 Thread* self = Thread::Current();
1747 CHECK_EQ(self, GetDebugThread()) << "Requests are only processed by debug thread";
1748 MutexLock mu(self, process_request_lock_);
1749 CHECK_EQ(processing_request_, false);
1750 processing_request_ = true;
1751}
1752
1753/*
1754 * Indicates a request has been processed (and we sent its reply). All threads waiting for us (see
1755 * WaitForProcessingRequest) are waken up so they can send events again.
1756 */
1757void JdwpState::EndProcessingRequest() {
1758 Thread* self = Thread::Current();
1759 CHECK_EQ(self, GetDebugThread()) << "Requests are only processed by debug thread";
1760 MutexLock mu(self, process_request_lock_);
1761 CHECK_EQ(processing_request_, true);
1762 processing_request_ = false;
1763 process_request_cond_.Broadcast(self);
1764}
1765
1766/*
1767 * Waits for any request being processed so we do not send an event in the meantime.
1768 */
1769void JdwpState::WaitForProcessingRequest() {
1770 Thread* self = Thread::Current();
1771 CHECK_NE(self, GetDebugThread()) << "Events should not be posted by debug thread";
1772 MutexLock mu(self, process_request_lock_);
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001773 bool waited = false;
Sebastien Hertz99660e12014-02-19 15:04:42 +01001774 while (processing_request_) {
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001775 VLOG(jdwp) << StringPrintf("wait for processing request");
1776 waited = true;
Sebastien Hertz99660e12014-02-19 15:04:42 +01001777 process_request_cond_.Wait(self);
1778 }
Sebastien Hertz400a3a92014-02-24 14:56:21 +01001779 if (waited) {
1780 VLOG(jdwp) << StringPrintf("finished waiting for processing request");
1781 }
Sebastien Hertz99660e12014-02-19 15:04:42 +01001782 CHECK_EQ(processing_request_, false);
1783}
1784
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001785} // namespace JDWP
1786
1787} // namespace art