blob: 88677d5750a69390dd7cdbe98dedb1e1f83cda3e [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
28#include "atomic.h"
29#include "debugger.h"
30#include "jdwp/jdwp_priv.h"
31#include "jdwp/jdwp_handler.h"
32#include "jdwp/jdwp_event.h"
33#include "jdwp/jdwp_constants.h"
34#include "jdwp/jdwp_expand_buf.h"
35#include "logging.h"
36#include "macros.h"
37#include "stringprintf.h"
38
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43namespace art {
44
45namespace JDWP {
46
47/*
48 * Helper function: read a "location" from an input buffer.
49 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070050static void JdwpReadLocation(const uint8_t** pBuf, JdwpLocation* pLoc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070051 memset(pLoc, 0, sizeof(*pLoc)); /* allows memcmp() later */
Elliott Hughes74847412012-06-20 18:10:21 -070052 pLoc->type_tag = ReadTypeTag(pBuf);
53 pLoc->class_id = ReadObjectId(pBuf);
54 pLoc->method_id = ReadMethodId(pBuf);
Elliott Hughes972a47b2012-02-21 18:16:06 -080055 pLoc->dex_pc = Read8BE(pBuf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070056}
57
58/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -070059 * Helper function: read a variable-width value from the input buffer.
60 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070061static uint64_t JdwpReadValue(const uint8_t** pBuf, size_t width) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070062 uint64_t value = -1;
63 switch (width) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070064 case 1: value = Read1(pBuf); break;
65 case 2: value = Read2BE(pBuf); break;
66 case 4: value = Read4BE(pBuf); break;
67 case 8: value = Read8BE(pBuf); break;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070068 default: LOG(FATAL) << width; break;
69 }
70 return value;
71}
72
73/*
74 * Helper function: write a variable-width value into the output input buffer.
75 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070076static void JdwpWriteValue(ExpandBuf* pReply, int width, uint64_t value) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070077 switch (width) {
78 case 1: expandBufAdd1(pReply, value); break;
79 case 2: expandBufAdd2BE(pReply, value); break;
80 case 4: expandBufAdd4BE(pReply, value); break;
81 case 8: expandBufAdd8BE(pReply, value); break;
82 default: LOG(FATAL) << width; break;
83 }
84}
85
86/*
87 * Common code for *_InvokeMethod requests.
88 *
Elliott Hughes74847412012-06-20 18:10:21 -070089 * If "is_constructor" is set, this returns "object_id" rather than the
Elliott Hughes872d4ec2011-10-21 17:07:15 -070090 * expected-to-be-void return value of the called function.
91 */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -070092static JdwpError FinishInvoke(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
Elliott Hughes74847412012-06-20 18:10:21 -070093 ObjectId thread_id, ObjectId object_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 RefTypeId class_id, MethodId method_id, bool is_constructor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -070096 CHECK(!is_constructor || object_id != 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070097
Elliott Hughes45651fd2012-02-21 15:48:20 -080098 uint32_t arg_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070099
Elliott Hughes74847412012-06-20 18:10:21 -0700100 VLOG(jdwp) << StringPrintf(" --> thread_id=%#llx object_id=%#llx", thread_id, object_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 VLOG(jdwp) << StringPrintf(" class_id=%#llx method_id=%x %s.%s", class_id,
102 method_id, Dbg::GetClassName(class_id).c_str(),
103 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes45651fd2012-02-21 15:48:20 -0800104 VLOG(jdwp) << StringPrintf(" %d args:", arg_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700105
Elliott Hughes45651fd2012-02-21 15:48:20 -0800106 UniquePtr<JdwpTag[]> argTypes(arg_count > 0 ? new JdwpTag[arg_count] : NULL);
107 UniquePtr<uint64_t[]> argValues(arg_count > 0 ? new uint64_t[arg_count] : NULL);
108 for (uint32_t i = 0; i < arg_count; ++i) {
109 argTypes[i] = ReadTag(&buf);
110 size_t width = Dbg::GetTagWidth(argTypes[i]);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700111 argValues[i] = JdwpReadValue(&buf, width);
Elliott Hughes229feb72012-02-23 13:33:29 -0800112 VLOG(jdwp) << " " << argTypes[i] << StringPrintf("(%zd): %#llx", width, argValues[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700113 }
114
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700115 uint32_t options = Read4BE(&buf); /* enum InvokeOptions bit flags */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 VLOG(jdwp) << StringPrintf(" options=0x%04x%s%s", options,
117 (options & INVOKE_SINGLE_THREADED) ? " (SINGLE_THREADED)" : "",
118 (options & INVOKE_NONVIRTUAL) ? " (NONVIRTUAL)" : "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700119
Elliott Hughes45651fd2012-02-21 15:48:20 -0800120 JdwpTag resultTag;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121 uint64_t resultValue;
122 ObjectId exceptObjId;
Elliott Hughes74847412012-06-20 18:10:21 -0700123 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 -0700124 if (err != ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800125 return err;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 }
127
128 if (err == ERR_NONE) {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800129 if (is_constructor) {
130 // If we invoked a constructor (which actually returns void), return the receiver,
131 // unless we threw, in which case we return NULL.
132 resultTag = JT_OBJECT;
Elliott Hughes74847412012-06-20 18:10:21 -0700133 resultValue = (exceptObjId == 0) ? object_id : 0;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800134 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700135
Elliott Hughes45651fd2012-02-21 15:48:20 -0800136 size_t width = Dbg::GetTagWidth(resultTag);
137 expandBufAdd1(pReply, resultTag);
138 if (width != 0) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700139 JdwpWriteValue(pReply, width, resultValue);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140 }
141 expandBufAdd1(pReply, JT_OBJECT);
142 expandBufAddObjectId(pReply, exceptObjId);
143
Elliott Hughes229feb72012-02-23 13:33:29 -0800144 VLOG(jdwp) << " --> returned " << resultTag << StringPrintf(" %#llx (except=%#llx)", resultValue, exceptObjId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700145
146 /* show detailed debug output */
147 if (resultTag == JT_STRING && exceptObjId == 0) {
148 if (resultValue != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800149 VLOG(jdwp) << " string '" << Dbg::StringToUtf8(resultValue) << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700150 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800151 VLOG(jdwp) << " string (null)";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152 }
153 }
154 }
155
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700156 return err;
157}
158
159
160/*
161 * Request for version info.
162 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163static JdwpError VM_Version(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700165 /* text information on runtime version */
166 std::string version(StringPrintf("Android Runtime %s", Runtime::Current()->GetVersion()));
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800167 expandBufAddUtf8String(pReply, version);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700168 /* JDWP version numbers */
169 expandBufAdd4BE(pReply, 1); // major
170 expandBufAdd4BE(pReply, 5); // minor
171 /* VM JRE version */
Elliott Hughesa2155262011-11-16 16:26:58 -0800172 expandBufAddUtf8String(pReply, "1.6.0"); /* e.g. 1.6.0_22 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700173 /* target VM name */
Elliott Hughesa2155262011-11-16 16:26:58 -0800174 expandBufAddUtf8String(pReply, "DalvikVM");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700175
176 return ERR_NONE;
177}
178
179/*
180 * Given a class JNI signature (e.g. "Ljava/lang/Error;"), return the
181 * referenceTypeID. We need to send back more than one if the class has
182 * been loaded by multiple class loaders.
183 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700184static JdwpError VM_ClassesBySignature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800186 std::string classDescriptor(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800187 VLOG(jdwp) << " Req for class by signature '" << classDescriptor << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700188
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800189 std::vector<RefTypeId> ids;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800190 Dbg::FindLoadedClassBySignature(classDescriptor.c_str(), ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700191
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800192 expandBufAdd4BE(pReply, ids.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700193
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800194 for (size_t i = 0; i < ids.size(); ++i) {
195 // Get class vs. interface and status flags.
Elliott Hughes436e3722012-02-17 20:01:47 -0800196 JDWP::JdwpTypeTag type_tag;
197 uint32_t class_status;
198 JDWP::JdwpError status = Dbg::GetClassInfo(ids[i], &type_tag, &class_status, NULL);
199 if (status != ERR_NONE) {
200 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800201 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202
Elliott Hughes436e3722012-02-17 20:01:47 -0800203 expandBufAdd1(pReply, type_tag);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800204 expandBufAddRefTypeId(pReply, ids[i]);
Elliott Hughes436e3722012-02-17 20:01:47 -0800205 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700206 }
207
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700208 return ERR_NONE;
209}
210
211/*
212 * Handle request for the thread IDs of all running threads.
213 *
214 * We exclude ourselves from the list, because we don't allow ourselves
215 * to be suspended, and that violates some JDWP expectations.
216 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700217static JdwpError VM_AllThreads(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -0700219 std::vector<ObjectId> thread_ids;
Elliott Hughes026b1462012-06-28 20:43:49 -0700220 Dbg::GetThreads(0, thread_ids);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700221
Elliott Hughescaf76542012-06-28 16:08:22 -0700222 expandBufAdd4BE(pReply, thread_ids.size());
223 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
224 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700225 }
226
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700227 return ERR_NONE;
228}
229
230/*
231 * List all thread groups that do not have a parent.
232 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233static JdwpError VM_TopLevelThreadGroups(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235 /*
236 * TODO: maintain a list of parentless thread groups in the VM.
237 *
238 * For now, just return "system". Application threads are created
239 * in "main", which is a child of "system".
240 */
241 uint32_t groups = 1;
242 expandBufAdd4BE(pReply, groups);
Elliott Hughescaf76542012-06-28 16:08:22 -0700243 //thread_group_id = debugGetMainThreadGroup();
244 //expandBufAdd8BE(pReply, thread_group_id);
245 ObjectId thread_group_id = Dbg::GetSystemThreadGroupId();
246 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700247
248 return ERR_NONE;
249}
250
251/*
252 * Respond with the sizes of the basic debugger types.
253 *
254 * All IDs are 8 bytes.
255 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256static JdwpError VM_IDSizes(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700258 expandBufAdd4BE(pReply, sizeof(FieldId));
259 expandBufAdd4BE(pReply, sizeof(MethodId));
260 expandBufAdd4BE(pReply, sizeof(ObjectId));
261 expandBufAdd4BE(pReply, sizeof(RefTypeId));
262 expandBufAdd4BE(pReply, sizeof(FrameId));
263 return ERR_NONE;
264}
265
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266static JdwpError VM_Dispose(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700267 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes86964332012-02-15 19:37:42 -0800268 Dbg::Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700269 return ERR_NONE;
270}
271
272/*
273 * Suspend the execution of the application running in the VM (i.e. suspend
274 * all threads).
275 *
276 * This needs to increment the "suspend count" on all threads.
277 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278static JdwpError VM_Suspend(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800280 Thread* self = Thread::Current();
281 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes475fc232011-10-25 15:00:35 -0700282 Dbg::SuspendVM();
jeffhaoa77f0f62012-12-05 17:19:31 -0800283 self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700284 return ERR_NONE;
285}
286
287/*
288 * Resume execution. Decrements the "suspend count" of all threads.
289 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290static JdwpError VM_Resume(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700291 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700292 Dbg::ResumeVM();
293 return ERR_NONE;
294}
295
296/*
297 * The debugger wants the entire VM to exit.
298 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700299static JdwpError VM_Exit(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700301 uint32_t exitCode = Get4BE(buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700302
303 LOG(WARNING) << "Debugger is telling the VM to exit with code=" << exitCode;
304
305 Dbg::Exit(exitCode);
306 return ERR_NOT_IMPLEMENTED; // shouldn't get here
307}
308
309/*
310 * Create a new string in the VM and return its ID.
311 *
312 * (Ctrl-Shift-I in Eclipse on an array of objects causes it to create the
313 * string "java.util.Arrays".)
314 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700315static JdwpError VM_CreateString(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700316 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800317 std::string str(ReadNewUtf8String(&buf));
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800318 VLOG(jdwp) << " Req to create string '" << str << "'";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700319 ObjectId stringId = Dbg::CreateString(str);
320 if (stringId == 0) {
321 return ERR_OUT_OF_MEMORY;
322 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700323 expandBufAddObjectId(pReply, stringId);
324 return ERR_NONE;
325}
326
327/*
328 * Tell the debugger what we are capable of.
329 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700330static JdwpError VM_Capabilities(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700331 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700332 expandBufAdd1(pReply, false); /* canWatchFieldModification */
333 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
334 expandBufAdd1(pReply, false); /* canGetBytecodes */
335 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
336 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
337 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
338 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
339 return ERR_NONE;
340}
341
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342static JdwpError VM_ClassPaths(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700343 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800344 expandBufAddUtf8String(pReply, "/");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700345
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800346 std::vector<std::string> class_path;
347 Split(Runtime::Current()->GetClassPathString(), ':', class_path);
348 expandBufAdd4BE(pReply, class_path.size());
349 for (size_t i = 0; i < class_path.size(); ++i) {
350 expandBufAddUtf8String(pReply, class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700351 }
352
Elliott Hughesa3ae2b72012-02-24 15:10:51 -0800353 std::vector<std::string> boot_class_path;
354 Split(Runtime::Current()->GetBootClassPathString(), ':', boot_class_path);
355 expandBufAdd4BE(pReply, boot_class_path.size());
356 for (size_t i = 0; i < boot_class_path.size(); ++i) {
357 expandBufAddUtf8String(pReply, boot_class_path[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700358 }
359
360 return ERR_NONE;
361}
362
363/*
364 * Release a list of object IDs. (Seen in jdb.)
365 *
366 * Currently does nothing.
367 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368static JdwpError VM_DisposeObjects(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700369 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700370 return ERR_NONE;
371}
372
373/*
374 * Tell the debugger what we are capable of.
375 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376static JdwpError VM_CapabilitiesNew(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700377 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700378 expandBufAdd1(pReply, false); /* canWatchFieldModification */
379 expandBufAdd1(pReply, false); /* canWatchFieldAccess */
380 expandBufAdd1(pReply, false); /* canGetBytecodes */
381 expandBufAdd1(pReply, true); /* canGetSyntheticAttribute */
382 expandBufAdd1(pReply, false); /* canGetOwnedMonitorInfo */
383 expandBufAdd1(pReply, false); /* canGetCurrentContendedMonitor */
384 expandBufAdd1(pReply, false); /* canGetMonitorInfo */
385 expandBufAdd1(pReply, false); /* canRedefineClasses */
386 expandBufAdd1(pReply, false); /* canAddMethod */
387 expandBufAdd1(pReply, false); /* canUnrestrictedlyRedefineClasses */
388 expandBufAdd1(pReply, false); /* canPopFrames */
389 expandBufAdd1(pReply, false); /* canUseInstanceFilters */
390 expandBufAdd1(pReply, false); /* canGetSourceDebugExtension */
391 expandBufAdd1(pReply, false); /* canRequestVMDeathEvent */
392 expandBufAdd1(pReply, false); /* canSetDefaultStratum */
393 expandBufAdd1(pReply, false); /* 1.6: canGetInstanceInfo */
394 expandBufAdd1(pReply, false); /* 1.6: canRequestMonitorEvents */
395 expandBufAdd1(pReply, false); /* 1.6: canGetMonitorFrameInfo */
396 expandBufAdd1(pReply, false); /* 1.6: canUseSourceNameFilters */
397 expandBufAdd1(pReply, false); /* 1.6: canGetConstantPool */
398 expandBufAdd1(pReply, false); /* 1.6: canForceEarlyReturn */
399
400 /* fill in reserved22 through reserved32; note count started at 1 */
401 for (int i = 22; i <= 32; i++) {
402 expandBufAdd1(pReply, false); /* reservedN */
403 }
404 return ERR_NONE;
405}
406
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407static JdwpError VM_AllClassesImpl(ExpandBuf* pReply, bool descriptor_and_status, bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700408 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800409 std::vector<JDWP::RefTypeId> classes;
410 Dbg::GetClassList(classes);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700411
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800412 expandBufAdd4BE(pReply, classes.size());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700413
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800414 for (size_t i = 0; i < classes.size(); ++i) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800415 static const char genericSignature[1] = "";
Elliott Hughes436e3722012-02-17 20:01:47 -0800416 JDWP::JdwpTypeTag type_tag;
Elliott Hughesa2155262011-11-16 16:26:58 -0800417 std::string descriptor;
Elliott Hughes436e3722012-02-17 20:01:47 -0800418 uint32_t class_status;
419 JDWP::JdwpError status = Dbg::GetClassInfo(classes[i], &type_tag, &class_status, &descriptor);
420 if (status != ERR_NONE) {
421 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800422 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700423
Elliott Hughes436e3722012-02-17 20:01:47 -0800424 expandBufAdd1(pReply, type_tag);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800425 expandBufAddRefTypeId(pReply, classes[i]);
Elliott Hughes86964332012-02-15 19:37:42 -0800426 if (descriptor_and_status) {
427 expandBufAddUtf8String(pReply, descriptor);
428 if (generic) {
429 expandBufAddUtf8String(pReply, genericSignature);
430 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800431 expandBufAdd4BE(pReply, class_status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800432 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700433 }
434
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700435 return ERR_NONE;
436}
437
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700438static JdwpError VM_AllClasses(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700439 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700440 return VM_AllClassesImpl(pReply, true, false);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800441}
442
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700443static JdwpError VM_AllClassesWithGeneric(JdwpState*, const uint8_t*, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700444 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700445 return VM_AllClassesImpl(pReply, true, true);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800446}
447
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700448static JdwpError RT_Modifiers(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700449 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700450 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800451 return Dbg::GetModifiers(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700452}
453
454/*
455 * Get values from static fields in a reference type.
456 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700457static JdwpError RT_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700458 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800459 RefTypeId refTypeId = ReadRefTypeId(&buf);
460 uint32_t field_count = Read4BE(&buf);
461 expandBufAdd4BE(pReply, field_count);
462 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700463 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800464 JdwpError status = Dbg::GetStaticFieldValue(refTypeId, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800465 if (status != ERR_NONE) {
466 return status;
467 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700468 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700469 return ERR_NONE;
470}
471
472/*
473 * Get the name of the source file in which a reference type was declared.
474 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700475static JdwpError RT_SourceFile(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700476 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700477 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes03181a82011-11-17 17:22:21 -0800478 std::string source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -0800479 JdwpError status = Dbg::GetSourceFile(refTypeId, source_file);
480 if (status != ERR_NONE) {
481 return status;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700482 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800483 expandBufAddUtf8String(pReply, source_file);
Elliott Hughes03181a82011-11-17 17:22:21 -0800484 return ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700485}
486
487/*
488 * Return the current status of the reference type.
489 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700490static JdwpError RT_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700491 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700492 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800493 JDWP::JdwpTypeTag type_tag;
494 uint32_t class_status;
495 JDWP::JdwpError status = Dbg::GetClassInfo(refTypeId, &type_tag, &class_status, NULL);
496 if (status != ERR_NONE) {
497 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800498 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800499 expandBufAdd4BE(pReply, class_status);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700500 return ERR_NONE;
501}
502
503/*
504 * Return interfaces implemented directly by this class.
505 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700506static JdwpError RT_Interfaces(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700507 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700508 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -0800509 VLOG(jdwp) << StringPrintf(" Req for interfaces in %#llx (%s)", refTypeId, Dbg::GetClassName(refTypeId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -0800510 return Dbg::OutputDeclaredInterfaces(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700511}
512
513/*
514 * Return the class object corresponding to this type.
515 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700516static JdwpError RT_ClassObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700517 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700518 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800519 ObjectId classObjectId;
Elliott Hughes436e3722012-02-17 20:01:47 -0800520 JdwpError status = Dbg::GetClassObject(refTypeId, classObjectId);
521 if (status != ERR_NONE) {
522 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800523 }
Elliott Hughes229feb72012-02-23 13:33:29 -0800524 VLOG(jdwp) << StringPrintf(" RefTypeId %#llx -> ObjectId %#llx", refTypeId, classObjectId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800525 expandBufAddObjectId(pReply, classObjectId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700526 return ERR_NONE;
527}
528
529/*
530 * Returns the value of the SourceDebugExtension attribute.
531 *
532 * JDB seems interested, but DEX files don't currently support this.
533 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700534static JdwpError RT_SourceDebugExtension(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700535 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700536 /* referenceTypeId in, string out */
537 return ERR_ABSENT_INFORMATION;
538}
539
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700540static JdwpError RT_Signature(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
541 bool with_generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700542 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700543 RefTypeId refTypeId = ReadRefTypeId(&buf);
544
Elliott Hughes229feb72012-02-23 13:33:29 -0800545 VLOG(jdwp) << StringPrintf(" Req for signature of refTypeId=%#llx", refTypeId);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800546 std::string signature;
Elliott Hughes98e43f62012-02-24 12:42:35 -0800547
548 JdwpError status = Dbg::GetSignature(refTypeId, signature);
549 if (status != ERR_NONE) {
550 return status;
551 }
552 expandBufAddUtf8String(pReply, signature);
553 if (with_generic) {
Elliott Hughes0cf74332012-02-23 23:14:00 -0800554 expandBufAddUtf8String(pReply, "");
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700555 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700556 return ERR_NONE;
557}
558
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700559static JdwpError RT_Signature(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700560 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700561 return RT_Signature(state, buf, dataLen, pReply, false);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800562}
563
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700564static JdwpError RT_SignatureWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
565 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700566 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700567 return RT_Signature(state, buf, dataLen, pReply, true);
Elliott Hughes98e43f62012-02-24 12:42:35 -0800568}
569
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700570/*
571 * Return the instance of java.lang.ClassLoader that loaded the specified
572 * reference type, or null if it was loaded by the system loader.
573 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700574static JdwpError RT_ClassLoader(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700576 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes436e3722012-02-17 20:01:47 -0800577 return Dbg::GetClassLoader(refTypeId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700578}
579
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700580static std::string Describe(const RefTypeId& refTypeId)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700581 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800582 std::string signature("unknown");
583 Dbg::GetSignature(refTypeId, signature);
Elliott Hughes229feb72012-02-23 13:33:29 -0800584 return StringPrintf("refTypeId=%#llx (%s)", refTypeId, signature.c_str());
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800585}
586
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700587/*
588 * Given a referenceTypeId, return a block of stuff that describes the
589 * fields declared by a class.
590 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700591static JdwpError RT_FieldsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700592 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700593 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800594 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800595 return Dbg::OutputDeclaredFields(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800596}
597
598// Obsolete equivalent of FieldsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700599static JdwpError RT_Fields(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700600 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800601 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800602 VLOG(jdwp) << " Req for fields in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800603 return Dbg::OutputDeclaredFields(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700604}
605
606/*
607 * Given a referenceTypeID, return a block of goodies describing the
608 * methods declared by a class.
609 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700610static JdwpError RT_MethodsWithGeneric(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700611 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700612 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800613 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800614 return Dbg::OutputDeclaredMethods(refTypeId, true, pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800615}
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700616
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800617// Obsolete equivalent of MethodsWithGeneric, without the generic type information.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700618static JdwpError RT_Methods(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700619 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800620 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800621 VLOG(jdwp) << " Req for methods in " << Describe(refTypeId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800622 return Dbg::OutputDeclaredMethods(refTypeId, false, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700623}
624
625/*
626 * Return the immediate superclass of a class.
627 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628static JdwpError CT_Superclass(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700629 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700630 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800631 RefTypeId superClassId;
Elliott Hughes74847412012-06-20 18:10:21 -0700632 JdwpError status = Dbg::GetSuperclass(class_id, superClassId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800633 if (status != ERR_NONE) {
634 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800635 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700636 expandBufAddRefTypeId(pReply, superClassId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700637 return ERR_NONE;
638}
639
640/*
641 * Set static class values.
642 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700643static JdwpError CT_SetValues(JdwpState* , const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700644 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700645 RefTypeId class_id = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700646 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700647
Elliott Hughes74847412012-06-20 18:10:21 -0700648 VLOG(jdwp) << StringPrintf(" Req to set %d values in class_id=%#llx", values, class_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700649
650 for (uint32_t i = 0; i < values; i++) {
651 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800652 JDWP::JdwpTag fieldTag = Dbg::GetStaticFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800653 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700654 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700655
Elliott Hughes2435a572012-02-17 16:07:41 -0800656 VLOG(jdwp) << " --> field=" << fieldId << " tag=" << fieldTag << " -> " << value;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800657 JdwpError status = Dbg::SetStaticFieldValue(fieldId, value, width);
658 if (status != ERR_NONE) {
659 return status;
660 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700661 }
662
663 return ERR_NONE;
664}
665
666/*
667 * Invoke a static method.
668 *
669 * Example: Eclipse sometimes uses java/lang/Class.forName(String s) on
670 * values in the "variables" display.
671 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700672static JdwpError CT_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
673 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700674 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700675 RefTypeId class_id = ReadRefTypeId(&buf);
676 ObjectId thread_id = ReadObjectId(&buf);
677 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700678
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700679 return FinishInvoke(state, buf, dataLen, pReply, thread_id, 0, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700680}
681
682/*
683 * Create a new object of the requested type, and invoke the specified
684 * constructor.
685 *
686 * Example: in IntelliJ, create a watch on "new String(myByteArray)" to
687 * see the contents of a byte[] as a string.
688 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700689static JdwpError CT_NewInstance(JdwpState* state, const uint8_t* buf, int dataLen,
690 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700691 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700692 RefTypeId class_id = ReadRefTypeId(&buf);
693 ObjectId thread_id = ReadObjectId(&buf);
694 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700695
Elliott Hughes74847412012-06-20 18:10:21 -0700696 VLOG(jdwp) << "Creating instance of " << Dbg::GetClassName(class_id);
697 ObjectId object_id;
698 JdwpError status = Dbg::CreateObject(class_id, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800699 if (status != ERR_NONE) {
700 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800701 }
Elliott Hughes74847412012-06-20 18:10:21 -0700702 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700703 return ERR_OUT_OF_MEMORY;
704 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700705 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700706}
707
708/*
709 * Create a new array object of the requested type and length.
710 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711static JdwpError AT_newInstance(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700712 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700713 RefTypeId arrayTypeId = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700714 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700715
Elliott Hughes2435a572012-02-17 16:07:41 -0800716 VLOG(jdwp) << "Creating array " << Dbg::GetClassName(arrayTypeId) << "[" << length << "]";
Elliott Hughes74847412012-06-20 18:10:21 -0700717 ObjectId object_id;
718 JdwpError status = Dbg::CreateArrayObject(arrayTypeId, length, object_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800719 if (status != ERR_NONE) {
720 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800721 }
Elliott Hughes74847412012-06-20 18:10:21 -0700722 if (object_id == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700723 return ERR_OUT_OF_MEMORY;
724 }
725 expandBufAdd1(pReply, JT_ARRAY);
Elliott Hughes74847412012-06-20 18:10:21 -0700726 expandBufAddObjectId(pReply, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700727 return ERR_NONE;
728}
729
730/*
731 * Return line number information for the method, if present.
732 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700733static JdwpError M_LineTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700734 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700735 RefTypeId refTypeId = ReadRefTypeId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700736 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700737
Elliott Hughes74847412012-06-20 18:10:21 -0700738 VLOG(jdwp) << " Req for line table in " << Dbg::GetClassName(refTypeId) << "." << Dbg::GetMethodName(refTypeId, method_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700739
Elliott Hughes74847412012-06-20 18:10:21 -0700740 Dbg::OutputLineTable(refTypeId, method_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700741
742 return ERR_NONE;
743}
744
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700745static JdwpError M_VariableTable(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply,
746 bool generic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700747 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700748 RefTypeId class_id = ReadRefTypeId(&buf);
749 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700750
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700751 VLOG(jdwp) << StringPrintf(" Req for LocalVarTab in class=%s method=%s",
752 Dbg::GetClassName(class_id).c_str(),
753 Dbg::GetMethodName(class_id, method_id).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700754
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800755 // We could return ERR_ABSENT_INFORMATION here if the DEX file was built without local variable
756 // information. That will cause Eclipse to make a best-effort attempt at displaying local
757 // variables anonymously. However, the attempt isn't very good, so we're probably better off just
758 // not showing anything.
Elliott Hughes74847412012-06-20 18:10:21 -0700759 Dbg::OutputVariableTable(class_id, method_id, generic, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700760 return ERR_NONE;
761}
762
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700763static JdwpError M_VariableTable(JdwpState* state, const uint8_t* buf, int dataLen,
764 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700765 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700766 return M_VariableTable(state, buf, dataLen, pReply, false);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800767}
768
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700769static JdwpError M_VariableTableWithGeneric(JdwpState* state, const uint8_t* buf, int dataLen,
770 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700771 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700772 return M_VariableTable(state, buf, dataLen, pReply, true);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800773}
774
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700775/*
776 * Given an object reference, return the runtime type of the object
777 * (class or array).
778 *
Elliott Hughes74847412012-06-20 18:10:21 -0700779 * This can get called on different things, e.g. thread_id gets
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700780 * passed in here.
781 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700782static JdwpError OR_ReferenceType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700783 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700784 ObjectId object_id = ReadObjectId(&buf);
785 VLOG(jdwp) << StringPrintf(" Req for type of object_id=%#llx", object_id);
786 return Dbg::GetReferenceType(object_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700787}
788
789/*
790 * Get values from the fields of an object.
791 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700792static JdwpError OR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700793 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700794 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800795 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700796
Elliott Hughes74847412012-06-20 18:10:21 -0700797 VLOG(jdwp) << StringPrintf(" Req for %d fields from object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700798
Elliott Hughes0cf74332012-02-23 23:14:00 -0800799 expandBufAdd4BE(pReply, field_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700800
Elliott Hughes0cf74332012-02-23 23:14:00 -0800801 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700802 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -0700803 JdwpError status = Dbg::GetFieldValue(object_id, fieldId, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800804 if (status != ERR_NONE) {
805 return status;
806 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700807 }
808
809 return ERR_NONE;
810}
811
812/*
813 * Set values in the fields of an object.
814 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700815static JdwpError OR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700816 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700817 ObjectId object_id = ReadObjectId(&buf);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800818 uint32_t field_count = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700819
Elliott Hughes74847412012-06-20 18:10:21 -0700820 VLOG(jdwp) << StringPrintf(" Req to set %d fields in object_id=%#llx", field_count, object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700821
Elliott Hughes0cf74332012-02-23 23:14:00 -0800822 for (uint32_t i = 0; i < field_count; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700823 FieldId fieldId = ReadFieldId(&buf);
824
Elliott Hughesaed4be92011-12-02 16:16:23 -0800825 JDWP::JdwpTag fieldTag = Dbg::GetFieldBasicTag(fieldId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800826 size_t width = Dbg::GetTagWidth(fieldTag);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700827 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700828
Elliott Hughes2435a572012-02-17 16:07:41 -0800829 VLOG(jdwp) << " --> fieldId=" << fieldId << " tag=" << fieldTag << "(" << width << ") value=" << value;
Elliott Hughes74847412012-06-20 18:10:21 -0700830 JdwpError status = Dbg::SetFieldValue(object_id, fieldId, value, width);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800831 if (status != ERR_NONE) {
832 return status;
833 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700834 }
835
836 return ERR_NONE;
837}
838
839/*
840 * Invoke an instance method. The invocation must occur in the specified
841 * thread, which must have been suspended by an event.
842 *
843 * The call is synchronous. All threads in the VM are resumed, unless the
844 * SINGLE_THREADED flag is set.
845 *
846 * If you ask Eclipse to "inspect" an object (or ask JDB to "print" an
847 * object), it will try to invoke the object's toString() function. This
848 * feature becomes crucial when examining ArrayLists with Eclipse.
849 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700850static JdwpError OR_InvokeMethod(JdwpState* state, const uint8_t* buf, int dataLen,
851 ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700852 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700853 ObjectId object_id = ReadObjectId(&buf);
854 ObjectId thread_id = ReadObjectId(&buf);
855 RefTypeId class_id = ReadRefTypeId(&buf);
856 MethodId method_id = ReadMethodId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700857
Elliott Hughes6e9d22c2012-06-22 15:02:37 -0700858 return FinishInvoke(state, buf, dataLen, pReply, thread_id, object_id, class_id, method_id, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700859}
860
861/*
862 * Disable garbage collection of the specified object.
863 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700864static JdwpError OR_DisableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700865 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700866 // this is currently a no-op
867 return ERR_NONE;
868}
869
870/*
871 * Enable garbage collection of the specified object.
872 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700873static JdwpError OR_EnableCollection(JdwpState*, const uint8_t*, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700874 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700875 // this is currently a no-op
876 return ERR_NONE;
877}
878
879/*
880 * Determine whether an object has been garbage collected.
881 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700882static JdwpError OR_IsCollected(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700883 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700884 ObjectId object_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700885
Elliott Hughes74847412012-06-20 18:10:21 -0700886 object_id = ReadObjectId(&buf);
887 VLOG(jdwp) << StringPrintf(" Req IsCollected(%#llx)", object_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700888
889 // TODO: currently returning false; must integrate with GC
890 expandBufAdd1(pReply, 0);
891
892 return ERR_NONE;
893}
894
895/*
896 * Return the string value in a string object.
897 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898static JdwpError SR_Value(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700899 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700900 ObjectId stringObject = ReadObjectId(&buf);
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800901 std::string str(Dbg::StringToUtf8(stringObject));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700902
Elliott Hughes82914b62012-04-09 15:56:29 -0700903 VLOG(jdwp) << StringPrintf(" Req for str %#llx --> %s", stringObject, PrintableString(str).c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700904
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800905 expandBufAddUtf8String(pReply, str);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700906
907 return ERR_NONE;
908}
909
910/*
911 * Return a thread's name.
912 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700913static JdwpError TR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700914 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700915 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700916
Elliott Hughes74847412012-06-20 18:10:21 -0700917 VLOG(jdwp) << StringPrintf(" Req for name of thread %#llx", thread_id);
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800918 std::string name;
Elliott Hughes74847412012-06-20 18:10:21 -0700919 if (!Dbg::GetThreadName(thread_id, name)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700920 return ERR_INVALID_THREAD;
921 }
Elliott Hughes74847412012-06-20 18:10:21 -0700922 VLOG(jdwp) << StringPrintf(" Name of thread %#llx is \"%s\"", thread_id, name.c_str());
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800923 expandBufAddUtf8String(pReply, name);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700924
925 return ERR_NONE;
926}
927
928/*
929 * Suspend the specified thread.
930 *
931 * It's supposed to remain suspended even if interpreted code wants to
932 * resume it; only the JDI is allowed to resume it.
933 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700934static JdwpError TR_Suspend(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700935 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700936 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700937
Elliott Hughes74847412012-06-20 18:10:21 -0700938 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700939 LOG(INFO) << " Warning: ignoring request to suspend self";
940 return ERR_THREAD_NOT_SUSPENDED;
941 }
Elliott Hughes74847412012-06-20 18:10:21 -0700942 VLOG(jdwp) << StringPrintf(" Req to suspend thread %#llx", thread_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700943 Thread* self = Thread::Current();
944 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
945 JdwpError result = Dbg::SuspendThread(thread_id);
946 self->TransitionFromSuspendedToRunnable();
947 return result;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700948}
949
950/*
951 * Resume the specified thread.
952 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700953static JdwpError TR_Resume(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700954 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700955 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700956
Elliott Hughes74847412012-06-20 18:10:21 -0700957 if (thread_id == Dbg::GetThreadSelfId()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700958 LOG(INFO) << " Warning: ignoring request to resume self";
959 return ERR_NONE;
960 }
Elliott Hughes74847412012-06-20 18:10:21 -0700961 VLOG(jdwp) << StringPrintf(" Req to resume thread %#llx", thread_id);
962 Dbg::ResumeThread(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700963 return ERR_NONE;
964}
965
966/*
967 * Return status of specified thread.
968 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700969static JdwpError TR_Status(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700970 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700971 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700972
Elliott Hughes74847412012-06-20 18:10:21 -0700973 VLOG(jdwp) << StringPrintf(" Req for status of thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700974
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800975 JDWP::JdwpThreadStatus threadStatus;
976 JDWP::JdwpSuspendStatus suspendStatus;
Elliott Hughes74847412012-06-20 18:10:21 -0700977 if (!Dbg::GetThreadStatus(thread_id, &threadStatus, &suspendStatus)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700978 return ERR_INVALID_THREAD;
979 }
980
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800981 VLOG(jdwp) << " --> " << threadStatus << ", " << suspendStatus;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700982
983 expandBufAdd4BE(pReply, threadStatus);
984 expandBufAdd4BE(pReply, suspendStatus);
985
986 return ERR_NONE;
987}
988
989/*
990 * Return the thread group that the specified thread is a member of.
991 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992static JdwpError TR_ThreadGroup(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700993 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -0700994 ObjectId thread_id = ReadObjectId(&buf);
995 return Dbg::GetThreadGroup(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700996}
997
998/*
999 * Return the current call stack of a suspended thread.
1000 *
1001 * If the thread isn't suspended, the error code isn't defined, but should
1002 * be THREAD_NOT_SUSPENDED.
1003 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004static JdwpError TR_Frames(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001005 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001006 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001007 uint32_t start_frame = Read4BE(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001008 uint32_t length = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001009
Elliott Hughes74847412012-06-20 18:10:21 -07001010 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001011 return ERR_INVALID_THREAD;
1012 }
Elliott Hughes74847412012-06-20 18:10:21 -07001013 if (!Dbg::IsSuspended(thread_id)) {
1014 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001015 return ERR_THREAD_NOT_SUSPENDED;
1016 }
1017
Elliott Hughes74847412012-06-20 18:10:21 -07001018 size_t actual_frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001019
Elliott Hughes74847412012-06-20 18:10:21 -07001020 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 -08001021 if (actual_frame_count <= 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001022 return ERR_THREAD_NOT_SUSPENDED; /* == 0 means 100% native */
1023 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001024
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001025 if (start_frame > actual_frame_count) {
1026 return ERR_INVALID_INDEX;
1027 }
1028 if (length == static_cast<uint32_t>(-1)) {
1029 length = actual_frame_count - start_frame;
1030 }
1031 if (start_frame + length > actual_frame_count) {
1032 return ERR_INVALID_LENGTH;
1033 }
1034
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001035 return Dbg::GetThreadFrames(thread_id, start_frame, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001036}
1037
1038/*
1039 * Returns the #of frames on the specified thread, which must be suspended.
1040 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001041static JdwpError TR_FrameCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001042 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001043 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001044
Elliott Hughes74847412012-06-20 18:10:21 -07001045 if (!Dbg::ThreadExists(thread_id)) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001046 return ERR_INVALID_THREAD;
1047 }
Elliott Hughes74847412012-06-20 18:10:21 -07001048 if (!Dbg::IsSuspended(thread_id)) {
1049 LOG(WARNING) << StringPrintf(" Rejecting req for frames in running thread %#llx", thread_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001050 return ERR_THREAD_NOT_SUSPENDED;
1051 }
1052
Elliott Hughes74847412012-06-20 18:10:21 -07001053 int frame_count = Dbg::GetThreadFrameCount(thread_id);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001054 if (frame_count < 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001055 return ERR_INVALID_THREAD;
1056 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001057 expandBufAdd4BE(pReply, static_cast<uint32_t>(frame_count));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001058
1059 return ERR_NONE;
1060}
1061
1062/*
1063 * Get the monitor that the thread is waiting on.
1064 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001065static JdwpError TR_CurrentContendedMonitor(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001066 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001067 ReadObjectId(&buf); // thread_id
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001068
1069 // TODO: create an Object to represent the monitor (we're currently
1070 // just using a raw Monitor struct in the VM)
1071
1072 return ERR_NOT_IMPLEMENTED;
1073}
1074
1075/*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 * Return the debug suspend count for the specified thread.
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001077 *
1078 * (The thread *might* still be running -- it might not have examined
1079 * its suspend count recently.)
1080 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081static JdwpError TR_DebugSuspendCount(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001082 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001083 ObjectId thread_id = ReadObjectId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084 return Dbg::GetThreadDebugSuspendCount(thread_id, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001085}
1086
1087/*
1088 * Return the name of a thread group.
1089 *
1090 * The Eclipse debugger recognizes "main" and "system" as special.
1091 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092static JdwpError TGR_Name(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001093 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001094 ObjectId thread_group_id = ReadObjectId(&buf);
1095 VLOG(jdwp) << StringPrintf(" Req for name of thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001096
Elliott Hughescaf76542012-06-28 16:08:22 -07001097 expandBufAddUtf8String(pReply, Dbg::GetThreadGroupName(thread_group_id));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001098
1099 return ERR_NONE;
1100}
1101
1102/*
1103 * Returns the thread group -- if any -- that contains the specified
1104 * thread group.
1105 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106static JdwpError TGR_Parent(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001108 ObjectId thread_group_id = ReadObjectId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001109
Elliott Hughescaf76542012-06-28 16:08:22 -07001110 ObjectId parentGroup = Dbg::GetThreadGroupParent(thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001111 expandBufAddObjectId(pReply, parentGroup);
1112
1113 return ERR_NONE;
1114}
1115
1116/*
1117 * Return the active threads and thread groups that are part of the
1118 * specified thread group.
1119 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001120static JdwpError TGR_Children(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescaf76542012-06-28 16:08:22 -07001122 ObjectId thread_group_id = ReadObjectId(&buf);
1123 VLOG(jdwp) << StringPrintf(" Req for threads in thread_group_id=%#llx", thread_group_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001124
Elliott Hughescaf76542012-06-28 16:08:22 -07001125 std::vector<ObjectId> thread_ids;
1126 Dbg::GetThreads(thread_group_id, thread_ids);
1127 expandBufAdd4BE(pReply, thread_ids.size());
1128 for (uint32_t i = 0; i < thread_ids.size(); ++i) {
1129 expandBufAddObjectId(pReply, thread_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001130 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001131
Elliott Hughescaf76542012-06-28 16:08:22 -07001132 std::vector<ObjectId> child_thread_groups_ids;
1133 Dbg::GetChildThreadGroups(thread_group_id, child_thread_groups_ids);
1134 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
1135 for (uint32_t i = 0; i < child_thread_groups_ids.size(); ++i) {
1136 expandBufAddObjectId(pReply, child_thread_groups_ids[i]);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001137 }
1138
1139 return ERR_NONE;
1140}
1141
1142/*
1143 * Return the #of components in the array.
1144 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001145static JdwpError AR_Length(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001147 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001148 VLOG(jdwp) << StringPrintf(" Req for length of array %#llx", arrayId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001149
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001150 int length;
1151 JdwpError status = Dbg::GetArrayLength(arrayId, length);
1152 if (status != ERR_NONE) {
1153 return status;
1154 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001155 VLOG(jdwp) << " --> " << length;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001156
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001157 expandBufAdd4BE(pReply, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001158
1159 return ERR_NONE;
1160}
1161
1162/*
1163 * Return the values from an array.
1164 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001165static JdwpError AR_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001167 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001168 uint32_t firstIndex = Read4BE(&buf);
1169 uint32_t length = Read4BE(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001170 VLOG(jdwp) << StringPrintf(" Req for array values %#llx first=%d len=%d", arrayId, firstIndex, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001171
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001172 return Dbg::OutputArray(arrayId, firstIndex, length, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001173}
1174
1175/*
1176 * Set values in an array.
1177 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001178static JdwpError AR_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001180 ObjectId arrayId = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001181 uint32_t firstIndex = Read4BE(&buf);
1182 uint32_t values = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001183
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001184 VLOG(jdwp) << StringPrintf(" Req to set array values %#llx first=%d count=%d", arrayId,
1185 firstIndex, values);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001186
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001187 return Dbg::SetArrayElements(arrayId, firstIndex, values, buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001188}
1189
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190static JdwpError CLR_VisibleClasses(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001191 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -07001192 ReadObjectId(&buf); // classLoaderObject
Elliott Hughes86964332012-02-15 19:37:42 -08001193 // TODO: we should only return classes which have the given class loader as a defining or
1194 // initiating loader. The former would be easy; the latter is hard, because we don't have
1195 // any such notion.
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001196 return VM_AllClassesImpl(pReply, false, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001197}
1198
1199/*
1200 * Set an event trigger.
1201 *
1202 * Reply with a requestID.
1203 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001204static JdwpError ER_Set(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001206 const uint8_t* origBuf = buf;
1207
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001208 uint8_t eventKind = Read1(&buf);
Elliott Hughesf8349362012-06-18 15:00:06 -07001209 uint8_t suspend_policy = Read1(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001210 uint32_t modifierCount = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001211
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001212 VLOG(jdwp) << " Set(kind=" << JdwpEventKind(eventKind)
Elliott Hughesf8349362012-06-18 15:00:06 -07001213 << " suspend=" << JdwpSuspendPolicy(suspend_policy)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001214 << " mods=" << modifierCount << ")";
1215
1216 CHECK_LT(modifierCount, 256U); /* reasonableness check */
1217
1218 JdwpEvent* pEvent = EventAlloc(modifierCount);
1219 pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
Elliott Hughesf8349362012-06-18 15:00:06 -07001220 pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001221 pEvent->modCount = modifierCount;
1222
1223 /*
1224 * Read modifiers. Ordering may be significant (see explanation of Count
1225 * mods in JDWP doc).
1226 */
Elliott Hughes972a47b2012-02-21 18:16:06 -08001227 for (uint32_t i = 0; i < modifierCount; ++i) {
1228 JdwpEventMod& mod = pEvent->mods[i];
1229 mod.modKind = static_cast<JdwpModKind>(Read1(&buf));
1230 switch (mod.modKind) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001231 case MK_COUNT: /* report once, when "--count" reaches 0 */
1232 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001233 uint32_t count = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001234 VLOG(jdwp) << " Count: " << count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001235 if (count == 0) {
1236 return ERR_INVALID_COUNT;
1237 }
Elliott Hughes972a47b2012-02-21 18:16:06 -08001238 mod.count.count = count;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001239 }
1240 break;
1241 case MK_CONDITIONAL: /* conditional on expression) */
1242 {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001243 uint32_t exprId = Read4BE(&buf);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001244 VLOG(jdwp) << " Conditional: " << exprId;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001245 mod.conditional.exprId = exprId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001246 }
1247 break;
1248 case MK_THREAD_ONLY: /* only report events in specified thread */
1249 {
Elliott Hughes74847412012-06-20 18:10:21 -07001250 ObjectId thread_id = ReadObjectId(&buf);
1251 VLOG(jdwp) << StringPrintf(" ThreadOnly: %#llx", thread_id);
1252 mod.threadOnly.threadId = thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001253 }
1254 break;
1255 case MK_CLASS_ONLY: /* for ClassPrepare, MethodEntry */
1256 {
Elliott Hughes74847412012-06-20 18:10:21 -07001257 RefTypeId class_id = ReadRefTypeId(&buf);
1258 VLOG(jdwp) << StringPrintf(" ClassOnly: %#llx (%s)", class_id, Dbg::GetClassName(class_id).c_str());
1259 mod.classOnly.refTypeId = class_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001260 }
1261 break;
1262 case MK_CLASS_MATCH: /* restrict events to matching classes */
1263 {
Elliott Hughes86964332012-02-15 19:37:42 -08001264 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001265 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001266 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001267 VLOG(jdwp) << " ClassMatch: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001268 mod.classMatch.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001269 }
1270 break;
1271 case MK_CLASS_EXCLUDE: /* restrict events to non-matching classes */
1272 {
Elliott Hughes86964332012-02-15 19:37:42 -08001273 // pattern is "java.foo.*", we want "java/foo/*".
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001274 std::string pattern(ReadNewUtf8String(&buf));
Elliott Hughes86964332012-02-15 19:37:42 -08001275 std::replace(pattern.begin(), pattern.end(), '.', '/');
Elliott Hughes2435a572012-02-17 16:07:41 -08001276 VLOG(jdwp) << " ClassExclude: '" << pattern << "'";
Elliott Hughes972a47b2012-02-21 18:16:06 -08001277 mod.classExclude.classPattern = strdup(pattern.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001278 }
1279 break;
1280 case MK_LOCATION_ONLY: /* restrict certain events based on loc */
1281 {
1282 JdwpLocation loc;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001283 JdwpReadLocation(&buf, &loc);
Elliott Hughes2435a572012-02-17 16:07:41 -08001284 VLOG(jdwp) << " LocationOnly: " << loc;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001285 mod.locationOnly.loc = loc;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001286 }
1287 break;
1288 case MK_EXCEPTION_ONLY: /* modifies EK_EXCEPTION events */
1289 {
1290 RefTypeId exceptionOrNull; /* null == all exceptions */
1291 uint8_t caught, uncaught;
1292
1293 exceptionOrNull = ReadRefTypeId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001294 caught = Read1(&buf);
1295 uncaught = Read1(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001296 VLOG(jdwp) << StringPrintf(" ExceptionOnly: type=%#llx(%s) caught=%d uncaught=%d",
Elliott Hughesc308a5d2012-02-16 17:12:06 -08001297 exceptionOrNull, (exceptionOrNull == 0) ? "null" : Dbg::GetClassName(exceptionOrNull).c_str(), caught, uncaught);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001298
Elliott Hughes972a47b2012-02-21 18:16:06 -08001299 mod.exceptionOnly.refTypeId = exceptionOrNull;
1300 mod.exceptionOnly.caught = caught;
1301 mod.exceptionOnly.uncaught = uncaught;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001302 }
1303 break;
1304 case MK_FIELD_ONLY: /* for field access/mod events */
1305 {
1306 RefTypeId declaring = ReadRefTypeId(&buf);
1307 FieldId fieldId = ReadFieldId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001308 VLOG(jdwp) << StringPrintf(" FieldOnly: %#llx %x", declaring, fieldId);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001309 mod.fieldOnly.refTypeId = declaring;
1310 mod.fieldOnly.fieldId = fieldId;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001311 }
1312 break;
1313 case MK_STEP: /* for use with EK_SINGLE_STEP */
1314 {
Elliott Hughes74847412012-06-20 18:10:21 -07001315 ObjectId thread_id;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001316 uint32_t size, depth;
1317
Elliott Hughes74847412012-06-20 18:10:21 -07001318 thread_id = ReadObjectId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001319 size = Read4BE(&buf);
1320 depth = Read4BE(&buf);
Elliott Hughes74847412012-06-20 18:10:21 -07001321 VLOG(jdwp) << StringPrintf(" Step: thread=%#llx", thread_id)
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001322 << " size=" << JdwpStepSize(size) << " depth=" << JdwpStepDepth(depth);
1323
Elliott Hughes74847412012-06-20 18:10:21 -07001324 mod.step.threadId = thread_id;
Elliott Hughes972a47b2012-02-21 18:16:06 -08001325 mod.step.size = size;
1326 mod.step.depth = depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001327 }
1328 break;
1329 case MK_INSTANCE_ONLY: /* report events related to a specific obj */
1330 {
1331 ObjectId instance = ReadObjectId(&buf);
Elliott Hughes229feb72012-02-23 13:33:29 -08001332 VLOG(jdwp) << StringPrintf(" InstanceOnly: %#llx", instance);
Elliott Hughes972a47b2012-02-21 18:16:06 -08001333 mod.instanceOnly.objectId = instance;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001334 }
1335 break;
1336 default:
Elliott Hughes972a47b2012-02-21 18:16:06 -08001337 LOG(WARNING) << "GLITCH: unsupported modKind=" << mod.modKind;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001338 break;
1339 }
1340 }
1341
1342 /*
1343 * Make sure we consumed all data. It is possible that the remote side
1344 * has sent us bad stuff, but for now we blame ourselves.
1345 */
1346 if (buf != origBuf + dataLen) {
1347 LOG(WARNING) << "GLITCH: dataLen is " << dataLen << ", we have consumed " << (buf - origBuf);
1348 }
1349
1350 /*
1351 * We reply with an integer "requestID".
1352 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001353 uint32_t requestId = state->NextEventSerial();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001354 expandBufAdd4BE(pReply, requestId);
1355
1356 pEvent->requestId = requestId;
1357
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001358 VLOG(jdwp) << StringPrintf(" --> event requestId=%#x", requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001359
1360 /* add it to the list */
Elliott Hughes761928d2011-11-16 18:33:03 -08001361 JdwpError err = state->RegisterEvent(pEvent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001362 if (err != ERR_NONE) {
1363 /* registration failed, probably because event is bogus */
1364 EventFree(pEvent);
1365 LOG(WARNING) << "WARNING: event request rejected";
1366 }
1367 return err;
1368}
1369
1370/*
1371 * Clear an event. Failure to find an event with a matching ID is a no-op
1372 * and does not return an error.
1373 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001374static JdwpError ER_Clear(JdwpState* state, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001375 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001376 uint8_t eventKind;
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001377 eventKind = Read1(&buf);
1378 uint32_t requestId = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001379
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001380 VLOG(jdwp) << StringPrintf(" Req to clear eventKind=%d requestId=%#x", eventKind, requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001381
Elliott Hughes761928d2011-11-16 18:33:03 -08001382 state->UnregisterEventById(requestId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001383
1384 return ERR_NONE;
1385}
1386
1387/*
1388 * Return the values of arguments and local variables.
1389 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001390static JdwpError SF_GetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001391 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001392 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001393 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001394 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001395
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001396 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 -07001397
1398 expandBufAdd4BE(pReply, slots); /* "int values" */
1399 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001400 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001401 JDWP::JdwpTag reqSigByte = ReadTag(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001402
Elliott Hughes2435a572012-02-17 16:07:41 -08001403 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001404
Elliott Hughesdbb40792011-11-18 17:05:22 -08001405 size_t width = Dbg::GetTagWidth(reqSigByte);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001406 uint8_t* ptr = expandBufAddSpace(pReply, width+1);
Elliott Hughes74847412012-06-20 18:10:21 -07001407 Dbg::GetLocalValue(thread_id, frame_id, slot, reqSigByte, ptr, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001408 }
1409
1410 return ERR_NONE;
1411}
1412
1413/*
1414 * Set the values of arguments and local variables.
1415 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001416static JdwpError SF_SetValues(JdwpState*, const uint8_t* buf, int, ExpandBuf*)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001417 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes74847412012-06-20 18:10:21 -07001418 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001419 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001420 uint32_t slots = Read4BE(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001421
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001422 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 -07001423
1424 for (uint32_t i = 0; i < slots; i++) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001425 uint32_t slot = Read4BE(&buf);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001426 JDWP::JdwpTag sigByte = ReadTag(&buf);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001427 size_t width = Dbg::GetTagWidth(sigByte);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001428 uint64_t value = JdwpReadValue(&buf, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001429
Elliott Hughes2435a572012-02-17 16:07:41 -08001430 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
Elliott Hughes74847412012-06-20 18:10:21 -07001431 Dbg::SetLocalValue(thread_id, frame_id, slot, sigByte, value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001432 }
1433
1434 return ERR_NONE;
1435}
1436
1437/*
1438 * Returns the value of "this" for the specified frame.
1439 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001440static JdwpError SF_ThisObject(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001441 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001442 ObjectId thread_id = ReadObjectId(&buf);
Elliott Hughes546b9862012-06-20 16:06:13 -07001443 FrameId frame_id = ReadFrameId(&buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001444
Elliott Hughes546b9862012-06-20 16:06:13 -07001445 ObjectId id;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001446 JdwpError rc = Dbg::GetThisObject(thread_id, frame_id, &id);
1447 if (rc != ERR_NONE) {
1448 return rc;
Elliott Hughes546b9862012-06-20 16:06:13 -07001449 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001450
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001451 uint8_t tag;
1452 rc = Dbg::GetObjectTag(id, tag);
1453 if (rc != ERR_NONE) {
1454 return rc;
1455 }
1456
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001457 VLOG(jdwp) << StringPrintf(" Req for 'this' in thread_id=%#llx frame=%lld --> %#llx '%c'",
1458 thread_id, frame_id, id, static_cast<char>(tag));
Elliott Hughes546b9862012-06-20 16:06:13 -07001459 expandBufAdd1(pReply, tag);
1460 expandBufAddObjectId(pReply, id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001461
1462 return ERR_NONE;
1463}
1464
1465/*
1466 * Return the reference type reflected by this class object.
1467 *
1468 * This appears to be required because ReferenceTypeId values are NEVER
1469 * reused, whereas ClassIds can be recycled like any other object. (Either
1470 * that, or I have no idea what this is for.)
1471 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001472static JdwpError COR_ReflectedType(JdwpState*, const uint8_t* buf, int, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001473 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001474 RefTypeId classObjectId = ReadRefTypeId(&buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001475 VLOG(jdwp) << StringPrintf(" Req for refTypeId for class=%#llx (%s)", classObjectId,
1476 Dbg::GetClassName(classObjectId).c_str());
Elliott Hughes436e3722012-02-17 20:01:47 -08001477 return Dbg::GetReflectedType(classObjectId, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001478}
1479
1480/*
1481 * Handle a DDM packet with a single chunk in it.
1482 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001483static JdwpError DDM_Chunk(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001484 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001485 uint8_t* replyBuf = NULL;
1486 int replyLen = -1;
1487
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001488 VLOG(jdwp) << StringPrintf(" Handling DDM packet (%.4s)", buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001489
Elliott Hughesa21039c2012-06-21 12:09:25 -07001490 state->NotifyDdmsActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001491
1492 /*
1493 * If they want to send something back, we copy it into the buffer.
1494 * A no-copy approach would be nicer.
1495 *
1496 * TODO: consider altering the JDWP stuff to hold the packet header
1497 * in a separate buffer. That would allow us to writev() DDM traffic
1498 * instead of copying it into the expanding buffer. The reduction in
1499 * heap requirements is probably more valuable than the efficiency.
1500 */
1501 if (Dbg::DdmHandlePacket(buf, dataLen, &replyBuf, &replyLen)) {
1502 CHECK(replyLen > 0 && replyLen < 1*1024*1024);
1503 memcpy(expandBufAddSpace(pReply, replyLen), replyBuf, replyLen);
1504 free(replyBuf);
1505 }
1506 return ERR_NONE;
1507}
1508
1509/*
1510 * Handler map decl.
1511 */
1512typedef JdwpError (*JdwpRequestHandler)(JdwpState* state, const uint8_t* buf, int dataLen, ExpandBuf* reply);
1513
1514struct JdwpHandlerMap {
1515 uint8_t cmdSet;
1516 uint8_t cmd;
1517 JdwpRequestHandler func;
1518 const char* descr;
1519};
1520
1521/*
1522 * Map commands to functions.
1523 *
1524 * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
1525 * and 128-256 are vendor-defined.
1526 */
1527static const JdwpHandlerMap gHandlerMap[] = {
1528 /* VirtualMachine command set (1) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001529 { 1, 1, VM_Version, "VirtualMachine.Version" },
1530 { 1, 2, VM_ClassesBySignature, "VirtualMachine.ClassesBySignature" },
1531 { 1, 3, VM_AllClasses, "VirtualMachine.AllClasses" },
1532 { 1, 4, VM_AllThreads, "VirtualMachine.AllThreads" },
1533 { 1, 5, VM_TopLevelThreadGroups, "VirtualMachine.TopLevelThreadGroups" },
1534 { 1, 6, VM_Dispose, "VirtualMachine.Dispose" },
1535 { 1, 7, VM_IDSizes, "VirtualMachine.IDSizes" },
1536 { 1, 8, VM_Suspend, "VirtualMachine.Suspend" },
1537 { 1, 9, VM_Resume, "VirtualMachine.Resume" },
1538 { 1, 10, VM_Exit, "VirtualMachine.Exit" },
1539 { 1, 11, VM_CreateString, "VirtualMachine.CreateString" },
1540 { 1, 12, VM_Capabilities, "VirtualMachine.Capabilities" },
1541 { 1, 13, VM_ClassPaths, "VirtualMachine.ClassPaths" },
1542 { 1, 14, VM_DisposeObjects, "VirtualMachine.DisposeObjects" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001543 { 1, 15, NULL, "VirtualMachine.HoldEvents" },
1544 { 1, 16, NULL, "VirtualMachine.ReleaseEvents" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001545 { 1, 17, VM_CapabilitiesNew, "VirtualMachine.CapabilitiesNew" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001546 { 1, 18, NULL, "VirtualMachine.RedefineClasses" },
1547 { 1, 19, NULL, "VirtualMachine.SetDefaultStratum" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001548 { 1, 20, VM_AllClassesWithGeneric, "VirtualMachine.AllClassesWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001549 { 1, 21, NULL, "VirtualMachine.InstanceCounts" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001550
1551 /* ReferenceType command set (2) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001552 { 2, 1, RT_Signature, "ReferenceType.Signature" },
1553 { 2, 2, RT_ClassLoader, "ReferenceType.ClassLoader" },
1554 { 2, 3, RT_Modifiers, "ReferenceType.Modifiers" },
1555 { 2, 4, RT_Fields, "ReferenceType.Fields" },
1556 { 2, 5, RT_Methods, "ReferenceType.Methods" },
1557 { 2, 6, RT_GetValues, "ReferenceType.GetValues" },
1558 { 2, 7, RT_SourceFile, "ReferenceType.SourceFile" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001559 { 2, 8, NULL, "ReferenceType.NestedTypes" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001560 { 2, 9, RT_Status, "ReferenceType.Status" },
1561 { 2, 10, RT_Interfaces, "ReferenceType.Interfaces" },
1562 { 2, 11, RT_ClassObject, "ReferenceType.ClassObject" },
1563 { 2, 12, RT_SourceDebugExtension, "ReferenceType.SourceDebugExtension" },
1564 { 2, 13, RT_SignatureWithGeneric, "ReferenceType.SignatureWithGeneric" },
1565 { 2, 14, RT_FieldsWithGeneric, "ReferenceType.FieldsWithGeneric" },
1566 { 2, 15, RT_MethodsWithGeneric, "ReferenceType.MethodsWithGeneric" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001567 { 2, 16, NULL, "ReferenceType.Instances" },
1568 { 2, 17, NULL, "ReferenceType.ClassFileVersion" },
1569 { 2, 18, NULL, "ReferenceType.ConstantPool" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001570
1571 /* ClassType command set (3) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001572 { 3, 1, CT_Superclass, "ClassType.Superclass" },
1573 { 3, 2, CT_SetValues, "ClassType.SetValues" },
1574 { 3, 3, CT_InvokeMethod, "ClassType.InvokeMethod" },
1575 { 3, 4, CT_NewInstance, "ClassType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001576
1577 /* ArrayType command set (4) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001578 { 4, 1, AT_newInstance, "ArrayType.NewInstance" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001579
1580 /* InterfaceType command set (5) */
1581
1582 /* Method command set (6) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001583 { 6, 1, M_LineTable, "Method.LineTable" },
1584 { 6, 2, M_VariableTable, "Method.VariableTable" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001585 { 6, 3, NULL, "Method.Bytecodes" },
1586 { 6, 4, NULL, "Method.IsObsolete" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001587 { 6, 5, M_VariableTableWithGeneric, "Method.VariableTableWithGeneric" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001588
1589 /* Field command set (8) */
1590
1591 /* ObjectReference command set (9) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001592 { 9, 1, OR_ReferenceType, "ObjectReference.ReferenceType" },
1593 { 9, 2, OR_GetValues, "ObjectReference.GetValues" },
1594 { 9, 3, OR_SetValues, "ObjectReference.SetValues" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001595 { 9, 4, NULL, "ObjectReference.UNUSED" },
1596 { 9, 5, NULL, "ObjectReference.MonitorInfo" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001597 { 9, 6, OR_InvokeMethod, "ObjectReference.InvokeMethod" },
1598 { 9, 7, OR_DisableCollection, "ObjectReference.DisableCollection" },
1599 { 9, 8, OR_EnableCollection, "ObjectReference.EnableCollection" },
1600 { 9, 9, OR_IsCollected, "ObjectReference.IsCollected" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001601 { 9, 10, NULL, "ObjectReference.ReferringObjects" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001602
1603 /* StringReference command set (10) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001604 { 10, 1, SR_Value, "StringReference.Value" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001605
1606 /* ThreadReference command set (11) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001607 { 11, 1, TR_Name, "ThreadReference.Name" },
1608 { 11, 2, TR_Suspend, "ThreadReference.Suspend" },
1609 { 11, 3, TR_Resume, "ThreadReference.Resume" },
1610 { 11, 4, TR_Status, "ThreadReference.Status" },
1611 { 11, 5, TR_ThreadGroup, "ThreadReference.ThreadGroup" },
1612 { 11, 6, TR_Frames, "ThreadReference.Frames" },
1613 { 11, 7, TR_FrameCount, "ThreadReference.FrameCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001614 { 11, 8, NULL, "ThreadReference.OwnedMonitors" },
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001615 { 11, 9, TR_CurrentContendedMonitor, "ThreadReference.CurrentContendedMonitor" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001616 { 11, 10, NULL, "ThreadReference.Stop" },
Elliott Hughes74847412012-06-20 18:10:21 -07001617 { 11, 11, NULL, "ThreadReference.Interrupt" },
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001618 { 11, 12, TR_DebugSuspendCount, "ThreadReference.SuspendCount" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001619 { 11, 13, NULL, "ThreadReference.OwnedMonitorsStackDepthInfo" },
1620 { 11, 14, NULL, "ThreadReference.ForceEarlyReturn" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001621
1622 /* ThreadGroupReference command set (12) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001623 { 12, 1, TGR_Name, "ThreadGroupReference.Name" },
1624 { 12, 2, TGR_Parent, "ThreadGroupReference.Parent" },
1625 { 12, 3, TGR_Children, "ThreadGroupReference.Children" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001626
1627 /* ArrayReference command set (13) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001628 { 13, 1, AR_Length, "ArrayReference.Length" },
1629 { 13, 2, AR_GetValues, "ArrayReference.GetValues" },
1630 { 13, 3, AR_SetValues, "ArrayReference.SetValues" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001631
1632 /* ClassLoaderReference command set (14) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001633 { 14, 1, CLR_VisibleClasses, "ClassLoaderReference.VisibleClasses" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001634
1635 /* EventRequest command set (15) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001636 { 15, 1, ER_Set, "EventRequest.Set" },
1637 { 15, 2, ER_Clear, "EventRequest.Clear" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001638 { 15, 3, NULL, "EventRequest.ClearAllBreakpoints" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001639
1640 /* StackFrame command set (16) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001641 { 16, 1, SF_GetValues, "StackFrame.GetValues" },
1642 { 16, 2, SF_SetValues, "StackFrame.SetValues" },
1643 { 16, 3, SF_ThisObject, "StackFrame.ThisObject" },
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001644 { 16, 4, NULL, "StackFrame.PopFrames" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001645
1646 /* ClassObjectReference command set (17) */
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001647 { 17, 1, COR_ReflectedType, "ClassObjectReference.ReflectedType" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001648
1649 /* Event command set (64) */
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001650 { 64, 100, NULL, "Event.Composite" }, // sent from VM to debugger, never received by VM
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001651
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001652 { 199, 1, DDM_Chunk, "DDM.Chunk" },
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001653};
1654
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001655static const char* GetCommandName(size_t cmdSet, size_t cmd) {
Elliott Hughes74847412012-06-20 18:10:21 -07001656 for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001657 if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
1658 return gHandlerMap[i].descr;
1659 }
1660 }
1661 return "?UNKNOWN?";
1662}
1663
1664static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
1665 std::string result;
1666 result += "REQ: ";
1667 result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
1668 result += StringPrintf(" (dataLen=%d id=0x%06x)", dataLen, pHeader->id);
1669 return result;
1670}
1671
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001672/*
1673 * Process a request from the debugger.
1674 *
1675 * On entry, the JDWP thread is in VMWAIT.
1676 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001677void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001678 JdwpError result = ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001679
1680 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
1681 /*
1682 * Activity from a debugger, not merely ddms. Mark us as having an
1683 * active debugger session, and zero out the last-activity timestamp
1684 * so waitForDebugger() doesn't return if we stall for a bit here.
1685 */
Elliott Hughesa2155262011-11-16 16:26:58 -08001686 Dbg::GoActive();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001687 QuasiAtomic::Swap64(0, &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001688 }
1689
1690 /*
1691 * If a debugger event has fired in another thread, wait until the
1692 * initiating thread has suspended itself before processing messages
1693 * from the debugger. Otherwise we (the JDWP thread) could be told to
1694 * resume the thread before it has suspended.
1695 *
1696 * We call with an argument of zero to wait for the current event
1697 * thread to finish, and then clear the block. Depending on the thread
1698 * suspend policy, this may allow events in other threads to fire,
1699 * but those events have no bearing on what the debugger has sent us
1700 * in the current request.
1701 *
1702 * Note that we MUST clear the event token before waking the event
1703 * thread up, or risk waiting for the thread to suspend after we've
1704 * told it to resume.
1705 */
Elliott Hughes376a7a02011-10-24 18:35:55 -07001706 SetWaitForEventThread(0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001707
1708 /*
1709 * Tell the VM that we're running and shouldn't be interrupted by GC.
1710 * Do this after anything that can stall indefinitely.
1711 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001712 Thread* self = Thread::Current();
1713 ThreadState old_state = self->TransitionFromSuspendedToRunnable();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001714
1715 expandBufAddSpace(pReply, kJDWPHeaderLen);
1716
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001717 size_t i;
1718 for (i = 0; i < arraysize(gHandlerMap); i++) {
1719 if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001720 VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
Elliott Hughes376a7a02011-10-24 18:35:55 -07001721 result = (*gHandlerMap[i].func)(this, buf, dataLen, pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001722 break;
1723 }
1724 }
1725 if (i == arraysize(gHandlerMap)) {
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001726 LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
1727 LOG(ERROR) << HexDump(buf, dataLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001728 result = ERR_NOT_IMPLEMENTED;
1729 }
1730
1731 /*
1732 * Set up the reply header.
1733 *
1734 * If we encountered an error, only send the header back.
1735 */
1736 uint8_t* replyBuf = expandBufGetBuffer(pReply);
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001737 Set4BE(replyBuf + 4, pHeader->id);
1738 Set1(replyBuf + 8, kJDWPFlagReply);
1739 Set2BE(replyBuf + 9, result);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001740 if (result == ERR_NONE) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001741 Set4BE(replyBuf + 0, expandBufGetLength(pReply));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001742 } else {
Elliott Hughesf7c3b662011-10-27 12:04:56 -07001743 Set4BE(replyBuf + 0, kJDWPHeaderLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001744 }
1745
Elliott Hughesa3c24aa2011-12-07 15:34:09 -08001746 size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001747 if (false) {
1748 LOG(INFO) << "reply: dataLen=" << respLen << " err=" << result << (result != ERR_NONE ? " **FAILED**" : "");
Elliott Hughesbfbf0e22012-03-29 18:09:19 -07001749 LOG(INFO) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001750 }
1751
1752 /*
1753 * Update last-activity timestamp. We really only need this during
1754 * the initial setup. Only update if this is a non-DDMS packet.
1755 */
1756 if (pHeader->cmdSet != kJDWPDdmCmdSet) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001757 QuasiAtomic::Swap64(MilliTime(), &last_activity_time_ms_);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001758 }
1759
1760 /* tell the VM that GC is okay again */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001761 self->TransitionFromRunnableToSuspended(old_state);
1762
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001763}
1764
1765} // namespace JDWP
1766
1767} // namespace art