blob: cf5b7225e7951dee66f98cdf3c6efcb511f9343d [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 Hughes74847412012-06-20 18:10:21 -0700920 if (!Dbg::GetThreadName(thread_id, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700921 return ERR_INVALID_THREAD;
922 }
Elliott Hughes74847412012-06-20 18:10:21 -0700923 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800924 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700925
926 return ERR_NONE;
927}
928
929/*
930 * Suspend the specified thread.
931 *
932 * It's supposed to remain suspended even if interpreted code wants to
933 * resume it; only the JDI is allowed to resume it.
934 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700936 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700937 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700938
Elliott Hughes74847412012-06-20 18:10:21 -0700939 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700940 LOG(INFO) << " Warning: ignoring request to suspend self";
941 return ERR_THREAD_NOT_SUSPENDED;
942 }
Elliott Hughes74847412012-06-20 18:10:21 -0700943 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 Thread* self = Thread::Current();
945 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
946 JdwpError result = Dbg::SuspendThread(thread_id);
947 self->TransitionFromSuspendedToRunnable();
948 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700949}
950
951/*
952 * Resume the specified thread.
953 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700955 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700956 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700957
Elliott Hughes74847412012-06-20 18:10:21 -0700958 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700959 LOG(INFO) << " Warning: ignoring request to resume self";
960 return ERR_NONE;
961 }
Elliott Hughes74847412012-06-20 18:10:21 -0700962 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
963 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700964 return ERR_NONE;
965}
966
967/*
968 * Return status of specified thread.
969 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700971 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700972 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700973
Elliott Hughes74847412012-06-20 18:10:21 -0700974 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700975
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800976 JDWP::JdwpThreadStatus threadStatus;
977 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes74847412012-06-20 18:10:21 -0700978 if (!Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700979 return ERR_INVALID_THREAD;
980 }
981
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800982 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700983
984 expandBufAdd4BE(pReply, threadStatus);
985 expandBufAdd4BE(pReply, suspendStatus);
986
987 return ERR_NONE;
988}
989
990/*
991 * Return the thread group that the specified thread is a member of.
992 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700993static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700994 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700995 ObjectId thread_id = ReadObjectId(&buf);
996 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700997}
998
999/*
1000 * Return the current call stack of a suspended thread.
1001 *
1002 * If the thread isn't suspended, the error code isn't defined, but should
1003 * be THREAD_NOT_SUSPENDED.
1004 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001005static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001006 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001007 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001008 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001009 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001010
Elliott Hughes74847412012-06-20 18:10:21 -07001011 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001012 return ERR_INVALID_THREAD;
1013 }
Elliott Hughes74847412012-06-20 18:10:21 -07001014 if (!Dbg::IsSuspended(thread_id)) {
1015 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001016 return ERR_THREAD_NOT_SUSPENDED;
1017 }
1018
Elliott Hughes74847412012-06-20 18:10:21 -07001019 size_t actual_frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001020
Elliott Hughes74847412012-06-20 18:10:21 -07001021 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 -08001022 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001023 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1024 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001025
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001026 if (start_frame > actual_frame_count) {
1027 return ERR_INVALID_INDEX;
1028 }
1029 if (length == static_cast<uint32_t>(-1)) {
1030 length = actual_frame_count - start_frame;
1031 }
1032 if (start_frame + length > actual_frame_count) {
1033 return ERR_INVALID_LENGTH;
1034 }
1035
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001036 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001037}
1038
1039/*
1040 * Returns the #of frames on the specified thread, which must be suspended.
1041 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001043 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001044 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001045
Elliott Hughes74847412012-06-20 18:10:21 -07001046 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001047 return ERR_INVALID_THREAD;
1048 }
Elliott Hughes74847412012-06-20 18:10:21 -07001049 if (!Dbg::IsSuspended(thread_id)) {
1050 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001051 return ERR_THREAD_NOT_SUSPENDED;
1052 }
1053
Elliott Hughes74847412012-06-20 18:10:21 -07001054 int frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001055 if (frame_count < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001056 return ERR_INVALID_THREAD;
1057 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001058 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001059
1060 return ERR_NONE;
1061}
1062
1063/*
1064 * Get the monitor that the thread is waiting on.
1065 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001066static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001067 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001068 ReadObjectId(&buf); // thread_id
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001069
1070 // TODO: create an Object to represent the monitor (we're currently
1071 // just using a raw Monitor struct in the VM)
1072
1073 return ERR_NOT_IMPLEMENTED;
1074}
1075
1076/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001078 *
1079 * (The thread *might* still be running -- it might not have examined
1080 * its suspend count recently.)
1081 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082static JdwpError TR_DebugSuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001083 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001084 ObjectId thread_id = ReadObjectId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001086}
1087
1088/*
1089 * Return the name of a thread group.
1090 *
1091 * The Eclipse debugger recognizes "main" and "system" as special.
1092 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001094 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001095 ObjectId thread_group_id = ReadObjectId(&buf);
1096 VLOG(jdwp) << StringPrintf(" Req for name of thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001097
Elliott Hughescaf76542012-06-28 16:08:22 -07001098 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001099
1100 return ERR_NONE;
1101}
1102
1103/*
1104 * Returns the thread group -- if any -- that contains the specified
1105 * thread group.
1106 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001107static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001109 ObjectId thread_group_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001110
Elliott Hughescaf76542012-06-28 16:08:22 -07001111 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001112 expandBufAddObjectId(pReply, parentGroup);
1113
1114 return ERR_NONE;
1115}
1116
1117/*
1118 * Return the active threads and thread groups that are part of the
1119 * specified thread group.
1120 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001121static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001123 ObjectId thread_group_id = ReadObjectId(&buf);
1124 VLOG(jdwp) << StringPrintf(" Req for threads in thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001125
Elliott Hughescaf76542012-06-28 16:08:22 -07001126 std::vector<ObjectId> thread_ids;
1127 Dbg::GetThreads(thread_group_id, thread_ids);
1128 expandBufAdd4BE(pReply, thread_ids.size());
1129 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1130 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001131 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001132
Elliott Hughescaf76542012-06-28 16:08:22 -07001133 std::vector<ObjectId> child_thread_groups_ids;
1134 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1135 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1136 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1137 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001138 }
1139
1140 return ERR_NONE;
1141}
1142
1143/*
1144 * Return the #of components in the array.
1145 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001148 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001149 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001150
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001151 int length;
1152 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1153 if (status != ERR_NONE) {
1154 return status;
1155 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001156 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001157
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001158 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001159
1160 return ERR_NONE;
1161}
1162
1163/*
1164 * Return the values from an array.
1165 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001166static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001168 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001169 uint32_t firstIndex = Read4BE(&buf);
1170 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001171 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001172
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001173 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001174}
1175
1176/*
1177 * Set values in an array.
1178 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001179static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001181 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001182 uint32_t firstIndex = Read4BE(&buf);
1183 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001184
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001185 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId,
1186 firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001187
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001188 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001189}
1190
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001191static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001193 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001194 // TODO: we should only return classes which have the given class loader as a defining or
1195 // initiating loader. The former would be easy; the latter is hard, because we don't have
1196 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001197 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001198}
1199
1200/*
1201 * Set an event trigger.
1202 *
1203 * Reply with a requestID.
1204 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001205static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001206 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001207 const uint8_t* origBuf = buf;
1208
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001209 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001210 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001211 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001212
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001213 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001214 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001215 << " mods=" << modifierCount << ")";
1216
1217 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1218
1219 JdwpEvent* pEvent = EventAlloc(modifierCount);
1220 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001221 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001222 pEvent->modCount = modifierCount;
1223
1224 /*
1225 * Read modifiers. Ordering may be significant (see explanation of Count
1226 * mods in JDWP doc).
1227 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001228 for (uint32_t i = 0; i < modifierCount; ++i) {
1229 JdwpEventMod& mod = pEvent->mods[i];
1230 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1231 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001232 case MK_COUNT: /* report once, when "--count" reaches 0 */
1233 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001234 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001235 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001236 if (count == 0) {
1237 return ERR_INVALID_COUNT;
1238 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001239 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001240 }
1241 break;
1242 case MK_CONDITIONAL: /* conditional on expression) */
1243 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001244 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001245 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001246 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001247 }
1248 break;
1249 case MK_THREAD_ONLY: /* only report events in specified thread */
1250 {
Elliott Hughes74847412012-06-20 18:10:21 -07001251 ObjectId thread_id = ReadObjectId(&buf);
1252 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1253 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001254 }
1255 break;
1256 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1257 {
Elliott Hughes74847412012-06-20 18:10:21 -07001258 RefTypeId class_id = ReadRefTypeId(&buf);
1259 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1260 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001261 }
1262 break;
1263 case MK_CLASS_MATCH: /* restrict events to 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) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001269 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001270 }
1271 break;
1272 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1273 {
Elliott Hughes86964332012-02-15 19:37:42 -08001274 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001275 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001276 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001277 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001278 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001279 }
1280 break;
1281 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1282 {
1283 JdwpLocation loc;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001284 JdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001285 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001286 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001287 }
1288 break;
1289 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1290 {
1291 RefTypeId exceptionOrNull; /* null == all exceptions */
1292 uint8_t caught, uncaught;
1293
1294 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001295 caught = Read1(&buf);
1296 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001297 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001298 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001299
Elliott Hughes972a47b2012-02-21 18:16:06 -08001300 mod.exceptionOnly.refTypeId = exceptionOrNull;
1301 mod.exceptionOnly.caught = caught;
1302 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001303 }
1304 break;
1305 case MK_FIELD_ONLY: /* for field access/mod events */
1306 {
1307 RefTypeId declaring = ReadRefTypeId(&buf);
1308 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001309 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001310 mod.fieldOnly.refTypeId = declaring;
1311 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001312 }
1313 break;
1314 case MK_STEP: /* for use with EK_SINGLE_STEP */
1315 {
Elliott Hughes74847412012-06-20 18:10:21 -07001316 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001317 uint32_t size, depth;
1318
Elliott Hughes74847412012-06-20 18:10:21 -07001319 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001320 size = Read4BE(&buf);
1321 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001322 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001323 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1324
Elliott Hughes74847412012-06-20 18:10:21 -07001325 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001326 mod.step.size = size;
1327 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001328 }
1329 break;
1330 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1331 {
1332 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001333 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001334 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001335 }
1336 break;
1337 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001338 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001339 break;
1340 }
1341 }
1342
1343 /*
1344 * Make sure we consumed all data. It is possible that the remote side
1345 * has sent us bad stuff, but for now we blame ourselves.
1346 */
1347 if (buf != origBuf + dataLen) {
1348 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1349 }
1350
1351 /*
1352 * We reply with an integer "requestID".
1353 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001354 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001355 expandBufAdd4BE(pReply, requestId);
1356
1357 pEvent->requestId = requestId;
1358
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001359 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001360
1361 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001362 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001363 if (err != ERR_NONE) {
1364 /* registration failed, probably because event is bogus */
1365 EventFree(pEvent);
1366 LOG(WARNING) << "WARNING: event request rejected";
1367 }
1368 return err;
1369}
1370
1371/*
1372 * Clear an event. Failure to find an event with a matching ID is a no-op
1373 * and does not return an error.
1374 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001375static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001376 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001377 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001378 eventKind = Read1(&buf);
1379 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001380
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001381 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001382
Elliott Hughes761928d2011-11-16 18:33:03 -08001383 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001384
1385 return ERR_NONE;
1386}
1387
1388/*
1389 * Return the values of arguments and local variables.
1390 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001391static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001392 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001393 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001394 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001395 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001396
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001397 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 -07001398
1399 expandBufAdd4BE(pReply, slots); /* "int values" */
1400 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001401 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001402 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001403
Elliott Hughes2435a572012-02-17 16:07:41 -08001404 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001405
Elliott Hughesdbb40792011-11-18 17:05:22 -08001406 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001407 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001408 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001409 }
1410
1411 return ERR_NONE;
1412}
1413
1414/*
1415 * Set the values of arguments and local variables.
1416 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001418 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001419 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001420 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001421 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001422
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001423 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 -07001424
1425 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001426 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001427 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001428 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001429 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001430
Elliott Hughes2435a572012-02-17 16:07:41 -08001431 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001432 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001433 }
1434
1435 return ERR_NONE;
1436}
1437
1438/*
1439 * Returns the value of "this" for the specified frame.
1440 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001441static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001442 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001443 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001444 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001445
Elliott Hughes546b9862012-06-20 16:06:13 -07001446 ObjectId id;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001447 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &id);
1448 if (rc != ERR_NONE) {
1449 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001450 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001451
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001452 uint8_t tag;
1453 rc = Dbg::GetObjectTag(id, tag);
1454 if (rc != ERR_NONE) {
1455 return rc;
1456 }
1457
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001458 VLOG(jdwp) << StringPrintf(" Req for 'this' in thread_id=%#llx frame=%lld --> %#llx '%c'",
1459 thread_id, frame_id, id, static_cast<char>(tag));
Elliott Hughes546b9862012-06-20 16:06:13 -07001460 expandBufAdd1(pReply, tag);
1461 expandBufAddObjectId(pReply, id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001462
1463 return ERR_NONE;
1464}
1465
1466/*
1467 * Return the reference type reflected by this class object.
1468 *
1469 * This appears to be required because ReferenceTypeId values are NEVER
1470 * reused, whereas ClassIds can be recycled like any other object. (Either
1471 * that, or I have no idea what this is for.)
1472 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001473static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001474 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001475 RefTypeId classObjectId = ReadRefTypeId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001476 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId,
1477 Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001478 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001479}
1480
1481/*
1482 * Handle a DDM packet with a single chunk in it.
1483 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001484static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001486 uint8_t* replyBuf = NULL;
1487 int replyLen = -1;
1488
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001489 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001490
Elliott Hughesa21039c2012-06-21 12:09:25 -07001491 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001492
1493 /*
1494 * If they want to send something back, we copy it into the buffer.
1495 * A no-copy approach would be nicer.
1496 *
1497 * TODO: consider altering the JDWP stuff to hold the packet header
1498 * in a separate buffer. That would allow us to writev() DDM traffic
1499 * instead of copying it into the expanding buffer. The reduction in
1500 * heap requirements is probably more valuable than the efficiency.
1501 */
1502 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1503 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1504 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1505 free(replyBuf);
1506 }
1507 return ERR_NONE;
1508}
1509
1510/*
1511 * Handler map decl.
1512 */
1513typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1514
1515struct JdwpHandlerMap {
1516 uint8_t cmdSet;
1517 uint8_t cmd;
1518 JdwpRequestHandler func;
1519 const char* descr;
1520};
1521
1522/*
1523 * Map commands to functions.
1524 *
1525 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1526 * and 128-256 are vendor-defined.
1527 */
1528static const JdwpHandlerMap gHandlerMap[] = {
1529 /* VirtualMachine command set (1) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001530 { 1, 1, VM_Version, "VirtualMachine.Version" },
1531 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1532 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1533 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1534 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1535 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1536 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1537 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1538 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1539 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1540 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1541 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1542 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1543 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001544 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1545 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001546 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001547 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1548 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001549 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001550 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001551
1552 /* ReferenceType command set (2) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001553 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1554 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1555 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1556 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1557 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1558 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1559 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001560 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001561 { 2, 9, RT_Status, "ReferenceType.Status" },
1562 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1563 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
1564 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1565 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1566 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1567 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001568 { 2, 16, NULL, "ReferenceType.Instances" },
1569 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1570 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001571
1572 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001573 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1574 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1575 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1576 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001577
1578 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001579 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001580
1581 /* InterfaceType command set (5) */
1582
1583 /* Method command set (6) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001584 { 6, 1, M_LineTable, "Method.LineTable" },
1585 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001586 { 6, 3, NULL, "Method.Bytecodes" },
1587 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001588 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001589
1590 /* Field command set (8) */
1591
1592 /* ObjectReference command set (9) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001593 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1594 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1595 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001596 { 9, 4, NULL, "ObjectReference.UNUSED" },
1597 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001598 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
1599 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
1600 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1601 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001602 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001603
1604 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001605 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001606
1607 /* ThreadReference command set (11) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001608 { 11, 1, TR_Name, "ThreadReference.Name" },
1609 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1610 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1611 { 11, 4, TR_Status, "ThreadReference.Status" },
1612 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1613 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1614 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001615 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001616 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001617 { 11, 10, NULL, "ThreadReference.Stop" },
Elliott Hughes74847412012-06-20 18:10:21 -07001618 { 11, 11, NULL, "ThreadReference.Interrupt" },
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001619 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001620 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1621 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001622
1623 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001624 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1625 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1626 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001627
1628 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001629 { 13, 1, AR_Length, "ArrayReference.Length" },
1630 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1631 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001632
1633 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001634 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001635
1636 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001637 { 15, 1, ER_Set, "EventRequest.Set" },
1638 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001639 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001640
1641 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001642 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1643 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1644 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001645 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001646
1647 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001648 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001649
1650 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001651 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001652
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001653 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001654};
1655
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001656static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001657 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001658 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1659 return gHandlerMap[i].descr;
1660 }
1661 }
1662 return "?UNKNOWN?";
1663}
1664
1665static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1666 std::string result;
1667 result += "REQ: ";
1668 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1669 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1670 return result;
1671}
1672
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001673/*
1674 * Process a request from the debugger.
1675 *
1676 * On entry, the JDWP thread is in VMWAIT.
1677 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001678void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001679 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001680
1681 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1682 /*
1683 * Activity from a debugger, not merely ddms. Mark us as having an
1684 * active debugger session, and zero out the last-activity timestamp
1685 * so waitForDebugger() doesn't return if we stall for a bit here.
1686 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001687 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001688 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001689 }
1690
1691 /*
1692 * If a debugger event has fired in another thread, wait until the
1693 * initiating thread has suspended itself before processing messages
1694 * from the debugger. Otherwise we (the JDWP thread) could be told to
1695 * resume the thread before it has suspended.
1696 *
1697 * We call with an argument of zero to wait for the current event
1698 * thread to finish, and then clear the block. Depending on the thread
1699 * suspend policy, this may allow events in other threads to fire,
1700 * but those events have no bearing on what the debugger has sent us
1701 * in the current request.
1702 *
1703 * Note that we MUST clear the event token before waking the event
1704 * thread up, or risk waiting for the thread to suspend after we've
1705 * told it to resume.
1706 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001707 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001708
1709 /*
1710 * Tell the VM that we're running and shouldn't be interrupted by GC.
1711 * Do this after anything that can stall indefinitely.
1712 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001713 Thread* self = Thread::Current();
1714 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001715
1716 expandBufAddSpace(pReply, kJDWPHeaderLen);
1717
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001718 size_t i;
1719 for (i = 0; i < arraysize(gHandlerMap); i++) {
1720 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001721 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001722 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001723 break;
1724 }
1725 }
1726 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001727 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1728 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001729 result = ERR_NOT_IMPLEMENTED;
1730 }
1731
1732 /*
1733 * Set up the reply header.
1734 *
1735 * If we encountered an error, only send the header back.
1736 */
1737 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001738 Set4BE(replyBuf + 4, pHeader->id);
1739 Set1(replyBuf + 8, kJDWPFlagReply);
1740 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001741 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001742 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001743 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001744 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001745 }
1746
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001747 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001748 if (false) {
1749 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001750 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001751 }
1752
1753 /*
1754 * Update last-activity timestamp. We really only need this during
1755 * the initial setup. Only update if this is a non-DDMS packet.
1756 */
1757 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001758 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001759 }
1760
1761 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001762 self->TransitionFromRunnableToSuspended(old_state);
1763
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001764}
1765
1766} // namespace JDWP
1767
1768} // namespace art