blob: 94aff1cf8a1b367ad703bab3fa0f418cdcb4f824 [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 Hughes07ed66b2012-12-12 18:34:25 -080034#include "atomic.h"
35#include "base/logging.h"
36#include "base/macros.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080037#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080038#include "debugger.h"
39#include "jdwp/jdwp_constants.h"
40#include "jdwp/jdwp_event.h"
41#include "jdwp/jdwp_expand_buf.h"
42#include "jdwp/jdwp_priv.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080043
Elliott Hughes872d4ec2011-10-21 17:07:15 -070044namespace art {
45
46namespace JDWP {
47
48/*
49 * Helper function: read a "location" from an input buffer.
50 */
Elliott Hughes4993bbc2013-01-10 15:41:25 -080051static void ReadLocation(const uint8_t** pBuf, JdwpLocation* pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070052 memset(pLoc, 0, sizeof(*pLoc)); /* allows memcmp() later */
Elliott Hughes74847412012-06-20 18:10:21 -070053 pLoc->type_tag = ReadTypeTag(pBuf);
54 pLoc->class_id = ReadObjectId(pBuf);
55 pLoc->method_id = ReadMethodId(pBuf);
Elliott Hughes972a47b2012-02-21 18:16:06 -080056 pLoc->dex_pc = Read8BE(pBuf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070057}
58
59/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -070060 * Helper function: read a variable-width value from the input buffer.
61 */
Elliott Hughes4993bbc2013-01-10 15:41:25 -080062static uint64_t ReadValue(const uint8_t** pBuf, size_t width) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070063 uint64_t value = -1;
64 switch (width) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070065 case 1: value = Read1(pBuf); break;
66 case 2: value = Read2BE(pBuf); break;
67 case 4: value = Read4BE(pBuf); break;
68 case 8: value = Read8BE(pBuf); break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070069 default: LOG(FATAL) << width; break;
70 }
71 return value;
72}
73
74/*
75 * Helper function: write a variable-width value into the output input buffer.
76 */
Elliott Hughes4993bbc2013-01-10 15:41:25 -080077static void WriteValue(ExpandBuf* pReply, int width, uint64_t value) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070078 switch (width) {
79 case 1: expandBufAdd1(pReply, value); break;
80 case 2: expandBufAdd2BE(pReply, value); break;
81 case 4: expandBufAdd4BE(pReply, value); break;
82 case 8: expandBufAdd8BE(pReply, value); break;
83 default: LOG(FATAL) << width; break;
84 }
85}
86
Elliott Hughes4993bbc2013-01-10 15:41:25 -080087static JdwpError WriteTaggedObject(ExpandBuf* reply, ObjectId object_id)
88 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
89 uint8_t tag;
90 JdwpError rc = Dbg::GetObjectTag(object_id, tag);
91 if (rc == ERR_NONE) {
92 expandBufAdd1(reply, tag);
93 expandBufAddObjectId(reply, object_id);
94 }
95 return rc;
96}
97
Elliott Hughes872d4ec2011-10-21 17:07:15 -070098/*
99 * Common code for *_InvokeMethod requests.
100 *
Elliott Hughes74847412012-06-20 18:10:21 -0700101 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700102 * expected-to-be-void return value of the called function.
103 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700104static JdwpError FinishInvoke(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
Elliott Hughes74847412012-06-20 18:10:21 -0700105 ObjectId thread_id, ObjectId object_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 RefTypeId class_id, MethodId method_id, bool is_constructor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700108 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700109
Elliott Hughes45651fd2012-02-21 15:48:20 -0800110 uint32_t arg_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700111
Elliott Hughes74847412012-06-20 18:10:21 -0700112 VLOG(jdwp) << StringPrintf(" --> thread_id=%#llx object_id=%#llx", thread_id, object_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700113 VLOG(jdwp) << StringPrintf(" class_id=%#llx method_id=%x %s.%s", class_id,
114 method_id, Dbg::GetClassName(class_id).c_str(),
115 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800116 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700117
Elliott Hughes45651fd2012-02-21 15:48:20 -0800118 UniquePtr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
119 UniquePtr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
120 for (uint32_t i = 0; i < arg_count; ++i) {
121 argTypes[i] = ReadTag(&buf);
122 size_t width = Dbg::GetTagWidth(argTypes[i]);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800123 argValues[i] = ReadValue(&buf, width);
Elliott Hughes229feb72012-02-23 13:33:29 -0800124 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#llx", width, argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125 }
126
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700127 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
129 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
130 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700131
Elliott Hughes45651fd2012-02-21 15:48:20 -0800132 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700133 uint64_t resultValue;
134 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700135 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 -0700136 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800137 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700138 }
139
140 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800141 if (is_constructor) {
142 // If we invoked a constructor (which actually returns void), return the receiver,
143 // unless we threw, in which case we return NULL.
144 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700145 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800146 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700147
Elliott Hughes45651fd2012-02-21 15:48:20 -0800148 size_t width = Dbg::GetTagWidth(resultTag);
149 expandBufAdd1(pReply, resultTag);
150 if (width != 0) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800151 WriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152 }
153 expandBufAdd1(pReply, JT_OBJECT);
154 expandBufAddObjectId(pReply, exceptObjId);
155
Elliott Hughes229feb72012-02-23 13:33:29 -0800156 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" %#llx (except=%#llx)", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700157
158 /* show detailed debug output */
159 if (resultTag == JT_STRING && exceptObjId == 0) {
160 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800161 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700162 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800163 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700164 }
165 }
166 }
167
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700168 return err;
169}
170
171
172/*
173 * Request for version info.
174 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175static JdwpError VM_Version(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700177 /* text information on runtime version */
178 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800179 expandBufAddUtf8String(pReply, version);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700180 /* JDWP version numbers */
181 expandBufAdd4BE(pReply, 1); // major
182 expandBufAdd4BE(pReply, 5); // minor
183 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800184 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700185 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800186 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700187
188 return ERR_NONE;
189}
190
191/*
192 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
193 * referenceTypeID. We need to send back more than one if the class has
194 * been loaded by multiple class loaders.
195 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700196static JdwpError VM_ClassesBySignature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800198 std::string classDescriptor(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800199 VLOG(jdwp) << " Req for class by signature '" << classDescriptor << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700200
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800201 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800202 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700203
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800204 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800206 for (size_t i = 0; i < ids.size(); ++i) {
207 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800208 JDWP::JdwpTypeTag type_tag;
209 uint32_t class_status;
210 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
211 if (status != ERR_NONE) {
212 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800213 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700214
Elliott Hughes436e3722012-02-17 20:01:47 -0800215 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800216 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800217 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700218 }
219
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700220 return ERR_NONE;
221}
222
223/*
224 * Handle request for the thread IDs of all running threads.
225 *
226 * We exclude ourselves from the list, because we don't allow ourselves
227 * to be suspended, and that violates some JDWP expectations.
228 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229static JdwpError VM_AllThreads(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700231 std::vector<ObjectId> thread_ids;
Elliott Hughes026b1462012-06-28 20:43:49 -0700232 Dbg::GetThreads(0, thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700233
Elliott Hughescaf76542012-06-28 16:08:22 -0700234 expandBufAdd4BE(pReply, thread_ids.size());
235 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
236 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700237 }
238
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700239 return ERR_NONE;
240}
241
242/*
243 * List all thread groups that do not have a parent.
244 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245static JdwpError VM_TopLevelThreadGroups(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700247 /*
248 * TODO: maintain a list of parentless thread groups in the VM.
249 *
250 * For now, just return "system". Application threads are created
251 * in "main", which is a child of "system".
252 */
253 uint32_t groups = 1;
254 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700255 //thread_group_id = debugGetMainThreadGroup();
256 //expandBufAdd8BE(pReply, thread_group_id);
257 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
258 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700259
260 return ERR_NONE;
261}
262
263/*
264 * Respond with the sizes of the basic debugger types.
265 *
266 * All IDs are 8 bytes.
267 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268static JdwpError VM_IDSizes(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700269 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700270 expandBufAdd4BE(pReply, sizeof(FieldId));
271 expandBufAdd4BE(pReply, sizeof(MethodId));
272 expandBufAdd4BE(pReply, sizeof(ObjectId));
273 expandBufAdd4BE(pReply, sizeof(RefTypeId));
274 expandBufAdd4BE(pReply, sizeof(FrameId));
275 return ERR_NONE;
276}
277
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278static JdwpError VM_Dispose(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes86964332012-02-15 19:37:42 -0800280 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700281 return ERR_NONE;
282}
283
284/*
285 * Suspend the execution of the application running in the VM (i.e. suspend
286 * all threads).
287 *
288 * This needs to increment the "suspend count" on all threads.
289 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290static JdwpError VM_Suspend(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700291 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800292 Thread* self = Thread::Current();
293 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700294 Dbg::SuspendVM();
jeffhaoa77f0f62012-12-05 17:19:31 -0800295 self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700296 return ERR_NONE;
297}
298
299/*
300 * Resume execution. Decrements the "suspend count" of all threads.
301 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302static JdwpError VM_Resume(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700303 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700304 Dbg::ResumeVM();
305 return ERR_NONE;
306}
307
308/*
309 * The debugger wants the entire VM to exit.
310 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311static JdwpError VM_Exit(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700312 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700313 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700314
315 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
316
317 Dbg::Exit(exitCode);
318 return ERR_NOT_IMPLEMENTED; // shouldn't get here
319}
320
321/*
322 * Create a new string in the VM and return its ID.
323 *
324 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
325 * string "java.util.Arrays".)
326 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700327static JdwpError VM_CreateString(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800329 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800330 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700331 ObjectId stringId = Dbg::CreateString(str);
332 if (stringId == 0) {
333 return ERR_OUT_OF_MEMORY;
334 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700335 expandBufAddObjectId(pReply, stringId);
336 return ERR_NONE;
337}
338
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339static JdwpError VM_ClassPaths(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700340 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800341 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700342
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800343 std::vector<std::string> class_path;
344 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
345 expandBufAdd4BE(pReply, class_path.size());
346 for (size_t i = 0; i < class_path.size(); ++i) {
347 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700348 }
349
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800350 std::vector<std::string> boot_class_path;
351 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
352 expandBufAdd4BE(pReply, boot_class_path.size());
353 for (size_t i = 0; i < boot_class_path.size(); ++i) {
354 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700355 }
356
357 return ERR_NONE;
358}
359
360/*
361 * Release a list of object IDs. (Seen in jdb.)
362 *
363 * Currently does nothing.
364 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700365static JdwpError VM_DisposeObjects(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700366 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700367 return ERR_NONE;
368}
369
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800370static JdwpError VM_Capabilities(JdwpState*, const uint8_t*, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700371 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800372 expandBufAdd1(reply, false); // canWatchFieldModification
373 expandBufAdd1(reply, false); // canWatchFieldAccess
374 expandBufAdd1(reply, false); // canGetBytecodes
375 expandBufAdd1(reply, true); // canGetSyntheticAttribute
376 expandBufAdd1(reply, true); // canGetOwnedMonitorInfo
Elliott Hughesf9501702013-01-11 11:22:27 -0800377 expandBufAdd1(reply, true); // canGetCurrentContendedMonitor
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800378 expandBufAdd1(reply, true); // canGetMonitorInfo
379 return ERR_NONE;
380}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700381
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800382static JdwpError VM_CapabilitiesNew(JdwpState*, const uint8_t*, int, ExpandBuf* reply)
383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
384
385 // The first few capabilities are the same as those reported by the older call.
386 VM_Capabilities(NULL, NULL, 0, reply);
387
388 expandBufAdd1(reply, false); // canRedefineClasses
389 expandBufAdd1(reply, false); // canAddMethod
390 expandBufAdd1(reply, false); // canUnrestrictedlyRedefineClasses
391 expandBufAdd1(reply, false); // canPopFrames
392 expandBufAdd1(reply, false); // canUseInstanceFilters
393 expandBufAdd1(reply, false); // canGetSourceDebugExtension
394 expandBufAdd1(reply, false); // canRequestVMDeathEvent
395 expandBufAdd1(reply, false); // canSetDefaultStratum
396 expandBufAdd1(reply, false); // 1.6: canGetInstanceInfo
397 expandBufAdd1(reply, false); // 1.6: canRequestMonitorEvents
Elliott Hughes734b8c62013-01-11 15:32:45 -0800398 expandBufAdd1(reply, true); // 1.6: canGetMonitorFrameInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800399 expandBufAdd1(reply, false); // 1.6: canUseSourceNameFilters
400 expandBufAdd1(reply, false); // 1.6: canGetConstantPool
401 expandBufAdd1(reply, false); // 1.6: canForceEarlyReturn
402
403 // Fill in reserved22 through reserved32; note count started at 1.
404 for (size_t i = 22; i <= 32; ++i) {
405 expandBufAdd1(reply, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700406 }
407 return ERR_NONE;
408}
409
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700410static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700411 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800412 std::vector<JDWP::RefTypeId> classes;
413 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700414
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800415 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700416
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800417 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800418 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800419 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800420 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800421 uint32_t class_status;
422 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
423 if (status != ERR_NONE) {
424 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800425 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700426
Elliott Hughes436e3722012-02-17 20:01:47 -0800427 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800428 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800429 if (descriptor_and_status) {
430 expandBufAddUtf8String(pReply, descriptor);
431 if (generic) {
432 expandBufAddUtf8String(pReply, genericSignature);
433 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800434 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800435 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700436 }
437
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700438 return ERR_NONE;
439}
440
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700441static JdwpError VM_AllClasses(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700442 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700443 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800444}
445
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700446static JdwpError VM_AllClassesWithGeneric(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700447 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700448 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800449}
450
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700451static JdwpError RT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700452 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700453 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800454 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700455}
456
457/*
458 * Get values from static fields in a reference type.
459 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700460static JdwpError RT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800462 RefTypeId refTypeId = ReadRefTypeId(&buf);
463 uint32_t field_count = Read4BE(&buf);
464 expandBufAdd4BE(pReply, field_count);
465 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700466 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800467 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800468 if (status != ERR_NONE) {
469 return status;
470 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700471 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700472 return ERR_NONE;
473}
474
475/*
476 * Get the name of the source file in which a reference type was declared.
477 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700478static JdwpError RT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700480 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800481 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800482 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
483 if (status != ERR_NONE) {
484 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700485 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800486 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800487 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700488}
489
490/*
491 * Return the current status of the reference type.
492 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700493static JdwpError RT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700494 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700495 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800496 JDWP::JdwpTypeTag type_tag;
497 uint32_t class_status;
498 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
499 if (status != ERR_NONE) {
500 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800501 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800502 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700503 return ERR_NONE;
504}
505
506/*
507 * Return interfaces implemented directly by this class.
508 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700509static JdwpError RT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700510 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700511 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800512 VLOG(jdwp) << StringPrintf(" Req for interfaces in %#llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800513 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700514}
515
516/*
517 * Return the class object corresponding to this type.
518 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700519static JdwpError RT_ClassObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700520 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700521 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800522 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800523 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
524 if (status != ERR_NONE) {
525 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800526 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800527 VLOG(jdwp) << StringPrintf(" RefTypeId %#llx -> ObjectId %#llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800528 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700529 return ERR_NONE;
530}
531
532/*
533 * Returns the value of the SourceDebugExtension attribute.
534 *
535 * JDB seems interested, but DEX files don't currently support this.
536 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700537static JdwpError RT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700538 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700539 /* referenceTypeId in, string out */
540 return ERR_ABSENT_INFORMATION;
541}
542
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700543static JdwpError RT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
544 bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700545 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700546 RefTypeId refTypeId = ReadRefTypeId(&buf);
547
Elliott Hughes229feb72012-02-23 13:33:29 -0800548 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=%#llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800549 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800550
551 JdwpError status = Dbg::GetSignature(refTypeId, signature);
552 if (status != ERR_NONE) {
553 return status;
554 }
555 expandBufAddUtf8String(pReply, signature);
556 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800557 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700558 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700559 return ERR_NONE;
560}
561
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700562static JdwpError RT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700563 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700564 return RT_Signature(state, buf, dataLen, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800565}
566
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700567static JdwpError RT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
568 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700569 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700570 return RT_Signature(state, buf, dataLen, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800571}
572
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700573/*
574 * Return the instance of java.lang.ClassLoader that loaded the specified
575 * reference type, or null if it was loaded by the system loader.
576 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700577static JdwpError RT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700578 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700579 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800580 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700581}
582
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700583static std::string Describe(const RefTypeId& refTypeId)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700584 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800585 std::string signature("unknown");
586 Dbg::GetSignature(refTypeId, signature);
Elliott Hughes229feb72012-02-23 13:33:29 -0800587 return StringPrintf("refTypeId=%#llx (%s)", refTypeId, signature.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800588}
589
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700590/*
591 * Given a referenceTypeId, return a block of stuff that describes the
592 * fields declared by a class.
593 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700594static JdwpError RT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700595 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700596 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800597 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800598 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800599}
600
601// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700602static JdwpError RT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700603 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800604 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800605 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800606 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700607}
608
609/*
610 * Given a referenceTypeID, return a block of goodies describing the
611 * methods declared by a class.
612 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613static JdwpError RT_MethodsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700614 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700615 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800616 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800617 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800618}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700619
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800620// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700621static JdwpError RT_Methods(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700622 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800623 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800624 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800625 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700626}
627
628/*
629 * Return the immediate superclass of a class.
630 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700631static JdwpError CT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700632 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700633 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800634 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700635 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800636 if (status != ERR_NONE) {
637 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800638 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700639 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700640 return ERR_NONE;
641}
642
643/*
644 * Set static class values.
645 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700646static JdwpError CT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700647 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700648 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700649 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700650
Elliott Hughes74847412012-06-20 18:10:21 -0700651 VLOG(jdwp) << StringPrintf(" Req to set %d values in class_id=%#llx", values, class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700652
653 for (uint32_t i = 0; i < values; i++) {
654 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800655 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800656 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800657 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700658
Elliott Hughes2435a572012-02-17 16:07:41 -0800659 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800660 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
661 if (status != ERR_NONE) {
662 return status;
663 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700664 }
665
666 return ERR_NONE;
667}
668
669/*
670 * Invoke a static method.
671 *
672 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
673 * values in the "variables" display.
674 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700675static JdwpError CT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
676 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700677 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700678 RefTypeId class_id = ReadRefTypeId(&buf);
679 ObjectId thread_id = ReadObjectId(&buf);
680 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700681
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700682 return FinishInvoke(state, buf, dataLen, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700683}
684
685/*
686 * Create a new object of the requested type, and invoke the specified
687 * constructor.
688 *
689 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
690 * see the contents of a byte[] as a string.
691 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700692static JdwpError CT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen,
693 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700694 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700695 RefTypeId class_id = ReadRefTypeId(&buf);
696 ObjectId thread_id = ReadObjectId(&buf);
697 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700698
Elliott Hughes74847412012-06-20 18:10:21 -0700699 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(class_id);
700 ObjectId object_id;
701 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800702 if (status != ERR_NONE) {
703 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800704 }
Elliott Hughes74847412012-06-20 18:10:21 -0700705 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700706 return ERR_OUT_OF_MEMORY;
707 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700708 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700709}
710
711/*
712 * Create a new array object of the requested type and length.
713 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714static JdwpError AT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700715 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700716 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700717 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700718
Elliott Hughes2435a572012-02-17 16:07:41 -0800719 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes74847412012-06-20 18:10:21 -0700720 ObjectId object_id;
721 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800722 if (status != ERR_NONE) {
723 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800724 }
Elliott Hughes74847412012-06-20 18:10:21 -0700725 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700726 return ERR_OUT_OF_MEMORY;
727 }
728 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700729 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700730 return ERR_NONE;
731}
732
733/*
734 * Return line number information for the method, if present.
735 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700736static JdwpError M_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700737 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700738 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700739 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700740
Elliott Hughes74847412012-06-20 18:10:21 -0700741 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId, method_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742
Elliott Hughes74847412012-06-20 18:10:21 -0700743 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700744
745 return ERR_NONE;
746}
747
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700748static JdwpError M_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
749 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700750 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700751 RefTypeId class_id = ReadRefTypeId(&buf);
752 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700753
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 VLOG(jdwp) << StringPrintf(" Req for LocalVarTab in class=%s method=%s",
755 Dbg::GetClassName(class_id).c_str(),
756 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700757
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800758 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
759 // information. That will cause Eclipse to make a best-effort attempt at displaying local
760 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
761 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700762 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700763 return ERR_NONE;
764}
765
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766static JdwpError M_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen,
767 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700768 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700769 return M_VariableTable(state, buf, dataLen, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800770}
771
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772static JdwpError M_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
773 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700774 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700775 return M_VariableTable(state, buf, dataLen, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800776}
777
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700778/*
779 * Given an object reference, return the runtime type of the object
780 * (class or array).
781 *
Elliott Hughes74847412012-06-20 18:10:21 -0700782 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700783 * passed in here.
784 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700785static JdwpError OR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700786 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700787 ObjectId object_id = ReadObjectId(&buf);
788 VLOG(jdwp) << StringPrintf(" Req for type of object_id=%#llx", object_id);
789 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700790}
791
792/*
793 * Get values from the fields of an object.
794 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700795static JdwpError OR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700796 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700797 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800798 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700799
Elliott Hughes74847412012-06-20 18:10:21 -0700800 VLOG(jdwp) << StringPrintf(" Req for %d fields from object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801
Elliott Hughes0cf74332012-02-23 23:14:00 -0800802 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700803
Elliott Hughes0cf74332012-02-23 23:14:00 -0800804 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700805 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700806 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800807 if (status != ERR_NONE) {
808 return status;
809 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700810 }
811
812 return ERR_NONE;
813}
814
815/*
816 * Set values in the fields of an object.
817 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818static JdwpError OR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700819 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700820 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800821 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700822
Elliott Hughes74847412012-06-20 18:10:21 -0700823 VLOG(jdwp) << StringPrintf(" Req to set %d fields in object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700824
Elliott Hughes0cf74332012-02-23 23:14:00 -0800825 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700826 FieldId fieldId = ReadFieldId(&buf);
827
Elliott Hughesaed4be92011-12-02 16:16:23 -0800828 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800829 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800830 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700831
Elliott Hughes2435a572012-02-17 16:07:41 -0800832 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700833 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800834 if (status != ERR_NONE) {
835 return status;
836 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700837 }
838
839 return ERR_NONE;
840}
841
Elliott Hughesf327e072013-01-09 16:01:26 -0800842static JdwpError OR_MonitorInfo(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
843 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
844 ObjectId object_id = ReadObjectId(&buf);
845 return Dbg::GetMonitorInfo(object_id, reply);
846}
847
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700848/*
849 * Invoke an instance method. The invocation must occur in the specified
850 * thread, which must have been suspended by an event.
851 *
852 * The call is synchronous. All threads in the VM are resumed, unless the
853 * SINGLE_THREADED flag is set.
854 *
855 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
856 * object), it will try to invoke the object's toString() function. This
857 * feature becomes crucial when examining ArrayLists with Eclipse.
858 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700859static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
860 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700861 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700862 ObjectId object_id = ReadObjectId(&buf);
863 ObjectId thread_id = ReadObjectId(&buf);
864 RefTypeId class_id = ReadRefTypeId(&buf);
865 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700866
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700867 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700868}
869
870/*
871 * Disable garbage collection of the specified object.
872 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700873static JdwpError OR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700874 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700875 // this is currently a no-op
876 return ERR_NONE;
877}
878
879/*
880 * Enable garbage collection of the specified object.
881 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700882static JdwpError OR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700883 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700884 // this is currently a no-op
885 return ERR_NONE;
886}
887
888/*
889 * Determine whether an object has been garbage collected.
890 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700891static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700892 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700893 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700894
Elliott Hughes74847412012-06-20 18:10:21 -0700895 object_id = ReadObjectId(&buf);
896 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700897
898 // TODO: currently returning false; must integrate with GC
899 expandBufAdd1(pReply, 0);
900
901 return ERR_NONE;
902}
903
904/*
905 * Return the string value in a string object.
906 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700907static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700908 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700909 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800910 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700911
Elliott Hughes82914b62012-04-09 15:56:29 -0700912 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700913
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800914 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700915
916 return ERR_NONE;
917}
918
919/*
920 * Return a thread's name.
921 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700922static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700923 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700924 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700925
Elliott Hughes74847412012-06-20 18:10:21 -0700926 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800927 std::string name;
Elliott Hughes221229c2013-01-08 18:17:50 -0800928 JdwpError error = Dbg::GetThreadName(thread_id, name);
929 if (error != ERR_NONE) {
930 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700931 }
Elliott Hughes74847412012-06-20 18:10:21 -0700932 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800933 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700934
935 return ERR_NONE;
936}
937
938/*
939 * Suspend the specified thread.
940 *
941 * It's supposed to remain suspended even if interpreted code wants to
942 * resume it; only the JDI is allowed to resume it.
943 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700945 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700946 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700947
Elliott Hughes74847412012-06-20 18:10:21 -0700948 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700949 LOG(INFO) << " Warning: ignoring request to suspend self";
950 return ERR_THREAD_NOT_SUSPENDED;
951 }
Elliott Hughes74847412012-06-20 18:10:21 -0700952 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700953 Thread* self = Thread::Current();
954 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
955 JdwpError result = Dbg::SuspendThread(thread_id);
956 self->TransitionFromSuspendedToRunnable();
957 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700958}
959
960/*
961 * Resume the specified thread.
962 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700964 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700965 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700966
Elliott Hughes74847412012-06-20 18:10:21 -0700967 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700968 LOG(INFO) << " Warning: ignoring request to resume self";
969 return ERR_NONE;
970 }
Elliott Hughes74847412012-06-20 18:10:21 -0700971 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
972 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700973 return ERR_NONE;
974}
975
976/*
977 * Return status of specified thread.
978 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700979static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700980 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700981 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700982
Elliott Hughes74847412012-06-20 18:10:21 -0700983 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700984
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800985 JDWP::JdwpThreadStatus threadStatus;
986 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -0800987 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
988 if (error != ERR_NONE) {
989 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700990 }
991
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800992 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700993
994 expandBufAdd4BE(pReply, threadStatus);
995 expandBufAdd4BE(pReply, suspendStatus);
996
997 return ERR_NONE;
998}
999
1000/*
1001 * Return the thread group that the specified thread is a member of.
1002 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001004 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001005 ObjectId thread_id = ReadObjectId(&buf);
1006 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001007}
1008
1009/*
1010 * Return the current call stack of a suspended thread.
1011 *
1012 * If the thread isn't suspended, the error code isn't defined, but should
1013 * be THREAD_NOT_SUSPENDED.
1014 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001015static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001016 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001017 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001018 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001019 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001020
Elliott Hughes221229c2013-01-08 18:17:50 -08001021 size_t actual_frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001022 JdwpError error = Dbg::GetThreadFrameCount(thread_id, actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001023 if (error != ERR_NONE) {
1024 return error;
1025 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001026
Elliott Hughes74847412012-06-20 18:10:21 -07001027 VLOG(jdwp) << StringPrintf(" Request for frames: thread_id=%#llx start=%d length=%d [count=%zd]", thread_id, start_frame, length, actual_frame_count);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001028 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001029 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1030 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001032 if (start_frame > actual_frame_count) {
1033 return ERR_INVALID_INDEX;
1034 }
1035 if (length == static_cast<uint32_t>(-1)) {
1036 length = actual_frame_count - start_frame;
1037 }
1038 if (start_frame + length > actual_frame_count) {
1039 return ERR_INVALID_LENGTH;
1040 }
1041
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001042 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001043}
1044
1045/*
1046 * Returns the #of frames on the specified thread, which must be suspended.
1047 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001049 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001050 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001051
Elliott Hughes221229c2013-01-08 18:17:50 -08001052 size_t frame_count;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001053 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, frame_count);
1054 if (rc != ERR_NONE) {
1055 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001056 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001057 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001058
1059 return ERR_NONE;
1060}
1061
Elliott Hughes734b8c62013-01-11 15:32:45 -08001062static JdwpError TR_OwnedMonitors(const uint8_t* buf, ExpandBuf* reply, bool with_stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001063 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1064 ObjectId thread_id = ReadObjectId(&buf);
1065
1066 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001067 std::vector<uint32_t> stack_depths;
1068 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, monitors, stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001069 if (rc != ERR_NONE) {
1070 return rc;
1071 }
1072
1073 expandBufAdd4BE(reply, monitors.size());
1074 for (size_t i = 0; i < monitors.size(); ++i) {
1075 rc = WriteTaggedObject(reply, monitors[i]);
1076 if (rc != ERR_NONE) {
1077 return rc;
1078 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001079 if (with_stack_depths) {
1080 expandBufAdd4BE(reply, stack_depths[i]);
1081 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001082 }
1083 return ERR_NONE;
1084}
1085
Elliott Hughes734b8c62013-01-11 15:32:45 -08001086static JdwpError TR_OwnedMonitors(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1087 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1088 return TR_OwnedMonitors(buf, reply, false);
1089}
1090
1091static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1093 return TR_OwnedMonitors(buf, reply, true);
1094}
1095
Elliott Hughesf9501702013-01-11 11:22:27 -08001096static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesf9501702013-01-11 11:22:27 -08001098 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001099
Elliott Hughesf9501702013-01-11 11:22:27 -08001100 ObjectId contended_monitor;
1101 JdwpError rc = Dbg::GetContendedMonitor(thread_id, contended_monitor);
1102 if (rc != ERR_NONE) {
1103 return rc;
1104 }
1105 return WriteTaggedObject(reply, contended_monitor);
1106}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001107
Elliott Hughesf9501702013-01-11 11:22:27 -08001108static JdwpError TR_Interrupt(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1110 ObjectId thread_id = ReadObjectId(&buf);
1111 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001112}
1113
1114/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001116 *
1117 * (The thread *might* still be running -- it might not have examined
1118 * its suspend count recently.)
1119 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001120static JdwpError TR_DebugSuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001122 ObjectId thread_id = ReadObjectId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001123 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001124}
1125
1126/*
1127 * Return the name of a thread group.
1128 *
1129 * The Eclipse debugger recognizes "main" and "system" as special.
1130 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001131static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001133 ObjectId thread_group_id = ReadObjectId(&buf);
1134 VLOG(jdwp) << StringPrintf(" Req for name of thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001135
Elliott Hughescaf76542012-06-28 16:08:22 -07001136 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001137
1138 return ERR_NONE;
1139}
1140
1141/*
1142 * Returns the thread group -- if any -- that contains the specified
1143 * thread group.
1144 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001145static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001147 ObjectId thread_group_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001148
Elliott Hughescaf76542012-06-28 16:08:22 -07001149 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001150 expandBufAddObjectId(pReply, parentGroup);
1151
1152 return ERR_NONE;
1153}
1154
1155/*
1156 * Return the active threads and thread groups that are part of the
1157 * specified thread group.
1158 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001159static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001160 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001161 ObjectId thread_group_id = ReadObjectId(&buf);
1162 VLOG(jdwp) << StringPrintf(" Req for threads in thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001163
Elliott Hughescaf76542012-06-28 16:08:22 -07001164 std::vector<ObjectId> thread_ids;
1165 Dbg::GetThreads(thread_group_id, thread_ids);
1166 expandBufAdd4BE(pReply, thread_ids.size());
1167 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1168 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001169 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001170
Elliott Hughescaf76542012-06-28 16:08:22 -07001171 std::vector<ObjectId> child_thread_groups_ids;
1172 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1173 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1174 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1175 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001176 }
1177
1178 return ERR_NONE;
1179}
1180
1181/*
1182 * Return the #of components in the array.
1183 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001184static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001186 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001187 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001188
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001189 int length;
1190 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1191 if (status != ERR_NONE) {
1192 return status;
1193 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001194 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001195
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001196 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001197
1198 return ERR_NONE;
1199}
1200
1201/*
1202 * Return the values from an array.
1203 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001204static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001206 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001207 uint32_t firstIndex = Read4BE(&buf);
1208 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001209 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001210
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001211 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001212}
1213
1214/*
1215 * Set values in an array.
1216 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001219 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001220 uint32_t firstIndex = Read4BE(&buf);
1221 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001222
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001223 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId,
1224 firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001225
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001226 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001227}
1228
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001229static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001231 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001232 // TODO: we should only return classes which have the given class loader as a defining or
1233 // initiating loader. The former would be easy; the latter is hard, because we don't have
1234 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001235 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001236}
1237
1238/*
1239 * Set an event trigger.
1240 *
1241 * Reply with a requestID.
1242 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001243static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001245 const uint8_t* origBuf = buf;
1246
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001247 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001248 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001249 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001250
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001251 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001252 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001253 << " mods=" << modifierCount << ")";
1254
1255 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1256
1257 JdwpEvent* pEvent = EventAlloc(modifierCount);
1258 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001259 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001260 pEvent->modCount = modifierCount;
1261
1262 /*
1263 * Read modifiers. Ordering may be significant (see explanation of Count
1264 * mods in JDWP doc).
1265 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001266 for (uint32_t i = 0; i < modifierCount; ++i) {
1267 JdwpEventMod& mod = pEvent->mods[i];
1268 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1269 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001270 case MK_COUNT: /* report once, when "--count" reaches 0 */
1271 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001272 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001273 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001274 if (count == 0) {
1275 return ERR_INVALID_COUNT;
1276 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001277 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001278 }
1279 break;
1280 case MK_CONDITIONAL: /* conditional on expression) */
1281 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001282 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001283 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001284 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001285 }
1286 break;
1287 case MK_THREAD_ONLY: /* only report events in specified thread */
1288 {
Elliott Hughes74847412012-06-20 18:10:21 -07001289 ObjectId thread_id = ReadObjectId(&buf);
1290 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1291 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001292 }
1293 break;
1294 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1295 {
Elliott Hughes74847412012-06-20 18:10:21 -07001296 RefTypeId class_id = ReadRefTypeId(&buf);
1297 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1298 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001299 }
1300 break;
1301 case MK_CLASS_MATCH: /* restrict events to matching classes */
1302 {
Elliott Hughes86964332012-02-15 19:37:42 -08001303 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001304 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001305 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001306 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001307 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001308 }
1309 break;
1310 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1311 {
Elliott Hughes86964332012-02-15 19:37:42 -08001312 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001313 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001314 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001315 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001316 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001317 }
1318 break;
1319 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1320 {
1321 JdwpLocation loc;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001322 ReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001323 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001324 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001325 }
1326 break;
1327 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1328 {
1329 RefTypeId exceptionOrNull; /* null == all exceptions */
1330 uint8_t caught, uncaught;
1331
1332 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001333 caught = Read1(&buf);
1334 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001335 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001336 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001337
Elliott Hughes972a47b2012-02-21 18:16:06 -08001338 mod.exceptionOnly.refTypeId = exceptionOrNull;
1339 mod.exceptionOnly.caught = caught;
1340 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001341 }
1342 break;
1343 case MK_FIELD_ONLY: /* for field access/mod events */
1344 {
1345 RefTypeId declaring = ReadRefTypeId(&buf);
1346 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001347 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001348 mod.fieldOnly.refTypeId = declaring;
1349 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001350 }
1351 break;
1352 case MK_STEP: /* for use with EK_SINGLE_STEP */
1353 {
Elliott Hughes74847412012-06-20 18:10:21 -07001354 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001355 uint32_t size, depth;
1356
Elliott Hughes74847412012-06-20 18:10:21 -07001357 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001358 size = Read4BE(&buf);
1359 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001360 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001361 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1362
Elliott Hughes74847412012-06-20 18:10:21 -07001363 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001364 mod.step.size = size;
1365 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001366 }
1367 break;
1368 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1369 {
1370 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001371 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001372 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001373 }
1374 break;
1375 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001376 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001377 break;
1378 }
1379 }
1380
1381 /*
1382 * Make sure we consumed all data. It is possible that the remote side
1383 * has sent us bad stuff, but for now we blame ourselves.
1384 */
1385 if (buf != origBuf + dataLen) {
1386 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1387 }
1388
1389 /*
1390 * We reply with an integer "requestID".
1391 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001392 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001393 expandBufAdd4BE(pReply, requestId);
1394
1395 pEvent->requestId = requestId;
1396
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001397 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001398
1399 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001400 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001401 if (err != ERR_NONE) {
1402 /* registration failed, probably because event is bogus */
1403 EventFree(pEvent);
1404 LOG(WARNING) << "WARNING: event request rejected";
1405 }
1406 return err;
1407}
1408
1409/*
1410 * Clear an event. Failure to find an event with a matching ID is a no-op
1411 * and does not return an error.
1412 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001413static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001414 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001415 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001416 eventKind = Read1(&buf);
1417 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001418
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001419 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001420
Elliott Hughes761928d2011-11-16 18:33:03 -08001421 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001422
1423 return ERR_NONE;
1424}
1425
1426/*
1427 * Return the values of arguments and local variables.
1428 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001429static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001430 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001431 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001432 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001433 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001434
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001435 VLOG(jdwp) << StringPrintf(" Req for %d slots in thread_id=%#llx frame_id=%lld", slots, thread_id, frame_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001436
1437 expandBufAdd4BE(pReply, slots); /* "int values" */
1438 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001439 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001440 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001441
Elliott Hughes2435a572012-02-17 16:07:41 -08001442 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001443
Elliott Hughesdbb40792011-11-18 17:05:22 -08001444 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001445 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001446 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001447 }
1448
1449 return ERR_NONE;
1450}
1451
1452/*
1453 * Set the values of arguments and local variables.
1454 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001455static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001456 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001457 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001458 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001459 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001460
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001461 VLOG(jdwp) << StringPrintf(" Req to set %d slots in thread_id=%#llx frame_id=%lld", slots, thread_id, frame_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001462
1463 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001464 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001465 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001466 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001467 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001468
Elliott Hughes2435a572012-02-17 16:07:41 -08001469 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001470 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001471 }
1472
1473 return ERR_NONE;
1474}
1475
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001476static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001477 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001478 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001479 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001480
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001481 ObjectId object_id;
1482 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001483 if (rc != ERR_NONE) {
1484 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001485 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001486
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001487 VLOG(jdwp) << StringPrintf(" Req for 'this' in thread_id=%#llx frame=%lld --> %#llx",
1488 thread_id, frame_id, object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001489
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001490 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001491}
1492
1493/*
1494 * Return the reference type reflected by this class object.
1495 *
1496 * This appears to be required because ReferenceTypeId values are NEVER
1497 * reused, whereas ClassIds can be recycled like any other object. (Either
1498 * that, or I have no idea what this is for.)
1499 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001500static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001501 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001502 RefTypeId classObjectId = ReadRefTypeId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001503 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId,
1504 Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001505 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001506}
1507
1508/*
1509 * Handle a DDM packet with a single chunk in it.
1510 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001511static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001512 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001513 uint8_t* replyBuf = NULL;
1514 int replyLen = -1;
1515
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001516 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001517
Elliott Hughesa21039c2012-06-21 12:09:25 -07001518 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001519
1520 /*
1521 * If they want to send something back, we copy it into the buffer.
1522 * A no-copy approach would be nicer.
1523 *
1524 * TODO: consider altering the JDWP stuff to hold the packet header
1525 * in a separate buffer. That would allow us to writev() DDM traffic
1526 * instead of copying it into the expanding buffer. The reduction in
1527 * heap requirements is probably more valuable than the efficiency.
1528 */
1529 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1530 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1531 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1532 free(replyBuf);
1533 }
1534 return ERR_NONE;
1535}
1536
1537/*
1538 * Handler map decl.
1539 */
1540typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1541
1542struct JdwpHandlerMap {
1543 uint8_t cmdSet;
1544 uint8_t cmd;
1545 JdwpRequestHandler func;
1546 const char* descr;
1547};
1548
1549/*
1550 * Map commands to functions.
1551 *
1552 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1553 * and 128-256 are vendor-defined.
1554 */
1555static const JdwpHandlerMap gHandlerMap[] = {
1556 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001557 { 1, 1, VM_Version, "VirtualMachine.Version" },
1558 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1559 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1560 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1561 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1562 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1563 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1564 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1565 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1566 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1567 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1568 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1569 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1570 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1571 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1572 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
1573 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1574 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1575 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001576 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001577 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001578
1579 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001580 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1581 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1582 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1583 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1584 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1585 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1586 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
1587 { 2, 8, NULL, "ReferenceType.NestedTypes" },
1588 { 2, 9, RT_Status, "ReferenceType.Status" },
1589 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1590 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001591 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1592 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001593 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1594 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
1595 { 2, 16, NULL, "ReferenceType.Instances" },
1596 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1597 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001598
1599 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001600 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1601 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1602 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1603 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001604
1605 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001606 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001607
1608 /* InterfaceType command set (5) */
1609
1610 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001611 { 6, 1, M_LineTable, "Method.LineTable" },
1612 { 6, 2, M_VariableTable, "Method.VariableTable" },
1613 { 6, 3, NULL, "Method.Bytecodes" },
1614 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001615 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001616
1617 /* Field command set (8) */
1618
1619 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001620 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1621 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1622 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
1623 { 9, 4, NULL, "ObjectReference.UNUSED" },
1624 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1625 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001626 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001627 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1628 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
1629 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001630
1631 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001632 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001633
1634 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001635 { 11, 1, TR_Name, "ThreadReference.Name" },
1636 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1637 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1638 { 11, 4, TR_Status, "ThreadReference.Status" },
1639 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1640 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1641 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1642 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1643 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1644 { 11, 10, NULL, "ThreadReference.Stop" },
1645 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1646 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1647 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1648 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001649
1650 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001651 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1652 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1653 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001654
1655 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001656 { 13, 1, AR_Length, "ArrayReference.Length" },
1657 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1658 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001659
1660 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001661 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001662
1663 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001664 { 15, 1, ER_Set, "EventRequest.Set" },
1665 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001666 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001667
1668 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001669 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1670 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1671 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001672 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001673
1674 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001675 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001676
1677 /* Event command set (64) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001678 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001679
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001680 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001681};
1682
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001683static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001684 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001685 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1686 return gHandlerMap[i].descr;
1687 }
1688 }
1689 return "?UNKNOWN?";
1690}
1691
1692static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1693 std::string result;
1694 result += "REQ: ";
1695 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1696 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1697 return result;
1698}
1699
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001700/*
1701 * Process a request from the debugger.
1702 *
1703 * On entry, the JDWP thread is in VMWAIT.
1704 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001705void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001706 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001707
1708 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1709 /*
1710 * Activity from a debugger, not merely ddms. Mark us as having an
1711 * active debugger session, and zero out the last-activity timestamp
1712 * so waitForDebugger() doesn't return if we stall for a bit here.
1713 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001714 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001715 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001716 }
1717
1718 /*
1719 * If a debugger event has fired in another thread, wait until the
1720 * initiating thread has suspended itself before processing messages
1721 * from the debugger. Otherwise we (the JDWP thread) could be told to
1722 * resume the thread before it has suspended.
1723 *
1724 * We call with an argument of zero to wait for the current event
1725 * thread to finish, and then clear the block. Depending on the thread
1726 * suspend policy, this may allow events in other threads to fire,
1727 * but those events have no bearing on what the debugger has sent us
1728 * in the current request.
1729 *
1730 * Note that we MUST clear the event token before waking the event
1731 * thread up, or risk waiting for the thread to suspend after we've
1732 * told it to resume.
1733 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001734 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001735
1736 /*
1737 * Tell the VM that we're running and shouldn't be interrupted by GC.
1738 * Do this after anything that can stall indefinitely.
1739 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001740 Thread* self = Thread::Current();
1741 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001742
1743 expandBufAddSpace(pReply, kJDWPHeaderLen);
1744
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001745 size_t i;
1746 for (i = 0; i < arraysize(gHandlerMap); i++) {
1747 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001748 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001749 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001750 break;
1751 }
1752 }
1753 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001754 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1755 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001756 result = ERR_NOT_IMPLEMENTED;
1757 }
1758
1759 /*
1760 * Set up the reply header.
1761 *
1762 * If we encountered an error, only send the header back.
1763 */
1764 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001765 Set4BE(replyBuf + 4, pHeader->id);
1766 Set1(replyBuf + 8, kJDWPFlagReply);
1767 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001768 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001769 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001770 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001771 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001772 }
1773
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001774 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001775 if (false) {
1776 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001777 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001778 }
1779
1780 /*
1781 * Update last-activity timestamp. We really only need this during
1782 * the initial setup. Only update if this is a non-DDMS packet.
1783 */
1784 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001785 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001786 }
1787
1788 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 self->TransitionFromRunnableToSuspended(old_state);
1790
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001791}
1792
1793} // namespace JDWP
1794
1795} // namespace art