blob: bc7c4d951841c7ae9b5185bbc0481617a16cadad [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
17/*
18 * Handle messages from debugger.
19 *
20 * GENERAL NOTE: we're not currently testing the message length for
21 * correctness. This is usually a bad idea, but here we can probably
22 * get away with it so long as the debugger isn't broken. We can
23 * change the "read" macros to use "dataLen" to avoid wandering into
24 * bad territory, and have a single "is dataLen correct" check at the
25 * end of each function. Not needed at this time.
26 */
27
Elliott Hughes872d4ec2011-10-21 17:07:15 -070028#include "jdwp/jdwp_handler.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070029
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
Elliott Hughesa96836a2013-01-17 12:27:49 -080034#include <string>
35
Elliott Hughes07ed66b2012-12-12 18:34:25 -080036#include "atomic.h"
37#include "base/logging.h"
38#include "base/macros.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080039#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080040#include "debugger.h"
41#include "jdwp/jdwp_constants.h"
42#include "jdwp/jdwp_event.h"
43#include "jdwp/jdwp_expand_buf.h"
44#include "jdwp/jdwp_priv.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "runtime.h"
Ian Rogers693ff612013-02-01 10:56:12 -080046#include "thread-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "UniquePtr.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080048
Elliott Hughes872d4ec2011-10-21 17:07:15 -070049namespace art {
50
51namespace JDWP {
52
Elliott Hughesa96836a2013-01-17 12:27:49 -080053static std::string DescribeField(const FieldId& field_id)
54 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
55 return StringPrintf("%#x (%s)", field_id, Dbg::GetFieldName(field_id).c_str());
56}
57
58static std::string DescribeMethod(const MethodId& method_id)
59 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
60 return StringPrintf("%#x (%s)", method_id, Dbg::GetMethodName(method_id).c_str());
61}
62
63static std::string DescribeRefTypeId(const RefTypeId& ref_type_id)
64 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
65 std::string signature("unknown");
66 Dbg::GetSignature(ref_type_id, signature);
67 return StringPrintf("%#llx (%s)", ref_type_id, signature.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -070068}
69
70/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -070071 * Helper function: read a variable-width value from the input buffer.
72 */
Elliott Hughes4993bbc2013-01-10 15:41:25 -080073static uint64_t ReadValue(const uint8_t** pBuf, size_t width) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070074 uint64_t value = -1;
75 switch (width) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070076 case 1: value = Read1(pBuf); break;
77 case 2: value = Read2BE(pBuf); break;
78 case 4: value = Read4BE(pBuf); break;
79 case 8: value = Read8BE(pBuf); break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070080 default: LOG(FATAL) << width; break;
81 }
82 return value;
83}
84
Elliott Hughesa96836a2013-01-17 12:27:49 -080085static int32_t ReadSigned32(const char* what, const uint8_t** pBuf) {
86 int32_t value = static_cast<int32_t>(Read4BE(pBuf));
87 VLOG(jdwp) << " " << what << " " << value;
88 return value;
89}
90
91uint32_t ReadUnsigned32(const char* what, const uint8_t** pBuf) {
92 uint32_t value = Read4BE(pBuf);
93 VLOG(jdwp) << " " << what << " " << value;
94 return value;
95}
96
97static FieldId ReadFieldId(const uint8_t** pBuf) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
98 FieldId id = Read4BE(pBuf);
99 VLOG(jdwp) << " field id " << DescribeField(id);
100 return id;
101}
102
103static MethodId ReadMethodId(const uint8_t** pBuf) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
104 MethodId id = Read4BE(pBuf);
105 VLOG(jdwp) << " method id " << DescribeMethod(id);
106 return id;
107}
108
109static ObjectId ReadObjectId(const char* specific_kind, const uint8_t** pBuf) {
110 ObjectId id = Read8BE(pBuf);
111 VLOG(jdwp) << StringPrintf(" %s id %#llx", specific_kind, id);
112 return id;
113}
114
115static ObjectId ReadArrayId(const uint8_t** pBuf) {
116 return ReadObjectId("array", pBuf);
117}
118
119ObjectId ReadObjectId(const uint8_t** pBuf) {
120 return ReadObjectId("object", pBuf);
121}
122
123static ObjectId ReadThreadId(const uint8_t** pBuf) {
124 return ReadObjectId("thread", pBuf);
125}
126
127static ObjectId ReadThreadGroupId(const uint8_t** pBuf) {
128 return ReadObjectId("thread group", pBuf);
129}
130
131static RefTypeId ReadRefTypeId(const uint8_t** pBuf) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
132 RefTypeId id = Read8BE(pBuf);
133 VLOG(jdwp) << " ref type id " << DescribeRefTypeId(id);
134 return id;
135}
136
137static FrameId ReadFrameId(const uint8_t** pBuf) {
138 FrameId id = Read8BE(pBuf);
139 VLOG(jdwp) << " frame id " << id;
140 return id;
141}
142
143static JdwpTag ReadTag(const uint8_t** pBuf) {
144 JdwpTag tag = static_cast<JdwpTag>(Read1(pBuf));
145 VLOG(jdwp) << " tag " << tag;
146 return tag;
147}
148
149static JdwpTypeTag ReadTypeTag(const uint8_t** pBuf) {
150 JdwpTypeTag tag = static_cast<JdwpTypeTag>(Read1(pBuf));
151 VLOG(jdwp) << " type tag " << tag;
152 return tag;
153}
154
155static JdwpLocation ReadLocation(const uint8_t** pBuf) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
156 JdwpLocation location;
157 memset(&location, 0, sizeof(location)); // Allows memcmp(3) later.
158 location.type_tag = ReadTypeTag(pBuf);
159 location.class_id = ReadObjectId(pBuf);
160 location.method_id = ReadMethodId(pBuf);
161 location.dex_pc = Read8BE(pBuf);
162 VLOG(jdwp) << " location " << location;
163 return location;
164}
165
166static std::string ReadUtf8String(unsigned char const** ppSrc) {
167 uint32_t length = Read4BE(ppSrc);
168 std::string s;
169 s.resize(length);
170 memcpy(&s[0], *ppSrc, length);
171 (*ppSrc) += length;
172 VLOG(jdwp) << " string \"" << s << "\"";
173 return s;
174}
175
176static JdwpModKind ReadModKind(const uint8_t** pBuf) {
177 JdwpModKind mod_kind = static_cast<JdwpModKind>(Read1(pBuf));
178 VLOG(jdwp) << " mod kind " << mod_kind;
179 return mod_kind;
180}
181
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700182/*
183 * Helper function: write a variable-width value into the output input buffer.
184 */
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800185static void WriteValue(ExpandBuf* pReply, int width, uint64_t value) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700186 switch (width) {
187 case 1: expandBufAdd1(pReply, value); break;
188 case 2: expandBufAdd2BE(pReply, value); break;
189 case 4: expandBufAdd4BE(pReply, value); break;
190 case 8: expandBufAdd8BE(pReply, value); break;
191 default: LOG(FATAL) << width; break;
192 }
193}
194
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800195static JdwpError WriteTaggedObject(ExpandBuf* reply, ObjectId object_id)
196 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
197 uint8_t tag;
198 JdwpError rc = Dbg::GetObjectTag(object_id, tag);
199 if (rc == ERR_NONE) {
200 expandBufAdd1(reply, tag);
201 expandBufAddObjectId(reply, object_id);
202 }
203 return rc;
204}
205
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800206static JdwpError WriteTaggedObjectList(ExpandBuf* reply, const std::vector<ObjectId>& objects)
207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
208 expandBufAdd4BE(reply, objects.size());
209 for (size_t i = 0; i < objects.size(); ++i) {
210 JdwpError rc = WriteTaggedObject(reply, objects[i]);
211 if (rc != ERR_NONE) {
212 return rc;
213 }
214 }
215 return ERR_NONE;
216}
217
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700218/*
219 * Common code for *_InvokeMethod requests.
220 *
Elliott Hughes74847412012-06-20 18:10:21 -0700221 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700222 * expected-to-be-void return value of the called function.
223 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700224static JdwpError FinishInvoke(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
Elliott Hughes74847412012-06-20 18:10:21 -0700225 ObjectId thread_id, ObjectId object_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 RefTypeId class_id, MethodId method_id, bool is_constructor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700228 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700229
Elliott Hughesa96836a2013-01-17 12:27:49 -0800230 int32_t arg_count = ReadSigned32("argument count", &buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700231
Elliott Hughes74847412012-06-20 18:10:21 -0700232 VLOG(jdwp) << StringPrintf(" --> thread_id=%#llx object_id=%#llx", thread_id, object_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 VLOG(jdwp) << StringPrintf(" class_id=%#llx method_id=%x %s.%s", class_id,
234 method_id, Dbg::GetClassName(class_id).c_str(),
Elliott Hughesa96836a2013-01-17 12:27:49 -0800235 Dbg::GetMethodName(method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800236 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700237
Elliott Hughes45651fd2012-02-21 15:48:20 -0800238 UniquePtr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
239 UniquePtr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800240 for (int32_t i = 0; i < arg_count; ++i) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800241 argTypes[i] = ReadTag(&buf);
242 size_t width = Dbg::GetTagWidth(argTypes[i]);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800243 argValues[i] = ReadValue(&buf, width);
Elliott Hughes229feb72012-02-23 13:33:29 -0800244 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#llx", width, argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700245 }
246
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700247 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
249 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
250 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700251
Elliott Hughes45651fd2012-02-21 15:48:20 -0800252 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700253 uint64_t resultValue;
254 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700255 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 -0700256 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800257 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700258 }
259
260 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800261 if (is_constructor) {
262 // If we invoked a constructor (which actually returns void), return the receiver,
263 // unless we threw, in which case we return NULL.
264 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700265 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800266 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700267
Elliott Hughes45651fd2012-02-21 15:48:20 -0800268 size_t width = Dbg::GetTagWidth(resultTag);
269 expandBufAdd1(pReply, resultTag);
270 if (width != 0) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800271 WriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700272 }
273 expandBufAdd1(pReply, JT_OBJECT);
274 expandBufAddObjectId(pReply, exceptObjId);
275
Elliott Hughes229feb72012-02-23 13:33:29 -0800276 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" %#llx (except=%#llx)", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700277
278 /* show detailed debug output */
279 if (resultTag == JT_STRING && exceptObjId == 0) {
280 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800281 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700282 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800283 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700284 }
285 }
286 }
287
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700288 return err;
289}
290
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291static JdwpError VM_Version(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes927bd292013-01-17 10:40:13 -0800293 // Text information on runtime version.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700294 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800295 expandBufAddUtf8String(pReply, version);
Elliott Hughes927bd292013-01-17 10:40:13 -0800296
297 // JDWP version numbers, major and minor.
298 expandBufAdd4BE(pReply, 1);
299 expandBufAdd4BE(pReply, 6);
300
301 // "java.version".
302 expandBufAddUtf8String(pReply, "1.6.0");
303
304 // "java.vm.name".
305 expandBufAddUtf8String(pReply, "Dalvik");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700306
307 return ERR_NONE;
308}
309
310/*
311 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
312 * referenceTypeID. We need to send back more than one if the class has
313 * been loaded by multiple class loaders.
314 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700315static JdwpError VM_ClassesBySignature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700316 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -0800317 std::string classDescriptor(ReadUtf8String(&buf));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700318
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800319 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800320 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700321
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800322 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700323
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800324 for (size_t i = 0; i < ids.size(); ++i) {
325 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800326 JDWP::JdwpTypeTag type_tag;
327 uint32_t class_status;
328 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
329 if (status != ERR_NONE) {
330 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800331 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700332
Elliott Hughes436e3722012-02-17 20:01:47 -0800333 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800334 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800335 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700336 }
337
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700338 return ERR_NONE;
339}
340
341/*
342 * Handle request for the thread IDs of all running threads.
343 *
344 * We exclude ourselves from the list, because we don't allow ourselves
345 * to be suspended, and that violates some JDWP expectations.
346 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700347static JdwpError VM_AllThreads(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700349 std::vector<ObjectId> thread_ids;
Elliott Hughes026b1462012-06-28 20:43:49 -0700350 Dbg::GetThreads(0, thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700351
Elliott Hughescaf76542012-06-28 16:08:22 -0700352 expandBufAdd4BE(pReply, thread_ids.size());
353 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
354 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700355 }
356
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700357 return ERR_NONE;
358}
359
360/*
361 * List all thread groups that do not have a parent.
362 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363static JdwpError VM_TopLevelThreadGroups(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700364 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700365 /*
366 * TODO: maintain a list of parentless thread groups in the VM.
367 *
368 * For now, just return "system". Application threads are created
369 * in "main", which is a child of "system".
370 */
371 uint32_t groups = 1;
372 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700373 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
374 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700375
376 return ERR_NONE;
377}
378
379/*
380 * Respond with the sizes of the basic debugger types.
381 *
382 * All IDs are 8 bytes.
383 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700384static JdwpError VM_IDSizes(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700385 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700386 expandBufAdd4BE(pReply, sizeof(FieldId));
387 expandBufAdd4BE(pReply, sizeof(MethodId));
388 expandBufAdd4BE(pReply, sizeof(ObjectId));
389 expandBufAdd4BE(pReply, sizeof(RefTypeId));
390 expandBufAdd4BE(pReply, sizeof(FrameId));
391 return ERR_NONE;
392}
393
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394static JdwpError VM_Dispose(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes86964332012-02-15 19:37:42 -0800396 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700397 return ERR_NONE;
398}
399
400/*
401 * Suspend the execution of the application running in the VM (i.e. suspend
402 * all threads).
403 *
404 * This needs to increment the "suspend count" on all threads.
405 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700406static JdwpError VM_Suspend(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800408 Thread* self = Thread::Current();
409 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700410 Dbg::SuspendVM();
jeffhaoa77f0f62012-12-05 17:19:31 -0800411 self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412 return ERR_NONE;
413}
414
415/*
416 * Resume execution. Decrements the "suspend count" of all threads.
417 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700418static JdwpError VM_Resume(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700419 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700420 Dbg::ResumeVM();
421 return ERR_NONE;
422}
423
Elliott Hughes64f574f2013-02-20 14:57:12 -0800424static JdwpError VM_Exit(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700425 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800426 uint32_t exit_status = ReadUnsigned32("exit_status", &buf);
427 state->ExitAfterReplying(exit_status);
428 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700429}
430
431/*
432 * Create a new string in the VM and return its ID.
433 *
434 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
435 * string "java.util.Arrays".)
436 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700437static JdwpError VM_CreateString(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700438 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -0800439 std::string str(ReadUtf8String(&buf));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700440 ObjectId stringId = Dbg::CreateString(str);
441 if (stringId == 0) {
442 return ERR_OUT_OF_MEMORY;
443 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700444 expandBufAddObjectId(pReply, stringId);
445 return ERR_NONE;
446}
447
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700448static JdwpError VM_ClassPaths(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700449 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800450 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800452 std::vector<std::string> class_path;
453 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
454 expandBufAdd4BE(pReply, class_path.size());
455 for (size_t i = 0; i < class_path.size(); ++i) {
456 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700457 }
458
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800459 std::vector<std::string> boot_class_path;
460 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
461 expandBufAdd4BE(pReply, boot_class_path.size());
462 for (size_t i = 0; i < boot_class_path.size(); ++i) {
463 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700464 }
465
466 return ERR_NONE;
467}
468
Elliott Hughes64f574f2013-02-20 14:57:12 -0800469static JdwpError VM_DisposeObjects(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700470 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800471 size_t object_count = ReadUnsigned32("object_count", &buf);
472 for (size_t i = 0; i < object_count; ++i) {
473 ObjectId object_id = ReadObjectId(&buf);
474 uint32_t reference_count = ReadUnsigned32("reference_count", &buf);
475 Dbg::DisposeObject(object_id, reference_count);
476 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700477 return ERR_NONE;
478}
479
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800480static JdwpError VM_Capabilities(JdwpState*, const uint8_t*, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700481 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800482 expandBufAdd1(reply, false); // canWatchFieldModification
483 expandBufAdd1(reply, false); // canWatchFieldAccess
Elliott Hughes9777ba22013-01-17 09:04:19 -0800484 expandBufAdd1(reply, true); // canGetBytecodes
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800485 expandBufAdd1(reply, true); // canGetSyntheticAttribute
486 expandBufAdd1(reply, true); // canGetOwnedMonitorInfo
Elliott Hughesf9501702013-01-11 11:22:27 -0800487 expandBufAdd1(reply, true); // canGetCurrentContendedMonitor
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800488 expandBufAdd1(reply, true); // canGetMonitorInfo
489 return ERR_NONE;
490}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700491
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800492static JdwpError VM_CapabilitiesNew(JdwpState*, const uint8_t*, int, ExpandBuf* reply)
493 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
494
495 // The first few capabilities are the same as those reported by the older call.
496 VM_Capabilities(NULL, NULL, 0, reply);
497
498 expandBufAdd1(reply, false); // canRedefineClasses
499 expandBufAdd1(reply, false); // canAddMethod
500 expandBufAdd1(reply, false); // canUnrestrictedlyRedefineClasses
501 expandBufAdd1(reply, false); // canPopFrames
502 expandBufAdd1(reply, false); // canUseInstanceFilters
503 expandBufAdd1(reply, false); // canGetSourceDebugExtension
504 expandBufAdd1(reply, false); // canRequestVMDeathEvent
505 expandBufAdd1(reply, false); // canSetDefaultStratum
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800506 expandBufAdd1(reply, true); // 1.6: canGetInstanceInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800507 expandBufAdd1(reply, false); // 1.6: canRequestMonitorEvents
Elliott Hughes734b8c62013-01-11 15:32:45 -0800508 expandBufAdd1(reply, true); // 1.6: canGetMonitorFrameInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800509 expandBufAdd1(reply, false); // 1.6: canUseSourceNameFilters
510 expandBufAdd1(reply, false); // 1.6: canGetConstantPool
511 expandBufAdd1(reply, false); // 1.6: canForceEarlyReturn
512
513 // Fill in reserved22 through reserved32; note count started at 1.
514 for (size_t i = 22; i <= 32; ++i) {
515 expandBufAdd1(reply, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700516 }
517 return ERR_NONE;
518}
519
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700521 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800522 std::vector<JDWP::RefTypeId> classes;
523 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700524
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800525 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700526
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800527 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800528 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800529 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800530 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800531 uint32_t class_status;
532 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
533 if (status != ERR_NONE) {
534 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800535 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700536
Elliott Hughes436e3722012-02-17 20:01:47 -0800537 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800538 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800539 if (descriptor_and_status) {
540 expandBufAddUtf8String(pReply, descriptor);
541 if (generic) {
542 expandBufAddUtf8String(pReply, genericSignature);
543 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800544 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800545 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700546 }
547
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700548 return ERR_NONE;
549}
550
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700551static JdwpError VM_AllClasses(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700552 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700553 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800554}
555
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700556static JdwpError VM_AllClassesWithGeneric(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700557 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700558 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800559}
560
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800561static JdwpError VM_InstanceCounts(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
562 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -0800563 int32_t class_count = ReadSigned32("class count", &buf);
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800564 if (class_count < 0) {
565 return ERR_ILLEGAL_ARGUMENT;
566 }
567 std::vector<RefTypeId> class_ids;
568 for (int32_t i = 0; i < class_count; ++i) {
569 class_ids.push_back(ReadRefTypeId(&buf));
570 }
571
572 std::vector<uint64_t> counts;
573 JdwpError rc = Dbg::GetInstanceCounts(class_ids, counts);
574 if (rc != ERR_NONE) {
575 return rc;
576 }
577
578 expandBufAdd4BE(pReply, counts.size());
579 for (size_t i = 0; i < counts.size(); ++i) {
580 expandBufAdd8BE(pReply, counts[i]);
581 }
582 return ERR_NONE;
583}
584
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700585static JdwpError RT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700586 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700587 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800588 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700589}
590
591/*
592 * Get values from static fields in a reference type.
593 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700594static JdwpError RT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700595 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800596 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800597 int32_t field_count = ReadSigned32("field count", &buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800598 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800599 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700600 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800601 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800602 if (status != ERR_NONE) {
603 return status;
604 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700605 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700606 return ERR_NONE;
607}
608
609/*
610 * Get the name of the source file in which a reference type was declared.
611 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700612static JdwpError RT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700613 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700614 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800615 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800616 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
617 if (status != ERR_NONE) {
618 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700619 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800620 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800621 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622}
623
624/*
625 * Return the current status of the reference type.
626 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627static JdwpError RT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700628 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700629 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800630 JDWP::JdwpTypeTag type_tag;
631 uint32_t class_status;
632 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
633 if (status != ERR_NONE) {
634 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800635 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800636 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700637 return ERR_NONE;
638}
639
640/*
641 * Return interfaces implemented directly by this class.
642 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700643static JdwpError RT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700644 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700645 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800646 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700647}
648
649/*
650 * Return the class object corresponding to this type.
651 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652static JdwpError RT_ClassObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700653 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700654 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800655 ObjectId class_object_id;
656 JdwpError status = Dbg::GetClassObject(refTypeId, class_object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800657 if (status != ERR_NONE) {
658 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800659 }
Elliott Hughesa96836a2013-01-17 12:27:49 -0800660 VLOG(jdwp) << StringPrintf(" --> ObjectId %#llx", class_object_id);
661 expandBufAddObjectId(pReply, class_object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700662 return ERR_NONE;
663}
664
665/*
666 * Returns the value of the SourceDebugExtension attribute.
667 *
668 * JDB seems interested, but DEX files don't currently support this.
669 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670static JdwpError RT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700671 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700672 /* referenceTypeId in, string out */
673 return ERR_ABSENT_INFORMATION;
674}
675
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700676static JdwpError RT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
677 bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700678 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700679 RefTypeId refTypeId = ReadRefTypeId(&buf);
680
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800681 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800682 JdwpError status = Dbg::GetSignature(refTypeId, signature);
683 if (status != ERR_NONE) {
684 return status;
685 }
686 expandBufAddUtf8String(pReply, signature);
687 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800688 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700689 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700690 return ERR_NONE;
691}
692
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700693static JdwpError RT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700694 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700695 return RT_Signature(state, buf, dataLen, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800696}
697
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698static JdwpError RT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
699 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700700 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700701 return RT_Signature(state, buf, dataLen, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800702}
703
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700704/*
705 * Return the instance of java.lang.ClassLoader that loaded the specified
706 * reference type, or null if it was loaded by the system loader.
707 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708static JdwpError RT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700709 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700710 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800711 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700712}
713
714/*
715 * Given a referenceTypeId, return a block of stuff that describes the
716 * fields declared by a class.
717 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700718static JdwpError RT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700719 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700720 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800721 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800722}
723
724// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700725static JdwpError RT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700726 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800727 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800728 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700729}
730
731/*
732 * Given a referenceTypeID, return a block of goodies describing the
733 * methods declared by a class.
734 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700735static JdwpError RT_MethodsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700736 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700737 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800738 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800739}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700740
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800741// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700742static JdwpError RT_Methods(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700743 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800744 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800745 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700746}
747
Elliott Hughes3b78c942013-01-15 17:35:41 -0800748static JdwpError RT_Instances(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
749 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
750 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800751 int32_t max_count = ReadSigned32("max count", &buf);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800752 if (max_count < 0) {
753 return ERR_ILLEGAL_ARGUMENT;
754 }
755
756 std::vector<ObjectId> instances;
757 JdwpError rc = Dbg::GetInstances(class_id, max_count, instances);
758 if (rc != ERR_NONE) {
759 return rc;
760 }
761
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800762 return WriteTaggedObjectList(reply, instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800763}
764
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700765/*
766 * Return the immediate superclass of a class.
767 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700768static JdwpError CT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700769 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700770 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800771 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700772 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800773 if (status != ERR_NONE) {
774 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800775 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700776 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700777 return ERR_NONE;
778}
779
780/*
781 * Set static class values.
782 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700783static JdwpError CT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700784 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700785 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800786 int32_t values_count = ReadSigned32("values count", &buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700787
Elliott Hughesa96836a2013-01-17 12:27:49 -0800788 UNUSED(class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700789
Elliott Hughesa96836a2013-01-17 12:27:49 -0800790 for (int32_t i = 0; i < values_count; ++i) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700791 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800792 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800793 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800794 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700795
Elliott Hughesa96836a2013-01-17 12:27:49 -0800796 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " --> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800797 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
798 if (status != ERR_NONE) {
799 return status;
800 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801 }
802
803 return ERR_NONE;
804}
805
806/*
807 * Invoke a static method.
808 *
809 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
810 * values in the "variables" display.
811 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812static JdwpError CT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
813 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700814 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700815 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800816 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700817 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700818
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700819 return FinishInvoke(state, buf, dataLen, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700820}
821
822/*
823 * Create a new object of the requested type, and invoke the specified
824 * constructor.
825 *
826 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
827 * see the contents of a byte[] as a string.
828 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700829static JdwpError CT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen,
830 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700831 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700832 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800833 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700834 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700835
Elliott Hughes74847412012-06-20 18:10:21 -0700836 ObjectId object_id;
837 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800838 if (status != ERR_NONE) {
839 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800840 }
Elliott Hughes74847412012-06-20 18:10:21 -0700841 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700842 return ERR_OUT_OF_MEMORY;
843 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700844 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700845}
846
847/*
848 * Create a new array object of the requested type and length.
849 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700850static JdwpError AT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700851 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700852 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800853 int32_t length = ReadSigned32("length", &buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700854
Elliott Hughes74847412012-06-20 18:10:21 -0700855 ObjectId object_id;
856 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800857 if (status != ERR_NONE) {
858 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800859 }
Elliott Hughes74847412012-06-20 18:10:21 -0700860 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700861 return ERR_OUT_OF_MEMORY;
862 }
863 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700864 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700865 return ERR_NONE;
866}
867
868/*
869 * Return line number information for the method, if present.
870 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700871static JdwpError M_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700872 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700873 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700874 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700875
Elliott Hughes74847412012-06-20 18:10:21 -0700876 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700877
878 return ERR_NONE;
879}
880
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700881static JdwpError M_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
882 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700883 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700884 RefTypeId class_id = ReadRefTypeId(&buf);
885 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700886
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800887 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
888 // information. That will cause Eclipse to make a best-effort attempt at displaying local
889 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
890 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700891 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700892 return ERR_NONE;
893}
894
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700895static JdwpError M_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen,
896 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700897 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700898 return M_VariableTable(state, buf, dataLen, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800899}
900
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700901static JdwpError M_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
902 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700903 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700904 return M_VariableTable(state, buf, dataLen, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800905}
906
Elliott Hughes9777ba22013-01-17 09:04:19 -0800907static JdwpError M_Bytecodes(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
908 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
909 RefTypeId class_id = ReadRefTypeId(&buf);
910 MethodId method_id = ReadMethodId(&buf);
911
912 std::vector<uint8_t> bytecodes;
913 JdwpError rc = Dbg::GetBytecodes(class_id, method_id, bytecodes);
914 if (rc != ERR_NONE) {
915 return rc;
916 }
917
918 expandBufAdd4BE(reply, bytecodes.size());
919 for (size_t i = 0; i < bytecodes.size(); ++i) {
920 expandBufAdd1(reply, bytecodes[i]);
921 }
922
923 return ERR_NONE;
924}
925
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700926/*
927 * Given an object reference, return the runtime type of the object
928 * (class or array).
929 *
Elliott Hughes74847412012-06-20 18:10:21 -0700930 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700931 * passed in here.
932 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933static JdwpError OR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700934 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700935 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700936 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700937}
938
939/*
940 * Get values from the fields of an object.
941 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700942static JdwpError OR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700943 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700944 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800945 int32_t field_count = ReadSigned32("field count", &buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700946
Elliott Hughes0cf74332012-02-23 23:14:00 -0800947 expandBufAdd4BE(pReply, field_count);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800948 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700949 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700950 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800951 if (status != ERR_NONE) {
952 return status;
953 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700954 }
955
956 return ERR_NONE;
957}
958
959/*
960 * Set values in the fields of an object.
961 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700962static JdwpError OR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700963 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700964 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -0800965 int32_t field_count = ReadSigned32("field count", &buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700966
Elliott Hughesa96836a2013-01-17 12:27:49 -0800967 for (int32_t i = 0; i < field_count; ++i) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700968 FieldId fieldId = ReadFieldId(&buf);
969
Elliott Hughesaed4be92011-12-02 16:16:23 -0800970 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800971 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800972 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700973
Elliott Hughes2435a572012-02-17 16:07:41 -0800974 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700975 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800976 if (status != ERR_NONE) {
977 return status;
978 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700979 }
980
981 return ERR_NONE;
982}
983
Elliott Hughesf327e072013-01-09 16:01:26 -0800984static JdwpError OR_MonitorInfo(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
985 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
986 ObjectId object_id = ReadObjectId(&buf);
987 return Dbg::GetMonitorInfo(object_id, reply);
988}
989
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700990/*
991 * Invoke an instance method. The invocation must occur in the specified
992 * thread, which must have been suspended by an event.
993 *
994 * The call is synchronous. All threads in the VM are resumed, unless the
995 * SINGLE_THREADED flag is set.
996 *
997 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
998 * object), it will try to invoke the object's toString() function. This
999 * feature becomes crucial when examining ArrayLists with Eclipse.
1000 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001001static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
1002 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001003 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001004 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -08001005 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001006 RefTypeId class_id = ReadRefTypeId(&buf);
1007 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001008
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001009 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001010}
1011
Elliott Hughes64f574f2013-02-20 14:57:12 -08001012static JdwpError OR_DisableCollection(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001013 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes64f574f2013-02-20 14:57:12 -08001014 ObjectId object_id = ReadObjectId(&buf);
1015 return Dbg::DisableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001016}
1017
Elliott Hughes64f574f2013-02-20 14:57:12 -08001018static JdwpError OR_EnableCollection(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001019 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes64f574f2013-02-20 14:57:12 -08001020 ObjectId object_id = ReadObjectId(&buf);
1021 return Dbg::EnableCollection(object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001022}
1023
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001025 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes64f574f2013-02-20 14:57:12 -08001026 ObjectId object_id = ReadObjectId(&buf);
1027 bool is_collected;
1028 JdwpError rc = Dbg::IsCollected(object_id, is_collected);
1029 expandBufAdd1(pReply, is_collected ? 1 : 0);
1030 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031}
1032
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001033static JdwpError OR_ReferringObjects(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1034 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1035 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -08001036 int32_t max_count = ReadSigned32("max count", &buf);
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001037 if (max_count < 0) {
1038 return ERR_ILLEGAL_ARGUMENT;
1039 }
1040
1041 std::vector<ObjectId> referring_objects;
1042 JdwpError rc = Dbg::GetReferringObjects(object_id, max_count, referring_objects);
1043 if (rc != ERR_NONE) {
1044 return rc;
1045 }
1046
1047 return WriteTaggedObjectList(reply, referring_objects);
1048}
1049
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001050/*
1051 * Return the string value in a string object.
1052 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001053static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001054 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001055 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001056 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001057
Elliott Hughesa96836a2013-01-17 12:27:49 -08001058 VLOG(jdwp) << StringPrintf(" --> %s", PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001059
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001060 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001061
1062 return ERR_NONE;
1063}
1064
1065/*
1066 * Return a thread's name.
1067 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001069 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001070 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001071
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001072 std::string name;
Elliott Hughes221229c2013-01-08 18:17:50 -08001073 JdwpError error = Dbg::GetThreadName(thread_id, name);
1074 if (error != ERR_NONE) {
1075 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001076 }
Elliott Hughes74847412012-06-20 18:10:21 -07001077 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001078 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001079
1080 return ERR_NONE;
1081}
1082
1083/*
1084 * Suspend the specified thread.
1085 *
1086 * It's supposed to remain suspended even if interpreted code wants to
1087 * resume it; only the JDI is allowed to resume it.
1088 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001090 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001091 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001092
Elliott Hughes74847412012-06-20 18:10:21 -07001093 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001094 LOG(INFO) << " Warning: ignoring request to suspend self";
1095 return ERR_THREAD_NOT_SUSPENDED;
1096 }
Elliott Hughesa96836a2013-01-17 12:27:49 -08001097
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001098 Thread* self = Thread::Current();
1099 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
1100 JdwpError result = Dbg::SuspendThread(thread_id);
1101 self->TransitionFromSuspendedToRunnable();
1102 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001103}
1104
1105/*
1106 * Resume the specified thread.
1107 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001108static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001110 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001111
Elliott Hughes74847412012-06-20 18:10:21 -07001112 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001113 LOG(INFO) << " Warning: ignoring request to resume self";
1114 return ERR_NONE;
1115 }
Elliott Hughesa96836a2013-01-17 12:27:49 -08001116
Elliott Hughes74847412012-06-20 18:10:21 -07001117 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001118 return ERR_NONE;
1119}
1120
1121/*
1122 * Return status of specified thread.
1123 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001124static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001126 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001127
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08001128 JDWP::JdwpThreadStatus threadStatus;
1129 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -08001130 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
1131 if (error != ERR_NONE) {
1132 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001133 }
1134
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001135 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001136
1137 expandBufAdd4BE(pReply, threadStatus);
1138 expandBufAdd4BE(pReply, suspendStatus);
1139
1140 return ERR_NONE;
1141}
1142
1143/*
1144 * Return the thread group that the specified thread is a member of.
1145 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001148 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001149 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001150}
1151
1152/*
1153 * Return the current call stack of a suspended thread.
1154 *
1155 * If the thread isn't suspended, the error code isn't defined, but should
1156 * be THREAD_NOT_SUSPENDED.
1157 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001158static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001160 ObjectId thread_id = ReadThreadId(&buf);
1161 uint32_t start_frame = ReadUnsigned32("start frame", &buf);
1162 uint32_t length = ReadUnsigned32("length", &buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001163
Elliott Hughes221229c2013-01-08 18:17:50 -08001164 size_t actual_frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001165 JdwpError error = Dbg::GetThreadFrameCount(thread_id, actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001166 if (error != ERR_NONE) {
1167 return error;
1168 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001169
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001170 if (actual_frame_count <= 0) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001171 return ERR_THREAD_NOT_SUSPENDED; // 0 means no managed frames (which means "in native").
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001172 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001173
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001174 if (start_frame > actual_frame_count) {
1175 return ERR_INVALID_INDEX;
1176 }
1177 if (length == static_cast<uint32_t>(-1)) {
1178 length = actual_frame_count - start_frame;
1179 }
1180 if (start_frame + length > actual_frame_count) {
1181 return ERR_INVALID_LENGTH;
1182 }
1183
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001184 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001185}
1186
1187/*
1188 * Returns the #of frames on the specified thread, which must be suspended.
1189 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001191 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001192 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001193
Elliott Hughes221229c2013-01-08 18:17:50 -08001194 size_t frame_count;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001195 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, frame_count);
1196 if (rc != ERR_NONE) {
1197 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001198 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001199 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001200
1201 return ERR_NONE;
1202}
1203
Elliott Hughes734b8c62013-01-11 15:32:45 -08001204static JdwpError TR_OwnedMonitors(const uint8_t* buf, ExpandBuf* reply, bool with_stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001206 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001207
1208 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001209 std::vector<uint32_t> stack_depths;
1210 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, monitors, stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001211 if (rc != ERR_NONE) {
1212 return rc;
1213 }
1214
1215 expandBufAdd4BE(reply, monitors.size());
1216 for (size_t i = 0; i < monitors.size(); ++i) {
1217 rc = WriteTaggedObject(reply, monitors[i]);
1218 if (rc != ERR_NONE) {
1219 return rc;
1220 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001221 if (with_stack_depths) {
1222 expandBufAdd4BE(reply, stack_depths[i]);
1223 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001224 }
1225 return ERR_NONE;
1226}
1227
Elliott Hughes734b8c62013-01-11 15:32:45 -08001228static JdwpError TR_OwnedMonitors(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1230 return TR_OwnedMonitors(buf, reply, false);
1231}
1232
1233static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1235 return TR_OwnedMonitors(buf, reply, true);
1236}
1237
Elliott Hughesf9501702013-01-11 11:22:27 -08001238static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001240 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001241
Elliott Hughesf9501702013-01-11 11:22:27 -08001242 ObjectId contended_monitor;
1243 JdwpError rc = Dbg::GetContendedMonitor(thread_id, contended_monitor);
1244 if (rc != ERR_NONE) {
1245 return rc;
1246 }
1247 return WriteTaggedObject(reply, contended_monitor);
1248}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001249
Elliott Hughesf9501702013-01-11 11:22:27 -08001250static JdwpError TR_Interrupt(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1251 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001252 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughesf9501702013-01-11 11:22:27 -08001253 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001254}
1255
1256/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001257 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001258 *
1259 * (The thread *might* still be running -- it might not have examined
1260 * its suspend count recently.)
1261 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001262static JdwpError TR_DebugSuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001264 ObjectId thread_id = ReadThreadId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001265 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001266}
1267
1268/*
1269 * Return the name of a thread group.
1270 *
1271 * The Eclipse debugger recognizes "main" and "system" as special.
1272 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001275 ObjectId thread_group_id = ReadThreadGroupId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001276
Elliott Hughescaf76542012-06-28 16:08:22 -07001277 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001278
1279 return ERR_NONE;
1280}
1281
1282/*
1283 * Returns the thread group -- if any -- that contains the specified
1284 * thread group.
1285 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001286static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001287 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001288 ObjectId thread_group_id = ReadThreadGroupId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001289
Elliott Hughescaf76542012-06-28 16:08:22 -07001290 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001291 expandBufAddObjectId(pReply, parentGroup);
1292
1293 return ERR_NONE;
1294}
1295
1296/*
1297 * Return the active threads and thread groups that are part of the
1298 * specified thread group.
1299 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001300static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001302 ObjectId thread_group_id = ReadThreadGroupId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001303
Elliott Hughescaf76542012-06-28 16:08:22 -07001304 std::vector<ObjectId> thread_ids;
1305 Dbg::GetThreads(thread_group_id, thread_ids);
1306 expandBufAdd4BE(pReply, thread_ids.size());
1307 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1308 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001309 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001310
Elliott Hughescaf76542012-06-28 16:08:22 -07001311 std::vector<ObjectId> child_thread_groups_ids;
1312 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1313 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1314 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1315 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001316 }
1317
1318 return ERR_NONE;
1319}
1320
1321/*
1322 * Return the #of components in the array.
1323 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001324static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001325 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001326 ObjectId array_id = ReadArrayId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001327
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001328 int length;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001329 JdwpError status = Dbg::GetArrayLength(array_id, length);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001330 if (status != ERR_NONE) {
1331 return status;
1332 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001333 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001334
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001335 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001336
1337 return ERR_NONE;
1338}
1339
1340/*
1341 * Return the values from an array.
1342 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001343static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001345 ObjectId array_id = ReadArrayId(&buf);
1346 uint32_t offset = ReadUnsigned32("offset", &buf);
1347 uint32_t length = ReadUnsigned32("length", &buf);
1348 return Dbg::OutputArray(array_id, offset, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001349}
1350
1351/*
1352 * Set values in an array.
1353 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001354static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001356 ObjectId array_id = ReadArrayId(&buf);
1357 uint32_t offset = ReadUnsigned32("offset", &buf);
1358 uint32_t length = ReadUnsigned32("length", &buf);
1359 return Dbg::SetArrayElements(array_id, offset, length, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001360}
1361
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001362static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001363 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001364 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001365 // TODO: we should only return classes which have the given class loader as a defining or
1366 // initiating loader. The former would be easy; the latter is hard, because we don't have
1367 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001368 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001369}
1370
1371/*
1372 * Set an event trigger.
1373 *
1374 * Reply with a requestID.
1375 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001376static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001377 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001378 const uint8_t* origBuf = buf;
1379
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001380 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001381 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -08001382 int32_t modifier_count = ReadSigned32("modifier count", &buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001383
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001384 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001385 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughesa96836a2013-01-17 12:27:49 -08001386 << " mods=" << modifier_count << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001387
Elliott Hughesa96836a2013-01-17 12:27:49 -08001388 CHECK_LT(modifier_count, 256); /* reasonableness check */
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001389
Elliott Hughesa96836a2013-01-17 12:27:49 -08001390 JdwpEvent* pEvent = EventAlloc(modifier_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001391 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001392 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughesa96836a2013-01-17 12:27:49 -08001393 pEvent->modCount = modifier_count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001394
1395 /*
1396 * Read modifiers. Ordering may be significant (see explanation of Count
1397 * mods in JDWP doc).
1398 */
Elliott Hughesa96836a2013-01-17 12:27:49 -08001399 for (int32_t i = 0; i < modifier_count; ++i) {
Elliott Hughes972a47b2012-02-21 18:16:06 -08001400 JdwpEventMod& mod = pEvent->mods[i];
Elliott Hughesa96836a2013-01-17 12:27:49 -08001401 mod.modKind = ReadModKind(&buf);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001402 switch (mod.modKind) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001403 case MK_COUNT:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001404 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001405 // Report once, when "--count" reaches 0.
1406 uint32_t count = ReadUnsigned32("count", &buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001407 if (count == 0) {
1408 return ERR_INVALID_COUNT;
1409 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001410 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001411 }
1412 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001413 case MK_CONDITIONAL:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001414 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001415 // Conditional on expression.
1416 uint32_t exprId = ReadUnsigned32("expr id", &buf);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001417 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001418 }
1419 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001420 case MK_THREAD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001421 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001422 // Only report events in specified thread.
1423 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001424 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001425 }
1426 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001427 case MK_CLASS_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001428 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001429 // For ClassPrepare, MethodEntry.
Elliott Hughes74847412012-06-20 18:10:21 -07001430 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001431 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001432 }
1433 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001434 case MK_CLASS_MATCH:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001435 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001436 // Restrict events to matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001437 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughesa96836a2013-01-17 12:27:49 -08001438 std::string pattern(ReadUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001439 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001440 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001441 }
1442 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001443 case MK_CLASS_EXCLUDE:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001444 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001445 // Restrict events to non-matching classes.
Elliott Hughes86964332012-02-15 19:37:42 -08001446 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughesa96836a2013-01-17 12:27:49 -08001447 std::string pattern(ReadUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001448 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes972a47b2012-02-21 18:16:06 -08001449 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001450 }
1451 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001452 case MK_LOCATION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001453 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001454 // Restrict certain events based on location.
1455 JdwpLocation location = ReadLocation(&buf);
1456 mod.locationOnly.loc = location;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001457 }
1458 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001459 case MK_EXCEPTION_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001460 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001461 // Modifies EK_EXCEPTION events,
1462 RefTypeId exceptionOrNull = ReadRefTypeId(&buf); // null => all exceptions.
1463 uint8_t caught = Read1(&buf);
1464 uint8_t uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001465 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001466 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001467
Elliott Hughes972a47b2012-02-21 18:16:06 -08001468 mod.exceptionOnly.refTypeId = exceptionOrNull;
1469 mod.exceptionOnly.caught = caught;
1470 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001471 }
1472 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001473 case MK_FIELD_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001474 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001475 // For field access/modification events.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001476 RefTypeId declaring = ReadRefTypeId(&buf);
1477 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001478 mod.fieldOnly.refTypeId = declaring;
1479 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001480 }
1481 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001482 case MK_STEP:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001483 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001484 // For use with EK_SINGLE_STEP.
1485 ObjectId thread_id = ReadThreadId(&buf);
1486 uint32_t size = Read4BE(&buf);
1487 uint32_t depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001488 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001489 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1490
Elliott Hughes74847412012-06-20 18:10:21 -07001491 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001492 mod.step.size = size;
1493 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001494 }
1495 break;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001496 case MK_INSTANCE_ONLY:
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001497 {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001498 // Report events related to a specific object.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001499 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001500 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001501 }
1502 break;
1503 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001504 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001505 break;
1506 }
1507 }
1508
1509 /*
1510 * Make sure we consumed all data. It is possible that the remote side
1511 * has sent us bad stuff, but for now we blame ourselves.
1512 */
1513 if (buf != origBuf + dataLen) {
1514 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1515 }
1516
1517 /*
1518 * We reply with an integer "requestID".
1519 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001520 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001521 expandBufAdd4BE(pReply, requestId);
1522
1523 pEvent->requestId = requestId;
1524
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001525 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001526
1527 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001528 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001529 if (err != ERR_NONE) {
1530 /* registration failed, probably because event is bogus */
1531 EventFree(pEvent);
1532 LOG(WARNING) << "WARNING: event request rejected";
1533 }
1534 return err;
1535}
1536
1537/*
1538 * Clear an event. Failure to find an event with a matching ID is a no-op
1539 * and does not return an error.
1540 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001541static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001542 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001543 uint8_t eventKind = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001544 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001545
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001546 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001547
Elliott Hughes761928d2011-11-16 18:33:03 -08001548 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001549
1550 return ERR_NONE;
1551}
1552
1553/*
1554 * Return the values of arguments and local variables.
1555 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001556static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001557 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001558 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001559 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -08001560 int32_t slot_count = ReadSigned32("slot count", &buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001561
Elliott Hughesa96836a2013-01-17 12:27:49 -08001562 expandBufAdd4BE(pReply, slot_count); /* "int values" */
1563 for (int32_t i = 0; i < slot_count; ++i) {
1564 uint32_t slot = ReadUnsigned32("slot", &buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001565 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001566
Elliott Hughes2435a572012-02-17 16:07:41 -08001567 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001568
Elliott Hughesdbb40792011-11-18 17:05:22 -08001569 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001570 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001571 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001572 }
1573
1574 return ERR_NONE;
1575}
1576
1577/*
1578 * Set the values of arguments and local variables.
1579 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001580static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001581 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001582 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001583 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesa96836a2013-01-17 12:27:49 -08001584 int32_t slot_count = ReadSigned32("slot count", &buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001585
Elliott Hughesa96836a2013-01-17 12:27:49 -08001586 for (int32_t i = 0; i < slot_count; ++i) {
1587 uint32_t slot = ReadUnsigned32("slot", &buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001588 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001589 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001590 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001591
Elliott Hughes2435a572012-02-17 16:07:41 -08001592 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001593 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001594 }
1595
1596 return ERR_NONE;
1597}
1598
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001599static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001600 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001601 ObjectId thread_id = ReadThreadId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001602 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001603
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001604 ObjectId object_id;
1605 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001606 if (rc != ERR_NONE) {
1607 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001608 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001609
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001610 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001611}
1612
1613/*
1614 * Return the reference type reflected by this class object.
1615 *
1616 * This appears to be required because ReferenceTypeId values are NEVER
1617 * reused, whereas ClassIds can be recycled like any other object. (Either
1618 * that, or I have no idea what this is for.)
1619 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001620static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001621 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa96836a2013-01-17 12:27:49 -08001622 RefTypeId class_object_id = ReadRefTypeId(&buf);
1623 return Dbg::GetReflectedType(class_object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001624}
1625
1626/*
1627 * Handle a DDM packet with a single chunk in it.
1628 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001629static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001630 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001631 uint8_t* replyBuf = NULL;
1632 int replyLen = -1;
1633
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001634 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001635
Elliott Hughesa21039c2012-06-21 12:09:25 -07001636 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001637
1638 /*
1639 * If they want to send something back, we copy it into the buffer.
1640 * A no-copy approach would be nicer.
1641 *
1642 * TODO: consider altering the JDWP stuff to hold the packet header
1643 * in a separate buffer. That would allow us to writev() DDM traffic
1644 * instead of copying it into the expanding buffer. The reduction in
1645 * heap requirements is probably more valuable than the efficiency.
1646 */
1647 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1648 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1649 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1650 free(replyBuf);
1651 }
1652 return ERR_NONE;
1653}
1654
1655/*
1656 * Handler map decl.
1657 */
1658typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1659
1660struct JdwpHandlerMap {
1661 uint8_t cmdSet;
1662 uint8_t cmd;
1663 JdwpRequestHandler func;
1664 const char* descr;
1665};
1666
1667/*
1668 * Map commands to functions.
1669 *
1670 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1671 * and 128-256 are vendor-defined.
1672 */
1673static const JdwpHandlerMap gHandlerMap[] = {
1674 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001675 { 1, 1, VM_Version, "VirtualMachine.Version" },
1676 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1677 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1678 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1679 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1680 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1681 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1682 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1683 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1684 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1685 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1686 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1687 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1688 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1689 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1690 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
1691 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1692 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1693 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001694 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001695 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001696
1697 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001698 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1699 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1700 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1701 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1702 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1703 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1704 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
1705 { 2, 8, NULL, "ReferenceType.NestedTypes" },
1706 { 2, 9, RT_Status, "ReferenceType.Status" },
1707 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1708 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001709 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1710 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001711 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1712 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughes3b78c942013-01-15 17:35:41 -08001713 { 2, 16, RT_Instances, "ReferenceType.Instances" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001714 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1715 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001716
1717 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001718 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1719 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1720 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1721 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001722
1723 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001724 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001725
1726 /* InterfaceType command set (5) */
1727
1728 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001729 { 6, 1, M_LineTable, "Method.LineTable" },
1730 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughes9777ba22013-01-17 09:04:19 -08001731 { 6, 3, M_Bytecodes, "Method.Bytecodes" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001732 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001733 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001734
1735 /* Field command set (8) */
1736
1737 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001738 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1739 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1740 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
1741 { 9, 4, NULL, "ObjectReference.UNUSED" },
1742 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1743 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001744 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001745 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1746 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001747 { 9, 10, OR_ReferringObjects, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001748
1749 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001750 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001751
1752 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001753 { 11, 1, TR_Name, "ThreadReference.Name" },
1754 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1755 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1756 { 11, 4, TR_Status, "ThreadReference.Status" },
1757 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1758 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1759 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1760 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1761 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1762 { 11, 10, NULL, "ThreadReference.Stop" },
1763 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1764 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1765 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1766 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001767
1768 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001769 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1770 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1771 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001772
1773 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001774 { 13, 1, AR_Length, "ArrayReference.Length" },
1775 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1776 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001777
1778 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001779 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001780
1781 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001782 { 15, 1, ER_Set, "EventRequest.Set" },
1783 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001784 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001785
1786 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001787 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1788 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1789 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001790 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001791
1792 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001793 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001794
1795 /* Event command set (64) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001796 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001797
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001798 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001799};
1800
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001801static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001802 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001803 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1804 return gHandlerMap[i].descr;
1805 }
1806 }
1807 return "?UNKNOWN?";
1808}
1809
1810static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1811 std::string result;
Elliott Hughes64f574f2013-02-20 14:57:12 -08001812 result += "REQUEST: ";
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001813 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
Elliott Hughes64f574f2013-02-20 14:57:12 -08001814 result += StringPrintf(" (length=%d id=0x%06x)", dataLen, pHeader->id);
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001815 return result;
1816}
1817
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001818/*
1819 * Process a request from the debugger.
1820 *
1821 * On entry, the JDWP thread is in VMWAIT.
1822 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001823void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001824 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001825
1826 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1827 /*
1828 * Activity from a debugger, not merely ddms. Mark us as having an
1829 * active debugger session, and zero out the last-activity timestamp
1830 * so waitForDebugger() doesn't return if we stall for a bit here.
1831 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001832 Dbg::GoActive();
Ian Rogers9adbff52013-01-23 18:19:03 -08001833 QuasiAtomic::Write64(&last_activity_time_ms_, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001834 }
1835
1836 /*
1837 * If a debugger event has fired in another thread, wait until the
1838 * initiating thread has suspended itself before processing messages
1839 * from the debugger. Otherwise we (the JDWP thread) could be told to
1840 * resume the thread before it has suspended.
1841 *
1842 * We call with an argument of zero to wait for the current event
1843 * thread to finish, and then clear the block. Depending on the thread
1844 * suspend policy, this may allow events in other threads to fire,
1845 * but those events have no bearing on what the debugger has sent us
1846 * in the current request.
1847 *
1848 * Note that we MUST clear the event token before waking the event
1849 * thread up, or risk waiting for the thread to suspend after we've
1850 * told it to resume.
1851 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001852 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001853
1854 /*
1855 * Tell the VM that we're running and shouldn't be interrupted by GC.
1856 * Do this after anything that can stall indefinitely.
1857 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001858 Thread* self = Thread::Current();
1859 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001860
1861 expandBufAddSpace(pReply, kJDWPHeaderLen);
1862
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001863 size_t i;
Elliott Hughesa96836a2013-01-17 12:27:49 -08001864 for (i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001865 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001866 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001867 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001868 break;
1869 }
1870 }
1871 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001872 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1873 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001874 result = ERR_NOT_IMPLEMENTED;
1875 }
1876
1877 /*
1878 * Set up the reply header.
1879 *
1880 * If we encountered an error, only send the header back.
1881 */
1882 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001883 Set4BE(replyBuf + 4, pHeader->id);
1884 Set1(replyBuf + 8, kJDWPFlagReply);
1885 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001886 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001887 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001888 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001889 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001890 }
1891
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001892 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes64f574f2013-02-20 14:57:12 -08001893 VLOG(jdwp) << "REPLY: " << GetCommandName(pHeader->cmdSet, pHeader->cmd) << " " << result << " (length=" << respLen << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001894 if (false) {
Elliott Hughes64f574f2013-02-20 14:57:12 -08001895 VLOG(jdwp) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001896 }
1897
1898 /*
1899 * Update last-activity timestamp. We really only need this during
1900 * the initial setup. Only update if this is a non-DDMS packet.
1901 */
1902 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Ian Rogers9adbff52013-01-23 18:19:03 -08001903 QuasiAtomic::Write64(&last_activity_time_ms_, MilliTime());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001904 }
1905
1906 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001907 self->TransitionFromRunnableToSuspended(old_state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001908}
1909
1910} // namespace JDWP
1911
1912} // namespace art