blob: 890bc29d76d642405818d82b9af4746d55e69d73 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Handle messages from debugger.
19 *
20 * GENERAL NOTE: we're not currently testing the message length for
21 * correctness. This is usually a bad idea, but here we can probably
22 * get away with it so long as the debugger isn't broken. We can
23 * change the "read" macros to use "dataLen" to avoid wandering into
24 * bad territory, and have a single "is dataLen correct" check at the
25 * end of each function. Not needed at this time.
26 */
27
Elliott Hughes872d4ec2011-10-21 17:07:15 -070028#include "jdwp/jdwp_handler.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070029
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
Elliott Hughes07ed66b2012-12-12 18:34:25 -080034#include "atomic.h"
35#include "base/logging.h"
36#include "base/macros.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080037#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080038#include "debugger.h"
39#include "jdwp/jdwp_constants.h"
40#include "jdwp/jdwp_event.h"
41#include "jdwp/jdwp_expand_buf.h"
42#include "jdwp/jdwp_priv.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080043
Elliott Hughes872d4ec2011-10-21 17:07:15 -070044namespace art {
45
46namespace JDWP {
47
48/*
49 * Helper function: read a "location" from an input buffer.
50 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070051static void JdwpReadLocation(const uint8_t** pBuf, JdwpLocation* pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070052 memset(pLoc, 0, sizeof(*pLoc)); /* allows memcmp() later */
Elliott Hughes74847412012-06-20 18:10:21 -070053 pLoc->type_tag = ReadTypeTag(pBuf);
54 pLoc->class_id = ReadObjectId(pBuf);
55 pLoc->method_id = ReadMethodId(pBuf);
Elliott Hughes972a47b2012-02-21 18:16:06 -080056 pLoc->dex_pc = Read8BE(pBuf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070057}
58
59/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -070060 * Helper function: read a variable-width value from the input buffer.
61 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070062static uint64_t JdwpReadValue(const uint8_t** pBuf, size_t width) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070063 uint64_t value = -1;
64 switch (width) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070065 case 1: value = Read1(pBuf); break;
66 case 2: value = Read2BE(pBuf); break;
67 case 4: value = Read4BE(pBuf); break;
68 case 8: value = Read8BE(pBuf); break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070069 default: LOG(FATAL) << width; break;
70 }
71 return value;
72}
73
74/*
75 * Helper function: write a variable-width value into the output input buffer.
76 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070077static void JdwpWriteValue(ExpandBuf* pReply, int width, uint64_t value) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070078 switch (width) {
79 case 1: expandBufAdd1(pReply, value); break;
80 case 2: expandBufAdd2BE(pReply, value); break;
81 case 4: expandBufAdd4BE(pReply, value); break;
82 case 8: expandBufAdd8BE(pReply, value); break;
83 default: LOG(FATAL) << width; break;
84 }
85}
86
87/*
88 * Common code for *_InvokeMethod requests.
89 *
Elliott Hughes74847412012-06-20 18:10:21 -070090 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -070091 * expected-to-be-void return value of the called function.
92 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070093static JdwpError FinishInvoke(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
Elliott Hughes74847412012-06-20 18:10:21 -070094 ObjectId thread_id, ObjectId object_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 RefTypeId class_id, MethodId method_id, bool is_constructor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -070097 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070098
Elliott Hughes45651fd2012-02-21 15:48:20 -080099 uint32_t arg_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700100
Elliott Hughes74847412012-06-20 18:10:21 -0700101 VLOG(jdwp) << StringPrintf(" --> thread_id=%#llx object_id=%#llx", thread_id, object_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 VLOG(jdwp) << StringPrintf(" class_id=%#llx method_id=%x %s.%s", class_id,
103 method_id, Dbg::GetClassName(class_id).c_str(),
104 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800105 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700106
Elliott Hughes45651fd2012-02-21 15:48:20 -0800107 UniquePtr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
108 UniquePtr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
109 for (uint32_t i = 0; i < arg_count; ++i) {
110 argTypes[i] = ReadTag(&buf);
111 size_t width = Dbg::GetTagWidth(argTypes[i]);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700112 argValues[i] = JdwpReadValue(&buf, width);
Elliott Hughes229feb72012-02-23 13:33:29 -0800113 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#llx", width, argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700114 }
115
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700116 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700117 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
118 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
119 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700120
Elliott Hughes45651fd2012-02-21 15:48:20 -0800121 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700122 uint64_t resultValue;
123 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700124 JdwpError err = Dbg::InvokeMethod(thread_id, object_id, class_id, method_id, arg_count, argValues.get(), argTypes.get(), options, &resultTag, &resultValue, &exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800126 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700127 }
128
129 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800130 if (is_constructor) {
131 // If we invoked a constructor (which actually returns void), return the receiver,
132 // unless we threw, in which case we return NULL.
133 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700134 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800135 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700136
Elliott Hughes45651fd2012-02-21 15:48:20 -0800137 size_t width = Dbg::GetTagWidth(resultTag);
138 expandBufAdd1(pReply, resultTag);
139 if (width != 0) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700140 JdwpWriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700141 }
142 expandBufAdd1(pReply, JT_OBJECT);
143 expandBufAddObjectId(pReply, exceptObjId);
144
Elliott Hughes229feb72012-02-23 13:33:29 -0800145 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" %#llx (except=%#llx)", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700146
147 /* show detailed debug output */
148 if (resultTag == JT_STRING && exceptObjId == 0) {
149 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800150 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700151 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800152 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700153 }
154 }
155 }
156
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700157 return err;
158}
159
160
161/*
162 * Request for version info.
163 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164static JdwpError VM_Version(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700166 /* text information on runtime version */
167 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800168 expandBufAddUtf8String(pReply, version);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700169 /* JDWP version numbers */
170 expandBufAdd4BE(pReply, 1); // major
171 expandBufAdd4BE(pReply, 5); // minor
172 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800173 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700174 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800175 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176
177 return ERR_NONE;
178}
179
180/*
181 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
182 * referenceTypeID. We need to send back more than one if the class has
183 * been loaded by multiple class loaders.
184 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700185static JdwpError VM_ClassesBySignature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800187 std::string classDescriptor(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800188 VLOG(jdwp) << " Req for class by signature '" << classDescriptor << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800190 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800191 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700192
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800193 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700194
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800195 for (size_t i = 0; i < ids.size(); ++i) {
196 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800197 JDWP::JdwpTypeTag type_tag;
198 uint32_t class_status;
199 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
200 if (status != ERR_NONE) {
201 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800202 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700203
Elliott Hughes436e3722012-02-17 20:01:47 -0800204 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800205 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800206 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700207 }
208
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700209 return ERR_NONE;
210}
211
212/*
213 * Handle request for the thread IDs of all running threads.
214 *
215 * We exclude ourselves from the list, because we don't allow ourselves
216 * to be suspended, and that violates some JDWP expectations.
217 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218static JdwpError VM_AllThreads(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700220 std::vector<ObjectId> thread_ids;
Elliott Hughes026b1462012-06-28 20:43:49 -0700221 Dbg::GetThreads(0, thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700222
Elliott Hughescaf76542012-06-28 16:08:22 -0700223 expandBufAdd4BE(pReply, thread_ids.size());
224 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
225 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226 }
227
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700228 return ERR_NONE;
229}
230
231/*
232 * List all thread groups that do not have a parent.
233 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234static JdwpError VM_TopLevelThreadGroups(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700235 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700236 /*
237 * TODO: maintain a list of parentless thread groups in the VM.
238 *
239 * For now, just return "system". Application threads are created
240 * in "main", which is a child of "system".
241 */
242 uint32_t groups = 1;
243 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700244 //thread_group_id = debugGetMainThreadGroup();
245 //expandBufAdd8BE(pReply, thread_group_id);
246 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
247 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700248
249 return ERR_NONE;
250}
251
252/*
253 * Respond with the sizes of the basic debugger types.
254 *
255 * All IDs are 8 bytes.
256 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257static JdwpError VM_IDSizes(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700259 expandBufAdd4BE(pReply, sizeof(FieldId));
260 expandBufAdd4BE(pReply, sizeof(MethodId));
261 expandBufAdd4BE(pReply, sizeof(ObjectId));
262 expandBufAdd4BE(pReply, sizeof(RefTypeId));
263 expandBufAdd4BE(pReply, sizeof(FrameId));
264 return ERR_NONE;
265}
266
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267static JdwpError VM_Dispose(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes86964332012-02-15 19:37:42 -0800269 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700270 return ERR_NONE;
271}
272
273/*
274 * Suspend the execution of the application running in the VM (i.e. suspend
275 * all threads).
276 *
277 * This needs to increment the "suspend count" on all threads.
278 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279static JdwpError VM_Suspend(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700280 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800281 Thread* self = Thread::Current();
282 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700283 Dbg::SuspendVM();
jeffhaoa77f0f62012-12-05 17:19:31 -0800284 self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700285 return ERR_NONE;
286}
287
288/*
289 * Resume execution. Decrements the "suspend count" of all threads.
290 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291static JdwpError VM_Resume(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700293 Dbg::ResumeVM();
294 return ERR_NONE;
295}
296
297/*
298 * The debugger wants the entire VM to exit.
299 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700300static JdwpError VM_Exit(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700302 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700303
304 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
305
306 Dbg::Exit(exitCode);
307 return ERR_NOT_IMPLEMENTED; // shouldn't get here
308}
309
310/*
311 * Create a new string in the VM and return its ID.
312 *
313 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
314 * string "java.util.Arrays".)
315 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700316static JdwpError VM_CreateString(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800318 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800319 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700320 ObjectId stringId = Dbg::CreateString(str);
321 if (stringId == 0) {
322 return ERR_OUT_OF_MEMORY;
323 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700324 expandBufAddObjectId(pReply, stringId);
325 return ERR_NONE;
326}
327
328/*
329 * Tell the debugger what we are capable of.
330 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331static JdwpError VM_Capabilities(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700332 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700333 expandBufAdd1(pReply, false); /* canWatchFieldModification */
334 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
335 expandBufAdd1(pReply, false); /* canGetBytecodes */
336 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
337 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
338 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
339 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
340 return ERR_NONE;
341}
342
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343static JdwpError VM_ClassPaths(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800345 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700346
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800347 std::vector<std::string> class_path;
348 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
349 expandBufAdd4BE(pReply, class_path.size());
350 for (size_t i = 0; i < class_path.size(); ++i) {
351 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700352 }
353
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800354 std::vector<std::string> boot_class_path;
355 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
356 expandBufAdd4BE(pReply, boot_class_path.size());
357 for (size_t i = 0; i < boot_class_path.size(); ++i) {
358 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700359 }
360
361 return ERR_NONE;
362}
363
364/*
365 * Release a list of object IDs. (Seen in jdb.)
366 *
367 * Currently does nothing.
368 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369static JdwpError VM_DisposeObjects(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700370 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700371 return ERR_NONE;
372}
373
374/*
375 * Tell the debugger what we are capable of.
376 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377static JdwpError VM_CapabilitiesNew(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700378 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700379 expandBufAdd1(pReply, false); /* canWatchFieldModification */
380 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
381 expandBufAdd1(pReply, false); /* canGetBytecodes */
382 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
383 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
384 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
385 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
386 expandBufAdd1(pReply, false); /* canRedefineClasses */
387 expandBufAdd1(pReply, false); /* canAddMethod */
388 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
389 expandBufAdd1(pReply, false); /* canPopFrames */
390 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
391 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
392 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
393 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
394 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
395 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
396 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
397 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
398 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
399 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
400
401 /* fill in reserved22 through reserved32; note count started at 1 */
402 for (int i = 22; i <= 32; i++) {
403 expandBufAdd1(pReply, false); /* reservedN */
404 }
405 return ERR_NONE;
406}
407
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700409 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800410 std::vector<JDWP::RefTypeId> classes;
411 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700412
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800413 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700414
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800415 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800416 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800417 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800418 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800419 uint32_t class_status;
420 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
421 if (status != ERR_NONE) {
422 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800423 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700424
Elliott Hughes436e3722012-02-17 20:01:47 -0800425 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800426 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800427 if (descriptor_and_status) {
428 expandBufAddUtf8String(pReply, descriptor);
429 if (generic) {
430 expandBufAddUtf8String(pReply, genericSignature);
431 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800432 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800433 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700434 }
435
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700436 return ERR_NONE;
437}
438
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439static JdwpError VM_AllClasses(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700440 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700441 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800442}
443
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700444static JdwpError VM_AllClassesWithGeneric(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700445 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700446 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800447}
448
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700449static JdwpError RT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700450 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700451 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800452 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700453}
454
455/*
456 * Get values from static fields in a reference type.
457 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700458static JdwpError RT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800460 RefTypeId refTypeId = ReadRefTypeId(&buf);
461 uint32_t field_count = Read4BE(&buf);
462 expandBufAdd4BE(pReply, field_count);
463 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700464 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800465 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800466 if (status != ERR_NONE) {
467 return status;
468 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700469 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700470 return ERR_NONE;
471}
472
473/*
474 * Get the name of the source file in which a reference type was declared.
475 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700476static JdwpError RT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700477 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700478 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800479 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800480 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
481 if (status != ERR_NONE) {
482 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700483 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800484 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800485 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700486}
487
488/*
489 * Return the current status of the reference type.
490 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700491static JdwpError RT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700492 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700493 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800494 JDWP::JdwpTypeTag type_tag;
495 uint32_t class_status;
496 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
497 if (status != ERR_NONE) {
498 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800499 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800500 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700501 return ERR_NONE;
502}
503
504/*
505 * Return interfaces implemented directly by this class.
506 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700507static JdwpError RT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700508 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700509 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800510 VLOG(jdwp) << StringPrintf(" Req for interfaces in %#llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800511 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700512}
513
514/*
515 * Return the class object corresponding to this type.
516 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700517static JdwpError RT_ClassObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700519 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800520 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800521 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
522 if (status != ERR_NONE) {
523 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800524 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800525 VLOG(jdwp) << StringPrintf(" RefTypeId %#llx -> ObjectId %#llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800526 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700527 return ERR_NONE;
528}
529
530/*
531 * Returns the value of the SourceDebugExtension attribute.
532 *
533 * JDB seems interested, but DEX files don't currently support this.
534 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700535static JdwpError RT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700536 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700537 /* referenceTypeId in, string out */
538 return ERR_ABSENT_INFORMATION;
539}
540
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700541static JdwpError RT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
542 bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700543 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700544 RefTypeId refTypeId = ReadRefTypeId(&buf);
545
Elliott Hughes229feb72012-02-23 13:33:29 -0800546 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=%#llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800547 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800548
549 JdwpError status = Dbg::GetSignature(refTypeId, signature);
550 if (status != ERR_NONE) {
551 return status;
552 }
553 expandBufAddUtf8String(pReply, signature);
554 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800555 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700556 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700557 return ERR_NONE;
558}
559
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700560static JdwpError RT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700561 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700562 return RT_Signature(state, buf, dataLen, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800563}
564
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700565static JdwpError RT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
566 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700567 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700568 return RT_Signature(state, buf, dataLen, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800569}
570
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700571/*
572 * Return the instance of java.lang.ClassLoader that loaded the specified
573 * reference type, or null if it was loaded by the system loader.
574 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700575static JdwpError RT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700576 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700577 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800578 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700579}
580
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700581static std::string Describe(const RefTypeId& refTypeId)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700582 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800583 std::string signature("unknown");
584 Dbg::GetSignature(refTypeId, signature);
Elliott Hughes229feb72012-02-23 13:33:29 -0800585 return StringPrintf("refTypeId=%#llx (%s)", refTypeId, signature.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800586}
587
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700588/*
589 * Given a referenceTypeId, return a block of stuff that describes the
590 * fields declared by a class.
591 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700592static JdwpError RT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700593 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700594 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800595 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800596 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800597}
598
599// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700600static JdwpError RT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700601 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800602 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800603 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800604 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700605}
606
607/*
608 * Given a referenceTypeID, return a block of goodies describing the
609 * methods declared by a class.
610 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611static JdwpError RT_MethodsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700612 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700613 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800614 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800615 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800616}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700617
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800618// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619static JdwpError RT_Methods(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700620 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800621 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800622 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800623 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700624}
625
626/*
627 * Return the immediate superclass of a class.
628 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700629static JdwpError CT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700630 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700631 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800632 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700633 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800634 if (status != ERR_NONE) {
635 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800636 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700637 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700638 return ERR_NONE;
639}
640
641/*
642 * Set static class values.
643 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700644static JdwpError CT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700645 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700646 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700647 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700648
Elliott Hughes74847412012-06-20 18:10:21 -0700649 VLOG(jdwp) << StringPrintf(" Req to set %d values in class_id=%#llx", values, class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700650
651 for (uint32_t i = 0; i < values; i++) {
652 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800653 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800654 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700655 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700656
Elliott Hughes2435a572012-02-17 16:07:41 -0800657 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800658 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
659 if (status != ERR_NONE) {
660 return status;
661 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700662 }
663
664 return ERR_NONE;
665}
666
667/*
668 * Invoke a static method.
669 *
670 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
671 * values in the "variables" display.
672 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673static JdwpError CT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
674 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700675 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700676 RefTypeId class_id = ReadRefTypeId(&buf);
677 ObjectId thread_id = ReadObjectId(&buf);
678 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700679
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700680 return FinishInvoke(state, buf, dataLen, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700681}
682
683/*
684 * Create a new object of the requested type, and invoke the specified
685 * constructor.
686 *
687 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
688 * see the contents of a byte[] as a string.
689 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690static JdwpError CT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen,
691 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700692 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700693 RefTypeId class_id = ReadRefTypeId(&buf);
694 ObjectId thread_id = ReadObjectId(&buf);
695 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700696
Elliott Hughes74847412012-06-20 18:10:21 -0700697 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(class_id);
698 ObjectId object_id;
699 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800700 if (status != ERR_NONE) {
701 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800702 }
Elliott Hughes74847412012-06-20 18:10:21 -0700703 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700704 return ERR_OUT_OF_MEMORY;
705 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700706 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700707}
708
709/*
710 * Create a new array object of the requested type and length.
711 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712static JdwpError AT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700713 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700714 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700715 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700716
Elliott Hughes2435a572012-02-17 16:07:41 -0800717 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes74847412012-06-20 18:10:21 -0700718 ObjectId object_id;
719 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800720 if (status != ERR_NONE) {
721 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800722 }
Elliott Hughes74847412012-06-20 18:10:21 -0700723 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700724 return ERR_OUT_OF_MEMORY;
725 }
726 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700727 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700728 return ERR_NONE;
729}
730
731/*
732 * Return line number information for the method, if present.
733 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734static JdwpError M_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700735 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700736 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700737 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700738
Elliott Hughes74847412012-06-20 18:10:21 -0700739 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId, method_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700740
Elliott Hughes74847412012-06-20 18:10:21 -0700741 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700742
743 return ERR_NONE;
744}
745
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700746static JdwpError M_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
747 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700748 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700749 RefTypeId class_id = ReadRefTypeId(&buf);
750 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700751
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700752 VLOG(jdwp) << StringPrintf(" Req for LocalVarTab in class=%s method=%s",
753 Dbg::GetClassName(class_id).c_str(),
754 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700755
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800756 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
757 // information. That will cause Eclipse to make a best-effort attempt at displaying local
758 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
759 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700760 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700761 return ERR_NONE;
762}
763
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700764static JdwpError M_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen,
765 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700766 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700767 return M_VariableTable(state, buf, dataLen, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800768}
769
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700770static JdwpError M_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
771 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700772 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700773 return M_VariableTable(state, buf, dataLen, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800774}
775
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700776/*
777 * Given an object reference, return the runtime type of the object
778 * (class or array).
779 *
Elliott Hughes74847412012-06-20 18:10:21 -0700780 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700781 * passed in here.
782 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700783static JdwpError OR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700784 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700785 ObjectId object_id = ReadObjectId(&buf);
786 VLOG(jdwp) << StringPrintf(" Req for type of object_id=%#llx", object_id);
787 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700788}
789
790/*
791 * Get values from the fields of an object.
792 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700793static JdwpError OR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700794 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700795 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800796 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700797
Elliott Hughes74847412012-06-20 18:10:21 -0700798 VLOG(jdwp) << StringPrintf(" Req for %d fields from object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700799
Elliott Hughes0cf74332012-02-23 23:14:00 -0800800 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801
Elliott Hughes0cf74332012-02-23 23:14:00 -0800802 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700803 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700804 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800805 if (status != ERR_NONE) {
806 return status;
807 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700808 }
809
810 return ERR_NONE;
811}
812
813/*
814 * Set values in the fields of an object.
815 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700816static JdwpError OR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700817 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700818 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800819 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700820
Elliott Hughes74847412012-06-20 18:10:21 -0700821 VLOG(jdwp) << StringPrintf(" Req to set %d fields in object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700822
Elliott Hughes0cf74332012-02-23 23:14:00 -0800823 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700824 FieldId fieldId = ReadFieldId(&buf);
825
Elliott Hughesaed4be92011-12-02 16:16:23 -0800826 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800827 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700828 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700829
Elliott Hughes2435a572012-02-17 16:07:41 -0800830 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700831 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800832 if (status != ERR_NONE) {
833 return status;
834 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700835 }
836
837 return ERR_NONE;
838}
839
840/*
841 * Invoke an instance method. The invocation must occur in the specified
842 * thread, which must have been suspended by an event.
843 *
844 * The call is synchronous. All threads in the VM are resumed, unless the
845 * SINGLE_THREADED flag is set.
846 *
847 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
848 * object), it will try to invoke the object's toString() function. This
849 * feature becomes crucial when examining ArrayLists with Eclipse.
850 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700851static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
852 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700853 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700854 ObjectId object_id = ReadObjectId(&buf);
855 ObjectId thread_id = ReadObjectId(&buf);
856 RefTypeId class_id = ReadRefTypeId(&buf);
857 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700858
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700859 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700860}
861
862/*
863 * Disable garbage collection of the specified object.
864 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865static JdwpError OR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700866 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700867 // this is currently a no-op
868 return ERR_NONE;
869}
870
871/*
872 * Enable garbage collection of the specified object.
873 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700874static JdwpError OR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700875 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700876 // this is currently a no-op
877 return ERR_NONE;
878}
879
880/*
881 * Determine whether an object has been garbage collected.
882 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700883static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700884 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700885 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700886
Elliott Hughes74847412012-06-20 18:10:21 -0700887 object_id = ReadObjectId(&buf);
888 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700889
890 // TODO: currently returning false; must integrate with GC
891 expandBufAdd1(pReply, 0);
892
893 return ERR_NONE;
894}
895
896/*
897 * Return the string value in a string object.
898 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700899static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700900 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700901 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800902 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700903
Elliott Hughes82914b62012-04-09 15:56:29 -0700904 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700905
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800906 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700907
908 return ERR_NONE;
909}
910
911/*
912 * Return a thread's name.
913 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700915 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700916 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700917
Elliott Hughes74847412012-06-20 18:10:21 -0700918 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800919 std::string name;
Elliott Hughes221229c2013-01-08 18:17:50 -0800920 JdwpError error = Dbg::GetThreadName(thread_id, name);
921 if (error != ERR_NONE) {
922 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700923 }
Elliott Hughes74847412012-06-20 18:10:21 -0700924 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800925 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700926
927 return ERR_NONE;
928}
929
930/*
931 * Suspend the specified thread.
932 *
933 * It's supposed to remain suspended even if interpreted code wants to
934 * resume it; only the JDI is allowed to resume it.
935 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700937 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700938 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700939
Elliott Hughes74847412012-06-20 18:10:21 -0700940 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700941 LOG(INFO) << " Warning: ignoring request to suspend self";
942 return ERR_THREAD_NOT_SUSPENDED;
943 }
Elliott Hughes74847412012-06-20 18:10:21 -0700944 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 Thread* self = Thread::Current();
946 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
947 JdwpError result = Dbg::SuspendThread(thread_id);
948 self->TransitionFromSuspendedToRunnable();
949 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700950}
951
952/*
953 * Resume the specified thread.
954 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700956 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700957 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700958
Elliott Hughes74847412012-06-20 18:10:21 -0700959 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700960 LOG(INFO) << " Warning: ignoring request to resume self";
961 return ERR_NONE;
962 }
Elliott Hughes74847412012-06-20 18:10:21 -0700963 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
964 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700965 return ERR_NONE;
966}
967
968/*
969 * Return status of specified thread.
970 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700972 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700973 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700974
Elliott Hughes74847412012-06-20 18:10:21 -0700975 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700976
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800977 JDWP::JdwpThreadStatus threadStatus;
978 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -0800979 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
980 if (error != ERR_NONE) {
981 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700982 }
983
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800984 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700985
986 expandBufAdd4BE(pReply, threadStatus);
987 expandBufAdd4BE(pReply, suspendStatus);
988
989 return ERR_NONE;
990}
991
992/*
993 * Return the thread group that the specified thread is a member of.
994 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700995static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700996 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700997 ObjectId thread_id = ReadObjectId(&buf);
998 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700999}
1000
1001/*
1002 * Return the current call stack of a suspended thread.
1003 *
1004 * If the thread isn't suspended, the error code isn't defined, but should
1005 * be THREAD_NOT_SUSPENDED.
1006 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001007static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001008 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001009 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001010 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001011 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001012
Elliott Hughes221229c2013-01-08 18:17:50 -08001013 bool is_suspended;
1014 JdwpError error = Dbg::IsSuspended(thread_id, is_suspended);
1015 if (error != ERR_NONE) {
1016 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001017 }
Elliott Hughes221229c2013-01-08 18:17:50 -08001018 if (!is_suspended) {
Elliott Hughes74847412012-06-20 18:10:21 -07001019 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001020 return ERR_THREAD_NOT_SUSPENDED;
1021 }
1022
Elliott Hughes221229c2013-01-08 18:17:50 -08001023 size_t actual_frame_count;
1024 error = Dbg::GetThreadFrameCount(thread_id, actual_frame_count);
1025 if (error != ERR_NONE) {
1026 return error;
1027 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001028
Elliott Hughes74847412012-06-20 18:10:21 -07001029 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 -08001030 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001031 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1032 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001033
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001034 if (start_frame > actual_frame_count) {
1035 return ERR_INVALID_INDEX;
1036 }
1037 if (length == static_cast<uint32_t>(-1)) {
1038 length = actual_frame_count - start_frame;
1039 }
1040 if (start_frame + length > actual_frame_count) {
1041 return ERR_INVALID_LENGTH;
1042 }
1043
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001044 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001045}
1046
1047/*
1048 * Returns the #of frames on the specified thread, which must be suspended.
1049 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001050static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001051 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001052 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001053
Elliott Hughes221229c2013-01-08 18:17:50 -08001054 bool is_suspended;
1055 JdwpError error = Dbg::IsSuspended(thread_id, is_suspended);
1056 if (error != ERR_NONE) {
1057 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001058 }
Elliott Hughes221229c2013-01-08 18:17:50 -08001059 if (!is_suspended) {
Elliott Hughes74847412012-06-20 18:10:21 -07001060 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001061 return ERR_THREAD_NOT_SUSPENDED;
1062 }
1063
Elliott Hughes221229c2013-01-08 18:17:50 -08001064 size_t frame_count;
1065 error = Dbg::GetThreadFrameCount(thread_id, frame_count);
1066 if (error != ERR_NONE) {
1067 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001068 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001069 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001070
1071 return ERR_NONE;
1072}
1073
1074/*
1075 * Get the monitor that the thread is waiting on.
1076 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001078 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001079 ReadObjectId(&buf); // thread_id
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001080
1081 // TODO: create an Object to represent the monitor (we're currently
1082 // just using a raw Monitor struct in the VM)
1083
1084 return ERR_NOT_IMPLEMENTED;
1085}
1086
1087/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001089 *
1090 * (The thread *might* still be running -- it might not have examined
1091 * its suspend count recently.)
1092 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093static JdwpError TR_DebugSuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001094 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001095 ObjectId thread_id = ReadObjectId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001096 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001097}
1098
1099/*
1100 * Return the name of a thread group.
1101 *
1102 * The Eclipse debugger recognizes "main" and "system" as special.
1103 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001104static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001106 ObjectId thread_group_id = ReadObjectId(&buf);
1107 VLOG(jdwp) << StringPrintf(" Req for name of thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001108
Elliott Hughescaf76542012-06-28 16:08:22 -07001109 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001110
1111 return ERR_NONE;
1112}
1113
1114/*
1115 * Returns the thread group -- if any -- that contains the specified
1116 * thread group.
1117 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001120 ObjectId thread_group_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001121
Elliott Hughescaf76542012-06-28 16:08:22 -07001122 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001123 expandBufAddObjectId(pReply, parentGroup);
1124
1125 return ERR_NONE;
1126}
1127
1128/*
1129 * Return the active threads and thread groups that are part of the
1130 * specified thread group.
1131 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001134 ObjectId thread_group_id = ReadObjectId(&buf);
1135 VLOG(jdwp) << StringPrintf(" Req for threads in thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001136
Elliott Hughescaf76542012-06-28 16:08:22 -07001137 std::vector<ObjectId> thread_ids;
1138 Dbg::GetThreads(thread_group_id, thread_ids);
1139 expandBufAdd4BE(pReply, thread_ids.size());
1140 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1141 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001142 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001143
Elliott Hughescaf76542012-06-28 16:08:22 -07001144 std::vector<ObjectId> child_thread_groups_ids;
1145 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1146 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1147 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1148 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001149 }
1150
1151 return ERR_NONE;
1152}
1153
1154/*
1155 * Return the #of components in the array.
1156 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001157static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001159 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001160 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001161
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001162 int length;
1163 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1164 if (status != ERR_NONE) {
1165 return status;
1166 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001167 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001168
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001169 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001170
1171 return ERR_NONE;
1172}
1173
1174/*
1175 * Return the values from an array.
1176 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001179 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001180 uint32_t firstIndex = Read4BE(&buf);
1181 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001182 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001183
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001184 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001185}
1186
1187/*
1188 * Set values in an array.
1189 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001191 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001192 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001193 uint32_t firstIndex = Read4BE(&buf);
1194 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001195
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId,
1197 firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001198
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001199 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001200}
1201
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001202static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001204 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001205 // TODO: we should only return classes which have the given class loader as a defining or
1206 // initiating loader. The former would be easy; the latter is hard, because we don't have
1207 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001208 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001209}
1210
1211/*
1212 * Set an event trigger.
1213 *
1214 * Reply with a requestID.
1215 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001216static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001218 const uint8_t* origBuf = buf;
1219
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001220 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001221 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001222 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001223
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001224 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001225 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001226 << " mods=" << modifierCount << ")";
1227
1228 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1229
1230 JdwpEvent* pEvent = EventAlloc(modifierCount);
1231 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001232 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001233 pEvent->modCount = modifierCount;
1234
1235 /*
1236 * Read modifiers. Ordering may be significant (see explanation of Count
1237 * mods in JDWP doc).
1238 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001239 for (uint32_t i = 0; i < modifierCount; ++i) {
1240 JdwpEventMod& mod = pEvent->mods[i];
1241 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1242 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001243 case MK_COUNT: /* report once, when "--count" reaches 0 */
1244 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001245 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001246 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001247 if (count == 0) {
1248 return ERR_INVALID_COUNT;
1249 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001250 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001251 }
1252 break;
1253 case MK_CONDITIONAL: /* conditional on expression) */
1254 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001255 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001256 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001257 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001258 }
1259 break;
1260 case MK_THREAD_ONLY: /* only report events in specified thread */
1261 {
Elliott Hughes74847412012-06-20 18:10:21 -07001262 ObjectId thread_id = ReadObjectId(&buf);
1263 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1264 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001265 }
1266 break;
1267 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1268 {
Elliott Hughes74847412012-06-20 18:10:21 -07001269 RefTypeId class_id = ReadRefTypeId(&buf);
1270 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1271 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001272 }
1273 break;
1274 case MK_CLASS_MATCH: /* restrict events to matching classes */
1275 {
Elliott Hughes86964332012-02-15 19:37:42 -08001276 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001277 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001278 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001279 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001280 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001281 }
1282 break;
1283 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1284 {
Elliott Hughes86964332012-02-15 19:37:42 -08001285 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001286 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001287 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001288 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001289 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001290 }
1291 break;
1292 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1293 {
1294 JdwpLocation loc;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001295 JdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001296 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001297 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001298 }
1299 break;
1300 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1301 {
1302 RefTypeId exceptionOrNull; /* null == all exceptions */
1303 uint8_t caught, uncaught;
1304
1305 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001306 caught = Read1(&buf);
1307 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001308 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001309 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001310
Elliott Hughes972a47b2012-02-21 18:16:06 -08001311 mod.exceptionOnly.refTypeId = exceptionOrNull;
1312 mod.exceptionOnly.caught = caught;
1313 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001314 }
1315 break;
1316 case MK_FIELD_ONLY: /* for field access/mod events */
1317 {
1318 RefTypeId declaring = ReadRefTypeId(&buf);
1319 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001320 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001321 mod.fieldOnly.refTypeId = declaring;
1322 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001323 }
1324 break;
1325 case MK_STEP: /* for use with EK_SINGLE_STEP */
1326 {
Elliott Hughes74847412012-06-20 18:10:21 -07001327 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001328 uint32_t size, depth;
1329
Elliott Hughes74847412012-06-20 18:10:21 -07001330 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001331 size = Read4BE(&buf);
1332 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001333 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001334 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1335
Elliott Hughes74847412012-06-20 18:10:21 -07001336 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001337 mod.step.size = size;
1338 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001339 }
1340 break;
1341 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1342 {
1343 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001344 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001345 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001346 }
1347 break;
1348 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001349 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001350 break;
1351 }
1352 }
1353
1354 /*
1355 * Make sure we consumed all data. It is possible that the remote side
1356 * has sent us bad stuff, but for now we blame ourselves.
1357 */
1358 if (buf != origBuf + dataLen) {
1359 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1360 }
1361
1362 /*
1363 * We reply with an integer "requestID".
1364 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001365 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001366 expandBufAdd4BE(pReply, requestId);
1367
1368 pEvent->requestId = requestId;
1369
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001370 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001371
1372 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001373 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001374 if (err != ERR_NONE) {
1375 /* registration failed, probably because event is bogus */
1376 EventFree(pEvent);
1377 LOG(WARNING) << "WARNING: event request rejected";
1378 }
1379 return err;
1380}
1381
1382/*
1383 * Clear an event. Failure to find an event with a matching ID is a no-op
1384 * and does not return an error.
1385 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001386static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001387 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001388 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001389 eventKind = Read1(&buf);
1390 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001391
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001392 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001393
Elliott Hughes761928d2011-11-16 18:33:03 -08001394 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001395
1396 return ERR_NONE;
1397}
1398
1399/*
1400 * Return the values of arguments and local variables.
1401 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001402static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001403 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001404 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001405 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001406 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001407
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001408 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 -07001409
1410 expandBufAdd4BE(pReply, slots); /* "int values" */
1411 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001412 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001413 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001414
Elliott Hughes2435a572012-02-17 16:07:41 -08001415 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001416
Elliott Hughesdbb40792011-11-18 17:05:22 -08001417 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001418 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001419 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001420 }
1421
1422 return ERR_NONE;
1423}
1424
1425/*
1426 * Set the values of arguments and local variables.
1427 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001428static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001429 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001430 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001431 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001432 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001433
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001434 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 -07001435
1436 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001437 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001438 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001439 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001440 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001441
Elliott Hughes2435a572012-02-17 16:07:41 -08001442 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001443 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001444 }
1445
1446 return ERR_NONE;
1447}
1448
1449/*
1450 * Returns the value of "this" for the specified frame.
1451 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001452static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001453 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001454 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001455 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001456
Elliott Hughes546b9862012-06-20 16:06:13 -07001457 ObjectId id;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001458 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &id);
1459 if (rc != ERR_NONE) {
1460 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001461 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001462
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001463 uint8_t tag;
1464 rc = Dbg::GetObjectTag(id, tag);
1465 if (rc != ERR_NONE) {
1466 return rc;
1467 }
1468
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001469 VLOG(jdwp) << StringPrintf(" Req for 'this' in thread_id=%#llx frame=%lld --> %#llx '%c'",
1470 thread_id, frame_id, id, static_cast<char>(tag));
Elliott Hughes546b9862012-06-20 16:06:13 -07001471 expandBufAdd1(pReply, tag);
1472 expandBufAddObjectId(pReply, id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001473
1474 return ERR_NONE;
1475}
1476
1477/*
1478 * Return the reference type reflected by this class object.
1479 *
1480 * This appears to be required because ReferenceTypeId values are NEVER
1481 * reused, whereas ClassIds can be recycled like any other object. (Either
1482 * that, or I have no idea what this is for.)
1483 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001484static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001486 RefTypeId classObjectId = ReadRefTypeId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001487 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId,
1488 Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001489 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001490}
1491
1492/*
1493 * Handle a DDM packet with a single chunk in it.
1494 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001495static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001496 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001497 uint8_t* replyBuf = NULL;
1498 int replyLen = -1;
1499
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001500 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001501
Elliott Hughesa21039c2012-06-21 12:09:25 -07001502 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001503
1504 /*
1505 * If they want to send something back, we copy it into the buffer.
1506 * A no-copy approach would be nicer.
1507 *
1508 * TODO: consider altering the JDWP stuff to hold the packet header
1509 * in a separate buffer. That would allow us to writev() DDM traffic
1510 * instead of copying it into the expanding buffer. The reduction in
1511 * heap requirements is probably more valuable than the efficiency.
1512 */
1513 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1514 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1515 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1516 free(replyBuf);
1517 }
1518 return ERR_NONE;
1519}
1520
1521/*
1522 * Handler map decl.
1523 */
1524typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1525
1526struct JdwpHandlerMap {
1527 uint8_t cmdSet;
1528 uint8_t cmd;
1529 JdwpRequestHandler func;
1530 const char* descr;
1531};
1532
1533/*
1534 * Map commands to functions.
1535 *
1536 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1537 * and 128-256 are vendor-defined.
1538 */
1539static const JdwpHandlerMap gHandlerMap[] = {
1540 /* VirtualMachine command set (1) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001541 { 1, 1, VM_Version, "VirtualMachine.Version" },
1542 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1543 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1544 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1545 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1546 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1547 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1548 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1549 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1550 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1551 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1552 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1553 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1554 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001555 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1556 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001557 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001558 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1559 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001560 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001561 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001562
1563 /* ReferenceType command set (2) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001564 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1565 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1566 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1567 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1568 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1569 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1570 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001571 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001572 { 2, 9, RT_Status, "ReferenceType.Status" },
1573 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1574 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
1575 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1576 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1577 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1578 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001579 { 2, 16, NULL, "ReferenceType.Instances" },
1580 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1581 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001582
1583 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001584 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1585 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1586 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1587 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001588
1589 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001590 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001591
1592 /* InterfaceType command set (5) */
1593
1594 /* Method command set (6) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001595 { 6, 1, M_LineTable, "Method.LineTable" },
1596 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001597 { 6, 3, NULL, "Method.Bytecodes" },
1598 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001599 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001600
1601 /* Field command set (8) */
1602
1603 /* ObjectReference command set (9) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001604 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1605 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1606 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001607 { 9, 4, NULL, "ObjectReference.UNUSED" },
1608 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001609 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
1610 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
1611 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1612 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001613 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001614
1615 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001616 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001617
1618 /* ThreadReference command set (11) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001619 { 11, 1, TR_Name, "ThreadReference.Name" },
1620 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1621 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1622 { 11, 4, TR_Status, "ThreadReference.Status" },
1623 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1624 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1625 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001626 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001627 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001628 { 11, 10, NULL, "ThreadReference.Stop" },
Elliott Hughes74847412012-06-20 18:10:21 -07001629 { 11, 11, NULL, "ThreadReference.Interrupt" },
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001630 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001631 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1632 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001633
1634 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001635 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1636 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1637 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001638
1639 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001640 { 13, 1, AR_Length, "ArrayReference.Length" },
1641 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1642 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001643
1644 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001645 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001646
1647 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001648 { 15, 1, ER_Set, "EventRequest.Set" },
1649 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001650 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001651
1652 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001653 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1654 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1655 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001656 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001657
1658 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001659 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001660
1661 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001662 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001663
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001664 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001665};
1666
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001667static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001668 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001669 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1670 return gHandlerMap[i].descr;
1671 }
1672 }
1673 return "?UNKNOWN?";
1674}
1675
1676static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1677 std::string result;
1678 result += "REQ: ";
1679 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1680 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1681 return result;
1682}
1683
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001684/*
1685 * Process a request from the debugger.
1686 *
1687 * On entry, the JDWP thread is in VMWAIT.
1688 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001689void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001690 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001691
1692 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1693 /*
1694 * Activity from a debugger, not merely ddms. Mark us as having an
1695 * active debugger session, and zero out the last-activity timestamp
1696 * so waitForDebugger() doesn't return if we stall for a bit here.
1697 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001698 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001699 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001700 }
1701
1702 /*
1703 * If a debugger event has fired in another thread, wait until the
1704 * initiating thread has suspended itself before processing messages
1705 * from the debugger. Otherwise we (the JDWP thread) could be told to
1706 * resume the thread before it has suspended.
1707 *
1708 * We call with an argument of zero to wait for the current event
1709 * thread to finish, and then clear the block. Depending on the thread
1710 * suspend policy, this may allow events in other threads to fire,
1711 * but those events have no bearing on what the debugger has sent us
1712 * in the current request.
1713 *
1714 * Note that we MUST clear the event token before waking the event
1715 * thread up, or risk waiting for the thread to suspend after we've
1716 * told it to resume.
1717 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001718 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001719
1720 /*
1721 * Tell the VM that we're running and shouldn't be interrupted by GC.
1722 * Do this after anything that can stall indefinitely.
1723 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 Thread* self = Thread::Current();
1725 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001726
1727 expandBufAddSpace(pReply, kJDWPHeaderLen);
1728
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001729 size_t i;
1730 for (i = 0; i < arraysize(gHandlerMap); i++) {
1731 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001732 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001733 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001734 break;
1735 }
1736 }
1737 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001738 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1739 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001740 result = ERR_NOT_IMPLEMENTED;
1741 }
1742
1743 /*
1744 * Set up the reply header.
1745 *
1746 * If we encountered an error, only send the header back.
1747 */
1748 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001749 Set4BE(replyBuf + 4, pHeader->id);
1750 Set1(replyBuf + 8, kJDWPFlagReply);
1751 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001752 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001753 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001754 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001755 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001756 }
1757
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001758 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001759 if (false) {
1760 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001761 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001762 }
1763
1764 /*
1765 * Update last-activity timestamp. We really only need this during
1766 * the initial setup. Only update if this is a non-DDMS packet.
1767 */
1768 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001769 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001770 }
1771
1772 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001773 self->TransitionFromRunnableToSuspended(old_state);
1774
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001775}
1776
1777} // namespace JDWP
1778
1779} // namespace art