blob: f68e5600c713e87c8f73d820bbd24aa45bce78da [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
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800396 expandBufAdd1(reply, true); // 1.6: canGetInstanceInfo
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800397 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
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800451static JdwpError VM_InstanceCounts(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
452 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
453 int32_t class_count = static_cast<int32_t>(Read4BE(&buf));
454 if (class_count < 0) {
455 return ERR_ILLEGAL_ARGUMENT;
456 }
457 std::vector<RefTypeId> class_ids;
458 for (int32_t i = 0; i < class_count; ++i) {
459 class_ids.push_back(ReadRefTypeId(&buf));
460 }
461
462 std::vector<uint64_t> counts;
463 JdwpError rc = Dbg::GetInstanceCounts(class_ids, counts);
464 if (rc != ERR_NONE) {
465 return rc;
466 }
467
468 expandBufAdd4BE(pReply, counts.size());
469 for (size_t i = 0; i < counts.size(); ++i) {
470 expandBufAdd8BE(pReply, counts[i]);
471 }
472 return ERR_NONE;
473}
474
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700475static JdwpError RT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700476 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700477 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800478 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700479}
480
481/*
482 * Get values from static fields in a reference type.
483 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700484static JdwpError RT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800486 RefTypeId refTypeId = ReadRefTypeId(&buf);
487 uint32_t field_count = Read4BE(&buf);
488 expandBufAdd4BE(pReply, field_count);
489 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700490 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800491 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800492 if (status != ERR_NONE) {
493 return status;
494 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700495 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700496 return ERR_NONE;
497}
498
499/*
500 * Get the name of the source file in which a reference type was declared.
501 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700502static JdwpError RT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700503 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700504 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800505 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800506 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
507 if (status != ERR_NONE) {
508 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700509 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800510 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800511 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700512}
513
514/*
515 * Return the current status of the reference type.
516 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700517static JdwpError RT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700519 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800520 JDWP::JdwpTypeTag type_tag;
521 uint32_t class_status;
522 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
523 if (status != ERR_NONE) {
524 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800525 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800526 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700527 return ERR_NONE;
528}
529
530/*
531 * Return interfaces implemented directly by this class.
532 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700533static JdwpError RT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700534 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700535 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800536 VLOG(jdwp) << StringPrintf(" Req for interfaces in %#llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800537 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700538}
539
540/*
541 * Return the class object corresponding to this type.
542 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700543static JdwpError RT_ClassObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700544 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700545 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800546 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800547 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
548 if (status != ERR_NONE) {
549 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800550 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800551 VLOG(jdwp) << StringPrintf(" RefTypeId %#llx -> ObjectId %#llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800552 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700553 return ERR_NONE;
554}
555
556/*
557 * Returns the value of the SourceDebugExtension attribute.
558 *
559 * JDB seems interested, but DEX files don't currently support this.
560 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700561static JdwpError RT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700562 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700563 /* referenceTypeId in, string out */
564 return ERR_ABSENT_INFORMATION;
565}
566
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700567static JdwpError RT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
568 bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700569 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700570 RefTypeId refTypeId = ReadRefTypeId(&buf);
571
Elliott Hughes229feb72012-02-23 13:33:29 -0800572 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=%#llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800573 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800574
575 JdwpError status = Dbg::GetSignature(refTypeId, signature);
576 if (status != ERR_NONE) {
577 return status;
578 }
579 expandBufAddUtf8String(pReply, signature);
580 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800581 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700582 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700583 return ERR_NONE;
584}
585
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700586static JdwpError RT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700587 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700588 return RT_Signature(state, buf, dataLen, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800589}
590
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700591static JdwpError RT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
592 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700593 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700594 return RT_Signature(state, buf, dataLen, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800595}
596
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700597/*
598 * Return the instance of java.lang.ClassLoader that loaded the specified
599 * reference type, or null if it was loaded by the system loader.
600 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700601static JdwpError RT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700602 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700603 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800604 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700605}
606
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607static std::string Describe(const RefTypeId& refTypeId)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700608 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800609 std::string signature("unknown");
610 Dbg::GetSignature(refTypeId, signature);
Elliott Hughes229feb72012-02-23 13:33:29 -0800611 return StringPrintf("refTypeId=%#llx (%s)", refTypeId, signature.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800612}
613
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700614/*
615 * Given a referenceTypeId, return a block of stuff that describes the
616 * fields declared by a class.
617 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700618static JdwpError RT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700619 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700620 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800621 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800622 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800623}
624
625// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626static JdwpError RT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700627 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800628 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800629 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800630 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700631}
632
633/*
634 * Given a referenceTypeID, return a block of goodies describing the
635 * methods declared by a class.
636 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700637static JdwpError RT_MethodsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700638 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700639 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800640 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800641 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800642}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700643
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800644// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700645static JdwpError RT_Methods(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700646 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800647 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800648 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800649 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700650}
651
652/*
653 * Return the immediate superclass of a class.
654 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655static JdwpError CT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700656 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700657 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800658 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700659 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800660 if (status != ERR_NONE) {
661 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800662 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700663 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700664 return ERR_NONE;
665}
666
667/*
668 * Set static class values.
669 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670static JdwpError CT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700671 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700672 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700673 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700674
Elliott Hughes74847412012-06-20 18:10:21 -0700675 VLOG(jdwp) << StringPrintf(" Req to set %d values in class_id=%#llx", values, class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700676
677 for (uint32_t i = 0; i < values; i++) {
678 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800679 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800680 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800681 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700682
Elliott Hughes2435a572012-02-17 16:07:41 -0800683 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800684 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
685 if (status != ERR_NONE) {
686 return status;
687 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700688 }
689
690 return ERR_NONE;
691}
692
693/*
694 * Invoke a static method.
695 *
696 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
697 * values in the "variables" display.
698 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700699static JdwpError CT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
700 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700701 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700702 RefTypeId class_id = ReadRefTypeId(&buf);
703 ObjectId thread_id = ReadObjectId(&buf);
704 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700705
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700706 return FinishInvoke(state, buf, dataLen, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700707}
708
709/*
710 * Create a new object of the requested type, and invoke the specified
711 * constructor.
712 *
713 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
714 * see the contents of a byte[] as a string.
715 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716static JdwpError CT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen,
717 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700718 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700719 RefTypeId class_id = ReadRefTypeId(&buf);
720 ObjectId thread_id = ReadObjectId(&buf);
721 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700722
Elliott Hughes74847412012-06-20 18:10:21 -0700723 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(class_id);
724 ObjectId object_id;
725 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800726 if (status != ERR_NONE) {
727 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800728 }
Elliott Hughes74847412012-06-20 18:10:21 -0700729 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700730 return ERR_OUT_OF_MEMORY;
731 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700732 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700733}
734
735/*
736 * Create a new array object of the requested type and length.
737 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700738static JdwpError AT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700739 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700740 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700741 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742
Elliott Hughes2435a572012-02-17 16:07:41 -0800743 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes74847412012-06-20 18:10:21 -0700744 ObjectId object_id;
745 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800746 if (status != ERR_NONE) {
747 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800748 }
Elliott Hughes74847412012-06-20 18:10:21 -0700749 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700750 return ERR_OUT_OF_MEMORY;
751 }
752 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700753 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700754 return ERR_NONE;
755}
756
757/*
758 * Return line number information for the method, if present.
759 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700760static JdwpError M_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700761 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700762 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700763 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700764
Elliott Hughes74847412012-06-20 18:10:21 -0700765 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId, method_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700766
Elliott Hughes74847412012-06-20 18:10:21 -0700767 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700768
769 return ERR_NONE;
770}
771
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772static JdwpError M_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
773 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700774 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700775 RefTypeId class_id = ReadRefTypeId(&buf);
776 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700777
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700778 VLOG(jdwp) << StringPrintf(" Req for LocalVarTab in class=%s method=%s",
779 Dbg::GetClassName(class_id).c_str(),
780 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700781
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800782 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
783 // information. That will cause Eclipse to make a best-effort attempt at displaying local
784 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
785 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700786 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700787 return ERR_NONE;
788}
789
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700790static JdwpError M_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen,
791 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700792 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700793 return M_VariableTable(state, buf, dataLen, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800794}
795
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700796static JdwpError M_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
797 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700798 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700799 return M_VariableTable(state, buf, dataLen, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800800}
801
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700802/*
803 * Given an object reference, return the runtime type of the object
804 * (class or array).
805 *
Elliott Hughes74847412012-06-20 18:10:21 -0700806 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700807 * passed in here.
808 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700809static JdwpError OR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700810 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700811 ObjectId object_id = ReadObjectId(&buf);
812 VLOG(jdwp) << StringPrintf(" Req for type of object_id=%#llx", object_id);
813 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700814}
815
816/*
817 * Get values from the fields of an object.
818 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700819static JdwpError OR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700820 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700821 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800822 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700823
Elliott Hughes74847412012-06-20 18:10:21 -0700824 VLOG(jdwp) << StringPrintf(" Req for %d fields from object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700825
Elliott Hughes0cf74332012-02-23 23:14:00 -0800826 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700827
Elliott Hughes0cf74332012-02-23 23:14:00 -0800828 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700829 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700830 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800831 if (status != ERR_NONE) {
832 return status;
833 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700834 }
835
836 return ERR_NONE;
837}
838
839/*
840 * Set values in the fields of an object.
841 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700842static JdwpError OR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700843 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700844 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800845 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700846
Elliott Hughes74847412012-06-20 18:10:21 -0700847 VLOG(jdwp) << StringPrintf(" Req to set %d fields in object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700848
Elliott Hughes0cf74332012-02-23 23:14:00 -0800849 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700850 FieldId fieldId = ReadFieldId(&buf);
851
Elliott Hughesaed4be92011-12-02 16:16:23 -0800852 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800853 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800854 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700855
Elliott Hughes2435a572012-02-17 16:07:41 -0800856 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700857 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800858 if (status != ERR_NONE) {
859 return status;
860 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700861 }
862
863 return ERR_NONE;
864}
865
Elliott Hughesf327e072013-01-09 16:01:26 -0800866static JdwpError OR_MonitorInfo(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
867 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
868 ObjectId object_id = ReadObjectId(&buf);
869 return Dbg::GetMonitorInfo(object_id, reply);
870}
871
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700872/*
873 * Invoke an instance method. The invocation must occur in the specified
874 * thread, which must have been suspended by an event.
875 *
876 * The call is synchronous. All threads in the VM are resumed, unless the
877 * SINGLE_THREADED flag is set.
878 *
879 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
880 * object), it will try to invoke the object's toString() function. This
881 * feature becomes crucial when examining ArrayLists with Eclipse.
882 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700883static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
884 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700885 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700886 ObjectId object_id = ReadObjectId(&buf);
887 ObjectId thread_id = ReadObjectId(&buf);
888 RefTypeId class_id = ReadRefTypeId(&buf);
889 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700890
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700891 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700892}
893
894/*
895 * Disable garbage collection of the specified object.
896 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700897static JdwpError OR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700898 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700899 // this is currently a no-op
900 return ERR_NONE;
901}
902
903/*
904 * Enable garbage collection of the specified object.
905 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700906static JdwpError OR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700907 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700908 // this is currently a no-op
909 return ERR_NONE;
910}
911
912/*
913 * Determine whether an object has been garbage collected.
914 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700915static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700916 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700917 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700918
Elliott Hughes74847412012-06-20 18:10:21 -0700919 object_id = ReadObjectId(&buf);
920 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700921
922 // TODO: currently returning false; must integrate with GC
923 expandBufAdd1(pReply, 0);
924
925 return ERR_NONE;
926}
927
928/*
929 * Return the string value in a string object.
930 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700931static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700932 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700933 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800934 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700935
Elliott Hughes82914b62012-04-09 15:56:29 -0700936 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700937
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800938 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700939
940 return ERR_NONE;
941}
942
943/*
944 * Return a thread's name.
945 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700947 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700948 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700949
Elliott Hughes74847412012-06-20 18:10:21 -0700950 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800951 std::string name;
Elliott Hughes221229c2013-01-08 18:17:50 -0800952 JdwpError error = Dbg::GetThreadName(thread_id, name);
953 if (error != ERR_NONE) {
954 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700955 }
Elliott Hughes74847412012-06-20 18:10:21 -0700956 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800957 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700958
959 return ERR_NONE;
960}
961
962/*
963 * Suspend the specified thread.
964 *
965 * It's supposed to remain suspended even if interpreted code wants to
966 * resume it; only the JDI is allowed to resume it.
967 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700969 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700970 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700971
Elliott Hughes74847412012-06-20 18:10:21 -0700972 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700973 LOG(INFO) << " Warning: ignoring request to suspend self";
974 return ERR_THREAD_NOT_SUSPENDED;
975 }
Elliott Hughes74847412012-06-20 18:10:21 -0700976 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 Thread* self = Thread::Current();
978 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
979 JdwpError result = Dbg::SuspendThread(thread_id);
980 self->TransitionFromSuspendedToRunnable();
981 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700982}
983
984/*
985 * Resume the specified thread.
986 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700987static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700988 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700989 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700990
Elliott Hughes74847412012-06-20 18:10:21 -0700991 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700992 LOG(INFO) << " Warning: ignoring request to resume self";
993 return ERR_NONE;
994 }
Elliott Hughes74847412012-06-20 18:10:21 -0700995 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
996 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700997 return ERR_NONE;
998}
999
1000/*
1001 * Return status of specified thread.
1002 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003static JdwpError TR_Status(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);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001006
Elliott Hughes74847412012-06-20 18:10:21 -07001007 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001008
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08001009 JDWP::JdwpThreadStatus threadStatus;
1010 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -08001011 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
1012 if (error != ERR_NONE) {
1013 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001014 }
1015
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001016 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001017
1018 expandBufAdd4BE(pReply, threadStatus);
1019 expandBufAdd4BE(pReply, suspendStatus);
1020
1021 return ERR_NONE;
1022}
1023
1024/*
1025 * Return the thread group that the specified thread is a member of.
1026 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001027static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001028 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001029 ObjectId thread_id = ReadObjectId(&buf);
1030 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031}
1032
1033/*
1034 * Return the current call stack of a suspended thread.
1035 *
1036 * If the thread isn't suspended, the error code isn't defined, but should
1037 * be THREAD_NOT_SUSPENDED.
1038 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001039static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001040 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001041 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001042 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001043 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001044
Elliott Hughes221229c2013-01-08 18:17:50 -08001045 size_t actual_frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001046 JdwpError error = Dbg::GetThreadFrameCount(thread_id, actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001047 if (error != ERR_NONE) {
1048 return error;
1049 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001050
Elliott Hughes74847412012-06-20 18:10:21 -07001051 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 -08001052 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001053 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1054 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001055
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001056 if (start_frame > actual_frame_count) {
1057 return ERR_INVALID_INDEX;
1058 }
1059 if (length == static_cast<uint32_t>(-1)) {
1060 length = actual_frame_count - start_frame;
1061 }
1062 if (start_frame + length > actual_frame_count) {
1063 return ERR_INVALID_LENGTH;
1064 }
1065
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001066 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001067}
1068
1069/*
1070 * Returns the #of frames on the specified thread, which must be suspended.
1071 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001072static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001074 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001075
Elliott Hughes221229c2013-01-08 18:17:50 -08001076 size_t frame_count;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001077 JdwpError rc = Dbg::GetThreadFrameCount(thread_id, frame_count);
1078 if (rc != ERR_NONE) {
1079 return rc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001080 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001081 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001082
1083 return ERR_NONE;
1084}
1085
Elliott Hughes734b8c62013-01-11 15:32:45 -08001086static JdwpError TR_OwnedMonitors(const uint8_t* buf, ExpandBuf* reply, bool with_stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001087 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1088 ObjectId thread_id = ReadObjectId(&buf);
1089
1090 std::vector<ObjectId> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -08001091 std::vector<uint32_t> stack_depths;
1092 JdwpError rc = Dbg::GetOwnedMonitors(thread_id, monitors, stack_depths);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001093 if (rc != ERR_NONE) {
1094 return rc;
1095 }
1096
1097 expandBufAdd4BE(reply, monitors.size());
1098 for (size_t i = 0; i < monitors.size(); ++i) {
1099 rc = WriteTaggedObject(reply, monitors[i]);
1100 if (rc != ERR_NONE) {
1101 return rc;
1102 }
Elliott Hughes734b8c62013-01-11 15:32:45 -08001103 if (with_stack_depths) {
1104 expandBufAdd4BE(reply, stack_depths[i]);
1105 }
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001106 }
1107 return ERR_NONE;
1108}
1109
Elliott Hughes734b8c62013-01-11 15:32:45 -08001110static JdwpError TR_OwnedMonitors(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1112 return TR_OwnedMonitors(buf, reply, false);
1113}
1114
1115static JdwpError TR_OwnedMonitorsStackDepthInfo(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1117 return TR_OwnedMonitors(buf, reply, true);
1118}
1119
Elliott Hughesf9501702013-01-11 11:22:27 -08001120static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesf9501702013-01-11 11:22:27 -08001122 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001123
Elliott Hughesf9501702013-01-11 11:22:27 -08001124 ObjectId contended_monitor;
1125 JdwpError rc = Dbg::GetContendedMonitor(thread_id, contended_monitor);
1126 if (rc != ERR_NONE) {
1127 return rc;
1128 }
1129 return WriteTaggedObject(reply, contended_monitor);
1130}
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001131
Elliott Hughesf9501702013-01-11 11:22:27 -08001132static JdwpError TR_Interrupt(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
1133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1134 ObjectId thread_id = ReadObjectId(&buf);
1135 return Dbg::Interrupt(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001136}
1137
1138/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001139 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001140 *
1141 * (The thread *might* still be running -- it might not have examined
1142 * its suspend count recently.)
1143 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001144static JdwpError TR_DebugSuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001146 ObjectId thread_id = ReadObjectId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001147 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001148}
1149
1150/*
1151 * Return the name of a thread group.
1152 *
1153 * The Eclipse debugger recognizes "main" and "system" as special.
1154 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001155static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001157 ObjectId thread_group_id = ReadObjectId(&buf);
1158 VLOG(jdwp) << StringPrintf(" Req for name of thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001159
Elliott Hughescaf76542012-06-28 16:08:22 -07001160 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001161
1162 return ERR_NONE;
1163}
1164
1165/*
1166 * Returns the thread group -- if any -- that contains the specified
1167 * thread group.
1168 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001171 ObjectId thread_group_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001172
Elliott Hughescaf76542012-06-28 16:08:22 -07001173 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001174 expandBufAddObjectId(pReply, parentGroup);
1175
1176 return ERR_NONE;
1177}
1178
1179/*
1180 * Return the active threads and thread groups that are part of the
1181 * specified thread group.
1182 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001183static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001185 ObjectId thread_group_id = ReadObjectId(&buf);
1186 VLOG(jdwp) << StringPrintf(" Req for threads in thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001187
Elliott Hughescaf76542012-06-28 16:08:22 -07001188 std::vector<ObjectId> thread_ids;
1189 Dbg::GetThreads(thread_group_id, thread_ids);
1190 expandBufAdd4BE(pReply, thread_ids.size());
1191 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1192 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001193 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001194
Elliott Hughescaf76542012-06-28 16:08:22 -07001195 std::vector<ObjectId> child_thread_groups_ids;
1196 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1197 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1198 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1199 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001200 }
1201
1202 return ERR_NONE;
1203}
1204
1205/*
1206 * Return the #of components in the array.
1207 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001208static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001210 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001211 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001212
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001213 int length;
1214 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1215 if (status != ERR_NONE) {
1216 return status;
1217 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001218 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001219
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001220 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001221
1222 return ERR_NONE;
1223}
1224
1225/*
1226 * Return the values from an array.
1227 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001230 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001231 uint32_t firstIndex = Read4BE(&buf);
1232 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001233 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001234
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001235 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001236}
1237
1238/*
1239 * Set values in an array.
1240 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001241static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001243 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001244 uint32_t firstIndex = Read4BE(&buf);
1245 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001246
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001247 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId,
1248 firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001249
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001250 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001251}
1252
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001253static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001254 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001255 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001256 // TODO: we should only return classes which have the given class loader as a defining or
1257 // initiating loader. The former would be easy; the latter is hard, because we don't have
1258 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001259 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001260}
1261
1262/*
1263 * Set an event trigger.
1264 *
1265 * Reply with a requestID.
1266 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001267static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001269 const uint8_t* origBuf = buf;
1270
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001271 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001272 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001273 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001274
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001275 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001276 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001277 << " mods=" << modifierCount << ")";
1278
1279 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1280
1281 JdwpEvent* pEvent = EventAlloc(modifierCount);
1282 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001283 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001284 pEvent->modCount = modifierCount;
1285
1286 /*
1287 * Read modifiers. Ordering may be significant (see explanation of Count
1288 * mods in JDWP doc).
1289 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001290 for (uint32_t i = 0; i < modifierCount; ++i) {
1291 JdwpEventMod& mod = pEvent->mods[i];
1292 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1293 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001294 case MK_COUNT: /* report once, when "--count" reaches 0 */
1295 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001296 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001297 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001298 if (count == 0) {
1299 return ERR_INVALID_COUNT;
1300 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001301 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001302 }
1303 break;
1304 case MK_CONDITIONAL: /* conditional on expression) */
1305 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001306 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001307 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001308 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001309 }
1310 break;
1311 case MK_THREAD_ONLY: /* only report events in specified thread */
1312 {
Elliott Hughes74847412012-06-20 18:10:21 -07001313 ObjectId thread_id = ReadObjectId(&buf);
1314 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1315 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001316 }
1317 break;
1318 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1319 {
Elliott Hughes74847412012-06-20 18:10:21 -07001320 RefTypeId class_id = ReadRefTypeId(&buf);
1321 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1322 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001323 }
1324 break;
1325 case MK_CLASS_MATCH: /* restrict events to matching classes */
1326 {
Elliott Hughes86964332012-02-15 19:37:42 -08001327 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001328 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001329 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001330 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001331 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001332 }
1333 break;
1334 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1335 {
Elliott Hughes86964332012-02-15 19:37:42 -08001336 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001337 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001338 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001339 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001340 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001341 }
1342 break;
1343 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1344 {
1345 JdwpLocation loc;
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001346 ReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001347 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001348 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001349 }
1350 break;
1351 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1352 {
1353 RefTypeId exceptionOrNull; /* null == all exceptions */
1354 uint8_t caught, uncaught;
1355
1356 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001357 caught = Read1(&buf);
1358 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001359 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001360 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001361
Elliott Hughes972a47b2012-02-21 18:16:06 -08001362 mod.exceptionOnly.refTypeId = exceptionOrNull;
1363 mod.exceptionOnly.caught = caught;
1364 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001365 }
1366 break;
1367 case MK_FIELD_ONLY: /* for field access/mod events */
1368 {
1369 RefTypeId declaring = ReadRefTypeId(&buf);
1370 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001371 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001372 mod.fieldOnly.refTypeId = declaring;
1373 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001374 }
1375 break;
1376 case MK_STEP: /* for use with EK_SINGLE_STEP */
1377 {
Elliott Hughes74847412012-06-20 18:10:21 -07001378 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001379 uint32_t size, depth;
1380
Elliott Hughes74847412012-06-20 18:10:21 -07001381 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001382 size = Read4BE(&buf);
1383 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001384 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001385 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1386
Elliott Hughes74847412012-06-20 18:10:21 -07001387 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001388 mod.step.size = size;
1389 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001390 }
1391 break;
1392 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1393 {
1394 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001395 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001396 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001397 }
1398 break;
1399 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001400 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001401 break;
1402 }
1403 }
1404
1405 /*
1406 * Make sure we consumed all data. It is possible that the remote side
1407 * has sent us bad stuff, but for now we blame ourselves.
1408 */
1409 if (buf != origBuf + dataLen) {
1410 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1411 }
1412
1413 /*
1414 * We reply with an integer "requestID".
1415 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001416 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001417 expandBufAdd4BE(pReply, requestId);
1418
1419 pEvent->requestId = requestId;
1420
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001421 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001422
1423 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001424 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001425 if (err != ERR_NONE) {
1426 /* registration failed, probably because event is bogus */
1427 EventFree(pEvent);
1428 LOG(WARNING) << "WARNING: event request rejected";
1429 }
1430 return err;
1431}
1432
1433/*
1434 * Clear an event. Failure to find an event with a matching ID is a no-op
1435 * and does not return an error.
1436 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001437static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001438 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001439 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001440 eventKind = Read1(&buf);
1441 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001442
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001443 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001444
Elliott Hughes761928d2011-11-16 18:33:03 -08001445 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001446
1447 return ERR_NONE;
1448}
1449
1450/*
1451 * Return the values of arguments and local variables.
1452 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001453static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001454 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001455 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001456 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001457 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001458
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001459 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 -07001460
1461 expandBufAdd4BE(pReply, slots); /* "int values" */
1462 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001463 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001464 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001465
Elliott Hughes2435a572012-02-17 16:07:41 -08001466 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001467
Elliott Hughesdbb40792011-11-18 17:05:22 -08001468 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001469 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001470 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001471 }
1472
1473 return ERR_NONE;
1474}
1475
1476/*
1477 * Set the values of arguments and local variables.
1478 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001479static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001480 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001481 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001482 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001483 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001484
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001485 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 -07001486
1487 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001488 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001489 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001490 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001491 uint64_t value = ReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001492
Elliott Hughes2435a572012-02-17 16:07:41 -08001493 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001494 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001495 }
1496
1497 return ERR_NONE;
1498}
1499
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001500static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001501 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001502 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001503 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001504
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001505 ObjectId object_id;
1506 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001507 if (rc != ERR_NONE) {
1508 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001509 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001510
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001511 VLOG(jdwp) << StringPrintf(" Req for 'this' in thread_id=%#llx frame=%lld --> %#llx",
1512 thread_id, frame_id, object_id);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001513
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001514 return WriteTaggedObject(reply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001515}
1516
1517/*
1518 * Return the reference type reflected by this class object.
1519 *
1520 * This appears to be required because ReferenceTypeId values are NEVER
1521 * reused, whereas ClassIds can be recycled like any other object. (Either
1522 * that, or I have no idea what this is for.)
1523 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001524static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001525 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001526 RefTypeId classObjectId = ReadRefTypeId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001527 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId,
1528 Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001529 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001530}
1531
1532/*
1533 * Handle a DDM packet with a single chunk in it.
1534 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001535static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001536 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001537 uint8_t* replyBuf = NULL;
1538 int replyLen = -1;
1539
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001540 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001541
Elliott Hughesa21039c2012-06-21 12:09:25 -07001542 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001543
1544 /*
1545 * If they want to send something back, we copy it into the buffer.
1546 * A no-copy approach would be nicer.
1547 *
1548 * TODO: consider altering the JDWP stuff to hold the packet header
1549 * in a separate buffer. That would allow us to writev() DDM traffic
1550 * instead of copying it into the expanding buffer. The reduction in
1551 * heap requirements is probably more valuable than the efficiency.
1552 */
1553 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1554 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1555 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1556 free(replyBuf);
1557 }
1558 return ERR_NONE;
1559}
1560
1561/*
1562 * Handler map decl.
1563 */
1564typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1565
1566struct JdwpHandlerMap {
1567 uint8_t cmdSet;
1568 uint8_t cmd;
1569 JdwpRequestHandler func;
1570 const char* descr;
1571};
1572
1573/*
1574 * Map commands to functions.
1575 *
1576 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1577 * and 128-256 are vendor-defined.
1578 */
1579static const JdwpHandlerMap gHandlerMap[] = {
1580 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001581 { 1, 1, VM_Version, "VirtualMachine.Version" },
1582 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1583 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1584 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1585 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1586 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1587 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1588 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1589 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1590 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1591 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1592 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1593 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1594 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1595 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1596 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
1597 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1598 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1599 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001600 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesec0f83d2013-01-15 16:54:08 -08001601 { 1, 21, VM_InstanceCounts, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001602
1603 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001604 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1605 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1606 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1607 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1608 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1609 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1610 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
1611 { 2, 8, NULL, "ReferenceType.NestedTypes" },
1612 { 2, 9, RT_Status, "ReferenceType.Status" },
1613 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1614 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001615 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1616 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001617 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1618 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
1619 { 2, 16, NULL, "ReferenceType.Instances" },
1620 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1621 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001622
1623 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001624 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1625 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1626 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1627 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001628
1629 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001630 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001631
1632 /* InterfaceType command set (5) */
1633
1634 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001635 { 6, 1, M_LineTable, "Method.LineTable" },
1636 { 6, 2, M_VariableTable, "Method.VariableTable" },
1637 { 6, 3, NULL, "Method.Bytecodes" },
1638 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001639 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001640
1641 /* Field command set (8) */
1642
1643 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001644 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1645 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1646 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
1647 { 9, 4, NULL, "ObjectReference.UNUSED" },
1648 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1649 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001650 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001651 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1652 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
1653 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001654
1655 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001656 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001657
1658 /* ThreadReference command set (11) */
Elliott Hughes734b8c62013-01-11 15:32:45 -08001659 { 11, 1, TR_Name, "ThreadReference.Name" },
1660 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1661 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1662 { 11, 4, TR_Status, "ThreadReference.Status" },
1663 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1664 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1665 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1666 { 11, 8, TR_OwnedMonitors, "ThreadReference.OwnedMonitors" },
1667 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
1668 { 11, 10, NULL, "ThreadReference.Stop" },
1669 { 11, 11, TR_Interrupt, "ThreadReference.Interrupt" },
1670 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1671 { 11, 13, TR_OwnedMonitorsStackDepthInfo, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1672 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001673
1674 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001675 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1676 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1677 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001678
1679 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001680 { 13, 1, AR_Length, "ArrayReference.Length" },
1681 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1682 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001683
1684 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001685 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001686
1687 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001688 { 15, 1, ER_Set, "EventRequest.Set" },
1689 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001690 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001691
1692 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001693 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1694 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1695 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001696 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001697
1698 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001699 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001700
1701 /* Event command set (64) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001702 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001703
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001704 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001705};
1706
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001707static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001708 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001709 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1710 return gHandlerMap[i].descr;
1711 }
1712 }
1713 return "?UNKNOWN?";
1714}
1715
1716static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1717 std::string result;
1718 result += "REQ: ";
1719 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1720 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1721 return result;
1722}
1723
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001724/*
1725 * Process a request from the debugger.
1726 *
1727 * On entry, the JDWP thread is in VMWAIT.
1728 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001729void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001730 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001731
1732 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1733 /*
1734 * Activity from a debugger, not merely ddms. Mark us as having an
1735 * active debugger session, and zero out the last-activity timestamp
1736 * so waitForDebugger() doesn't return if we stall for a bit here.
1737 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001738 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001739 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001740 }
1741
1742 /*
1743 * If a debugger event has fired in another thread, wait until the
1744 * initiating thread has suspended itself before processing messages
1745 * from the debugger. Otherwise we (the JDWP thread) could be told to
1746 * resume the thread before it has suspended.
1747 *
1748 * We call with an argument of zero to wait for the current event
1749 * thread to finish, and then clear the block. Depending on the thread
1750 * suspend policy, this may allow events in other threads to fire,
1751 * but those events have no bearing on what the debugger has sent us
1752 * in the current request.
1753 *
1754 * Note that we MUST clear the event token before waking the event
1755 * thread up, or risk waiting for the thread to suspend after we've
1756 * told it to resume.
1757 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001758 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001759
1760 /*
1761 * Tell the VM that we're running and shouldn't be interrupted by GC.
1762 * Do this after anything that can stall indefinitely.
1763 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 Thread* self = Thread::Current();
1765 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001766
1767 expandBufAddSpace(pReply, kJDWPHeaderLen);
1768
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001769 size_t i;
1770 for (i = 0; i < arraysize(gHandlerMap); i++) {
1771 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001772 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001773 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001774 break;
1775 }
1776 }
1777 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001778 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1779 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001780 result = ERR_NOT_IMPLEMENTED;
1781 }
1782
1783 /*
1784 * Set up the reply header.
1785 *
1786 * If we encountered an error, only send the header back.
1787 */
1788 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001789 Set4BE(replyBuf + 4, pHeader->id);
1790 Set1(replyBuf + 8, kJDWPFlagReply);
1791 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001792 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001793 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001794 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001795 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001796 }
1797
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001798 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001799 if (false) {
1800 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001801 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001802 }
1803
1804 /*
1805 * Update last-activity timestamp. We really only need this during
1806 * the initial setup. Only update if this is a non-DDMS packet.
1807 */
1808 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001809 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001810 }
1811
1812 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001813 self->TransitionFromRunnableToSuspended(old_state);
1814
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001815}
1816
1817} // namespace JDWP
1818
1819} // namespace art