blob: 4e89838d0f21cddf11ff0f966c2c47d65dcf514b [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
Elliott Hughesb1a58792013-07-11 18:10:58 -070023#include "cutils/properties.h"
Elliott Hughes545a0642011-11-08 19:10:03 -080024#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070027#include "dex_instruction.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
29#include "gc/space/large_object_space.h"
30#include "gc/space/space-inl.h"
Jeff Hao5d917302013-02-27 17:57:33 -080031#include "invoke_arg_array_builder.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080032#include "jdwp/object_registry.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/abstract_method-inl.h"
34#include "mirror/class.h"
35#include "mirror/class-inl.h"
36#include "mirror/class_loader.h"
37#include "mirror/field-inl.h"
38#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
40#include "mirror/throwable.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080041#include "oat/runtime/context.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080042#include "object_utils.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070043#include "safe_map.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080044#include "scoped_thread_state_change.h"
Elliott Hughes6a5bd492011-10-28 14:33:57 -070045#include "ScopedLocalRef.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070046#include "ScopedPrimitiveArray.h"
Ian Rogers1f539342012-10-03 21:09:42 -070047#include "sirt_ref.h"
Elliott Hughes47fce012011-10-25 18:37:19 -070048#include "stack_indirect_reference_table.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070049#include "thread_list.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080050#include "throw_location.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070052#include "well_known_classes.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070053
Elliott Hughes872d4ec2011-10-21 17:07:15 -070054namespace art {
55
Elliott Hughes545a0642011-11-08 19:10:03 -080056static const size_t kMaxAllocRecordStackDepth = 16; // Max 255.
Elliott Hughesb1a58792013-07-11 18:10:58 -070057static const size_t kDefaultNumAllocRecords = 64*1024; // Must be a power of 2.
Elliott Hughes475fc232011-10-25 15:00:35 -070058
Elliott Hughes545a0642011-11-08 19:10:03 -080059struct AllocRecordStackTraceElement {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 mirror::AbstractMethod* method;
Ian Rogers0399dde2012-06-06 17:09:28 -070061 uint32_t dex_pc;
Elliott Hughes545a0642011-11-08 19:10:03 -080062
Ian Rogersb726dcb2012-09-05 08:57:23 -070063 int32_t LineNumber() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -070064 return MethodHelper(method).GetLineNumFromDexPC(dex_pc);
Elliott Hughes545a0642011-11-08 19:10:03 -080065 }
66};
67
68struct AllocRecord {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 mirror::Class* type;
Elliott Hughes545a0642011-11-08 19:10:03 -080070 size_t byte_count;
71 uint16_t thin_lock_id;
72 AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth]; // Unused entries have NULL method.
73
74 size_t GetDepth() {
75 size_t depth = 0;
76 while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) {
77 ++depth;
78 }
79 return depth;
80 }
81};
82
Elliott Hughes86964332012-02-15 19:37:42 -080083struct Breakpoint {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 mirror::AbstractMethod* method;
Elliott Hughesa656a0f2012-02-21 18:03:44 -080085 uint32_t dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 Breakpoint(mirror::AbstractMethod* method, uint32_t dex_pc) : method(method), dex_pc(dex_pc) {}
Elliott Hughes86964332012-02-15 19:37:42 -080087};
88
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs)
Ian Rogersb726dcb2012-09-05 08:57:23 -070090 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes229feb72012-02-23 13:33:29 -080091 os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.method).c_str(), rhs.dex_pc);
Elliott Hughes86964332012-02-15 19:37:42 -080092 return os;
93}
94
95struct SingleStepControl {
96 // Are we single-stepping right now?
97 bool is_active;
98 Thread* thread;
99
100 JDWP::JdwpStepSize step_size;
101 JDWP::JdwpStepDepth step_depth;
102
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 const mirror::AbstractMethod* method;
Elliott Hughes2435a572012-02-17 16:07:41 -0800104 int32_t line_number; // Or -1 for native methods.
105 std::set<uint32_t> dex_pcs;
Elliott Hughes86964332012-02-15 19:37:42 -0800106 int stack_depth;
107};
108
Ian Rogers62d6c772013-02-27 08:32:07 -0800109class DebugInstrumentationListener : public instrumentation::InstrumentationListener {
110 public:
111 DebugInstrumentationListener() {}
112 virtual ~DebugInstrumentationListener() {}
113
114 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
115 const mirror::AbstractMethod* method, uint32_t dex_pc)
116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
117 if (method->IsNative()) {
118 // TODO: post location events is a suspension point and native method entry stubs aren't.
119 return;
120 }
121 Dbg::PostLocationEvent(method, 0, this_object, Dbg::kMethodEntry);
122 }
123
124 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
125 const mirror::AbstractMethod* method,
126 uint32_t dex_pc, const JValue& return_value)
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
128 UNUSED(return_value);
129 if (method->IsNative()) {
130 // TODO: post location events is a suspension point and native method entry stubs aren't.
131 return;
132 }
133 Dbg::PostLocationEvent(method, dex_pc, this_object, Dbg::kMethodExit);
134 }
135
136 virtual void MethodUnwind(Thread* thread, const mirror::AbstractMethod* method,
137 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
138 // We're not recorded to listen to this kind of event, so complain.
139 LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method)
140 << " " << dex_pc;
141 }
142
143 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
144 const mirror::AbstractMethod* method, uint32_t new_dex_pc)
145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
146 Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc);
147 }
148
149 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
150 mirror::AbstractMethod* catch_method, uint32_t catch_dex_pc,
151 mirror::Throwable* exception_object)
152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
153 Dbg::PostException(thread, throw_location, catch_method, catch_dex_pc, exception_object);
154 }
155
156} gDebugInstrumentationListener;
157
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700158// JDWP is allowed unless the Zygote forbids it.
159static bool gJdwpAllowed = true;
160
Elliott Hughesc0f09332012-03-26 13:27:06 -0700161// Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line?
Elliott Hughes3bb81562011-10-21 18:52:59 -0700162static bool gJdwpConfigured = false;
163
Elliott Hughesc0f09332012-03-26 13:27:06 -0700164// Broken-down JDWP options. (Only valid if IsJdwpConfigured() is true.)
Elliott Hughes376a7a02011-10-24 18:35:55 -0700165static JDWP::JdwpOptions gJdwpOptions;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700166
167// Runtime JDWP state.
168static JDWP::JdwpState* gJdwpState = NULL;
169static bool gDebuggerConnected; // debugger or DDMS is connected.
170static bool gDebuggerActive; // debugger is making requests.
Elliott Hughes86964332012-02-15 19:37:42 -0800171static bool gDisposed; // debugger called VirtualMachine.Dispose, so we should drop the connection.
Elliott Hughes3bb81562011-10-21 18:52:59 -0700172
Elliott Hughes47fce012011-10-25 18:37:19 -0700173static bool gDdmThreadNotification = false;
174
Elliott Hughes767a1472011-10-26 18:49:02 -0700175// DDMS GC-related settings.
176static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
177static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
178static Dbg::HpsgWhat gDdmHpsgWhat;
179static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
180static Dbg::HpsgWhat gDdmNhsgWhat;
181
Elliott Hughes475fc232011-10-25 15:00:35 -0700182static ObjectRegistry* gRegistry = NULL;
183
Elliott Hughes545a0642011-11-08 19:10:03 -0800184// Recent allocation tracking.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700185static Mutex gAllocTrackerLock DEFAULT_MUTEX_ACQUIRED_AFTER ("AllocTracker lock");
Elliott Hughesf8349362012-06-18 15:00:06 -0700186AllocRecord* Dbg::recent_allocation_records_ PT_GUARDED_BY(gAllocTrackerLock) = NULL; // TODO: CircularBuffer<AllocRecord>
Elliott Hughesb1a58792013-07-11 18:10:58 -0700187static size_t gAllocRecordMax GUARDED_BY(gAllocTrackerLock) = 0;
Elliott Hughesf8349362012-06-18 15:00:06 -0700188static size_t gAllocRecordHead GUARDED_BY(gAllocTrackerLock) = 0;
189static size_t gAllocRecordCount GUARDED_BY(gAllocTrackerLock) = 0;
Elliott Hughes545a0642011-11-08 19:10:03 -0800190
Elliott Hughes86964332012-02-15 19:37:42 -0800191// Breakpoints and single-stepping.
jeffhao09bfc6a2012-12-11 18:11:43 -0800192static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_);
193static SingleStepControl gSingleStepControl GUARDED_BY(Locks::breakpoint_lock_);
Elliott Hughes86964332012-02-15 19:37:42 -0800194
Ian Rogers62d6c772013-02-27 08:32:07 -0800195static bool IsBreakpoint(const mirror::AbstractMethod* m, uint32_t dex_pc)
jeffhao09bfc6a2012-12-11 18:11:43 -0800196 LOCKS_EXCLUDED(Locks::breakpoint_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao09bfc6a2012-12-11 18:11:43 -0800198 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Elliott Hughes86964332012-02-15 19:37:42 -0800199 for (size_t i = 0; i < gBreakpoints.size(); ++i) {
Elliott Hughesa656a0f2012-02-21 18:03:44 -0800200 if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == dex_pc) {
Elliott Hughes86964332012-02-15 19:37:42 -0800201 VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
202 return true;
203 }
204 }
205 return false;
206}
207
Elliott Hughes9e0c1752013-01-09 14:02:58 -0800208static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) {
209 MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
210 // A thread may be suspended for GC; in this code, we really want to know whether
211 // there's a debugger suspension active.
212 return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
213}
214
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215static mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700216 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800218 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800219 status = JDWP::ERR_INVALID_OBJECT;
220 return NULL;
221 }
222 if (!o->IsArrayInstance()) {
223 status = JDWP::ERR_INVALID_ARRAY;
224 return NULL;
225 }
226 status = JDWP::ERR_NONE;
227 return o->AsArray();
228}
229
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800233 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800234 status = JDWP::ERR_INVALID_OBJECT;
235 return NULL;
236 }
237 if (!o->IsClass()) {
238 status = JDWP::ERR_INVALID_CLASS;
239 return NULL;
240 }
241 status = JDWP::ERR_NONE;
242 return o->AsClass();
243}
244
Elliott Hughes221229c2013-01-08 18:17:50 -0800245static JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread)
jeffhaoa77f0f62012-12-05 17:19:31 -0800246 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700247 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 mirror::Object* thread_peer = gRegistry->Get<mirror::Object*>(thread_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800250 if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) {
Elliott Hughes221229c2013-01-08 18:17:50 -0800251 // This isn't even an object.
252 return JDWP::ERR_INVALID_OBJECT;
Elliott Hughes436e3722012-02-17 20:01:47 -0800253 }
Elliott Hughes221229c2013-01-08 18:17:50 -0800254
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800255 mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
Elliott Hughes221229c2013-01-08 18:17:50 -0800256 if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
257 // This isn't a thread.
258 return JDWP::ERR_INVALID_THREAD;
259 }
260
261 thread = Thread::FromManagedThread(soa, thread_peer);
262 if (thread == NULL) {
263 // This is a java.lang.Thread without a Thread*. Must be a zombie.
264 return JDWP::ERR_THREAD_NOT_ALIVE;
265 }
266 return JDWP::ERR_NONE;
Elliott Hughes436e3722012-02-17 20:01:47 -0800267}
268
Elliott Hughes24437992011-11-30 14:49:33 -0800269static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
270 // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
271 // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
272 return static_cast<JDWP::JdwpTag>(descriptor[0]);
273}
274
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800275static JDWP::JdwpTag TagFromClass(mirror::Class* c)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700276 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes86b00102011-12-05 17:54:26 -0800277 CHECK(c != NULL);
Elliott Hughes24437992011-11-30 14:49:33 -0800278 if (c->IsArrayClass()) {
279 return JDWP::JT_ARRAY;
280 }
281
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800282 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughes24437992011-11-30 14:49:33 -0800283 if (c->IsStringClass()) {
284 return JDWP::JT_STRING;
285 } else if (c->IsClassClass()) {
286 return JDWP::JT_CLASS_OBJECT;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800287 } else if (class_linker->FindSystemClass("Ljava/lang/Thread;")->IsAssignableFrom(c)) {
Elliott Hughes24437992011-11-30 14:49:33 -0800288 return JDWP::JT_THREAD;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800289 } else if (class_linker->FindSystemClass("Ljava/lang/ThreadGroup;")->IsAssignableFrom(c)) {
Elliott Hughes24437992011-11-30 14:49:33 -0800290 return JDWP::JT_THREAD_GROUP;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800291 } else if (class_linker->FindSystemClass("Ljava/lang/ClassLoader;")->IsAssignableFrom(c)) {
Elliott Hughes24437992011-11-30 14:49:33 -0800292 return JDWP::JT_CLASS_LOADER;
Elliott Hughes24437992011-11-30 14:49:33 -0800293 } else {
294 return JDWP::JT_OBJECT;
295 }
296}
297
298/*
299 * Objects declared to hold Object might actually hold a more specific
300 * type. The debugger may take a special interest in these (e.g. it
301 * wants to display the contents of Strings), so we want to return an
302 * appropriate tag.
303 *
304 * Null objects are tagged JT_OBJECT.
305 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306static JDWP::JdwpTag TagFromObject(const mirror::Object* o)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700307 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes24437992011-11-30 14:49:33 -0800308 return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(o->GetClass());
309}
310
311static bool IsPrimitiveTag(JDWP::JdwpTag tag) {
312 switch (tag) {
313 case JDWP::JT_BOOLEAN:
314 case JDWP::JT_BYTE:
315 case JDWP::JT_CHAR:
316 case JDWP::JT_FLOAT:
317 case JDWP::JT_DOUBLE:
318 case JDWP::JT_INT:
319 case JDWP::JT_LONG:
320 case JDWP::JT_SHORT:
321 case JDWP::JT_VOID:
322 return true;
323 default:
324 return false;
325 }
326}
327
Elliott Hughes3bb81562011-10-21 18:52:59 -0700328/*
329 * Handle one of the JDWP name/value pairs.
330 *
331 * JDWP options are:
332 * help: if specified, show help message and bail
333 * transport: may be dt_socket or dt_shmem
334 * address: for dt_socket, "host:port", or just "port" when listening
335 * server: if "y", wait for debugger to attach; if "n", attach to debugger
336 * timeout: how long to wait for debugger to connect / listen
337 *
338 * Useful with server=n (these aren't supported yet):
339 * onthrow=<exception-name>: connect to debugger when exception thrown
340 * onuncaught=y|n: connect to debugger when uncaught exception thrown
341 * launch=<command-line>: launch the debugger itself
342 *
343 * The "transport" option is required, as is "address" if server=n.
344 */
345static bool ParseJdwpOption(const std::string& name, const std::string& value) {
346 if (name == "transport") {
347 if (value == "dt_socket") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700348 gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700349 } else if (value == "dt_android_adb") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700350 gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700351 } else {
352 LOG(ERROR) << "JDWP transport not supported: " << value;
353 return false;
354 }
355 } else if (name == "server") {
356 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700357 gJdwpOptions.server = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700358 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700359 gJdwpOptions.server = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700360 } else {
361 LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
362 return false;
363 }
364 } else if (name == "suspend") {
365 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700366 gJdwpOptions.suspend = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700367 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700368 gJdwpOptions.suspend = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700369 } else {
370 LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
371 return false;
372 }
373 } else if (name == "address") {
374 /* this is either <port> or <host>:<port> */
375 std::string port_string;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700376 gJdwpOptions.host.clear();
Elliott Hughes3bb81562011-10-21 18:52:59 -0700377 std::string::size_type colon = value.find(':');
378 if (colon != std::string::npos) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700379 gJdwpOptions.host = value.substr(0, colon);
Elliott Hughes3bb81562011-10-21 18:52:59 -0700380 port_string = value.substr(colon + 1);
381 } else {
382 port_string = value;
383 }
384 if (port_string.empty()) {
385 LOG(ERROR) << "JDWP address missing port: " << value;
386 return false;
387 }
388 char* end;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800389 uint64_t port = strtoul(port_string.c_str(), &end, 10);
390 if (*end != '\0' || port > 0xffff) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700391 LOG(ERROR) << "JDWP address has junk in port field: " << value;
392 return false;
393 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700394 gJdwpOptions.port = port;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700395 } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
396 /* valid but unsupported */
397 LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
398 } else {
399 LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
400 }
401
402 return true;
403}
404
405/*
406 * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
407 * "transport=dt_socket,address=8000,server=y,suspend=n"
408 */
409bool Dbg::ParseJdwpOptions(const std::string& options) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800410 VLOG(jdwp) << "ParseJdwpOptions: " << options;
Elliott Hughes47fce012011-10-25 18:37:19 -0700411
Elliott Hughes3bb81562011-10-21 18:52:59 -0700412 std::vector<std::string> pairs;
413 Split(options, ',', pairs);
414
415 for (size_t i = 0; i < pairs.size(); ++i) {
416 std::string::size_type equals = pairs[i].find('=');
417 if (equals == std::string::npos) {
418 LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
419 return false;
420 }
421 ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
422 }
423
Elliott Hughes376a7a02011-10-24 18:35:55 -0700424 if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700425 LOG(ERROR) << "Must specify JDWP transport: " << options;
426 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700427 if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700428 LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
429 return false;
430 }
431
432 gJdwpConfigured = true;
433 return true;
434}
435
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700436void Dbg::StartJdwp() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700437 if (!gJdwpAllowed || !IsJdwpConfigured()) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700438 // No JDWP for you!
439 return;
440 }
441
Elliott Hughes475fc232011-10-25 15:00:35 -0700442 CHECK(gRegistry == NULL);
443 gRegistry = new ObjectRegistry;
444
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700445 // Init JDWP if the debugger is enabled. This may connect out to a
446 // debugger, passively listen for a debugger, or block waiting for a
447 // debugger.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700448 gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
449 if (gJdwpState == NULL) {
Elliott Hughesf8a2df72011-12-01 12:19:54 -0800450 // We probably failed because some other process has the port already, which means that
451 // if we don't abort the user is likely to think they're talking to us when they're actually
452 // talking to that other process.
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800453 LOG(FATAL) << "Debugger thread failed to initialize";
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700454 }
455
456 // If a debugger has already attached, send the "welcome" message.
457 // This may cause us to suspend all threads.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700458 if (gJdwpState->IsActive()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700459 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes376a7a02011-10-24 18:35:55 -0700460 if (!gJdwpState->PostVMStart()) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800461 LOG(WARNING) << "Failed to post 'start' message to debugger";
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700462 }
463 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700464}
465
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700466void Dbg::StopJdwp() {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700467 delete gJdwpState;
Elliott Hughes475fc232011-10-25 15:00:35 -0700468 delete gRegistry;
469 gRegistry = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700470}
471
Elliott Hughes767a1472011-10-26 18:49:02 -0700472void Dbg::GcDidFinish() {
473 if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700474 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes81ff3182012-03-23 20:35:56 -0700475 LOG(DEBUG) << "Sending heap info to DDM";
Elliott Hughes7162ad92011-10-27 14:08:42 -0700476 DdmSendHeapInfo(gDdmHpifWhen);
Elliott Hughes767a1472011-10-26 18:49:02 -0700477 }
478 if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes81ff3182012-03-23 20:35:56 -0700480 LOG(DEBUG) << "Dumping heap to DDM";
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700481 DdmSendHeapSegments(false);
Elliott Hughes767a1472011-10-26 18:49:02 -0700482 }
483 if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700484 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes767a1472011-10-26 18:49:02 -0700485 LOG(DEBUG) << "Dumping native heap to DDM";
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700486 DdmSendHeapSegments(true);
Elliott Hughes767a1472011-10-26 18:49:02 -0700487 }
488}
489
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700490void Dbg::SetJdwpAllowed(bool allowed) {
491 gJdwpAllowed = allowed;
492}
493
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700494DebugInvokeReq* Dbg::GetInvokeReq() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700495 return Thread::Current()->GetInvokeReq();
496}
497
498Thread* Dbg::GetDebugThread() {
499 return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
500}
501
502void Dbg::ClearWaitForEventThread() {
503 gJdwpState->ClearWaitForEventThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700504}
505
506void Dbg::Connected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700507 CHECK(!gDebuggerConnected);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800508 VLOG(jdwp) << "JDWP has attached";
Elliott Hughes3bb81562011-10-21 18:52:59 -0700509 gDebuggerConnected = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800510 gDisposed = false;
511}
512
513void Dbg::Disposed() {
514 gDisposed = true;
515}
516
517bool Dbg::IsDisposed() {
518 return gDisposed;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700519}
520
Elliott Hughesa2155262011-11-16 16:26:58 -0800521void Dbg::GoActive() {
522 // Enable all debugging features, including scans for breakpoints.
523 // This is a no-op if we're already active.
524 // Only called from the JDWP handler thread.
525 if (gDebuggerActive) {
526 return;
527 }
528
Elliott Hughesc0f09332012-03-26 13:27:06 -0700529 {
530 // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
jeffhao09bfc6a2012-12-11 18:11:43 -0800531 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Elliott Hughesc0f09332012-03-26 13:27:06 -0700532 CHECK_EQ(gBreakpoints.size(), 0U);
533 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800534
Ian Rogers62d6c772013-02-27 08:32:07 -0800535 Runtime* runtime = Runtime::Current();
536 runtime->GetThreadList()->SuspendAll();
537 Thread* self = Thread::Current();
538 ThreadState old_state = self->SetStateUnsafe(kRunnable);
539 CHECK_NE(old_state, kRunnable);
540 runtime->GetInstrumentation()->AddListener(&gDebugInstrumentationListener,
541 instrumentation::Instrumentation::kMethodEntered |
542 instrumentation::Instrumentation::kMethodExited |
Jeff Hao14dd5a82013-04-11 10:23:36 -0700543 instrumentation::Instrumentation::kDexPcMoved |
544 instrumentation::Instrumentation::kExceptionCaught);
Elliott Hughesa2155262011-11-16 16:26:58 -0800545 gDebuggerActive = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800546 CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
547 runtime->GetThreadList()->ResumeAll();
548
549 LOG(INFO) << "Debugger is active";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700550}
551
552void Dbg::Disconnected() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700553 CHECK(gDebuggerConnected);
554
Elliott Hughesc0f09332012-03-26 13:27:06 -0700555 LOG(INFO) << "Debugger is no longer active";
Elliott Hughes234ab152011-10-26 14:02:26 -0700556
Ian Rogers62d6c772013-02-27 08:32:07 -0800557 // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread
558 // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener
559 // and clear the object registry.
560 Runtime* runtime = Runtime::Current();
561 runtime->GetThreadList()->SuspendAll();
562 Thread* self = Thread::Current();
563 ThreadState old_state = self->SetStateUnsafe(kRunnable);
564 runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener,
565 instrumentation::Instrumentation::kMethodEntered |
566 instrumentation::Instrumentation::kMethodExited |
Jeff Hao14dd5a82013-04-11 10:23:36 -0700567 instrumentation::Instrumentation::kDexPcMoved |
568 instrumentation::Instrumentation::kExceptionCaught);
Elliott Hughesc0f09332012-03-26 13:27:06 -0700569 gDebuggerActive = false;
Elliott Hughes234ab152011-10-26 14:02:26 -0700570 gRegistry->Clear();
571 gDebuggerConnected = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800572 CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
573 runtime->GetThreadList()->ResumeAll();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700574}
575
Elliott Hughesc0f09332012-03-26 13:27:06 -0700576bool Dbg::IsDebuggerActive() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700577 return gDebuggerActive;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700578}
579
Elliott Hughesc0f09332012-03-26 13:27:06 -0700580bool Dbg::IsJdwpConfigured() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700581 return gJdwpConfigured;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700582}
583
584int64_t Dbg::LastDebuggerActivity() {
Elliott Hughesca951522011-12-05 12:01:32 -0800585 return gJdwpState->LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700586}
587
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700588void Dbg::UndoDebuggerSuspensions() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700589 Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700590}
591
Elliott Hughes88d63092013-01-09 09:55:54 -0800592std::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800593 mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800594 if (o == NULL) {
595 return "NULL";
596 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800597 if (o == ObjectRegistry::kInvalidObject) {
Elliott Hughes88d63092013-01-09 09:55:54 -0800598 return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
Elliott Hughes436e3722012-02-17 20:01:47 -0800599 }
600 if (!o->IsClass()) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800601 return StringPrintf("non-class %p", o); // This is only used for debugging output anyway.
602 }
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800603 return DescriptorToName(ClassHelper(o->AsClass()).GetDescriptor());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700604}
605
Elliott Hughes88d63092013-01-09 09:55:54 -0800606JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800607 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800608 mirror::Class* c = DecodeClass(id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -0800609 if (c == NULL) {
610 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800611 }
Elliott Hughes88d63092013-01-09 09:55:54 -0800612 class_object_id = gRegistry->Add(c);
Elliott Hughes436e3722012-02-17 20:01:47 -0800613 return JDWP::ERR_NONE;
Elliott Hughes86964332012-02-15 19:37:42 -0800614}
615
Elliott Hughes88d63092013-01-09 09:55:54 -0800616JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800617 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618 mirror::Class* c = DecodeClass(id, status);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800619 if (c == NULL) {
620 return status;
621 }
622 if (c->IsInterface()) {
623 // http://code.google.com/p/android/issues/detail?id=20856
Elliott Hughes88d63092013-01-09 09:55:54 -0800624 superclass_id = 0;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800625 } else {
Elliott Hughes88d63092013-01-09 09:55:54 -0800626 superclass_id = gRegistry->Add(c->GetSuperClass());
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800627 }
628 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700629}
630
Elliott Hughes436e3722012-02-17 20:01:47 -0800631JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800632 mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800633 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800634 return JDWP::ERR_INVALID_OBJECT;
635 }
636 expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader()));
637 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700638}
639
Elliott Hughes436e3722012-02-17 20:01:47 -0800640JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
641 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800642 mirror::Class* c = DecodeClass(id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -0800643 if (c == NULL) {
644 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800645 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800646
647 uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
648
649 // Set ACC_SUPER; dex files don't contain this flag, but all classes are supposed to have it set.
650 // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
651 access_flags |= kAccSuper;
652
653 expandBufAdd4BE(pReply, access_flags);
654
655 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700656}
657
Elliott Hughesf327e072013-01-09 16:01:26 -0800658JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply)
659 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800660 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800661 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughesf327e072013-01-09 16:01:26 -0800662 return JDWP::ERR_INVALID_OBJECT;
663 }
664
665 // Ensure all threads are suspended while we read objects' lock words.
666 Thread* self = Thread::Current();
667 Locks::mutator_lock_->SharedUnlock(self);
668 Locks::mutator_lock_->ExclusiveLock(self);
669
670 MonitorInfo monitor_info(o);
671
672 Locks::mutator_lock_->ExclusiveUnlock(self);
673 Locks::mutator_lock_->SharedLock(self);
674
675 if (monitor_info.owner != NULL) {
676 expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner->GetPeer()));
677 } else {
678 expandBufAddObjectId(reply, gRegistry->Add(NULL));
679 }
680 expandBufAdd4BE(reply, monitor_info.entry_count);
681 expandBufAdd4BE(reply, monitor_info.waiters.size());
682 for (size_t i = 0; i < monitor_info.waiters.size(); ++i) {
683 expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters[i]->GetPeer()));
684 }
685 return JDWP::ERR_NONE;
686}
687
Elliott Hughes734b8c62013-01-11 15:32:45 -0800688JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
689 std::vector<JDWP::ObjectId>& monitors,
690 std::vector<uint32_t>& stack_depths)
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800691 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
692 ScopedObjectAccessUnchecked soa(Thread::Current());
693 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
694 Thread* thread;
695 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
696 if (error != JDWP::ERR_NONE) {
697 return error;
698 }
699 if (!IsSuspendedForDebugger(soa, thread)) {
700 return JDWP::ERR_THREAD_NOT_SUSPENDED;
701 }
702
703 struct OwnedMonitorVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800704 OwnedMonitorVisitor(Thread* thread, Context* context)
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800705 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -0800706 : StackVisitor(thread, context), current_stack_depth(0) {}
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800707
708 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
709 // annotalysis.
710 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
711 if (!GetMethod()->IsRuntimeMethod()) {
712 Monitor::VisitLocks(this, AppendOwnedMonitors, this);
Elliott Hughes734b8c62013-01-11 15:32:45 -0800713 ++current_stack_depth;
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800714 }
715 return true;
716 }
717
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800718 static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800719 OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
Elliott Hughes734b8c62013-01-11 15:32:45 -0800720 visitor->monitors.push_back(owned_monitor);
721 visitor->stack_depths.push_back(visitor->current_stack_depth);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800722 }
723
Elliott Hughes734b8c62013-01-11 15:32:45 -0800724 size_t current_stack_depth;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800725 std::vector<mirror::Object*> monitors;
Elliott Hughes734b8c62013-01-11 15:32:45 -0800726 std::vector<uint32_t> stack_depths;
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800727 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800728 UniquePtr<Context> context(Context::Create());
729 OwnedMonitorVisitor visitor(thread, context.get());
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800730 visitor.WalkStack();
731
732 for (size_t i = 0; i < visitor.monitors.size(); ++i) {
733 monitors.push_back(gRegistry->Add(visitor.monitors[i]));
Elliott Hughes734b8c62013-01-11 15:32:45 -0800734 stack_depths.push_back(visitor.stack_depths[i]);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800735 }
736
737 return JDWP::ERR_NONE;
738}
739
Elliott Hughesf9501702013-01-11 11:22:27 -0800740JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, JDWP::ObjectId& contended_monitor)
741 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
742 ScopedObjectAccessUnchecked soa(Thread::Current());
743 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
744 Thread* thread;
745 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
746 if (error != JDWP::ERR_NONE) {
747 return error;
748 }
749 if (!IsSuspendedForDebugger(soa, thread)) {
750 return JDWP::ERR_THREAD_NOT_SUSPENDED;
751 }
752
753 contended_monitor = gRegistry->Add(Monitor::GetContendedMonitor(thread));
754
755 return JDWP::ERR_NONE;
756}
757
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800758JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
759 std::vector<uint64_t>& counts)
760 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
761
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800762 std::vector<mirror::Class*> classes;
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800763 counts.clear();
764 for (size_t i = 0; i < class_ids.size(); ++i) {
765 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800766 mirror::Class* c = DecodeClass(class_ids[i], status);
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800767 if (c == NULL) {
768 return status;
769 }
770 classes.push_back(c);
771 counts.push_back(0);
772 }
773
774 Runtime::Current()->GetHeap()->CountInstances(classes, false, &counts[0]);
775 return JDWP::ERR_NONE;
776}
777
Elliott Hughes3b78c942013-01-15 17:35:41 -0800778JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances)
779 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
780 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800781 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800782 if (c == NULL) {
783 return status;
784 }
785
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800786 std::vector<mirror::Object*> raw_instances;
Elliott Hughes3b78c942013-01-15 17:35:41 -0800787 Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances);
788 for (size_t i = 0; i < raw_instances.size(); ++i) {
789 instances.push_back(gRegistry->Add(raw_instances[i]));
790 }
791 return JDWP::ERR_NONE;
792}
793
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800794JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
795 std::vector<JDWP::ObjectId>& referring_objects)
796 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800797 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800798 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800799 return JDWP::ERR_INVALID_OBJECT;
800 }
801
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800802 std::vector<mirror::Object*> raw_instances;
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800803 Runtime::Current()->GetHeap()->GetReferringObjects(o, max_count, raw_instances);
804 for (size_t i = 0; i < raw_instances.size(); ++i) {
805 referring_objects.push_back(gRegistry->Add(raw_instances[i]));
806 }
807 return JDWP::ERR_NONE;
808}
809
Elliott Hughes64f574f2013-02-20 14:57:12 -0800810JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id)
811 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
812 gRegistry->DisableCollection(object_id);
813 return JDWP::ERR_NONE;
814}
815
816JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id)
817 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
818 gRegistry->EnableCollection(object_id);
819 return JDWP::ERR_NONE;
820}
821
822JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected)
823 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
824 is_collected = gRegistry->IsCollected(object_id);
825 return JDWP::ERR_NONE;
826}
827
828void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count)
829 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
830 gRegistry->DisposeObject(object_id, reference_count);
831}
832
Elliott Hughes88d63092013-01-09 09:55:54 -0800833JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800834 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800835 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -0800836 if (c == NULL) {
837 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800838 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800839
840 expandBufAdd1(pReply, c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS);
Elliott Hughes88d63092013-01-09 09:55:54 -0800841 expandBufAddRefTypeId(pReply, class_id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800842 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700843}
844
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800845void Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800846 // Get the complete list of reference classes (i.e. all classes except
847 // the primitive types).
848 // Returns a newly-allocated buffer full of RefTypeId values.
849 struct ClassListCreator {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800850 explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800851 }
852
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800853 static bool Visit(mirror::Class* c, void* arg) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800854 return reinterpret_cast<ClassListCreator*>(arg)->Visit(c);
855 }
856
Elliott Hughes64f574f2013-02-20 14:57:12 -0800857 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
858 // annotalysis.
859 bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughesa2155262011-11-16 16:26:58 -0800860 if (!c->IsPrimitive()) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800861 classes.push_back(gRegistry->AddRefType(c));
Elliott Hughesa2155262011-11-16 16:26:58 -0800862 }
863 return true;
864 }
865
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800866 std::vector<JDWP::RefTypeId>& classes;
Elliott Hughesa2155262011-11-16 16:26:58 -0800867 };
868
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800869 ClassListCreator clc(classes);
Elliott Hughesa2155262011-11-16 16:26:58 -0800870 Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700871}
872
Elliott Hughes88d63092013-01-09 09:55:54 -0800873JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800874 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800875 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -0800876 if (c == NULL) {
877 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800878 }
879
Elliott Hughesa2155262011-11-16 16:26:58 -0800880 if (c->IsArrayClass()) {
881 *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
882 *pTypeTag = JDWP::TT_ARRAY;
883 } else {
884 if (c->IsErroneous()) {
885 *pStatus = JDWP::CS_ERROR;
886 } else {
887 *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
888 }
889 *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
890 }
891
892 if (pDescriptor != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800893 *pDescriptor = ClassHelper(c).GetDescriptor();
Elliott Hughesa2155262011-11-16 16:26:58 -0800894 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800895 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700896}
897
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800898void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800899 std::vector<mirror::Class*> classes;
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800900 Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
901 ids.clear();
902 for (size_t i = 0; i < classes.size(); ++i) {
903 ids.push_back(gRegistry->Add(classes[i]));
904 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700905}
906
Elliott Hughes64f574f2013-02-20 14:57:12 -0800907JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply)
908 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800909 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800910 if (o == NULL || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800911 return JDWP::ERR_INVALID_OBJECT;
Elliott Hughes499c5132011-11-17 14:55:11 -0800912 }
Elliott Hughes2435a572012-02-17 16:07:41 -0800913
914 JDWP::JdwpTypeTag type_tag;
915 if (o->GetClass()->IsArrayClass()) {
916 type_tag = JDWP::TT_ARRAY;
917 } else if (o->GetClass()->IsInterface()) {
918 type_tag = JDWP::TT_INTERFACE;
919 } else {
920 type_tag = JDWP::TT_CLASS;
921 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800922 JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
Elliott Hughes2435a572012-02-17 16:07:41 -0800923
924 expandBufAdd1(pReply, type_tag);
925 expandBufAddRefTypeId(pReply, type_id);
926
927 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700928}
929
Elliott Hughes88d63092013-01-09 09:55:54 -0800930JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string& signature) {
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800931 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800932 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800933 if (c == NULL) {
934 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800935 }
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800936 signature = ClassHelper(c).GetDescriptor();
937 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700938}
939
Elliott Hughes88d63092013-01-09 09:55:54 -0800940JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800941 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800942 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -0800943 if (c == NULL) {
944 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800945 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800946 result = ClassHelper(c).GetSourceFile();
947 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700948}
949
Elliott Hughes88d63092013-01-09 09:55:54 -0800950JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800951 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800952 if (o == ObjectRegistry::kInvalidObject) {
Elliott Hughes546b9862012-06-20 16:06:13 -0700953 return JDWP::ERR_INVALID_OBJECT;
954 }
955 tag = TagFromObject(o);
956 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700957}
958
Elliott Hughesaed4be92011-12-02 16:16:23 -0800959size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
Elliott Hughesdbb40792011-11-18 17:05:22 -0800960 switch (tag) {
961 case JDWP::JT_VOID:
962 return 0;
963 case JDWP::JT_BYTE:
964 case JDWP::JT_BOOLEAN:
965 return 1;
966 case JDWP::JT_CHAR:
967 case JDWP::JT_SHORT:
968 return 2;
969 case JDWP::JT_FLOAT:
970 case JDWP::JT_INT:
971 return 4;
972 case JDWP::JT_ARRAY:
973 case JDWP::JT_OBJECT:
974 case JDWP::JT_STRING:
975 case JDWP::JT_THREAD:
976 case JDWP::JT_THREAD_GROUP:
977 case JDWP::JT_CLASS_LOADER:
978 case JDWP::JT_CLASS_OBJECT:
979 return sizeof(JDWP::ObjectId);
980 case JDWP::JT_DOUBLE:
981 case JDWP::JT_LONG:
982 return 8;
983 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800984 LOG(FATAL) << "Unknown tag " << tag;
Elliott Hughesdbb40792011-11-18 17:05:22 -0800985 return -1;
986 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700987}
988
Elliott Hughes88d63092013-01-09 09:55:54 -0800989JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800990 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800991 mirror::Array* a = DecodeArray(array_id, status);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800992 if (a == NULL) {
993 return status;
Elliott Hughes24437992011-11-30 14:49:33 -0800994 }
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800995 length = a->GetLength();
996 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700997}
998
Elliott Hughes88d63092013-01-09 09:55:54 -0800999JDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001000 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001001 mirror::Array* a = DecodeArray(array_id, status);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001002 if (a == NULL) {
1003 return status;
1004 }
Elliott Hughes24437992011-11-30 14:49:33 -08001005
1006 if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
1007 LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001008 return JDWP::ERR_INVALID_LENGTH;
Elliott Hughes24437992011-11-30 14:49:33 -08001009 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001010 std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor());
Elliott Hughes24437992011-11-30 14:49:33 -08001011 JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
1012
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001013 expandBufAdd1(pReply, tag);
1014 expandBufAdd4BE(pReply, count);
1015
Elliott Hughes24437992011-11-30 14:49:33 -08001016 if (IsPrimitiveTag(tag)) {
1017 size_t width = GetTagWidth(tag);
Elliott Hughes24437992011-11-30 14:49:33 -08001018 uint8_t* dst = expandBufAddSpace(pReply, count * width);
1019 if (width == 8) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001020 const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t)));
Elliott Hughes24437992011-11-30 14:49:33 -08001021 for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
1022 } else if (width == 4) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001023 const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t)));
Elliott Hughes24437992011-11-30 14:49:33 -08001024 for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
1025 } else if (width == 2) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001026 const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t)));
Elliott Hughes24437992011-11-30 14:49:33 -08001027 for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
1028 } else {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001029 const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t)));
Elliott Hughes24437992011-11-30 14:49:33 -08001030 memcpy(dst, &src[offset * width], count * width);
1031 }
1032 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001033 mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>();
Elliott Hughes24437992011-11-30 14:49:33 -08001034 for (int i = 0; i < count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001035 mirror::Object* element = oa->Get(offset + i);
Elliott Hughes24437992011-11-30 14:49:33 -08001036 JDWP::JdwpTag specific_tag = (element != NULL) ? TagFromObject(element) : tag;
1037 expandBufAdd1(pReply, specific_tag);
1038 expandBufAddObjectId(pReply, gRegistry->Add(element));
1039 }
1040 }
1041
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001042 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001043}
1044
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001045template <typename T> void CopyArrayData(mirror::Array* a, JDWP::Request& src, int offset, int count) {
1046 DCHECK(a->GetClass()->IsPrimitiveArray());
1047
1048 T* dst = &(reinterpret_cast<T*>(a->GetRawData(sizeof(T)))[offset * sizeof(T)]);
1049 for (int i = 0; i < count; ++i) {
1050 *dst++ = src.ReadValue(sizeof(T));
1051 }
1052}
1053
Elliott Hughes88d63092013-01-09 09:55:54 -08001054JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001055 JDWP::Request& request)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001056 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001057 JDWP::JdwpError status;
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001058 mirror::Array* dst = DecodeArray(array_id, status);
1059 if (dst == NULL) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001060 return status;
1061 }
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001062
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001063 if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001064 LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001065 return JDWP::ERR_INVALID_LENGTH;
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001066 }
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001067 std::string descriptor(ClassHelper(dst->GetClass()).GetDescriptor());
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001068 JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
1069
1070 if (IsPrimitiveTag(tag)) {
1071 size_t width = GetTagWidth(tag);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001072 if (width == 8) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001073 CopyArrayData<uint64_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001074 } else if (width == 4) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001075 CopyArrayData<uint32_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001076 } else if (width == 2) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001077 CopyArrayData<uint16_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001078 } else {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001079 CopyArrayData<uint8_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001080 }
1081 } else {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001082 mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>();
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001083 for (int i = 0; i < count; ++i) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001084 JDWP::ObjectId id = request.ReadObjectId();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001085 mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08001086 if (o == ObjectRegistry::kInvalidObject) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001087 return JDWP::ERR_INVALID_OBJECT;
1088 }
1089 oa->Set(offset + i, o);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001090 }
1091 }
1092
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001093 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001094}
1095
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001096JDWP::ObjectId Dbg::CreateString(const std::string& str) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001097 return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str()));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001098}
1099
Elliott Hughes88d63092013-01-09 09:55:54 -08001100JDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001101 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001102 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -08001103 if (c == NULL) {
1104 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001105 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001106 new_object = gRegistry->Add(c->AllocObject(Thread::Current()));
Elliott Hughes436e3722012-02-17 20:01:47 -08001107 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001108}
1109
Elliott Hughesbf13d362011-12-08 15:51:37 -08001110/*
1111 * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1112 */
Elliott Hughes88d63092013-01-09 09:55:54 -08001113JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001114 JDWP::ObjectId& new_array) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001115 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001116 mirror::Class* c = DecodeClass(array_class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -08001117 if (c == NULL) {
1118 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001119 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001120 new_array = gRegistry->Add(mirror::Array::Alloc(Thread::Current(), c, length));
Elliott Hughes436e3722012-02-17 20:01:47 -08001121 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001122}
1123
Elliott Hughes88d63092013-01-09 09:55:54 -08001124bool Dbg::MatchType(JDWP::RefTypeId instance_class_id, JDWP::RefTypeId class_id) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001125 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001126 mirror::Class* c1 = DecodeClass(instance_class_id, status);
Elliott Hughesa656a0f2012-02-21 18:03:44 -08001127 CHECK(c1 != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001128 mirror::Class* c2 = DecodeClass(class_id, status);
Elliott Hughesa656a0f2012-02-21 18:03:44 -08001129 CHECK(c2 != NULL);
1130 return c1->IsAssignableFrom(c2);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001131}
1132
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001133static JDWP::FieldId ToFieldId(const mirror::Field* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001135#ifdef MOVING_GARBAGE_COLLECTOR
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001136 UNIMPLEMENTED(FATAL);
Elliott Hughes03181a82011-11-17 17:22:21 -08001137#else
1138 return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
1139#endif
1140}
1141
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001142static JDWP::MethodId ToMethodId(const mirror::AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001144#ifdef MOVING_GARBAGE_COLLECTOR
1145 UNIMPLEMENTED(FATAL);
1146#else
1147 return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m));
1148#endif
1149}
1150
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001151static mirror::Field* FromFieldId(JDWP::FieldId fid)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesaed4be92011-12-02 16:16:23 -08001153#ifdef MOVING_GARBAGE_COLLECTOR
1154 UNIMPLEMENTED(FATAL);
1155#else
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001156 return reinterpret_cast<mirror::Field*>(static_cast<uintptr_t>(fid));
Elliott Hughesaed4be92011-12-02 16:16:23 -08001157#endif
1158}
1159
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001160static mirror::AbstractMethod* FromMethodId(JDWP::MethodId mid)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001162#ifdef MOVING_GARBAGE_COLLECTOR
1163 UNIMPLEMENTED(FATAL);
1164#else
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001165 return reinterpret_cast<mirror::AbstractMethod*>(static_cast<uintptr_t>(mid));
Elliott Hughes03181a82011-11-17 17:22:21 -08001166#endif
1167}
1168
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001169static void SetLocation(JDWP::JdwpLocation& location, mirror::AbstractMethod* m, uint32_t dex_pc)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001171 if (m == NULL) {
1172 memset(&location, 0, sizeof(location));
1173 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001174 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes74847412012-06-20 18:10:21 -07001175 location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1176 location.class_id = gRegistry->Add(c);
1177 location.method_id = ToMethodId(m);
Ian Rogers0399dde2012-06-06 17:09:28 -07001178 location.dex_pc = dex_pc;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001179 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08001180}
1181
Elliott Hughesa96836a2013-01-17 12:27:49 -08001182std::string Dbg::GetMethodName(JDWP::MethodId method_id)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001184 mirror::AbstractMethod* m = FromMethodId(method_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001185 return MethodHelper(m).GetName();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001186}
1187
Elliott Hughesa96836a2013-01-17 12:27:49 -08001188std::string Dbg::GetFieldName(JDWP::FieldId field_id)
1189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001190 mirror::Field* f = FromFieldId(field_id);
Elliott Hughesa96836a2013-01-17 12:27:49 -08001191 return FieldHelper(f).GetName();
1192}
1193
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001194/*
1195 * Augment the access flags for synthetic methods and fields by setting
1196 * the (as described by the spec) "0xf0000000 bit". Also, strip out any
1197 * flags not specified by the Java programming language.
1198 */
1199static uint32_t MangleAccessFlags(uint32_t accessFlags) {
1200 accessFlags &= kAccJavaFlagsMask;
1201 if ((accessFlags & kAccSynthetic) != 0) {
1202 accessFlags |= 0xf0000000;
1203 }
1204 return accessFlags;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001205}
1206
Elliott Hughesdbb40792011-11-18 17:05:22 -08001207static const uint16_t kEclipseWorkaroundSlot = 1000;
1208
1209/*
1210 * Eclipse appears to expect that the "this" reference is in slot zero.
1211 * If it's not, the "variables" display will show two copies of "this",
1212 * possibly because it gets "this" from SF.ThisObject and then displays
1213 * all locals with nonzero slot numbers.
1214 *
1215 * So, we remap the item in slot 0 to 1000, and remap "this" to zero. On
1216 * SF.GetValues / SF.SetValues we map them back.
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001217 *
1218 * TODO: jdb uses the value to determine whether a variable is a local or an argument,
1219 * by checking whether it's less than the number of arguments. To make that work, we'd
1220 * have to "mangle" all the arguments to come first, not just the implicit argument 'this'.
Elliott Hughesdbb40792011-11-18 17:05:22 -08001221 */
1222static uint16_t MangleSlot(uint16_t slot, const char* name) {
1223 uint16_t newSlot = slot;
1224 if (strcmp(name, "this") == 0) {
1225 newSlot = 0;
1226 } else if (slot == 0) {
1227 newSlot = kEclipseWorkaroundSlot;
1228 }
1229 return newSlot;
1230}
1231
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001232static uint16_t DemangleSlot(uint16_t slot, mirror::AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001234 if (slot == kEclipseWorkaroundSlot) {
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001235 return 0;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001236 } else if (slot == 0) {
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001237 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
Elliott Hughescaf76542012-06-28 16:08:22 -07001238 CHECK(code_item != NULL) << PrettyMethod(m);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001239 return code_item->registers_size_ - code_item->ins_size_;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001240 }
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001241 return slot;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001242}
1243
Elliott Hughes88d63092013-01-09 09:55:54 -08001244JDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001245 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001246 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -08001247 if (c == NULL) {
1248 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001249 }
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001250
1251 size_t instance_field_count = c->NumInstanceFields();
1252 size_t static_field_count = c->NumStaticFields();
1253
1254 expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1255
1256 for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001257 mirror::Field* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001258 FieldHelper fh(f);
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001259 expandBufAddFieldId(pReply, ToFieldId(f));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001260 expandBufAddUtf8String(pReply, fh.GetName());
1261 expandBufAddUtf8String(pReply, fh.GetTypeDescriptor());
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001262 if (with_generic) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001263 static const char genericSignature[1] = "";
1264 expandBufAddUtf8String(pReply, genericSignature);
1265 }
1266 expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1267 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001268 return JDWP::ERR_NONE;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001269}
1270
Elliott Hughes88d63092013-01-09 09:55:54 -08001271JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001272 JDWP::ExpandBuf* pReply) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001273 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001274 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -08001275 if (c == NULL) {
1276 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001277 }
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001278
1279 size_t direct_method_count = c->NumDirectMethods();
1280 size_t virtual_method_count = c->NumVirtualMethods();
1281
1282 expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
1283
1284 for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001285 mirror::AbstractMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001286 MethodHelper mh(m);
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001287 expandBufAddMethodId(pReply, ToMethodId(m));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001288 expandBufAddUtf8String(pReply, mh.GetName());
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001289 expandBufAddUtf8String(pReply, mh.GetSignature());
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001290 if (with_generic) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001291 static const char genericSignature[1] = "";
1292 expandBufAddUtf8String(pReply, genericSignature);
1293 }
1294 expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags()));
1295 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001296 return JDWP::ERR_NONE;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001297}
1298
Elliott Hughes88d63092013-01-09 09:55:54 -08001299JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
Elliott Hughes436e3722012-02-17 20:01:47 -08001300 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001301 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes436e3722012-02-17 20:01:47 -08001302 if (c == NULL) {
1303 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001304 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001305
1306 ClassHelper kh(c);
Ian Rogersd24e2642012-06-06 21:21:43 -07001307 size_t interface_count = kh.NumDirectInterfaces();
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001308 expandBufAdd4BE(pReply, interface_count);
1309 for (size_t i = 0; i < interface_count; ++i) {
Elliott Hughes64f574f2013-02-20 14:57:12 -08001310 expandBufAddRefTypeId(pReply, gRegistry->AddRefType(kh.GetDirectInterface(i)));
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001311 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001312 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001313}
1314
Elliott Hughes88d63092013-01-09 09:55:54 -08001315void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001316 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001317 struct DebugCallbackContext {
1318 int numItems;
1319 JDWP::ExpandBuf* pReply;
1320
Elliott Hughes2435a572012-02-17 16:07:41 -08001321 static bool Callback(void* context, uint32_t address, uint32_t line_number) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001322 DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1323 expandBufAdd8BE(pContext->pReply, address);
Elliott Hughes2435a572012-02-17 16:07:41 -08001324 expandBufAdd4BE(pContext->pReply, line_number);
Elliott Hughes03181a82011-11-17 17:22:21 -08001325 pContext->numItems++;
1326 return true;
1327 }
1328 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001329 mirror::AbstractMethod* m = FromMethodId(method_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001330 MethodHelper mh(m);
Elliott Hughes03181a82011-11-17 17:22:21 -08001331 uint64_t start, end;
1332 if (m->IsNative()) {
1333 start = -1;
1334 end = -1;
1335 } else {
1336 start = 0;
jeffhao14f0db92012-12-14 17:50:42 -08001337 // Return the index of the last instruction
1338 end = mh.GetCodeItem()->insns_size_in_code_units_ - 1;
Elliott Hughes03181a82011-11-17 17:22:21 -08001339 }
1340
1341 expandBufAdd8BE(pReply, start);
1342 expandBufAdd8BE(pReply, end);
1343
1344 // Add numLines later
1345 size_t numLinesOffset = expandBufGetLength(pReply);
1346 expandBufAdd4BE(pReply, 0);
1347
1348 DebugCallbackContext context;
1349 context.numItems = 0;
1350 context.pReply = pReply;
1351
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001352 mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(),
1353 DebugCallbackContext::Callback, NULL, &context);
Elliott Hughes03181a82011-11-17 17:22:21 -08001354
1355 JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001356}
1357
Elliott Hughes88d63092013-01-09 09:55:54 -08001358void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, JDWP::ExpandBuf* pReply) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001359 struct DebugCallbackContext {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001360 JDWP::ExpandBuf* pReply;
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001361 size_t variable_count;
1362 bool with_generic;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001363
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001364 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 -08001365 DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1366
Elliott Hughesad3da692012-02-24 16:51:35 -08001367 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 -08001368
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001369 slot = MangleSlot(slot, name);
1370
Elliott Hughesdbb40792011-11-18 17:05:22 -08001371 expandBufAdd8BE(pContext->pReply, startAddress);
1372 expandBufAddUtf8String(pContext->pReply, name);
1373 expandBufAddUtf8String(pContext->pReply, descriptor);
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001374 if (pContext->with_generic) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001375 expandBufAddUtf8String(pContext->pReply, signature);
1376 }
1377 expandBufAdd4BE(pContext->pReply, endAddress - startAddress);
1378 expandBufAdd4BE(pContext->pReply, slot);
1379
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001380 ++pContext->variable_count;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001381 }
1382 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001383 mirror::AbstractMethod* m = FromMethodId(method_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001384 MethodHelper mh(m);
1385 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Elliott Hughesdbb40792011-11-18 17:05:22 -08001386
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001387 // arg_count considers doubles and longs to take 2 units.
1388 // variable_count considers everything to take 1 unit.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001389 std::string shorty(mh.GetShorty());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001390 expandBufAdd4BE(pReply, mirror::AbstractMethod::NumArgRegisters(shorty));
Elliott Hughesdbb40792011-11-18 17:05:22 -08001391
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001392 // We don't know the total number of variables yet, so leave a blank and update it later.
1393 size_t variable_count_offset = expandBufGetLength(pReply);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001394 expandBufAdd4BE(pReply, 0);
1395
1396 DebugCallbackContext context;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001397 context.pReply = pReply;
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001398 context.variable_count = 0;
1399 context.with_generic = with_generic;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001400
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001401 mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL,
1402 DebugCallbackContext::Callback, &context);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001403
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001404 JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001405}
1406
Elliott Hughes9777ba22013-01-17 09:04:19 -08001407JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
1408 std::vector<uint8_t>& bytecodes)
1409 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001410 mirror::AbstractMethod* m = FromMethodId(method_id);
Elliott Hughes9777ba22013-01-17 09:04:19 -08001411 if (m == NULL) {
1412 return JDWP::ERR_INVALID_METHODID;
1413 }
1414 MethodHelper mh(m);
1415 const DexFile::CodeItem* code_item = mh.GetCodeItem();
1416 size_t byte_count = code_item->insns_size_in_code_units_ * 2;
1417 const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_);
1418 const uint8_t* end = begin + byte_count;
1419 for (const uint8_t* p = begin; p != end; ++p) {
1420 bytecodes.push_back(*p);
1421 }
1422 return JDWP::ERR_NONE;
1423}
1424
Elliott Hughes88d63092013-01-09 09:55:54 -08001425JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
1426 return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001427}
1428
Elliott Hughes88d63092013-01-09 09:55:54 -08001429JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
1430 return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001431}
1432
Elliott Hughes88d63092013-01-09 09:55:54 -08001433static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
1434 JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001435 bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001436 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes0cf74332012-02-23 23:14:00 -08001437 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001438 mirror::Class* c = DecodeClass(ref_type_id, status);
Elliott Hughes88d63092013-01-09 09:55:54 -08001439 if (ref_type_id != 0 && c == NULL) {
Elliott Hughes0cf74332012-02-23 23:14:00 -08001440 return status;
1441 }
1442
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001443 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08001444 if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001445 return JDWP::ERR_INVALID_OBJECT;
1446 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001447 mirror::Field* f = FromFieldId(field_id);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001448
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001449 mirror::Class* receiver_class = c;
Elliott Hughes0cf74332012-02-23 23:14:00 -08001450 if (receiver_class == NULL && o != NULL) {
1451 receiver_class = o->GetClass();
1452 }
1453 // TODO: should we give up now if receiver_class is NULL?
1454 if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
1455 LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001456 return JDWP::ERR_INVALID_FIELDID;
1457 }
Elliott Hughesaed4be92011-12-02 16:16:23 -08001458
Elliott Hughes0cf74332012-02-23 23:14:00 -08001459 // The RI only enforces the static/non-static mismatch in one direction.
1460 // TODO: should we change the tests and check both?
1461 if (is_static) {
1462 if (!f->IsStatic()) {
1463 return JDWP::ERR_INVALID_FIELDID;
1464 }
1465 } else {
1466 if (f->IsStatic()) {
1467 LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001468 }
1469 }
jeffhao0dfbb7e2012-11-28 15:26:03 -08001470 if (f->IsStatic()) {
1471 o = f->GetDeclaringClass();
1472 }
Elliott Hughes0cf74332012-02-23 23:14:00 -08001473
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001474 JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
Elliott Hughesaed4be92011-12-02 16:16:23 -08001475
1476 if (IsPrimitiveTag(tag)) {
1477 expandBufAdd1(pReply, tag);
1478 if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
1479 expandBufAdd1(pReply, f->Get32(o));
1480 } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
1481 expandBufAdd2BE(pReply, f->Get32(o));
1482 } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
1483 expandBufAdd4BE(pReply, f->Get32(o));
1484 } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1485 expandBufAdd8BE(pReply, f->Get64(o));
1486 } else {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08001487 LOG(FATAL) << "Unknown tag: " << tag;
Elliott Hughesaed4be92011-12-02 16:16:23 -08001488 }
1489 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001490 mirror::Object* value = f->GetObject(o);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001491 expandBufAdd1(pReply, TagFromObject(value));
1492 expandBufAddObjectId(pReply, gRegistry->Add(value));
1493 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001494 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001495}
1496
Elliott Hughes88d63092013-01-09 09:55:54 -08001497JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001498 JDWP::ExpandBuf* pReply) {
Elliott Hughes88d63092013-01-09 09:55:54 -08001499 return GetFieldValueImpl(0, object_id, field_id, pReply, false);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001500}
1501
Elliott Hughes88d63092013-01-09 09:55:54 -08001502JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) {
1503 return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001504}
1505
Elliott Hughes88d63092013-01-09 09:55:54 -08001506static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001507 uint64_t value, int width, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001508 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001509 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08001510 if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001511 return JDWP::ERR_INVALID_OBJECT;
1512 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001513 mirror::Field* f = FromFieldId(field_id);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001514
1515 // The RI only enforces the static/non-static mismatch in one direction.
1516 // TODO: should we change the tests and check both?
1517 if (is_static) {
1518 if (!f->IsStatic()) {
1519 return JDWP::ERR_INVALID_FIELDID;
1520 }
1521 } else {
1522 if (f->IsStatic()) {
1523 LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001524 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001525 }
jeffhao0dfbb7e2012-11-28 15:26:03 -08001526 if (f->IsStatic()) {
1527 o = f->GetDeclaringClass();
1528 }
Elliott Hughesaed4be92011-12-02 16:16:23 -08001529
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001530 JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
Elliott Hughesaed4be92011-12-02 16:16:23 -08001531
1532 if (IsPrimitiveTag(tag)) {
1533 if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001534 CHECK_EQ(width, 8);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001535 f->Set64(o, value);
1536 } else {
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001537 CHECK_LE(width, 4);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001538 f->Set32(o, value);
1539 }
1540 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001541 mirror::Object* v = gRegistry->Get<mirror::Object*>(value);
Elliott Hughes64f574f2013-02-20 14:57:12 -08001542 if (v == ObjectRegistry::kInvalidObject) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001543 return JDWP::ERR_INVALID_OBJECT;
1544 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001545 if (v != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001546 mirror::Class* field_type = FieldHelper(f).GetType();
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001547 if (!field_type->IsAssignableFrom(v->GetClass())) {
1548 return JDWP::ERR_INVALID_OBJECT;
1549 }
1550 }
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001551 f->SetObject(o, v);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001552 }
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001553
1554 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001555}
1556
Elliott Hughes88d63092013-01-09 09:55:54 -08001557JDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001558 int width) {
Elliott Hughes88d63092013-01-09 09:55:54 -08001559 return SetFieldValueImpl(object_id, field_id, value, width, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001560}
1561
Elliott Hughes88d63092013-01-09 09:55:54 -08001562JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
1563 return SetFieldValueImpl(0, field_id, value, width, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001564}
1565
Elliott Hughes88d63092013-01-09 09:55:54 -08001566std::string Dbg::StringToUtf8(JDWP::ObjectId string_id) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001567 mirror::String* s = gRegistry->Get<mirror::String*>(string_id);
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001568 return s->ToModifiedUtf8();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001569}
1570
Elliott Hughes221229c2013-01-08 18:17:50 -08001571JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) {
jeffhaoa77f0f62012-12-05 17:19:31 -08001572 ScopedObjectAccessUnchecked soa(Thread::Current());
1573 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001574 Thread* thread;
1575 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1576 if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
1577 return error;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001578 }
Elliott Hughes221229c2013-01-08 18:17:50 -08001579
1580 // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001581 mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
1582 mirror::Field* java_lang_Thread_name_field =
1583 soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
1584 mirror::String* s =
1585 reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object));
Elliott Hughes221229c2013-01-08 18:17:50 -08001586 if (s != NULL) {
1587 name = s->ToModifiedUtf8();
1588 }
1589 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001590}
1591
Elliott Hughes221229c2013-01-08 18:17:50 -08001592JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001593 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001594 mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08001595 if (thread_object == ObjectRegistry::kInvalidObject) {
Elliott Hughes2435a572012-02-17 16:07:41 -08001596 return JDWP::ERR_INVALID_OBJECT;
1597 }
1598
1599 // Okay, so it's an object, but is it actually a thread?
Ian Rogers50b35e22012-10-04 10:09:15 -07001600 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001601 Thread* thread;
1602 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1603 if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1604 // Zombie threads are in the null group.
1605 expandBufAddObjectId(pReply, JDWP::ObjectId(0));
1606 return JDWP::ERR_NONE;
1607 }
1608 if (error != JDWP::ERR_NONE) {
1609 return error;
Elliott Hughes2435a572012-02-17 16:07:41 -08001610 }
Elliott Hughes499c5132011-11-17 14:55:11 -08001611
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001612 mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Thread;");
Elliott Hughes499c5132011-11-17 14:55:11 -08001613 CHECK(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001614 mirror::Field* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;");
Elliott Hughes499c5132011-11-17 14:55:11 -08001615 CHECK(f != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001616 mirror::Object* group = f->GetObject(thread_object);
Elliott Hughes499c5132011-11-17 14:55:11 -08001617 CHECK(group != NULL);
Elliott Hughes2435a572012-02-17 16:07:41 -08001618 JDWP::ObjectId thread_group_id = gRegistry->Add(group);
1619
1620 expandBufAddObjectId(pReply, thread_group_id);
1621 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001622}
1623
Elliott Hughes88d63092013-01-09 09:55:54 -08001624std::string Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001625 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001626 mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
Elliott Hughes499c5132011-11-17 14:55:11 -08001627 CHECK(thread_group != NULL);
1628
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001629 mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;");
Elliott Hughes499c5132011-11-17 14:55:11 -08001630 CHECK(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001631 mirror::Field* f = c->FindInstanceField("name", "Ljava/lang/String;");
Elliott Hughes499c5132011-11-17 14:55:11 -08001632 CHECK(f != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001633 mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group));
Elliott Hughes499c5132011-11-17 14:55:11 -08001634 return s->ToModifiedUtf8();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001635}
1636
Elliott Hughes88d63092013-01-09 09:55:54 -08001637JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001638 mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
Elliott Hughes4e235312011-12-02 11:34:15 -08001639 CHECK(thread_group != NULL);
1640
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001641 mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;");
Elliott Hughes4e235312011-12-02 11:34:15 -08001642 CHECK(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001643 mirror::Field* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;");
Elliott Hughes4e235312011-12-02 11:34:15 -08001644 CHECK(f != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001645 mirror::Object* parent = f->GetObject(thread_group);
Elliott Hughes4e235312011-12-02 11:34:15 -08001646 return gRegistry->Add(parent);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001647}
1648
1649JDWP::ObjectId Dbg::GetSystemThreadGroupId() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001650 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001651 mirror::Field* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
1652 mirror::Object* group = f->GetObject(f->GetDeclaringClass());
Ian Rogers365c1022012-06-22 15:05:28 -07001653 return gRegistry->Add(group);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001654}
1655
1656JDWP::ObjectId Dbg::GetMainThreadGroupId() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001658 mirror::Field* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup);
1659 mirror::Object* group = f->GetObject(f->GetDeclaringClass());
Ian Rogers365c1022012-06-22 15:05:28 -07001660 return gRegistry->Add(group);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001661}
1662
Elliott Hughes221229c2013-01-08 18:17:50 -08001663JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, JDWP::JdwpSuspendStatus* pSuspendStatus) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001664 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes499c5132011-11-17 14:55:11 -08001665
Elliott Hughes9e0c1752013-01-09 14:02:58 -08001666 *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
1667
Ian Rogers50b35e22012-10-04 10:09:15 -07001668 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001669 Thread* thread;
1670 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1671 if (error != JDWP::ERR_NONE) {
1672 if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1673 *pThreadStatus = JDWP::TS_ZOMBIE;
Elliott Hughes221229c2013-01-08 18:17:50 -08001674 return JDWP::ERR_NONE;
1675 }
1676 return error;
Elliott Hughes499c5132011-11-17 14:55:11 -08001677 }
1678
Elliott Hughes9e0c1752013-01-09 14:02:58 -08001679 if (IsSuspendedForDebugger(soa, thread)) {
1680 *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
Elliott Hughes499c5132011-11-17 14:55:11 -08001681 }
1682
Elliott Hughes9e0c1752013-01-09 14:02:58 -08001683 switch (thread->GetState()) {
1684 case kBlocked: *pThreadStatus = JDWP::TS_MONITOR; break;
1685 case kNative: *pThreadStatus = JDWP::TS_RUNNING; break;
1686 case kRunnable: *pThreadStatus = JDWP::TS_RUNNING; break;
1687 case kSleeping: *pThreadStatus = JDWP::TS_SLEEPING; break;
1688 case kStarting: *pThreadStatus = JDWP::TS_ZOMBIE; break;
1689 case kSuspended: *pThreadStatus = JDWP::TS_RUNNING; break;
1690 case kTerminated: *pThreadStatus = JDWP::TS_ZOMBIE; break;
1691 case kTimedWaiting: *pThreadStatus = JDWP::TS_WAIT; break;
1692 case kWaitingForDebuggerSend: *pThreadStatus = JDWP::TS_WAIT; break;
1693 case kWaitingForDebuggerSuspension: *pThreadStatus = JDWP::TS_WAIT; break;
1694 case kWaitingForDebuggerToAttach: *pThreadStatus = JDWP::TS_WAIT; break;
1695 case kWaitingForGcToComplete: *pThreadStatus = JDWP::TS_WAIT; break;
Ian Rogers1d54e732013-05-02 21:10:01 -07001696 case kWaitingForCheckPointsToRun: *pThreadStatus = JDWP::TS_WAIT; break;
Elliott Hughes9e0c1752013-01-09 14:02:58 -08001697 case kWaitingForJniOnLoad: *pThreadStatus = JDWP::TS_WAIT; break;
1698 case kWaitingForSignalCatcherOutput: *pThreadStatus = JDWP::TS_WAIT; break;
1699 case kWaitingInMainDebuggerLoop: *pThreadStatus = JDWP::TS_WAIT; break;
1700 case kWaitingInMainSignalCatcherLoop: *pThreadStatus = JDWP::TS_WAIT; break;
1701 case kWaitingPerformingGc: *pThreadStatus = JDWP::TS_WAIT; break;
1702 case kWaiting: *pThreadStatus = JDWP::TS_WAIT; break;
1703 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
1704 }
Elliott Hughes221229c2013-01-08 18:17:50 -08001705 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001706}
1707
Elliott Hughes221229c2013-01-08 18:17:50 -08001708JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001709 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -07001710 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001711 Thread* thread;
1712 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1713 if (error != JDWP::ERR_NONE) {
1714 return error;
Elliott Hughes2435a572012-02-17 16:07:41 -08001715 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001716 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001717 expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
Elliott Hughes2435a572012-02-17 16:07:41 -08001718 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001719}
1720
Elliott Hughesf9501702013-01-11 11:22:27 -08001721JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
1722 ScopedObjectAccess soa(Thread::Current());
1723 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1724 Thread* thread;
1725 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1726 if (error != JDWP::ERR_NONE) {
1727 return error;
1728 }
1729 thread->Interrupt();
1730 return JDWP::ERR_NONE;
1731}
1732
Elliott Hughescaf76542012-06-28 16:08:22 -07001733void Dbg::GetThreads(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& thread_ids) {
Ian Rogers365c1022012-06-22 15:05:28 -07001734 class ThreadListVisitor {
1735 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001736 ThreadListVisitor(const ScopedObjectAccessUnchecked& soa, mirror::Object* desired_thread_group,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001737 std::vector<JDWP::ObjectId>& thread_ids)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001738 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
jeffhao0dfbb7e2012-11-28 15:26:03 -08001739 : soa_(soa), desired_thread_group_(desired_thread_group), thread_ids_(thread_ids) {}
Ian Rogers365c1022012-06-22 15:05:28 -07001740
Elliott Hughesa2155262011-11-16 16:26:58 -08001741 static void Visit(Thread* t, void* arg) {
1742 reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t);
1743 }
1744
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001745 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
1746 // annotalysis.
1747 void Visit(Thread* t) NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughesa2155262011-11-16 16:26:58 -08001748 if (t == Dbg::GetDebugThread()) {
1749 // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
1750 // query all threads, so it's easier if we just don't tell them about this thread.
1751 return;
1752 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001753 mirror::Object* peer = t->GetPeer();
jeffhao0dfbb7e2012-11-28 15:26:03 -08001754 if (IsInDesiredThreadGroup(peer)) {
Ian Rogers120f1c72012-09-28 17:17:10 -07001755 thread_ids_.push_back(gRegistry->Add(peer));
Elliott Hughesa2155262011-11-16 16:26:58 -08001756 }
1757 }
1758
Ian Rogers365c1022012-06-22 15:05:28 -07001759 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001760 bool IsInDesiredThreadGroup(mirror::Object* peer)
jeffhao0dfbb7e2012-11-28 15:26:03 -08001761 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao0dfbb7e2012-11-28 15:26:03 -08001762 // peer might be NULL if the thread is still starting up.
1763 if (peer == NULL) {
1764 // We can't tell the debugger about this thread yet.
1765 // TODO: if we identified threads to the debugger by their Thread*
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001766 // rather than their peer's mirror::Object*, we could fix this.
jeffhao0dfbb7e2012-11-28 15:26:03 -08001767 // Doing so might help us report ZOMBIE threads too.
1768 return false;
1769 }
jeffhaoc1e04902012-12-13 12:41:10 -08001770 // Do we want threads from all thread groups?
1771 if (desired_thread_group_ == NULL) {
1772 return true;
1773 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001774 mirror::Object* group = soa_.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(peer);
jeffhao0dfbb7e2012-11-28 15:26:03 -08001775 return (group == desired_thread_group_);
1776 }
1777
Mathieu Chartierdbe6f462012-09-25 16:54:50 -07001778 const ScopedObjectAccessUnchecked& soa_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001779 mirror::Object* const desired_thread_group_;
Elliott Hughescaf76542012-06-28 16:08:22 -07001780 std::vector<JDWP::ObjectId>& thread_ids_;
Elliott Hughesa2155262011-11-16 16:26:58 -08001781 };
1782
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001783 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001784 mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001785 ThreadListVisitor tlv(soa, thread_group, thread_ids);
Ian Rogers50b35e22012-10-04 10:09:15 -07001786 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -07001787 Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv);
Elliott Hughescaf76542012-06-28 16:08:22 -07001788}
Elliott Hughesa2155262011-11-16 16:26:58 -08001789
Elliott Hughescaf76542012-06-28 16:08:22 -07001790void Dbg::GetChildThreadGroups(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& child_thread_group_ids) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001791 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001792 mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
Elliott Hughescaf76542012-06-28 16:08:22 -07001793
1794 // Get the ArrayList<ThreadGroup> "groups" out of this thread group...
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001795 mirror::Field* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;");
1796 mirror::Object* groups_array_list = groups_field->GetObject(thread_group);
Elliott Hughescaf76542012-06-28 16:08:22 -07001797
1798 // Get the array and size out of the ArrayList<ThreadGroup>...
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001799 mirror::Field* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;");
1800 mirror::Field* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I");
1801 mirror::ObjectArray<mirror::Object>* groups_array =
1802 array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>();
Elliott Hughescaf76542012-06-28 16:08:22 -07001803 const int32_t size = size_field->GetInt(groups_array_list);
1804
1805 // Copy the first 'size' elements out of the array into the result.
1806 for (int32_t i = 0; i < size; ++i) {
1807 child_thread_group_ids.push_back(gRegistry->Add(groups_array->Get(i)));
Elliott Hughesa2155262011-11-16 16:26:58 -08001808 }
1809}
1810
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001811static int GetStackDepth(Thread* thread)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001812 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -07001813 struct CountStackDepthVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08001814 CountStackDepthVisitor(Thread* thread)
1815 : StackVisitor(thread, NULL), depth(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -07001816
Elliott Hughes64f574f2013-02-20 14:57:12 -08001817 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
1818 // annotalysis.
1819 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers0399dde2012-06-06 17:09:28 -07001820 if (!GetMethod()->IsRuntimeMethod()) {
Elliott Hughesf8a2df72011-12-01 12:19:54 -08001821 ++depth;
1822 }
Elliott Hughes530fa002012-03-12 11:44:49 -07001823 return true;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001824 }
1825 size_t depth;
1826 };
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001827
Ian Rogers7a22fa62013-01-23 12:16:16 -08001828 CountStackDepthVisitor visitor(thread);
Ian Rogers0399dde2012-06-06 17:09:28 -07001829 visitor.WalkStack();
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001830 return visitor.depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001831}
1832
Elliott Hughes221229c2013-01-08 18:17:50 -08001833JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001834 ScopedObjectAccess soa(Thread::Current());
jeffhaoa77f0f62012-12-05 17:19:31 -08001835 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001836 Thread* thread;
1837 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1838 if (error != JDWP::ERR_NONE) {
1839 return error;
1840 }
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001841 if (!IsSuspendedForDebugger(soa, thread)) {
1842 return JDWP::ERR_THREAD_NOT_SUSPENDED;
1843 }
Elliott Hughes221229c2013-01-08 18:17:50 -08001844 result = GetStackDepth(thread);
1845 return JDWP::ERR_NONE;
Elliott Hughes86964332012-02-15 19:37:42 -08001846}
1847
Ian Rogers306057f2012-11-26 12:45:53 -08001848JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame,
1849 size_t frame_count, JDWP::ExpandBuf* buf) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001850 class GetFrameVisitor : public StackVisitor {
1851 public:
Ian Rogers7a22fa62013-01-23 12:16:16 -08001852 GetFrameVisitor(Thread* thread, size_t start_frame, size_t frame_count, JDWP::ExpandBuf* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001853 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08001854 : StackVisitor(thread, NULL), depth_(0),
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001855 start_frame_(start_frame), frame_count_(frame_count), buf_(buf) {
1856 expandBufAdd4BE(buf_, frame_count_);
Elliott Hughes03181a82011-11-17 17:22:21 -08001857 }
Ian Rogers0399dde2012-06-06 17:09:28 -07001858
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001859 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
1860 // annotalysis.
1861 virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers0399dde2012-06-06 17:09:28 -07001862 if (GetMethod()->IsRuntimeMethod()) {
Elliott Hughes530fa002012-03-12 11:44:49 -07001863 return true; // The debugger can't do anything useful with a frame that has no Method*.
Elliott Hughes03181a82011-11-17 17:22:21 -08001864 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001865 if (depth_ >= start_frame_ + frame_count_) {
Elliott Hughes530fa002012-03-12 11:44:49 -07001866 return false;
Elliott Hughes03181a82011-11-17 17:22:21 -08001867 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001868 if (depth_ >= start_frame_) {
1869 JDWP::FrameId frame_id(GetFrameId());
1870 JDWP::JdwpLocation location;
1871 SetLocation(location, GetMethod(), GetDexPc());
Elliott Hughes7baf96f2012-06-22 16:33:50 -07001872 VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3lld ", depth_, frame_id) << location;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001873 expandBufAdd8BE(buf_, frame_id);
1874 expandBufAddLocation(buf_, location);
1875 }
1876 ++depth_;
Elliott Hughes530fa002012-03-12 11:44:49 -07001877 return true;
Elliott Hughes03181a82011-11-17 17:22:21 -08001878 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001879
1880 private:
1881 size_t depth_;
1882 const size_t start_frame_;
1883 const size_t frame_count_;
1884 JDWP::ExpandBuf* buf_;
Elliott Hughes03181a82011-11-17 17:22:21 -08001885 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886
1887 ScopedObjectAccessUnchecked soa(Thread::Current());
jeffhaoa77f0f62012-12-05 17:19:31 -08001888 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001889 Thread* thread;
1890 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1891 if (error != JDWP::ERR_NONE) {
1892 return error;
1893 }
Elliott Hughesf15f4a02013-01-09 10:09:38 -08001894 if (!IsSuspendedForDebugger(soa, thread)) {
1895 return JDWP::ERR_THREAD_NOT_SUSPENDED;
1896 }
Ian Rogers7a22fa62013-01-23 12:16:16 -08001897 GetFrameVisitor visitor(thread, start_frame, frame_count, buf);
Ian Rogers0399dde2012-06-06 17:09:28 -07001898 visitor.WalkStack();
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001899 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001900}
1901
1902JDWP::ObjectId Dbg::GetThreadSelfId() {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -07001903 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogerscfaa4552012-11-26 21:00:08 -08001904 return gRegistry->Add(soa.Self()->GetPeer());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001905}
1906
Elliott Hughes475fc232011-10-25 15:00:35 -07001907void Dbg::SuspendVM() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001908 Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001909}
1910
1911void Dbg::ResumeVM() {
Elliott Hughesc61a2672012-06-21 14:52:29 -07001912 Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001913}
1914
Elliott Hughes221229c2013-01-08 18:17:50 -08001915JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001916 ScopedLocalRef<jobject> peer(Thread::Current()->GetJniEnv(), NULL);
1917 {
1918 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001919 peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id)));
Elliott Hughes4e235312011-12-02 11:34:15 -08001920 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001921 if (peer.get() == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001922 return JDWP::ERR_THREAD_NOT_ALIVE;
1923 }
1924 // Suspend thread to build stack trace.
Elliott Hughesf327e072013-01-09 16:01:26 -08001925 bool timed_out;
1926 Thread* thread = Thread::SuspendForDebugger(peer.get(), request_suspension, &timed_out);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001927 if (thread != NULL) {
1928 return JDWP::ERR_NONE;
Elliott Hughesf327e072013-01-09 16:01:26 -08001929 } else if (timed_out) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001930 return JDWP::ERR_INTERNAL;
1931 } else {
1932 return JDWP::ERR_THREAD_NOT_ALIVE;
1933 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001934}
1935
Elliott Hughes221229c2013-01-08 18:17:50 -08001936void Dbg::ResumeThread(JDWP::ObjectId thread_id) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001937 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001938 mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id);
jeffhaoa77f0f62012-12-05 17:19:31 -08001939 Thread* thread;
1940 {
1941 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1942 thread = Thread::FromManagedThread(soa, peer);
1943 }
Elliott Hughes4e235312011-12-02 11:34:15 -08001944 if (thread == NULL) {
1945 LOG(WARNING) << "No such thread for resume: " << peer;
1946 return;
1947 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001948 bool needs_resume;
1949 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001950 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 needs_resume = thread->GetSuspendCount() > 0;
1952 }
1953 if (needs_resume) {
Elliott Hughes546b9862012-06-20 16:06:13 -07001954 Runtime::Current()->GetThreadList()->Resume(thread, true);
1955 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001956}
1957
1958void Dbg::SuspendSelf() {
Elliott Hughes475fc232011-10-25 15:00:35 -07001959 Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001960}
1961
Ian Rogers0399dde2012-06-06 17:09:28 -07001962struct GetThisVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08001963 GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001964 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08001965 : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {}
Ian Rogers0399dde2012-06-06 17:09:28 -07001966
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001967 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
1968 // annotalysis.
1969 virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001970 if (frame_id != GetFrameId()) {
Ian Rogers0399dde2012-06-06 17:09:28 -07001971 return true; // continue
Ian Rogers0399dde2012-06-06 17:09:28 -07001972 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -08001973 this_object = GetThisObject();
1974 return false;
Ian Rogers0399dde2012-06-06 17:09:28 -07001975 }
Elliott Hughes86b00102011-12-05 17:54:26 -08001976 }
Ian Rogers0399dde2012-06-06 17:09:28 -07001977
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001978 mirror::Object* this_object;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001979 JDWP::FrameId frame_id;
Ian Rogers0399dde2012-06-06 17:09:28 -07001980};
1981
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001982JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
1983 JDWP::ObjectId* result) {
1984 ScopedObjectAccessUnchecked soa(Thread::Current());
1985 Thread* thread;
1986 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001987 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08001988 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1989 if (error != JDWP::ERR_NONE) {
1990 return error;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001991 }
Elliott Hughes9e0c1752013-01-09 14:02:58 -08001992 if (!IsSuspendedForDebugger(soa, thread)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001993 return JDWP::ERR_THREAD_NOT_SUSPENDED;
1994 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001995 }
Elliott Hughescaf76542012-06-28 16:08:22 -07001996 UniquePtr<Context> context(Context::Create());
Ian Rogers7a22fa62013-01-23 12:16:16 -08001997 GetThisVisitor visitor(thread, context.get(), frame_id);
Ian Rogers0399dde2012-06-06 17:09:28 -07001998 visitor.WalkStack();
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07001999 *result = gRegistry->Add(visitor.this_object);
2000 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002001}
2002
Elliott Hughes88d63092013-01-09 09:55:54 -08002003void Dbg::GetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002004 uint8_t* buf, size_t width) {
Ian Rogers0399dde2012-06-06 17:09:28 -07002005 struct GetLocalVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08002006 GetLocalVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id, int slot,
2007 JDWP::JdwpTag tag, uint8_t* buf, size_t width)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002008 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08002009 : StackVisitor(thread, context), frame_id_(frame_id), slot_(slot), tag_(tag),
Ian Rogersca190662012-06-26 15:45:57 -07002010 buf_(buf), width_(width) {}
2011
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002012 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2013 // annotalysis.
2014 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers0399dde2012-06-06 17:09:28 -07002015 if (GetFrameId() != frame_id_) {
2016 return true; // Not our frame, carry on.
Elliott Hughesdbb40792011-11-18 17:05:22 -08002017 }
Ian Rogers0399dde2012-06-06 17:09:28 -07002018 // TODO: check that the tag is compatible with the actual type of the slot!
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002019 mirror::AbstractMethod* m = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -07002020 uint16_t reg = DemangleSlot(slot_, m);
Elliott Hughesdbb40792011-11-18 17:05:22 -08002021
Ian Rogers0399dde2012-06-06 17:09:28 -07002022 switch (tag_) {
2023 case JDWP::JT_BOOLEAN:
2024 {
2025 CHECK_EQ(width_, 1U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002026 uint32_t intVal = GetVReg(m, reg, kIntVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002027 VLOG(jdwp) << "get boolean local " << reg << " = " << intVal;
2028 JDWP::Set1(buf_+1, intVal != 0);
2029 }
2030 break;
2031 case JDWP::JT_BYTE:
2032 {
2033 CHECK_EQ(width_, 1U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002034 uint32_t intVal = GetVReg(m, reg, kIntVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002035 VLOG(jdwp) << "get byte local " << reg << " = " << intVal;
2036 JDWP::Set1(buf_+1, intVal);
2037 }
2038 break;
2039 case JDWP::JT_SHORT:
2040 case JDWP::JT_CHAR:
2041 {
2042 CHECK_EQ(width_, 2U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002043 uint32_t intVal = GetVReg(m, reg, kIntVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002044 VLOG(jdwp) << "get short/char local " << reg << " = " << intVal;
2045 JDWP::Set2BE(buf_+1, intVal);
2046 }
2047 break;
2048 case JDWP::JT_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002049 {
2050 CHECK_EQ(width_, 4U);
2051 uint32_t intVal = GetVReg(m, reg, kIntVReg);
2052 VLOG(jdwp) << "get int local " << reg << " = " << intVal;
2053 JDWP::Set4BE(buf_+1, intVal);
2054 }
2055 break;
Ian Rogers0399dde2012-06-06 17:09:28 -07002056 case JDWP::JT_FLOAT:
2057 {
2058 CHECK_EQ(width_, 4U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002059 uint32_t intVal = GetVReg(m, reg, kFloatVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002060 VLOG(jdwp) << "get int/float local " << reg << " = " << intVal;
2061 JDWP::Set4BE(buf_+1, intVal);
2062 }
2063 break;
2064 case JDWP::JT_ARRAY:
2065 {
2066 CHECK_EQ(width_, sizeof(JDWP::ObjectId));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002067 mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
Ian Rogers0399dde2012-06-06 17:09:28 -07002068 VLOG(jdwp) << "get array local " << reg << " = " << o;
2069 if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
2070 LOG(FATAL) << "Register " << reg << " expected to hold array: " << o;
2071 }
2072 JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
2073 }
2074 break;
2075 case JDWP::JT_CLASS_LOADER:
2076 case JDWP::JT_CLASS_OBJECT:
2077 case JDWP::JT_OBJECT:
2078 case JDWP::JT_STRING:
2079 case JDWP::JT_THREAD:
2080 case JDWP::JT_THREAD_GROUP:
2081 {
2082 CHECK_EQ(width_, sizeof(JDWP::ObjectId));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002083 mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
Ian Rogers0399dde2012-06-06 17:09:28 -07002084 VLOG(jdwp) << "get object local " << reg << " = " << o;
2085 if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
2086 LOG(FATAL) << "Register " << reg << " expected to hold object: " << o;
2087 }
2088 tag_ = TagFromObject(o);
2089 JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
2090 }
2091 break;
2092 case JDWP::JT_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002093 {
2094 CHECK_EQ(width_, 8U);
2095 uint32_t lo = GetVReg(m, reg, kDoubleLoVReg);
2096 uint64_t hi = GetVReg(m, reg + 1, kDoubleHiVReg);
2097 uint64_t longVal = (hi << 32) | lo;
2098 VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
2099 JDWP::Set8BE(buf_+1, longVal);
2100 }
2101 break;
Ian Rogers0399dde2012-06-06 17:09:28 -07002102 case JDWP::JT_LONG:
2103 {
2104 CHECK_EQ(width_, 8U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002105 uint32_t lo = GetVReg(m, reg, kLongLoVReg);
2106 uint64_t hi = GetVReg(m, reg + 1, kLongHiVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002107 uint64_t longVal = (hi << 32) | lo;
2108 VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
2109 JDWP::Set8BE(buf_+1, longVal);
2110 }
2111 break;
2112 default:
2113 LOG(FATAL) << "Unknown tag " << tag_;
2114 break;
2115 }
2116
2117 // Prepend tag, which may have been updated.
2118 JDWP::Set1(buf_, tag_);
2119 return false;
2120 }
2121
2122 const JDWP::FrameId frame_id_;
2123 const int slot_;
2124 JDWP::JdwpTag tag_;
2125 uint8_t* const buf_;
2126 const size_t width_;
2127 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002128
2129 ScopedObjectAccessUnchecked soa(Thread::Current());
jeffhaoa77f0f62012-12-05 17:19:31 -08002130 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08002131 Thread* thread;
2132 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2133 if (error != JDWP::ERR_NONE) {
2134 return;
2135 }
Ian Rogers0399dde2012-06-06 17:09:28 -07002136 UniquePtr<Context> context(Context::Create());
Ian Rogers7a22fa62013-01-23 12:16:16 -08002137 GetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, buf, width);
Ian Rogers0399dde2012-06-06 17:09:28 -07002138 visitor.WalkStack();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002139}
2140
Elliott Hughes88d63092013-01-09 09:55:54 -08002141void Dbg::SetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag,
Ian Rogers0399dde2012-06-06 17:09:28 -07002142 uint64_t value, size_t width) {
2143 struct SetLocalVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08002144 SetLocalVisitor(Thread* thread, Context* context,
Ian Rogers0399dde2012-06-06 17:09:28 -07002145 JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint64_t value,
Ian Rogersca190662012-06-26 15:45:57 -07002146 size_t width)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08002148 : StackVisitor(thread, context),
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002149 frame_id_(frame_id), slot_(slot), tag_(tag), value_(value), width_(width) {}
Ian Rogersca190662012-06-26 15:45:57 -07002150
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002151 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2152 // annotalysis.
2153 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers0399dde2012-06-06 17:09:28 -07002154 if (GetFrameId() != frame_id_) {
2155 return true; // Not our frame, carry on.
2156 }
2157 // TODO: check that the tag is compatible with the actual type of the slot!
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002158 mirror::AbstractMethod* m = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -07002159 uint16_t reg = DemangleSlot(slot_, m);
2160
2161 switch (tag_) {
2162 case JDWP::JT_BOOLEAN:
2163 case JDWP::JT_BYTE:
2164 CHECK_EQ(width_, 1U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002165 SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002166 break;
2167 case JDWP::JT_SHORT:
2168 case JDWP::JT_CHAR:
2169 CHECK_EQ(width_, 2U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002170 SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002171 break;
2172 case JDWP::JT_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002173 CHECK_EQ(width_, 4U);
2174 SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
2175 break;
Ian Rogers0399dde2012-06-06 17:09:28 -07002176 case JDWP::JT_FLOAT:
2177 CHECK_EQ(width_, 4U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002178 SetVReg(m, reg, static_cast<uint32_t>(value_), kFloatVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002179 break;
2180 case JDWP::JT_ARRAY:
2181 case JDWP::JT_OBJECT:
2182 case JDWP::JT_STRING:
2183 {
2184 CHECK_EQ(width_, sizeof(JDWP::ObjectId));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002185 mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value_));
Elliott Hughes64f574f2013-02-20 14:57:12 -08002186 if (o == ObjectRegistry::kInvalidObject) {
Ian Rogers0399dde2012-06-06 17:09:28 -07002187 UNIMPLEMENTED(FATAL) << "return an error code when given an invalid object to store";
2188 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002189 SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), kReferenceVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002190 }
2191 break;
2192 case JDWP::JT_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002193 CHECK_EQ(width_, 8U);
2194 SetVReg(m, reg, static_cast<uint32_t>(value_), kDoubleLoVReg);
2195 SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kDoubleHiVReg);
2196 break;
Ian Rogers0399dde2012-06-06 17:09:28 -07002197 case JDWP::JT_LONG:
2198 CHECK_EQ(width_, 8U);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002199 SetVReg(m, reg, static_cast<uint32_t>(value_), kLongLoVReg);
2200 SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kLongHiVReg);
Ian Rogers0399dde2012-06-06 17:09:28 -07002201 break;
2202 default:
2203 LOG(FATAL) << "Unknown tag " << tag_;
2204 break;
2205 }
2206 return false;
2207 }
2208
2209 const JDWP::FrameId frame_id_;
2210 const int slot_;
2211 const JDWP::JdwpTag tag_;
2212 const uint64_t value_;
2213 const size_t width_;
2214 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002215
2216 ScopedObjectAccessUnchecked soa(Thread::Current());
jeffhaoa77f0f62012-12-05 17:19:31 -08002217 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08002218 Thread* thread;
2219 JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2220 if (error != JDWP::ERR_NONE) {
2221 return;
2222 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002223 UniquePtr<Context> context(Context::Create());
Ian Rogers7a22fa62013-01-23 12:16:16 -08002224 SetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, value, width);
Ian Rogers0399dde2012-06-06 17:09:28 -07002225 visitor.WalkStack();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002226}
2227
Ian Rogers62d6c772013-02-27 08:32:07 -08002228void Dbg::PostLocationEvent(const mirror::AbstractMethod* m, int dex_pc,
2229 mirror::Object* this_object, int event_flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002230 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002231
2232 JDWP::JdwpLocation location;
Elliott Hughes74847412012-06-20 18:10:21 -07002233 location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
Elliott Hughes64f574f2013-02-20 14:57:12 -08002234 location.class_id = gRegistry->AddRefType(c);
Elliott Hughes74847412012-06-20 18:10:21 -07002235 location.method_id = ToMethodId(m);
Elliott Hughes972a47b2012-02-21 18:16:06 -08002236 location.dex_pc = m->IsNative() ? -1 : dex_pc;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002237
Elliott Hughes64f574f2013-02-20 14:57:12 -08002238 // If 'this_object' isn't already in the registry, we know that we're not looking for it,
2239 // so there's no point adding it to the registry and burning through ids.
2240 JDWP::ObjectId this_id = 0;
2241 if (gRegistry->Contains(this_object)) {
2242 this_id = gRegistry->Add(this_object);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002243 }
Elliott Hughes64f574f2013-02-20 14:57:12 -08002244 gJdwpState->PostLocationEvent(&location, this_id, event_flags);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002245}
2246
Ian Rogers62d6c772013-02-27 08:32:07 -08002247void Dbg::PostException(Thread* thread, const ThrowLocation& throw_location,
2248 mirror::AbstractMethod* catch_method,
Elliott Hughes64f574f2013-02-20 14:57:12 -08002249 uint32_t catch_dex_pc, mirror::Throwable* exception_object) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07002250 if (!IsDebuggerActive()) {
Ian Rogers0ad5bb82011-12-07 10:16:32 -08002251 return;
2252 }
Elliott Hughes4740cdf2011-12-07 14:07:12 -08002253
Ian Rogers62d6c772013-02-27 08:32:07 -08002254 JDWP::JdwpLocation jdwp_throw_location;
2255 SetLocation(jdwp_throw_location, throw_location.GetMethod(), throw_location.GetDexPc());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002256 JDWP::JdwpLocation catch_location;
Elliott Hughescaf76542012-06-28 16:08:22 -07002257 SetLocation(catch_location, catch_method, catch_dex_pc);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002258
2259 // We need 'this' for InstanceOnly filters.
Ian Rogers62d6c772013-02-27 08:32:07 -08002260 JDWP::ObjectId this_id = gRegistry->Add(throw_location.GetThis());
Elliott Hughes64f574f2013-02-20 14:57:12 -08002261 JDWP::ObjectId exception_id = gRegistry->Add(exception_object);
2262 JDWP::RefTypeId exception_class_id = gRegistry->AddRefType(exception_object->GetClass());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002263
Ian Rogers62d6c772013-02-27 08:32:07 -08002264 gJdwpState->PostException(&jdwp_throw_location, exception_id, exception_class_id, &catch_location,
2265 this_id);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002266}
2267
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002268void Dbg::PostClassPrepare(mirror::Class* c) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07002269 if (!IsDebuggerActive()) {
Elliott Hughes4740cdf2011-12-07 14:07:12 -08002270 return;
2271 }
2272
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08002273 // OLD-TODO - we currently always send both "verified" and "prepared" since
Elliott Hughes4740cdf2011-12-07 14:07:12 -08002274 // debuggers seem to like that. There might be some advantage to honesty,
2275 // since the class may not yet be verified.
2276 int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
2277 JDWP::JdwpTypeTag tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
2278 gJdwpState->PostClassPrepare(tag, gRegistry->Add(c), ClassHelper(c).GetDescriptor(), state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002279}
2280
Ian Rogers62d6c772013-02-27 08:32:07 -08002281void Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object,
2282 const mirror::AbstractMethod* m, uint32_t dex_pc) {
2283 if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
Elliott Hughes2aa2e392012-02-17 17:15:43 -08002284 return;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002285 }
2286
Elliott Hughes2aa2e392012-02-17 17:15:43 -08002287 int event_flags = 0;
2288
Elliott Hughes86964332012-02-15 19:37:42 -08002289 if (IsBreakpoint(m, dex_pc)) {
2290 event_flags |= kBreakpoint;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002291 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002292
jeffhao09bfc6a2012-12-11 18:11:43 -08002293 {
2294 // If the debugger is single-stepping one of our threads, check to
2295 // see if we're that thread and we've reached a step point.
2296 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -08002297 if (gSingleStepControl.is_active && gSingleStepControl.thread == thread) {
jeffhao09bfc6a2012-12-11 18:11:43 -08002298 CHECK(!m->IsNative());
2299 if (gSingleStepControl.step_depth == JDWP::SD_INTO) {
2300 // Step into method calls. We break when the line number
2301 // or method pointer changes. If we're in SS_MIN mode, we
2302 // always stop.
2303 if (gSingleStepControl.method != m) {
2304 event_flags |= kSingleStep;
2305 VLOG(jdwp) << "SS new method";
2306 } else if (gSingleStepControl.step_size == JDWP::SS_MIN) {
Elliott Hughes86964332012-02-15 19:37:42 -08002307 event_flags |= kSingleStep;
2308 VLOG(jdwp) << "SS new instruction";
Elliott Hughes2435a572012-02-17 16:07:41 -08002309 } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) {
2310 event_flags |= kSingleStep;
2311 VLOG(jdwp) << "SS new line";
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002312 }
jeffhao09bfc6a2012-12-11 18:11:43 -08002313 } else if (gSingleStepControl.step_depth == JDWP::SD_OVER) {
2314 // Step over method calls. We break when the line number is
2315 // different and the frame depth is <= the original frame
2316 // depth. (We can't just compare on the method, because we
2317 // might get unrolled past it by an exception, and it's tricky
2318 // to identify recursion.)
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002319
Ian Rogers62d6c772013-02-27 08:32:07 -08002320 int stack_depth = GetStackDepth(thread);
Elliott Hughes86964332012-02-15 19:37:42 -08002321
jeffhao09bfc6a2012-12-11 18:11:43 -08002322 if (stack_depth < gSingleStepControl.stack_depth) {
2323 // popped up one or more frames, always trigger
2324 event_flags |= kSingleStep;
2325 VLOG(jdwp) << "SS method pop";
2326 } else if (stack_depth == gSingleStepControl.stack_depth) {
2327 // same depth, see if we moved
2328 if (gSingleStepControl.step_size == JDWP::SS_MIN) {
2329 event_flags |= kSingleStep;
2330 VLOG(jdwp) << "SS new instruction";
2331 } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) {
2332 event_flags |= kSingleStep;
2333 VLOG(jdwp) << "SS new line";
2334 }
2335 }
2336 } else {
2337 CHECK_EQ(gSingleStepControl.step_depth, JDWP::SD_OUT);
2338 // Return from the current method. We break when the frame
2339 // depth pops up.
2340
2341 // This differs from the "method exit" break in that it stops
2342 // with the PC at the next instruction in the returned-to
2343 // function, rather than the end of the returning function.
2344
Ian Rogers62d6c772013-02-27 08:32:07 -08002345 int stack_depth = GetStackDepth(thread);
jeffhao09bfc6a2012-12-11 18:11:43 -08002346 if (stack_depth < gSingleStepControl.stack_depth) {
2347 event_flags |= kSingleStep;
2348 VLOG(jdwp) << "SS method pop";
2349 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002350 }
2351 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002352 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002353
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002354 // If there's something interesting going on, see if it matches one
2355 // of the debugger filters.
2356 if (event_flags != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002357 Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002358 }
2359}
2360
Elliott Hughes86964332012-02-15 19:37:42 -08002361void Dbg::WatchLocation(const JDWP::JdwpLocation* location) {
jeffhao09bfc6a2012-12-11 18:11:43 -08002362 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002363 mirror::AbstractMethod* m = FromMethodId(location->method_id);
Elliott Hughes972a47b2012-02-21 18:16:06 -08002364 gBreakpoints.push_back(Breakpoint(m, location->dex_pc));
Elliott Hughes86964332012-02-15 19:37:42 -08002365 VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " << gBreakpoints[gBreakpoints.size() - 1];
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002366}
2367
Elliott Hughes86964332012-02-15 19:37:42 -08002368void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location) {
jeffhao09bfc6a2012-12-11 18:11:43 -08002369 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002370 mirror::AbstractMethod* m = FromMethodId(location->method_id);
Elliott Hughes86964332012-02-15 19:37:42 -08002371 for (size_t i = 0; i < gBreakpoints.size(); ++i) {
Elliott Hughes972a47b2012-02-21 18:16:06 -08002372 if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == location->dex_pc) {
Elliott Hughes86964332012-02-15 19:37:42 -08002373 VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
2374 gBreakpoints.erase(gBreakpoints.begin() + i);
2375 return;
2376 }
2377 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002378}
2379
Jeff Hao449db332013-04-12 18:30:52 -07002380// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
2381// cause suspension if the thread is the current thread.
2382class ScopedThreadSuspension {
2383 public:
Ian Rogers33e95662013-05-20 20:29:14 -07002384 ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
2385 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
Jeff Hao449db332013-04-12 18:30:52 -07002386 thread_(NULL),
2387 error_(JDWP::ERR_NONE),
2388 self_suspend_(false),
Ian Rogers33e95662013-05-20 20:29:14 -07002389 other_suspend_(false) {
Jeff Hao449db332013-04-12 18:30:52 -07002390 ScopedObjectAccessUnchecked soa(self);
2391 {
2392 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2393 error_ = DecodeThread(soa, thread_id, thread_);
2394 }
2395 if (error_ == JDWP::ERR_NONE) {
2396 if (thread_ == soa.Self()) {
2397 self_suspend_ = true;
2398 } else {
2399 soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
2400 jobject thread_peer = gRegistry->GetJObject(thread_id);
2401 bool timed_out;
2402 Thread* suspended_thread = Thread::SuspendForDebugger(thread_peer, true, &timed_out);
2403 CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension);
2404 if (suspended_thread == NULL) {
2405 // Thread terminated from under us while suspending.
2406 error_ = JDWP::ERR_INVALID_THREAD;
2407 } else {
2408 CHECK_EQ(suspended_thread, thread_);
2409 other_suspend_ = true;
2410 }
2411 }
2412 }
Elliott Hughes2435a572012-02-17 16:07:41 -08002413 }
Elliott Hughes86964332012-02-15 19:37:42 -08002414
Jeff Hao449db332013-04-12 18:30:52 -07002415 Thread* GetThread() const {
2416 return thread_;
2417 }
2418
2419 JDWP::JdwpError GetError() const {
2420 return error_;
2421 }
2422
2423 ~ScopedThreadSuspension() {
2424 if (other_suspend_) {
2425 Runtime::Current()->GetThreadList()->Resume(thread_, true);
2426 }
2427 }
2428
2429 private:
2430 Thread* thread_;
2431 JDWP::JdwpError error_;
2432 bool self_suspend_;
2433 bool other_suspend_;
2434};
2435
2436JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
2437 JDWP::JdwpStepDepth step_depth) {
2438 Thread* self = Thread::Current();
2439 ScopedThreadSuspension sts(self, thread_id);
2440 if (sts.GetError() != JDWP::ERR_NONE) {
2441 return sts.GetError();
2442 }
2443
2444 MutexLock mu2(self, *Locks::breakpoint_lock_);
Elliott Hughes86964332012-02-15 19:37:42 -08002445 // TODO: there's no theoretical reason why we couldn't support single-stepping
2446 // of multiple threads at once, but we never did so historically.
Jeff Hao449db332013-04-12 18:30:52 -07002447 if (gSingleStepControl.thread != NULL && sts.GetThread() != gSingleStepControl.thread) {
Elliott Hughes86964332012-02-15 19:37:42 -08002448 LOG(WARNING) << "single-step already active for " << *gSingleStepControl.thread
Jeff Hao449db332013-04-12 18:30:52 -07002449 << "; switching to " << *sts.GetThread();
Elliott Hughes86964332012-02-15 19:37:42 -08002450 }
2451
Elliott Hughes2435a572012-02-17 16:07:41 -08002452 //
2453 // Work out what Method* we're in, the current line number, and how deep the stack currently
2454 // is for step-out.
2455 //
2456
Ian Rogers0399dde2012-06-06 17:09:28 -07002457 struct SingleStepStackVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08002458 SingleStepStackVisitor(Thread* thread)
jeffhao09bfc6a2012-12-11 18:11:43 -08002459 EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002460 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08002461 : StackVisitor(thread, NULL) {
Elliott Hughes86964332012-02-15 19:37:42 -08002462 gSingleStepControl.method = NULL;
2463 gSingleStepControl.stack_depth = 0;
2464 }
Ian Rogersca190662012-06-26 15:45:57 -07002465
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002466 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2467 // annotalysis.
2468 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
jeffhao09bfc6a2012-12-11 18:11:43 -08002469 Locks::breakpoint_lock_->AssertHeld(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002470 const mirror::AbstractMethod* m = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -07002471 if (!m->IsRuntimeMethod()) {
Elliott Hughes86964332012-02-15 19:37:42 -08002472 ++gSingleStepControl.stack_depth;
2473 if (gSingleStepControl.method == NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002474 const mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache();
Elliott Hughes2435a572012-02-17 16:07:41 -08002475 gSingleStepControl.method = m;
2476 gSingleStepControl.line_number = -1;
2477 if (dex_cache != NULL) {
Ian Rogers4445a7e2012-10-05 17:19:13 -07002478 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers0399dde2012-06-06 17:09:28 -07002479 gSingleStepControl.line_number = dex_file.GetLineNumFromPC(m, GetDexPc());
Elliott Hughes2435a572012-02-17 16:07:41 -08002480 }
Elliott Hughes86964332012-02-15 19:37:42 -08002481 }
2482 }
Elliott Hughes530fa002012-03-12 11:44:49 -07002483 return true;
Elliott Hughes86964332012-02-15 19:37:42 -08002484 }
2485 };
Jeff Hao449db332013-04-12 18:30:52 -07002486
2487 SingleStepStackVisitor visitor(sts.GetThread());
Ian Rogers0399dde2012-06-06 17:09:28 -07002488 visitor.WalkStack();
Elliott Hughes86964332012-02-15 19:37:42 -08002489
Elliott Hughes2435a572012-02-17 16:07:41 -08002490 //
2491 // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
2492 //
2493
2494 struct DebugCallbackContext {
jeffhao09bfc6a2012-12-11 18:11:43 -08002495 DebugCallbackContext() EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_) {
Elliott Hughes2435a572012-02-17 16:07:41 -08002496 last_pc_valid = false;
2497 last_pc = 0;
Elliott Hughes2435a572012-02-17 16:07:41 -08002498 }
2499
jeffhao09bfc6a2012-12-11 18:11:43 -08002500 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2501 // annotalysis.
2502 static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) NO_THREAD_SAFETY_ANALYSIS {
2503 Locks::breakpoint_lock_->AssertHeld(Thread::Current());
Elliott Hughes2435a572012-02-17 16:07:41 -08002504 DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context);
2505 if (static_cast<int32_t>(line_number) == gSingleStepControl.line_number) {
2506 if (!context->last_pc_valid) {
2507 // Everything from this address until the next line change is ours.
2508 context->last_pc = address;
2509 context->last_pc_valid = true;
2510 }
2511 // Otherwise, if we're already in a valid range for this line,
2512 // just keep going (shouldn't really happen)...
2513 } else if (context->last_pc_valid) { // and the line number is new
2514 // Add everything from the last entry up until here to the set
2515 for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) {
2516 gSingleStepControl.dex_pcs.insert(dex_pc);
2517 }
2518 context->last_pc_valid = false;
2519 }
2520 return false; // There may be multiple entries for any given line.
2521 }
2522
jeffhao09bfc6a2012-12-11 18:11:43 -08002523 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2524 // annotalysis.
2525 ~DebugCallbackContext() NO_THREAD_SAFETY_ANALYSIS {
2526 Locks::breakpoint_lock_->AssertHeld(Thread::Current());
Elliott Hughes2435a572012-02-17 16:07:41 -08002527 // If the line number was the last in the position table...
2528 if (last_pc_valid) {
2529 size_t end = MethodHelper(gSingleStepControl.method).GetCodeItem()->insns_size_in_code_units_;
2530 for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) {
2531 gSingleStepControl.dex_pcs.insert(dex_pc);
2532 }
2533 }
2534 }
2535
2536 bool last_pc_valid;
2537 uint32_t last_pc;
2538 };
Elliott Hughes3e2e1a22012-02-21 11:33:41 -08002539 gSingleStepControl.dex_pcs.clear();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002540 const mirror::AbstractMethod* m = gSingleStepControl.method;
Elliott Hughes3e2e1a22012-02-21 11:33:41 -08002541 if (m->IsNative()) {
2542 gSingleStepControl.line_number = -1;
2543 } else {
2544 DebugCallbackContext context;
2545 MethodHelper mh(m);
2546 mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(),
2547 DebugCallbackContext::Callback, NULL, &context);
2548 }
Elliott Hughes2435a572012-02-17 16:07:41 -08002549
2550 //
2551 // Everything else...
2552 //
2553
Jeff Hao449db332013-04-12 18:30:52 -07002554 gSingleStepControl.thread = sts.GetThread();
Elliott Hughes86964332012-02-15 19:37:42 -08002555 gSingleStepControl.step_size = step_size;
2556 gSingleStepControl.step_depth = step_depth;
2557 gSingleStepControl.is_active = true;
2558
Elliott Hughes2435a572012-02-17 16:07:41 -08002559 if (VLOG_IS_ON(jdwp)) {
2560 VLOG(jdwp) << "Single-step thread: " << *gSingleStepControl.thread;
2561 VLOG(jdwp) << "Single-step step size: " << gSingleStepControl.step_size;
2562 VLOG(jdwp) << "Single-step step depth: " << gSingleStepControl.step_depth;
2563 VLOG(jdwp) << "Single-step current method: " << PrettyMethod(gSingleStepControl.method);
2564 VLOG(jdwp) << "Single-step current line: " << gSingleStepControl.line_number;
2565 VLOG(jdwp) << "Single-step current stack depth: " << gSingleStepControl.stack_depth;
2566 VLOG(jdwp) << "Single-step dex_pc values:";
2567 for (std::set<uint32_t>::iterator it = gSingleStepControl.dex_pcs.begin() ; it != gSingleStepControl.dex_pcs.end(); ++it) {
Elliott Hughes229feb72012-02-23 13:33:29 -08002568 VLOG(jdwp) << StringPrintf(" %#x", *it);
Elliott Hughes2435a572012-02-17 16:07:41 -08002569 }
2570 }
2571
2572 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002573}
2574
Elliott Hughes221229c2013-01-08 18:17:50 -08002575void Dbg::UnconfigureStep(JDWP::ObjectId /*thread_id*/) {
jeffhao09bfc6a2012-12-11 18:11:43 -08002576 MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -07002577
Elliott Hughes86964332012-02-15 19:37:42 -08002578 gSingleStepControl.is_active = false;
2579 gSingleStepControl.thread = NULL;
Elliott Hughes2435a572012-02-17 16:07:41 -08002580 gSingleStepControl.dex_pcs.clear();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002581}
2582
Elliott Hughes45651fd2012-02-21 15:48:20 -08002583static char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
2584 switch (tag) {
2585 default:
2586 LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
2587
2588 // Primitives.
2589 case JDWP::JT_BYTE: return 'B';
2590 case JDWP::JT_CHAR: return 'C';
2591 case JDWP::JT_FLOAT: return 'F';
2592 case JDWP::JT_DOUBLE: return 'D';
2593 case JDWP::JT_INT: return 'I';
2594 case JDWP::JT_LONG: return 'J';
2595 case JDWP::JT_SHORT: return 'S';
2596 case JDWP::JT_VOID: return 'V';
2597 case JDWP::JT_BOOLEAN: return 'Z';
2598
2599 // Reference types.
2600 case JDWP::JT_ARRAY:
2601 case JDWP::JT_OBJECT:
2602 case JDWP::JT_STRING:
2603 case JDWP::JT_THREAD:
2604 case JDWP::JT_THREAD_GROUP:
2605 case JDWP::JT_CLASS_LOADER:
2606 case JDWP::JT_CLASS_OBJECT:
2607 return 'L';
2608 }
2609}
2610
Elliott Hughes88d63092013-01-09 09:55:54 -08002611JDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id,
2612 JDWP::RefTypeId class_id, JDWP::MethodId method_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002613 uint32_t arg_count, uint64_t* arg_values,
2614 JDWP::JdwpTag* arg_types, uint32_t options,
2615 JDWP::JdwpTag* pResultTag, uint64_t* pResultValue,
2616 JDWP::ObjectId* pExceptionId) {
Elliott Hughesd07986f2011-12-06 18:27:45 -08002617 ThreadList* thread_list = Runtime::Current()->GetThreadList();
2618
2619 Thread* targetThread = NULL;
2620 DebugInvokeReq* req = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002621 Thread* self = Thread::Current();
Elliott Hughesd07986f2011-12-06 18:27:45 -08002622 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002623 ScopedObjectAccessUnchecked soa(self);
Ian Rogers50b35e22012-10-04 10:09:15 -07002624 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Elliott Hughes221229c2013-01-08 18:17:50 -08002625 JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread);
2626 if (error != JDWP::ERR_NONE) {
2627 LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
2628 return error;
Elliott Hughesd07986f2011-12-06 18:27:45 -08002629 }
2630 req = targetThread->GetInvokeReq();
2631 if (!req->ready) {
2632 LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
2633 return JDWP::ERR_INVALID_THREAD;
2634 }
2635
2636 /*
2637 * We currently have a bug where we don't successfully resume the
2638 * target thread if the suspend count is too deep. We're expected to
2639 * require one "resume" for each "suspend", but when asked to execute
2640 * a method we have to resume fully and then re-suspend it back to the
2641 * same level. (The easiest way to cause this is to type "suspend"
2642 * multiple times in jdb.)
2643 *
2644 * It's unclear what this means when the event specifies "resume all"
2645 * and some threads are suspended more deeply than others. This is
2646 * a rare problem, so for now we just prevent it from hanging forever
2647 * by rejecting the method invocation request. Without this, we will
2648 * be stuck waiting on a suspended thread.
2649 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002650 int suspend_count;
2651 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002652 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002653 suspend_count = targetThread->GetSuspendCount();
2654 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08002655 if (suspend_count > 1) {
2656 LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count;
2657 return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here.
2658 }
2659
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08002660 JDWP::JdwpError status;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002661 mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08002662 if (receiver == ObjectRegistry::kInvalidObject) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08002663 return JDWP::ERR_INVALID_OBJECT;
2664 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08002665
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002666 mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id);
Elliott Hughes64f574f2013-02-20 14:57:12 -08002667 if (thread == ObjectRegistry::kInvalidObject) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08002668 return JDWP::ERR_INVALID_OBJECT;
2669 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08002670 // TODO: check that 'thread' is actually a java.lang.Thread!
2671
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002672 mirror::Class* c = DecodeClass(class_id, status);
Elliott Hughes45651fd2012-02-21 15:48:20 -08002673 if (c == NULL) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08002674 return status;
2675 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08002676
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002677 mirror::AbstractMethod* m = FromMethodId(method_id);
Elliott Hughes45651fd2012-02-21 15:48:20 -08002678 if (m->IsStatic() != (receiver == NULL)) {
2679 return JDWP::ERR_INVALID_METHODID;
2680 }
2681 if (m->IsStatic()) {
2682 if (m->GetDeclaringClass() != c) {
2683 return JDWP::ERR_INVALID_METHODID;
2684 }
2685 } else {
2686 if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
2687 return JDWP::ERR_INVALID_METHODID;
2688 }
2689 }
2690
2691 // Check the argument list matches the method.
2692 MethodHelper mh(m);
2693 if (mh.GetShortyLength() - 1 != arg_count) {
2694 return JDWP::ERR_ILLEGAL_ARGUMENT;
2695 }
2696 const char* shorty = mh.GetShorty();
Elliott Hughes09201632013-04-15 15:50:07 -07002697 const DexFile::TypeList* types = mh.GetParameterTypeList();
Elliott Hughes45651fd2012-02-21 15:48:20 -08002698 for (size_t i = 0; i < arg_count; ++i) {
2699 if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
2700 return JDWP::ERR_ILLEGAL_ARGUMENT;
2701 }
Elliott Hughes09201632013-04-15 15:50:07 -07002702
2703 if (shorty[i + 1] == 'L') {
2704 // Did we really get an argument of an appropriate reference type?
2705 mirror::Class* parameter_type = mh.GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
2706 mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i]);
2707 if (argument == ObjectRegistry::kInvalidObject) {
2708 return JDWP::ERR_INVALID_OBJECT;
2709 }
2710 if (!argument->InstanceOf(parameter_type)) {
2711 return JDWP::ERR_ILLEGAL_ARGUMENT;
2712 }
2713
2714 // Turn the on-the-wire ObjectId into a jobject.
2715 jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
2716 v.l = gRegistry->GetJObject(arg_values[i]);
2717 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08002718 }
2719
2720 req->receiver_ = receiver;
2721 req->thread_ = thread;
2722 req->class_ = c;
2723 req->method_ = m;
2724 req->arg_count_ = arg_count;
2725 req->arg_values_ = arg_values;
Elliott Hughesd07986f2011-12-06 18:27:45 -08002726 req->options_ = options;
2727 req->invoke_needed_ = true;
2728 }
2729
2730 // The fact that we've released the thread list lock is a bit risky --- if the thread goes
2731 // away we're sitting high and dry -- but we must release this before the ResumeAllThreads
2732 // call, and it's unwise to hold it during WaitForSuspend.
2733
2734 {
2735 /*
2736 * We change our (JDWP thread) status, which should be THREAD_RUNNING,
Elliott Hughes81ff3182012-03-23 20:35:56 -07002737 * so we can suspend for a GC if the invoke request causes us to
Elliott Hughesd07986f2011-12-06 18:27:45 -08002738 * run out of memory. It's also a good idea to change it before locking
2739 * the invokeReq mutex, although that should never be held for long.
2740 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002741 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002742
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002743 VLOG(jdwp) << " Transferring control to event thread";
Elliott Hughesd07986f2011-12-06 18:27:45 -08002744 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002745 MutexLock mu(self, req->lock_);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002746
2747 if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002748 VLOG(jdwp) << " Resuming all threads";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002749 thread_list->UndoDebuggerSuspensions();
Elliott Hughesd07986f2011-12-06 18:27:45 -08002750 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002751 VLOG(jdwp) << " Resuming event thread only";
Elliott Hughesd07986f2011-12-06 18:27:45 -08002752 thread_list->Resume(targetThread, true);
2753 }
2754
2755 // Wait for the request to finish executing.
2756 while (req->invoke_needed_) {
Ian Rogersc604d732012-10-14 16:09:54 -07002757 req->cond_.Wait(self);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002758 }
2759 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002760 VLOG(jdwp) << " Control has returned from event thread";
Elliott Hughesd07986f2011-12-06 18:27:45 -08002761
2762 /* wait for thread to re-suspend itself */
Elliott Hughes221229c2013-01-08 18:17:50 -08002763 SuspendThread(thread_id, false /* request_suspension */ );
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002764 self->TransitionFromSuspendedToRunnable();
Elliott Hughesd07986f2011-12-06 18:27:45 -08002765 }
2766
2767 /*
2768 * Suspend the threads. We waited for the target thread to suspend
2769 * itself, so all we need to do is suspend the others.
2770 *
2771 * The suspendAllThreads() call will double-suspend the event thread,
2772 * so we want to resume the target thread once to keep the books straight.
2773 */
2774 if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002775 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002776 VLOG(jdwp) << " Suspending all threads";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002777 thread_list->SuspendAllForDebugger();
2778 self->TransitionFromSuspendedToRunnable();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002779 VLOG(jdwp) << " Resuming event thread to balance the count";
Elliott Hughesd07986f2011-12-06 18:27:45 -08002780 thread_list->Resume(targetThread, true);
2781 }
2782
2783 // Copy the result.
2784 *pResultTag = req->result_tag;
2785 if (IsPrimitiveTag(req->result_tag)) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07002786 *pResultValue = req->result_value.GetJ();
Elliott Hughesd07986f2011-12-06 18:27:45 -08002787 } else {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07002788 *pResultValue = gRegistry->Add(req->result_value.GetL());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002789 }
2790 *pExceptionId = req->exception;
2791 return req->error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002792}
2793
2794void Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002795 ScopedObjectAccess soa(Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002796
Elliott Hughes81ff3182012-03-23 20:35:56 -07002797 // We can be called while an exception is pending. We need
Elliott Hughesd07986f2011-12-06 18:27:45 -08002798 // to preserve that across the method invocation.
Ian Rogers62d6c772013-02-27 08:32:07 -08002799 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), NULL);
2800 SirtRef<mirror::AbstractMethod> old_throw_method(soa.Self(), NULL);
2801 SirtRef<mirror::Throwable> old_exception(soa.Self(), NULL);
2802 uint32_t old_throw_dex_pc;
2803 {
2804 ThrowLocation old_throw_location;
2805 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
2806 old_throw_this_object.reset(old_throw_location.GetThis());
2807 old_throw_method.reset(old_throw_location.GetMethod());
2808 old_exception.reset(old_exception_obj);
2809 old_throw_dex_pc = old_throw_location.GetDexPc();
2810 soa.Self()->ClearException();
2811 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08002812
2813 // Translate the method through the vtable, unless the debugger wants to suppress it.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002814 mirror::AbstractMethod* m = pReq->method_;
Elliott Hughesd07986f2011-12-06 18:27:45 -08002815 if ((pReq->options_ & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002816 mirror::AbstractMethod* actual_method = pReq->class_->FindVirtualMethodForVirtualOrInterface(pReq->method_);
Elliott Hughes45651fd2012-02-21 15:48:20 -08002817 if (actual_method != m) {
2818 VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) << " to " << PrettyMethod(actual_method);
2819 m = actual_method;
2820 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08002821 }
Elliott Hughescfa9cfa2013-04-16 16:52:01 -07002822 VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m)
2823 << " receiver=" << pReq->receiver_
2824 << " arg_count=" << pReq->arg_count_;
Elliott Hughesd07986f2011-12-06 18:27:45 -08002825 CHECK(m != NULL);
2826
2827 CHECK_EQ(sizeof(jvalue), sizeof(uint64_t));
2828
Jeff Hao5d917302013-02-27 17:57:33 -08002829 MethodHelper mh(m);
2830 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
2831 arg_array.BuildArgArray(soa, pReq->receiver_, reinterpret_cast<jvalue*>(pReq->arg_values_));
Jeff Hao6474d192013-03-26 14:08:09 -07002832 InvokeWithArgArray(soa, m, &arg_array, &pReq->result_value, mh.GetShorty()[0]);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002833
Ian Rogers62d6c772013-02-27 08:32:07 -08002834 mirror::Throwable* exception = soa.Self()->GetException(NULL);
2835 soa.Self()->ClearException();
2836 pReq->exception = gRegistry->Add(exception);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002837 pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty());
2838 if (pReq->exception != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002839 VLOG(jdwp) << " JDWP invocation returning with exception=" << exception
2840 << " " << exception->Dump();
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07002841 pReq->result_value.SetJ(0);
Elliott Hughesd07986f2011-12-06 18:27:45 -08002842 } else if (pReq->result_tag == JDWP::JT_OBJECT) {
2843 /* if no exception thrown, examine object result more closely */
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07002844 JDWP::JdwpTag new_tag = TagFromObject(pReq->result_value.GetL());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002845 if (new_tag != pReq->result_tag) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002846 VLOG(jdwp) << " JDWP promoted result from " << pReq->result_tag << " to " << new_tag;
Elliott Hughesd07986f2011-12-06 18:27:45 -08002847 pReq->result_tag = new_tag;
2848 }
2849
2850 /*
2851 * Register the object. We don't actually need an ObjectId yet,
2852 * but we do need to be sure that the GC won't move or discard the
2853 * object when we switch out of RUNNING. The ObjectId conversion
2854 * will add the object to the "do not touch" list.
2855 *
2856 * We can't use the "tracked allocation" mechanism here because
2857 * the object is going to be handed off to a different thread.
2858 */
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07002859 gRegistry->Add(pReq->result_value.GetL());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002860 }
2861
2862 if (old_exception.get() != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002863 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
2864 old_throw_dex_pc);
2865 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughesd07986f2011-12-06 18:27:45 -08002866 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002867}
2868
Elliott Hughesd07986f2011-12-06 18:27:45 -08002869/*
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002870 * "request" contains a full JDWP packet, possibly with multiple chunks. We
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002871 * need to process each, accumulate the replies, and ship the whole thing
2872 * back.
2873 *
2874 * Returns "true" if we have a reply. The reply buffer is newly allocated,
2875 * and includes the chunk type/length, followed by the data.
2876 *
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08002877 * OLD-TODO: we currently assume that the request and reply include a single
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002878 * chunk. If this becomes inconvenient we will need to adapt.
2879 */
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002880bool Dbg::DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002881 Thread* self = Thread::Current();
2882 JNIEnv* env = self->GetJniEnv();
2883
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002884 uint32_t type = request.ReadUnsigned32("type");
2885 uint32_t length = request.ReadUnsigned32("length");
2886
2887 // Create a byte[] corresponding to 'request'.
2888 size_t request_length = request.size();
2889 ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length));
Elliott Hughes6a5bd492011-10-28 14:33:57 -07002890 if (dataArray.get() == NULL) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002891 LOG(WARNING) << "byte[] allocation failed: " << request_length;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002892 env->ExceptionClear();
2893 return false;
2894 }
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002895 env->SetByteArrayRegion(dataArray.get(), 0, request_length, reinterpret_cast<const jbyte*>(request.data()));
2896 request.Skip(request_length);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002897
2898 // Run through and find all chunks. [Currently just find the first.]
Elliott Hughes6a5bd492011-10-28 14:33:57 -07002899 ScopedByteArrayRO contents(env, dataArray.get());
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002900 if (length != request_length) {
2901 LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, request_length);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002902 return false;
2903 }
2904
2905 // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
Elliott Hugheseac76672012-05-24 21:56:51 -07002906 ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
2907 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002908 type, dataArray.get(), 0, length));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002909 if (env->ExceptionCheck()) {
2910 LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
2911 env->ExceptionDescribe();
2912 env->ExceptionClear();
2913 return false;
2914 }
2915
Elliott Hughes6a5bd492011-10-28 14:33:57 -07002916 if (chunk.get() == NULL) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002917 return false;
2918 }
2919
2920 /*
2921 * Pull the pieces out of the chunk. We copy the results into a
2922 * newly-allocated buffer that the caller can free. We don't want to
2923 * continue using the Chunk object because nothing has a reference to it.
2924 *
2925 * We could avoid this by returning type/data/offset/length and having
2926 * the caller be aware of the object lifetime issues, but that
Elliott Hughes81ff3182012-03-23 20:35:56 -07002927 * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002928 * if we have responses for multiple chunks.
2929 *
2930 * So we're pretty much stuck with copying data around multiple times.
2931 */
Elliott Hugheseac76672012-05-24 21:56:51 -07002932 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 -08002933 jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
Elliott Hugheseac76672012-05-24 21:56:51 -07002934 length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
Elliott Hugheseac76672012-05-24 21:56:51 -07002935 type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002936
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002937 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 -07002938 if (length == 0 || replyData.get() == NULL) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002939 return false;
2940 }
2941
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002942 const int kChunkHdrLen = 8;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002943 uint8_t* reply = new uint8_t[length + kChunkHdrLen];
2944 if (reply == NULL) {
2945 LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
2946 return false;
2947 }
Elliott Hughesf7c3b662011-10-27 12:04:56 -07002948 JDWP::Set4BE(reply + 0, type);
2949 JDWP::Set4BE(reply + 4, length);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07002950 env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002951
2952 *pReplyBuf = reply;
2953 *pReplyLen = length + kChunkHdrLen;
2954
Elliott Hughes4b9702c2013-02-20 18:13:24 -08002955 VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07002956 return true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002957}
2958
Elliott Hughesa2155262011-11-16 16:26:58 -08002959void Dbg::DdmBroadcast(bool connect) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002960 VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
Elliott Hughes47fce012011-10-25 18:37:19 -07002961
2962 Thread* self = Thread::Current();
Ian Rogers50b35e22012-10-04 10:09:15 -07002963 if (self->GetState() != kRunnable) {
2964 LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
2965 /* try anyway? */
Elliott Hughes47fce012011-10-25 18:37:19 -07002966 }
2967
2968 JNIEnv* env = self->GetJniEnv();
Elliott Hughes47fce012011-10-25 18:37:19 -07002969 jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
Elliott Hugheseac76672012-05-24 21:56:51 -07002970 env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
2971 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
2972 event);
Elliott Hughes47fce012011-10-25 18:37:19 -07002973 if (env->ExceptionCheck()) {
2974 LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
2975 env->ExceptionDescribe();
2976 env->ExceptionClear();
2977 }
2978}
2979
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002980void Dbg::DdmConnected() {
Elliott Hughesa2155262011-11-16 16:26:58 -08002981 Dbg::DdmBroadcast(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002982}
2983
2984void Dbg::DdmDisconnected() {
Elliott Hughesa2155262011-11-16 16:26:58 -08002985 Dbg::DdmBroadcast(false);
Elliott Hughes47fce012011-10-25 18:37:19 -07002986 gDdmThreadNotification = false;
2987}
2988
2989/*
Elliott Hughes82188472011-11-07 18:11:48 -08002990 * Send a notification when a thread starts, stops, or changes its name.
Elliott Hughes47fce012011-10-25 18:37:19 -07002991 *
2992 * Because we broadcast the full set of threads when the notifications are
2993 * first enabled, it's possible for "thread" to be actively executing.
2994 */
Elliott Hughes82188472011-11-07 18:11:48 -08002995void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
Elliott Hughes47fce012011-10-25 18:37:19 -07002996 if (!gDdmThreadNotification) {
2997 return;
2998 }
2999
Elliott Hughes82188472011-11-07 18:11:48 -08003000 if (type == CHUNK_TYPE("THDE")) {
Elliott Hughes47fce012011-10-25 18:37:19 -07003001 uint8_t buf[4];
Elliott Hughesf7c3b662011-10-27 12:04:56 -07003002 JDWP::Set4BE(&buf[0], t->GetThinLockId());
Elliott Hughes47fce012011-10-25 18:37:19 -07003003 Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
Elliott Hughes82188472011-11-07 18:11:48 -08003004 } else {
3005 CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003006 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003007 SirtRef<mirror::String> name(soa.Self(), t->GetThreadName(soa));
Elliott Hughes82188472011-11-07 18:11:48 -08003008 size_t char_count = (name.get() != NULL) ? name->GetLength() : 0;
jeffhao725a9572012-11-13 18:20:12 -08003009 const jchar* chars = (name.get() != NULL) ? name->GetCharArray()->GetData() : NULL;
Elliott Hughes82188472011-11-07 18:11:48 -08003010
Elliott Hughes21f32d72011-11-09 17:44:13 -08003011 std::vector<uint8_t> bytes;
Elliott Hughes545a0642011-11-08 19:10:03 -08003012 JDWP::Append4BE(bytes, t->GetThinLockId());
3013 JDWP::AppendUtf16BE(bytes, chars, char_count);
Elliott Hughes21f32d72011-11-09 17:44:13 -08003014 CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
3015 Dbg::DdmSendChunk(type, bytes);
Elliott Hughes47fce012011-10-25 18:37:19 -07003016 }
3017}
3018
Elliott Hughes47fce012011-10-25 18:37:19 -07003019void Dbg::DdmSetThreadNotification(bool enable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003020 // Enable/disable thread notifications.
Elliott Hughes47fce012011-10-25 18:37:19 -07003021 gDdmThreadNotification = enable;
3022 if (enable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003023 // Suspend the VM then post thread start notifications for all threads. Threads attaching will
3024 // see a suspension in progress and block until that ends. They then post their own start
3025 // notification.
3026 SuspendVM();
3027 std::list<Thread*> threads;
Ian Rogers50b35e22012-10-04 10:09:15 -07003028 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003029 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003030 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003031 threads = Runtime::Current()->GetThreadList()->GetList();
3032 }
3033 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003034 ScopedObjectAccess soa(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003035 typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto
3036 for (It it = threads.begin(), end = threads.end(); it != end; ++it) {
3037 Dbg::DdmSendThreadNotification(*it, CHUNK_TYPE("THCR"));
3038 }
3039 }
3040 ResumeVM();
Elliott Hughes47fce012011-10-25 18:37:19 -07003041 }
3042}
3043
Elliott Hughesa2155262011-11-16 16:26:58 -08003044void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07003045 if (IsDebuggerActive()) {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -07003046 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogerscfaa4552012-11-26 21:00:08 -08003047 JDWP::ObjectId id = gRegistry->Add(t->GetPeer());
Elliott Hughes82188472011-11-07 18:11:48 -08003048 gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR"));
Elliott Hughes47fce012011-10-25 18:37:19 -07003049 }
Elliott Hughes82188472011-11-07 18:11:48 -08003050 Dbg::DdmSendThreadNotification(t, type);
Elliott Hughes47fce012011-10-25 18:37:19 -07003051}
3052
3053void Dbg::PostThreadStart(Thread* t) {
Elliott Hughesa2155262011-11-16 16:26:58 -08003054 Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
Elliott Hughes47fce012011-10-25 18:37:19 -07003055}
3056
3057void Dbg::PostThreadDeath(Thread* t) {
Elliott Hughesa2155262011-11-16 16:26:58 -08003058 Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003059}
3060
Elliott Hughes82188472011-11-07 18:11:48 -08003061void Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) {
Elliott Hughes3bb81562011-10-21 18:52:59 -07003062 CHECK(buf != NULL);
3063 iovec vec[1];
3064 vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
3065 vec[0].iov_len = byte_count;
3066 Dbg::DdmSendChunkV(type, vec, 1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003067}
3068
Elliott Hughes21f32d72011-11-09 17:44:13 -08003069void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) {
3070 DdmSendChunk(type, bytes.size(), &bytes[0]);
3071}
3072
Elliott Hughescccd84f2011-12-05 16:51:54 -08003073void Dbg::DdmSendChunkV(uint32_t type, const struct iovec* iov, int iov_count) {
Elliott Hughes3bb81562011-10-21 18:52:59 -07003074 if (gJdwpState == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003075 VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
Elliott Hughes3bb81562011-10-21 18:52:59 -07003076 } else {
Elliott Hughescccd84f2011-12-05 16:51:54 -08003077 gJdwpState->DdmSendChunkV(type, iov, iov_count);
Elliott Hughes3bb81562011-10-21 18:52:59 -07003078 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003079}
3080
Elliott Hughes767a1472011-10-26 18:49:02 -07003081int Dbg::DdmHandleHpifChunk(HpifWhen when) {
3082 if (when == HPIF_WHEN_NOW) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07003083 DdmSendHeapInfo(when);
Elliott Hughes767a1472011-10-26 18:49:02 -07003084 return true;
3085 }
3086
3087 if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
3088 LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
3089 return false;
3090 }
3091
3092 gDdmHpifWhen = when;
3093 return true;
3094}
3095
3096bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
3097 if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
3098 LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
3099 return false;
3100 }
3101
3102 if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
3103 LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
3104 return false;
3105 }
3106
3107 if (native) {
3108 gDdmNhsgWhen = when;
3109 gDdmNhsgWhat = what;
3110 } else {
3111 gDdmHpsgWhen = when;
3112 gDdmHpsgWhat = what;
3113 }
3114 return true;
3115}
3116
Elliott Hughes7162ad92011-10-27 14:08:42 -07003117void Dbg::DdmSendHeapInfo(HpifWhen reason) {
3118 // If there's a one-shot 'when', reset it.
3119 if (reason == gDdmHpifWhen) {
3120 if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
3121 gDdmHpifWhen = HPIF_WHEN_NEVER;
3122 }
3123 }
3124
3125 /*
3126 * Chunk HPIF (client --> server)
3127 *
3128 * Heap Info. General information about the heap,
3129 * suitable for a summary display.
3130 *
3131 * [u4]: number of heaps
3132 *
3133 * For each heap:
3134 * [u4]: heap ID
3135 * [u8]: timestamp in ms since Unix epoch
3136 * [u1]: capture reason (same as 'when' value from server)
3137 * [u4]: max heap size in bytes (-Xmx)
3138 * [u4]: current heap size in bytes
3139 * [u4]: current number of bytes allocated
3140 * [u4]: current number of objects allocated
3141 */
3142 uint8_t heap_count = 1;
Ian Rogers1d54e732013-05-02 21:10:01 -07003143 gc::Heap* heap = Runtime::Current()->GetHeap();
Elliott Hughes21f32d72011-11-09 17:44:13 -08003144 std::vector<uint8_t> bytes;
Elliott Hughes545a0642011-11-08 19:10:03 -08003145 JDWP::Append4BE(bytes, heap_count);
3146 JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap).
3147 JDWP::Append8BE(bytes, MilliTime());
3148 JDWP::Append1BE(bytes, reason);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003149 JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes.
3150 JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes.
3151 JDWP::Append4BE(bytes, heap->GetBytesAllocated());
3152 JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
Elliott Hughes21f32d72011-11-09 17:44:13 -08003153 CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
3154 Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes);
Elliott Hughes767a1472011-10-26 18:49:02 -07003155}
3156
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003157enum HpsgSolidity {
3158 SOLIDITY_FREE = 0,
3159 SOLIDITY_HARD = 1,
3160 SOLIDITY_SOFT = 2,
3161 SOLIDITY_WEAK = 3,
3162 SOLIDITY_PHANTOM = 4,
3163 SOLIDITY_FINALIZABLE = 5,
3164 SOLIDITY_SWEEP = 6,
3165};
3166
3167enum HpsgKind {
3168 KIND_OBJECT = 0,
3169 KIND_CLASS_OBJECT = 1,
3170 KIND_ARRAY_1 = 2,
3171 KIND_ARRAY_2 = 3,
3172 KIND_ARRAY_4 = 4,
3173 KIND_ARRAY_8 = 5,
3174 KIND_UNKNOWN = 6,
3175 KIND_NATIVE = 7,
3176};
3177
3178#define HPSG_PARTIAL (1<<7)
3179#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
3180
Ian Rogers30fab402012-01-23 15:43:46 -08003181class HeapChunkContext {
3182 public:
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003183 // Maximum chunk size. Obtain this from the formula:
3184 // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
3185 HeapChunkContext(bool merge, bool native)
Ian Rogers30fab402012-01-23 15:43:46 -08003186 : buf_(16384 - 16),
3187 type_(0),
3188 merge_(merge) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003189 Reset();
3190 if (native) {
Ian Rogers30fab402012-01-23 15:43:46 -08003191 type_ = CHUNK_TYPE("NHSG");
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003192 } else {
Ian Rogers30fab402012-01-23 15:43:46 -08003193 type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003194 }
3195 }
3196
3197 ~HeapChunkContext() {
Ian Rogers30fab402012-01-23 15:43:46 -08003198 if (p_ > &buf_[0]) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003199 Flush();
3200 }
3201 }
3202
3203 void EnsureHeader(const void* chunk_ptr) {
Ian Rogers30fab402012-01-23 15:43:46 -08003204 if (!needHeader_) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003205 return;
3206 }
3207
3208 // Start a new HPSx chunk.
Ian Rogers30fab402012-01-23 15:43:46 -08003209 JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap).
3210 JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes.
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003211
Ian Rogers30fab402012-01-23 15:43:46 -08003212 JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start.
3213 JDWP::Write4BE(&p_, 0); // offset of this piece (relative to the virtual address).
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003214 // [u4]: length of piece, in allocation units
3215 // 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 -08003216 pieceLenField_ = p_;
3217 JDWP::Write4BE(&p_, 0x55555555);
3218 needHeader_ = false;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003219 }
3220
Ian Rogersb726dcb2012-09-05 08:57:23 -07003221 void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd636b062013-01-18 17:51:18 -08003222 if (pieceLenField_ == NULL) {
3223 // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
3224 CHECK(needHeader_);
3225 return;
3226 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003227 // Patch the "length of piece" field.
Ian Rogers30fab402012-01-23 15:43:46 -08003228 CHECK_LE(&buf_[0], pieceLenField_);
3229 CHECK_LE(pieceLenField_, p_);
3230 JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003231
Ian Rogers30fab402012-01-23 15:43:46 -08003232 Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003233 Reset();
3234 }
3235
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003236 static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07003237 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3238 Locks::mutator_lock_) {
Ian Rogers30fab402012-01-23 15:43:46 -08003239 reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes);
Elliott Hughesa2155262011-11-16 16:26:58 -08003240 }
3241
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003242 private:
Elliott Hughesa2155262011-11-16 16:26:58 -08003243 enum { ALLOCATION_UNIT_SIZE = 8 };
3244
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003245 void Reset() {
Ian Rogers30fab402012-01-23 15:43:46 -08003246 p_ = &buf_[0];
Ian Rogers15bf2d32012-08-28 17:33:04 -07003247 startOfNextMemoryChunk_ = NULL;
Ian Rogers30fab402012-01-23 15:43:46 -08003248 totalAllocationUnits_ = 0;
3249 needHeader_ = true;
3250 pieceLenField_ = NULL;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003251 }
3252
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003253 void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes)
Ian Rogersb726dcb2012-09-05 08:57:23 -07003254 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3255 Locks::mutator_lock_) {
Ian Rogers30fab402012-01-23 15:43:46 -08003256 // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
3257 // in the following code not to allocate memory, by ensuring buf_ is of the correct size
Ian Rogers15bf2d32012-08-28 17:33:04 -07003258 if (used_bytes == 0) {
3259 if (start == NULL) {
3260 // Reset for start of new heap.
3261 startOfNextMemoryChunk_ = NULL;
3262 Flush();
3263 }
3264 // Only process in use memory so that free region information
3265 // also includes dlmalloc book keeping.
Elliott Hughesa2155262011-11-16 16:26:58 -08003266 return;
Elliott Hughesa2155262011-11-16 16:26:58 -08003267 }
3268
Ian Rogers15bf2d32012-08-28 17:33:04 -07003269 /* If we're looking at the native heap, we'll just return
3270 * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks
3271 */
3272 bool native = type_ == CHUNK_TYPE("NHSG");
3273
3274 if (startOfNextMemoryChunk_ != NULL) {
3275 // Transmit any pending free memory. Native free memory of
3276 // over kMaxFreeLen could be because of the use of mmaps, so
3277 // don't report. If not free memory then start a new segment.
3278 bool flush = true;
3279 if (start > startOfNextMemoryChunk_) {
3280 const size_t kMaxFreeLen = 2 * kPageSize;
3281 void* freeStart = startOfNextMemoryChunk_;
3282 void* freeEnd = start;
3283 size_t freeLen = (char*)freeEnd - (char*)freeStart;
3284 if (!native || freeLen < kMaxFreeLen) {
3285 AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), freeStart, freeLen);
3286 flush = false;
3287 }
3288 }
3289 if (flush) {
3290 startOfNextMemoryChunk_ = NULL;
3291 Flush();
3292 }
3293 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003294 const mirror::Object* obj = reinterpret_cast<const mirror::Object*>(start);
Elliott Hughesa2155262011-11-16 16:26:58 -08003295
3296 // Determine the type of this chunk.
3297 // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
3298 // If it's the same, we should combine them.
Ian Rogers15bf2d32012-08-28 17:33:04 -07003299 uint8_t state = ExamineObject(obj, native);
3300 // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
3301 // allocation then the first sizeof(size_t) may belong to it.
3302 const size_t dlMallocOverhead = sizeof(size_t);
3303 AppendChunk(state, start, used_bytes + dlMallocOverhead);
3304 startOfNextMemoryChunk_ = (char*)start + used_bytes + dlMallocOverhead;
3305 }
Elliott Hughesa2155262011-11-16 16:26:58 -08003306
Ian Rogers15bf2d32012-08-28 17:33:04 -07003307 void AppendChunk(uint8_t state, void* ptr, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07003308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07003309 // Make sure there's enough room left in the buffer.
3310 // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
3311 // 17 bytes for any header.
3312 size_t needed = (((length/ALLOCATION_UNIT_SIZE + 255) / 256) * 2) + 17;
3313 size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
3314 if (bytesLeft < needed) {
3315 Flush();
3316 }
3317
3318 bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
3319 if (bytesLeft < needed) {
3320 LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
3321 << needed << " bytes)";
3322 return;
3323 }
3324 EnsureHeader(ptr);
Elliott Hughesa2155262011-11-16 16:26:58 -08003325 // Write out the chunk description.
Ian Rogers15bf2d32012-08-28 17:33:04 -07003326 length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units.
3327 totalAllocationUnits_ += length;
3328 while (length > 256) {
Ian Rogers30fab402012-01-23 15:43:46 -08003329 *p_++ = state | HPSG_PARTIAL;
3330 *p_++ = 255; // length - 1
Ian Rogers15bf2d32012-08-28 17:33:04 -07003331 length -= 256;
Elliott Hughesa2155262011-11-16 16:26:58 -08003332 }
Ian Rogers30fab402012-01-23 15:43:46 -08003333 *p_++ = state;
Ian Rogers15bf2d32012-08-28 17:33:04 -07003334 *p_++ = length - 1;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003335 }
3336
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003337 uint8_t ExamineObject(const mirror::Object* o, bool is_native_heap)
Ian Rogersb726dcb2012-09-05 08:57:23 -07003338 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Elliott Hughesa2155262011-11-16 16:26:58 -08003339 if (o == NULL) {
3340 return HPSG_STATE(SOLIDITY_FREE, 0);
3341 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003342
Elliott Hughesa2155262011-11-16 16:26:58 -08003343 // It's an allocated chunk. Figure out what it is.
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003344
Elliott Hughesa2155262011-11-16 16:26:58 -08003345 // If we're looking at the native heap, we'll just return
3346 // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003347 if (is_native_heap) {
Elliott Hughesa2155262011-11-16 16:26:58 -08003348 return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
3349 }
3350
Ian Rogers5bfa60f2012-09-02 21:17:56 -07003351 if (!Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07003352 return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003353 }
3354
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003355 mirror::Class* c = o->GetClass();
Elliott Hughesa2155262011-11-16 16:26:58 -08003356 if (c == NULL) {
3357 // The object was probably just created but hasn't been initialized yet.
3358 return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
3359 }
3360
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003361 if (!Runtime::Current()->GetHeap()->IsHeapAddress(c)) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07003362 LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
Elliott Hughesa2155262011-11-16 16:26:58 -08003363 return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
3364 }
3365
3366 if (c->IsClassClass()) {
3367 return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
3368 }
3369
3370 if (c->IsArrayClass()) {
3371 if (o->IsObjectArray()) {
3372 return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3373 }
3374 switch (c->GetComponentSize()) {
3375 case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
3376 case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
3377 case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3378 case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
3379 }
3380 }
3381
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003382 return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
3383 }
3384
Ian Rogers30fab402012-01-23 15:43:46 -08003385 std::vector<uint8_t> buf_;
3386 uint8_t* p_;
3387 uint8_t* pieceLenField_;
Ian Rogers15bf2d32012-08-28 17:33:04 -07003388 void* startOfNextMemoryChunk_;
Ian Rogers30fab402012-01-23 15:43:46 -08003389 size_t totalAllocationUnits_;
3390 uint32_t type_;
3391 bool merge_;
3392 bool needHeader_;
3393
Elliott Hughesa2155262011-11-16 16:26:58 -08003394 DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
3395};
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003396
3397void Dbg::DdmSendHeapSegments(bool native) {
3398 Dbg::HpsgWhen when;
3399 Dbg::HpsgWhat what;
3400 if (!native) {
3401 when = gDdmHpsgWhen;
3402 what = gDdmHpsgWhat;
3403 } else {
3404 when = gDdmNhsgWhen;
3405 what = gDdmNhsgWhat;
3406 }
3407 if (when == HPSG_WHEN_NEVER) {
3408 return;
3409 }
3410
3411 // Figure out what kind of chunks we'll be sending.
3412 CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what);
3413
3414 // First, send a heap start chunk.
3415 uint8_t heap_id[4];
3416 JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap).
3417 Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id);
3418
3419 // Send a series of heap segment chunks.
Elliott Hughesa2155262011-11-16 16:26:58 -08003420 HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native);
3421 if (native) {
Ian Rogers1d54e732013-05-02 21:10:01 -07003422 dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context);
Elliott Hughesa2155262011-11-16 16:26:58 -08003423 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -07003424 gc::Heap* heap = Runtime::Current()->GetHeap();
3425 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07003426 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -08003427 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -07003428 typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
3429 for (It cur = spaces.begin(), end = spaces.end(); cur != end; ++cur) {
3430 if ((*cur)->IsDlMallocSpace()) {
3431 (*cur)->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07003432 }
3433 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07003434 // Walk the large objects, these are not in the AllocSpace.
3435 heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
Elliott Hughesa2155262011-11-16 16:26:58 -08003436 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07003437
3438 // Finally, send a heap end chunk.
3439 Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id);
Elliott Hughes767a1472011-10-26 18:49:02 -07003440}
3441
Elliott Hughesb1a58792013-07-11 18:10:58 -07003442static size_t GetAllocTrackerMax() {
3443#ifdef HAVE_ANDROID_OS
3444 // Check whether there's a system property overriding the number of records.
3445 const char* propertyName = "dalvik.vm.allocTrackerMax";
3446 char allocRecordMaxString[PROPERTY_VALUE_MAX];
3447 if (property_get(propertyName, allocRecordMaxString, "") > 0) {
3448 char* end;
3449 size_t value = strtoul(allocRecordMaxString, &end, 10);
3450 if (*end != '\0') {
3451 ALOGE("Ignoring %s '%s' --- invalid", propertyName, allocRecordMaxString);
3452 return kDefaultNumAllocRecords;
3453 }
3454 if (!IsPowerOfTwo(value)) {
3455 ALOGE("Ignoring %s '%s' --- not power of two", propertyName, allocRecordMaxString);
3456 return kDefaultNumAllocRecords;
3457 }
3458 return value;
3459 }
3460#endif
3461 return kDefaultNumAllocRecords;
3462}
3463
Elliott Hughes545a0642011-11-08 19:10:03 -08003464void Dbg::SetAllocTrackingEnabled(bool enabled) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003465 MutexLock mu(Thread::Current(), gAllocTrackerLock);
Elliott Hughes545a0642011-11-08 19:10:03 -08003466 if (enabled) {
3467 if (recent_allocation_records_ == NULL) {
Elliott Hughesb1a58792013-07-11 18:10:58 -07003468 gAllocRecordMax = GetAllocTrackerMax();
3469 LOG(INFO) << "Enabling alloc tracker (" << gAllocRecordMax << " entries of "
3470 << kMaxAllocRecordStackDepth << " frames, taking "
3471 << PrettySize(sizeof(AllocRecord) * gAllocRecordMax) << ")";
Elliott Hughes545a0642011-11-08 19:10:03 -08003472 gAllocRecordHead = gAllocRecordCount = 0;
Elliott Hughesb1a58792013-07-11 18:10:58 -07003473 recent_allocation_records_ = new AllocRecord[gAllocRecordMax];
Elliott Hughes545a0642011-11-08 19:10:03 -08003474 CHECK(recent_allocation_records_ != NULL);
3475 }
3476 } else {
3477 delete[] recent_allocation_records_;
3478 recent_allocation_records_ = NULL;
3479 }
3480}
3481
Ian Rogers0399dde2012-06-06 17:09:28 -07003482struct AllocRecordStackVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -08003483 AllocRecordStackVisitor(Thread* thread, AllocRecord* record)
Ian Rogersb726dcb2012-09-05 08:57:23 -07003484 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -08003485 : StackVisitor(thread, NULL), record(record), depth(0) {}
Elliott Hughes545a0642011-11-08 19:10:03 -08003486
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003487 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
3488 // annotalysis.
3489 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughes545a0642011-11-08 19:10:03 -08003490 if (depth >= kMaxAllocRecordStackDepth) {
Elliott Hughes530fa002012-03-12 11:44:49 -07003491 return false;
Elliott Hughes545a0642011-11-08 19:10:03 -08003492 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003493 mirror::AbstractMethod* m = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -07003494 if (!m->IsRuntimeMethod()) {
3495 record->stack[depth].method = m;
3496 record->stack[depth].dex_pc = GetDexPc();
Elliott Hughes530fa002012-03-12 11:44:49 -07003497 ++depth;
Elliott Hughes545a0642011-11-08 19:10:03 -08003498 }
Elliott Hughes530fa002012-03-12 11:44:49 -07003499 return true;
Elliott Hughes545a0642011-11-08 19:10:03 -08003500 }
3501
3502 ~AllocRecordStackVisitor() {
3503 // Clear out any unused stack trace elements.
3504 for (; depth < kMaxAllocRecordStackDepth; ++depth) {
3505 record->stack[depth].method = NULL;
Ian Rogers0399dde2012-06-06 17:09:28 -07003506 record->stack[depth].dex_pc = 0;
Elliott Hughes545a0642011-11-08 19:10:03 -08003507 }
3508 }
3509
3510 AllocRecord* record;
3511 size_t depth;
3512};
3513
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003514void Dbg::RecordAllocation(mirror::Class* type, size_t byte_count) {
Elliott Hughes545a0642011-11-08 19:10:03 -08003515 Thread* self = Thread::Current();
3516 CHECK(self != NULL);
3517
Ian Rogers50b35e22012-10-04 10:09:15 -07003518 MutexLock mu(self, gAllocTrackerLock);
Elliott Hughes545a0642011-11-08 19:10:03 -08003519 if (recent_allocation_records_ == NULL) {
3520 return;
3521 }
3522
3523 // Advance and clip.
Elliott Hughesb1a58792013-07-11 18:10:58 -07003524 if (++gAllocRecordHead == gAllocRecordMax) {
Elliott Hughes545a0642011-11-08 19:10:03 -08003525 gAllocRecordHead = 0;
3526 }
3527
3528 // Fill in the basics.
3529 AllocRecord* record = &recent_allocation_records_[gAllocRecordHead];
3530 record->type = type;
3531 record->byte_count = byte_count;
3532 record->thin_lock_id = self->GetThinLockId();
3533
3534 // Fill in the stack trace.
Ian Rogers7a22fa62013-01-23 12:16:16 -08003535 AllocRecordStackVisitor visitor(self, record);
Ian Rogers0399dde2012-06-06 17:09:28 -07003536 visitor.WalkStack();
Elliott Hughes545a0642011-11-08 19:10:03 -08003537
Elliott Hughesb1a58792013-07-11 18:10:58 -07003538 if (gAllocRecordCount < gAllocRecordMax) {
Elliott Hughes545a0642011-11-08 19:10:03 -08003539 ++gAllocRecordCount;
3540 }
3541}
3542
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003543// Returns the index of the head element.
3544//
3545// We point at the most-recently-written record, so if gAllocRecordCount is 1
3546// we want to use the current element. Take "head+1" and subtract count
3547// from it.
3548//
3549// We need to handle underflow in our circular buffer, so we add
Elliott Hughesb1a58792013-07-11 18:10:58 -07003550// gAllocRecordMax and then mask it back down.
Elliott Hughesf8349362012-06-18 15:00:06 -07003551static inline int HeadIndex() EXCLUSIVE_LOCKS_REQUIRED(gAllocTrackerLock) {
Elliott Hughesb1a58792013-07-11 18:10:58 -07003552 return (gAllocRecordHead+1 + gAllocRecordMax - gAllocRecordCount) & (gAllocRecordMax-1);
Elliott Hughes545a0642011-11-08 19:10:03 -08003553}
3554
3555void Dbg::DumpRecentAllocations() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003556 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -07003557 MutexLock mu(soa.Self(), gAllocTrackerLock);
Elliott Hughes545a0642011-11-08 19:10:03 -08003558 if (recent_allocation_records_ == NULL) {
3559 LOG(INFO) << "Not recording tracked allocations";
3560 return;
3561 }
3562
3563 // "i" is the head of the list. We want to start at the end of the
3564 // list and move forward to the tail.
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003565 size_t i = HeadIndex();
Elliott Hughes545a0642011-11-08 19:10:03 -08003566 size_t count = gAllocRecordCount;
3567
3568 LOG(INFO) << "Tracked allocations, (head=" << gAllocRecordHead << " count=" << count << ")";
3569 while (count--) {
3570 AllocRecord* record = &recent_allocation_records_[i];
3571
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003572 LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->thin_lock_id, record->byte_count)
Elliott Hughes545a0642011-11-08 19:10:03 -08003573 << PrettyClass(record->type);
3574
3575 for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003576 const mirror::AbstractMethod* m = record->stack[stack_frame].method;
Elliott Hughes545a0642011-11-08 19:10:03 -08003577 if (m == NULL) {
3578 break;
3579 }
3580 LOG(INFO) << " " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber();
3581 }
3582
3583 // pause periodically to help logcat catch up
3584 if ((count % 5) == 0) {
3585 usleep(40000);
3586 }
3587
Elliott Hughesb1a58792013-07-11 18:10:58 -07003588 i = (i + 1) & (gAllocRecordMax-1);
Elliott Hughes545a0642011-11-08 19:10:03 -08003589 }
3590}
3591
3592class StringTable {
3593 public:
3594 StringTable() {
3595 }
3596
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003597 void Add(const char* s) {
Elliott Hughes545a0642011-11-08 19:10:03 -08003598 table_.insert(s);
3599 }
3600
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003601 size_t IndexOf(const char* s) const {
3602 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
3603 It it = table_.find(s);
3604 if (it == table_.end()) {
3605 LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
3606 }
3607 return std::distance(table_.begin(), it);
Elliott Hughes545a0642011-11-08 19:10:03 -08003608 }
3609
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003610 size_t Size() const {
Elliott Hughes545a0642011-11-08 19:10:03 -08003611 return table_.size();
3612 }
3613
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003614 void WriteTo(std::vector<uint8_t>& bytes) const {
3615 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
Elliott Hughes545a0642011-11-08 19:10:03 -08003616 for (It it = table_.begin(); it != table_.end(); ++it) {
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003617 const char* s = (*it).c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003618 size_t s_len = CountModifiedUtf8Chars(s);
3619 UniquePtr<uint16_t> s_utf16(new uint16_t[s_len]);
3620 ConvertModifiedUtf8ToUtf16(s_utf16.get(), s);
3621 JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
Elliott Hughes545a0642011-11-08 19:10:03 -08003622 }
3623 }
3624
3625 private:
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07003626 std::set<std::string> table_;
Elliott Hughes545a0642011-11-08 19:10:03 -08003627 DISALLOW_COPY_AND_ASSIGN(StringTable);
3628};
3629
3630/*
3631 * The data we send to DDMS contains everything we have recorded.
3632 *
3633 * Message header (all values big-endian):
3634 * (1b) message header len (to allow future expansion); includes itself
3635 * (1b) entry header len
3636 * (1b) stack frame len
3637 * (2b) number of entries
3638 * (4b) offset to string table from start of message
3639 * (2b) number of class name strings
3640 * (2b) number of method name strings
3641 * (2b) number of source file name strings
3642 * For each entry:
3643 * (4b) total allocation size
Elliott Hughes221229c2013-01-08 18:17:50 -08003644 * (2b) thread id
Elliott Hughes545a0642011-11-08 19:10:03 -08003645 * (2b) allocated object's class name index
3646 * (1b) stack depth
3647 * For each stack frame:
3648 * (2b) method's class name
3649 * (2b) method name
3650 * (2b) method source file
3651 * (2b) line number, clipped to 32767; -2 if native; -1 if no source
3652 * (xb) class name strings
3653 * (xb) method name strings
3654 * (xb) source file strings
3655 *
3656 * As with other DDM traffic, strings are sent as a 4-byte length
3657 * followed by UTF-16 data.
3658 *
3659 * We send up 16-bit unsigned indexes into string tables. In theory there
Elliott Hughesb1a58792013-07-11 18:10:58 -07003660 * can be (kMaxAllocRecordStackDepth * gAllocRecordMax) unique strings in
Elliott Hughes545a0642011-11-08 19:10:03 -08003661 * each table, but in practice there should be far fewer.
3662 *
3663 * The chief reason for using a string table here is to keep the size of
3664 * the DDMS message to a minimum. This is partly to make the protocol
3665 * efficient, but also because we have to form the whole thing up all at
3666 * once in a memory buffer.
3667 *
3668 * We use separate string tables for class names, method names, and source
3669 * files to keep the indexes small. There will generally be no overlap
3670 * between the contents of these tables.
3671 */
3672jbyteArray Dbg::GetRecentAllocations() {
3673 if (false) {
3674 DumpRecentAllocations();
3675 }
3676
Ian Rogers50b35e22012-10-04 10:09:15 -07003677 Thread* self = Thread::Current();
Elliott Hughes545a0642011-11-08 19:10:03 -08003678 std::vector<uint8_t> bytes;
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003679 {
3680 MutexLock mu(self, gAllocTrackerLock);
3681 //
3682 // Part 1: generate string tables.
3683 //
3684 StringTable class_names;
3685 StringTable method_names;
3686 StringTable filenames;
Elliott Hughes545a0642011-11-08 19:10:03 -08003687
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003688 int count = gAllocRecordCount;
3689 int idx = HeadIndex();
3690 while (count--) {
3691 AllocRecord* record = &recent_allocation_records_[idx];
Elliott Hughes545a0642011-11-08 19:10:03 -08003692
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003693 class_names.Add(ClassHelper(record->type).GetDescriptor());
Elliott Hughes545a0642011-11-08 19:10:03 -08003694
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003695 MethodHelper mh;
3696 for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
3697 mirror::AbstractMethod* m = record->stack[i].method;
3698 if (m != NULL) {
3699 mh.ChangeMethod(m);
3700 class_names.Add(mh.GetDeclaringClassDescriptor());
3701 method_names.Add(mh.GetName());
3702 filenames.Add(mh.GetDeclaringClassSourceFile());
3703 }
3704 }
Elliott Hughes545a0642011-11-08 19:10:03 -08003705
Elliott Hughesb1a58792013-07-11 18:10:58 -07003706 idx = (idx + 1) & (gAllocRecordMax-1);
Elliott Hughes545a0642011-11-08 19:10:03 -08003707 }
3708
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003709 LOG(INFO) << "allocation records: " << gAllocRecordCount;
3710
3711 //
3712 // Part 2: Generate the output and store it in the buffer.
3713 //
3714
3715 // (1b) message header len (to allow future expansion); includes itself
3716 // (1b) entry header len
3717 // (1b) stack frame len
3718 const int kMessageHeaderLen = 15;
3719 const int kEntryHeaderLen = 9;
3720 const int kStackFrameLen = 8;
3721 JDWP::Append1BE(bytes, kMessageHeaderLen);
3722 JDWP::Append1BE(bytes, kEntryHeaderLen);
3723 JDWP::Append1BE(bytes, kStackFrameLen);
3724
3725 // (2b) number of entries
3726 // (4b) offset to string table from start of message
3727 // (2b) number of class name strings
3728 // (2b) number of method name strings
3729 // (2b) number of source file name strings
3730 JDWP::Append2BE(bytes, gAllocRecordCount);
3731 size_t string_table_offset = bytes.size();
3732 JDWP::Append4BE(bytes, 0); // We'll patch this later...
3733 JDWP::Append2BE(bytes, class_names.Size());
3734 JDWP::Append2BE(bytes, method_names.Size());
3735 JDWP::Append2BE(bytes, filenames.Size());
3736
3737 count = gAllocRecordCount;
3738 idx = HeadIndex();
3739 ClassHelper kh;
3740 while (count--) {
3741 // For each entry:
3742 // (4b) total allocation size
3743 // (2b) thread id
3744 // (2b) allocated object's class name index
3745 // (1b) stack depth
3746 AllocRecord* record = &recent_allocation_records_[idx];
3747 size_t stack_depth = record->GetDepth();
3748 kh.ChangeClass(record->type);
3749 size_t allocated_object_class_name_index = class_names.IndexOf(kh.GetDescriptor());
3750 JDWP::Append4BE(bytes, record->byte_count);
3751 JDWP::Append2BE(bytes, record->thin_lock_id);
3752 JDWP::Append2BE(bytes, allocated_object_class_name_index);
3753 JDWP::Append1BE(bytes, stack_depth);
3754
3755 MethodHelper mh;
3756 for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
3757 // For each stack frame:
3758 // (2b) method's class name
3759 // (2b) method name
3760 // (2b) method source file
3761 // (2b) line number, clipped to 32767; -2 if native; -1 if no source
3762 mh.ChangeMethod(record->stack[stack_frame].method);
3763 size_t class_name_index = class_names.IndexOf(mh.GetDeclaringClassDescriptor());
3764 size_t method_name_index = method_names.IndexOf(mh.GetName());
3765 size_t file_name_index = filenames.IndexOf(mh.GetDeclaringClassSourceFile());
3766 JDWP::Append2BE(bytes, class_name_index);
3767 JDWP::Append2BE(bytes, method_name_index);
3768 JDWP::Append2BE(bytes, file_name_index);
3769 JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber());
3770 }
3771
Elliott Hughesb1a58792013-07-11 18:10:58 -07003772 idx = (idx + 1) & (gAllocRecordMax-1);
Mathieu Chartier46e811b2013-07-10 17:09:14 -07003773 }
3774
3775 // (xb) class name strings
3776 // (xb) method name strings
3777 // (xb) source file strings
3778 JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
3779 class_names.WriteTo(bytes);
3780 method_names.WriteTo(bytes);
3781 filenames.WriteTo(bytes);
Elliott Hughes545a0642011-11-08 19:10:03 -08003782 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003783 JNIEnv* env = self->GetJniEnv();
Elliott Hughes545a0642011-11-08 19:10:03 -08003784 jbyteArray result = env->NewByteArray(bytes.size());
3785 if (result != NULL) {
3786 env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
3787 }
3788 return result;
3789}
3790
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003791} // namespace art