blob: fe726aa6bd70ee949deea1896985636c1b70a8c5 [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#include "debugger.h"
18
Elliott Hughes3bb81562011-10-21 18:52:59 -070019#include <sys/uio.h>
20
Elliott Hughes545a0642011-11-08 19:10:03 -080021#include <set>
22
23#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070026#include "dex_instruction.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/accounting/card_table-inl.h"
28#include "gc/space/large_object_space.h"
29#include "gc/space/space-inl.h"
Jeff Hao5d917302013-02-27 17:57:33 -080030#include "invoke_arg_array_builder.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080031#include "jdwp/object_registry.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/abstract_method-inl.h"
33#include "mirror/class.h"
34#include "mirror/class-inl.h"
35#include "mirror/class_loader.h"
36#include "mirror/field-inl.h"
37#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
39#include "mirror/throwable.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080040#include "oat/runtime/context.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080041#include "object_utils.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070042#include "safe_map.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080043#include "scoped_thread_state_change.h"
Elliott Hughes6a5bd492011-10-28 14:33:57 -070044#include "ScopedLocalRef.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070045#include "ScopedPrimitiveArray.h"
Ian Rogers1f539342012-10-03 21:09:42 -070046#include "sirt_ref.h"
Elliott Hughes47fce012011-10-25 18:37:19 -070047#include "stack_indirect_reference_table.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070048#include "thread_list.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080049#include "throw_location.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070051#include "well_known_classes.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070052
Brian Carlstrom3d92d522013-07-12 09:03:08 -070053#ifdef HAVE_ANDROID_OS
54#include "cutils/properties.h"
55#endif
56
Elliott Hughes872d4ec2011-10-21 17:07:15 -070057namespace art {
58
Elliott Hughes545a0642011-11-08 19:10:03 -080059static const size_t kMaxAllocRecordStackDepth = 16; // Max 255.
Elliott Hughesb1a58792013-07-11 18:10:58 -070060static const size_t kDefaultNumAllocRecords = 64*1024; // Must be a power of 2.
Elliott Hughes475fc232011-10-25 15:00:35 -070061
Elliott Hughes545a0642011-11-08 19:10:03 -080062struct AllocRecordStackTraceElement {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::AbstractMethod* method;
Ian Rogers0399dde2012-06-06 17:09:28 -070064 uint32_t dex_pc;
Elliott Hughes545a0642011-11-08 19:10:03 -080065
Ian Rogersb726dcb2012-09-05 08:57:23 -070066 int32_t LineNumber() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -070067 return MethodHelper(method).GetLineNumFromDexPC(dex_pc);
Elliott Hughes545a0642011-11-08 19:10:03 -080068 }
69};
70
71struct AllocRecord {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 mirror::Class* type;
Elliott Hughes545a0642011-11-08 19:10:03 -080073 size_t byte_count;
74 uint16_t thin_lock_id;
75 AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth]; // Unused entries have NULL method.
76
77 size_t GetDepth() {
78 size_t depth = 0;
79 while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) {
80 ++depth;
81 }
82 return depth;
83 }
84};
85
Elliott Hughes86964332012-02-15 19:37:42 -080086struct Breakpoint {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 mirror::AbstractMethod* method;
Elliott Hughesa656a0f2012-02-21 18:03:44 -080088 uint32_t dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 Breakpoint(mirror::AbstractMethod* method, uint32_t dex_pc) : method(method), dex_pc(dex_pc) {}
Elliott Hughes86964332012-02-15 19:37:42 -080090};
91
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs)
Ian Rogersb726dcb2012-09-05 08:57:23 -070093 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes229feb72012-02-23 13:33:29 -080094 os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.method).c_str(), rhs.dex_pc);
Elliott Hughes86964332012-02-15 19:37:42 -080095 return os;
96}
97
98struct SingleStepControl {
99 // Are we single-stepping right now?
100 bool is_active;
101 Thread* thread;
102
103 JDWP::JdwpStepSize step_size;
104 JDWP::JdwpStepDepth step_depth;
105
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 const mirror::AbstractMethod* method;
Elliott Hughes2435a572012-02-17 16:07:41 -0800107 int32_t line_number; // Or -1 for native methods.
108 std::set<uint32_t> dex_pcs;
Elliott Hughes86964332012-02-15 19:37:42 -0800109 int stack_depth;
110};
111
Ian Rogers62d6c772013-02-27 08:32:07 -0800112class DebugInstrumentationListener : public instrumentation::InstrumentationListener {
113 public:
114 DebugInstrumentationListener() {}
115 virtual ~DebugInstrumentationListener() {}
116
117 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
118 const mirror::AbstractMethod* method, uint32_t dex_pc)
119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
120 if (method->IsNative()) {
121 // TODO: post location events is a suspension point and native method entry stubs aren't.
122 return;
123 }
124 Dbg::PostLocationEvent(method, 0, this_object, Dbg::kMethodEntry);
125 }
126
127 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
128 const mirror::AbstractMethod* method,
129 uint32_t dex_pc, const JValue& return_value)
130 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
131 UNUSED(return_value);
132 if (method->IsNative()) {
133 // TODO: post location events is a suspension point and native method entry stubs aren't.
134 return;
135 }
136 Dbg::PostLocationEvent(method, dex_pc, this_object, Dbg::kMethodExit);
137 }
138
139 virtual void MethodUnwind(Thread* thread, const mirror::AbstractMethod* method,
140 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
141 // We're not recorded to listen to this kind of event, so complain.
142 LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method)
143 << " " << dex_pc;
144 }
145
146 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
147 const mirror::AbstractMethod* method, uint32_t new_dex_pc)
148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
149 Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc);
150 }
151
152 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
153 mirror::AbstractMethod* catch_method, uint32_t catch_dex_pc,
154 mirror::Throwable* exception_object)
155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
156 Dbg::PostException(thread, throw_location, catch_method, catch_dex_pc, exception_object);
157 }
158
159} gDebugInstrumentationListener;
160
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700161// JDWP is allowed unless the Zygote forbids it.
162static bool gJdwpAllowed = true;
163
Elliott Hughesc0f09332012-03-26 13:27:06 -0700164// Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line?
Elliott Hughes3bb81562011-10-21 18:52:59 -0700165static bool gJdwpConfigured = false;
166
Elliott Hughesc0f09332012-03-26 13:27:06 -0700167// Broken-down JDWP options. (Only valid if IsJdwpConfigured() is true.)
Elliott Hughes376a7a02011-10-24 18:35:55 -0700168static JDWP::JdwpOptions gJdwpOptions;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700169
170// Runtime JDWP state.
171static JDWP::JdwpState* gJdwpState = NULL;
172static bool gDebuggerConnected; // debugger or DDMS is connected.
173static bool gDebuggerActive; // debugger is making requests.
Elliott Hughes86964332012-02-15 19:37:42 -0800174static bool gDisposed; // debugger called VirtualMachine.Dispose, so we should drop the connection.
Elliott Hughes3bb81562011-10-21 18:52:59 -0700175
Elliott Hughes47fce012011-10-25 18:37:19 -0700176static bool gDdmThreadNotification = false;
177
Elliott Hughes767a1472011-10-26 18:49:02 -0700178// DDMS GC-related settings.
179static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
180static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
181static Dbg::HpsgWhat gDdmHpsgWhat;
182static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
183static Dbg::HpsgWhat gDdmNhsgWhat;
184
Elliott Hughes475fc232011-10-25 15:00:35 -0700185static ObjectRegistry* gRegistry = NULL;
186
Elliott Hughes545a0642011-11-08 19:10:03 -0800187// Recent allocation tracking.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188static Mutex gAllocTrackerLock DEFAULT_MUTEX_ACQUIRED_AFTER ("AllocTracker lock");
Elliott Hughesf8349362012-06-18 15:00:06 -0700189AllocRecord* Dbg::recent_allocation_records_ PT_GUARDED_BY(gAllocTrackerLock) = NULL; // TODO: CircularBuffer<AllocRecord>
Elliott Hughesb1a58792013-07-11 18:10:58 -0700190static size_t gAllocRecordMax GUARDED_BY(gAllocTrackerLock) = 0;
Elliott Hughesf8349362012-06-18 15:00:06 -0700191static size_t gAllocRecordHead GUARDED_BY(gAllocTrackerLock) = 0;
192static size_t gAllocRecordCount GUARDED_BY(gAllocTrackerLock) = 0;
Elliott Hughes545a0642011-11-08 19:10:03 -0800193
Elliott Hughes86964332012-02-15 19:37:42 -0800194// Breakpoints and single-stepping.
jeffhao09bfc6a2012-12-11 18:11:43 -0800195static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_);
196static SingleStepControl gSingleStepControl GUARDED_BY(Locks::breakpoint_lock_);
Elliott Hughes86964332012-02-15 19:37:42 -0800197
Ian Rogers62d6c772013-02-27 08:32:07 -0800198static bool IsBreakpoint(const mirror::AbstractMethod* m, uint32_t dex_pc)
jeffhao09bfc6a2012-12-11 18:11:43 -0800199 LOCKS_EXCLUDED(Locks::breakpoint_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700200 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao09bfc6a2012-12-11 18:11:43 -0800201 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Elliott Hughes86964332012-02-15 19:37:42 -0800202 for (size_t i = 0; i < gBreakpoints.size(); ++i) {
Elliott Hughesa656a0f2012-02-21 18:03:44 -0800203 if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == dex_pc) {
Elliott Hughes86964332012-02-15 19:37:42 -0800204 VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
205 return true;
206 }
207 }
208 return false;
209}
210
Elliott Hughes9e0c1752013-01-09 14:02:58 -0800211static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) {
212 MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
213 // A thread may be suspended for GC; in this code, we really want to know whether
214 // there's a debugger suspension active.
215 return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
216}
217
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218static mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800221 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800222 status = JDWP::ERR_INVALID_OBJECT;
223 return NULL;
224 }
225 if (!o->IsArrayInstance()) {
226 status = JDWP::ERR_INVALID_ARRAY;
227 return NULL;
228 }
229 status = JDWP::ERR_NONE;
230 return o->AsArray();
231}
232
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800236 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800237 status = JDWP::ERR_INVALID_OBJECT;
238 return NULL;
239 }
240 if (!o->IsClass()) {
241 status = JDWP::ERR_INVALID_CLASS;
242 return NULL;
243 }
244 status = JDWP::ERR_NONE;
245 return o->AsClass();
246}
247
Elliott Hughes221229c2013-01-08 18:17:50 -0800248static JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread)
jeffhaoa77f0f62012-12-05 17:19:31 -0800249 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
251 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 mirror::Object* thread_peer = gRegistry->Get<mirror::Object*>(thread_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800253 if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) {
Elliott Hughes221229c2013-01-08 18:17:50 -0800254 // This isn't even an object.
255 return JDWP::ERR_INVALID_OBJECT;
Elliott Hughes436e3722012-02-17 20:01:47 -0800256 }
Elliott Hughes221229c2013-01-08 18:17:50 -0800257
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800258 mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
Elliott Hughes221229c2013-01-08 18:17:50 -0800259 if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
260 // This isn't a thread.
261 return JDWP::ERR_INVALID_THREAD;
262 }
263
264 thread = Thread::FromManagedThread(soa, thread_peer);
265 if (thread == NULL) {
266 // This is a java.lang.Thread without a Thread*. Must be a zombie.
267 return JDWP::ERR_THREAD_NOT_ALIVE;
268 }
269 return JDWP::ERR_NONE;
Elliott Hughes436e3722012-02-17 20:01:47 -0800270}
271
Elliott Hughes24437992011-11-30 14:49:33 -0800272static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
273 // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
274 // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
275 return static_cast<JDWP::JdwpTag>(descriptor[0]);
276}
277
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278static JDWP::JdwpTag TagFromClass(mirror::Class* c)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes86b00102011-12-05 17:54:26 -0800280 CHECK(c != NULL);
Elliott Hughes24437992011-11-30 14:49:33 -0800281 if (c->IsArrayClass()) {
282 return JDWP::JT_ARRAY;
283 }
284
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800285 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughes24437992011-11-30 14:49:33 -0800286 if (c->IsStringClass()) {
287 return JDWP::JT_STRING;
288 } else if (c->IsClassClass()) {
289 return JDWP::JT_CLASS_OBJECT;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800290 } else if (class_linker->FindSystemClass("Ljava/lang/Thread;")->IsAssignableFrom(c)) {
Elliott Hughes24437992011-11-30 14:49:33 -0800291 return JDWP::JT_THREAD;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800292 } else if (class_linker->FindSystemClass("Ljava/lang/ThreadGroup;")->IsAssignableFrom(c)) {
Elliott Hughes24437992011-11-30 14:49:33 -0800293 return JDWP::JT_THREAD_GROUP;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800294 } else if (class_linker->FindSystemClass("Ljava/lang/ClassLoader;")->IsAssignableFrom(c)) {
Elliott Hughes24437992011-11-30 14:49:33 -0800295 return JDWP::JT_CLASS_LOADER;
Elliott Hughes24437992011-11-30 14:49:33 -0800296 } else {
297 return JDWP::JT_OBJECT;
298 }
299}
300
301/*
302 * Objects declared to hold Object might actually hold a more specific
303 * type. The debugger may take a special interest in these (e.g. it
304 * wants to display the contents of Strings), so we want to return an
305 * appropriate tag.
306 *
307 * Null objects are tagged JT_OBJECT.
308 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800309static JDWP::JdwpTag TagFromObject(const mirror::Object* o)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700310 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes24437992011-11-30 14:49:33 -0800311 return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(o->GetClass());
312}
313
314static bool IsPrimitiveTag(JDWP::JdwpTag tag) {
315 switch (tag) {
316 case JDWP::JT_BOOLEAN:
317 case JDWP::JT_BYTE:
318 case JDWP::JT_CHAR:
319 case JDWP::JT_FLOAT:
320 case JDWP::JT_DOUBLE:
321 case JDWP::JT_INT:
322 case JDWP::JT_LONG:
323 case JDWP::JT_SHORT:
324 case JDWP::JT_VOID:
325 return true;
326 default:
327 return false;
328 }
329}
330
Elliott Hughes3bb81562011-10-21 18:52:59 -0700331/*
332 * Handle one of the JDWP name/value pairs.
333 *
334 * JDWP options are:
335 * help: if specified, show help message and bail
336 * transport: may be dt_socket or dt_shmem
337 * address: for dt_socket, "host:port", or just "port" when listening
338 * server: if "y", wait for debugger to attach; if "n", attach to debugger
339 * timeout: how long to wait for debugger to connect / listen
340 *
341 * Useful with server=n (these aren't supported yet):
342 * onthrow=<exception-name>: connect to debugger when exception thrown
343 * onuncaught=y|n: connect to debugger when uncaught exception thrown
344 * launch=<command-line>: launch the debugger itself
345 *
346 * The "transport" option is required, as is "address" if server=n.
347 */
348static bool ParseJdwpOption(const std::string& name, const std::string& value) {
349 if (name == "transport") {
350 if (value == "dt_socket") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700351 gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700352 } else if (value == "dt_android_adb") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700353 gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700354 } else {
355 LOG(ERROR) << "JDWP transport not supported: " << value;
356 return false;
357 }
358 } else if (name == "server") {
359 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700360 gJdwpOptions.server = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700361 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700362 gJdwpOptions.server = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700363 } else {
364 LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
365 return false;
366 }
367 } else if (name == "suspend") {
368 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700369 gJdwpOptions.suspend = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700370 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700371 gJdwpOptions.suspend = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700372 } else {
373 LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
374 return false;
375 }
376 } else if (name == "address") {
377 /* this is either <port> or <host>:<port> */
378 std::string port_string;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700379 gJdwpOptions.host.clear();
Elliott Hughes3bb81562011-10-21 18:52:59 -0700380 std::string::size_type colon = value.find(':');
381 if (colon != std::string::npos) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700382 gJdwpOptions.host = value.substr(0, colon);
Elliott Hughes3bb81562011-10-21 18:52:59 -0700383 port_string = value.substr(colon + 1);
384 } else {
385 port_string = value;
386 }
387 if (port_string.empty()) {
388 LOG(ERROR) << "JDWP address missing port: " << value;
389 return false;
390 }
391 char* end;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800392 uint64_t port = strtoul(port_string.c_str(), &end, 10);
393 if (*end != '\0' || port > 0xffff) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700394 LOG(ERROR) << "JDWP address has junk in port field: " << value;
395 return false;
396 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700397 gJdwpOptions.port = port;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700398 } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
399 /* valid but unsupported */
400 LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
401 } else {
402 LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
403 }
404
405 return true;
406}
407
408/*
409 * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
410 * "transport=dt_socket,address=8000,server=y,suspend=n"
411 */
412bool Dbg::ParseJdwpOptions(const std::string& options) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800413 VLOG(jdwp) << "ParseJdwpOptions: " << options;
Elliott Hughes47fce012011-10-25 18:37:19 -0700414
Elliott Hughes3bb81562011-10-21 18:52:59 -0700415 std::vector<std::string> pairs;
416 Split(options, ',', pairs);
417
418 for (size_t i = 0; i < pairs.size(); ++i) {
419 std::string::size_type equals = pairs[i].find('=');
420 if (equals == std::string::npos) {
421 LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
422 return false;
423 }
424 ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
425 }
426
Elliott Hughes376a7a02011-10-24 18:35:55 -0700427 if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700428 LOG(ERROR) << "Must specify JDWP transport: " << options;
429 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700430 if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700431 LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
432 return false;
433 }
434
435 gJdwpConfigured = true;
436 return true;
437}
438
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700439void Dbg::StartJdwp() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700440 if (!gJdwpAllowed || !IsJdwpConfigured()) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700441 // No JDWP for you!
442 return;
443 }
444
Elliott Hughes475fc232011-10-25 15:00:35 -0700445 CHECK(gRegistry == NULL);
446 gRegistry = new ObjectRegistry;
447
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700448 // Init JDWP if the debugger is enabled. This may connect out to a
449 // debugger, passively listen for a debugger, or block waiting for a
450 // debugger.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700451 gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
452 if (gJdwpState == NULL) {
Elliott Hughesf8a2df72011-12-01 12:19:54 -0800453 // We probably failed because some other process has the port already, which means that
454 // if we don't abort the user is likely to think they're talking to us when they're actually
455 // talking to that other process.
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800456 LOG(FATAL) << "Debugger thread failed to initialize";
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700457 }
458
459 // If a debugger has already attached, send the "welcome" message.
460 // This may cause us to suspend all threads.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700461 if (gJdwpState->IsActive()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700462 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes376a7a02011-10-24 18:35:55 -0700463 if (!gJdwpState->PostVMStart()) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800464 LOG(WARNING) << "Failed to post 'start' message to debugger";
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700465 }
466 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700467}
468
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700469void Dbg::StopJdwp() {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700470 delete gJdwpState;
Elliott Hughes475fc232011-10-25 15:00:35 -0700471 delete gRegistry;
472 gRegistry = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700473}
474
Elliott Hughes767a1472011-10-26 18:49:02 -0700475void Dbg::GcDidFinish() {
476 if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700477 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes81ff3182012-03-23 20:35:56 -0700478 LOG(DEBUG) << "Sending heap info to DDM";
Elliott Hughes7162ad92011-10-27 14:08:42 -0700479 DdmSendHeapInfo(gDdmHpifWhen);
Elliott Hughes767a1472011-10-26 18:49:02 -0700480 }
481 if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700482 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes81ff3182012-03-23 20:35:56 -0700483 LOG(DEBUG) << "Dumping heap to DDM";
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700484 DdmSendHeapSegments(false);
Elliott Hughes767a1472011-10-26 18:49:02 -0700485 }
486 if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700487 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes767a1472011-10-26 18:49:02 -0700488 LOG(DEBUG) << "Dumping native heap to DDM";
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700489 DdmSendHeapSegments(true);
Elliott Hughes767a1472011-10-26 18:49:02 -0700490 }
491}
492
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700493void Dbg::SetJdwpAllowed(bool allowed) {
494 gJdwpAllowed = allowed;
495}
496
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700497DebugInvokeReq* Dbg::GetInvokeReq() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700498 return Thread::Current()->GetInvokeReq();
499}
500
501Thread* Dbg::GetDebugThread() {
502 return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
503}
504
505void Dbg::ClearWaitForEventThread() {
506 gJdwpState->ClearWaitForEventThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700507}
508
509void Dbg::Connected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700510 CHECK(!gDebuggerConnected);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800511 VLOG(jdwp) << "JDWP has attached";
Elliott Hughes3bb81562011-10-21 18:52:59 -0700512 gDebuggerConnected = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800513 gDisposed = false;
514}
515
516void Dbg::Disposed() {
517 gDisposed = true;
518}
519
520bool Dbg::IsDisposed() {
521 return gDisposed;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700522}
523
Elliott Hughesa2155262011-11-16 16:26:58 -0800524void Dbg::GoActive() {
525 // Enable all debugging features, including scans for breakpoints.
526 // This is a no-op if we're already active.
527 // Only called from the JDWP handler thread.
528 if (gDebuggerActive) {
529 return;
530 }
531
Elliott Hughesc0f09332012-03-26 13:27:06 -0700532 {
533 // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
jeffhao09bfc6a2012-12-11 18:11:43 -0800534 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Elliott Hughesc0f09332012-03-26 13:27:06 -0700535 CHECK_EQ(gBreakpoints.size(), 0U);
536 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800537
Ian Rogers62d6c772013-02-27 08:32:07 -0800538 Runtime* runtime = Runtime::Current();
539 runtime->GetThreadList()->SuspendAll();
540 Thread* self = Thread::Current();
541 ThreadState old_state = self->SetStateUnsafe(kRunnable);
542 CHECK_NE(old_state, kRunnable);
543 runtime->GetInstrumentation()->AddListener(&gDebugInstrumentationListener,
544 instrumentation::Instrumentation::kMethodEntered |
545 instrumentation::Instrumentation::kMethodExited |
Jeff Hao14dd5a82013-04-11 10:23:36 -0700546 instrumentation::Instrumentation::kDexPcMoved |
547 instrumentation::Instrumentation::kExceptionCaught);
Elliott Hughesa2155262011-11-16 16:26:58 -0800548 gDebuggerActive = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800549 CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
550 runtime->GetThreadList()->ResumeAll();
551
552 LOG(INFO) << "Debugger is active";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700553}
554
555void Dbg::Disconnected() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700556 CHECK(gDebuggerConnected);
557
Elliott Hughesc0f09332012-03-26 13:27:06 -0700558 LOG(INFO) << "Debugger is no longer active";
Elliott Hughes234ab152011-10-26 14:02:26 -0700559
Ian Rogers62d6c772013-02-27 08:32:07 -0800560 // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread
561 // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener
562 // and clear the object registry.
563 Runtime* runtime = Runtime::Current();
564 runtime->GetThreadList()->SuspendAll();
565 Thread* self = Thread::Current();
566 ThreadState old_state = self->SetStateUnsafe(kRunnable);
567 runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener,
568 instrumentation::Instrumentation::kMethodEntered |
569 instrumentation::Instrumentation::kMethodExited |
Jeff Hao14dd5a82013-04-11 10:23:36 -0700570 instrumentation::Instrumentation::kDexPcMoved |
571 instrumentation::Instrumentation::kExceptionCaught);
Elliott Hughesc0f09332012-03-26 13:27:06 -0700572 gDebuggerActive = false;
Elliott Hughes234ab152011-10-26 14:02:26 -0700573 gRegistry->Clear();
574 gDebuggerConnected = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800575 CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
576 runtime->GetThreadList()->ResumeAll();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700577}
578
Elliott Hughesc0f09332012-03-26 13:27:06 -0700579bool Dbg::IsDebuggerActive() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700580 return gDebuggerActive;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700581}
582
Elliott Hughesc0f09332012-03-26 13:27:06 -0700583bool Dbg::IsJdwpConfigured() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700584 return gJdwpConfigured;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700585}
586
587int64_t Dbg::LastDebuggerActivity() {
Elliott Hughesca951522011-12-05 12:01:32 -0800588 return gJdwpState->LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700589}
590
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700591void Dbg::UndoDebuggerSuspensions() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700592 Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700593}
594
Elliott Hughes88d63092013-01-09 09:55:54 -0800595std::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800596 mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800597 if (o == NULL) {
598 return "NULL";
599 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800600 if (o == ObjectRegistry::kInvalidObject) {
Elliott Hughes88d63092013-01-09 09:55:54 -0800601 return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
Elliott Hughes436e3722012-02-17 20:01:47 -0800602 }
603 if (!o->IsClass()) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800604 return StringPrintf("non-class %p", o); // This is only used for debugging output anyway.
605 }
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800606 return DescriptorToName(ClassHelper(o->AsClass()).GetDescriptor());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700607}
608
Elliott Hughes88d63092013-01-09 09:55:54 -0800609JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800610 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611 mirror::Class* c = DecodeClass(id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -0800612 if (c == NULL) {
613 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800614 }
Elliott Hughes88d63092013-01-09 09:55:54 -0800615 class_object_id = gRegistry->Add(c);
Elliott Hughes436e3722012-02-17 20:01:47 -0800616 return JDWP::ERR_NONE;
Elliott Hughes86964332012-02-15 19:37:42 -0800617}
618
Elliott Hughes88d63092013-01-09 09:55:54 -0800619JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800620 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621 mirror::Class* c = DecodeClass(id, status);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800622 if (c == NULL) {
623 return status;
624 }
625 if (c->IsInterface()) {
626 // http://code.google.com/p/android/issues/detail?id=20856
Elliott Hughes88d63092013-01-09 09:55:54 -0800627 superclass_id = 0;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800628 } else {
Elliott Hughes88d63092013-01-09 09:55:54 -0800629 superclass_id = gRegistry->Add(c->GetSuperClass());
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800630 }
631 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700632}
633
Elliott Hughes436e3722012-02-17 20:01:47 -0800634JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800635 mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800636 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800637 return JDWP::ERR_INVALID_OBJECT;
638 }
639 expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader()));
640 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700641}
642
Elliott Hughes436e3722012-02-17 20:01:47 -0800643JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
644 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800645 mirror::Class* c = DecodeClass(id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -0800646 if (c == NULL) {
647 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800648 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800649
650 uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
651
652 // Set ACC_SUPER; dex files don't contain this flag, but all classes are supposed to have it set.
653 // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
654 access_flags |= kAccSuper;
655
656 expandBufAdd4BE(pReply, access_flags);
657
658 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700659}
660
Elliott Hughesf327e072013-01-09 16:01:26 -0800661JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply)
662 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800663 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800664 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughesf327e072013-01-09 16:01:26 -0800665 return JDWP::ERR_INVALID_OBJECT;
666 }
667
668 // Ensure all threads are suspended while we read objects' lock words.
669 Thread* self = Thread::Current();
670 Locks::mutator_lock_->SharedUnlock(self);
671 Locks::mutator_lock_->ExclusiveLock(self);
672
673 MonitorInfo monitor_info(o);
674
675 Locks::mutator_lock_->ExclusiveUnlock(self);
676 Locks::mutator_lock_->SharedLock(self);
677
678 if (monitor_info.owner != NULL) {
679 expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner->GetPeer()));
680 } else {
681 expandBufAddObjectId(reply, gRegistry->Add(NULL));
682 }
683 expandBufAdd4BE(reply, monitor_info.entry_count);
684 expandBufAdd4BE(reply, monitor_info.waiters.size());
685 for (size_t i = 0; i < monitor_info.waiters.size(); ++i) {
686 expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters[i]->GetPeer()));
687 }
688 return JDWP::ERR_NONE;
689}
690
Elliott Hughes734b8c62013-01-11 15:32:45 -0800691JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
692 std::vector<JDWP::ObjectId>& monitors,
693 std::vector<uint32_t>& stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800694 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
695 ScopedObjectAccessUnchecked soa(Thread::Current());
696 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
697 Thread* thread;
698 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
699 if (error != JDWP::ERR_NONE) {
700 return error;
701 }
702 if (!IsSuspendedForDebugger(soa, thread)) {
703 return JDWP::ERR_THREAD_NOT_SUSPENDED;
704 }
705
706 struct OwnedMonitorVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800707 OwnedMonitorVisitor(Thread* thread, Context* context)
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800708 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -0800709 : StackVisitor(thread, context), current_stack_depth(0) {}
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800710
711 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
712 // annotalysis.
713 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
714 if (!GetMethod()->IsRuntimeMethod()) {
715 Monitor::VisitLocks(this, AppendOwnedMonitors, this);
Elliott Hughes734b8c62013-01-11 15:32:45 -0800716 ++current_stack_depth;
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800717 }
718 return true;
719 }
720
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800721 static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800722 OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
Elliott Hughes734b8c62013-01-11 15:32:45 -0800723 visitor->monitors.push_back(owned_monitor);
724 visitor->stack_depths.push_back(visitor->current_stack_depth);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800725 }
726
Elliott Hughes734b8c62013-01-11 15:32:45 -0800727 size_t current_stack_depth;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800728 std::vector<mirror::Object*> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -0800729 std::vector<uint32_t> stack_depths;
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800730 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800731 UniquePtr<Context> context(Context::Create());
732 OwnedMonitorVisitor visitor(thread, context.get());
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800733 visitor.WalkStack();
734
735 for (size_t i = 0; i < visitor.monitors.size(); ++i) {
736 monitors.push_back(gRegistry->Add(visitor.monitors[i]));
Elliott Hughes734b8c62013-01-11 15:32:45 -0800737 stack_depths.push_back(visitor.stack_depths[i]);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800738 }
739
740 return JDWP::ERR_NONE;
741}
742
Elliott Hughesf9501702013-01-11 11:22:27 -0800743JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, JDWP::ObjectId& contended_monitor)
744 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
745 ScopedObjectAccessUnchecked soa(Thread::Current());
746 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
747 Thread* thread;
748 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
749 if (error != JDWP::ERR_NONE) {
750 return error;
751 }
752 if (!IsSuspendedForDebugger(soa, thread)) {
753 return JDWP::ERR_THREAD_NOT_SUSPENDED;
754 }
755
756 contended_monitor = gRegistry->Add(Monitor::GetContendedMonitor(thread));
757
758 return JDWP::ERR_NONE;
759}
760
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800761JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
762 std::vector<uint64_t>& counts)
763 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
764
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800765 std::vector<mirror::Class*> classes;
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800766 counts.clear();
767 for (size_t i = 0; i < class_ids.size(); ++i) {
768 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800769 mirror::Class* c = DecodeClass(class_ids[i], status);
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800770 if (c == NULL) {
771 return status;
772 }
773 classes.push_back(c);
774 counts.push_back(0);
775 }
776
777 Runtime::Current()->GetHeap()->CountInstances(classes, false, &counts[0]);
778 return JDWP::ERR_NONE;
779}
780
Elliott Hughes3b78c942013-01-15 17:35:41 -0800781JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances)
782 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
783 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800784 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800785 if (c == NULL) {
786 return status;
787 }
788
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800789 std::vector<mirror::Object*> raw_instances;
Elliott Hughes3b78c942013-01-15 17:35:41 -0800790 Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances);
791 for (size_t i = 0; i < raw_instances.size(); ++i) {
792 instances.push_back(gRegistry->Add(raw_instances[i]));
793 }
794 return JDWP::ERR_NONE;
795}
796
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800797JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
798 std::vector<JDWP::ObjectId>& referring_objects)
799 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800800 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800801 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800802 return JDWP::ERR_INVALID_OBJECT;
803 }
804
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800805 std::vector<mirror::Object*> raw_instances;
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800806 Runtime::Current()->GetHeap()->GetReferringObjects(o, max_count, raw_instances);
807 for (size_t i = 0; i < raw_instances.size(); ++i) {
808 referring_objects.push_back(gRegistry->Add(raw_instances[i]));
809 }
810 return JDWP::ERR_NONE;
811}
812
Elliott Hughes64f574f2013-02-20 14:57:12 -0800813JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id)
814 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
815 gRegistry->DisableCollection(object_id);
816 return JDWP::ERR_NONE;
817}
818
819JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id)
820 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
821 gRegistry->EnableCollection(object_id);
822 return JDWP::ERR_NONE;
823}
824
825JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected)
826 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
827 is_collected = gRegistry->IsCollected(object_id);
828 return JDWP::ERR_NONE;
829}
830
831void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count)
832 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
833 gRegistry->DisposeObject(object_id, reference_count);
834}
835
Elliott Hughes88d63092013-01-09 09:55:54 -0800836JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800837 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800838 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -0800839 if (c == NULL) {
840 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800841 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800842
843 expandBufAdd1(pReply, c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS);
Elliott Hughes88d63092013-01-09 09:55:54 -0800844 expandBufAddRefTypeId(pReply, class_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800845 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700846}
847
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800848void Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800849 // Get the complete list of reference classes (i.e. all classes except
850 // the primitive types).
851 // Returns a newly-allocated buffer full of RefTypeId values.
852 struct ClassListCreator {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800853 explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800854 }
855
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800856 static bool Visit(mirror::Class* c, void* arg) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800857 return reinterpret_cast<ClassListCreator*>(arg)->Visit(c);
858 }
859
Elliott Hughes64f574f2013-02-20 14:57:12 -0800860 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
861 // annotalysis.
862 bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughesa2155262011-11-16 16:26:58 -0800863 if (!c->IsPrimitive()) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800864 classes.push_back(gRegistry->AddRefType(c));
Elliott Hughesa2155262011-11-16 16:26:58 -0800865 }
866 return true;
867 }
868
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800869 std::vector<JDWP::RefTypeId>& classes;
Elliott Hughesa2155262011-11-16 16:26:58 -0800870 };
871
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800872 ClassListCreator clc(classes);
Elliott Hughesa2155262011-11-16 16:26:58 -0800873 Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700874}
875
Elliott Hughes88d63092013-01-09 09:55:54 -0800876JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800877 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800878 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -0800879 if (c == NULL) {
880 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800881 }
882
Elliott Hughesa2155262011-11-16 16:26:58 -0800883 if (c->IsArrayClass()) {
884 *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
885 *pTypeTag = JDWP::TT_ARRAY;
886 } else {
887 if (c->IsErroneous()) {
888 *pStatus = JDWP::CS_ERROR;
889 } else {
890 *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
891 }
892 *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
893 }
894
895 if (pDescriptor != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800896 *pDescriptor = ClassHelper(c).GetDescriptor();
Elliott Hughesa2155262011-11-16 16:26:58 -0800897 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800898 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700899}
900
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800901void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800902 std::vector<mirror::Class*> classes;
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800903 Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
904 ids.clear();
905 for (size_t i = 0; i < classes.size(); ++i) {
906 ids.push_back(gRegistry->Add(classes[i]));
907 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700908}
909
Elliott Hughes64f574f2013-02-20 14:57:12 -0800910JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply)
911 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800912 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800913 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800914 return JDWP::ERR_INVALID_OBJECT;
Elliott Hughes499c5132011-11-17 14:55:11 -0800915 }
Elliott Hughes2435a572012-02-17 16:07:41 -0800916
917 JDWP::JdwpTypeTag type_tag;
918 if (o->GetClass()->IsArrayClass()) {
919 type_tag = JDWP::TT_ARRAY;
920 } else if (o->GetClass()->IsInterface()) {
921 type_tag = JDWP::TT_INTERFACE;
922 } else {
923 type_tag = JDWP::TT_CLASS;
924 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800925 JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
Elliott Hughes2435a572012-02-17 16:07:41 -0800926
927 expandBufAdd1(pReply, type_tag);
928 expandBufAddRefTypeId(pReply, type_id);
929
930 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700931}
932
Elliott Hughes88d63092013-01-09 09:55:54 -0800933JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string& signature) {
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800934 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800935 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800936 if (c == NULL) {
937 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800938 }
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800939 signature = ClassHelper(c).GetDescriptor();
940 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700941}
942
Elliott Hughes88d63092013-01-09 09:55:54 -0800943JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800944 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800945 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -0800946 if (c == NULL) {
947 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800948 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800949 result = ClassHelper(c).GetSourceFile();
950 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700951}
952
Elliott Hughes88d63092013-01-09 09:55:54 -0800953JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800954 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800955 if (o == ObjectRegistry::kInvalidObject) {
Elliott Hughes546b9862012-06-20 16:06:13 -0700956 return JDWP::ERR_INVALID_OBJECT;
957 }
958 tag = TagFromObject(o);
959 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700960}
961
Elliott Hughesaed4be92011-12-02 16:16:23 -0800962size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
Elliott Hughesdbb40792011-11-18 17:05:22 -0800963 switch (tag) {
964 case JDWP::JT_VOID:
965 return 0;
966 case JDWP::JT_BYTE:
967 case JDWP::JT_BOOLEAN:
968 return 1;
969 case JDWP::JT_CHAR:
970 case JDWP::JT_SHORT:
971 return 2;
972 case JDWP::JT_FLOAT:
973 case JDWP::JT_INT:
974 return 4;
975 case JDWP::JT_ARRAY:
976 case JDWP::JT_OBJECT:
977 case JDWP::JT_STRING:
978 case JDWP::JT_THREAD:
979 case JDWP::JT_THREAD_GROUP:
980 case JDWP::JT_CLASS_LOADER:
981 case JDWP::JT_CLASS_OBJECT:
982 return sizeof(JDWP::ObjectId);
983 case JDWP::JT_DOUBLE:
984 case JDWP::JT_LONG:
985 return 8;
986 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800987 LOG(FATAL) << "Unknown tag " << tag;
Elliott Hughesdbb40792011-11-18 17:05:22 -0800988 return -1;
989 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700990}
991
Elliott Hughes88d63092013-01-09 09:55:54 -0800992JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800993 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800994 mirror::Array* a = DecodeArray(array_id, status);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800995 if (a == NULL) {
996 return status;
Elliott Hughes24437992011-11-30 14:49:33 -0800997 }
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800998 length = a->GetLength();
999 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001000}
1001
Elliott Hughes88d63092013-01-09 09:55:54 -08001002JDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001003 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001004 mirror::Array* a = DecodeArray(array_id, status);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001005 if (a == NULL) {
1006 return status;
1007 }
Elliott Hughes24437992011-11-30 14:49:33 -08001008
1009 if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
1010 LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001011 return JDWP::ERR_INVALID_LENGTH;
Elliott Hughes24437992011-11-30 14:49:33 -08001012 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001013 std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor());
Elliott Hughes24437992011-11-30 14:49:33 -08001014 JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
1015
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001016 expandBufAdd1(pReply, tag);
1017 expandBufAdd4BE(pReply, count);
1018
Elliott Hughes24437992011-11-30 14:49:33 -08001019 if (IsPrimitiveTag(tag)) {
1020 size_t width = GetTagWidth(tag);
Elliott Hughes24437992011-11-30 14:49:33 -08001021 uint8_t* dst = expandBufAddSpace(pReply, count * width);
1022 if (width == 8) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001023 const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t)));
Elliott Hughes24437992011-11-30 14:49:33 -08001024 for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
1025 } else if (width == 4) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001026 const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t)));
Elliott Hughes24437992011-11-30 14:49:33 -08001027 for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
1028 } else if (width == 2) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001029 const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t)));
Elliott Hughes24437992011-11-30 14:49:33 -08001030 for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
1031 } else {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001032 const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t)));
Elliott Hughes24437992011-11-30 14:49:33 -08001033 memcpy(dst, &src[offset * width], count * width);
1034 }
1035 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001036 mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>();
Elliott Hughes24437992011-11-30 14:49:33 -08001037 for (int i = 0; i < count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001038 mirror::Object* element = oa->Get(offset + i);
Elliott Hughes24437992011-11-30 14:49:33 -08001039 JDWP::JdwpTag specific_tag = (element != NULL) ? TagFromObject(element) : tag;
1040 expandBufAdd1(pReply, specific_tag);
1041 expandBufAddObjectId(pReply, gRegistry->Add(element));
1042 }
1043 }
1044
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001045 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001046}
1047
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001048template <typename T> void CopyArrayData(mirror::Array* a, JDWP::Request& src, int offset, int count) {
1049 DCHECK(a->GetClass()->IsPrimitiveArray());
1050
1051 T* dst = &(reinterpret_cast<T*>(a->GetRawData(sizeof(T)))[offset * sizeof(T)]);
1052 for (int i = 0; i < count; ++i) {
1053 *dst++ = src.ReadValue(sizeof(T));
1054 }
1055}
1056
Elliott Hughes88d63092013-01-09 09:55:54 -08001057JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001058 JDWP::Request& request)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001059 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001060 JDWP::JdwpError status;
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001061 mirror::Array* dst = DecodeArray(array_id, status);
1062 if (dst == NULL) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001063 return status;
1064 }
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001065
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001066 if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001067 LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001068 return JDWP::ERR_INVALID_LENGTH;
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001069 }
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001070 std::string descriptor(ClassHelper(dst->GetClass()).GetDescriptor());
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001071 JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
1072
1073 if (IsPrimitiveTag(tag)) {
1074 size_t width = GetTagWidth(tag);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001075 if (width == 8) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001076 CopyArrayData<uint64_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001077 } else if (width == 4) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001078 CopyArrayData<uint32_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001079 } else if (width == 2) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001080 CopyArrayData<uint16_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001081 } else {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001082 CopyArrayData<uint8_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001083 }
1084 } else {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001085 mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>();
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001086 for (int i = 0; i < count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001087 JDWP::ObjectId id = request.ReadObjectId();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001088 mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08001089 if (o == ObjectRegistry::kInvalidObject) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001090 return JDWP::ERR_INVALID_OBJECT;
1091 }
1092 oa->Set(offset + i, o);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001093 }
1094 }
1095
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001096 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001097}
1098
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001099JDWP::ObjectId Dbg::CreateString(const std::string& str) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001100 return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001101}
1102
Elliott Hughes88d63092013-01-09 09:55:54 -08001103JDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001104 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001105 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -08001106 if (c == NULL) {
1107 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001108 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001109 new_object = gRegistry->Add(c->AllocObject(Thread::Current()));
Elliott Hughes436e3722012-02-17 20:01:47 -08001110 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001111}
1112
Elliott Hughesbf13d362011-12-08 15:51:37 -08001113/*
1114 * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1115 */
Elliott Hughes88d63092013-01-09 09:55:54 -08001116JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001117 JDWP::ObjectId& new_array) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001118 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001119 mirror::Class* c = DecodeClass(array_class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -08001120 if (c == NULL) {
1121 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001122 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001123 new_array = gRegistry->Add(mirror::Array::Alloc(Thread::Current(), c, length));
Elliott Hughes436e3722012-02-17 20:01:47 -08001124 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001125}
1126
Elliott Hughes88d63092013-01-09 09:55:54 -08001127bool Dbg::MatchType(JDWP::RefTypeId instance_class_id, JDWP::RefTypeId class_id) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001128 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001129 mirror::Class* c1 = DecodeClass(instance_class_id, status);
Elliott Hughesa656a0f2012-02-21 18:03:44 -08001130 CHECK(c1 != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001131 mirror::Class* c2 = DecodeClass(class_id, status);
Elliott Hughesa656a0f2012-02-21 18:03:44 -08001132 CHECK(c2 != NULL);
1133 return c1->IsAssignableFrom(c2);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001134}
1135
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001136static JDWP::FieldId ToFieldId(const mirror::Field* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001138#ifdef MOVING_GARBAGE_COLLECTOR
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001139 UNIMPLEMENTED(FATAL);
Elliott Hughes03181a82011-11-17 17:22:21 -08001140#else
1141 return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
1142#endif
1143}
1144
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001145static JDWP::MethodId ToMethodId(const mirror::AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001147#ifdef MOVING_GARBAGE_COLLECTOR
1148 UNIMPLEMENTED(FATAL);
1149#else
1150 return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m));
1151#endif
1152}
1153
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001154static mirror::Field* FromFieldId(JDWP::FieldId fid)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesaed4be92011-12-02 16:16:23 -08001156#ifdef MOVING_GARBAGE_COLLECTOR
1157 UNIMPLEMENTED(FATAL);
1158#else
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001159 return reinterpret_cast<mirror::Field*>(static_cast<uintptr_t>(fid));
Elliott Hughesaed4be92011-12-02 16:16:23 -08001160#endif
1161}
1162
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001163static mirror::AbstractMethod* FromMethodId(JDWP::MethodId mid)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001165#ifdef MOVING_GARBAGE_COLLECTOR
1166 UNIMPLEMENTED(FATAL);
1167#else
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001168 return reinterpret_cast<mirror::AbstractMethod*>(static_cast<uintptr_t>(mid));
Elliott Hughes03181a82011-11-17 17:22:21 -08001169#endif
1170}
1171
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001172static void SetLocation(JDWP::JdwpLocation& location, mirror::AbstractMethod* m, uint32_t dex_pc)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001174 if (m == NULL) {
1175 memset(&location, 0, sizeof(location));
1176 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001177 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes74847412012-06-20 18:10:21 -07001178 location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1179 location.class_id = gRegistry->Add(c);
1180 location.method_id = ToMethodId(m);
Ian Rogers0399dde2012-06-06 17:09:28 -07001181 location.dex_pc = dex_pc;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001182 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08001183}
1184
Elliott Hughesa96836a2013-01-17 12:27:49 -08001185std::string Dbg::GetMethodName(JDWP::MethodId method_id)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001187 mirror::AbstractMethod* m = FromMethodId(method_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001188 return MethodHelper(m).GetName();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001189}
1190
Elliott Hughesa96836a2013-01-17 12:27:49 -08001191std::string Dbg::GetFieldName(JDWP::FieldId field_id)
1192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001193 mirror::Field* f = FromFieldId(field_id);
Elliott Hughesa96836a2013-01-17 12:27:49 -08001194 return FieldHelper(f).GetName();
1195}
1196
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001197/*
1198 * Augment the access flags for synthetic methods and fields by setting
1199 * the (as described by the spec) "0xf0000000 bit". Also, strip out any
1200 * flags not specified by the Java programming language.
1201 */
1202static uint32_t MangleAccessFlags(uint32_t accessFlags) {
1203 accessFlags &= kAccJavaFlagsMask;
1204 if ((accessFlags & kAccSynthetic) != 0) {
1205 accessFlags |= 0xf0000000;
1206 }
1207 return accessFlags;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001208}
1209
Elliott Hughesdbb40792011-11-18 17:05:22 -08001210static const uint16_t kEclipseWorkaroundSlot = 1000;
1211
1212/*
1213 * Eclipse appears to expect that the "this" reference is in slot zero.
1214 * If it's not, the "variables" display will show two copies of "this",
1215 * possibly because it gets "this" from SF.ThisObject and then displays
1216 * all locals with nonzero slot numbers.
1217 *
1218 * So, we remap the item in slot 0 to 1000, and remap "this" to zero. On
1219 * SF.GetValues / SF.SetValues we map them back.
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001220 *
1221 * TODO: jdb uses the value to determine whether a variable is a local or an argument,
1222 * by checking whether it's less than the number of arguments. To make that work, we'd
1223 * have to "mangle" all the arguments to come first, not just the implicit argument 'this'.
Elliott Hughesdbb40792011-11-18 17:05:22 -08001224 */
1225static uint16_t MangleSlot(uint16_t slot, const char* name) {
1226 uint16_t newSlot = slot;
1227 if (strcmp(name, "this") == 0) {
1228 newSlot = 0;
1229 } else if (slot == 0) {
1230 newSlot = kEclipseWorkaroundSlot;
1231 }
1232 return newSlot;
1233}
1234
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001235static uint16_t DemangleSlot(uint16_t slot, mirror::AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001237 if (slot == kEclipseWorkaroundSlot) {
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001238 return 0;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001239 } else if (slot == 0) {
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001240 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
Elliott Hughescaf76542012-06-28 16:08:22 -07001241 CHECK(code_item != NULL) << PrettyMethod(m);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001242 return code_item->registers_size_ - code_item->ins_size_;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001243 }
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001244 return slot;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001245}
1246
Elliott Hughes88d63092013-01-09 09:55:54 -08001247JDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001248 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001249 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -08001250 if (c == NULL) {
1251 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001252 }
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001253
1254 size_t instance_field_count = c->NumInstanceFields();
1255 size_t static_field_count = c->NumStaticFields();
1256
1257 expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1258
1259 for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001260 mirror::Field* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001261 FieldHelper fh(f);
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001262 expandBufAddFieldId(pReply, ToFieldId(f));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001263 expandBufAddUtf8String(pReply, fh.GetName());
1264 expandBufAddUtf8String(pReply, fh.GetTypeDescriptor());
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001265 if (with_generic) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001266 static const char genericSignature[1] = "";
1267 expandBufAddUtf8String(pReply, genericSignature);
1268 }
1269 expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1270 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001271 return JDWP::ERR_NONE;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001272}
1273
Elliott Hughes88d63092013-01-09 09:55:54 -08001274JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001275 JDWP::ExpandBuf* pReply) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001276 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001277 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -08001278 if (c == NULL) {
1279 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001280 }
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001281
1282 size_t direct_method_count = c->NumDirectMethods();
1283 size_t virtual_method_count = c->NumVirtualMethods();
1284
1285 expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
1286
1287 for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001288 mirror::AbstractMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001289 MethodHelper mh(m);
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001290 expandBufAddMethodId(pReply, ToMethodId(m));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001291 expandBufAddUtf8String(pReply, mh.GetName());
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001292 expandBufAddUtf8String(pReply, mh.GetSignature());
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001293 if (with_generic) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001294 static const char genericSignature[1] = "";
1295 expandBufAddUtf8String(pReply, genericSignature);
1296 }
1297 expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags()));
1298 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001299 return JDWP::ERR_NONE;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001300}
1301
Elliott Hughes88d63092013-01-09 09:55:54 -08001302JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001303 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001304 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -08001305 if (c == NULL) {
1306 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001307 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001308
1309 ClassHelper kh(c);
Ian Rogersd24e2642012-06-06 21:21:43 -07001310 size_t interface_count = kh.NumDirectInterfaces();
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001311 expandBufAdd4BE(pReply, interface_count);
1312 for (size_t i = 0; i < interface_count; ++i) {
Elliott Hughes64f574f2013-02-20 14:57:12 -08001313 expandBufAddRefTypeId(pReply, gRegistry->AddRefType(kh.GetDirectInterface(i)));
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001314 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001315 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001316}
1317
Elliott Hughes88d63092013-01-09 09:55:54 -08001318void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001320 struct DebugCallbackContext {
1321 int numItems;
1322 JDWP::ExpandBuf* pReply;
1323
Elliott Hughes2435a572012-02-17 16:07:41 -08001324 static bool Callback(void* context, uint32_t address, uint32_t line_number) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001325 DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1326 expandBufAdd8BE(pContext->pReply, address);
Elliott Hughes2435a572012-02-17 16:07:41 -08001327 expandBufAdd4BE(pContext->pReply, line_number);
Elliott Hughes03181a82011-11-17 17:22:21 -08001328 pContext->numItems++;
1329 return true;
1330 }
1331 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001332 mirror::AbstractMethod* m = FromMethodId(method_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001333 MethodHelper mh(m);
Elliott Hughes03181a82011-11-17 17:22:21 -08001334 uint64_t start, end;
1335 if (m->IsNative()) {
1336 start = -1;
1337 end = -1;
1338 } else {
1339 start = 0;
jeffhao14f0db92012-12-14 17:50:42 -08001340 // Return the index of the last instruction
1341 end = mh.GetCodeItem()->insns_size_in_code_units_ - 1;
Elliott Hughes03181a82011-11-17 17:22:21 -08001342 }
1343
1344 expandBufAdd8BE(pReply, start);
1345 expandBufAdd8BE(pReply, end);
1346
1347 // Add numLines later
1348 size_t numLinesOffset = expandBufGetLength(pReply);
1349 expandBufAdd4BE(pReply, 0);
1350
1351 DebugCallbackContext context;
1352 context.numItems = 0;
1353 context.pReply = pReply;
1354
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001355 mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(),
1356 DebugCallbackContext::Callback, NULL, &context);
Elliott Hughes03181a82011-11-17 17:22:21 -08001357
1358 JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001359}
1360
Elliott Hughes88d63092013-01-09 09:55:54 -08001361void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, JDWP::ExpandBuf* pReply) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001362 struct DebugCallbackContext {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001363 JDWP::ExpandBuf* pReply;
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001364 size_t variable_count;
1365 bool with_generic;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001366
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001367 static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, const char* name, const char* descriptor, const char* signature) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001368 DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1369
Elliott Hughesad3da692012-02-24 16:51:35 -08001370 VLOG(jdwp) << StringPrintf(" %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d", pContext->variable_count, startAddress, endAddress - startAddress, name, descriptor, signature, slot, MangleSlot(slot, name));
Elliott Hughesdbb40792011-11-18 17:05:22 -08001371
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001372 slot = MangleSlot(slot, name);
1373
Elliott Hughesdbb40792011-11-18 17:05:22 -08001374 expandBufAdd8BE(pContext->pReply, startAddress);
1375 expandBufAddUtf8String(pContext->pReply, name);
1376 expandBufAddUtf8String(pContext->pReply, descriptor);
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001377 if (pContext->with_generic) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001378 expandBufAddUtf8String(pContext->pReply, signature);
1379 }
1380 expandBufAdd4BE(pContext->pReply, endAddress - startAddress);
1381 expandBufAdd4BE(pContext->pReply, slot);
1382
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001383 ++pContext->variable_count;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001384 }
1385 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001386 mirror::AbstractMethod* m = FromMethodId(method_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001387 MethodHelper mh(m);
1388 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Elliott Hughesdbb40792011-11-18 17:05:22 -08001389
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001390 // arg_count considers doubles and longs to take 2 units.
1391 // variable_count considers everything to take 1 unit.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001392 std::string shorty(mh.GetShorty());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001393 expandBufAdd4BE(pReply, mirror::AbstractMethod::NumArgRegisters(shorty));
Elliott Hughesdbb40792011-11-18 17:05:22 -08001394
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001395 // We don't know the total number of variables yet, so leave a blank and update it later.
1396 size_t variable_count_offset = expandBufGetLength(pReply);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001397 expandBufAdd4BE(pReply, 0);
1398
1399 DebugCallbackContext context;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001400 context.pReply = pReply;
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001401 context.variable_count = 0;
1402 context.with_generic = with_generic;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001403
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001404 mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL,
1405 DebugCallbackContext::Callback, &context);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001406
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001407 JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001408}
1409
Elliott Hughes9777ba22013-01-17 09:04:19 -08001410JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
1411 std::vector<uint8_t>& bytecodes)
1412 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001413 mirror::AbstractMethod* m = FromMethodId(method_id);
Elliott Hughes9777ba22013-01-17 09:04:19 -08001414 if (m == NULL) {
1415 return JDWP::ERR_INVALID_METHODID;
1416 }
1417 MethodHelper mh(m);
1418 const DexFile::CodeItem* code_item = mh.GetCodeItem();
1419 size_t byte_count = code_item->insns_size_in_code_units_ * 2;
1420 const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_);
1421 const uint8_t* end = begin + byte_count;
1422 for (const uint8_t* p = begin; p != end; ++p) {
1423 bytecodes.push_back(*p);
1424 }
1425 return JDWP::ERR_NONE;
1426}
1427
Elliott Hughes88d63092013-01-09 09:55:54 -08001428JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
1429 return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001430}
1431
Elliott Hughes88d63092013-01-09 09:55:54 -08001432JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
1433 return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001434}
1435
Elliott Hughes88d63092013-01-09 09:55:54 -08001436static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
1437 JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001438 bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001439 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes0cf74332012-02-23 23:14:00 -08001440 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001441 mirror::Class* c = DecodeClass(ref_type_id, status);
Elliott Hughes88d63092013-01-09 09:55:54 -08001442 if (ref_type_id != 0 && c == NULL) {
Elliott Hughes0cf74332012-02-23 23:14:00 -08001443 return status;
1444 }
1445
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001446 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08001447 if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001448 return JDWP::ERR_INVALID_OBJECT;
1449 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001450 mirror::Field* f = FromFieldId(field_id);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001451
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001452 mirror::Class* receiver_class = c;
Elliott Hughes0cf74332012-02-23 23:14:00 -08001453 if (receiver_class == NULL && o != NULL) {
1454 receiver_class = o->GetClass();
1455 }
1456 // TODO: should we give up now if receiver_class is NULL?
1457 if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
1458 LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001459 return JDWP::ERR_INVALID_FIELDID;
1460 }
Elliott Hughesaed4be92011-12-02 16:16:23 -08001461
Elliott Hughes0cf74332012-02-23 23:14:00 -08001462 // The RI only enforces the static/non-static mismatch in one direction.
1463 // TODO: should we change the tests and check both?
1464 if (is_static) {
1465 if (!f->IsStatic()) {
1466 return JDWP::ERR_INVALID_FIELDID;
1467 }
1468 } else {
1469 if (f->IsStatic()) {
1470 LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001471 }
1472 }
jeffhao0dfbb7e2012-11-28 15:26:03 -08001473 if (f->IsStatic()) {
1474 o = f->GetDeclaringClass();
1475 }
Elliott Hughes0cf74332012-02-23 23:14:00 -08001476
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001477 JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
Elliott Hughesaed4be92011-12-02 16:16:23 -08001478
1479 if (IsPrimitiveTag(tag)) {
1480 expandBufAdd1(pReply, tag);
1481 if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
1482 expandBufAdd1(pReply, f->Get32(o));
1483 } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
1484 expandBufAdd2BE(pReply, f->Get32(o));
1485 } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
1486 expandBufAdd4BE(pReply, f->Get32(o));
1487 } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1488 expandBufAdd8BE(pReply, f->Get64(o));
1489 } else {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08001490 LOG(FATAL) << "Unknown tag: " << tag;
Elliott Hughesaed4be92011-12-02 16:16:23 -08001491 }
1492 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001493 mirror::Object* value = f->GetObject(o);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001494 expandBufAdd1(pReply, TagFromObject(value));
1495 expandBufAddObjectId(pReply, gRegistry->Add(value));
1496 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001497 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001498}
1499
Elliott Hughes88d63092013-01-09 09:55:54 -08001500JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001501 JDWP::ExpandBuf* pReply) {
Elliott Hughes88d63092013-01-09 09:55:54 -08001502 return GetFieldValueImpl(0, object_id, field_id, pReply, false);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001503}
1504
Elliott Hughes88d63092013-01-09 09:55:54 -08001505JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) {
1506 return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001507}
1508
Elliott Hughes88d63092013-01-09 09:55:54 -08001509static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001510 uint64_t value, int width, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001511 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001512 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08001513 if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001514 return JDWP::ERR_INVALID_OBJECT;
1515 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001516 mirror::Field* f = FromFieldId(field_id);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001517
1518 // The RI only enforces the static/non-static mismatch in one direction.
1519 // TODO: should we change the tests and check both?
1520 if (is_static) {
1521 if (!f->IsStatic()) {
1522 return JDWP::ERR_INVALID_FIELDID;
1523 }
1524 } else {
1525 if (f->IsStatic()) {
1526 LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001527 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001528 }
jeffhao0dfbb7e2012-11-28 15:26:03 -08001529 if (f->IsStatic()) {
1530 o = f->GetDeclaringClass();
1531 }
Elliott Hughesaed4be92011-12-02 16:16:23 -08001532
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001533 JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
Elliott Hughesaed4be92011-12-02 16:16:23 -08001534
1535 if (IsPrimitiveTag(tag)) {
1536 if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001537 CHECK_EQ(width, 8);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001538 f->Set64(o, value);
1539 } else {
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 CHECK_LE(width, 4);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001541 f->Set32(o, value);
1542 }
1543 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001544 mirror::Object* v = gRegistry->Get<mirror::Object*>(value);
Elliott Hughes64f574f2013-02-20 14:57:12 -08001545 if (v == ObjectRegistry::kInvalidObject) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001546 return JDWP::ERR_INVALID_OBJECT;
1547 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001548 if (v != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001549 mirror::Class* field_type = FieldHelper(f).GetType();
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001550 if (!field_type->IsAssignableFrom(v->GetClass())) {
1551 return JDWP::ERR_INVALID_OBJECT;
1552 }
1553 }
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001554 f->SetObject(o, v);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001555 }
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001556
1557 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001558}
1559
Elliott Hughes88d63092013-01-09 09:55:54 -08001560JDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001561 int width) {
Elliott Hughes88d63092013-01-09 09:55:54 -08001562 return SetFieldValueImpl(object_id, field_id, value, width, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001563}
1564
Elliott Hughes88d63092013-01-09 09:55:54 -08001565JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
1566 return SetFieldValueImpl(0, field_id, value, width, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001567}
1568
Elliott Hughes88d63092013-01-09 09:55:54 -08001569std::string Dbg::StringToUtf8(JDWP::ObjectId string_id) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001570 mirror::String* s = gRegistry->Get<mirror::String*>(string_id);
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001571 return s->ToModifiedUtf8();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001572}
1573
Elliott Hughes221229c2013-01-08 18:17:50 -08001574JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) {
jeffhaoa77f0f62012-12-05 17:19:31 -08001575 ScopedObjectAccessUnchecked soa(Thread::Current());
1576 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001577 Thread* thread;
1578 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1579 if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
1580 return error;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001581 }
Elliott Hughes221229c2013-01-08 18:17:50 -08001582
1583 // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001584 mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
1585 mirror::Field* java_lang_Thread_name_field =
1586 soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
1587 mirror::String* s =
1588 reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object));
Elliott Hughes221229c2013-01-08 18:17:50 -08001589 if (s != NULL) {
1590 name = s->ToModifiedUtf8();
1591 }
1592 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001593}
1594
Elliott Hughes221229c2013-01-08 18:17:50 -08001595JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001596 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001597 mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08001598 if (thread_object == ObjectRegistry::kInvalidObject) {
Elliott Hughes2435a572012-02-17 16:07:41 -08001599 return JDWP::ERR_INVALID_OBJECT;
1600 }
1601
1602 // Okay, so it's an object, but is it actually a thread?
Ian Rogers50b35e22012-10-04 10:09:15 -07001603 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001604 Thread* thread;
1605 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1606 if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1607 // Zombie threads are in the null group.
1608 expandBufAddObjectId(pReply, JDWP::ObjectId(0));
1609 return JDWP::ERR_NONE;
1610 }
1611 if (error != JDWP::ERR_NONE) {
1612 return error;
Elliott Hughes2435a572012-02-17 16:07:41 -08001613 }
Elliott Hughes499c5132011-11-17 14:55:11 -08001614
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001615 mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Thread;");
Elliott Hughes499c5132011-11-17 14:55:11 -08001616 CHECK(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001617 mirror::Field* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;");
Elliott Hughes499c5132011-11-17 14:55:11 -08001618 CHECK(f != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001619 mirror::Object* group = f->GetObject(thread_object);
Elliott Hughes499c5132011-11-17 14:55:11 -08001620 CHECK(group != NULL);
Elliott Hughes2435a572012-02-17 16:07:41 -08001621 JDWP::ObjectId thread_group_id = gRegistry->Add(group);
1622
1623 expandBufAddObjectId(pReply, thread_group_id);
1624 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001625}
1626
Elliott Hughes88d63092013-01-09 09:55:54 -08001627std::string Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001628 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001629 mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
Elliott Hughes499c5132011-11-17 14:55:11 -08001630 CHECK(thread_group != NULL);
1631
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001632 mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;");
Elliott Hughes499c5132011-11-17 14:55:11 -08001633 CHECK(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001634 mirror::Field* f = c->FindInstanceField("name", "Ljava/lang/String;");
Elliott Hughes499c5132011-11-17 14:55:11 -08001635 CHECK(f != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001636 mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group));
Elliott Hughes499c5132011-11-17 14:55:11 -08001637 return s->ToModifiedUtf8();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001638}
1639
Elliott Hughes88d63092013-01-09 09:55:54 -08001640JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001641 mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
Elliott Hughes4e235312011-12-02 11:34:15 -08001642 CHECK(thread_group != NULL);
1643
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001644 mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;");
Elliott Hughes4e235312011-12-02 11:34:15 -08001645 CHECK(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001646 mirror::Field* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;");
Elliott Hughes4e235312011-12-02 11:34:15 -08001647 CHECK(f != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001648 mirror::Object* parent = f->GetObject(thread_group);
Elliott Hughes4e235312011-12-02 11:34:15 -08001649 return gRegistry->Add(parent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001650}
1651
1652JDWP::ObjectId Dbg::GetSystemThreadGroupId() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001653 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001654 mirror::Field* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
1655 mirror::Object* group = f->GetObject(f->GetDeclaringClass());
Ian Rogers365c1022012-06-22 15:05:28 -07001656 return gRegistry->Add(group);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001657}
1658
1659JDWP::ObjectId Dbg::GetMainThreadGroupId() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001660 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001661 mirror::Field* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup);
1662 mirror::Object* group = f->GetObject(f->GetDeclaringClass());
Ian Rogers365c1022012-06-22 15:05:28 -07001663 return gRegistry->Add(group);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001664}
1665
Elliott Hughes221229c2013-01-08 18:17:50 -08001666JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, JDWP::JdwpSuspendStatus* pSuspendStatus) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001667 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes499c5132011-11-17 14:55:11 -08001668
Elliott Hughes9e0c1752013-01-09 14:02:58 -08001669 *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
1670
Ian Rogers50b35e22012-10-04 10:09:15 -07001671 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001672 Thread* thread;
1673 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1674 if (error != JDWP::ERR_NONE) {
1675 if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1676 *pThreadStatus = JDWP::TS_ZOMBIE;
Elliott Hughes221229c2013-01-08 18:17:50 -08001677 return JDWP::ERR_NONE;
1678 }
1679 return error;
Elliott Hughes499c5132011-11-17 14:55:11 -08001680 }
1681
Elliott Hughes9e0c1752013-01-09 14:02:58 -08001682 if (IsSuspendedForDebugger(soa, thread)) {
1683 *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
Elliott Hughes499c5132011-11-17 14:55:11 -08001684 }
1685
Elliott Hughes9e0c1752013-01-09 14:02:58 -08001686 switch (thread->GetState()) {
1687 case kBlocked: *pThreadStatus = JDWP::TS_MONITOR; break;
1688 case kNative: *pThreadStatus = JDWP::TS_RUNNING; break;
1689 case kRunnable: *pThreadStatus = JDWP::TS_RUNNING; break;
1690 case kSleeping: *pThreadStatus = JDWP::TS_SLEEPING; break;
1691 case kStarting: *pThreadStatus = JDWP::TS_ZOMBIE; break;
1692 case kSuspended: *pThreadStatus = JDWP::TS_RUNNING; break;
1693 case kTerminated: *pThreadStatus = JDWP::TS_ZOMBIE; break;
1694 case kTimedWaiting: *pThreadStatus = JDWP::TS_WAIT; break;
1695 case kWaitingForDebuggerSend: *pThreadStatus = JDWP::TS_WAIT; break;
1696 case kWaitingForDebuggerSuspension: *pThreadStatus = JDWP::TS_WAIT; break;
1697 case kWaitingForDebuggerToAttach: *pThreadStatus = JDWP::TS_WAIT; break;
1698 case kWaitingForGcToComplete: *pThreadStatus = JDWP::TS_WAIT; break;
Ian Rogers1d54e732013-05-02 21:10:01 -07001699 case kWaitingForCheckPointsToRun: *pThreadStatus = JDWP::TS_WAIT; break;
Elliott Hughes9e0c1752013-01-09 14:02:58 -08001700 case kWaitingForJniOnLoad: *pThreadStatus = JDWP::TS_WAIT; break;
1701 case kWaitingForSignalCatcherOutput: *pThreadStatus = JDWP::TS_WAIT; break;
1702 case kWaitingInMainDebuggerLoop: *pThreadStatus = JDWP::TS_WAIT; break;
1703 case kWaitingInMainSignalCatcherLoop: *pThreadStatus = JDWP::TS_WAIT; break;
1704 case kWaitingPerformingGc: *pThreadStatus = JDWP::TS_WAIT; break;
1705 case kWaiting: *pThreadStatus = JDWP::TS_WAIT; break;
1706 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
1707 }
Elliott Hughes221229c2013-01-08 18:17:50 -08001708 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001709}
1710
Elliott Hughes221229c2013-01-08 18:17:50 -08001711JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001712 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -07001713 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001714 Thread* thread;
1715 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1716 if (error != JDWP::ERR_NONE) {
1717 return error;
Elliott Hughes2435a572012-02-17 16:07:41 -08001718 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001719 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001720 expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
Elliott Hughes2435a572012-02-17 16:07:41 -08001721 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001722}
1723
Elliott Hughesf9501702013-01-11 11:22:27 -08001724JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
1725 ScopedObjectAccess soa(Thread::Current());
1726 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1727 Thread* thread;
1728 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1729 if (error != JDWP::ERR_NONE) {
1730 return error;
1731 }
1732 thread->Interrupt();
1733 return JDWP::ERR_NONE;
1734}
1735
Elliott Hughescaf76542012-06-28 16:08:22 -07001736void Dbg::GetThreads(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& thread_ids) {
Ian Rogers365c1022012-06-22 15:05:28 -07001737 class ThreadListVisitor {
1738 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001739 ThreadListVisitor(const ScopedObjectAccessUnchecked& soa, mirror::Object* desired_thread_group,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001740 std::vector<JDWP::ObjectId>& thread_ids)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001741 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
jeffhao0dfbb7e2012-11-28 15:26:03 -08001742 : soa_(soa), desired_thread_group_(desired_thread_group), thread_ids_(thread_ids) {}
Ian Rogers365c1022012-06-22 15:05:28 -07001743
Elliott Hughesa2155262011-11-16 16:26:58 -08001744 static void Visit(Thread* t, void* arg) {
1745 reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t);
1746 }
1747
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001748 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
1749 // annotalysis.
1750 void Visit(Thread* t) NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughesa2155262011-11-16 16:26:58 -08001751 if (t == Dbg::GetDebugThread()) {
1752 // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
1753 // query all threads, so it's easier if we just don't tell them about this thread.
1754 return;
1755 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001756 mirror::Object* peer = t->GetPeer();
jeffhao0dfbb7e2012-11-28 15:26:03 -08001757 if (IsInDesiredThreadGroup(peer)) {
Ian Rogers120f1c72012-09-28 17:17:10 -07001758 thread_ids_.push_back(gRegistry->Add(peer));
Elliott Hughesa2155262011-11-16 16:26:58 -08001759 }
1760 }
1761
Ian Rogers365c1022012-06-22 15:05:28 -07001762 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001763 bool IsInDesiredThreadGroup(mirror::Object* peer)
jeffhao0dfbb7e2012-11-28 15:26:03 -08001764 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao0dfbb7e2012-11-28 15:26:03 -08001765 // peer might be NULL if the thread is still starting up.
1766 if (peer == NULL) {
1767 // We can't tell the debugger about this thread yet.
1768 // TODO: if we identified threads to the debugger by their Thread*
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001769 // rather than their peer's mirror::Object*, we could fix this.
jeffhao0dfbb7e2012-11-28 15:26:03 -08001770 // Doing so might help us report ZOMBIE threads too.
1771 return false;
1772 }
jeffhaoc1e04902012-12-13 12:41:10 -08001773 // Do we want threads from all thread groups?
1774 if (desired_thread_group_ == NULL) {
1775 return true;
1776 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001777 mirror::Object* group = soa_.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(peer);
jeffhao0dfbb7e2012-11-28 15:26:03 -08001778 return (group == desired_thread_group_);
1779 }
1780
Mathieu Chartierdbe6f462012-09-25 16:54:50 -07001781 const ScopedObjectAccessUnchecked& soa_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001782 mirror::Object* const desired_thread_group_;
Elliott Hughescaf76542012-06-28 16:08:22 -07001783 std::vector<JDWP::ObjectId>& thread_ids_;
Elliott Hughesa2155262011-11-16 16:26:58 -08001784 };
1785
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001786 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001787 mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001788 ThreadListVisitor tlv(soa, thread_group, thread_ids);
Ian Rogers50b35e22012-10-04 10:09:15 -07001789 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -07001790 Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv);
Elliott Hughescaf76542012-06-28 16:08:22 -07001791}
Elliott Hughesa2155262011-11-16 16:26:58 -08001792
Elliott Hughescaf76542012-06-28 16:08:22 -07001793void Dbg::GetChildThreadGroups(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& child_thread_group_ids) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001794 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001795 mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
Elliott Hughescaf76542012-06-28 16:08:22 -07001796
1797 // Get the ArrayList<ThreadGroup> "groups" out of this thread group...
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001798 mirror::Field* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;");
1799 mirror::Object* groups_array_list = groups_field->GetObject(thread_group);
Elliott Hughescaf76542012-06-28 16:08:22 -07001800
1801 // Get the array and size out of the ArrayList<ThreadGroup>...
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001802 mirror::Field* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;");
1803 mirror::Field* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I");
1804 mirror::ObjectArray<mirror::Object>* groups_array =
1805 array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>();
Elliott Hughescaf76542012-06-28 16:08:22 -07001806 const int32_t size = size_field->GetInt(groups_array_list);
1807
1808 // Copy the first 'size' elements out of the array into the result.
1809 for (int32_t i = 0; i < size; ++i) {
1810 child_thread_group_ids.push_back(gRegistry->Add(groups_array->Get(i)));
Elliott Hughesa2155262011-11-16 16:26:58 -08001811 }
1812}
1813
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001814static int GetStackDepth(Thread* thread)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001815 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -07001816 struct CountStackDepthVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08001817 CountStackDepthVisitor(Thread* thread)
1818 : StackVisitor(thread, NULL), depth(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -07001819
Elliott Hughes64f574f2013-02-20 14:57:12 -08001820 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
1821 // annotalysis.
1822 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers0399dde2012-06-06 17:09:28 -07001823 if (!GetMethod()->IsRuntimeMethod()) {
Elliott Hughesf8a2df72011-12-01 12:19:54 -08001824 ++depth;
1825 }
Elliott Hughes530fa002012-03-12 11:44:49 -07001826 return true;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001827 }
1828 size_t depth;
1829 };
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001830
Ian Rogers7a22fa62013-01-23 12:16:16 -08001831 CountStackDepthVisitor visitor(thread);
Ian Rogers0399dde2012-06-06 17:09:28 -07001832 visitor.WalkStack();
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001833 return visitor.depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001834}
1835
Elliott Hughes221229c2013-01-08 18:17:50 -08001836JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001837 ScopedObjectAccess soa(Thread::Current());
jeffhaoa77f0f62012-12-05 17:19:31 -08001838 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001839 Thread* thread;
1840 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1841 if (error != JDWP::ERR_NONE) {
1842 return error;
1843 }
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001844 if (!IsSuspendedForDebugger(soa, thread)) {
1845 return JDWP::ERR_THREAD_NOT_SUSPENDED;
1846 }
Elliott Hughes221229c2013-01-08 18:17:50 -08001847 result = GetStackDepth(thread);
1848 return JDWP::ERR_NONE;
Elliott Hughes86964332012-02-15 19:37:42 -08001849}
1850
Ian Rogers306057f2012-11-26 12:45:53 -08001851JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame,
1852 size_t frame_count, JDWP::ExpandBuf* buf) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001853 class GetFrameVisitor : public StackVisitor {
1854 public:
Ian Rogers7a22fa62013-01-23 12:16:16 -08001855 GetFrameVisitor(Thread* thread, size_t start_frame, size_t frame_count, JDWP::ExpandBuf* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001856 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08001857 : StackVisitor(thread, NULL), depth_(0),
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001858 start_frame_(start_frame), frame_count_(frame_count), buf_(buf) {
1859 expandBufAdd4BE(buf_, frame_count_);
Elliott Hughes03181a82011-11-17 17:22:21 -08001860 }
Ian Rogers0399dde2012-06-06 17:09:28 -07001861
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001862 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
1863 // annotalysis.
1864 virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers0399dde2012-06-06 17:09:28 -07001865 if (GetMethod()->IsRuntimeMethod()) {
Elliott Hughes530fa002012-03-12 11:44:49 -07001866 return true; // The debugger can't do anything useful with a frame that has no Method*.
Elliott Hughes03181a82011-11-17 17:22:21 -08001867 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001868 if (depth_ >= start_frame_ + frame_count_) {
Elliott Hughes530fa002012-03-12 11:44:49 -07001869 return false;
Elliott Hughes03181a82011-11-17 17:22:21 -08001870 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001871 if (depth_ >= start_frame_) {
1872 JDWP::FrameId frame_id(GetFrameId());
1873 JDWP::JdwpLocation location;
1874 SetLocation(location, GetMethod(), GetDexPc());
Elliott Hughes7baf96f2012-06-22 16:33:50 -07001875 VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3lld ", depth_, frame_id) << location;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001876 expandBufAdd8BE(buf_, frame_id);
1877 expandBufAddLocation(buf_, location);
1878 }
1879 ++depth_;
Elliott Hughes530fa002012-03-12 11:44:49 -07001880 return true;
Elliott Hughes03181a82011-11-17 17:22:21 -08001881 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001882
1883 private:
1884 size_t depth_;
1885 const size_t start_frame_;
1886 const size_t frame_count_;
1887 JDWP::ExpandBuf* buf_;
Elliott Hughes03181a82011-11-17 17:22:21 -08001888 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889
1890 ScopedObjectAccessUnchecked soa(Thread::Current());
jeffhaoa77f0f62012-12-05 17:19:31 -08001891 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001892 Thread* thread;
1893 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1894 if (error != JDWP::ERR_NONE) {
1895 return error;
1896 }
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001897 if (!IsSuspendedForDebugger(soa, thread)) {
1898 return JDWP::ERR_THREAD_NOT_SUSPENDED;
1899 }
Ian Rogers7a22fa62013-01-23 12:16:16 -08001900 GetFrameVisitor visitor(thread, start_frame, frame_count, buf);
Ian Rogers0399dde2012-06-06 17:09:28 -07001901 visitor.WalkStack();
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001902 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001903}
1904
1905JDWP::ObjectId Dbg::GetThreadSelfId() {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -07001906 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogerscfaa4552012-11-26 21:00:08 -08001907 return gRegistry->Add(soa.Self()->GetPeer());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001908}
1909
Elliott Hughes475fc232011-10-25 15:00:35 -07001910void Dbg::SuspendVM() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001911 Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001912}
1913
1914void Dbg::ResumeVM() {
Elliott Hughesc61a2672012-06-21 14:52:29 -07001915 Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001916}
1917
Elliott Hughes221229c2013-01-08 18:17:50 -08001918JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001919 ScopedLocalRef<jobject> peer(Thread::Current()->GetJniEnv(), NULL);
1920 {
1921 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001922 peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id)));
Elliott Hughes4e235312011-12-02 11:34:15 -08001923 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001924 if (peer.get() == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 return JDWP::ERR_THREAD_NOT_ALIVE;
1926 }
1927 // Suspend thread to build stack trace.
Elliott Hughesf327e072013-01-09 16:01:26 -08001928 bool timed_out;
1929 Thread* thread = Thread::SuspendForDebugger(peer.get(), request_suspension, &timed_out);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001930 if (thread != NULL) {
1931 return JDWP::ERR_NONE;
Elliott Hughesf327e072013-01-09 16:01:26 -08001932 } else if (timed_out) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001933 return JDWP::ERR_INTERNAL;
1934 } else {
1935 return JDWP::ERR_THREAD_NOT_ALIVE;
1936 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001937}
1938
Elliott Hughes221229c2013-01-08 18:17:50 -08001939void Dbg::ResumeThread(JDWP::ObjectId thread_id) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001940 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001941 mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id);
jeffhaoa77f0f62012-12-05 17:19:31 -08001942 Thread* thread;
1943 {
1944 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1945 thread = Thread::FromManagedThread(soa, peer);
1946 }
Elliott Hughes4e235312011-12-02 11:34:15 -08001947 if (thread == NULL) {
1948 LOG(WARNING) << "No such thread for resume: " << peer;
1949 return;
1950 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 bool needs_resume;
1952 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001953 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001954 needs_resume = thread->GetSuspendCount() > 0;
1955 }
1956 if (needs_resume) {
Elliott Hughes546b9862012-06-20 16:06:13 -07001957 Runtime::Current()->GetThreadList()->Resume(thread, true);
1958 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001959}
1960
1961void Dbg::SuspendSelf() {
Elliott Hughes475fc232011-10-25 15:00:35 -07001962 Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001963}
1964
Ian Rogers0399dde2012-06-06 17:09:28 -07001965struct GetThisVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08001966 GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001967 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08001968 : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {}
Ian Rogers0399dde2012-06-06 17:09:28 -07001969
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001970 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
1971 // annotalysis.
1972 virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001973 if (frame_id != GetFrameId()) {
Ian Rogers0399dde2012-06-06 17:09:28 -07001974 return true; // continue
Ian Rogers0399dde2012-06-06 17:09:28 -07001975 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -08001976 this_object = GetThisObject();
1977 return false;
Ian Rogers0399dde2012-06-06 17:09:28 -07001978 }
Elliott Hughes86b00102011-12-05 17:54:26 -08001979 }
Ian Rogers0399dde2012-06-06 17:09:28 -07001980
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001981 mirror::Object* this_object;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001982 JDWP::FrameId frame_id;
Ian Rogers0399dde2012-06-06 17:09:28 -07001983};
1984
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001985JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
1986 JDWP::ObjectId* result) {
1987 ScopedObjectAccessUnchecked soa(Thread::Current());
1988 Thread* thread;
1989 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001990 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001991 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1992 if (error != JDWP::ERR_NONE) {
1993 return error;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001994 }
Elliott Hughes9e0c1752013-01-09 14:02:58 -08001995 if (!IsSuspendedForDebugger(soa, thread)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001996 return JDWP::ERR_THREAD_NOT_SUSPENDED;
1997 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001998 }
Elliott Hughescaf76542012-06-28 16:08:22 -07001999 UniquePtr<Context> context(Context::Create());
Ian Rogers7a22fa62013-01-23 12:16:16 -08002000 GetThisVisitor visitor(thread, context.get(), frame_id);
Ian Rogers0399dde2012-06-06 17:09:28 -07002001 visitor.WalkStack();
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002002 *result = gRegistry->Add(visitor.this_object);
2003 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002004}
2005
Elliott Hughes88d63092013-01-09 09:55:54 -08002006void Dbg::GetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002007 uint8_t* buf, size_t width) {
Ian Rogers0399dde2012-06-06 17:09:28 -07002008 struct GetLocalVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08002009 GetLocalVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id, int slot,
2010 JDWP::JdwpTag tag, uint8_t* buf, size_t width)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002011 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08002012 : StackVisitor(thread, context), frame_id_(frame_id), slot_(slot), tag_(tag),
Ian Rogersca190662012-06-26 15:45:57 -07002013 buf_(buf), width_(width) {}
2014
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002015 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2016 // annotalysis.
2017 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers0399dde2012-06-06 17:09:28 -07002018 if (GetFrameId() != frame_id_) {
2019 return true; // Not our frame, carry on.
Elliott Hughesdbb40792011-11-18 17:05:22 -08002020 }
Ian Rogers0399dde2012-06-06 17:09:28 -07002021 // TODO: check that the tag is compatible with the actual type of the slot!
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002022 mirror::AbstractMethod* m = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -07002023 uint16_t reg = DemangleSlot(slot_, m);
Elliott Hughesdbb40792011-11-18 17:05:22 -08002024
Ian Rogers0399dde2012-06-06 17:09:28 -07002025 switch (tag_) {
2026 case JDWP::JT_BOOLEAN:
2027 {
2028 CHECK_EQ(width_, 1U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002029 uint32_t intVal = GetVReg(m, reg, kIntVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002030 VLOG(jdwp) << "get boolean local " << reg << " = " << intVal;
2031 JDWP::Set1(buf_+1, intVal != 0);
2032 }
2033 break;
2034 case JDWP::JT_BYTE:
2035 {
2036 CHECK_EQ(width_, 1U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002037 uint32_t intVal = GetVReg(m, reg, kIntVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002038 VLOG(jdwp) << "get byte local " << reg << " = " << intVal;
2039 JDWP::Set1(buf_+1, intVal);
2040 }
2041 break;
2042 case JDWP::JT_SHORT:
2043 case JDWP::JT_CHAR:
2044 {
2045 CHECK_EQ(width_, 2U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002046 uint32_t intVal = GetVReg(m, reg, kIntVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002047 VLOG(jdwp) << "get short/char local " << reg << " = " << intVal;
2048 JDWP::Set2BE(buf_+1, intVal);
2049 }
2050 break;
2051 case JDWP::JT_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002052 {
2053 CHECK_EQ(width_, 4U);
2054 uint32_t intVal = GetVReg(m, reg, kIntVReg);
2055 VLOG(jdwp) << "get int local " << reg << " = " << intVal;
2056 JDWP::Set4BE(buf_+1, intVal);
2057 }
2058 break;
Ian Rogers0399dde2012-06-06 17:09:28 -07002059 case JDWP::JT_FLOAT:
2060 {
2061 CHECK_EQ(width_, 4U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002062 uint32_t intVal = GetVReg(m, reg, kFloatVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002063 VLOG(jdwp) << "get int/float local " << reg << " = " << intVal;
2064 JDWP::Set4BE(buf_+1, intVal);
2065 }
2066 break;
2067 case JDWP::JT_ARRAY:
2068 {
2069 CHECK_EQ(width_, sizeof(JDWP::ObjectId));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002070 mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
Ian Rogers0399dde2012-06-06 17:09:28 -07002071 VLOG(jdwp) << "get array local " << reg << " = " << o;
2072 if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
2073 LOG(FATAL) << "Register " << reg << " expected to hold array: " << o;
2074 }
2075 JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
2076 }
2077 break;
2078 case JDWP::JT_CLASS_LOADER:
2079 case JDWP::JT_CLASS_OBJECT:
2080 case JDWP::JT_OBJECT:
2081 case JDWP::JT_STRING:
2082 case JDWP::JT_THREAD:
2083 case JDWP::JT_THREAD_GROUP:
2084 {
2085 CHECK_EQ(width_, sizeof(JDWP::ObjectId));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002086 mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
Ian Rogers0399dde2012-06-06 17:09:28 -07002087 VLOG(jdwp) << "get object local " << reg << " = " << o;
2088 if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
2089 LOG(FATAL) << "Register " << reg << " expected to hold object: " << o;
2090 }
2091 tag_ = TagFromObject(o);
2092 JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
2093 }
2094 break;
2095 case JDWP::JT_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002096 {
2097 CHECK_EQ(width_, 8U);
2098 uint32_t lo = GetVReg(m, reg, kDoubleLoVReg);
2099 uint64_t hi = GetVReg(m, reg + 1, kDoubleHiVReg);
2100 uint64_t longVal = (hi << 32) | lo;
2101 VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
2102 JDWP::Set8BE(buf_+1, longVal);
2103 }
2104 break;
Ian Rogers0399dde2012-06-06 17:09:28 -07002105 case JDWP::JT_LONG:
2106 {
2107 CHECK_EQ(width_, 8U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002108 uint32_t lo = GetVReg(m, reg, kLongLoVReg);
2109 uint64_t hi = GetVReg(m, reg + 1, kLongHiVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002110 uint64_t longVal = (hi << 32) | lo;
2111 VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
2112 JDWP::Set8BE(buf_+1, longVal);
2113 }
2114 break;
2115 default:
2116 LOG(FATAL) << "Unknown tag " << tag_;
2117 break;
2118 }
2119
2120 // Prepend tag, which may have been updated.
2121 JDWP::Set1(buf_, tag_);
2122 return false;
2123 }
2124
2125 const JDWP::FrameId frame_id_;
2126 const int slot_;
2127 JDWP::JdwpTag tag_;
2128 uint8_t* const buf_;
2129 const size_t width_;
2130 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002131
2132 ScopedObjectAccessUnchecked soa(Thread::Current());
jeffhaoa77f0f62012-12-05 17:19:31 -08002133 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08002134 Thread* thread;
2135 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2136 if (error != JDWP::ERR_NONE) {
2137 return;
2138 }
Ian Rogers0399dde2012-06-06 17:09:28 -07002139 UniquePtr<Context> context(Context::Create());
Ian Rogers7a22fa62013-01-23 12:16:16 -08002140 GetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, buf, width);
Ian Rogers0399dde2012-06-06 17:09:28 -07002141 visitor.WalkStack();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002142}
2143
Elliott Hughes88d63092013-01-09 09:55:54 -08002144void Dbg::SetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag,
Ian Rogers0399dde2012-06-06 17:09:28 -07002145 uint64_t value, size_t width) {
2146 struct SetLocalVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08002147 SetLocalVisitor(Thread* thread, Context* context,
Ian Rogers0399dde2012-06-06 17:09:28 -07002148 JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint64_t value,
Ian Rogersca190662012-06-26 15:45:57 -07002149 size_t width)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08002151 : StackVisitor(thread, context),
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002152 frame_id_(frame_id), slot_(slot), tag_(tag), value_(value), width_(width) {}
Ian Rogersca190662012-06-26 15:45:57 -07002153
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002154 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2155 // annotalysis.
2156 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers0399dde2012-06-06 17:09:28 -07002157 if (GetFrameId() != frame_id_) {
2158 return true; // Not our frame, carry on.
2159 }
2160 // TODO: check that the tag is compatible with the actual type of the slot!
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002161 mirror::AbstractMethod* m = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -07002162 uint16_t reg = DemangleSlot(slot_, m);
2163
2164 switch (tag_) {
2165 case JDWP::JT_BOOLEAN:
2166 case JDWP::JT_BYTE:
2167 CHECK_EQ(width_, 1U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002168 SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002169 break;
2170 case JDWP::JT_SHORT:
2171 case JDWP::JT_CHAR:
2172 CHECK_EQ(width_, 2U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002173 SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002174 break;
2175 case JDWP::JT_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002176 CHECK_EQ(width_, 4U);
2177 SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
2178 break;
Ian Rogers0399dde2012-06-06 17:09:28 -07002179 case JDWP::JT_FLOAT:
2180 CHECK_EQ(width_, 4U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002181 SetVReg(m, reg, static_cast<uint32_t>(value_), kFloatVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002182 break;
2183 case JDWP::JT_ARRAY:
2184 case JDWP::JT_OBJECT:
2185 case JDWP::JT_STRING:
2186 {
2187 CHECK_EQ(width_, sizeof(JDWP::ObjectId));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002188 mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value_));
Elliott Hughes64f574f2013-02-20 14:57:12 -08002189 if (o == ObjectRegistry::kInvalidObject) {
Ian Rogers0399dde2012-06-06 17:09:28 -07002190 UNIMPLEMENTED(FATAL) << "return an error code when given an invalid object to store";
2191 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002192 SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), kReferenceVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002193 }
2194 break;
2195 case JDWP::JT_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002196 CHECK_EQ(width_, 8U);
2197 SetVReg(m, reg, static_cast<uint32_t>(value_), kDoubleLoVReg);
2198 SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kDoubleHiVReg);
2199 break;
Ian Rogers0399dde2012-06-06 17:09:28 -07002200 case JDWP::JT_LONG:
2201 CHECK_EQ(width_, 8U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002202 SetVReg(m, reg, static_cast<uint32_t>(value_), kLongLoVReg);
2203 SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kLongHiVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002204 break;
2205 default:
2206 LOG(FATAL) << "Unknown tag " << tag_;
2207 break;
2208 }
2209 return false;
2210 }
2211
2212 const JDWP::FrameId frame_id_;
2213 const int slot_;
2214 const JDWP::JdwpTag tag_;
2215 const uint64_t value_;
2216 const size_t width_;
2217 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002218
2219 ScopedObjectAccessUnchecked soa(Thread::Current());
jeffhaoa77f0f62012-12-05 17:19:31 -08002220 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08002221 Thread* thread;
2222 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2223 if (error != JDWP::ERR_NONE) {
2224 return;
2225 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002226 UniquePtr<Context> context(Context::Create());
Ian Rogers7a22fa62013-01-23 12:16:16 -08002227 SetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, value, width);
Ian Rogers0399dde2012-06-06 17:09:28 -07002228 visitor.WalkStack();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002229}
2230
Ian Rogers62d6c772013-02-27 08:32:07 -08002231void Dbg::PostLocationEvent(const mirror::AbstractMethod* m, int dex_pc,
2232 mirror::Object* this_object, int event_flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002233 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002234
2235 JDWP::JdwpLocation location;
Elliott Hughes74847412012-06-20 18:10:21 -07002236 location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
Elliott Hughes64f574f2013-02-20 14:57:12 -08002237 location.class_id = gRegistry->AddRefType(c);
Elliott Hughes74847412012-06-20 18:10:21 -07002238 location.method_id = ToMethodId(m);
Elliott Hughes972a47b2012-02-21 18:16:06 -08002239 location.dex_pc = m->IsNative() ? -1 : dex_pc;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002240
Elliott Hughes64f574f2013-02-20 14:57:12 -08002241 // If 'this_object' isn't already in the registry, we know that we're not looking for it,
2242 // so there's no point adding it to the registry and burning through ids.
2243 JDWP::ObjectId this_id = 0;
2244 if (gRegistry->Contains(this_object)) {
2245 this_id = gRegistry->Add(this_object);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002246 }
Elliott Hughes64f574f2013-02-20 14:57:12 -08002247 gJdwpState->PostLocationEvent(&location, this_id, event_flags);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002248}
2249
Ian Rogers62d6c772013-02-27 08:32:07 -08002250void Dbg::PostException(Thread* thread, const ThrowLocation& throw_location,
2251 mirror::AbstractMethod* catch_method,
Elliott Hughes64f574f2013-02-20 14:57:12 -08002252 uint32_t catch_dex_pc, mirror::Throwable* exception_object) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07002253 if (!IsDebuggerActive()) {
Ian Rogers0ad5bb82011-12-07 10:16:32 -08002254 return;
2255 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -08002256
Ian Rogers62d6c772013-02-27 08:32:07 -08002257 JDWP::JdwpLocation jdwp_throw_location;
2258 SetLocation(jdwp_throw_location, throw_location.GetMethod(), throw_location.GetDexPc());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002259 JDWP::JdwpLocation catch_location;
Elliott Hughescaf76542012-06-28 16:08:22 -07002260 SetLocation(catch_location, catch_method, catch_dex_pc);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002261
2262 // We need 'this' for InstanceOnly filters.
Ian Rogers62d6c772013-02-27 08:32:07 -08002263 JDWP::ObjectId this_id = gRegistry->Add(throw_location.GetThis());
Elliott Hughes64f574f2013-02-20 14:57:12 -08002264 JDWP::ObjectId exception_id = gRegistry->Add(exception_object);
2265 JDWP::RefTypeId exception_class_id = gRegistry->AddRefType(exception_object->GetClass());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002266
Ian Rogers62d6c772013-02-27 08:32:07 -08002267 gJdwpState->PostException(&jdwp_throw_location, exception_id, exception_class_id, &catch_location,
2268 this_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002269}
2270
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002271void Dbg::PostClassPrepare(mirror::Class* c) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07002272 if (!IsDebuggerActive()) {
Elliott Hughes4740cdf2011-12-07 14:07:12 -08002273 return;
2274 }
2275
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08002276 // OLD-TODO - we currently always send both "verified" and "prepared" since
Elliott Hughes4740cdf2011-12-07 14:07:12 -08002277 // debuggers seem to like that. There might be some advantage to honesty,
2278 // since the class may not yet be verified.
2279 int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
2280 JDWP::JdwpTypeTag tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
2281 gJdwpState->PostClassPrepare(tag, gRegistry->Add(c), ClassHelper(c).GetDescriptor(), state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002282}
2283
Ian Rogers62d6c772013-02-27 08:32:07 -08002284void Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object,
2285 const mirror::AbstractMethod* m, uint32_t dex_pc) {
2286 if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
Elliott Hughes2aa2e392012-02-17 17:15:43 -08002287 return;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002288 }
2289
Elliott Hughes2aa2e392012-02-17 17:15:43 -08002290 int event_flags = 0;
2291
Elliott Hughes86964332012-02-15 19:37:42 -08002292 if (IsBreakpoint(m, dex_pc)) {
2293 event_flags |= kBreakpoint;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002294 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002295
jeffhao09bfc6a2012-12-11 18:11:43 -08002296 {
2297 // If the debugger is single-stepping one of our threads, check to
2298 // see if we're that thread and we've reached a step point.
2299 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -08002300 if (gSingleStepControl.is_active && gSingleStepControl.thread == thread) {
jeffhao09bfc6a2012-12-11 18:11:43 -08002301 CHECK(!m->IsNative());
2302 if (gSingleStepControl.step_depth == JDWP::SD_INTO) {
2303 // Step into method calls. We break when the line number
2304 // or method pointer changes. If we're in SS_MIN mode, we
2305 // always stop.
2306 if (gSingleStepControl.method != m) {
2307 event_flags |= kSingleStep;
2308 VLOG(jdwp) << "SS new method";
2309 } else if (gSingleStepControl.step_size == JDWP::SS_MIN) {
Elliott Hughes86964332012-02-15 19:37:42 -08002310 event_flags |= kSingleStep;
2311 VLOG(jdwp) << "SS new instruction";
Elliott Hughes2435a572012-02-17 16:07:41 -08002312 } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) {
2313 event_flags |= kSingleStep;
2314 VLOG(jdwp) << "SS new line";
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002315 }
jeffhao09bfc6a2012-12-11 18:11:43 -08002316 } else if (gSingleStepControl.step_depth == JDWP::SD_OVER) {
2317 // Step over method calls. We break when the line number is
2318 // different and the frame depth is <= the original frame
2319 // depth. (We can't just compare on the method, because we
2320 // might get unrolled past it by an exception, and it's tricky
2321 // to identify recursion.)
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002322
Ian Rogers62d6c772013-02-27 08:32:07 -08002323 int stack_depth = GetStackDepth(thread);
Elliott Hughes86964332012-02-15 19:37:42 -08002324
jeffhao09bfc6a2012-12-11 18:11:43 -08002325 if (stack_depth < gSingleStepControl.stack_depth) {
2326 // popped up one or more frames, always trigger
2327 event_flags |= kSingleStep;
2328 VLOG(jdwp) << "SS method pop";
2329 } else if (stack_depth == gSingleStepControl.stack_depth) {
2330 // same depth, see if we moved
2331 if (gSingleStepControl.step_size == JDWP::SS_MIN) {
2332 event_flags |= kSingleStep;
2333 VLOG(jdwp) << "SS new instruction";
2334 } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) {
2335 event_flags |= kSingleStep;
2336 VLOG(jdwp) << "SS new line";
2337 }
2338 }
2339 } else {
2340 CHECK_EQ(gSingleStepControl.step_depth, JDWP::SD_OUT);
2341 // Return from the current method. We break when the frame
2342 // depth pops up.
2343
2344 // This differs from the "method exit" break in that it stops
2345 // with the PC at the next instruction in the returned-to
2346 // function, rather than the end of the returning function.
2347
Ian Rogers62d6c772013-02-27 08:32:07 -08002348 int stack_depth = GetStackDepth(thread);
jeffhao09bfc6a2012-12-11 18:11:43 -08002349 if (stack_depth < gSingleStepControl.stack_depth) {
2350 event_flags |= kSingleStep;
2351 VLOG(jdwp) << "SS method pop";
2352 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002353 }
2354 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002355 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002356
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002357 // If there's something interesting going on, see if it matches one
2358 // of the debugger filters.
2359 if (event_flags != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002360 Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002361 }
2362}
2363
Elliott Hughes86964332012-02-15 19:37:42 -08002364void Dbg::WatchLocation(const JDWP::JdwpLocation* location) {
jeffhao09bfc6a2012-12-11 18:11:43 -08002365 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002366 mirror::AbstractMethod* m = FromMethodId(location->method_id);
Elliott Hughes972a47b2012-02-21 18:16:06 -08002367 gBreakpoints.push_back(Breakpoint(m, location->dex_pc));
Elliott Hughes86964332012-02-15 19:37:42 -08002368 VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " << gBreakpoints[gBreakpoints.size() - 1];
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002369}
2370
Elliott Hughes86964332012-02-15 19:37:42 -08002371void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location) {
jeffhao09bfc6a2012-12-11 18:11:43 -08002372 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002373 mirror::AbstractMethod* m = FromMethodId(location->method_id);
Elliott Hughes86964332012-02-15 19:37:42 -08002374 for (size_t i = 0; i < gBreakpoints.size(); ++i) {
Elliott Hughes972a47b2012-02-21 18:16:06 -08002375 if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == location->dex_pc) {
Elliott Hughes86964332012-02-15 19:37:42 -08002376 VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
2377 gBreakpoints.erase(gBreakpoints.begin() + i);
2378 return;
2379 }
2380 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002381}
2382
Jeff Hao449db332013-04-12 18:30:52 -07002383// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
2384// cause suspension if the thread is the current thread.
2385class ScopedThreadSuspension {
2386 public:
Ian Rogers33e95662013-05-20 20:29:14 -07002387 ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
2388 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
Jeff Hao449db332013-04-12 18:30:52 -07002389 thread_(NULL),
2390 error_(JDWP::ERR_NONE),
2391 self_suspend_(false),
Ian Rogers33e95662013-05-20 20:29:14 -07002392 other_suspend_(false) {
Jeff Hao449db332013-04-12 18:30:52 -07002393 ScopedObjectAccessUnchecked soa(self);
2394 {
2395 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2396 error_ = DecodeThread(soa, thread_id, thread_);
2397 }
2398 if (error_ == JDWP::ERR_NONE) {
2399 if (thread_ == soa.Self()) {
2400 self_suspend_ = true;
2401 } else {
2402 soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
2403 jobject thread_peer = gRegistry->GetJObject(thread_id);
2404 bool timed_out;
2405 Thread* suspended_thread = Thread::SuspendForDebugger(thread_peer, true, &timed_out);
2406 CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension);
2407 if (suspended_thread == NULL) {
2408 // Thread terminated from under us while suspending.
2409 error_ = JDWP::ERR_INVALID_THREAD;
2410 } else {
2411 CHECK_EQ(suspended_thread, thread_);
2412 other_suspend_ = true;
2413 }
2414 }
2415 }
Elliott Hughes2435a572012-02-17 16:07:41 -08002416 }
Elliott Hughes86964332012-02-15 19:37:42 -08002417
Jeff Hao449db332013-04-12 18:30:52 -07002418 Thread* GetThread() const {
2419 return thread_;
2420 }
2421
2422 JDWP::JdwpError GetError() const {
2423 return error_;
2424 }
2425
2426 ~ScopedThreadSuspension() {
2427 if (other_suspend_) {
2428 Runtime::Current()->GetThreadList()->Resume(thread_, true);
2429 }
2430 }
2431
2432 private:
2433 Thread* thread_;
2434 JDWP::JdwpError error_;
2435 bool self_suspend_;
2436 bool other_suspend_;
2437};
2438
2439JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
2440 JDWP::JdwpStepDepth step_depth) {
2441 Thread* self = Thread::Current();
2442 ScopedThreadSuspension sts(self, thread_id);
2443 if (sts.GetError() != JDWP::ERR_NONE) {
2444 return sts.GetError();
2445 }
2446
2447 MutexLock mu2(self, *Locks::breakpoint_lock_);
Elliott Hughes86964332012-02-15 19:37:42 -08002448 // TODO: there's no theoretical reason why we couldn't support single-stepping
2449 // of multiple threads at once, but we never did so historically.
Jeff Hao449db332013-04-12 18:30:52 -07002450 if (gSingleStepControl.thread != NULL && sts.GetThread() != gSingleStepControl.thread) {
Elliott Hughes86964332012-02-15 19:37:42 -08002451 LOG(WARNING) << "single-step already active for " << *gSingleStepControl.thread
Jeff Hao449db332013-04-12 18:30:52 -07002452 << "; switching to " << *sts.GetThread();
Elliott Hughes86964332012-02-15 19:37:42 -08002453 }
2454
Elliott Hughes2435a572012-02-17 16:07:41 -08002455 //
2456 // Work out what Method* we're in, the current line number, and how deep the stack currently
2457 // is for step-out.
2458 //
2459
Ian Rogers0399dde2012-06-06 17:09:28 -07002460 struct SingleStepStackVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08002461 SingleStepStackVisitor(Thread* thread)
jeffhao09bfc6a2012-12-11 18:11:43 -08002462 EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002463 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08002464 : StackVisitor(thread, NULL) {
Elliott Hughes86964332012-02-15 19:37:42 -08002465 gSingleStepControl.method = NULL;
2466 gSingleStepControl.stack_depth = 0;
2467 }
Ian Rogersca190662012-06-26 15:45:57 -07002468
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002469 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2470 // annotalysis.
2471 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
jeffhao09bfc6a2012-12-11 18:11:43 -08002472 Locks::breakpoint_lock_->AssertHeld(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002473 const mirror::AbstractMethod* m = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -07002474 if (!m->IsRuntimeMethod()) {
Elliott Hughes86964332012-02-15 19:37:42 -08002475 ++gSingleStepControl.stack_depth;
2476 if (gSingleStepControl.method == NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002477 const mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache();
Elliott Hughes2435a572012-02-17 16:07:41 -08002478 gSingleStepControl.method = m;
2479 gSingleStepControl.line_number = -1;
2480 if (dex_cache != NULL) {
Ian Rogers4445a7e2012-10-05 17:19:13 -07002481 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers0399dde2012-06-06 17:09:28 -07002482 gSingleStepControl.line_number = dex_file.GetLineNumFromPC(m, GetDexPc());
Elliott Hughes2435a572012-02-17 16:07:41 -08002483 }
Elliott Hughes86964332012-02-15 19:37:42 -08002484 }
2485 }
Elliott Hughes530fa002012-03-12 11:44:49 -07002486 return true;
Elliott Hughes86964332012-02-15 19:37:42 -08002487 }
2488 };
Jeff Hao449db332013-04-12 18:30:52 -07002489
2490 SingleStepStackVisitor visitor(sts.GetThread());
Ian Rogers0399dde2012-06-06 17:09:28 -07002491 visitor.WalkStack();
Elliott Hughes86964332012-02-15 19:37:42 -08002492
Elliott Hughes2435a572012-02-17 16:07:41 -08002493 //
2494 // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
2495 //
2496
2497 struct DebugCallbackContext {
jeffhao09bfc6a2012-12-11 18:11:43 -08002498 DebugCallbackContext() EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_) {
Elliott Hughes2435a572012-02-17 16:07:41 -08002499 last_pc_valid = false;
2500 last_pc = 0;
Elliott Hughes2435a572012-02-17 16:07:41 -08002501 }
2502
jeffhao09bfc6a2012-12-11 18:11:43 -08002503 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2504 // annotalysis.
2505 static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) NO_THREAD_SAFETY_ANALYSIS {
2506 Locks::breakpoint_lock_->AssertHeld(Thread::Current());
Elliott Hughes2435a572012-02-17 16:07:41 -08002507 DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context);
2508 if (static_cast<int32_t>(line_number) == gSingleStepControl.line_number) {
2509 if (!context->last_pc_valid) {
2510 // Everything from this address until the next line change is ours.
2511 context->last_pc = address;
2512 context->last_pc_valid = true;
2513 }
2514 // Otherwise, if we're already in a valid range for this line,
2515 // just keep going (shouldn't really happen)...
2516 } else if (context->last_pc_valid) { // and the line number is new
2517 // Add everything from the last entry up until here to the set
2518 for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) {
2519 gSingleStepControl.dex_pcs.insert(dex_pc);
2520 }
2521 context->last_pc_valid = false;
2522 }
2523 return false; // There may be multiple entries for any given line.
2524 }
2525
jeffhao09bfc6a2012-12-11 18:11:43 -08002526 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2527 // annotalysis.
2528 ~DebugCallbackContext() NO_THREAD_SAFETY_ANALYSIS {
2529 Locks::breakpoint_lock_->AssertHeld(Thread::Current());
Elliott Hughes2435a572012-02-17 16:07:41 -08002530 // If the line number was the last in the position table...
2531 if (last_pc_valid) {
2532 size_t end = MethodHelper(gSingleStepControl.method).GetCodeItem()->insns_size_in_code_units_;
2533 for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) {
2534 gSingleStepControl.dex_pcs.insert(dex_pc);
2535 }
2536 }
2537 }
2538
2539 bool last_pc_valid;
2540 uint32_t last_pc;
2541 };
Elliott Hughes3e2e1a22012-02-21 11:33:41 -08002542 gSingleStepControl.dex_pcs.clear();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002543 const mirror::AbstractMethod* m = gSingleStepControl.method;
Elliott Hughes3e2e1a22012-02-21 11:33:41 -08002544 if (m->IsNative()) {
2545 gSingleStepControl.line_number = -1;
2546 } else {
2547 DebugCallbackContext context;
2548 MethodHelper mh(m);
2549 mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(),
2550 DebugCallbackContext::Callback, NULL, &context);
2551 }
Elliott Hughes2435a572012-02-17 16:07:41 -08002552
2553 //
2554 // Everything else...
2555 //
2556
Jeff Hao449db332013-04-12 18:30:52 -07002557 gSingleStepControl.thread = sts.GetThread();
Elliott Hughes86964332012-02-15 19:37:42 -08002558 gSingleStepControl.step_size = step_size;
2559 gSingleStepControl.step_depth = step_depth;
2560 gSingleStepControl.is_active = true;
2561
Elliott Hughes2435a572012-02-17 16:07:41 -08002562 if (VLOG_IS_ON(jdwp)) {
2563 VLOG(jdwp) << "Single-step thread: " << *gSingleStepControl.thread;
2564 VLOG(jdwp) << "Single-step step size: " << gSingleStepControl.step_size;
2565 VLOG(jdwp) << "Single-step step depth: " << gSingleStepControl.step_depth;
2566 VLOG(jdwp) << "Single-step current method: " << PrettyMethod(gSingleStepControl.method);
2567 VLOG(jdwp) << "Single-step current line: " << gSingleStepControl.line_number;
2568 VLOG(jdwp) << "Single-step current stack depth: " << gSingleStepControl.stack_depth;
2569 VLOG(jdwp) << "Single-step dex_pc values:";
2570 for (std::set<uint32_t>::iterator it = gSingleStepControl.dex_pcs.begin() ; it != gSingleStepControl.dex_pcs.end(); ++it) {
Elliott Hughes229feb72012-02-23 13:33:29 -08002571 VLOG(jdwp) << StringPrintf(" %#x", *it);
Elliott Hughes2435a572012-02-17 16:07:41 -08002572 }
2573 }
2574
2575 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002576}
2577
Elliott Hughes221229c2013-01-08 18:17:50 -08002578void Dbg::UnconfigureStep(JDWP::ObjectId /*thread_id*/) {
jeffhao09bfc6a2012-12-11 18:11:43 -08002579 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -07002580
Elliott Hughes86964332012-02-15 19:37:42 -08002581 gSingleStepControl.is_active = false;
2582 gSingleStepControl.thread = NULL;
Elliott Hughes2435a572012-02-17 16:07:41 -08002583 gSingleStepControl.dex_pcs.clear();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002584}
2585
Elliott Hughes45651fd2012-02-21 15:48:20 -08002586static char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
2587 switch (tag) {
2588 default:
2589 LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
2590
2591 // Primitives.
2592 case JDWP::JT_BYTE: return 'B';
2593 case JDWP::JT_CHAR: return 'C';
2594 case JDWP::JT_FLOAT: return 'F';
2595 case JDWP::JT_DOUBLE: return 'D';
2596 case JDWP::JT_INT: return 'I';
2597 case JDWP::JT_LONG: return 'J';
2598 case JDWP::JT_SHORT: return 'S';
2599 case JDWP::JT_VOID: return 'V';
2600 case JDWP::JT_BOOLEAN: return 'Z';
2601
2602 // Reference types.
2603 case JDWP::JT_ARRAY:
2604 case JDWP::JT_OBJECT:
2605 case JDWP::JT_STRING:
2606 case JDWP::JT_THREAD:
2607 case JDWP::JT_THREAD_GROUP:
2608 case JDWP::JT_CLASS_LOADER:
2609 case JDWP::JT_CLASS_OBJECT:
2610 return 'L';
2611 }
2612}
2613
Elliott Hughes88d63092013-01-09 09:55:54 -08002614JDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id,
2615 JDWP::RefTypeId class_id, JDWP::MethodId method_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002616 uint32_t arg_count, uint64_t* arg_values,
2617 JDWP::JdwpTag* arg_types, uint32_t options,
2618 JDWP::JdwpTag* pResultTag, uint64_t* pResultValue,
2619 JDWP::ObjectId* pExceptionId) {
Elliott Hughesd07986f2011-12-06 18:27:45 -08002620 ThreadList* thread_list = Runtime::Current()->GetThreadList();
2621
2622 Thread* targetThread = NULL;
2623 DebugInvokeReq* req = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002624 Thread* self = Thread::Current();
Elliott Hughesd07986f2011-12-06 18:27:45 -08002625 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002626 ScopedObjectAccessUnchecked soa(self);
Ian Rogers50b35e22012-10-04 10:09:15 -07002627 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08002628 JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread);
2629 if (error != JDWP::ERR_NONE) {
2630 LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
2631 return error;
Elliott Hughesd07986f2011-12-06 18:27:45 -08002632 }
2633 req = targetThread->GetInvokeReq();
2634 if (!req->ready) {
2635 LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
2636 return JDWP::ERR_INVALID_THREAD;
2637 }
2638
2639 /*
2640 * We currently have a bug where we don't successfully resume the
2641 * target thread if the suspend count is too deep. We're expected to
2642 * require one "resume" for each "suspend", but when asked to execute
2643 * a method we have to resume fully and then re-suspend it back to the
2644 * same level. (The easiest way to cause this is to type "suspend"
2645 * multiple times in jdb.)
2646 *
2647 * It's unclear what this means when the event specifies "resume all"
2648 * and some threads are suspended more deeply than others. This is
2649 * a rare problem, so for now we just prevent it from hanging forever
2650 * by rejecting the method invocation request. Without this, we will
2651 * be stuck waiting on a suspended thread.
2652 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002653 int suspend_count;
2654 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002655 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002656 suspend_count = targetThread->GetSuspendCount();
2657 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08002658 if (suspend_count > 1) {
2659 LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count;
2660 return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here.
2661 }
2662
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08002663 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002664 mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08002665 if (receiver == ObjectRegistry::kInvalidObject) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08002666 return JDWP::ERR_INVALID_OBJECT;
2667 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08002668
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002669 mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08002670 if (thread == ObjectRegistry::kInvalidObject) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08002671 return JDWP::ERR_INVALID_OBJECT;
2672 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08002673 // TODO: check that 'thread' is actually a java.lang.Thread!
2674
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002675 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes45651fd2012-02-21 15:48:20 -08002676 if (c == NULL) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08002677 return status;
2678 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08002679
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002680 mirror::AbstractMethod* m = FromMethodId(method_id);
Elliott Hughes45651fd2012-02-21 15:48:20 -08002681 if (m->IsStatic() != (receiver == NULL)) {
2682 return JDWP::ERR_INVALID_METHODID;
2683 }
2684 if (m->IsStatic()) {
2685 if (m->GetDeclaringClass() != c) {
2686 return JDWP::ERR_INVALID_METHODID;
2687 }
2688 } else {
2689 if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
2690 return JDWP::ERR_INVALID_METHODID;
2691 }
2692 }
2693
2694 // Check the argument list matches the method.
2695 MethodHelper mh(m);
2696 if (mh.GetShortyLength() - 1 != arg_count) {
2697 return JDWP::ERR_ILLEGAL_ARGUMENT;
2698 }
2699 const char* shorty = mh.GetShorty();
Elliott Hughes09201632013-04-15 15:50:07 -07002700 const DexFile::TypeList* types = mh.GetParameterTypeList();
Elliott Hughes45651fd2012-02-21 15:48:20 -08002701 for (size_t i = 0; i < arg_count; ++i) {
2702 if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
2703 return JDWP::ERR_ILLEGAL_ARGUMENT;
2704 }
Elliott Hughes09201632013-04-15 15:50:07 -07002705
2706 if (shorty[i + 1] == 'L') {
2707 // Did we really get an argument of an appropriate reference type?
2708 mirror::Class* parameter_type = mh.GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
2709 mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i]);
2710 if (argument == ObjectRegistry::kInvalidObject) {
2711 return JDWP::ERR_INVALID_OBJECT;
2712 }
2713 if (!argument->InstanceOf(parameter_type)) {
2714 return JDWP::ERR_ILLEGAL_ARGUMENT;
2715 }
2716
2717 // Turn the on-the-wire ObjectId into a jobject.
2718 jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
2719 v.l = gRegistry->GetJObject(arg_values[i]);
2720 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08002721 }
2722
2723 req->receiver_ = receiver;
2724 req->thread_ = thread;
2725 req->class_ = c;
2726 req->method_ = m;
2727 req->arg_count_ = arg_count;
2728 req->arg_values_ = arg_values;
Elliott Hughesd07986f2011-12-06 18:27:45 -08002729 req->options_ = options;
2730 req->invoke_needed_ = true;
2731 }
2732
2733 // The fact that we've released the thread list lock is a bit risky --- if the thread goes
2734 // away we're sitting high and dry -- but we must release this before the ResumeAllThreads
2735 // call, and it's unwise to hold it during WaitForSuspend.
2736
2737 {
2738 /*
2739 * We change our (JDWP thread) status, which should be THREAD_RUNNING,
Elliott Hughes81ff3182012-03-23 20:35:56 -07002740 * so we can suspend for a GC if the invoke request causes us to
Elliott Hughesd07986f2011-12-06 18:27:45 -08002741 * run out of memory. It's also a good idea to change it before locking
2742 * the invokeReq mutex, although that should never be held for long.
2743 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002744 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002745
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002746 VLOG(jdwp) << " Transferring control to event thread";
Elliott Hughesd07986f2011-12-06 18:27:45 -08002747 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002748 MutexLock mu(self, req->lock_);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002749
2750 if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002751 VLOG(jdwp) << " Resuming all threads";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002752 thread_list->UndoDebuggerSuspensions();
Elliott Hughesd07986f2011-12-06 18:27:45 -08002753 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002754 VLOG(jdwp) << " Resuming event thread only";
Elliott Hughesd07986f2011-12-06 18:27:45 -08002755 thread_list->Resume(targetThread, true);
2756 }
2757
2758 // Wait for the request to finish executing.
2759 while (req->invoke_needed_) {
Ian Rogersc604d732012-10-14 16:09:54 -07002760 req->cond_.Wait(self);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002761 }
2762 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002763 VLOG(jdwp) << " Control has returned from event thread";
Elliott Hughesd07986f2011-12-06 18:27:45 -08002764
2765 /* wait for thread to re-suspend itself */
Elliott Hughes221229c2013-01-08 18:17:50 -08002766 SuspendThread(thread_id, false /* request_suspension */ );
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002767 self->TransitionFromSuspendedToRunnable();
Elliott Hughesd07986f2011-12-06 18:27:45 -08002768 }
2769
2770 /*
2771 * Suspend the threads. We waited for the target thread to suspend
2772 * itself, so all we need to do is suspend the others.
2773 *
2774 * The suspendAllThreads() call will double-suspend the event thread,
2775 * so we want to resume the target thread once to keep the books straight.
2776 */
2777 if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002778 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002779 VLOG(jdwp) << " Suspending all threads";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002780 thread_list->SuspendAllForDebugger();
2781 self->TransitionFromSuspendedToRunnable();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002782 VLOG(jdwp) << " Resuming event thread to balance the count";
Elliott Hughesd07986f2011-12-06 18:27:45 -08002783 thread_list->Resume(targetThread, true);
2784 }
2785
2786 // Copy the result.
2787 *pResultTag = req->result_tag;
2788 if (IsPrimitiveTag(req->result_tag)) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07002789 *pResultValue = req->result_value.GetJ();
Elliott Hughesd07986f2011-12-06 18:27:45 -08002790 } else {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07002791 *pResultValue = gRegistry->Add(req->result_value.GetL());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002792 }
2793 *pExceptionId = req->exception;
2794 return req->error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002795}
2796
2797void Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002798 ScopedObjectAccess soa(Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002799
Elliott Hughes81ff3182012-03-23 20:35:56 -07002800 // We can be called while an exception is pending. We need
Elliott Hughesd07986f2011-12-06 18:27:45 -08002801 // to preserve that across the method invocation.
Ian Rogers62d6c772013-02-27 08:32:07 -08002802 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), NULL);
2803 SirtRef<mirror::AbstractMethod> old_throw_method(soa.Self(), NULL);
2804 SirtRef<mirror::Throwable> old_exception(soa.Self(), NULL);
2805 uint32_t old_throw_dex_pc;
2806 {
2807 ThrowLocation old_throw_location;
2808 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
2809 old_throw_this_object.reset(old_throw_location.GetThis());
2810 old_throw_method.reset(old_throw_location.GetMethod());
2811 old_exception.reset(old_exception_obj);
2812 old_throw_dex_pc = old_throw_location.GetDexPc();
2813 soa.Self()->ClearException();
2814 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08002815
2816 // Translate the method through the vtable, unless the debugger wants to suppress it.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002817 mirror::AbstractMethod* m = pReq->method_;
Elliott Hughesd07986f2011-12-06 18:27:45 -08002818 if ((pReq->options_ & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002819 mirror::AbstractMethod* actual_method = pReq->class_->FindVirtualMethodForVirtualOrInterface(pReq->method_);
Elliott Hughes45651fd2012-02-21 15:48:20 -08002820 if (actual_method != m) {
2821 VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) << " to " << PrettyMethod(actual_method);
2822 m = actual_method;
2823 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08002824 }
Elliott Hughescfa9cfa2013-04-16 16:52:01 -07002825 VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m)
2826 << " receiver=" << pReq->receiver_
2827 << " arg_count=" << pReq->arg_count_;
Elliott Hughesd07986f2011-12-06 18:27:45 -08002828 CHECK(m != NULL);
2829
2830 CHECK_EQ(sizeof(jvalue), sizeof(uint64_t));
2831
Jeff Hao5d917302013-02-27 17:57:33 -08002832 MethodHelper mh(m);
2833 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
2834 arg_array.BuildArgArray(soa, pReq->receiver_, reinterpret_cast<jvalue*>(pReq->arg_values_));
Jeff Hao6474d192013-03-26 14:08:09 -07002835 InvokeWithArgArray(soa, m, &arg_array, &pReq->result_value, mh.GetShorty()[0]);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002836
Ian Rogers62d6c772013-02-27 08:32:07 -08002837 mirror::Throwable* exception = soa.Self()->GetException(NULL);
2838 soa.Self()->ClearException();
2839 pReq->exception = gRegistry->Add(exception);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002840 pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty());
2841 if (pReq->exception != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002842 VLOG(jdwp) << " JDWP invocation returning with exception=" << exception
2843 << " " << exception->Dump();
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07002844 pReq->result_value.SetJ(0);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002845 } else if (pReq->result_tag == JDWP::JT_OBJECT) {
2846 /* if no exception thrown, examine object result more closely */
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07002847 JDWP::JdwpTag new_tag = TagFromObject(pReq->result_value.GetL());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002848 if (new_tag != pReq->result_tag) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002849 VLOG(jdwp) << " JDWP promoted result from " << pReq->result_tag << " to " << new_tag;
Elliott Hughesd07986f2011-12-06 18:27:45 -08002850 pReq->result_tag = new_tag;
2851 }
2852
2853 /*
2854 * Register the object. We don't actually need an ObjectId yet,
2855 * but we do need to be sure that the GC won't move or discard the
2856 * object when we switch out of RUNNING. The ObjectId conversion
2857 * will add the object to the "do not touch" list.
2858 *
2859 * We can't use the "tracked allocation" mechanism here because
2860 * the object is going to be handed off to a different thread.
2861 */
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07002862 gRegistry->Add(pReq->result_value.GetL());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002863 }
2864
2865 if (old_exception.get() != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002866 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
2867 old_throw_dex_pc);
2868 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002869 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002870}
2871
Elliott Hughesd07986f2011-12-06 18:27:45 -08002872/*
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002873 * "request" contains a full JDWP packet, possibly with multiple chunks. We
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002874 * need to process each, accumulate the replies, and ship the whole thing
2875 * back.
2876 *
2877 * Returns "true" if we have a reply. The reply buffer is newly allocated,
2878 * and includes the chunk type/length, followed by the data.
2879 *
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08002880 * OLD-TODO: we currently assume that the request and reply include a single
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002881 * chunk. If this becomes inconvenient we will need to adapt.
2882 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002883bool Dbg::DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002884 Thread* self = Thread::Current();
2885 JNIEnv* env = self->GetJniEnv();
2886
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002887 uint32_t type = request.ReadUnsigned32("type");
2888 uint32_t length = request.ReadUnsigned32("length");
2889
2890 // Create a byte[] corresponding to 'request'.
2891 size_t request_length = request.size();
2892 ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length));
Elliott Hughes6a5bd492011-10-28 14:33:57 -07002893 if (dataArray.get() == NULL) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002894 LOG(WARNING) << "byte[] allocation failed: " << request_length;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002895 env->ExceptionClear();
2896 return false;
2897 }
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002898 env->SetByteArrayRegion(dataArray.get(), 0, request_length, reinterpret_cast<const jbyte*>(request.data()));
2899 request.Skip(request_length);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002900
2901 // Run through and find all chunks. [Currently just find the first.]
Elliott Hughes6a5bd492011-10-28 14:33:57 -07002902 ScopedByteArrayRO contents(env, dataArray.get());
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002903 if (length != request_length) {
2904 LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, request_length);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002905 return false;
2906 }
2907
2908 // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
Elliott Hugheseac76672012-05-24 21:56:51 -07002909 ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
2910 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002911 type, dataArray.get(), 0, length));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002912 if (env->ExceptionCheck()) {
2913 LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
2914 env->ExceptionDescribe();
2915 env->ExceptionClear();
2916 return false;
2917 }
2918
Elliott Hughes6a5bd492011-10-28 14:33:57 -07002919 if (chunk.get() == NULL) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002920 return false;
2921 }
2922
2923 /*
2924 * Pull the pieces out of the chunk. We copy the results into a
2925 * newly-allocated buffer that the caller can free. We don't want to
2926 * continue using the Chunk object because nothing has a reference to it.
2927 *
2928 * We could avoid this by returning type/data/offset/length and having
2929 * the caller be aware of the object lifetime issues, but that
Elliott Hughes81ff3182012-03-23 20:35:56 -07002930 * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002931 * if we have responses for multiple chunks.
2932 *
2933 * So we're pretty much stuck with copying data around multiple times.
2934 */
Elliott Hugheseac76672012-05-24 21:56:51 -07002935 ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002936 jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
Elliott Hugheseac76672012-05-24 21:56:51 -07002937 length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
Elliott Hugheseac76672012-05-24 21:56:51 -07002938 type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002939
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002940 VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07002941 if (length == 0 || replyData.get() == NULL) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002942 return false;
2943 }
2944
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002945 const int kChunkHdrLen = 8;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002946 uint8_t* reply = new uint8_t[length + kChunkHdrLen];
2947 if (reply == NULL) {
2948 LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
2949 return false;
2950 }
Elliott Hughesf7c3b662011-10-27 12:04:56 -07002951 JDWP::Set4BE(reply + 0, type);
2952 JDWP::Set4BE(reply + 4, length);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07002953 env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002954
2955 *pReplyBuf = reply;
2956 *pReplyLen = length + kChunkHdrLen;
2957
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002958 VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002959 return true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002960}
2961
Elliott Hughesa2155262011-11-16 16:26:58 -08002962void Dbg::DdmBroadcast(bool connect) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002963 VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
Elliott Hughes47fce012011-10-25 18:37:19 -07002964
2965 Thread* self = Thread::Current();
Ian Rogers50b35e22012-10-04 10:09:15 -07002966 if (self->GetState() != kRunnable) {
2967 LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
2968 /* try anyway? */
Elliott Hughes47fce012011-10-25 18:37:19 -07002969 }
2970
2971 JNIEnv* env = self->GetJniEnv();
Elliott Hughes47fce012011-10-25 18:37:19 -07002972 jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
Elliott Hugheseac76672012-05-24 21:56:51 -07002973 env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
2974 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
2975 event);
Elliott Hughes47fce012011-10-25 18:37:19 -07002976 if (env->ExceptionCheck()) {
2977 LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
2978 env->ExceptionDescribe();
2979 env->ExceptionClear();
2980 }
2981}
2982
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002983void Dbg::DdmConnected() {
Elliott Hughesa2155262011-11-16 16:26:58 -08002984 Dbg::DdmBroadcast(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002985}
2986
2987void Dbg::DdmDisconnected() {
Elliott Hughesa2155262011-11-16 16:26:58 -08002988 Dbg::DdmBroadcast(false);
Elliott Hughes47fce012011-10-25 18:37:19 -07002989 gDdmThreadNotification = false;
2990}
2991
2992/*
Elliott Hughes82188472011-11-07 18:11:48 -08002993 * Send a notification when a thread starts, stops, or changes its name.
Elliott Hughes47fce012011-10-25 18:37:19 -07002994 *
2995 * Because we broadcast the full set of threads when the notifications are
2996 * first enabled, it's possible for "thread" to be actively executing.
2997 */
Elliott Hughes82188472011-11-07 18:11:48 -08002998void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
Elliott Hughes47fce012011-10-25 18:37:19 -07002999 if (!gDdmThreadNotification) {
3000 return;
3001 }
3002
Elliott Hughes82188472011-11-07 18:11:48 -08003003 if (type == CHUNK_TYPE("THDE")) {
Elliott Hughes47fce012011-10-25 18:37:19 -07003004 uint8_t buf[4];
Elliott Hughesf7c3b662011-10-27 12:04:56 -07003005 JDWP::Set4BE(&buf[0], t->GetThinLockId());
Elliott Hughes47fce012011-10-25 18:37:19 -07003006 Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
Elliott Hughes82188472011-11-07 18:11:48 -08003007 } else {
3008 CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003009 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003010 SirtRef<mirror::String> name(soa.Self(), t->GetThreadName(soa));
Elliott Hughes82188472011-11-07 18:11:48 -08003011 size_t char_count = (name.get() != NULL) ? name->GetLength() : 0;
jeffhao725a9572012-11-13 18:20:12 -08003012 const jchar* chars = (name.get() != NULL) ? name->GetCharArray()->GetData() : NULL;
Elliott Hughes82188472011-11-07 18:11:48 -08003013
Elliott Hughes21f32d72011-11-09 17:44:13 -08003014 std::vector<uint8_t> bytes;
Elliott Hughes545a0642011-11-08 19:10:03 -08003015 JDWP::Append4BE(bytes, t->GetThinLockId());
3016 JDWP::AppendUtf16BE(bytes, chars, char_count);
Elliott Hughes21f32d72011-11-09 17:44:13 -08003017 CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
3018 Dbg::DdmSendChunk(type, bytes);
Elliott Hughes47fce012011-10-25 18:37:19 -07003019 }
3020}
3021
Elliott Hughes47fce012011-10-25 18:37:19 -07003022void Dbg::DdmSetThreadNotification(bool enable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003023 // Enable/disable thread notifications.
Elliott Hughes47fce012011-10-25 18:37:19 -07003024 gDdmThreadNotification = enable;
3025 if (enable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003026 // Suspend the VM then post thread start notifications for all threads. Threads attaching will
3027 // see a suspension in progress and block until that ends. They then post their own start
3028 // notification.
3029 SuspendVM();
3030 std::list<Thread*> threads;
Ian Rogers50b35e22012-10-04 10:09:15 -07003031 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003032 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003033 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003034 threads = Runtime::Current()->GetThreadList()->GetList();
3035 }
3036 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003037 ScopedObjectAccess soa(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003038 typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto
3039 for (It it = threads.begin(), end = threads.end(); it != end; ++it) {
3040 Dbg::DdmSendThreadNotification(*it, CHUNK_TYPE("THCR"));
3041 }
3042 }
3043 ResumeVM();
Elliott Hughes47fce012011-10-25 18:37:19 -07003044 }
3045}
3046
Elliott Hughesa2155262011-11-16 16:26:58 -08003047void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07003048 if (IsDebuggerActive()) {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -07003049 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogerscfaa4552012-11-26 21:00:08 -08003050 JDWP::ObjectId id = gRegistry->Add(t->GetPeer());
Elliott Hughes82188472011-11-07 18:11:48 -08003051 gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR"));
Elliott Hughes47fce012011-10-25 18:37:19 -07003052 }
Elliott Hughes82188472011-11-07 18:11:48 -08003053 Dbg::DdmSendThreadNotification(t, type);
Elliott Hughes47fce012011-10-25 18:37:19 -07003054}
3055
3056void Dbg::PostThreadStart(Thread* t) {
Elliott Hughesa2155262011-11-16 16:26:58 -08003057 Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
Elliott Hughes47fce012011-10-25 18:37:19 -07003058}
3059
3060void Dbg::PostThreadDeath(Thread* t) {
Elliott Hughesa2155262011-11-16 16:26:58 -08003061 Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003062}
3063
Elliott Hughes82188472011-11-07 18:11:48 -08003064void Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) {
Elliott Hughes3bb81562011-10-21 18:52:59 -07003065 CHECK(buf != NULL);
3066 iovec vec[1];
3067 vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
3068 vec[0].iov_len = byte_count;
3069 Dbg::DdmSendChunkV(type, vec, 1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003070}
3071
Elliott Hughes21f32d72011-11-09 17:44:13 -08003072void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) {
3073 DdmSendChunk(type, bytes.size(), &bytes[0]);
3074}
3075
Elliott Hughescccd84f2011-12-05 16:51:54 -08003076void Dbg::DdmSendChunkV(uint32_t type, const struct iovec* iov, int iov_count) {
Elliott Hughes3bb81562011-10-21 18:52:59 -07003077 if (gJdwpState == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003078 VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
Elliott Hughes3bb81562011-10-21 18:52:59 -07003079 } else {
Elliott Hughescccd84f2011-12-05 16:51:54 -08003080 gJdwpState->DdmSendChunkV(type, iov, iov_count);
Elliott Hughes3bb81562011-10-21 18:52:59 -07003081 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003082}
3083
Elliott Hughes767a1472011-10-26 18:49:02 -07003084int Dbg::DdmHandleHpifChunk(HpifWhen when) {
3085 if (when == HPIF_WHEN_NOW) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07003086 DdmSendHeapInfo(when);
Elliott Hughes767a1472011-10-26 18:49:02 -07003087 return true;
3088 }
3089
3090 if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
3091 LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
3092 return false;
3093 }
3094
3095 gDdmHpifWhen = when;
3096 return true;
3097}
3098
3099bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
3100 if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
3101 LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
3102 return false;
3103 }
3104
3105 if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
3106 LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
3107 return false;
3108 }
3109
3110 if (native) {
3111 gDdmNhsgWhen = when;
3112 gDdmNhsgWhat = what;
3113 } else {
3114 gDdmHpsgWhen = when;
3115 gDdmHpsgWhat = what;
3116 }
3117 return true;
3118}
3119
Elliott Hughes7162ad92011-10-27 14:08:42 -07003120void Dbg::DdmSendHeapInfo(HpifWhen reason) {
3121 // If there's a one-shot 'when', reset it.
3122 if (reason == gDdmHpifWhen) {
3123 if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
3124 gDdmHpifWhen = HPIF_WHEN_NEVER;
3125 }
3126 }
3127
3128 /*
3129 * Chunk HPIF (client --> server)
3130 *
3131 * Heap Info. General information about the heap,
3132 * suitable for a summary display.
3133 *
3134 * [u4]: number of heaps
3135 *
3136 * For each heap:
3137 * [u4]: heap ID
3138 * [u8]: timestamp in ms since Unix epoch
3139 * [u1]: capture reason (same as 'when' value from server)
3140 * [u4]: max heap size in bytes (-Xmx)
3141 * [u4]: current heap size in bytes
3142 * [u4]: current number of bytes allocated
3143 * [u4]: current number of objects allocated
3144 */
3145 uint8_t heap_count = 1;
Ian Rogers1d54e732013-05-02 21:10:01 -07003146 gc::Heap* heap = Runtime::Current()->GetHeap();
Elliott Hughes21f32d72011-11-09 17:44:13 -08003147 std::vector<uint8_t> bytes;
Elliott Hughes545a0642011-11-08 19:10:03 -08003148 JDWP::Append4BE(bytes, heap_count);
3149 JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap).
3150 JDWP::Append8BE(bytes, MilliTime());
3151 JDWP::Append1BE(bytes, reason);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003152 JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes.
3153 JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes.
3154 JDWP::Append4BE(bytes, heap->GetBytesAllocated());
3155 JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
Elliott Hughes21f32d72011-11-09 17:44:13 -08003156 CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
3157 Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes);
Elliott Hughes767a1472011-10-26 18:49:02 -07003158}
3159
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003160enum HpsgSolidity {
3161 SOLIDITY_FREE = 0,
3162 SOLIDITY_HARD = 1,
3163 SOLIDITY_SOFT = 2,
3164 SOLIDITY_WEAK = 3,
3165 SOLIDITY_PHANTOM = 4,
3166 SOLIDITY_FINALIZABLE = 5,
3167 SOLIDITY_SWEEP = 6,
3168};
3169
3170enum HpsgKind {
3171 KIND_OBJECT = 0,
3172 KIND_CLASS_OBJECT = 1,
3173 KIND_ARRAY_1 = 2,
3174 KIND_ARRAY_2 = 3,
3175 KIND_ARRAY_4 = 4,
3176 KIND_ARRAY_8 = 5,
3177 KIND_UNKNOWN = 6,
3178 KIND_NATIVE = 7,
3179};
3180
3181#define HPSG_PARTIAL (1<<7)
3182#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
3183
Ian Rogers30fab402012-01-23 15:43:46 -08003184class HeapChunkContext {
3185 public:
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003186 // Maximum chunk size. Obtain this from the formula:
3187 // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
3188 HeapChunkContext(bool merge, bool native)
Ian Rogers30fab402012-01-23 15:43:46 -08003189 : buf_(16384 - 16),
3190 type_(0),
3191 merge_(merge) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003192 Reset();
3193 if (native) {
Ian Rogers30fab402012-01-23 15:43:46 -08003194 type_ = CHUNK_TYPE("NHSG");
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003195 } else {
Ian Rogers30fab402012-01-23 15:43:46 -08003196 type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003197 }
3198 }
3199
3200 ~HeapChunkContext() {
Ian Rogers30fab402012-01-23 15:43:46 -08003201 if (p_ > &buf_[0]) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003202 Flush();
3203 }
3204 }
3205
3206 void EnsureHeader(const void* chunk_ptr) {
Ian Rogers30fab402012-01-23 15:43:46 -08003207 if (!needHeader_) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003208 return;
3209 }
3210
3211 // Start a new HPSx chunk.
Ian Rogers30fab402012-01-23 15:43:46 -08003212 JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap).
3213 JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes.
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003214
Ian Rogers30fab402012-01-23 15:43:46 -08003215 JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start.
3216 JDWP::Write4BE(&p_, 0); // offset of this piece (relative to the virtual address).
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003217 // [u4]: length of piece, in allocation units
3218 // We won't know this until we're done, so save the offset and stuff in a dummy value.
Ian Rogers30fab402012-01-23 15:43:46 -08003219 pieceLenField_ = p_;
3220 JDWP::Write4BE(&p_, 0x55555555);
3221 needHeader_ = false;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003222 }
3223
Ian Rogersb726dcb2012-09-05 08:57:23 -07003224 void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd636b062013-01-18 17:51:18 -08003225 if (pieceLenField_ == NULL) {
3226 // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
3227 CHECK(needHeader_);
3228 return;
3229 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003230 // Patch the "length of piece" field.
Ian Rogers30fab402012-01-23 15:43:46 -08003231 CHECK_LE(&buf_[0], pieceLenField_);
3232 CHECK_LE(pieceLenField_, p_);
3233 JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003234
Ian Rogers30fab402012-01-23 15:43:46 -08003235 Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003236 Reset();
3237 }
3238
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003239 static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07003240 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3241 Locks::mutator_lock_) {
Ian Rogers30fab402012-01-23 15:43:46 -08003242 reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes);
Elliott Hughesa2155262011-11-16 16:26:58 -08003243 }
3244
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003245 private:
Elliott Hughesa2155262011-11-16 16:26:58 -08003246 enum { ALLOCATION_UNIT_SIZE = 8 };
3247
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003248 void Reset() {
Ian Rogers30fab402012-01-23 15:43:46 -08003249 p_ = &buf_[0];
Ian Rogers15bf2d32012-08-28 17:33:04 -07003250 startOfNextMemoryChunk_ = NULL;
Ian Rogers30fab402012-01-23 15:43:46 -08003251 totalAllocationUnits_ = 0;
3252 needHeader_ = true;
3253 pieceLenField_ = NULL;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003254 }
3255
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003256 void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes)
Ian Rogersb726dcb2012-09-05 08:57:23 -07003257 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3258 Locks::mutator_lock_) {
Ian Rogers30fab402012-01-23 15:43:46 -08003259 // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
3260 // in the following code not to allocate memory, by ensuring buf_ is of the correct size
Ian Rogers15bf2d32012-08-28 17:33:04 -07003261 if (used_bytes == 0) {
3262 if (start == NULL) {
3263 // Reset for start of new heap.
3264 startOfNextMemoryChunk_ = NULL;
3265 Flush();
3266 }
3267 // Only process in use memory so that free region information
3268 // also includes dlmalloc book keeping.
Elliott Hughesa2155262011-11-16 16:26:58 -08003269 return;
Elliott Hughesa2155262011-11-16 16:26:58 -08003270 }
3271
Ian Rogers15bf2d32012-08-28 17:33:04 -07003272 /* If we're looking at the native heap, we'll just return
3273 * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks
3274 */
3275 bool native = type_ == CHUNK_TYPE("NHSG");
3276
3277 if (startOfNextMemoryChunk_ != NULL) {
3278 // Transmit any pending free memory. Native free memory of
3279 // over kMaxFreeLen could be because of the use of mmaps, so
3280 // don't report. If not free memory then start a new segment.
3281 bool flush = true;
3282 if (start > startOfNextMemoryChunk_) {
3283 const size_t kMaxFreeLen = 2 * kPageSize;
3284 void* freeStart = startOfNextMemoryChunk_;
3285 void* freeEnd = start;
3286 size_t freeLen = (char*)freeEnd - (char*)freeStart;
3287 if (!native || freeLen < kMaxFreeLen) {
3288 AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), freeStart, freeLen);
3289 flush = false;
3290 }
3291 }
3292 if (flush) {
3293 startOfNextMemoryChunk_ = NULL;
3294 Flush();
3295 }
3296 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003297 const mirror::Object* obj = reinterpret_cast<const mirror::Object*>(start);
Elliott Hughesa2155262011-11-16 16:26:58 -08003298
3299 // Determine the type of this chunk.
3300 // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
3301 // If it's the same, we should combine them.
Ian Rogers15bf2d32012-08-28 17:33:04 -07003302 uint8_t state = ExamineObject(obj, native);
3303 // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
3304 // allocation then the first sizeof(size_t) may belong to it.
3305 const size_t dlMallocOverhead = sizeof(size_t);
3306 AppendChunk(state, start, used_bytes + dlMallocOverhead);
3307 startOfNextMemoryChunk_ = (char*)start + used_bytes + dlMallocOverhead;
3308 }
Elliott Hughesa2155262011-11-16 16:26:58 -08003309
Ian Rogers15bf2d32012-08-28 17:33:04 -07003310 void AppendChunk(uint8_t state, void* ptr, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07003311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07003312 // Make sure there's enough room left in the buffer.
3313 // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
3314 // 17 bytes for any header.
3315 size_t needed = (((length/ALLOCATION_UNIT_SIZE + 255) / 256) * 2) + 17;
3316 size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
3317 if (bytesLeft < needed) {
3318 Flush();
3319 }
3320
3321 bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
3322 if (bytesLeft < needed) {
3323 LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
3324 << needed << " bytes)";
3325 return;
3326 }
3327 EnsureHeader(ptr);
Elliott Hughesa2155262011-11-16 16:26:58 -08003328 // Write out the chunk description.
Ian Rogers15bf2d32012-08-28 17:33:04 -07003329 length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units.
3330 totalAllocationUnits_ += length;
3331 while (length > 256) {
Ian Rogers30fab402012-01-23 15:43:46 -08003332 *p_++ = state | HPSG_PARTIAL;
3333 *p_++ = 255; // length - 1
Ian Rogers15bf2d32012-08-28 17:33:04 -07003334 length -= 256;
Elliott Hughesa2155262011-11-16 16:26:58 -08003335 }
Ian Rogers30fab402012-01-23 15:43:46 -08003336 *p_++ = state;
Ian Rogers15bf2d32012-08-28 17:33:04 -07003337 *p_++ = length - 1;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003338 }
3339
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003340 uint8_t ExamineObject(const mirror::Object* o, bool is_native_heap)
Ian Rogersb726dcb2012-09-05 08:57:23 -07003341 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Elliott Hughesa2155262011-11-16 16:26:58 -08003342 if (o == NULL) {
3343 return HPSG_STATE(SOLIDITY_FREE, 0);
3344 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003345
Elliott Hughesa2155262011-11-16 16:26:58 -08003346 // It's an allocated chunk. Figure out what it is.
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003347
Elliott Hughesa2155262011-11-16 16:26:58 -08003348 // If we're looking at the native heap, we'll just return
3349 // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003350 if (is_native_heap) {
Elliott Hughesa2155262011-11-16 16:26:58 -08003351 return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
3352 }
3353
Ian Rogers5bfa60f2012-09-02 21:17:56 -07003354 if (!Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07003355 return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003356 }
3357
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003358 mirror::Class* c = o->GetClass();
Elliott Hughesa2155262011-11-16 16:26:58 -08003359 if (c == NULL) {
3360 // The object was probably just created but hasn't been initialized yet.
3361 return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
3362 }
3363
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003364 if (!Runtime::Current()->GetHeap()->IsHeapAddress(c)) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07003365 LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
Elliott Hughesa2155262011-11-16 16:26:58 -08003366 return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
3367 }
3368
3369 if (c->IsClassClass()) {
3370 return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
3371 }
3372
3373 if (c->IsArrayClass()) {
3374 if (o->IsObjectArray()) {
3375 return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3376 }
3377 switch (c->GetComponentSize()) {
3378 case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
3379 case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
3380 case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3381 case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
3382 }
3383 }
3384
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003385 return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
3386 }
3387
Ian Rogers30fab402012-01-23 15:43:46 -08003388 std::vector<uint8_t> buf_;
3389 uint8_t* p_;
3390 uint8_t* pieceLenField_;
Ian Rogers15bf2d32012-08-28 17:33:04 -07003391 void* startOfNextMemoryChunk_;
Ian Rogers30fab402012-01-23 15:43:46 -08003392 size_t totalAllocationUnits_;
3393 uint32_t type_;
3394 bool merge_;
3395 bool needHeader_;
3396
Elliott Hughesa2155262011-11-16 16:26:58 -08003397 DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
3398};
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003399
3400void Dbg::DdmSendHeapSegments(bool native) {
3401 Dbg::HpsgWhen when;
3402 Dbg::HpsgWhat what;
3403 if (!native) {
3404 when = gDdmHpsgWhen;
3405 what = gDdmHpsgWhat;
3406 } else {
3407 when = gDdmNhsgWhen;
3408 what = gDdmNhsgWhat;
3409 }
3410 if (when == HPSG_WHEN_NEVER) {
3411 return;
3412 }
3413
3414 // Figure out what kind of chunks we'll be sending.
3415 CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what);
3416
3417 // First, send a heap start chunk.
3418 uint8_t heap_id[4];
3419 JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap).
3420 Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id);
3421
3422 // Send a series of heap segment chunks.
Elliott Hughesa2155262011-11-16 16:26:58 -08003423 HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native);
3424 if (native) {
Ian Rogers1d54e732013-05-02 21:10:01 -07003425 dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context);
Elliott Hughesa2155262011-11-16 16:26:58 -08003426 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -07003427 gc::Heap* heap = Runtime::Current()->GetHeap();
3428 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07003429 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08003430 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -07003431 typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
3432 for (It cur = spaces.begin(), end = spaces.end(); cur != end; ++cur) {
3433 if ((*cur)->IsDlMallocSpace()) {
3434 (*cur)->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07003435 }
3436 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07003437 // Walk the large objects, these are not in the AllocSpace.
3438 heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
Elliott Hughesa2155262011-11-16 16:26:58 -08003439 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003440
3441 // Finally, send a heap end chunk.
3442 Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id);
Elliott Hughes767a1472011-10-26 18:49:02 -07003443}
3444
Elliott Hughesb1a58792013-07-11 18:10:58 -07003445static size_t GetAllocTrackerMax() {
3446#ifdef HAVE_ANDROID_OS
3447 // Check whether there's a system property overriding the number of records.
3448 const char* propertyName = "dalvik.vm.allocTrackerMax";
3449 char allocRecordMaxString[PROPERTY_VALUE_MAX];
3450 if (property_get(propertyName, allocRecordMaxString, "") > 0) {
3451 char* end;
3452 size_t value = strtoul(allocRecordMaxString, &end, 10);
3453 if (*end != '\0') {
3454 ALOGE("Ignoring %s '%s' --- invalid", propertyName, allocRecordMaxString);
3455 return kDefaultNumAllocRecords;
3456 }
3457 if (!IsPowerOfTwo(value)) {
3458 ALOGE("Ignoring %s '%s' --- not power of two", propertyName, allocRecordMaxString);
3459 return kDefaultNumAllocRecords;
3460 }
3461 return value;
3462 }
3463#endif
3464 return kDefaultNumAllocRecords;
3465}
3466
Elliott Hughes545a0642011-11-08 19:10:03 -08003467void Dbg::SetAllocTrackingEnabled(bool enabled) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003468 MutexLock mu(Thread::Current(), gAllocTrackerLock);
Elliott Hughes545a0642011-11-08 19:10:03 -08003469 if (enabled) {
3470 if (recent_allocation_records_ == NULL) {
Elliott Hughesb1a58792013-07-11 18:10:58 -07003471 gAllocRecordMax = GetAllocTrackerMax();
3472 LOG(INFO) << "Enabling alloc tracker (" << gAllocRecordMax << " entries of "
3473 << kMaxAllocRecordStackDepth << " frames, taking "
3474 << PrettySize(sizeof(AllocRecord) * gAllocRecordMax) << ")";
Elliott Hughes545a0642011-11-08 19:10:03 -08003475 gAllocRecordHead = gAllocRecordCount = 0;
Elliott Hughesb1a58792013-07-11 18:10:58 -07003476 recent_allocation_records_ = new AllocRecord[gAllocRecordMax];
Elliott Hughes545a0642011-11-08 19:10:03 -08003477 CHECK(recent_allocation_records_ != NULL);
3478 }
3479 } else {
3480 delete[] recent_allocation_records_;
3481 recent_allocation_records_ = NULL;
3482 }
3483}
3484
Ian Rogers0399dde2012-06-06 17:09:28 -07003485struct AllocRecordStackVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08003486 AllocRecordStackVisitor(Thread* thread, AllocRecord* record)
Ian Rogersb726dcb2012-09-05 08:57:23 -07003487 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08003488 : StackVisitor(thread, NULL), record(record), depth(0) {}
Elliott Hughes545a0642011-11-08 19:10:03 -08003489
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003490 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
3491 // annotalysis.
3492 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughes545a0642011-11-08 19:10:03 -08003493 if (depth >= kMaxAllocRecordStackDepth) {
Elliott Hughes530fa002012-03-12 11:44:49 -07003494 return false;
Elliott Hughes545a0642011-11-08 19:10:03 -08003495 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003496 mirror::AbstractMethod* m = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -07003497 if (!m->IsRuntimeMethod()) {
3498 record->stack[depth].method = m;
3499 record->stack[depth].dex_pc = GetDexPc();
Elliott Hughes530fa002012-03-12 11:44:49 -07003500 ++depth;
Elliott Hughes545a0642011-11-08 19:10:03 -08003501 }
Elliott Hughes530fa002012-03-12 11:44:49 -07003502 return true;
Elliott Hughes545a0642011-11-08 19:10:03 -08003503 }
3504
3505 ~AllocRecordStackVisitor() {
3506 // Clear out any unused stack trace elements.
3507 for (; depth < kMaxAllocRecordStackDepth; ++depth) {
3508 record->stack[depth].method = NULL;
Ian Rogers0399dde2012-06-06 17:09:28 -07003509 record->stack[depth].dex_pc = 0;
Elliott Hughes545a0642011-11-08 19:10:03 -08003510 }
3511 }
3512
3513 AllocRecord* record;
3514 size_t depth;
3515};
3516
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003517void Dbg::RecordAllocation(mirror::Class* type, size_t byte_count) {
Elliott Hughes545a0642011-11-08 19:10:03 -08003518 Thread* self = Thread::Current();
3519 CHECK(self != NULL);
3520
Ian Rogers50b35e22012-10-04 10:09:15 -07003521 MutexLock mu(self, gAllocTrackerLock);
Elliott Hughes545a0642011-11-08 19:10:03 -08003522 if (recent_allocation_records_ == NULL) {
3523 return;
3524 }
3525
3526 // Advance and clip.
Elliott Hughesb1a58792013-07-11 18:10:58 -07003527 if (++gAllocRecordHead == gAllocRecordMax) {
Elliott Hughes545a0642011-11-08 19:10:03 -08003528 gAllocRecordHead = 0;
3529 }
3530
3531 // Fill in the basics.
3532 AllocRecord* record = &recent_allocation_records_[gAllocRecordHead];
3533 record->type = type;
3534 record->byte_count = byte_count;
3535 record->thin_lock_id = self->GetThinLockId();
3536
3537 // Fill in the stack trace.
Ian Rogers7a22fa62013-01-23 12:16:16 -08003538 AllocRecordStackVisitor visitor(self, record);
Ian Rogers0399dde2012-06-06 17:09:28 -07003539 visitor.WalkStack();
Elliott Hughes545a0642011-11-08 19:10:03 -08003540
Elliott Hughesb1a58792013-07-11 18:10:58 -07003541 if (gAllocRecordCount < gAllocRecordMax) {
Elliott Hughes545a0642011-11-08 19:10:03 -08003542 ++gAllocRecordCount;
3543 }
3544}
3545
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003546// Returns the index of the head element.
3547//
3548// We point at the most-recently-written record, so if gAllocRecordCount is 1
3549// we want to use the current element. Take "head+1" and subtract count
3550// from it.
3551//
3552// We need to handle underflow in our circular buffer, so we add
Elliott Hughesb1a58792013-07-11 18:10:58 -07003553// gAllocRecordMax and then mask it back down.
Elliott Hughesf8349362012-06-18 15:00:06 -07003554static inline int HeadIndex() EXCLUSIVE_LOCKS_REQUIRED(gAllocTrackerLock) {
Elliott Hughesb1a58792013-07-11 18:10:58 -07003555 return (gAllocRecordHead+1 + gAllocRecordMax - gAllocRecordCount) & (gAllocRecordMax-1);
Elliott Hughes545a0642011-11-08 19:10:03 -08003556}
3557
3558void Dbg::DumpRecentAllocations() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003559 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -07003560 MutexLock mu(soa.Self(), gAllocTrackerLock);
Elliott Hughes545a0642011-11-08 19:10:03 -08003561 if (recent_allocation_records_ == NULL) {
3562 LOG(INFO) << "Not recording tracked allocations";
3563 return;
3564 }
3565
3566 // "i" is the head of the list. We want to start at the end of the
3567 // list and move forward to the tail.
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003568 size_t i = HeadIndex();
Elliott Hughes545a0642011-11-08 19:10:03 -08003569 size_t count = gAllocRecordCount;
3570
3571 LOG(INFO) << "Tracked allocations, (head=" << gAllocRecordHead << " count=" << count << ")";
3572 while (count--) {
3573 AllocRecord* record = &recent_allocation_records_[i];
3574
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003575 LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->thin_lock_id, record->byte_count)
Elliott Hughes545a0642011-11-08 19:10:03 -08003576 << PrettyClass(record->type);
3577
3578 for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003579 const mirror::AbstractMethod* m = record->stack[stack_frame].method;
Elliott Hughes545a0642011-11-08 19:10:03 -08003580 if (m == NULL) {
3581 break;
3582 }
3583 LOG(INFO) << " " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber();
3584 }
3585
3586 // pause periodically to help logcat catch up
3587 if ((count % 5) == 0) {
3588 usleep(40000);
3589 }
3590
Elliott Hughesb1a58792013-07-11 18:10:58 -07003591 i = (i + 1) & (gAllocRecordMax-1);
Elliott Hughes545a0642011-11-08 19:10:03 -08003592 }
3593}
3594
3595class StringTable {
3596 public:
3597 StringTable() {
3598 }
3599
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003600 void Add(const char* s) {
Elliott Hughes545a0642011-11-08 19:10:03 -08003601 table_.insert(s);
3602 }
3603
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003604 size_t IndexOf(const char* s) const {
3605 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
3606 It it = table_.find(s);
3607 if (it == table_.end()) {
3608 LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
3609 }
3610 return std::distance(table_.begin(), it);
Elliott Hughes545a0642011-11-08 19:10:03 -08003611 }
3612
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003613 size_t Size() const {
Elliott Hughes545a0642011-11-08 19:10:03 -08003614 return table_.size();
3615 }
3616
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003617 void WriteTo(std::vector<uint8_t>& bytes) const {
3618 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
Elliott Hughes545a0642011-11-08 19:10:03 -08003619 for (It it = table_.begin(); it != table_.end(); ++it) {
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003620 const char* s = (*it).c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003621 size_t s_len = CountModifiedUtf8Chars(s);
3622 UniquePtr<uint16_t> s_utf16(new uint16_t[s_len]);
3623 ConvertModifiedUtf8ToUtf16(s_utf16.get(), s);
3624 JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
Elliott Hughes545a0642011-11-08 19:10:03 -08003625 }
3626 }
3627
3628 private:
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003629 std::set<std::string> table_;
Elliott Hughes545a0642011-11-08 19:10:03 -08003630 DISALLOW_COPY_AND_ASSIGN(StringTable);
3631};
3632
3633/*
3634 * The data we send to DDMS contains everything we have recorded.
3635 *
3636 * Message header (all values big-endian):
3637 * (1b) message header len (to allow future expansion); includes itself
3638 * (1b) entry header len
3639 * (1b) stack frame len
3640 * (2b) number of entries
3641 * (4b) offset to string table from start of message
3642 * (2b) number of class name strings
3643 * (2b) number of method name strings
3644 * (2b) number of source file name strings
3645 * For each entry:
3646 * (4b) total allocation size
Elliott Hughes221229c2013-01-08 18:17:50 -08003647 * (2b) thread id
Elliott Hughes545a0642011-11-08 19:10:03 -08003648 * (2b) allocated object's class name index
3649 * (1b) stack depth
3650 * For each stack frame:
3651 * (2b) method's class name
3652 * (2b) method name
3653 * (2b) method source file
3654 * (2b) line number, clipped to 32767; -2 if native; -1 if no source
3655 * (xb) class name strings
3656 * (xb) method name strings
3657 * (xb) source file strings
3658 *
3659 * As with other DDM traffic, strings are sent as a 4-byte length
3660 * followed by UTF-16 data.
3661 *
3662 * We send up 16-bit unsigned indexes into string tables. In theory there
Elliott Hughesb1a58792013-07-11 18:10:58 -07003663 * can be (kMaxAllocRecordStackDepth * gAllocRecordMax) unique strings in
Elliott Hughes545a0642011-11-08 19:10:03 -08003664 * each table, but in practice there should be far fewer.
3665 *
3666 * The chief reason for using a string table here is to keep the size of
3667 * the DDMS message to a minimum. This is partly to make the protocol
3668 * efficient, but also because we have to form the whole thing up all at
3669 * once in a memory buffer.
3670 *
3671 * We use separate string tables for class names, method names, and source
3672 * files to keep the indexes small. There will generally be no overlap
3673 * between the contents of these tables.
3674 */
3675jbyteArray Dbg::GetRecentAllocations() {
3676 if (false) {
3677 DumpRecentAllocations();
3678 }
3679
Ian Rogers50b35e22012-10-04 10:09:15 -07003680 Thread* self = Thread::Current();
Elliott Hughes545a0642011-11-08 19:10:03 -08003681 std::vector<uint8_t> bytes;
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003682 {
3683 MutexLock mu(self, gAllocTrackerLock);
3684 //
3685 // Part 1: generate string tables.
3686 //
3687 StringTable class_names;
3688 StringTable method_names;
3689 StringTable filenames;
Elliott Hughes545a0642011-11-08 19:10:03 -08003690
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003691 int count = gAllocRecordCount;
3692 int idx = HeadIndex();
3693 while (count--) {
3694 AllocRecord* record = &recent_allocation_records_[idx];
Elliott Hughes545a0642011-11-08 19:10:03 -08003695
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003696 class_names.Add(ClassHelper(record->type).GetDescriptor());
Elliott Hughes545a0642011-11-08 19:10:03 -08003697
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003698 MethodHelper mh;
3699 for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
3700 mirror::AbstractMethod* m = record->stack[i].method;
3701 if (m != NULL) {
3702 mh.ChangeMethod(m);
3703 class_names.Add(mh.GetDeclaringClassDescriptor());
3704 method_names.Add(mh.GetName());
3705 filenames.Add(mh.GetDeclaringClassSourceFile());
3706 }
3707 }
Elliott Hughes545a0642011-11-08 19:10:03 -08003708
Elliott Hughesb1a58792013-07-11 18:10:58 -07003709 idx = (idx + 1) & (gAllocRecordMax-1);
Elliott Hughes545a0642011-11-08 19:10:03 -08003710 }
3711
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003712 LOG(INFO) << "allocation records: " << gAllocRecordCount;
3713
3714 //
3715 // Part 2: Generate the output and store it in the buffer.
3716 //
3717
3718 // (1b) message header len (to allow future expansion); includes itself
3719 // (1b) entry header len
3720 // (1b) stack frame len
3721 const int kMessageHeaderLen = 15;
3722 const int kEntryHeaderLen = 9;
3723 const int kStackFrameLen = 8;
3724 JDWP::Append1BE(bytes, kMessageHeaderLen);
3725 JDWP::Append1BE(bytes, kEntryHeaderLen);
3726 JDWP::Append1BE(bytes, kStackFrameLen);
3727
3728 // (2b) number of entries
3729 // (4b) offset to string table from start of message
3730 // (2b) number of class name strings
3731 // (2b) number of method name strings
3732 // (2b) number of source file name strings
3733 JDWP::Append2BE(bytes, gAllocRecordCount);
3734 size_t string_table_offset = bytes.size();
3735 JDWP::Append4BE(bytes, 0); // We'll patch this later...
3736 JDWP::Append2BE(bytes, class_names.Size());
3737 JDWP::Append2BE(bytes, method_names.Size());
3738 JDWP::Append2BE(bytes, filenames.Size());
3739
3740 count = gAllocRecordCount;
3741 idx = HeadIndex();
3742 ClassHelper kh;
3743 while (count--) {
3744 // For each entry:
3745 // (4b) total allocation size
3746 // (2b) thread id
3747 // (2b) allocated object's class name index
3748 // (1b) stack depth
3749 AllocRecord* record = &recent_allocation_records_[idx];
3750 size_t stack_depth = record->GetDepth();
3751 kh.ChangeClass(record->type);
3752 size_t allocated_object_class_name_index = class_names.IndexOf(kh.GetDescriptor());
3753 JDWP::Append4BE(bytes, record->byte_count);
3754 JDWP::Append2BE(bytes, record->thin_lock_id);
3755 JDWP::Append2BE(bytes, allocated_object_class_name_index);
3756 JDWP::Append1BE(bytes, stack_depth);
3757
3758 MethodHelper mh;
3759 for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
3760 // For each stack frame:
3761 // (2b) method's class name
3762 // (2b) method name
3763 // (2b) method source file
3764 // (2b) line number, clipped to 32767; -2 if native; -1 if no source
3765 mh.ChangeMethod(record->stack[stack_frame].method);
3766 size_t class_name_index = class_names.IndexOf(mh.GetDeclaringClassDescriptor());
3767 size_t method_name_index = method_names.IndexOf(mh.GetName());
3768 size_t file_name_index = filenames.IndexOf(mh.GetDeclaringClassSourceFile());
3769 JDWP::Append2BE(bytes, class_name_index);
3770 JDWP::Append2BE(bytes, method_name_index);
3771 JDWP::Append2BE(bytes, file_name_index);
3772 JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber());
3773 }
3774
Elliott Hughesb1a58792013-07-11 18:10:58 -07003775 idx = (idx + 1) & (gAllocRecordMax-1);
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003776 }
3777
3778 // (xb) class name strings
3779 // (xb) method name strings
3780 // (xb) source file strings
3781 JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
3782 class_names.WriteTo(bytes);
3783 method_names.WriteTo(bytes);
3784 filenames.WriteTo(bytes);
Elliott Hughes545a0642011-11-08 19:10:03 -08003785 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003786 JNIEnv* env = self->GetJniEnv();
Elliott Hughes545a0642011-11-08 19:10:03 -08003787 jbyteArray result = env->NewByteArray(bytes.size());
3788 if (result != NULL) {
3789 env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
3790 }
3791 return result;
3792}
3793
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003794} // namespace art