blob: 1732ea525857aca50324d8ca4c0f805913a866c6 [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 Hughes6e9d22c2012-06-22 15:02:37 -070051static void JdwpReadLocation(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 Hughes6e9d22c2012-06-22 15:02:37 -070062static uint64_t JdwpReadValue(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 Hughes6e9d22c2012-06-22 15:02:37 -070077static void JdwpWriteValue(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
87/*
88 * Common code for *_InvokeMethod requests.
89 *
Elliott Hughes74847412012-06-20 18:10:21 -070090 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -070091 * expected-to-be-void return value of the called function.
92 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070093static JdwpError FinishInvoke(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
Elliott Hughes74847412012-06-20 18:10:21 -070094 ObjectId thread_id, ObjectId object_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 RefTypeId class_id, MethodId method_id, bool is_constructor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -070097 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070098
Elliott Hughes45651fd2012-02-21 15:48:20 -080099 uint32_t arg_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700100
Elliott Hughes74847412012-06-20 18:10:21 -0700101 VLOG(jdwp) << StringPrintf(" --> thread_id=%#llx object_id=%#llx", thread_id, object_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 VLOG(jdwp) << StringPrintf(" class_id=%#llx method_id=%x %s.%s", class_id,
103 method_id, Dbg::GetClassName(class_id).c_str(),
104 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800105 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700106
Elliott Hughes45651fd2012-02-21 15:48:20 -0800107 UniquePtr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
108 UniquePtr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
109 for (uint32_t i = 0; i < arg_count; ++i) {
110 argTypes[i] = ReadTag(&buf);
111 size_t width = Dbg::GetTagWidth(argTypes[i]);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700112 argValues[i] = JdwpReadValue(&buf, width);
Elliott Hughes229feb72012-02-23 13:33:29 -0800113 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#llx", width, argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700114 }
115
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700116 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700117 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
118 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
119 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700120
Elliott Hughes45651fd2012-02-21 15:48:20 -0800121 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700122 uint64_t resultValue;
123 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700124 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 -0700125 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800126 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700127 }
128
129 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800130 if (is_constructor) {
131 // If we invoked a constructor (which actually returns void), return the receiver,
132 // unless we threw, in which case we return NULL.
133 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700134 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800135 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700136
Elliott Hughes45651fd2012-02-21 15:48:20 -0800137 size_t width = Dbg::GetTagWidth(resultTag);
138 expandBufAdd1(pReply, resultTag);
139 if (width != 0) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700140 JdwpWriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700141 }
142 expandBufAdd1(pReply, JT_OBJECT);
143 expandBufAddObjectId(pReply, exceptObjId);
144
Elliott Hughes229feb72012-02-23 13:33:29 -0800145 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" %#llx (except=%#llx)", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700146
147 /* show detailed debug output */
148 if (resultTag == JT_STRING && exceptObjId == 0) {
149 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800150 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700151 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800152 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700153 }
154 }
155 }
156
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700157 return err;
158}
159
160
161/*
162 * Request for version info.
163 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164static JdwpError VM_Version(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700166 /* text information on runtime version */
167 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800168 expandBufAddUtf8String(pReply, version);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169 /* JDWP version numbers */
170 expandBufAdd4BE(pReply, 1); // major
171 expandBufAdd4BE(pReply, 5); // minor
172 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800173 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700174 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800175 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176
177 return ERR_NONE;
178}
179
180/*
181 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
182 * referenceTypeID. We need to send back more than one if the class has
183 * been loaded by multiple class loaders.
184 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700185static JdwpError VM_ClassesBySignature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800187 std::string classDescriptor(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800188 VLOG(jdwp) << " Req for class by signature '" << classDescriptor << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800190 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800191 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700192
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800193 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700194
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800195 for (size_t i = 0; i < ids.size(); ++i) {
196 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800197 JDWP::JdwpTypeTag type_tag;
198 uint32_t class_status;
199 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
200 if (status != ERR_NONE) {
201 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800202 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700203
Elliott Hughes436e3722012-02-17 20:01:47 -0800204 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800205 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800206 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700207 }
208
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700209 return ERR_NONE;
210}
211
212/*
213 * Handle request for the thread IDs of all running threads.
214 *
215 * We exclude ourselves from the list, because we don't allow ourselves
216 * to be suspended, and that violates some JDWP expectations.
217 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218static JdwpError VM_AllThreads(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700220 std::vector<ObjectId> thread_ids;
Elliott Hughes026b1462012-06-28 20:43:49 -0700221 Dbg::GetThreads(0, thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700222
Elliott Hughescaf76542012-06-28 16:08:22 -0700223 expandBufAdd4BE(pReply, thread_ids.size());
224 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
225 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226 }
227
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700228 return ERR_NONE;
229}
230
231/*
232 * List all thread groups that do not have a parent.
233 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234static JdwpError VM_TopLevelThreadGroups(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700235 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700236 /*
237 * TODO: maintain a list of parentless thread groups in the VM.
238 *
239 * For now, just return "system". Application threads are created
240 * in "main", which is a child of "system".
241 */
242 uint32_t groups = 1;
243 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700244 //thread_group_id = debugGetMainThreadGroup();
245 //expandBufAdd8BE(pReply, thread_group_id);
246 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
247 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700248
249 return ERR_NONE;
250}
251
252/*
253 * Respond with the sizes of the basic debugger types.
254 *
255 * All IDs are 8 bytes.
256 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257static JdwpError VM_IDSizes(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700259 expandBufAdd4BE(pReply, sizeof(FieldId));
260 expandBufAdd4BE(pReply, sizeof(MethodId));
261 expandBufAdd4BE(pReply, sizeof(ObjectId));
262 expandBufAdd4BE(pReply, sizeof(RefTypeId));
263 expandBufAdd4BE(pReply, sizeof(FrameId));
264 return ERR_NONE;
265}
266
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267static JdwpError VM_Dispose(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes86964332012-02-15 19:37:42 -0800269 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700270 return ERR_NONE;
271}
272
273/*
274 * Suspend the execution of the application running in the VM (i.e. suspend
275 * all threads).
276 *
277 * This needs to increment the "suspend count" on all threads.
278 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279static JdwpError VM_Suspend(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700280 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800281 Thread* self = Thread::Current();
282 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700283 Dbg::SuspendVM();
jeffhaoa77f0f62012-12-05 17:19:31 -0800284 self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700285 return ERR_NONE;
286}
287
288/*
289 * Resume execution. Decrements the "suspend count" of all threads.
290 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291static JdwpError VM_Resume(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700293 Dbg::ResumeVM();
294 return ERR_NONE;
295}
296
297/*
298 * The debugger wants the entire VM to exit.
299 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700300static JdwpError VM_Exit(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700302 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700303
304 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
305
306 Dbg::Exit(exitCode);
307 return ERR_NOT_IMPLEMENTED; // shouldn't get here
308}
309
310/*
311 * Create a new string in the VM and return its ID.
312 *
313 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
314 * string "java.util.Arrays".)
315 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700316static JdwpError VM_CreateString(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800318 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800319 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700320 ObjectId stringId = Dbg::CreateString(str);
321 if (stringId == 0) {
322 return ERR_OUT_OF_MEMORY;
323 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700324 expandBufAddObjectId(pReply, stringId);
325 return ERR_NONE;
326}
327
328/*
329 * Tell the debugger what we are capable of.
330 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331static JdwpError VM_Capabilities(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700332 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700333 expandBufAdd1(pReply, false); /* canWatchFieldModification */
334 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
335 expandBufAdd1(pReply, false); /* canGetBytecodes */
336 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
337 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
338 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
339 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
340 return ERR_NONE;
341}
342
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343static JdwpError VM_ClassPaths(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800345 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700346
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800347 std::vector<std::string> class_path;
348 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
349 expandBufAdd4BE(pReply, class_path.size());
350 for (size_t i = 0; i < class_path.size(); ++i) {
351 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700352 }
353
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800354 std::vector<std::string> boot_class_path;
355 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
356 expandBufAdd4BE(pReply, boot_class_path.size());
357 for (size_t i = 0; i < boot_class_path.size(); ++i) {
358 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700359 }
360
361 return ERR_NONE;
362}
363
364/*
365 * Release a list of object IDs. (Seen in jdb.)
366 *
367 * Currently does nothing.
368 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369static JdwpError VM_DisposeObjects(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700370 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700371 return ERR_NONE;
372}
373
374/*
375 * Tell the debugger what we are capable of.
376 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377static JdwpError VM_CapabilitiesNew(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700378 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700379 expandBufAdd1(pReply, false); /* canWatchFieldModification */
380 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
381 expandBufAdd1(pReply, false); /* canGetBytecodes */
382 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
383 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
384 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
385 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
386 expandBufAdd1(pReply, false); /* canRedefineClasses */
387 expandBufAdd1(pReply, false); /* canAddMethod */
388 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
389 expandBufAdd1(pReply, false); /* canPopFrames */
390 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
391 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
392 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
393 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
394 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
395 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
396 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
397 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
398 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
399 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
400
401 /* fill in reserved22 through reserved32; note count started at 1 */
402 for (int i = 22; i <= 32; i++) {
403 expandBufAdd1(pReply, false); /* reservedN */
404 }
405 return ERR_NONE;
406}
407
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700409 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800410 std::vector<JDWP::RefTypeId> classes;
411 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800413 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700414
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800415 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800416 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800417 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800418 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800419 uint32_t class_status;
420 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
421 if (status != ERR_NONE) {
422 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800423 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424
Elliott Hughes436e3722012-02-17 20:01:47 -0800425 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800426 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800427 if (descriptor_and_status) {
428 expandBufAddUtf8String(pReply, descriptor);
429 if (generic) {
430 expandBufAddUtf8String(pReply, genericSignature);
431 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800432 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800433 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700434 }
435
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700436 return ERR_NONE;
437}
438
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439static JdwpError VM_AllClasses(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700440 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700441 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800442}
443
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700444static JdwpError VM_AllClassesWithGeneric(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700445 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700446 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800447}
448
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700449static JdwpError RT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700450 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800452 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700453}
454
455/*
456 * Get values from static fields in a reference type.
457 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700458static JdwpError RT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800460 RefTypeId refTypeId = ReadRefTypeId(&buf);
461 uint32_t field_count = Read4BE(&buf);
462 expandBufAdd4BE(pReply, field_count);
463 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700464 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800465 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800466 if (status != ERR_NONE) {
467 return status;
468 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700469 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700470 return ERR_NONE;
471}
472
473/*
474 * Get the name of the source file in which a reference type was declared.
475 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700476static JdwpError RT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700477 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700478 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800479 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800480 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
481 if (status != ERR_NONE) {
482 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700483 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800484 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800485 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700486}
487
488/*
489 * Return the current status of the reference type.
490 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700491static JdwpError RT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700492 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700493 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800494 JDWP::JdwpTypeTag type_tag;
495 uint32_t class_status;
496 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
497 if (status != ERR_NONE) {
498 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800499 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800500 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700501 return ERR_NONE;
502}
503
504/*
505 * Return interfaces implemented directly by this class.
506 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700507static JdwpError RT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700508 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700509 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800510 VLOG(jdwp) << StringPrintf(" Req for interfaces in %#llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800511 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700512}
513
514/*
515 * Return the class object corresponding to this type.
516 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700517static JdwpError RT_ClassObject(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 Hughes7b3cdfc2011-12-08 21:28:17 -0800520 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800521 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
522 if (status != ERR_NONE) {
523 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800524 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800525 VLOG(jdwp) << StringPrintf(" RefTypeId %#llx -> ObjectId %#llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800526 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700527 return ERR_NONE;
528}
529
530/*
531 * Returns the value of the SourceDebugExtension attribute.
532 *
533 * JDB seems interested, but DEX files don't currently support this.
534 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700535static JdwpError RT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700536 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700537 /* referenceTypeId in, string out */
538 return ERR_ABSENT_INFORMATION;
539}
540
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700541static JdwpError RT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
542 bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700543 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700544 RefTypeId refTypeId = ReadRefTypeId(&buf);
545
Elliott Hughes229feb72012-02-23 13:33:29 -0800546 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=%#llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800547 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800548
549 JdwpError status = Dbg::GetSignature(refTypeId, signature);
550 if (status != ERR_NONE) {
551 return status;
552 }
553 expandBufAddUtf8String(pReply, signature);
554 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800555 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700556 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700557 return ERR_NONE;
558}
559
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700560static JdwpError RT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700561 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700562 return RT_Signature(state, buf, dataLen, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800563}
564
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700565static JdwpError RT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
566 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700567 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700568 return RT_Signature(state, buf, dataLen, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800569}
570
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700571/*
572 * Return the instance of java.lang.ClassLoader that loaded the specified
573 * reference type, or null if it was loaded by the system loader.
574 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700575static JdwpError RT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700576 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700577 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800578 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700579}
580
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700581static std::string Describe(const RefTypeId& refTypeId)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700582 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800583 std::string signature("unknown");
584 Dbg::GetSignature(refTypeId, signature);
Elliott Hughes229feb72012-02-23 13:33:29 -0800585 return StringPrintf("refTypeId=%#llx (%s)", refTypeId, signature.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800586}
587
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700588/*
589 * Given a referenceTypeId, return a block of stuff that describes the
590 * fields declared by a class.
591 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700592static JdwpError RT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700593 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700594 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800595 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800596 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800597}
598
599// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700600static JdwpError RT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700601 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800602 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800603 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800604 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700605}
606
607/*
608 * Given a referenceTypeID, return a block of goodies describing the
609 * methods declared by a class.
610 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611static JdwpError RT_MethodsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700612 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700613 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800614 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800615 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800616}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800618// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619static JdwpError RT_Methods(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700620 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800621 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800622 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800623 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700624}
625
626/*
627 * Return the immediate superclass of a class.
628 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700629static JdwpError CT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700630 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700631 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800632 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700633 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800634 if (status != ERR_NONE) {
635 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800636 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700637 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700638 return ERR_NONE;
639}
640
641/*
642 * Set static class values.
643 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700644static JdwpError CT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700645 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700646 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700647 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700648
Elliott Hughes74847412012-06-20 18:10:21 -0700649 VLOG(jdwp) << StringPrintf(" Req to set %d values in class_id=%#llx", values, class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700650
651 for (uint32_t i = 0; i < values; i++) {
652 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800653 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800654 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700655 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700656
Elliott Hughes2435a572012-02-17 16:07:41 -0800657 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800658 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
659 if (status != ERR_NONE) {
660 return status;
661 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700662 }
663
664 return ERR_NONE;
665}
666
667/*
668 * Invoke a static method.
669 *
670 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
671 * values in the "variables" display.
672 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673static JdwpError CT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
674 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700675 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700676 RefTypeId class_id = ReadRefTypeId(&buf);
677 ObjectId thread_id = ReadObjectId(&buf);
678 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700679
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700680 return FinishInvoke(state, buf, dataLen, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700681}
682
683/*
684 * Create a new object of the requested type, and invoke the specified
685 * constructor.
686 *
687 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
688 * see the contents of a byte[] as a string.
689 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690static JdwpError CT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen,
691 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700692 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700693 RefTypeId class_id = ReadRefTypeId(&buf);
694 ObjectId thread_id = ReadObjectId(&buf);
695 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700696
Elliott Hughes74847412012-06-20 18:10:21 -0700697 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(class_id);
698 ObjectId object_id;
699 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800700 if (status != ERR_NONE) {
701 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800702 }
Elliott Hughes74847412012-06-20 18:10:21 -0700703 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700704 return ERR_OUT_OF_MEMORY;
705 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700706 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700707}
708
709/*
710 * Create a new array object of the requested type and length.
711 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712static JdwpError AT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700713 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700714 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700715 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700716
Elliott Hughes2435a572012-02-17 16:07:41 -0800717 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes74847412012-06-20 18:10:21 -0700718 ObjectId object_id;
719 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800720 if (status != ERR_NONE) {
721 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800722 }
Elliott Hughes74847412012-06-20 18:10:21 -0700723 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700724 return ERR_OUT_OF_MEMORY;
725 }
726 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700727 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700728 return ERR_NONE;
729}
730
731/*
732 * Return line number information for the method, if present.
733 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734static JdwpError M_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700735 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700736 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700737 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700738
Elliott Hughes74847412012-06-20 18:10:21 -0700739 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId, method_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700740
Elliott Hughes74847412012-06-20 18:10:21 -0700741 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742
743 return ERR_NONE;
744}
745
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700746static JdwpError M_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
747 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700748 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700749 RefTypeId class_id = ReadRefTypeId(&buf);
750 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700751
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700752 VLOG(jdwp) << StringPrintf(" Req for LocalVarTab in class=%s method=%s",
753 Dbg::GetClassName(class_id).c_str(),
754 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700755
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800756 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
757 // information. That will cause Eclipse to make a best-effort attempt at displaying local
758 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
759 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700760 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700761 return ERR_NONE;
762}
763
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700764static JdwpError M_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen,
765 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700766 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700767 return M_VariableTable(state, buf, dataLen, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800768}
769
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700770static JdwpError M_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
771 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700772 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700773 return M_VariableTable(state, buf, dataLen, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800774}
775
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700776/*
777 * Given an object reference, return the runtime type of the object
778 * (class or array).
779 *
Elliott Hughes74847412012-06-20 18:10:21 -0700780 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700781 * passed in here.
782 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700783static JdwpError OR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700784 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700785 ObjectId object_id = ReadObjectId(&buf);
786 VLOG(jdwp) << StringPrintf(" Req for type of object_id=%#llx", object_id);
787 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700788}
789
790/*
791 * Get values from the fields of an object.
792 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700793static JdwpError OR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700794 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700795 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800796 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700797
Elliott Hughes74847412012-06-20 18:10:21 -0700798 VLOG(jdwp) << StringPrintf(" Req for %d fields from object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700799
Elliott Hughes0cf74332012-02-23 23:14:00 -0800800 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801
Elliott Hughes0cf74332012-02-23 23:14:00 -0800802 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700803 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700804 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800805 if (status != ERR_NONE) {
806 return status;
807 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700808 }
809
810 return ERR_NONE;
811}
812
813/*
814 * Set values in the fields of an object.
815 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700816static JdwpError OR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700817 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700818 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800819 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700820
Elliott Hughes74847412012-06-20 18:10:21 -0700821 VLOG(jdwp) << StringPrintf(" Req to set %d fields in object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700822
Elliott Hughes0cf74332012-02-23 23:14:00 -0800823 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700824 FieldId fieldId = ReadFieldId(&buf);
825
Elliott Hughesaed4be92011-12-02 16:16:23 -0800826 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800827 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700828 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700829
Elliott Hughes2435a572012-02-17 16:07:41 -0800830 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700831 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800832 if (status != ERR_NONE) {
833 return status;
834 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700835 }
836
837 return ERR_NONE;
838}
839
840/*
841 * Invoke an instance method. The invocation must occur in the specified
842 * thread, which must have been suspended by an event.
843 *
844 * The call is synchronous. All threads in the VM are resumed, unless the
845 * SINGLE_THREADED flag is set.
846 *
847 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
848 * object), it will try to invoke the object's toString() function. This
849 * feature becomes crucial when examining ArrayLists with Eclipse.
850 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700851static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
852 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700853 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700854 ObjectId object_id = ReadObjectId(&buf);
855 ObjectId thread_id = ReadObjectId(&buf);
856 RefTypeId class_id = ReadRefTypeId(&buf);
857 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700858
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700859 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700860}
861
862/*
863 * Disable garbage collection of the specified object.
864 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865static JdwpError OR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700866 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700867 // this is currently a no-op
868 return ERR_NONE;
869}
870
871/*
872 * Enable garbage collection of the specified object.
873 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700874static JdwpError OR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700875 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700876 // this is currently a no-op
877 return ERR_NONE;
878}
879
880/*
881 * Determine whether an object has been garbage collected.
882 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700883static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700884 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700885 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700886
Elliott Hughes74847412012-06-20 18:10:21 -0700887 object_id = ReadObjectId(&buf);
888 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700889
890 // TODO: currently returning false; must integrate with GC
891 expandBufAdd1(pReply, 0);
892
893 return ERR_NONE;
894}
895
896/*
897 * Return the string value in a string object.
898 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700899static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700900 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700901 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800902 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700903
Elliott Hughes82914b62012-04-09 15:56:29 -0700904 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700905
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800906 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700907
908 return ERR_NONE;
909}
910
911/*
912 * Return a thread's name.
913 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700915 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700916 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700917
Elliott Hughes74847412012-06-20 18:10:21 -0700918 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800919 std::string name;
Elliott Hughes221229c2013-01-08 18:17:50 -0800920 JdwpError error = Dbg::GetThreadName(thread_id, name);
921 if (error != ERR_NONE) {
922 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700923 }
Elliott Hughes74847412012-06-20 18:10:21 -0700924 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800925 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700926
927 return ERR_NONE;
928}
929
930/*
931 * Suspend the specified thread.
932 *
933 * It's supposed to remain suspended even if interpreted code wants to
934 * resume it; only the JDI is allowed to resume it.
935 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700937 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700938 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700939
Elliott Hughes74847412012-06-20 18:10:21 -0700940 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700941 LOG(INFO) << " Warning: ignoring request to suspend self";
942 return ERR_THREAD_NOT_SUSPENDED;
943 }
Elliott Hughes74847412012-06-20 18:10:21 -0700944 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 Thread* self = Thread::Current();
946 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
947 JdwpError result = Dbg::SuspendThread(thread_id);
948 self->TransitionFromSuspendedToRunnable();
949 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700950}
951
952/*
953 * Resume the specified thread.
954 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700956 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700957 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700958
Elliott Hughes74847412012-06-20 18:10:21 -0700959 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700960 LOG(INFO) << " Warning: ignoring request to resume self";
961 return ERR_NONE;
962 }
Elliott Hughes74847412012-06-20 18:10:21 -0700963 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
964 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700965 return ERR_NONE;
966}
967
968/*
969 * Return status of specified thread.
970 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700972 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700973 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700974
Elliott Hughes74847412012-06-20 18:10:21 -0700975 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700976
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800977 JDWP::JdwpThreadStatus threadStatus;
978 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -0800979 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
980 if (error != ERR_NONE) {
981 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700982 }
983
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800984 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700985
986 expandBufAdd4BE(pReply, threadStatus);
987 expandBufAdd4BE(pReply, suspendStatus);
988
989 return ERR_NONE;
990}
991
992/*
993 * Return the thread group that the specified thread is a member of.
994 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700995static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700996 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700997 ObjectId thread_id = ReadObjectId(&buf);
998 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700999}
1000
1001/*
1002 * Return the current call stack of a suspended thread.
1003 *
1004 * If the thread isn't suspended, the error code isn't defined, but should
1005 * be THREAD_NOT_SUSPENDED.
1006 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001007static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001008 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001009 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001010 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001011 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001012
Elliott Hughes221229c2013-01-08 18:17:50 -08001013 size_t actual_frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001014 JdwpError error = Dbg::GetThreadFrameCount(thread_id, actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001015 if (error != ERR_NONE) {
1016 return error;
1017 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001018
Elliott Hughes74847412012-06-20 18:10:21 -07001019 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 -08001020 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001021 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1022 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001023
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001024 if (start_frame > actual_frame_count) {
1025 return ERR_INVALID_INDEX;
1026 }
1027 if (length == static_cast<uint32_t>(-1)) {
1028 length = actual_frame_count - start_frame;
1029 }
1030 if (start_frame + length > actual_frame_count) {
1031 return ERR_INVALID_LENGTH;
1032 }
1033
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001034 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001035}
1036
1037/*
1038 * Returns the #of frames on the specified thread, which must be suspended.
1039 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001040static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001041 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001042 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001043
Elliott Hughes221229c2013-01-08 18:17:50 -08001044 size_t frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001045 JdwpError error = Dbg::GetThreadFrameCount(thread_id, frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001046 if (error != ERR_NONE) {
1047 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001048 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001049 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001050
1051 return ERR_NONE;
1052}
1053
1054/*
1055 * Get the monitor that the thread is waiting on.
1056 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001057static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001058 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001059 ReadObjectId(&buf); // thread_id
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001060
1061 // TODO: create an Object to represent the monitor (we're currently
1062 // just using a raw Monitor struct in the VM)
1063
1064 return ERR_NOT_IMPLEMENTED;
1065}
1066
1067/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001069 *
1070 * (The thread *might* still be running -- it might not have examined
1071 * its suspend count recently.)
1072 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073static JdwpError TR_DebugSuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001074 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001075 ObjectId thread_id = ReadObjectId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001077}
1078
1079/*
1080 * Return the name of a thread group.
1081 *
1082 * The Eclipse debugger recognizes "main" and "system" as special.
1083 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001085 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001086 ObjectId thread_group_id = ReadObjectId(&buf);
1087 VLOG(jdwp) << StringPrintf(" Req for name of thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001088
Elliott Hughescaf76542012-06-28 16:08:22 -07001089 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001090
1091 return ERR_NONE;
1092}
1093
1094/*
1095 * Returns the thread group -- if any -- that contains the specified
1096 * thread group.
1097 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001098static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001099 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001100 ObjectId thread_group_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001101
Elliott Hughescaf76542012-06-28 16:08:22 -07001102 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001103 expandBufAddObjectId(pReply, parentGroup);
1104
1105 return ERR_NONE;
1106}
1107
1108/*
1109 * Return the active threads and thread groups that are part of the
1110 * specified thread group.
1111 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001112static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001114 ObjectId thread_group_id = ReadObjectId(&buf);
1115 VLOG(jdwp) << StringPrintf(" Req for threads in thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001116
Elliott Hughescaf76542012-06-28 16:08:22 -07001117 std::vector<ObjectId> thread_ids;
1118 Dbg::GetThreads(thread_group_id, thread_ids);
1119 expandBufAdd4BE(pReply, thread_ids.size());
1120 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1121 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001122 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001123
Elliott Hughescaf76542012-06-28 16:08:22 -07001124 std::vector<ObjectId> child_thread_groups_ids;
1125 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1126 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1127 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1128 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001129 }
1130
1131 return ERR_NONE;
1132}
1133
1134/*
1135 * Return the #of components in the array.
1136 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001137static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001139 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001140 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001141
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001142 int length;
1143 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1144 if (status != ERR_NONE) {
1145 return status;
1146 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001147 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001148
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001149 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001150
1151 return ERR_NONE;
1152}
1153
1154/*
1155 * Return the values from an array.
1156 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001157static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001159 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001160 uint32_t firstIndex = Read4BE(&buf);
1161 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001162 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001163
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001164 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001165}
1166
1167/*
1168 * Set values in an array.
1169 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001170static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001172 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001173 uint32_t firstIndex = Read4BE(&buf);
1174 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001175
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001176 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId,
1177 firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001178
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001179 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001180}
1181
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001182static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001184 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001185 // TODO: we should only return classes which have the given class loader as a defining or
1186 // initiating loader. The former would be easy; the latter is hard, because we don't have
1187 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001188 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001189}
1190
1191/*
1192 * Set an event trigger.
1193 *
1194 * Reply with a requestID.
1195 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001198 const uint8_t* origBuf = buf;
1199
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001200 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001201 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001202 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001203
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001204 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001205 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001206 << " mods=" << modifierCount << ")";
1207
1208 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1209
1210 JdwpEvent* pEvent = EventAlloc(modifierCount);
1211 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001212 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001213 pEvent->modCount = modifierCount;
1214
1215 /*
1216 * Read modifiers. Ordering may be significant (see explanation of Count
1217 * mods in JDWP doc).
1218 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001219 for (uint32_t i = 0; i < modifierCount; ++i) {
1220 JdwpEventMod& mod = pEvent->mods[i];
1221 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1222 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001223 case MK_COUNT: /* report once, when "--count" reaches 0 */
1224 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001225 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001226 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001227 if (count == 0) {
1228 return ERR_INVALID_COUNT;
1229 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001230 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001231 }
1232 break;
1233 case MK_CONDITIONAL: /* conditional on expression) */
1234 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001235 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001236 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001237 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001238 }
1239 break;
1240 case MK_THREAD_ONLY: /* only report events in specified thread */
1241 {
Elliott Hughes74847412012-06-20 18:10:21 -07001242 ObjectId thread_id = ReadObjectId(&buf);
1243 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1244 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001245 }
1246 break;
1247 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1248 {
Elliott Hughes74847412012-06-20 18:10:21 -07001249 RefTypeId class_id = ReadRefTypeId(&buf);
1250 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1251 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001252 }
1253 break;
1254 case MK_CLASS_MATCH: /* restrict events to matching classes */
1255 {
Elliott Hughes86964332012-02-15 19:37:42 -08001256 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001257 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001258 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001259 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001260 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001261 }
1262 break;
1263 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1264 {
Elliott Hughes86964332012-02-15 19:37:42 -08001265 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001266 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001267 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001268 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001269 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001270 }
1271 break;
1272 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1273 {
1274 JdwpLocation loc;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001275 JdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001276 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001277 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001278 }
1279 break;
1280 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1281 {
1282 RefTypeId exceptionOrNull; /* null == all exceptions */
1283 uint8_t caught, uncaught;
1284
1285 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001286 caught = Read1(&buf);
1287 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001288 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001289 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001290
Elliott Hughes972a47b2012-02-21 18:16:06 -08001291 mod.exceptionOnly.refTypeId = exceptionOrNull;
1292 mod.exceptionOnly.caught = caught;
1293 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001294 }
1295 break;
1296 case MK_FIELD_ONLY: /* for field access/mod events */
1297 {
1298 RefTypeId declaring = ReadRefTypeId(&buf);
1299 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001300 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001301 mod.fieldOnly.refTypeId = declaring;
1302 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001303 }
1304 break;
1305 case MK_STEP: /* for use with EK_SINGLE_STEP */
1306 {
Elliott Hughes74847412012-06-20 18:10:21 -07001307 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001308 uint32_t size, depth;
1309
Elliott Hughes74847412012-06-20 18:10:21 -07001310 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001311 size = Read4BE(&buf);
1312 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001313 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001314 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1315
Elliott Hughes74847412012-06-20 18:10:21 -07001316 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001317 mod.step.size = size;
1318 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001319 }
1320 break;
1321 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1322 {
1323 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001324 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001325 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001326 }
1327 break;
1328 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001329 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001330 break;
1331 }
1332 }
1333
1334 /*
1335 * Make sure we consumed all data. It is possible that the remote side
1336 * has sent us bad stuff, but for now we blame ourselves.
1337 */
1338 if (buf != origBuf + dataLen) {
1339 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1340 }
1341
1342 /*
1343 * We reply with an integer "requestID".
1344 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001345 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001346 expandBufAdd4BE(pReply, requestId);
1347
1348 pEvent->requestId = requestId;
1349
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001350 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001351
1352 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001353 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001354 if (err != ERR_NONE) {
1355 /* registration failed, probably because event is bogus */
1356 EventFree(pEvent);
1357 LOG(WARNING) << "WARNING: event request rejected";
1358 }
1359 return err;
1360}
1361
1362/*
1363 * Clear an event. Failure to find an event with a matching ID is a no-op
1364 * and does not return an error.
1365 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001366static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001367 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001368 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001369 eventKind = Read1(&buf);
1370 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001371
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001372 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001373
Elliott Hughes761928d2011-11-16 18:33:03 -08001374 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001375
1376 return ERR_NONE;
1377}
1378
1379/*
1380 * Return the values of arguments and local variables.
1381 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001382static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001384 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001385 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001386 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001387
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001388 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 -07001389
1390 expandBufAdd4BE(pReply, slots); /* "int values" */
1391 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001392 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001393 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001394
Elliott Hughes2435a572012-02-17 16:07:41 -08001395 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001396
Elliott Hughesdbb40792011-11-18 17:05:22 -08001397 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001398 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001399 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001400 }
1401
1402 return ERR_NONE;
1403}
1404
1405/*
1406 * Set the values of arguments and local variables.
1407 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001408static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001409 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001410 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001411 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001412 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001413
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001414 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 -07001415
1416 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001417 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001418 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001419 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001420 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001421
Elliott Hughes2435a572012-02-17 16:07:41 -08001422 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001423 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001424 }
1425
1426 return ERR_NONE;
1427}
1428
1429/*
1430 * Returns the value of "this" for the specified frame.
1431 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001433 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001434 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001435 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001436
Elliott Hughes546b9862012-06-20 16:06:13 -07001437 ObjectId id;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001438 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &id);
1439 if (rc != ERR_NONE) {
1440 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001441 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001442
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001443 uint8_t tag;
1444 rc = Dbg::GetObjectTag(id, tag);
1445 if (rc != ERR_NONE) {
1446 return rc;
1447 }
1448
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001449 VLOG(jdwp) << StringPrintf(" Req for 'this' in thread_id=%#llx frame=%lld --> %#llx '%c'",
1450 thread_id, frame_id, id, static_cast<char>(tag));
Elliott Hughes546b9862012-06-20 16:06:13 -07001451 expandBufAdd1(pReply, tag);
1452 expandBufAddObjectId(pReply, id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001453
1454 return ERR_NONE;
1455}
1456
1457/*
1458 * Return the reference type reflected by this class object.
1459 *
1460 * This appears to be required because ReferenceTypeId values are NEVER
1461 * reused, whereas ClassIds can be recycled like any other object. (Either
1462 * that, or I have no idea what this is for.)
1463 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001464static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001465 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001466 RefTypeId classObjectId = ReadRefTypeId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001467 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId,
1468 Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001469 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001470}
1471
1472/*
1473 * Handle a DDM packet with a single chunk in it.
1474 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001475static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001476 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001477 uint8_t* replyBuf = NULL;
1478 int replyLen = -1;
1479
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001480 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001481
Elliott Hughesa21039c2012-06-21 12:09:25 -07001482 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001483
1484 /*
1485 * If they want to send something back, we copy it into the buffer.
1486 * A no-copy approach would be nicer.
1487 *
1488 * TODO: consider altering the JDWP stuff to hold the packet header
1489 * in a separate buffer. That would allow us to writev() DDM traffic
1490 * instead of copying it into the expanding buffer. The reduction in
1491 * heap requirements is probably more valuable than the efficiency.
1492 */
1493 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1494 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1495 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1496 free(replyBuf);
1497 }
1498 return ERR_NONE;
1499}
1500
1501/*
1502 * Handler map decl.
1503 */
1504typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1505
1506struct JdwpHandlerMap {
1507 uint8_t cmdSet;
1508 uint8_t cmd;
1509 JdwpRequestHandler func;
1510 const char* descr;
1511};
1512
1513/*
1514 * Map commands to functions.
1515 *
1516 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1517 * and 128-256 are vendor-defined.
1518 */
1519static const JdwpHandlerMap gHandlerMap[] = {
1520 /* VirtualMachine command set (1) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001521 { 1, 1, VM_Version, "VirtualMachine.Version" },
1522 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1523 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1524 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1525 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1526 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1527 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1528 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1529 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1530 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1531 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1532 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1533 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1534 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001535 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1536 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001537 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001538 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1539 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001540 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001541 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001542
1543 /* ReferenceType command set (2) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001544 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1545 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1546 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1547 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1548 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1549 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1550 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001551 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001552 { 2, 9, RT_Status, "ReferenceType.Status" },
1553 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1554 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
1555 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1556 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1557 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1558 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001559 { 2, 16, NULL, "ReferenceType.Instances" },
1560 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1561 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001562
1563 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001564 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1565 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1566 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1567 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001568
1569 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001570 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001571
1572 /* InterfaceType command set (5) */
1573
1574 /* Method command set (6) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001575 { 6, 1, M_LineTable, "Method.LineTable" },
1576 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001577 { 6, 3, NULL, "Method.Bytecodes" },
1578 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001579 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001580
1581 /* Field command set (8) */
1582
1583 /* ObjectReference command set (9) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001584 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1585 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1586 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001587 { 9, 4, NULL, "ObjectReference.UNUSED" },
1588 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001589 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
1590 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
1591 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1592 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001593 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001594
1595 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001596 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001597
1598 /* ThreadReference command set (11) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001599 { 11, 1, TR_Name, "ThreadReference.Name" },
1600 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1601 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1602 { 11, 4, TR_Status, "ThreadReference.Status" },
1603 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1604 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1605 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001606 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001607 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001608 { 11, 10, NULL, "ThreadReference.Stop" },
Elliott Hughes74847412012-06-20 18:10:21 -07001609 { 11, 11, NULL, "ThreadReference.Interrupt" },
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001610 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001611 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1612 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001613
1614 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001615 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1616 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1617 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001618
1619 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001620 { 13, 1, AR_Length, "ArrayReference.Length" },
1621 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1622 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001623
1624 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001625 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001626
1627 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001628 { 15, 1, ER_Set, "EventRequest.Set" },
1629 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001630 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001631
1632 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001633 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1634 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1635 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001636 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001637
1638 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001639 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001640
1641 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001642 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001643
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001644 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001645};
1646
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001647static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001648 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001649 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1650 return gHandlerMap[i].descr;
1651 }
1652 }
1653 return "?UNKNOWN?";
1654}
1655
1656static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1657 std::string result;
1658 result += "REQ: ";
1659 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1660 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1661 return result;
1662}
1663
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001664/*
1665 * Process a request from the debugger.
1666 *
1667 * On entry, the JDWP thread is in VMWAIT.
1668 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001669void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001670 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001671
1672 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1673 /*
1674 * Activity from a debugger, not merely ddms. Mark us as having an
1675 * active debugger session, and zero out the last-activity timestamp
1676 * so waitForDebugger() doesn't return if we stall for a bit here.
1677 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001678 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001679 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001680 }
1681
1682 /*
1683 * If a debugger event has fired in another thread, wait until the
1684 * initiating thread has suspended itself before processing messages
1685 * from the debugger. Otherwise we (the JDWP thread) could be told to
1686 * resume the thread before it has suspended.
1687 *
1688 * We call with an argument of zero to wait for the current event
1689 * thread to finish, and then clear the block. Depending on the thread
1690 * suspend policy, this may allow events in other threads to fire,
1691 * but those events have no bearing on what the debugger has sent us
1692 * in the current request.
1693 *
1694 * Note that we MUST clear the event token before waking the event
1695 * thread up, or risk waiting for the thread to suspend after we've
1696 * told it to resume.
1697 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001698 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001699
1700 /*
1701 * Tell the VM that we're running and shouldn't be interrupted by GC.
1702 * Do this after anything that can stall indefinitely.
1703 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001704 Thread* self = Thread::Current();
1705 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001706
1707 expandBufAddSpace(pReply, kJDWPHeaderLen);
1708
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001709 size_t i;
1710 for (i = 0; i < arraysize(gHandlerMap); i++) {
1711 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001712 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001713 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001714 break;
1715 }
1716 }
1717 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001718 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1719 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001720 result = ERR_NOT_IMPLEMENTED;
1721 }
1722
1723 /*
1724 * Set up the reply header.
1725 *
1726 * If we encountered an error, only send the header back.
1727 */
1728 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001729 Set4BE(replyBuf + 4, pHeader->id);
1730 Set1(replyBuf + 8, kJDWPFlagReply);
1731 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001732 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001733 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001734 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001735 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001736 }
1737
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001738 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001739 if (false) {
1740 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001741 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001742 }
1743
1744 /*
1745 * Update last-activity timestamp. We really only need this during
1746 * the initial setup. Only update if this is a non-DDMS packet.
1747 */
1748 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001749 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001750 }
1751
1752 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001753 self->TransitionFromRunnableToSuspended(old_state);
1754
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001755}
1756
1757} // namespace JDWP
1758
1759} // namespace art