blob: 97e6304a52cb2ca02f5c0e697770f466fedb23f5 [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 Hughesf327e072013-01-09 16:01:26 -0800333 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, true); // canGetMonitorInfo
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700340 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 Hughesf327e072013-01-09 16:01:26 -0800379 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, true); // 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
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700400
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
Elliott Hughesf327e072013-01-09 16:01:26 -0800840static JdwpError OR_MonitorInfo(JdwpState*, const uint8_t* buf, int, ExpandBuf* reply)
841 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
842 ObjectId object_id = ReadObjectId(&buf);
843 return Dbg::GetMonitorInfo(object_id, reply);
844}
845
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700846/*
847 * Invoke an instance method. The invocation must occur in the specified
848 * thread, which must have been suspended by an event.
849 *
850 * The call is synchronous. All threads in the VM are resumed, unless the
851 * SINGLE_THREADED flag is set.
852 *
853 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
854 * object), it will try to invoke the object's toString() function. This
855 * feature becomes crucial when examining ArrayLists with Eclipse.
856 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700857static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
858 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700859 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700860 ObjectId object_id = ReadObjectId(&buf);
861 ObjectId thread_id = ReadObjectId(&buf);
862 RefTypeId class_id = ReadRefTypeId(&buf);
863 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700864
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700865 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700866}
867
868/*
869 * Disable garbage collection of the specified object.
870 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700871static JdwpError OR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700872 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700873 // this is currently a no-op
874 return ERR_NONE;
875}
876
877/*
878 * Enable garbage collection of the specified object.
879 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700880static JdwpError OR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700881 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700882 // this is currently a no-op
883 return ERR_NONE;
884}
885
886/*
887 * Determine whether an object has been garbage collected.
888 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700889static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700890 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700891 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700892
Elliott Hughes74847412012-06-20 18:10:21 -0700893 object_id = ReadObjectId(&buf);
894 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700895
896 // TODO: currently returning false; must integrate with GC
897 expandBufAdd1(pReply, 0);
898
899 return ERR_NONE;
900}
901
902/*
903 * Return the string value in a string object.
904 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700905static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700906 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700907 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800908 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700909
Elliott Hughes82914b62012-04-09 15:56:29 -0700910 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700911
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800912 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700913
914 return ERR_NONE;
915}
916
917/*
918 * Return a thread's name.
919 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700920static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700921 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700922 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700923
Elliott Hughes74847412012-06-20 18:10:21 -0700924 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800925 std::string name;
Elliott Hughes221229c2013-01-08 18:17:50 -0800926 JdwpError error = Dbg::GetThreadName(thread_id, name);
927 if (error != ERR_NONE) {
928 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700929 }
Elliott Hughes74847412012-06-20 18:10:21 -0700930 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800931 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700932
933 return ERR_NONE;
934}
935
936/*
937 * Suspend the specified thread.
938 *
939 * It's supposed to remain suspended even if interpreted code wants to
940 * resume it; only the JDI is allowed to resume it.
941 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700942static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700943 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700944 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700945
Elliott Hughes74847412012-06-20 18:10:21 -0700946 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700947 LOG(INFO) << " Warning: ignoring request to suspend self";
948 return ERR_THREAD_NOT_SUSPENDED;
949 }
Elliott Hughes74847412012-06-20 18:10:21 -0700950 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700951 Thread* self = Thread::Current();
952 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
953 JdwpError result = Dbg::SuspendThread(thread_id);
954 self->TransitionFromSuspendedToRunnable();
955 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700956}
957
958/*
959 * Resume the specified thread.
960 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700962 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700963 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700964
Elliott Hughes74847412012-06-20 18:10:21 -0700965 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700966 LOG(INFO) << " Warning: ignoring request to resume self";
967 return ERR_NONE;
968 }
Elliott Hughes74847412012-06-20 18:10:21 -0700969 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
970 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700971 return ERR_NONE;
972}
973
974/*
975 * Return status of specified thread.
976 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700978 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700979 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700980
Elliott Hughes74847412012-06-20 18:10:21 -0700981 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700982
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800983 JDWP::JdwpThreadStatus threadStatus;
984 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes221229c2013-01-08 18:17:50 -0800985 JdwpError error = Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus);
986 if (error != ERR_NONE) {
987 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700988 }
989
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800990 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700991
992 expandBufAdd4BE(pReply, threadStatus);
993 expandBufAdd4BE(pReply, suspendStatus);
994
995 return ERR_NONE;
996}
997
998/*
999 * Return the thread group that the specified thread is a member of.
1000 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001001static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001002 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001003 ObjectId thread_id = ReadObjectId(&buf);
1004 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001005}
1006
1007/*
1008 * Return the current call stack of a suspended thread.
1009 *
1010 * If the thread isn't suspended, the error code isn't defined, but should
1011 * be THREAD_NOT_SUSPENDED.
1012 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001014 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001015 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001016 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001017 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001018
Elliott Hughes221229c2013-01-08 18:17:50 -08001019 size_t actual_frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001020 JdwpError error = Dbg::GetThreadFrameCount(thread_id, actual_frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001021 if (error != ERR_NONE) {
1022 return error;
1023 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001024
Elliott Hughes74847412012-06-20 18:10:21 -07001025 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 -08001026 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001027 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1028 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001029
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001030 if (start_frame > actual_frame_count) {
1031 return ERR_INVALID_INDEX;
1032 }
1033 if (length == static_cast<uint32_t>(-1)) {
1034 length = actual_frame_count - start_frame;
1035 }
1036 if (start_frame + length > actual_frame_count) {
1037 return ERR_INVALID_LENGTH;
1038 }
1039
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001040 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001041}
1042
1043/*
1044 * Returns the #of frames on the specified thread, which must be suspended.
1045 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001047 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001048 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001049
Elliott Hughes221229c2013-01-08 18:17:50 -08001050 size_t frame_count;
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001051 JdwpError error = Dbg::GetThreadFrameCount(thread_id, frame_count);
Elliott Hughes221229c2013-01-08 18:17:50 -08001052 if (error != ERR_NONE) {
1053 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001054 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001055 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001056
1057 return ERR_NONE;
1058}
1059
1060/*
1061 * Get the monitor that the thread is waiting on.
1062 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001064 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001065 ReadObjectId(&buf); // thread_id
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001066
1067 // TODO: create an Object to represent the monitor (we're currently
1068 // just using a raw Monitor struct in the VM)
1069
1070 return ERR_NOT_IMPLEMENTED;
1071}
1072
1073/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001074 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001075 *
1076 * (The thread *might* still be running -- it might not have examined
1077 * its suspend count recently.)
1078 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079static JdwpError TR_DebugSuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001080 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001081 ObjectId thread_id = ReadObjectId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001083}
1084
1085/*
1086 * Return the name of a thread group.
1087 *
1088 * The Eclipse debugger recognizes "main" and "system" as special.
1089 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001090static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001091 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001092 ObjectId thread_group_id = ReadObjectId(&buf);
1093 VLOG(jdwp) << StringPrintf(" Req for name of thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001094
Elliott Hughescaf76542012-06-28 16:08:22 -07001095 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001096
1097 return ERR_NONE;
1098}
1099
1100/*
1101 * Returns the thread group -- if any -- that contains the specified
1102 * thread group.
1103 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001104static JdwpError TGR_Parent(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);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001107
Elliott Hughescaf76542012-06-28 16:08:22 -07001108 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001109 expandBufAddObjectId(pReply, parentGroup);
1110
1111 return ERR_NONE;
1112}
1113
1114/*
1115 * Return the active threads and thread groups that are part of the
1116 * specified thread group.
1117 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118static JdwpError TGR_Children(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);
1121 VLOG(jdwp) << StringPrintf(" Req for threads in thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001122
Elliott Hughescaf76542012-06-28 16:08:22 -07001123 std::vector<ObjectId> thread_ids;
1124 Dbg::GetThreads(thread_group_id, thread_ids);
1125 expandBufAdd4BE(pReply, thread_ids.size());
1126 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1127 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001128 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001129
Elliott Hughescaf76542012-06-28 16:08:22 -07001130 std::vector<ObjectId> child_thread_groups_ids;
1131 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1132 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1133 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1134 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001135 }
1136
1137 return ERR_NONE;
1138}
1139
1140/*
1141 * Return the #of components in the array.
1142 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001143static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001144 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001145 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001146 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001147
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001148 int length;
1149 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1150 if (status != ERR_NONE) {
1151 return status;
1152 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001153 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001154
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001155 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001156
1157 return ERR_NONE;
1158}
1159
1160/*
1161 * Return the values from an array.
1162 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001165 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001166 uint32_t firstIndex = Read4BE(&buf);
1167 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001168 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001169
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001170 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001171}
1172
1173/*
1174 * Set values in an array.
1175 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001176static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001178 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001179 uint32_t firstIndex = Read4BE(&buf);
1180 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001181
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001182 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId,
1183 firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001184
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001185 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001186}
1187
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001190 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001191 // TODO: we should only return classes which have the given class loader as a defining or
1192 // initiating loader. The former would be easy; the latter is hard, because we don't have
1193 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001194 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001195}
1196
1197/*
1198 * Set an event trigger.
1199 *
1200 * Reply with a requestID.
1201 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001202static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001204 const uint8_t* origBuf = buf;
1205
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001206 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001207 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001208 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001209
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001210 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001211 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001212 << " mods=" << modifierCount << ")";
1213
1214 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1215
1216 JdwpEvent* pEvent = EventAlloc(modifierCount);
1217 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001218 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001219 pEvent->modCount = modifierCount;
1220
1221 /*
1222 * Read modifiers. Ordering may be significant (see explanation of Count
1223 * mods in JDWP doc).
1224 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001225 for (uint32_t i = 0; i < modifierCount; ++i) {
1226 JdwpEventMod& mod = pEvent->mods[i];
1227 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1228 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001229 case MK_COUNT: /* report once, when "--count" reaches 0 */
1230 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001231 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001232 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001233 if (count == 0) {
1234 return ERR_INVALID_COUNT;
1235 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001236 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001237 }
1238 break;
1239 case MK_CONDITIONAL: /* conditional on expression) */
1240 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001241 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001242 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001243 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001244 }
1245 break;
1246 case MK_THREAD_ONLY: /* only report events in specified thread */
1247 {
Elliott Hughes74847412012-06-20 18:10:21 -07001248 ObjectId thread_id = ReadObjectId(&buf);
1249 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1250 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001251 }
1252 break;
1253 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1254 {
Elliott Hughes74847412012-06-20 18:10:21 -07001255 RefTypeId class_id = ReadRefTypeId(&buf);
1256 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1257 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001258 }
1259 break;
1260 case MK_CLASS_MATCH: /* restrict events to matching classes */
1261 {
Elliott Hughes86964332012-02-15 19:37:42 -08001262 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001263 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001264 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001265 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001266 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001267 }
1268 break;
1269 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1270 {
Elliott Hughes86964332012-02-15 19:37:42 -08001271 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001272 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001273 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001274 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001275 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001276 }
1277 break;
1278 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1279 {
1280 JdwpLocation loc;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001281 JdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001282 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001283 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001284 }
1285 break;
1286 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1287 {
1288 RefTypeId exceptionOrNull; /* null == all exceptions */
1289 uint8_t caught, uncaught;
1290
1291 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001292 caught = Read1(&buf);
1293 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001294 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001295 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001296
Elliott Hughes972a47b2012-02-21 18:16:06 -08001297 mod.exceptionOnly.refTypeId = exceptionOrNull;
1298 mod.exceptionOnly.caught = caught;
1299 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001300 }
1301 break;
1302 case MK_FIELD_ONLY: /* for field access/mod events */
1303 {
1304 RefTypeId declaring = ReadRefTypeId(&buf);
1305 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001306 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001307 mod.fieldOnly.refTypeId = declaring;
1308 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001309 }
1310 break;
1311 case MK_STEP: /* for use with EK_SINGLE_STEP */
1312 {
Elliott Hughes74847412012-06-20 18:10:21 -07001313 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001314 uint32_t size, depth;
1315
Elliott Hughes74847412012-06-20 18:10:21 -07001316 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001317 size = Read4BE(&buf);
1318 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001319 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001320 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1321
Elliott Hughes74847412012-06-20 18:10:21 -07001322 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001323 mod.step.size = size;
1324 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001325 }
1326 break;
1327 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1328 {
1329 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001330 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001331 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001332 }
1333 break;
1334 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001335 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001336 break;
1337 }
1338 }
1339
1340 /*
1341 * Make sure we consumed all data. It is possible that the remote side
1342 * has sent us bad stuff, but for now we blame ourselves.
1343 */
1344 if (buf != origBuf + dataLen) {
1345 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1346 }
1347
1348 /*
1349 * We reply with an integer "requestID".
1350 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001351 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001352 expandBufAdd4BE(pReply, requestId);
1353
1354 pEvent->requestId = requestId;
1355
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001356 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001357
1358 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001359 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001360 if (err != ERR_NONE) {
1361 /* registration failed, probably because event is bogus */
1362 EventFree(pEvent);
1363 LOG(WARNING) << "WARNING: event request rejected";
1364 }
1365 return err;
1366}
1367
1368/*
1369 * Clear an event. Failure to find an event with a matching ID is a no-op
1370 * and does not return an error.
1371 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001372static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001373 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001374 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001375 eventKind = Read1(&buf);
1376 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001377
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001378 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001379
Elliott Hughes761928d2011-11-16 18:33:03 -08001380 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001381
1382 return ERR_NONE;
1383}
1384
1385/*
1386 * Return the values of arguments and local variables.
1387 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001388static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001390 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001391 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001392 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001393
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001394 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 -07001395
1396 expandBufAdd4BE(pReply, slots); /* "int values" */
1397 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001398 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001399 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001400
Elliott Hughes2435a572012-02-17 16:07:41 -08001401 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001402
Elliott Hughesdbb40792011-11-18 17:05:22 -08001403 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001404 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001405 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001406 }
1407
1408 return ERR_NONE;
1409}
1410
1411/*
1412 * Set the values of arguments and local variables.
1413 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001414static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001415 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001416 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001417 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001418 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001419
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001420 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 -07001421
1422 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001423 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001424 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001425 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001426 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001427
Elliott Hughes2435a572012-02-17 16:07:41 -08001428 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001429 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001430 }
1431
1432 return ERR_NONE;
1433}
1434
1435/*
1436 * Returns the value of "this" for the specified frame.
1437 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001438static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001439 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001440 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001441 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001442
Elliott Hughes546b9862012-06-20 16:06:13 -07001443 ObjectId id;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001444 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &id);
1445 if (rc != ERR_NONE) {
1446 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001447 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001448
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001449 uint8_t tag;
1450 rc = Dbg::GetObjectTag(id, tag);
1451 if (rc != ERR_NONE) {
1452 return rc;
1453 }
1454
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001455 VLOG(jdwp) << StringPrintf(" Req for 'this' in thread_id=%#llx frame=%lld --> %#llx '%c'",
1456 thread_id, frame_id, id, static_cast<char>(tag));
Elliott Hughes546b9862012-06-20 16:06:13 -07001457 expandBufAdd1(pReply, tag);
1458 expandBufAddObjectId(pReply, id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001459
1460 return ERR_NONE;
1461}
1462
1463/*
1464 * Return the reference type reflected by this class object.
1465 *
1466 * This appears to be required because ReferenceTypeId values are NEVER
1467 * reused, whereas ClassIds can be recycled like any other object. (Either
1468 * that, or I have no idea what this is for.)
1469 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001470static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001471 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001472 RefTypeId classObjectId = ReadRefTypeId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001473 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId,
1474 Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001475 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001476}
1477
1478/*
1479 * Handle a DDM packet with a single chunk in it.
1480 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001481static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001482 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001483 uint8_t* replyBuf = NULL;
1484 int replyLen = -1;
1485
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001486 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001487
Elliott Hughesa21039c2012-06-21 12:09:25 -07001488 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001489
1490 /*
1491 * If they want to send something back, we copy it into the buffer.
1492 * A no-copy approach would be nicer.
1493 *
1494 * TODO: consider altering the JDWP stuff to hold the packet header
1495 * in a separate buffer. That would allow us to writev() DDM traffic
1496 * instead of copying it into the expanding buffer. The reduction in
1497 * heap requirements is probably more valuable than the efficiency.
1498 */
1499 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1500 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1501 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1502 free(replyBuf);
1503 }
1504 return ERR_NONE;
1505}
1506
1507/*
1508 * Handler map decl.
1509 */
1510typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1511
1512struct JdwpHandlerMap {
1513 uint8_t cmdSet;
1514 uint8_t cmd;
1515 JdwpRequestHandler func;
1516 const char* descr;
1517};
1518
1519/*
1520 * Map commands to functions.
1521 *
1522 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1523 * and 128-256 are vendor-defined.
1524 */
1525static const JdwpHandlerMap gHandlerMap[] = {
1526 /* VirtualMachine command set (1) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001527 { 1, 1, VM_Version, "VirtualMachine.Version" },
1528 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1529 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1530 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1531 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1532 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1533 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1534 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1535 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1536 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1537 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1538 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1539 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1540 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
1541 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1542 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
1543 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
1544 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1545 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001546 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001547 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001548
1549 /* ReferenceType command set (2) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001550 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1551 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1552 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1553 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1554 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1555 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1556 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
1557 { 2, 8, NULL, "ReferenceType.NestedTypes" },
1558 { 2, 9, RT_Status, "ReferenceType.Status" },
1559 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1560 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001561 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1562 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001563 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1564 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
1565 { 2, 16, NULL, "ReferenceType.Instances" },
1566 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1567 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001568
1569 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001570 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1571 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1572 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1573 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001574
1575 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001576 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001577
1578 /* InterfaceType command set (5) */
1579
1580 /* Method command set (6) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001581 { 6, 1, M_LineTable, "Method.LineTable" },
1582 { 6, 2, M_VariableTable, "Method.VariableTable" },
1583 { 6, 3, NULL, "Method.Bytecodes" },
1584 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001585 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001586
1587 /* Field command set (8) */
1588
1589 /* ObjectReference command set (9) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001590 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1591 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1592 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
1593 { 9, 4, NULL, "ObjectReference.UNUSED" },
1594 { 9, 5, OR_MonitorInfo, "ObjectReference.MonitorInfo" },
1595 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001596 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001597 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1598 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
1599 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001600
1601 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001602 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001603
1604 /* ThreadReference command set (11) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001605 { 11, 1, TR_Name, "ThreadReference.Name" },
1606 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1607 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1608 { 11, 4, TR_Status, "ThreadReference.Status" },
1609 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1610 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1611 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
1612 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001613 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001614 { 11, 10, NULL, "ThreadReference.Stop" },
1615 { 11, 11, NULL, "ThreadReference.Interrupt" },
1616 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
1617 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1618 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001619
1620 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001621 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1622 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1623 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001624
1625 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001626 { 13, 1, AR_Length, "ArrayReference.Length" },
1627 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1628 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001629
1630 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001631 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001632
1633 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001634 { 15, 1, ER_Set, "EventRequest.Set" },
1635 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001636 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001637
1638 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001639 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1640 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1641 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesf327e072013-01-09 16:01:26 -08001642 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001643
1644 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001645 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001646
1647 /* Event command set (64) */
Elliott Hughesf327e072013-01-09 16:01:26 -08001648 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001649
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001650 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001651};
1652
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001653static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001654 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001655 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1656 return gHandlerMap[i].descr;
1657 }
1658 }
1659 return "?UNKNOWN?";
1660}
1661
1662static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1663 std::string result;
1664 result += "REQ: ";
1665 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1666 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1667 return result;
1668}
1669
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001670/*
1671 * Process a request from the debugger.
1672 *
1673 * On entry, the JDWP thread is in VMWAIT.
1674 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001675void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001676 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001677
1678 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1679 /*
1680 * Activity from a debugger, not merely ddms. Mark us as having an
1681 * active debugger session, and zero out the last-activity timestamp
1682 * so waitForDebugger() doesn't return if we stall for a bit here.
1683 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001684 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001685 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001686 }
1687
1688 /*
1689 * If a debugger event has fired in another thread, wait until the
1690 * initiating thread has suspended itself before processing messages
1691 * from the debugger. Otherwise we (the JDWP thread) could be told to
1692 * resume the thread before it has suspended.
1693 *
1694 * We call with an argument of zero to wait for the current event
1695 * thread to finish, and then clear the block. Depending on the thread
1696 * suspend policy, this may allow events in other threads to fire,
1697 * but those events have no bearing on what the debugger has sent us
1698 * in the current request.
1699 *
1700 * Note that we MUST clear the event token before waking the event
1701 * thread up, or risk waiting for the thread to suspend after we've
1702 * told it to resume.
1703 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001704 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001705
1706 /*
1707 * Tell the VM that we're running and shouldn't be interrupted by GC.
1708 * Do this after anything that can stall indefinitely.
1709 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001710 Thread* self = Thread::Current();
1711 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001712
1713 expandBufAddSpace(pReply, kJDWPHeaderLen);
1714
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001715 size_t i;
1716 for (i = 0; i < arraysize(gHandlerMap); i++) {
1717 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001718 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001719 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001720 break;
1721 }
1722 }
1723 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001724 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1725 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001726 result = ERR_NOT_IMPLEMENTED;
1727 }
1728
1729 /*
1730 * Set up the reply header.
1731 *
1732 * If we encountered an error, only send the header back.
1733 */
1734 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001735 Set4BE(replyBuf + 4, pHeader->id);
1736 Set1(replyBuf + 8, kJDWPFlagReply);
1737 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001738 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001739 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001740 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001741 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001742 }
1743
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001744 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001745 if (false) {
1746 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001747 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001748 }
1749
1750 /*
1751 * Update last-activity timestamp. We really only need this during
1752 * the initial setup. Only update if this is a non-DDMS packet.
1753 */
1754 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001755 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001756 }
1757
1758 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001759 self->TransitionFromRunnableToSuspended(old_state);
1760
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001761}
1762
1763} // namespace JDWP
1764
1765} // namespace art