Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 19 | #include <sys/uio.h> |
| 20 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 21 | #include <set> |
| 22 | |
| 23 | #include "class_linker.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame^] | 25 | #include "dex_file-inl.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 26 | #include "dex_instruction.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "gc/card_table-inl.h" |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 28 | #include "gc/large_object_space.h" |
| 29 | #include "gc/space.h" |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 30 | #include "invoke_arg_array_builder.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 31 | #include "jdwp/object_registry.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "mirror/abstract_method-inl.h" |
| 33 | #include "mirror/class.h" |
| 34 | #include "mirror/class-inl.h" |
| 35 | #include "mirror/class_loader.h" |
| 36 | #include "mirror/field-inl.h" |
| 37 | #include "mirror/object-inl.h" |
| 38 | #include "mirror/object_array-inl.h" |
| 39 | #include "mirror/throwable.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 40 | #include "oat/runtime/context.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 41 | #include "object_utils.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 42 | #include "safe_map.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 43 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 44 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 45 | #include "ScopedPrimitiveArray.h" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 46 | #include "sirt_ref.h" |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 47 | #include "stack_indirect_reference_table.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 48 | #include "thread_list.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | #include "utf.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 50 | #include "well_known_classes.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 51 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 52 | namespace art { |
| 53 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 54 | static const size_t kMaxAllocRecordStackDepth = 16; // Max 255. |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 55 | static const size_t kNumAllocRecords = 512; // Must be a power of 2. |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 56 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 57 | struct AllocRecordStackTraceElement { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | mirror::AbstractMethod* method; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 59 | uint32_t dex_pc; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 60 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 61 | int32_t LineNumber() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 62 | return MethodHelper(method).GetLineNumFromDexPC(dex_pc); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
| 66 | struct AllocRecord { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 67 | mirror::Class* type; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 68 | size_t byte_count; |
| 69 | uint16_t thin_lock_id; |
| 70 | AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth]; // Unused entries have NULL method. |
| 71 | |
| 72 | size_t GetDepth() { |
| 73 | size_t depth = 0; |
| 74 | while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) { |
| 75 | ++depth; |
| 76 | } |
| 77 | return depth; |
| 78 | } |
| 79 | }; |
| 80 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 81 | struct Breakpoint { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 82 | mirror::AbstractMethod* method; |
Elliott Hughes | a656a0f | 2012-02-21 18:03:44 -0800 | [diff] [blame] | 83 | uint32_t dex_pc; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | Breakpoint(mirror::AbstractMethod* method, uint32_t dex_pc) : method(method), dex_pc(dex_pc) {} |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 87 | static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 88 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 229feb7 | 2012-02-23 13:33:29 -0800 | [diff] [blame] | 89 | os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.method).c_str(), rhs.dex_pc); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 90 | return os; |
| 91 | } |
| 92 | |
| 93 | struct SingleStepControl { |
| 94 | // Are we single-stepping right now? |
| 95 | bool is_active; |
| 96 | Thread* thread; |
| 97 | |
| 98 | JDWP::JdwpStepSize step_size; |
| 99 | JDWP::JdwpStepDepth step_depth; |
| 100 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 101 | const mirror::AbstractMethod* method; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 102 | int32_t line_number; // Or -1 for native methods. |
| 103 | std::set<uint32_t> dex_pcs; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 104 | int stack_depth; |
| 105 | }; |
| 106 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 107 | // JDWP is allowed unless the Zygote forbids it. |
| 108 | static bool gJdwpAllowed = true; |
| 109 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 110 | // Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line? |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 111 | static bool gJdwpConfigured = false; |
| 112 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 113 | // Broken-down JDWP options. (Only valid if IsJdwpConfigured() is true.) |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 114 | static JDWP::JdwpOptions gJdwpOptions; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 115 | |
| 116 | // Runtime JDWP state. |
| 117 | static JDWP::JdwpState* gJdwpState = NULL; |
| 118 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
| 119 | static bool gDebuggerActive; // debugger is making requests. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 120 | static bool gDisposed; // debugger called VirtualMachine.Dispose, so we should drop the connection. |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 121 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 122 | static bool gDdmThreadNotification = false; |
| 123 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 124 | // DDMS GC-related settings. |
| 125 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 126 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 127 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 128 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 129 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 130 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 131 | static ObjectRegistry* gRegistry = NULL; |
| 132 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 133 | // Recent allocation tracking. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 134 | static Mutex gAllocTrackerLock DEFAULT_MUTEX_ACQUIRED_AFTER ("AllocTracker lock"); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 135 | AllocRecord* Dbg::recent_allocation_records_ PT_GUARDED_BY(gAllocTrackerLock) = NULL; // TODO: CircularBuffer<AllocRecord> |
| 136 | static size_t gAllocRecordHead GUARDED_BY(gAllocTrackerLock) = 0; |
| 137 | static size_t gAllocRecordCount GUARDED_BY(gAllocTrackerLock) = 0; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 138 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 139 | // Breakpoints and single-stepping. |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 140 | static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_); |
| 141 | static SingleStepControl gSingleStepControl GUARDED_BY(Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 142 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 143 | static bool IsBreakpoint(mirror::AbstractMethod* m, uint32_t dex_pc) |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 144 | LOCKS_EXCLUDED(Locks::breakpoint_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 145 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 146 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 147 | for (size_t i = 0; i < gBreakpoints.size(); ++i) { |
Elliott Hughes | a656a0f | 2012-02-21 18:03:44 -0800 | [diff] [blame] | 148 | if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == dex_pc) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 149 | VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i]; |
| 150 | return true; |
| 151 | } |
| 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 156 | static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) { |
| 157 | MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); |
| 158 | // A thread may be suspended for GC; in this code, we really want to know whether |
| 159 | // there's a debugger suspension active. |
| 160 | return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0; |
| 161 | } |
| 162 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 163 | static mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 164 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 165 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 166 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 167 | status = JDWP::ERR_INVALID_OBJECT; |
| 168 | return NULL; |
| 169 | } |
| 170 | if (!o->IsArrayInstance()) { |
| 171 | status = JDWP::ERR_INVALID_ARRAY; |
| 172 | return NULL; |
| 173 | } |
| 174 | status = JDWP::ERR_NONE; |
| 175 | return o->AsArray(); |
| 176 | } |
| 177 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 178 | static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 179 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 180 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 181 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 182 | status = JDWP::ERR_INVALID_OBJECT; |
| 183 | return NULL; |
| 184 | } |
| 185 | if (!o->IsClass()) { |
| 186 | status = JDWP::ERR_INVALID_CLASS; |
| 187 | return NULL; |
| 188 | } |
| 189 | status = JDWP::ERR_NONE; |
| 190 | return o->AsClass(); |
| 191 | } |
| 192 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 193 | static JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread) |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 194 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 195 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) |
| 196 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 197 | mirror::Object* thread_peer = gRegistry->Get<mirror::Object*>(thread_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 198 | if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 199 | // This isn't even an object. |
| 200 | return JDWP::ERR_INVALID_OBJECT; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 201 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 202 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 203 | mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 204 | if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) { |
| 205 | // This isn't a thread. |
| 206 | return JDWP::ERR_INVALID_THREAD; |
| 207 | } |
| 208 | |
| 209 | thread = Thread::FromManagedThread(soa, thread_peer); |
| 210 | if (thread == NULL) { |
| 211 | // This is a java.lang.Thread without a Thread*. Must be a zombie. |
| 212 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 213 | } |
| 214 | return JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 217 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 218 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 219 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 220 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 221 | } |
| 222 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 223 | static JDWP::JdwpTag TagFromClass(mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 224 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 225 | CHECK(c != NULL); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 226 | if (c->IsArrayClass()) { |
| 227 | return JDWP::JT_ARRAY; |
| 228 | } |
| 229 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 230 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 231 | if (c->IsStringClass()) { |
| 232 | return JDWP::JT_STRING; |
| 233 | } else if (c->IsClassClass()) { |
| 234 | return JDWP::JT_CLASS_OBJECT; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 235 | } else if (class_linker->FindSystemClass("Ljava/lang/Thread;")->IsAssignableFrom(c)) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 236 | return JDWP::JT_THREAD; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 237 | } else if (class_linker->FindSystemClass("Ljava/lang/ThreadGroup;")->IsAssignableFrom(c)) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 238 | return JDWP::JT_THREAD_GROUP; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 239 | } else if (class_linker->FindSystemClass("Ljava/lang/ClassLoader;")->IsAssignableFrom(c)) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 240 | return JDWP::JT_CLASS_LOADER; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 241 | } else { |
| 242 | return JDWP::JT_OBJECT; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Objects declared to hold Object might actually hold a more specific |
| 248 | * type. The debugger may take a special interest in these (e.g. it |
| 249 | * wants to display the contents of Strings), so we want to return an |
| 250 | * appropriate tag. |
| 251 | * |
| 252 | * Null objects are tagged JT_OBJECT. |
| 253 | */ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 254 | static JDWP::JdwpTag TagFromObject(const mirror::Object* o) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 255 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 256 | return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(o->GetClass()); |
| 257 | } |
| 258 | |
| 259 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 260 | switch (tag) { |
| 261 | case JDWP::JT_BOOLEAN: |
| 262 | case JDWP::JT_BYTE: |
| 263 | case JDWP::JT_CHAR: |
| 264 | case JDWP::JT_FLOAT: |
| 265 | case JDWP::JT_DOUBLE: |
| 266 | case JDWP::JT_INT: |
| 267 | case JDWP::JT_LONG: |
| 268 | case JDWP::JT_SHORT: |
| 269 | case JDWP::JT_VOID: |
| 270 | return true; |
| 271 | default: |
| 272 | return false; |
| 273 | } |
| 274 | } |
| 275 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 276 | /* |
| 277 | * Handle one of the JDWP name/value pairs. |
| 278 | * |
| 279 | * JDWP options are: |
| 280 | * help: if specified, show help message and bail |
| 281 | * transport: may be dt_socket or dt_shmem |
| 282 | * address: for dt_socket, "host:port", or just "port" when listening |
| 283 | * server: if "y", wait for debugger to attach; if "n", attach to debugger |
| 284 | * timeout: how long to wait for debugger to connect / listen |
| 285 | * |
| 286 | * Useful with server=n (these aren't supported yet): |
| 287 | * onthrow=<exception-name>: connect to debugger when exception thrown |
| 288 | * onuncaught=y|n: connect to debugger when uncaught exception thrown |
| 289 | * launch=<command-line>: launch the debugger itself |
| 290 | * |
| 291 | * The "transport" option is required, as is "address" if server=n. |
| 292 | */ |
| 293 | static bool ParseJdwpOption(const std::string& name, const std::string& value) { |
| 294 | if (name == "transport") { |
| 295 | if (value == "dt_socket") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 296 | gJdwpOptions.transport = JDWP::kJdwpTransportSocket; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 297 | } else if (value == "dt_android_adb") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 298 | gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 299 | } else { |
| 300 | LOG(ERROR) << "JDWP transport not supported: " << value; |
| 301 | return false; |
| 302 | } |
| 303 | } else if (name == "server") { |
| 304 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 305 | gJdwpOptions.server = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 306 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 307 | gJdwpOptions.server = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 308 | } else { |
| 309 | LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'"; |
| 310 | return false; |
| 311 | } |
| 312 | } else if (name == "suspend") { |
| 313 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 314 | gJdwpOptions.suspend = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 315 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 316 | gJdwpOptions.suspend = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 317 | } else { |
| 318 | LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'"; |
| 319 | return false; |
| 320 | } |
| 321 | } else if (name == "address") { |
| 322 | /* this is either <port> or <host>:<port> */ |
| 323 | std::string port_string; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 324 | gJdwpOptions.host.clear(); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 325 | std::string::size_type colon = value.find(':'); |
| 326 | if (colon != std::string::npos) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 327 | gJdwpOptions.host = value.substr(0, colon); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 328 | port_string = value.substr(colon + 1); |
| 329 | } else { |
| 330 | port_string = value; |
| 331 | } |
| 332 | if (port_string.empty()) { |
| 333 | LOG(ERROR) << "JDWP address missing port: " << value; |
| 334 | return false; |
| 335 | } |
| 336 | char* end; |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 337 | uint64_t port = strtoul(port_string.c_str(), &end, 10); |
| 338 | if (*end != '\0' || port > 0xffff) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 339 | LOG(ERROR) << "JDWP address has junk in port field: " << value; |
| 340 | return false; |
| 341 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 342 | gJdwpOptions.port = port; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 343 | } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") { |
| 344 | /* valid but unsupported */ |
| 345 | LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'"; |
| 346 | } else { |
| 347 | LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'"; |
| 348 | } |
| 349 | |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.: |
| 355 | * "transport=dt_socket,address=8000,server=y,suspend=n" |
| 356 | */ |
| 357 | bool Dbg::ParseJdwpOptions(const std::string& options) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 358 | VLOG(jdwp) << "ParseJdwpOptions: " << options; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 359 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 360 | std::vector<std::string> pairs; |
| 361 | Split(options, ',', pairs); |
| 362 | |
| 363 | for (size_t i = 0; i < pairs.size(); ++i) { |
| 364 | std::string::size_type equals = pairs[i].find('='); |
| 365 | if (equals == std::string::npos) { |
| 366 | LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'"; |
| 367 | return false; |
| 368 | } |
| 369 | ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1)); |
| 370 | } |
| 371 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 372 | if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 373 | LOG(ERROR) << "Must specify JDWP transport: " << options; |
| 374 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 375 | if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 376 | LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options; |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | gJdwpConfigured = true; |
| 381 | return true; |
| 382 | } |
| 383 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 384 | void Dbg::StartJdwp() { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 385 | if (!gJdwpAllowed || !IsJdwpConfigured()) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 386 | // No JDWP for you! |
| 387 | return; |
| 388 | } |
| 389 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 390 | CHECK(gRegistry == NULL); |
| 391 | gRegistry = new ObjectRegistry; |
| 392 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 393 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 394 | // debugger, passively listen for a debugger, or block waiting for a |
| 395 | // debugger. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 396 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
| 397 | if (gJdwpState == NULL) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 398 | // We probably failed because some other process has the port already, which means that |
| 399 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 400 | // talking to that other process. |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 401 | LOG(FATAL) << "Debugger thread failed to initialize"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | // If a debugger has already attached, send the "welcome" message. |
| 405 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 406 | if (gJdwpState->IsActive()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 407 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 408 | if (!gJdwpState->PostVMStart()) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 409 | LOG(WARNING) << "Failed to post 'start' message to debugger"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 414 | void Dbg::StopJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 415 | delete gJdwpState; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 416 | delete gRegistry; |
| 417 | gRegistry = NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 420 | void Dbg::GcDidFinish() { |
| 421 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 422 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 423 | LOG(DEBUG) << "Sending heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 424 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 425 | } |
| 426 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 427 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 428 | LOG(DEBUG) << "Dumping heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 429 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 430 | } |
| 431 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 432 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 433 | LOG(DEBUG) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 434 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 438 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 439 | gJdwpAllowed = allowed; |
| 440 | } |
| 441 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 442 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 443 | return Thread::Current()->GetInvokeReq(); |
| 444 | } |
| 445 | |
| 446 | Thread* Dbg::GetDebugThread() { |
| 447 | return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL; |
| 448 | } |
| 449 | |
| 450 | void Dbg::ClearWaitForEventThread() { |
| 451 | gJdwpState->ClearWaitForEventThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 455 | CHECK(!gDebuggerConnected); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 456 | VLOG(jdwp) << "JDWP has attached"; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 457 | gDebuggerConnected = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 458 | gDisposed = false; |
| 459 | } |
| 460 | |
| 461 | void Dbg::Disposed() { |
| 462 | gDisposed = true; |
| 463 | } |
| 464 | |
| 465 | bool Dbg::IsDisposed() { |
| 466 | return gDisposed; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 469 | static void SetDebuggerUpdatesEnabledCallback(Thread* t, void* user_data) { |
| 470 | t->SetDebuggerUpdatesEnabled(*reinterpret_cast<bool*>(user_data)); |
| 471 | } |
| 472 | |
| 473 | static void SetDebuggerUpdatesEnabled(bool enabled) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 474 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 475 | Runtime::Current()->GetThreadList()->ForEach(SetDebuggerUpdatesEnabledCallback, &enabled); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 478 | void Dbg::GoActive() { |
| 479 | // Enable all debugging features, including scans for breakpoints. |
| 480 | // This is a no-op if we're already active. |
| 481 | // Only called from the JDWP handler thread. |
| 482 | if (gDebuggerActive) { |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | LOG(INFO) << "Debugger is active"; |
| 487 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 488 | { |
| 489 | // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected? |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 490 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 491 | CHECK_EQ(gBreakpoints.size(), 0U); |
| 492 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 493 | |
| 494 | gDebuggerActive = true; |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 495 | SetDebuggerUpdatesEnabled(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 499 | CHECK(gDebuggerConnected); |
| 500 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 501 | LOG(INFO) << "Debugger is no longer active"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 502 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 503 | gDebuggerActive = false; |
| 504 | SetDebuggerUpdatesEnabled(false); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 505 | |
| 506 | gRegistry->Clear(); |
| 507 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 510 | bool Dbg::IsDebuggerActive() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 511 | return gDebuggerActive; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 514 | bool Dbg::IsJdwpConfigured() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 515 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | int64_t Dbg::LastDebuggerActivity() { |
Elliott Hughes | ca95152 | 2011-12-05 12:01:32 -0800 | [diff] [blame] | 519 | return gJdwpState->LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 522 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 523 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 526 | std::string Dbg::GetClassName(JDWP::RefTypeId class_id) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 527 | mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 528 | if (o == NULL) { |
| 529 | return "NULL"; |
| 530 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 531 | if (o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 532 | return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id)); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 533 | } |
| 534 | if (!o->IsClass()) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 535 | return StringPrintf("non-class %p", o); // This is only used for debugging output anyway. |
| 536 | } |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame] | 537 | return DescriptorToName(ClassHelper(o->AsClass()).GetDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 540 | JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 541 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 542 | mirror::Class* c = DecodeClass(id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 543 | if (c == NULL) { |
| 544 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 545 | } |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 546 | class_object_id = gRegistry->Add(c); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 547 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 550 | JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 551 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 552 | mirror::Class* c = DecodeClass(id, status); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 553 | if (c == NULL) { |
| 554 | return status; |
| 555 | } |
| 556 | if (c->IsInterface()) { |
| 557 | // http://code.google.com/p/android/issues/detail?id=20856 |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 558 | superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 559 | } else { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 560 | superclass_id = gRegistry->Add(c->GetSuperClass()); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 561 | } |
| 562 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 565 | JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 566 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 567 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 568 | return JDWP::ERR_INVALID_OBJECT; |
| 569 | } |
| 570 | expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader())); |
| 571 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 574 | JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
| 575 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 576 | mirror::Class* c = DecodeClass(id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 577 | if (c == NULL) { |
| 578 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 579 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 580 | |
| 581 | uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask; |
| 582 | |
| 583 | // Set ACC_SUPER; dex files don't contain this flag, but all classes are supposed to have it set. |
| 584 | // Class.getModifiers doesn't return it, but JDWP does, so we set it here. |
| 585 | access_flags |= kAccSuper; |
| 586 | |
| 587 | expandBufAdd4BE(pReply, access_flags); |
| 588 | |
| 589 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 592 | JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) |
| 593 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 594 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 595 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 596 | return JDWP::ERR_INVALID_OBJECT; |
| 597 | } |
| 598 | |
| 599 | // Ensure all threads are suspended while we read objects' lock words. |
| 600 | Thread* self = Thread::Current(); |
| 601 | Locks::mutator_lock_->SharedUnlock(self); |
| 602 | Locks::mutator_lock_->ExclusiveLock(self); |
| 603 | |
| 604 | MonitorInfo monitor_info(o); |
| 605 | |
| 606 | Locks::mutator_lock_->ExclusiveUnlock(self); |
| 607 | Locks::mutator_lock_->SharedLock(self); |
| 608 | |
| 609 | if (monitor_info.owner != NULL) { |
| 610 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner->GetPeer())); |
| 611 | } else { |
| 612 | expandBufAddObjectId(reply, gRegistry->Add(NULL)); |
| 613 | } |
| 614 | expandBufAdd4BE(reply, monitor_info.entry_count); |
| 615 | expandBufAdd4BE(reply, monitor_info.waiters.size()); |
| 616 | for (size_t i = 0; i < monitor_info.waiters.size(); ++i) { |
| 617 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters[i]->GetPeer())); |
| 618 | } |
| 619 | return JDWP::ERR_NONE; |
| 620 | } |
| 621 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 622 | JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id, |
| 623 | std::vector<JDWP::ObjectId>& monitors, |
| 624 | std::vector<uint32_t>& stack_depths) |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 625 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 626 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 627 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 628 | Thread* thread; |
| 629 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 630 | if (error != JDWP::ERR_NONE) { |
| 631 | return error; |
| 632 | } |
| 633 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 634 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 635 | } |
| 636 | |
| 637 | struct OwnedMonitorVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 638 | OwnedMonitorVisitor(Thread* thread, Context* context) |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 639 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 640 | : StackVisitor(thread, context), current_stack_depth(0) {} |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 641 | |
| 642 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 643 | // annotalysis. |
| 644 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 645 | if (!GetMethod()->IsRuntimeMethod()) { |
| 646 | Monitor::VisitLocks(this, AppendOwnedMonitors, this); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 647 | ++current_stack_depth; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 648 | } |
| 649 | return true; |
| 650 | } |
| 651 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 652 | static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 653 | OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 654 | visitor->monitors.push_back(owned_monitor); |
| 655 | visitor->stack_depths.push_back(visitor->current_stack_depth); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 658 | size_t current_stack_depth; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 659 | std::vector<mirror::Object*> monitors; |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 660 | std::vector<uint32_t> stack_depths; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 661 | }; |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 662 | UniquePtr<Context> context(Context::Create()); |
| 663 | OwnedMonitorVisitor visitor(thread, context.get()); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 664 | visitor.WalkStack(); |
| 665 | |
| 666 | for (size_t i = 0; i < visitor.monitors.size(); ++i) { |
| 667 | monitors.push_back(gRegistry->Add(visitor.monitors[i])); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 668 | stack_depths.push_back(visitor.stack_depths[i]); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | return JDWP::ERR_NONE; |
| 672 | } |
| 673 | |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 674 | JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, JDWP::ObjectId& contended_monitor) |
| 675 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 676 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 677 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 678 | Thread* thread; |
| 679 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 680 | if (error != JDWP::ERR_NONE) { |
| 681 | return error; |
| 682 | } |
| 683 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 684 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 685 | } |
| 686 | |
| 687 | contended_monitor = gRegistry->Add(Monitor::GetContendedMonitor(thread)); |
| 688 | |
| 689 | return JDWP::ERR_NONE; |
| 690 | } |
| 691 | |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 692 | JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids, |
| 693 | std::vector<uint64_t>& counts) |
| 694 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 695 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 696 | std::vector<mirror::Class*> classes; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 697 | counts.clear(); |
| 698 | for (size_t i = 0; i < class_ids.size(); ++i) { |
| 699 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 700 | mirror::Class* c = DecodeClass(class_ids[i], status); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 701 | if (c == NULL) { |
| 702 | return status; |
| 703 | } |
| 704 | classes.push_back(c); |
| 705 | counts.push_back(0); |
| 706 | } |
| 707 | |
| 708 | Runtime::Current()->GetHeap()->CountInstances(classes, false, &counts[0]); |
| 709 | return JDWP::ERR_NONE; |
| 710 | } |
| 711 | |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 712 | JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances) |
| 713 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 714 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 715 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 716 | if (c == NULL) { |
| 717 | return status; |
| 718 | } |
| 719 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 720 | std::vector<mirror::Object*> raw_instances; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 721 | Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances); |
| 722 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
| 723 | instances.push_back(gRegistry->Add(raw_instances[i])); |
| 724 | } |
| 725 | return JDWP::ERR_NONE; |
| 726 | } |
| 727 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 728 | JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count, |
| 729 | std::vector<JDWP::ObjectId>& referring_objects) |
| 730 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 731 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 732 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 733 | return JDWP::ERR_INVALID_OBJECT; |
| 734 | } |
| 735 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 736 | std::vector<mirror::Object*> raw_instances; |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 737 | Runtime::Current()->GetHeap()->GetReferringObjects(o, max_count, raw_instances); |
| 738 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
| 739 | referring_objects.push_back(gRegistry->Add(raw_instances[i])); |
| 740 | } |
| 741 | return JDWP::ERR_NONE; |
| 742 | } |
| 743 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 744 | JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id) |
| 745 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 746 | gRegistry->DisableCollection(object_id); |
| 747 | return JDWP::ERR_NONE; |
| 748 | } |
| 749 | |
| 750 | JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id) |
| 751 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 752 | gRegistry->EnableCollection(object_id); |
| 753 | return JDWP::ERR_NONE; |
| 754 | } |
| 755 | |
| 756 | JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected) |
| 757 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 758 | is_collected = gRegistry->IsCollected(object_id); |
| 759 | return JDWP::ERR_NONE; |
| 760 | } |
| 761 | |
| 762 | void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) |
| 763 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 764 | gRegistry->DisposeObject(object_id, reference_count); |
| 765 | } |
| 766 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 767 | JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 768 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 769 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 770 | if (c == NULL) { |
| 771 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 772 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 773 | |
| 774 | expandBufAdd1(pReply, c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS); |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 775 | expandBufAddRefTypeId(pReply, class_id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 776 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 779 | void Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 780 | // Get the complete list of reference classes (i.e. all classes except |
| 781 | // the primitive types). |
| 782 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 783 | struct ClassListCreator { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 784 | explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 785 | } |
| 786 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 787 | static bool Visit(mirror::Class* c, void* arg) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 788 | return reinterpret_cast<ClassListCreator*>(arg)->Visit(c); |
| 789 | } |
| 790 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 791 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 792 | // annotalysis. |
| 793 | bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 794 | if (!c->IsPrimitive()) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 795 | classes.push_back(gRegistry->AddRefType(c)); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 796 | } |
| 797 | return true; |
| 798 | } |
| 799 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 800 | std::vector<JDWP::RefTypeId>& classes; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 801 | }; |
| 802 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 803 | ClassListCreator clc(classes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 804 | Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 807 | JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 808 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 809 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 810 | if (c == NULL) { |
| 811 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 812 | } |
| 813 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 814 | if (c->IsArrayClass()) { |
| 815 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 816 | *pTypeTag = JDWP::TT_ARRAY; |
| 817 | } else { |
| 818 | if (c->IsErroneous()) { |
| 819 | *pStatus = JDWP::CS_ERROR; |
| 820 | } else { |
| 821 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 822 | } |
| 823 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 824 | } |
| 825 | |
| 826 | if (pDescriptor != NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 827 | *pDescriptor = ClassHelper(c).GetDescriptor(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 828 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 829 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 832 | void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 833 | std::vector<mirror::Class*> classes; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 834 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
| 835 | ids.clear(); |
| 836 | for (size_t i = 0; i < classes.size(); ++i) { |
| 837 | ids.push_back(gRegistry->Add(classes[i])); |
| 838 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 841 | JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) |
| 842 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 843 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 844 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 845 | return JDWP::ERR_INVALID_OBJECT; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 846 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 847 | |
| 848 | JDWP::JdwpTypeTag type_tag; |
| 849 | if (o->GetClass()->IsArrayClass()) { |
| 850 | type_tag = JDWP::TT_ARRAY; |
| 851 | } else if (o->GetClass()->IsInterface()) { |
| 852 | type_tag = JDWP::TT_INTERFACE; |
| 853 | } else { |
| 854 | type_tag = JDWP::TT_CLASS; |
| 855 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 856 | JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 857 | |
| 858 | expandBufAdd1(pReply, type_tag); |
| 859 | expandBufAddRefTypeId(pReply, type_id); |
| 860 | |
| 861 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 864 | JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string& signature) { |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 865 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 866 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 867 | if (c == NULL) { |
| 868 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 869 | } |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 870 | signature = ClassHelper(c).GetDescriptor(); |
| 871 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 874 | JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 875 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 876 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 877 | if (c == NULL) { |
| 878 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 879 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 880 | result = ClassHelper(c).GetSourceFile(); |
| 881 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 884 | JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 885 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 886 | if (o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 887 | return JDWP::ERR_INVALID_OBJECT; |
| 888 | } |
| 889 | tag = TagFromObject(o); |
| 890 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 893 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 894 | switch (tag) { |
| 895 | case JDWP::JT_VOID: |
| 896 | return 0; |
| 897 | case JDWP::JT_BYTE: |
| 898 | case JDWP::JT_BOOLEAN: |
| 899 | return 1; |
| 900 | case JDWP::JT_CHAR: |
| 901 | case JDWP::JT_SHORT: |
| 902 | return 2; |
| 903 | case JDWP::JT_FLOAT: |
| 904 | case JDWP::JT_INT: |
| 905 | return 4; |
| 906 | case JDWP::JT_ARRAY: |
| 907 | case JDWP::JT_OBJECT: |
| 908 | case JDWP::JT_STRING: |
| 909 | case JDWP::JT_THREAD: |
| 910 | case JDWP::JT_THREAD_GROUP: |
| 911 | case JDWP::JT_CLASS_LOADER: |
| 912 | case JDWP::JT_CLASS_OBJECT: |
| 913 | return sizeof(JDWP::ObjectId); |
| 914 | case JDWP::JT_DOUBLE: |
| 915 | case JDWP::JT_LONG: |
| 916 | return 8; |
| 917 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 918 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 919 | return -1; |
| 920 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 923 | JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 924 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 925 | mirror::Array* a = DecodeArray(array_id, status); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 926 | if (a == NULL) { |
| 927 | return status; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 928 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 929 | length = a->GetLength(); |
| 930 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 933 | JDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 934 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 935 | mirror::Array* a = DecodeArray(array_id, status); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 936 | if (a == NULL) { |
| 937 | return status; |
| 938 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 939 | |
| 940 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 941 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 942 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 943 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 944 | std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 945 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 946 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 947 | expandBufAdd1(pReply, tag); |
| 948 | expandBufAdd4BE(pReply, count); |
| 949 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 950 | if (IsPrimitiveTag(tag)) { |
| 951 | size_t width = GetTagWidth(tag); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 952 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 953 | if (width == 8) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 954 | const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t))); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 955 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 956 | } else if (width == 4) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 957 | const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t))); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 958 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 959 | } else if (width == 2) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 960 | const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t))); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 961 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 962 | } else { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 963 | const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t))); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 964 | memcpy(dst, &src[offset * width], count * width); |
| 965 | } |
| 966 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 967 | mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 968 | for (int i = 0; i < count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 969 | mirror::Object* element = oa->Get(offset + i); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 970 | JDWP::JdwpTag specific_tag = (element != NULL) ? TagFromObject(element) : tag; |
| 971 | expandBufAdd1(pReply, specific_tag); |
| 972 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 973 | } |
| 974 | } |
| 975 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 976 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 979 | template <typename T> void CopyArrayData(mirror::Array* a, JDWP::Request& src, int offset, int count) { |
| 980 | DCHECK(a->GetClass()->IsPrimitiveArray()); |
| 981 | |
| 982 | T* dst = &(reinterpret_cast<T*>(a->GetRawData(sizeof(T)))[offset * sizeof(T)]); |
| 983 | for (int i = 0; i < count; ++i) { |
| 984 | *dst++ = src.ReadValue(sizeof(T)); |
| 985 | } |
| 986 | } |
| 987 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 988 | JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count, |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 989 | JDWP::Request& request) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 990 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 991 | JDWP::JdwpError status; |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 992 | mirror::Array* dst = DecodeArray(array_id, status); |
| 993 | if (dst == NULL) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 994 | return status; |
| 995 | } |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 996 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 997 | if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 998 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 999 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1000 | } |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1001 | std::string descriptor(ClassHelper(dst->GetClass()).GetDescriptor()); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1002 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 1003 | |
| 1004 | if (IsPrimitiveTag(tag)) { |
| 1005 | size_t width = GetTagWidth(tag); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1006 | if (width == 8) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1007 | CopyArrayData<uint64_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1008 | } else if (width == 4) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1009 | CopyArrayData<uint32_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1010 | } else if (width == 2) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1011 | CopyArrayData<uint16_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1012 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1013 | CopyArrayData<uint8_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1014 | } |
| 1015 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1016 | mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>(); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1017 | for (int i = 0; i < count; ++i) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1018 | JDWP::ObjectId id = request.ReadObjectId(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1019 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1020 | if (o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1021 | return JDWP::ERR_INVALID_OBJECT; |
| 1022 | } |
| 1023 | oa->Set(offset + i, o); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1027 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1030 | JDWP::ObjectId Dbg::CreateString(const std::string& str) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1031 | return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str())); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1034 | JDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1035 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1036 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1037 | if (c == NULL) { |
| 1038 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1039 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1040 | new_object = gRegistry->Add(c->AllocObject(Thread::Current())); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1041 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 1044 | /* |
| 1045 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 1046 | */ |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1047 | JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1048 | JDWP::ObjectId& new_array) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1049 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1050 | mirror::Class* c = DecodeClass(array_class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1051 | if (c == NULL) { |
| 1052 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1053 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1054 | new_array = gRegistry->Add(mirror::Array::Alloc(Thread::Current(), c, length)); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1055 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1058 | bool Dbg::MatchType(JDWP::RefTypeId instance_class_id, JDWP::RefTypeId class_id) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1059 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1060 | mirror::Class* c1 = DecodeClass(instance_class_id, status); |
Elliott Hughes | a656a0f | 2012-02-21 18:03:44 -0800 | [diff] [blame] | 1061 | CHECK(c1 != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1062 | mirror::Class* c2 = DecodeClass(class_id, status); |
Elliott Hughes | a656a0f | 2012-02-21 18:03:44 -0800 | [diff] [blame] | 1063 | CHECK(c2 != NULL); |
| 1064 | return c1->IsAssignableFrom(c2); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1067 | static JDWP::FieldId ToFieldId(const mirror::Field* f) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1068 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1069 | #ifdef MOVING_GARBAGE_COLLECTOR |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1070 | UNIMPLEMENTED(FATAL); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1071 | #else |
| 1072 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
| 1073 | #endif |
| 1074 | } |
| 1075 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1076 | static JDWP::MethodId ToMethodId(const mirror::AbstractMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1077 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1078 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 1079 | UNIMPLEMENTED(FATAL); |
| 1080 | #else |
| 1081 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
| 1082 | #endif |
| 1083 | } |
| 1084 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1085 | static mirror::Field* FromFieldId(JDWP::FieldId fid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1086 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1087 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 1088 | UNIMPLEMENTED(FATAL); |
| 1089 | #else |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1090 | return reinterpret_cast<mirror::Field*>(static_cast<uintptr_t>(fid)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1091 | #endif |
| 1092 | } |
| 1093 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1094 | static mirror::AbstractMethod* FromMethodId(JDWP::MethodId mid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1095 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1096 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 1097 | UNIMPLEMENTED(FATAL); |
| 1098 | #else |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1099 | return reinterpret_cast<mirror::AbstractMethod*>(static_cast<uintptr_t>(mid)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1100 | #endif |
| 1101 | } |
| 1102 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1103 | static void SetLocation(JDWP::JdwpLocation& location, mirror::AbstractMethod* m, uint32_t dex_pc) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1104 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1105 | if (m == NULL) { |
| 1106 | memset(&location, 0, sizeof(location)); |
| 1107 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1108 | mirror::Class* c = m->GetDeclaringClass(); |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 1109 | location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1110 | location.class_id = gRegistry->Add(c); |
| 1111 | location.method_id = ToMethodId(m); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1112 | location.dex_pc = dex_pc; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1113 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1114 | } |
| 1115 | |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1116 | std::string Dbg::GetMethodName(JDWP::MethodId method_id) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1117 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1118 | mirror::AbstractMethod* m = FromMethodId(method_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1119 | return MethodHelper(m).GetName(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1122 | std::string Dbg::GetFieldName(JDWP::FieldId field_id) |
| 1123 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1124 | mirror::Field* f = FromFieldId(field_id); |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1125 | return FieldHelper(f).GetName(); |
| 1126 | } |
| 1127 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1128 | /* |
| 1129 | * Augment the access flags for synthetic methods and fields by setting |
| 1130 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 1131 | * flags not specified by the Java programming language. |
| 1132 | */ |
| 1133 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 1134 | accessFlags &= kAccJavaFlagsMask; |
| 1135 | if ((accessFlags & kAccSynthetic) != 0) { |
| 1136 | accessFlags |= 0xf0000000; |
| 1137 | } |
| 1138 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1141 | static const uint16_t kEclipseWorkaroundSlot = 1000; |
| 1142 | |
| 1143 | /* |
| 1144 | * Eclipse appears to expect that the "this" reference is in slot zero. |
| 1145 | * If it's not, the "variables" display will show two copies of "this", |
| 1146 | * possibly because it gets "this" from SF.ThisObject and then displays |
| 1147 | * all locals with nonzero slot numbers. |
| 1148 | * |
| 1149 | * So, we remap the item in slot 0 to 1000, and remap "this" to zero. On |
| 1150 | * SF.GetValues / SF.SetValues we map them back. |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1151 | * |
| 1152 | * TODO: jdb uses the value to determine whether a variable is a local or an argument, |
| 1153 | * by checking whether it's less than the number of arguments. To make that work, we'd |
| 1154 | * have to "mangle" all the arguments to come first, not just the implicit argument 'this'. |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1155 | */ |
| 1156 | static uint16_t MangleSlot(uint16_t slot, const char* name) { |
| 1157 | uint16_t newSlot = slot; |
| 1158 | if (strcmp(name, "this") == 0) { |
| 1159 | newSlot = 0; |
| 1160 | } else if (slot == 0) { |
| 1161 | newSlot = kEclipseWorkaroundSlot; |
| 1162 | } |
| 1163 | return newSlot; |
| 1164 | } |
| 1165 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1166 | static uint16_t DemangleSlot(uint16_t slot, mirror::AbstractMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1167 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1168 | if (slot == kEclipseWorkaroundSlot) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1169 | return 0; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1170 | } else if (slot == 0) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1171 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1172 | CHECK(code_item != NULL) << PrettyMethod(m); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1173 | return code_item->registers_size_ - code_item->ins_size_; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1174 | } |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1175 | return slot; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1176 | } |
| 1177 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1178 | JDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1179 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1180 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1181 | if (c == NULL) { |
| 1182 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1183 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1184 | |
| 1185 | size_t instance_field_count = c->NumInstanceFields(); |
| 1186 | size_t static_field_count = c->NumStaticFields(); |
| 1187 | |
| 1188 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 1189 | |
| 1190 | for (size_t i = 0; i < instance_field_count + static_field_count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1191 | mirror::Field* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1192 | FieldHelper fh(f); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1193 | expandBufAddFieldId(pReply, ToFieldId(f)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1194 | expandBufAddUtf8String(pReply, fh.GetName()); |
| 1195 | expandBufAddUtf8String(pReply, fh.GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1196 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1197 | static const char genericSignature[1] = ""; |
| 1198 | expandBufAddUtf8String(pReply, genericSignature); |
| 1199 | } |
| 1200 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 1201 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1202 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1203 | } |
| 1204 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1205 | JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1206 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1207 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1208 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1209 | if (c == NULL) { |
| 1210 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1211 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1212 | |
| 1213 | size_t direct_method_count = c->NumDirectMethods(); |
| 1214 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 1215 | |
| 1216 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 1217 | |
| 1218 | for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1219 | mirror::AbstractMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1220 | MethodHelper mh(m); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1221 | expandBufAddMethodId(pReply, ToMethodId(m)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1222 | expandBufAddUtf8String(pReply, mh.GetName()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1223 | expandBufAddUtf8String(pReply, mh.GetSignature()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1224 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1225 | static const char genericSignature[1] = ""; |
| 1226 | expandBufAddUtf8String(pReply, genericSignature); |
| 1227 | } |
| 1228 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 1229 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1230 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1233 | JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1234 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1235 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1236 | if (c == NULL) { |
| 1237 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1238 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1239 | |
| 1240 | ClassHelper kh(c); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 1241 | size_t interface_count = kh.NumDirectInterfaces(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1242 | expandBufAdd4BE(pReply, interface_count); |
| 1243 | for (size_t i = 0; i < interface_count; ++i) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1244 | expandBufAddRefTypeId(pReply, gRegistry->AddRefType(kh.GetDirectInterface(i))); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1245 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1246 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1249 | void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1250 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1251 | struct DebugCallbackContext { |
| 1252 | int numItems; |
| 1253 | JDWP::ExpandBuf* pReply; |
| 1254 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1255 | static bool Callback(void* context, uint32_t address, uint32_t line_number) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1256 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1257 | expandBufAdd8BE(pContext->pReply, address); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1258 | expandBufAdd4BE(pContext->pReply, line_number); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1259 | pContext->numItems++; |
| 1260 | return true; |
| 1261 | } |
| 1262 | }; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1263 | mirror::AbstractMethod* m = FromMethodId(method_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1264 | MethodHelper mh(m); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1265 | uint64_t start, end; |
| 1266 | if (m->IsNative()) { |
| 1267 | start = -1; |
| 1268 | end = -1; |
| 1269 | } else { |
| 1270 | start = 0; |
jeffhao | 14f0db9 | 2012-12-14 17:50:42 -0800 | [diff] [blame] | 1271 | // Return the index of the last instruction |
| 1272 | end = mh.GetCodeItem()->insns_size_in_code_units_ - 1; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | expandBufAdd8BE(pReply, start); |
| 1276 | expandBufAdd8BE(pReply, end); |
| 1277 | |
| 1278 | // Add numLines later |
| 1279 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 1280 | expandBufAdd4BE(pReply, 0); |
| 1281 | |
| 1282 | DebugCallbackContext context; |
| 1283 | context.numItems = 0; |
| 1284 | context.pReply = pReply; |
| 1285 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1286 | mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(), |
| 1287 | DebugCallbackContext::Callback, NULL, &context); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1288 | |
| 1289 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1292 | void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1293 | struct DebugCallbackContext { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1294 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1295 | size_t variable_count; |
| 1296 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1297 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1298 | static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, const char* name, const char* descriptor, const char* signature) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1299 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1300 | |
Elliott Hughes | ad3da69 | 2012-02-24 16:51:35 -0800 | [diff] [blame] | 1301 | 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 Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1302 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1303 | slot = MangleSlot(slot, name); |
| 1304 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1305 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 1306 | expandBufAddUtf8String(pContext->pReply, name); |
| 1307 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1308 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1309 | expandBufAddUtf8String(pContext->pReply, signature); |
| 1310 | } |
| 1311 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 1312 | expandBufAdd4BE(pContext->pReply, slot); |
| 1313 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1314 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1315 | } |
| 1316 | }; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1317 | mirror::AbstractMethod* m = FromMethodId(method_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1318 | MethodHelper mh(m); |
| 1319 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1320 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1321 | // arg_count considers doubles and longs to take 2 units. |
| 1322 | // variable_count considers everything to take 1 unit. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1323 | std::string shorty(mh.GetShorty()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1324 | expandBufAdd4BE(pReply, mirror::AbstractMethod::NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1325 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1326 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1327 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1328 | expandBufAdd4BE(pReply, 0); |
| 1329 | |
| 1330 | DebugCallbackContext context; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1331 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1332 | context.variable_count = 0; |
| 1333 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1334 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1335 | mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL, |
| 1336 | DebugCallbackContext::Callback, &context); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1337 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1338 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1341 | JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id, |
| 1342 | std::vector<uint8_t>& bytecodes) |
| 1343 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1344 | mirror::AbstractMethod* m = FromMethodId(method_id); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1345 | if (m == NULL) { |
| 1346 | return JDWP::ERR_INVALID_METHODID; |
| 1347 | } |
| 1348 | MethodHelper mh(m); |
| 1349 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
| 1350 | size_t byte_count = code_item->insns_size_in_code_units_ * 2; |
| 1351 | const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_); |
| 1352 | const uint8_t* end = begin + byte_count; |
| 1353 | for (const uint8_t* p = begin; p != end; ++p) { |
| 1354 | bytecodes.push_back(*p); |
| 1355 | } |
| 1356 | return JDWP::ERR_NONE; |
| 1357 | } |
| 1358 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1359 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) { |
| 1360 | return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1363 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) { |
| 1364 | return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1367 | static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id, |
| 1368 | JDWP::FieldId field_id, JDWP::ExpandBuf* pReply, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1369 | bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1370 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1371 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1372 | mirror::Class* c = DecodeClass(ref_type_id, status); |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1373 | if (ref_type_id != 0 && c == NULL) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1374 | return status; |
| 1375 | } |
| 1376 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1377 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1378 | if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1379 | return JDWP::ERR_INVALID_OBJECT; |
| 1380 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1381 | mirror::Field* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1382 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1383 | mirror::Class* receiver_class = c; |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1384 | if (receiver_class == NULL && o != NULL) { |
| 1385 | receiver_class = o->GetClass(); |
| 1386 | } |
| 1387 | // TODO: should we give up now if receiver_class is NULL? |
| 1388 | if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) { |
| 1389 | LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1390 | return JDWP::ERR_INVALID_FIELDID; |
| 1391 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1392 | |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1393 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1394 | // TODO: should we change the tests and check both? |
| 1395 | if (is_static) { |
| 1396 | if (!f->IsStatic()) { |
| 1397 | return JDWP::ERR_INVALID_FIELDID; |
| 1398 | } |
| 1399 | } else { |
| 1400 | if (f->IsStatic()) { |
| 1401 | LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1402 | } |
| 1403 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1404 | if (f->IsStatic()) { |
| 1405 | o = f->GetDeclaringClass(); |
| 1406 | } |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1407 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1408 | JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1409 | |
| 1410 | if (IsPrimitiveTag(tag)) { |
| 1411 | expandBufAdd1(pReply, tag); |
| 1412 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 1413 | expandBufAdd1(pReply, f->Get32(o)); |
| 1414 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 1415 | expandBufAdd2BE(pReply, f->Get32(o)); |
| 1416 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 1417 | expandBufAdd4BE(pReply, f->Get32(o)); |
| 1418 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1419 | expandBufAdd8BE(pReply, f->Get64(o)); |
| 1420 | } else { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1421 | LOG(FATAL) << "Unknown tag: " << tag; |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1422 | } |
| 1423 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1424 | mirror::Object* value = f->GetObject(o); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1425 | expandBufAdd1(pReply, TagFromObject(value)); |
| 1426 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 1427 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1428 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1431 | JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1432 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1433 | return GetFieldValueImpl(0, object_id, field_id, pReply, false); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1436 | JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) { |
| 1437 | return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1440 | static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1441 | uint64_t value, int width, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1442 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1443 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1444 | if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1445 | return JDWP::ERR_INVALID_OBJECT; |
| 1446 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1447 | mirror::Field* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1448 | |
| 1449 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1450 | // TODO: should we change the tests and check both? |
| 1451 | if (is_static) { |
| 1452 | if (!f->IsStatic()) { |
| 1453 | return JDWP::ERR_INVALID_FIELDID; |
| 1454 | } |
| 1455 | } else { |
| 1456 | if (f->IsStatic()) { |
| 1457 | LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1458 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1459 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1460 | if (f->IsStatic()) { |
| 1461 | o = f->GetDeclaringClass(); |
| 1462 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1463 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1464 | JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1465 | |
| 1466 | if (IsPrimitiveTag(tag)) { |
| 1467 | if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1468 | CHECK_EQ(width, 8); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1469 | f->Set64(o, value); |
| 1470 | } else { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1471 | CHECK_LE(width, 4); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1472 | f->Set32(o, value); |
| 1473 | } |
| 1474 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1475 | mirror::Object* v = gRegistry->Get<mirror::Object*>(value); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1476 | if (v == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1477 | return JDWP::ERR_INVALID_OBJECT; |
| 1478 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1479 | if (v != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1480 | mirror::Class* field_type = FieldHelper(f).GetType(); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1481 | if (!field_type->IsAssignableFrom(v->GetClass())) { |
| 1482 | return JDWP::ERR_INVALID_OBJECT; |
| 1483 | } |
| 1484 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1485 | f->SetObject(o, v); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1486 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1487 | |
| 1488 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1491 | JDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1492 | int width) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1493 | return SetFieldValueImpl(object_id, field_id, value, width, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1496 | JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) { |
| 1497 | return SetFieldValueImpl(0, field_id, value, width, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1500 | std::string Dbg::StringToUtf8(JDWP::ObjectId string_id) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1501 | mirror::String* s = gRegistry->Get<mirror::String*>(string_id); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1502 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1505 | JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1506 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1507 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1508 | Thread* thread; |
| 1509 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1510 | if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1511 | return error; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1512 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1513 | |
| 1514 | // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1515 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id); |
| 1516 | mirror::Field* java_lang_Thread_name_field = |
| 1517 | soa.DecodeField(WellKnownClasses::java_lang_Thread_name); |
| 1518 | mirror::String* s = |
| 1519 | reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object)); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1520 | if (s != NULL) { |
| 1521 | name = s->ToModifiedUtf8(); |
| 1522 | } |
| 1523 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1526 | JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1527 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1528 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1529 | if (thread_object == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1530 | return JDWP::ERR_INVALID_OBJECT; |
| 1531 | } |
| 1532 | |
| 1533 | // Okay, so it's an object, but is it actually a thread? |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1534 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1535 | Thread* thread; |
| 1536 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1537 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1538 | // Zombie threads are in the null group. |
| 1539 | expandBufAddObjectId(pReply, JDWP::ObjectId(0)); |
| 1540 | return JDWP::ERR_NONE; |
| 1541 | } |
| 1542 | if (error != JDWP::ERR_NONE) { |
| 1543 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1544 | } |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1545 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1546 | mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Thread;"); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1547 | CHECK(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1548 | mirror::Field* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;"); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1549 | CHECK(f != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1550 | mirror::Object* group = f->GetObject(thread_object); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1551 | CHECK(group != NULL); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1552 | JDWP::ObjectId thread_group_id = gRegistry->Add(group); |
| 1553 | |
| 1554 | expandBufAddObjectId(pReply, thread_group_id); |
| 1555 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1558 | std::string Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1559 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1560 | mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1561 | CHECK(thread_group != NULL); |
| 1562 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1563 | mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1564 | CHECK(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1565 | mirror::Field* f = c->FindInstanceField("name", "Ljava/lang/String;"); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1566 | CHECK(f != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1567 | mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group)); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1568 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1571 | JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1572 | mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1573 | CHECK(thread_group != NULL); |
| 1574 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1575 | mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1576 | CHECK(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1577 | mirror::Field* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;"); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1578 | CHECK(f != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1579 | mirror::Object* parent = f->GetObject(thread_group); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1580 | return gRegistry->Add(parent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1584 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1585 | mirror::Field* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup); |
| 1586 | mirror::Object* group = f->GetObject(f->GetDeclaringClass()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1587 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | JDWP::ObjectId Dbg::GetMainThreadGroupId() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1591 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1592 | mirror::Field* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup); |
| 1593 | mirror::Object* group = f->GetObject(f->GetDeclaringClass()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1594 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1597 | JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, JDWP::JdwpSuspendStatus* pSuspendStatus) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1598 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1599 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 1600 | *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED; |
| 1601 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1602 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1603 | Thread* thread; |
| 1604 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1605 | if (error != JDWP::ERR_NONE) { |
| 1606 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1607 | *pThreadStatus = JDWP::TS_ZOMBIE; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1608 | return JDWP::ERR_NONE; |
| 1609 | } |
| 1610 | return error; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1611 | } |
| 1612 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 1613 | if (IsSuspendedForDebugger(soa, thread)) { |
| 1614 | *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1615 | } |
| 1616 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 1617 | switch (thread->GetState()) { |
| 1618 | case kBlocked: *pThreadStatus = JDWP::TS_MONITOR; break; |
| 1619 | case kNative: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1620 | case kRunnable: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1621 | case kSleeping: *pThreadStatus = JDWP::TS_SLEEPING; break; |
| 1622 | case kStarting: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1623 | case kSuspended: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1624 | case kTerminated: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1625 | case kTimedWaiting: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1626 | case kWaitingForDebuggerSend: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1627 | case kWaitingForDebuggerSuspension: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1628 | case kWaitingForDebuggerToAttach: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1629 | case kWaitingForGcToComplete: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1630 | case kWaitingForJniOnLoad: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1631 | case kWaitingForSignalCatcherOutput: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1632 | case kWaitingInMainDebuggerLoop: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1633 | case kWaitingInMainSignalCatcherLoop: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1634 | case kWaitingPerformingGc: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1635 | case kWaiting: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1636 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 1637 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1638 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1641 | JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1642 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1643 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1644 | Thread* thread; |
| 1645 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1646 | if (error != JDWP::ERR_NONE) { |
| 1647 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1648 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1649 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1650 | expandBufAdd4BE(pReply, thread->GetDebugSuspendCount()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1651 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 1654 | JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) { |
| 1655 | ScopedObjectAccess soa(Thread::Current()); |
| 1656 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 1657 | Thread* thread; |
| 1658 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1659 | if (error != JDWP::ERR_NONE) { |
| 1660 | return error; |
| 1661 | } |
| 1662 | thread->Interrupt(); |
| 1663 | return JDWP::ERR_NONE; |
| 1664 | } |
| 1665 | |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1666 | void Dbg::GetThreads(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& thread_ids) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1667 | class ThreadListVisitor { |
| 1668 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1669 | ThreadListVisitor(const ScopedObjectAccessUnchecked& soa, mirror::Object* desired_thread_group, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1670 | std::vector<JDWP::ObjectId>& thread_ids) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1671 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1672 | : soa_(soa), desired_thread_group_(desired_thread_group), thread_ids_(thread_ids) {} |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1673 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1674 | static void Visit(Thread* t, void* arg) { |
| 1675 | reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t); |
| 1676 | } |
| 1677 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1678 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1679 | // annotalysis. |
| 1680 | void Visit(Thread* t) NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1681 | if (t == Dbg::GetDebugThread()) { |
| 1682 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 1683 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 1684 | return; |
| 1685 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1686 | mirror::Object* peer = t->GetPeer(); |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1687 | if (IsInDesiredThreadGroup(peer)) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1688 | thread_ids_.push_back(gRegistry->Add(peer)); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1692 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1693 | bool IsInDesiredThreadGroup(mirror::Object* peer) |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1694 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1695 | // peer might be NULL if the thread is still starting up. |
| 1696 | if (peer == NULL) { |
| 1697 | // We can't tell the debugger about this thread yet. |
| 1698 | // TODO: if we identified threads to the debugger by their Thread* |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1699 | // rather than their peer's mirror::Object*, we could fix this. |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1700 | // Doing so might help us report ZOMBIE threads too. |
| 1701 | return false; |
| 1702 | } |
jeffhao | c1e0490 | 2012-12-13 12:41:10 -0800 | [diff] [blame] | 1703 | // Do we want threads from all thread groups? |
| 1704 | if (desired_thread_group_ == NULL) { |
| 1705 | return true; |
| 1706 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1707 | mirror::Object* group = soa_.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(peer); |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1708 | return (group == desired_thread_group_); |
| 1709 | } |
| 1710 | |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 1711 | const ScopedObjectAccessUnchecked& soa_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1712 | mirror::Object* const desired_thread_group_; |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1713 | std::vector<JDWP::ObjectId>& thread_ids_; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1714 | }; |
| 1715 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1716 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1717 | mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1718 | ThreadListVisitor tlv(soa, thread_group, thread_ids); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1719 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1720 | Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1721 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1722 | |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1723 | void Dbg::GetChildThreadGroups(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& child_thread_group_ids) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1724 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1725 | mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1726 | |
| 1727 | // Get the ArrayList<ThreadGroup> "groups" out of this thread group... |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1728 | mirror::Field* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;"); |
| 1729 | mirror::Object* groups_array_list = groups_field->GetObject(thread_group); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1730 | |
| 1731 | // Get the array and size out of the ArrayList<ThreadGroup>... |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1732 | mirror::Field* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;"); |
| 1733 | mirror::Field* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I"); |
| 1734 | mirror::ObjectArray<mirror::Object>* groups_array = |
| 1735 | array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1736 | const int32_t size = size_field->GetInt(groups_array_list); |
| 1737 | |
| 1738 | // Copy the first 'size' elements out of the array into the result. |
| 1739 | for (int32_t i = 0; i < size; ++i) { |
| 1740 | child_thread_group_ids.push_back(gRegistry->Add(groups_array->Get(i))); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1744 | static int GetStackDepth(Thread* thread) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1745 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1746 | struct CountStackDepthVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1747 | CountStackDepthVisitor(Thread* thread) |
| 1748 | : StackVisitor(thread, NULL), depth(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1749 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1750 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1751 | // annotalysis. |
| 1752 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1753 | if (!GetMethod()->IsRuntimeMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1754 | ++depth; |
| 1755 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1756 | return true; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1757 | } |
| 1758 | size_t depth; |
| 1759 | }; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1760 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1761 | CountStackDepthVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1762 | visitor.WalkStack(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1763 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1766 | JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1767 | ScopedObjectAccess soa(Thread::Current()); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1768 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1769 | Thread* thread; |
| 1770 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1771 | if (error != JDWP::ERR_NONE) { |
| 1772 | return error; |
| 1773 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 1774 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 1775 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 1776 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1777 | result = GetStackDepth(thread); |
| 1778 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1779 | } |
| 1780 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 1781 | JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame, |
| 1782 | size_t frame_count, JDWP::ExpandBuf* buf) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1783 | class GetFrameVisitor : public StackVisitor { |
| 1784 | public: |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1785 | GetFrameVisitor(Thread* thread, size_t start_frame, size_t frame_count, JDWP::ExpandBuf* buf) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1786 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1787 | : StackVisitor(thread, NULL), depth_(0), |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1788 | start_frame_(start_frame), frame_count_(frame_count), buf_(buf) { |
| 1789 | expandBufAdd4BE(buf_, frame_count_); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1790 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1791 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1792 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1793 | // annotalysis. |
| 1794 | virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1795 | if (GetMethod()->IsRuntimeMethod()) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1796 | return true; // The debugger can't do anything useful with a frame that has no Method*. |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1797 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1798 | if (depth_ >= start_frame_ + frame_count_) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1799 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1800 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1801 | if (depth_ >= start_frame_) { |
| 1802 | JDWP::FrameId frame_id(GetFrameId()); |
| 1803 | JDWP::JdwpLocation location; |
| 1804 | SetLocation(location, GetMethod(), GetDexPc()); |
Elliott Hughes | 7baf96f | 2012-06-22 16:33:50 -0700 | [diff] [blame] | 1805 | VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3lld ", depth_, frame_id) << location; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1806 | expandBufAdd8BE(buf_, frame_id); |
| 1807 | expandBufAddLocation(buf_, location); |
| 1808 | } |
| 1809 | ++depth_; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1810 | return true; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1811 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1812 | |
| 1813 | private: |
| 1814 | size_t depth_; |
| 1815 | const size_t start_frame_; |
| 1816 | const size_t frame_count_; |
| 1817 | JDWP::ExpandBuf* buf_; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1818 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1819 | |
| 1820 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1821 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1822 | Thread* thread; |
| 1823 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1824 | if (error != JDWP::ERR_NONE) { |
| 1825 | return error; |
| 1826 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 1827 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 1828 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 1829 | } |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1830 | GetFrameVisitor visitor(thread, start_frame, frame_count, buf); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1831 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1832 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 1836 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1837 | return gRegistry->Add(soa.Self()->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1840 | void Dbg::SuspendVM() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1841 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | void Dbg::ResumeVM() { |
Elliott Hughes | c61a267 | 2012-06-21 14:52:29 -0700 | [diff] [blame] | 1845 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1848 | JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1849 | ScopedLocalRef<jobject> peer(Thread::Current()->GetJniEnv(), NULL); |
| 1850 | { |
| 1851 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1852 | peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id))); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1853 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1854 | if (peer.get() == NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1855 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 1856 | } |
| 1857 | // Suspend thread to build stack trace. |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 1858 | bool timed_out; |
| 1859 | Thread* thread = Thread::SuspendForDebugger(peer.get(), request_suspension, &timed_out); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1860 | if (thread != NULL) { |
| 1861 | return JDWP::ERR_NONE; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 1862 | } else if (timed_out) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1863 | return JDWP::ERR_INTERNAL; |
| 1864 | } else { |
| 1865 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 1866 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1869 | void Dbg::ResumeThread(JDWP::ObjectId thread_id) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1870 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1871 | mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1872 | Thread* thread; |
| 1873 | { |
| 1874 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 1875 | thread = Thread::FromManagedThread(soa, peer); |
| 1876 | } |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1877 | if (thread == NULL) { |
| 1878 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 1879 | return; |
| 1880 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1881 | bool needs_resume; |
| 1882 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1883 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1884 | needs_resume = thread->GetSuspendCount() > 0; |
| 1885 | } |
| 1886 | if (needs_resume) { |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1887 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
| 1888 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1892 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1895 | struct GetThisVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1896 | GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1897 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1898 | : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1899 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1900 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1901 | // annotalysis. |
| 1902 | virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1903 | if (frame_id != GetFrameId()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1904 | return true; // continue |
| 1905 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1906 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1907 | if (m->IsNative() || m->IsStatic()) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1908 | this_object = NULL; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1909 | } else { |
| 1910 | uint16_t reg = DemangleSlot(0, m); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1911 | this_object = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg)); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1912 | } |
| 1913 | return false; |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1914 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1915 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1916 | mirror::Object* this_object; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1917 | JDWP::FrameId frame_id; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1918 | }; |
| 1919 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1920 | static mirror::Object* GetThis(Thread* self, mirror::AbstractMethod* m, size_t frame_id) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1921 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1922 | // TODO: should we return the 'this' we passed through to non-static native methods? |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1923 | if (m->IsNative() || m->IsStatic()) { |
| 1924 | return NULL; |
| 1925 | } |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1926 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1927 | UniquePtr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1928 | GetThisVisitor visitor(self, context.get(), frame_id); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1929 | visitor.WalkStack(); |
| 1930 | return visitor.this_object; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1931 | } |
| 1932 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1933 | JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, |
| 1934 | JDWP::ObjectId* result) { |
| 1935 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1936 | Thread* thread; |
| 1937 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1938 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1939 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1940 | if (error != JDWP::ERR_NONE) { |
| 1941 | return error; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1942 | } |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 1943 | if (!IsSuspendedForDebugger(soa, thread)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1944 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 1945 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1946 | } |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1947 | UniquePtr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1948 | GetThisVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1949 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1950 | *result = gRegistry->Add(visitor.this_object); |
| 1951 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1954 | void Dbg::GetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1955 | uint8_t* buf, size_t width) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1956 | struct GetLocalVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1957 | GetLocalVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id, int slot, |
| 1958 | JDWP::JdwpTag tag, uint8_t* buf, size_t width) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1959 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1960 | : StackVisitor(thread, context), frame_id_(frame_id), slot_(slot), tag_(tag), |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 1961 | buf_(buf), width_(width) {} |
| 1962 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1963 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1964 | // annotalysis. |
| 1965 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1966 | if (GetFrameId() != frame_id_) { |
| 1967 | return true; // Not our frame, carry on. |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1968 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1969 | // TODO: check that the tag is compatible with the actual type of the slot! |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1970 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1971 | uint16_t reg = DemangleSlot(slot_, m); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1972 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1973 | switch (tag_) { |
| 1974 | case JDWP::JT_BOOLEAN: |
| 1975 | { |
| 1976 | CHECK_EQ(width_, 1U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1977 | uint32_t intVal = GetVReg(m, reg, kIntVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1978 | VLOG(jdwp) << "get boolean local " << reg << " = " << intVal; |
| 1979 | JDWP::Set1(buf_+1, intVal != 0); |
| 1980 | } |
| 1981 | break; |
| 1982 | case JDWP::JT_BYTE: |
| 1983 | { |
| 1984 | CHECK_EQ(width_, 1U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1985 | uint32_t intVal = GetVReg(m, reg, kIntVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1986 | VLOG(jdwp) << "get byte local " << reg << " = " << intVal; |
| 1987 | JDWP::Set1(buf_+1, intVal); |
| 1988 | } |
| 1989 | break; |
| 1990 | case JDWP::JT_SHORT: |
| 1991 | case JDWP::JT_CHAR: |
| 1992 | { |
| 1993 | CHECK_EQ(width_, 2U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1994 | uint32_t intVal = GetVReg(m, reg, kIntVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1995 | VLOG(jdwp) << "get short/char local " << reg << " = " << intVal; |
| 1996 | JDWP::Set2BE(buf_+1, intVal); |
| 1997 | } |
| 1998 | break; |
| 1999 | case JDWP::JT_INT: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2000 | { |
| 2001 | CHECK_EQ(width_, 4U); |
| 2002 | uint32_t intVal = GetVReg(m, reg, kIntVReg); |
| 2003 | VLOG(jdwp) << "get int local " << reg << " = " << intVal; |
| 2004 | JDWP::Set4BE(buf_+1, intVal); |
| 2005 | } |
| 2006 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2007 | case JDWP::JT_FLOAT: |
| 2008 | { |
| 2009 | CHECK_EQ(width_, 4U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2010 | uint32_t intVal = GetVReg(m, reg, kFloatVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2011 | VLOG(jdwp) << "get int/float local " << reg << " = " << intVal; |
| 2012 | JDWP::Set4BE(buf_+1, intVal); |
| 2013 | } |
| 2014 | break; |
| 2015 | case JDWP::JT_ARRAY: |
| 2016 | { |
| 2017 | CHECK_EQ(width_, sizeof(JDWP::ObjectId)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2018 | mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg)); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2019 | VLOG(jdwp) << "get array local " << reg << " = " << o; |
| 2020 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) { |
| 2021 | LOG(FATAL) << "Register " << reg << " expected to hold array: " << o; |
| 2022 | } |
| 2023 | JDWP::SetObjectId(buf_+1, gRegistry->Add(o)); |
| 2024 | } |
| 2025 | break; |
| 2026 | case JDWP::JT_CLASS_LOADER: |
| 2027 | case JDWP::JT_CLASS_OBJECT: |
| 2028 | case JDWP::JT_OBJECT: |
| 2029 | case JDWP::JT_STRING: |
| 2030 | case JDWP::JT_THREAD: |
| 2031 | case JDWP::JT_THREAD_GROUP: |
| 2032 | { |
| 2033 | CHECK_EQ(width_, sizeof(JDWP::ObjectId)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2034 | mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg)); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2035 | VLOG(jdwp) << "get object local " << reg << " = " << o; |
| 2036 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) { |
| 2037 | LOG(FATAL) << "Register " << reg << " expected to hold object: " << o; |
| 2038 | } |
| 2039 | tag_ = TagFromObject(o); |
| 2040 | JDWP::SetObjectId(buf_+1, gRegistry->Add(o)); |
| 2041 | } |
| 2042 | break; |
| 2043 | case JDWP::JT_DOUBLE: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2044 | { |
| 2045 | CHECK_EQ(width_, 8U); |
| 2046 | uint32_t lo = GetVReg(m, reg, kDoubleLoVReg); |
| 2047 | uint64_t hi = GetVReg(m, reg + 1, kDoubleHiVReg); |
| 2048 | uint64_t longVal = (hi << 32) | lo; |
| 2049 | VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal; |
| 2050 | JDWP::Set8BE(buf_+1, longVal); |
| 2051 | } |
| 2052 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2053 | case JDWP::JT_LONG: |
| 2054 | { |
| 2055 | CHECK_EQ(width_, 8U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2056 | uint32_t lo = GetVReg(m, reg, kLongLoVReg); |
| 2057 | uint64_t hi = GetVReg(m, reg + 1, kLongHiVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2058 | uint64_t longVal = (hi << 32) | lo; |
| 2059 | VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal; |
| 2060 | JDWP::Set8BE(buf_+1, longVal); |
| 2061 | } |
| 2062 | break; |
| 2063 | default: |
| 2064 | LOG(FATAL) << "Unknown tag " << tag_; |
| 2065 | break; |
| 2066 | } |
| 2067 | |
| 2068 | // Prepend tag, which may have been updated. |
| 2069 | JDWP::Set1(buf_, tag_); |
| 2070 | return false; |
| 2071 | } |
| 2072 | |
| 2073 | const JDWP::FrameId frame_id_; |
| 2074 | const int slot_; |
| 2075 | JDWP::JdwpTag tag_; |
| 2076 | uint8_t* const buf_; |
| 2077 | const size_t width_; |
| 2078 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2079 | |
| 2080 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2081 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2082 | Thread* thread; |
| 2083 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 2084 | if (error != JDWP::ERR_NONE) { |
| 2085 | return; |
| 2086 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2087 | UniquePtr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2088 | GetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, buf, width); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2089 | visitor.WalkStack(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2090 | } |
| 2091 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 2092 | void Dbg::SetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2093 | uint64_t value, size_t width) { |
| 2094 | struct SetLocalVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2095 | SetLocalVisitor(Thread* thread, Context* context, |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2096 | JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint64_t value, |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2097 | size_t width) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2098 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2099 | : StackVisitor(thread, context), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2100 | frame_id_(frame_id), slot_(slot), tag_(tag), value_(value), width_(width) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2101 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2102 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2103 | // annotalysis. |
| 2104 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2105 | if (GetFrameId() != frame_id_) { |
| 2106 | return true; // Not our frame, carry on. |
| 2107 | } |
| 2108 | // TODO: check that the tag is compatible with the actual type of the slot! |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2109 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2110 | uint16_t reg = DemangleSlot(slot_, m); |
| 2111 | |
| 2112 | switch (tag_) { |
| 2113 | case JDWP::JT_BOOLEAN: |
| 2114 | case JDWP::JT_BYTE: |
| 2115 | CHECK_EQ(width_, 1U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2116 | SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2117 | break; |
| 2118 | case JDWP::JT_SHORT: |
| 2119 | case JDWP::JT_CHAR: |
| 2120 | CHECK_EQ(width_, 2U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2121 | SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2122 | break; |
| 2123 | case JDWP::JT_INT: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2124 | CHECK_EQ(width_, 4U); |
| 2125 | SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg); |
| 2126 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2127 | case JDWP::JT_FLOAT: |
| 2128 | CHECK_EQ(width_, 4U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2129 | SetVReg(m, reg, static_cast<uint32_t>(value_), kFloatVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2130 | break; |
| 2131 | case JDWP::JT_ARRAY: |
| 2132 | case JDWP::JT_OBJECT: |
| 2133 | case JDWP::JT_STRING: |
| 2134 | { |
| 2135 | CHECK_EQ(width_, sizeof(JDWP::ObjectId)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2136 | mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value_)); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2137 | if (o == ObjectRegistry::kInvalidObject) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2138 | UNIMPLEMENTED(FATAL) << "return an error code when given an invalid object to store"; |
| 2139 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2140 | SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), kReferenceVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2141 | } |
| 2142 | break; |
| 2143 | case JDWP::JT_DOUBLE: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2144 | CHECK_EQ(width_, 8U); |
| 2145 | SetVReg(m, reg, static_cast<uint32_t>(value_), kDoubleLoVReg); |
| 2146 | SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kDoubleHiVReg); |
| 2147 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2148 | case JDWP::JT_LONG: |
| 2149 | CHECK_EQ(width_, 8U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2150 | SetVReg(m, reg, static_cast<uint32_t>(value_), kLongLoVReg); |
| 2151 | SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kLongHiVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2152 | break; |
| 2153 | default: |
| 2154 | LOG(FATAL) << "Unknown tag " << tag_; |
| 2155 | break; |
| 2156 | } |
| 2157 | return false; |
| 2158 | } |
| 2159 | |
| 2160 | const JDWP::FrameId frame_id_; |
| 2161 | const int slot_; |
| 2162 | const JDWP::JdwpTag tag_; |
| 2163 | const uint64_t value_; |
| 2164 | const size_t width_; |
| 2165 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2166 | |
| 2167 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2168 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2169 | Thread* thread; |
| 2170 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 2171 | if (error != JDWP::ERR_NONE) { |
| 2172 | return; |
| 2173 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2174 | UniquePtr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2175 | SetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, value, width); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2176 | visitor.WalkStack(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2179 | void Dbg::PostLocationEvent(const mirror::AbstractMethod* m, int dex_pc, mirror::Object* this_object, int event_flags) { |
| 2180 | mirror::Class* c = m->GetDeclaringClass(); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2181 | |
| 2182 | JDWP::JdwpLocation location; |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 2183 | location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2184 | location.class_id = gRegistry->AddRefType(c); |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 2185 | location.method_id = ToMethodId(m); |
Elliott Hughes | 972a47b | 2012-02-21 18:16:06 -0800 | [diff] [blame] | 2186 | location.dex_pc = m->IsNative() ? -1 : dex_pc; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2187 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2188 | // If 'this_object' isn't already in the registry, we know that we're not looking for it, |
| 2189 | // so there's no point adding it to the registry and burning through ids. |
| 2190 | JDWP::ObjectId this_id = 0; |
| 2191 | if (gRegistry->Contains(this_object)) { |
| 2192 | this_id = gRegistry->Add(this_object); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2193 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2194 | gJdwpState->PostLocationEvent(&location, this_id, event_flags); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2197 | void Dbg::PostException(Thread* thread, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2198 | JDWP::FrameId throw_frame_id, mirror::AbstractMethod* throw_method, |
| 2199 | uint32_t throw_dex_pc, mirror::AbstractMethod* catch_method, |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2200 | uint32_t catch_dex_pc, mirror::Throwable* exception_object) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2201 | if (!IsDebuggerActive()) { |
Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 2202 | return; |
| 2203 | } |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2204 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2205 | JDWP::JdwpLocation throw_location; |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2206 | SetLocation(throw_location, throw_method, throw_dex_pc); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2207 | JDWP::JdwpLocation catch_location; |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2208 | SetLocation(catch_location, catch_method, catch_dex_pc); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2209 | |
| 2210 | // We need 'this' for InstanceOnly filters. |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2211 | UniquePtr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2212 | GetThisVisitor visitor(thread, context.get(), throw_frame_id); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2213 | visitor.WalkStack(); |
| 2214 | JDWP::ObjectId this_id = gRegistry->Add(visitor.this_object); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2215 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2216 | JDWP::ObjectId exception_id = gRegistry->Add(exception_object); |
| 2217 | JDWP::RefTypeId exception_class_id = gRegistry->AddRefType(exception_object->GetClass()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2218 | |
| 2219 | gJdwpState->PostException(&throw_location, exception_id, exception_class_id, &catch_location, this_id); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2222 | void Dbg::PostClassPrepare(mirror::Class* c) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2223 | if (!IsDebuggerActive()) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2224 | return; |
| 2225 | } |
| 2226 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2227 | // OLD-TODO - we currently always send both "verified" and "prepared" since |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2228 | // debuggers seem to like that. There might be some advantage to honesty, |
| 2229 | // since the class may not yet be verified. |
| 2230 | int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 2231 | JDWP::JdwpTypeTag tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 2232 | gJdwpState->PostClassPrepare(tag, gRegistry->Add(c), ClassHelper(c).GetDescriptor(), state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2235 | void Dbg::UpdateDebugger(int32_t dex_pc, Thread* self) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2236 | if (!IsDebuggerActive() || dex_pc == -2 /* fake method exit */) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2237 | return; |
| 2238 | } |
| 2239 | |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2240 | size_t frame_id; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2241 | mirror::AbstractMethod* m = self->GetCurrentMethod(NULL, &frame_id); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2242 | //LOG(INFO) << "UpdateDebugger " << PrettyMethod(m) << "@" << dex_pc << " frame " << frame_id; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2243 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2244 | if (dex_pc == -1) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 2245 | // We use a pc of -1 to represent method entry, since we might branch back to pc 0 later. |
| 2246 | // This means that for this special notification, there can't be anything else interesting |
| 2247 | // going on, so we're done already. |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2248 | Dbg::PostLocationEvent(m, 0, GetThis(self, m, frame_id), kMethodEntry); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 2249 | return; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2250 | } |
| 2251 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 2252 | int event_flags = 0; |
| 2253 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2254 | if (IsBreakpoint(m, dex_pc)) { |
| 2255 | event_flags |= kBreakpoint; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2256 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2257 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2258 | { |
| 2259 | // If the debugger is single-stepping one of our threads, check to |
| 2260 | // see if we're that thread and we've reached a step point. |
| 2261 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| 2262 | if (gSingleStepControl.is_active && gSingleStepControl.thread == self) { |
| 2263 | CHECK(!m->IsNative()); |
| 2264 | if (gSingleStepControl.step_depth == JDWP::SD_INTO) { |
| 2265 | // Step into method calls. We break when the line number |
| 2266 | // or method pointer changes. If we're in SS_MIN mode, we |
| 2267 | // always stop. |
| 2268 | if (gSingleStepControl.method != m) { |
| 2269 | event_flags |= kSingleStep; |
| 2270 | VLOG(jdwp) << "SS new method"; |
| 2271 | } else if (gSingleStepControl.step_size == JDWP::SS_MIN) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2272 | event_flags |= kSingleStep; |
| 2273 | VLOG(jdwp) << "SS new instruction"; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2274 | } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) { |
| 2275 | event_flags |= kSingleStep; |
| 2276 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2277 | } |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2278 | } else if (gSingleStepControl.step_depth == JDWP::SD_OVER) { |
| 2279 | // Step over method calls. We break when the line number is |
| 2280 | // different and the frame depth is <= the original frame |
| 2281 | // depth. (We can't just compare on the method, because we |
| 2282 | // might get unrolled past it by an exception, and it's tricky |
| 2283 | // to identify recursion.) |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2284 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2285 | int stack_depth = GetStackDepth(self); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2286 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2287 | if (stack_depth < gSingleStepControl.stack_depth) { |
| 2288 | // popped up one or more frames, always trigger |
| 2289 | event_flags |= kSingleStep; |
| 2290 | VLOG(jdwp) << "SS method pop"; |
| 2291 | } else if (stack_depth == gSingleStepControl.stack_depth) { |
| 2292 | // same depth, see if we moved |
| 2293 | if (gSingleStepControl.step_size == JDWP::SS_MIN) { |
| 2294 | event_flags |= kSingleStep; |
| 2295 | VLOG(jdwp) << "SS new instruction"; |
| 2296 | } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) { |
| 2297 | event_flags |= kSingleStep; |
| 2298 | VLOG(jdwp) << "SS new line"; |
| 2299 | } |
| 2300 | } |
| 2301 | } else { |
| 2302 | CHECK_EQ(gSingleStepControl.step_depth, JDWP::SD_OUT); |
| 2303 | // Return from the current method. We break when the frame |
| 2304 | // depth pops up. |
| 2305 | |
| 2306 | // This differs from the "method exit" break in that it stops |
| 2307 | // with the PC at the next instruction in the returned-to |
| 2308 | // function, rather than the end of the returning function. |
| 2309 | |
| 2310 | int stack_depth = GetStackDepth(self); |
| 2311 | if (stack_depth < gSingleStepControl.stack_depth) { |
| 2312 | event_flags |= kSingleStep; |
| 2313 | VLOG(jdwp) << "SS method pop"; |
| 2314 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2315 | } |
| 2316 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2317 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2318 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2319 | // Check to see if this is a "return" instruction. JDWP says we should |
| 2320 | // send the event *after* the code has been executed, but it also says |
| 2321 | // the location we provide is the last instruction. Since the "return" |
| 2322 | // instruction has no interesting side effects, we should be safe. |
| 2323 | // (We can't just move this down to the returnFromMethod label because |
| 2324 | // we potentially need to combine it with other events.) |
| 2325 | // We're also not supposed to generate a method exit event if the method |
| 2326 | // terminates "with a thrown exception". |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2327 | if (dex_pc >= 0) { |
| 2328 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2329 | CHECK(code_item != NULL) << PrettyMethod(m) << " @" << dex_pc; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2330 | CHECK_LT(dex_pc, static_cast<int32_t>(code_item->insns_size_in_code_units_)); |
| 2331 | if (Instruction::At(&code_item->insns_[dex_pc])->IsReturn()) { |
| 2332 | event_flags |= kMethodExit; |
| 2333 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2334 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2335 | |
| 2336 | // If there's something interesting going on, see if it matches one |
| 2337 | // of the debugger filters. |
| 2338 | if (event_flags != 0) { |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2339 | Dbg::PostLocationEvent(m, dex_pc, GetThis(self, m, frame_id), event_flags); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2340 | } |
| 2341 | } |
| 2342 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2343 | void Dbg::WatchLocation(const JDWP::JdwpLocation* location) { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2344 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2345 | mirror::AbstractMethod* m = FromMethodId(location->method_id); |
Elliott Hughes | 972a47b | 2012-02-21 18:16:06 -0800 | [diff] [blame] | 2346 | gBreakpoints.push_back(Breakpoint(m, location->dex_pc)); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2347 | VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " << gBreakpoints[gBreakpoints.size() - 1]; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2348 | } |
| 2349 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2350 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location) { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2351 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2352 | mirror::AbstractMethod* m = FromMethodId(location->method_id); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2353 | for (size_t i = 0; i < gBreakpoints.size(); ++i) { |
Elliott Hughes | 972a47b | 2012-02-21 18:16:06 -0800 | [diff] [blame] | 2354 | if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == location->dex_pc) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2355 | VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i]; |
| 2356 | gBreakpoints.erase(gBreakpoints.begin() + i); |
| 2357 | return; |
| 2358 | } |
| 2359 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2360 | } |
| 2361 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2362 | JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2363 | JDWP::JdwpStepDepth step_depth) { |
| 2364 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2365 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2366 | Thread* thread; |
| 2367 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 2368 | if (error != JDWP::ERR_NONE) { |
| 2369 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2370 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2371 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2372 | MutexLock mu2(soa.Self(), *Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2373 | // TODO: there's no theoretical reason why we couldn't support single-stepping |
| 2374 | // of multiple threads at once, but we never did so historically. |
| 2375 | if (gSingleStepControl.thread != NULL && thread != gSingleStepControl.thread) { |
| 2376 | LOG(WARNING) << "single-step already active for " << *gSingleStepControl.thread |
| 2377 | << "; switching to " << *thread; |
| 2378 | } |
| 2379 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2380 | // |
| 2381 | // Work out what Method* we're in, the current line number, and how deep the stack currently |
| 2382 | // is for step-out. |
| 2383 | // |
| 2384 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2385 | struct SingleStepStackVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2386 | SingleStepStackVisitor(Thread* thread) |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2387 | EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2388 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2389 | : StackVisitor(thread, NULL) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2390 | gSingleStepControl.method = NULL; |
| 2391 | gSingleStepControl.stack_depth = 0; |
| 2392 | } |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2393 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2394 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2395 | // annotalysis. |
| 2396 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2397 | Locks::breakpoint_lock_->AssertHeld(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2398 | const mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2399 | if (!m->IsRuntimeMethod()) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2400 | ++gSingleStepControl.stack_depth; |
| 2401 | if (gSingleStepControl.method == NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2402 | const mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache(); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2403 | gSingleStepControl.method = m; |
| 2404 | gSingleStepControl.line_number = -1; |
| 2405 | if (dex_cache != NULL) { |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 2406 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2407 | gSingleStepControl.line_number = dex_file.GetLineNumFromPC(m, GetDexPc()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2408 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2409 | } |
| 2410 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2411 | return true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2412 | } |
| 2413 | }; |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2414 | SingleStepStackVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2415 | visitor.WalkStack(); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2416 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2417 | // |
| 2418 | // Find the dex_pc values that correspond to the current line, for line-based single-stepping. |
| 2419 | // |
| 2420 | |
| 2421 | struct DebugCallbackContext { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2422 | DebugCallbackContext() EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2423 | last_pc_valid = false; |
| 2424 | last_pc = 0; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2425 | } |
| 2426 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2427 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2428 | // annotalysis. |
| 2429 | static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) NO_THREAD_SAFETY_ANALYSIS { |
| 2430 | Locks::breakpoint_lock_->AssertHeld(Thread::Current()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2431 | DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context); |
| 2432 | if (static_cast<int32_t>(line_number) == gSingleStepControl.line_number) { |
| 2433 | if (!context->last_pc_valid) { |
| 2434 | // Everything from this address until the next line change is ours. |
| 2435 | context->last_pc = address; |
| 2436 | context->last_pc_valid = true; |
| 2437 | } |
| 2438 | // Otherwise, if we're already in a valid range for this line, |
| 2439 | // just keep going (shouldn't really happen)... |
| 2440 | } else if (context->last_pc_valid) { // and the line number is new |
| 2441 | // Add everything from the last entry up until here to the set |
| 2442 | for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) { |
| 2443 | gSingleStepControl.dex_pcs.insert(dex_pc); |
| 2444 | } |
| 2445 | context->last_pc_valid = false; |
| 2446 | } |
| 2447 | return false; // There may be multiple entries for any given line. |
| 2448 | } |
| 2449 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2450 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2451 | // annotalysis. |
| 2452 | ~DebugCallbackContext() NO_THREAD_SAFETY_ANALYSIS { |
| 2453 | Locks::breakpoint_lock_->AssertHeld(Thread::Current()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2454 | // If the line number was the last in the position table... |
| 2455 | if (last_pc_valid) { |
| 2456 | size_t end = MethodHelper(gSingleStepControl.method).GetCodeItem()->insns_size_in_code_units_; |
| 2457 | for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) { |
| 2458 | gSingleStepControl.dex_pcs.insert(dex_pc); |
| 2459 | } |
| 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | bool last_pc_valid; |
| 2464 | uint32_t last_pc; |
| 2465 | }; |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 2466 | gSingleStepControl.dex_pcs.clear(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2467 | const mirror::AbstractMethod* m = gSingleStepControl.method; |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 2468 | if (m->IsNative()) { |
| 2469 | gSingleStepControl.line_number = -1; |
| 2470 | } else { |
| 2471 | DebugCallbackContext context; |
| 2472 | MethodHelper mh(m); |
| 2473 | mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(), |
| 2474 | DebugCallbackContext::Callback, NULL, &context); |
| 2475 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2476 | |
| 2477 | // |
| 2478 | // Everything else... |
| 2479 | // |
| 2480 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2481 | gSingleStepControl.thread = thread; |
| 2482 | gSingleStepControl.step_size = step_size; |
| 2483 | gSingleStepControl.step_depth = step_depth; |
| 2484 | gSingleStepControl.is_active = true; |
| 2485 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2486 | if (VLOG_IS_ON(jdwp)) { |
| 2487 | VLOG(jdwp) << "Single-step thread: " << *gSingleStepControl.thread; |
| 2488 | VLOG(jdwp) << "Single-step step size: " << gSingleStepControl.step_size; |
| 2489 | VLOG(jdwp) << "Single-step step depth: " << gSingleStepControl.step_depth; |
| 2490 | VLOG(jdwp) << "Single-step current method: " << PrettyMethod(gSingleStepControl.method); |
| 2491 | VLOG(jdwp) << "Single-step current line: " << gSingleStepControl.line_number; |
| 2492 | VLOG(jdwp) << "Single-step current stack depth: " << gSingleStepControl.stack_depth; |
| 2493 | VLOG(jdwp) << "Single-step dex_pc values:"; |
| 2494 | for (std::set<uint32_t>::iterator it = gSingleStepControl.dex_pcs.begin() ; it != gSingleStepControl.dex_pcs.end(); ++it) { |
Elliott Hughes | 229feb7 | 2012-02-23 13:33:29 -0800 | [diff] [blame] | 2495 | VLOG(jdwp) << StringPrintf(" %#x", *it); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2500 | } |
| 2501 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2502 | void Dbg::UnconfigureStep(JDWP::ObjectId /*thread_id*/) { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2503 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 2504 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2505 | gSingleStepControl.is_active = false; |
| 2506 | gSingleStepControl.thread = NULL; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2507 | gSingleStepControl.dex_pcs.clear(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2510 | static char JdwpTagToShortyChar(JDWP::JdwpTag tag) { |
| 2511 | switch (tag) { |
| 2512 | default: |
| 2513 | LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag); |
| 2514 | |
| 2515 | // Primitives. |
| 2516 | case JDWP::JT_BYTE: return 'B'; |
| 2517 | case JDWP::JT_CHAR: return 'C'; |
| 2518 | case JDWP::JT_FLOAT: return 'F'; |
| 2519 | case JDWP::JT_DOUBLE: return 'D'; |
| 2520 | case JDWP::JT_INT: return 'I'; |
| 2521 | case JDWP::JT_LONG: return 'J'; |
| 2522 | case JDWP::JT_SHORT: return 'S'; |
| 2523 | case JDWP::JT_VOID: return 'V'; |
| 2524 | case JDWP::JT_BOOLEAN: return 'Z'; |
| 2525 | |
| 2526 | // Reference types. |
| 2527 | case JDWP::JT_ARRAY: |
| 2528 | case JDWP::JT_OBJECT: |
| 2529 | case JDWP::JT_STRING: |
| 2530 | case JDWP::JT_THREAD: |
| 2531 | case JDWP::JT_THREAD_GROUP: |
| 2532 | case JDWP::JT_CLASS_LOADER: |
| 2533 | case JDWP::JT_CLASS_OBJECT: |
| 2534 | return 'L'; |
| 2535 | } |
| 2536 | } |
| 2537 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 2538 | JDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id, |
| 2539 | JDWP::RefTypeId class_id, JDWP::MethodId method_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2540 | uint32_t arg_count, uint64_t* arg_values, |
| 2541 | JDWP::JdwpTag* arg_types, uint32_t options, |
| 2542 | JDWP::JdwpTag* pResultTag, uint64_t* pResultValue, |
| 2543 | JDWP::ObjectId* pExceptionId) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2544 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 2545 | |
| 2546 | Thread* targetThread = NULL; |
| 2547 | DebugInvokeReq* req = NULL; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2548 | Thread* self = Thread::Current(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2549 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2550 | ScopedObjectAccessUnchecked soa(self); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2551 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2552 | JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread); |
| 2553 | if (error != JDWP::ERR_NONE) { |
| 2554 | LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id; |
| 2555 | return error; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2556 | } |
| 2557 | req = targetThread->GetInvokeReq(); |
| 2558 | if (!req->ready) { |
| 2559 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 2560 | return JDWP::ERR_INVALID_THREAD; |
| 2561 | } |
| 2562 | |
| 2563 | /* |
| 2564 | * We currently have a bug where we don't successfully resume the |
| 2565 | * target thread if the suspend count is too deep. We're expected to |
| 2566 | * require one "resume" for each "suspend", but when asked to execute |
| 2567 | * a method we have to resume fully and then re-suspend it back to the |
| 2568 | * same level. (The easiest way to cause this is to type "suspend" |
| 2569 | * multiple times in jdb.) |
| 2570 | * |
| 2571 | * It's unclear what this means when the event specifies "resume all" |
| 2572 | * and some threads are suspended more deeply than others. This is |
| 2573 | * a rare problem, so for now we just prevent it from hanging forever |
| 2574 | * by rejecting the method invocation request. Without this, we will |
| 2575 | * be stuck waiting on a suspended thread. |
| 2576 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2577 | int suspend_count; |
| 2578 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2579 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2580 | suspend_count = targetThread->GetSuspendCount(); |
| 2581 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2582 | if (suspend_count > 1) { |
| 2583 | LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count; |
| 2584 | return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here. |
| 2585 | } |
| 2586 | |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2587 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2588 | mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2589 | if (receiver == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2590 | return JDWP::ERR_INVALID_OBJECT; |
| 2591 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2592 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2593 | mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2594 | if (thread == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2595 | return JDWP::ERR_INVALID_OBJECT; |
| 2596 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2597 | // TODO: check that 'thread' is actually a java.lang.Thread! |
| 2598 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2599 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2600 | if (c == NULL) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2601 | return status; |
| 2602 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2603 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2604 | mirror::AbstractMethod* m = FromMethodId(method_id); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2605 | if (m->IsStatic() != (receiver == NULL)) { |
| 2606 | return JDWP::ERR_INVALID_METHODID; |
| 2607 | } |
| 2608 | if (m->IsStatic()) { |
| 2609 | if (m->GetDeclaringClass() != c) { |
| 2610 | return JDWP::ERR_INVALID_METHODID; |
| 2611 | } |
| 2612 | } else { |
| 2613 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
| 2614 | return JDWP::ERR_INVALID_METHODID; |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | // Check the argument list matches the method. |
| 2619 | MethodHelper mh(m); |
| 2620 | if (mh.GetShortyLength() - 1 != arg_count) { |
| 2621 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 2622 | } |
| 2623 | const char* shorty = mh.GetShorty(); |
| 2624 | for (size_t i = 0; i < arg_count; ++i) { |
| 2625 | if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) { |
| 2626 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | req->receiver_ = receiver; |
| 2631 | req->thread_ = thread; |
| 2632 | req->class_ = c; |
| 2633 | req->method_ = m; |
| 2634 | req->arg_count_ = arg_count; |
| 2635 | req->arg_values_ = arg_values; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2636 | req->options_ = options; |
| 2637 | req->invoke_needed_ = true; |
| 2638 | } |
| 2639 | |
| 2640 | // The fact that we've released the thread list lock is a bit risky --- if the thread goes |
| 2641 | // away we're sitting high and dry -- but we must release this before the ResumeAllThreads |
| 2642 | // call, and it's unwise to hold it during WaitForSuspend. |
| 2643 | |
| 2644 | { |
| 2645 | /* |
| 2646 | * We change our (JDWP thread) status, which should be THREAD_RUNNING, |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 2647 | * so we can suspend for a GC if the invoke request causes us to |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2648 | * run out of memory. It's also a good idea to change it before locking |
| 2649 | * the invokeReq mutex, although that should never be held for long. |
| 2650 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2651 | self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2652 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2653 | VLOG(jdwp) << " Transferring control to event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2654 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2655 | MutexLock mu(self, req->lock_); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2656 | |
| 2657 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2658 | VLOG(jdwp) << " Resuming all threads"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2659 | thread_list->UndoDebuggerSuspensions(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2660 | } else { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2661 | VLOG(jdwp) << " Resuming event thread only"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2662 | thread_list->Resume(targetThread, true); |
| 2663 | } |
| 2664 | |
| 2665 | // Wait for the request to finish executing. |
| 2666 | while (req->invoke_needed_) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 2667 | req->cond_.Wait(self); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2668 | } |
| 2669 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2670 | VLOG(jdwp) << " Control has returned from event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2671 | |
| 2672 | /* wait for thread to re-suspend itself */ |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2673 | SuspendThread(thread_id, false /* request_suspension */ ); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2674 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | /* |
| 2678 | * Suspend the threads. We waited for the target thread to suspend |
| 2679 | * itself, so all we need to do is suspend the others. |
| 2680 | * |
| 2681 | * The suspendAllThreads() call will double-suspend the event thread, |
| 2682 | * so we want to resume the target thread once to keep the books straight. |
| 2683 | */ |
| 2684 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2685 | self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2686 | VLOG(jdwp) << " Suspending all threads"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2687 | thread_list->SuspendAllForDebugger(); |
| 2688 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2689 | VLOG(jdwp) << " Resuming event thread to balance the count"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2690 | thread_list->Resume(targetThread, true); |
| 2691 | } |
| 2692 | |
| 2693 | // Copy the result. |
| 2694 | *pResultTag = req->result_tag; |
| 2695 | if (IsPrimitiveTag(req->result_tag)) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2696 | *pResultValue = req->result_value.GetJ(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2697 | } else { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2698 | *pResultValue = gRegistry->Add(req->result_value.GetL()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2699 | } |
| 2700 | *pExceptionId = req->exception; |
| 2701 | return req->error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2705 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2706 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 2707 | // We can be called while an exception is pending. We need |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2708 | // to preserve that across the method invocation. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2709 | SirtRef<mirror::Throwable> old_exception(soa.Self(), soa.Self()->GetException()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2710 | soa.Self()->ClearException(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2711 | |
| 2712 | // Translate the method through the vtable, unless the debugger wants to suppress it. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2713 | mirror::AbstractMethod* m = pReq->method_; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2714 | if ((pReq->options_ & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver_ != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2715 | mirror::AbstractMethod* actual_method = pReq->class_->FindVirtualMethodForVirtualOrInterface(pReq->method_); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2716 | if (actual_method != m) { |
| 2717 | VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) << " to " << PrettyMethod(actual_method); |
| 2718 | m = actual_method; |
| 2719 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2720 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2721 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2722 | CHECK(m != NULL); |
| 2723 | |
| 2724 | CHECK_EQ(sizeof(jvalue), sizeof(uint64_t)); |
| 2725 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2726 | LOG(INFO) << "self=" << soa.Self() << " pReq->receiver_=" << pReq->receiver_ << " m=" << m |
| 2727 | << " #" << pReq->arg_count_ << " " << pReq->arg_values_; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 2728 | |
| 2729 | MethodHelper mh(m); |
| 2730 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
| 2731 | arg_array.BuildArgArray(soa, pReq->receiver_, reinterpret_cast<jvalue*>(pReq->arg_values_)); |
| 2732 | JValue unused_result; |
| 2733 | if (mh.IsReturnFloatOrDouble()) { |
| 2734 | InvokeWithArgArray(soa, m, &arg_array, &unused_result, &pReq->result_value); |
| 2735 | } else { |
| 2736 | InvokeWithArgArray(soa, m, &arg_array, &pReq->result_value, &unused_result); |
| 2737 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2738 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2739 | pReq->exception = gRegistry->Add(soa.Self()->GetException()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2740 | pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty()); |
| 2741 | if (pReq->exception != 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2742 | mirror::Object* exc = soa.Self()->GetException(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2743 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exc << " " << PrettyTypeOf(exc); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2744 | soa.Self()->ClearException(); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2745 | pReq->result_value.SetJ(0); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2746 | } else if (pReq->result_tag == JDWP::JT_OBJECT) { |
| 2747 | /* if no exception thrown, examine object result more closely */ |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2748 | JDWP::JdwpTag new_tag = TagFromObject(pReq->result_value.GetL()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2749 | if (new_tag != pReq->result_tag) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2750 | VLOG(jdwp) << " JDWP promoted result from " << pReq->result_tag << " to " << new_tag; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2751 | pReq->result_tag = new_tag; |
| 2752 | } |
| 2753 | |
| 2754 | /* |
| 2755 | * Register the object. We don't actually need an ObjectId yet, |
| 2756 | * but we do need to be sure that the GC won't move or discard the |
| 2757 | * object when we switch out of RUNNING. The ObjectId conversion |
| 2758 | * will add the object to the "do not touch" list. |
| 2759 | * |
| 2760 | * We can't use the "tracked allocation" mechanism here because |
| 2761 | * the object is going to be handed off to a different thread. |
| 2762 | */ |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2763 | gRegistry->Add(pReq->result_value.GetL()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | if (old_exception.get() != NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2767 | soa.Self()->SetException(old_exception.get()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2768 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2771 | /* |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 2772 | * "request" contains a full JDWP packet, possibly with multiple chunks. We |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2773 | * need to process each, accumulate the replies, and ship the whole thing |
| 2774 | * back. |
| 2775 | * |
| 2776 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 2777 | * and includes the chunk type/length, followed by the data. |
| 2778 | * |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2779 | * OLD-TODO: we currently assume that the request and reply include a single |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2780 | * chunk. If this becomes inconvenient we will need to adapt. |
| 2781 | */ |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 2782 | bool Dbg::DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2783 | Thread* self = Thread::Current(); |
| 2784 | JNIEnv* env = self->GetJniEnv(); |
| 2785 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 2786 | uint32_t type = request.ReadUnsigned32("type"); |
| 2787 | uint32_t length = request.ReadUnsigned32("length"); |
| 2788 | |
| 2789 | // Create a byte[] corresponding to 'request'. |
| 2790 | size_t request_length = request.size(); |
| 2791 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length)); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2792 | if (dataArray.get() == NULL) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 2793 | LOG(WARNING) << "byte[] allocation failed: " << request_length; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2794 | env->ExceptionClear(); |
| 2795 | return false; |
| 2796 | } |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 2797 | env->SetByteArrayRegion(dataArray.get(), 0, request_length, reinterpret_cast<const jbyte*>(request.data())); |
| 2798 | request.Skip(request_length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2799 | |
| 2800 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2801 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 2802 | if (length != request_length) { |
| 2803 | LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, request_length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2804 | return false; |
| 2805 | } |
| 2806 | |
| 2807 | // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)". |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 2808 | ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 2809 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch, |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 2810 | type, dataArray.get(), 0, length)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2811 | if (env->ExceptionCheck()) { |
| 2812 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 2813 | env->ExceptionDescribe(); |
| 2814 | env->ExceptionClear(); |
| 2815 | return false; |
| 2816 | } |
| 2817 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2818 | if (chunk.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2819 | return false; |
| 2820 | } |
| 2821 | |
| 2822 | /* |
| 2823 | * Pull the pieces out of the chunk. We copy the results into a |
| 2824 | * newly-allocated buffer that the caller can free. We don't want to |
| 2825 | * continue using the Chunk object because nothing has a reference to it. |
| 2826 | * |
| 2827 | * We could avoid this by returning type/data/offset/length and having |
| 2828 | * the caller be aware of the object lifetime issues, but that |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 2829 | * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2830 | * if we have responses for multiple chunks. |
| 2831 | * |
| 2832 | * So we're pretty much stuck with copying data around multiple times. |
| 2833 | */ |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 2834 | ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data))); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 2835 | jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 2836 | length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 2837 | type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2838 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2839 | VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2840 | if (length == 0 || replyData.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2841 | return false; |
| 2842 | } |
| 2843 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 2844 | const int kChunkHdrLen = 8; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2845 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
| 2846 | if (reply == NULL) { |
| 2847 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 2848 | return false; |
| 2849 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 2850 | JDWP::Set4BE(reply + 0, type); |
| 2851 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2852 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2853 | |
| 2854 | *pReplyBuf = reply; |
| 2855 | *pReplyLen = length + kChunkHdrLen; |
| 2856 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 2857 | VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2858 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2859 | } |
| 2860 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2861 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2862 | VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2863 | |
| 2864 | Thread* self = Thread::Current(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2865 | if (self->GetState() != kRunnable) { |
| 2866 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 2867 | /* try anyway? */ |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2871 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 2872 | env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 2873 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast, |
| 2874 | event); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2875 | if (env->ExceptionCheck()) { |
| 2876 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 2877 | env->ExceptionDescribe(); |
| 2878 | env->ExceptionClear(); |
| 2879 | } |
| 2880 | } |
| 2881 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2882 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2883 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2884 | } |
| 2885 | |
| 2886 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2887 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2888 | gDdmThreadNotification = false; |
| 2889 | } |
| 2890 | |
| 2891 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2892 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2893 | * |
| 2894 | * Because we broadcast the full set of threads when the notifications are |
| 2895 | * first enabled, it's possible for "thread" to be actively executing. |
| 2896 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2897 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2898 | if (!gDdmThreadNotification) { |
| 2899 | return; |
| 2900 | } |
| 2901 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2902 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2903 | uint8_t buf[4]; |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 2904 | JDWP::Set4BE(&buf[0], t->GetThinLockId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2905 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2906 | } else { |
| 2907 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2908 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2909 | SirtRef<mirror::String> name(soa.Self(), t->GetThreadName(soa)); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2910 | size_t char_count = (name.get() != NULL) ? name->GetLength() : 0; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 2911 | const jchar* chars = (name.get() != NULL) ? name->GetCharArray()->GetData() : NULL; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2912 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2913 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2914 | JDWP::Append4BE(bytes, t->GetThinLockId()); |
| 2915 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2916 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 2917 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2918 | } |
| 2919 | } |
| 2920 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2921 | void Dbg::DdmSetThreadNotification(bool enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2922 | // Enable/disable thread notifications. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2923 | gDdmThreadNotification = enable; |
| 2924 | if (enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2925 | // Suspend the VM then post thread start notifications for all threads. Threads attaching will |
| 2926 | // see a suspension in progress and block until that ends. They then post their own start |
| 2927 | // notification. |
| 2928 | SuspendVM(); |
| 2929 | std::list<Thread*> threads; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2930 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2931 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2932 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2933 | threads = Runtime::Current()->GetThreadList()->GetList(); |
| 2934 | } |
| 2935 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2936 | ScopedObjectAccess soa(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2937 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 2938 | for (It it = threads.begin(), end = threads.end(); it != end; ++it) { |
| 2939 | Dbg::DdmSendThreadNotification(*it, CHUNK_TYPE("THCR")); |
| 2940 | } |
| 2941 | } |
| 2942 | ResumeVM(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2946 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2947 | if (IsDebuggerActive()) { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 2948 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 2949 | JDWP::ObjectId id = gRegistry->Add(t->GetPeer()); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2950 | gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2951 | // If this thread's just joined the party while we're already debugging, make sure it knows |
| 2952 | // to give us updates when it's running. |
| 2953 | t->SetDebuggerUpdatesEnabled(true); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2954 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2955 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2959 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2963 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2964 | } |
| 2965 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2966 | void Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 2967 | CHECK(buf != NULL); |
| 2968 | iovec vec[1]; |
| 2969 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 2970 | vec[0].iov_len = byte_count; |
| 2971 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2972 | } |
| 2973 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2974 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 2975 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 2976 | } |
| 2977 | |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 2978 | void Dbg::DdmSendChunkV(uint32_t type, const struct iovec* iov, int iov_count) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 2979 | if (gJdwpState == NULL) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2980 | VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 2981 | } else { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 2982 | gJdwpState->DdmSendChunkV(type, iov, iov_count); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 2983 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2984 | } |
| 2985 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 2986 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 2987 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 2988 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 2989 | return true; |
| 2990 | } |
| 2991 | |
| 2992 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 2993 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 2994 | return false; |
| 2995 | } |
| 2996 | |
| 2997 | gDdmHpifWhen = when; |
| 2998 | return true; |
| 2999 | } |
| 3000 | |
| 3001 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 3002 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 3003 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 3004 | return false; |
| 3005 | } |
| 3006 | |
| 3007 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 3008 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 3009 | return false; |
| 3010 | } |
| 3011 | |
| 3012 | if (native) { |
| 3013 | gDdmNhsgWhen = when; |
| 3014 | gDdmNhsgWhat = what; |
| 3015 | } else { |
| 3016 | gDdmHpsgWhen = when; |
| 3017 | gDdmHpsgWhat = what; |
| 3018 | } |
| 3019 | return true; |
| 3020 | } |
| 3021 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 3022 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 3023 | // If there's a one-shot 'when', reset it. |
| 3024 | if (reason == gDdmHpifWhen) { |
| 3025 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 3026 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 3027 | } |
| 3028 | } |
| 3029 | |
| 3030 | /* |
| 3031 | * Chunk HPIF (client --> server) |
| 3032 | * |
| 3033 | * Heap Info. General information about the heap, |
| 3034 | * suitable for a summary display. |
| 3035 | * |
| 3036 | * [u4]: number of heaps |
| 3037 | * |
| 3038 | * For each heap: |
| 3039 | * [u4]: heap ID |
| 3040 | * [u8]: timestamp in ms since Unix epoch |
| 3041 | * [u1]: capture reason (same as 'when' value from server) |
| 3042 | * [u4]: max heap size in bytes (-Xmx) |
| 3043 | * [u4]: current heap size in bytes |
| 3044 | * [u4]: current number of bytes allocated |
| 3045 | * [u4]: current number of objects allocated |
| 3046 | */ |
| 3047 | uint8_t heap_count = 1; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 3048 | Heap* heap = Runtime::Current()->GetHeap(); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 3049 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3050 | JDWP::Append4BE(bytes, heap_count); |
| 3051 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
| 3052 | JDWP::Append8BE(bytes, MilliTime()); |
| 3053 | JDWP::Append1BE(bytes, reason); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 3054 | JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes. |
| 3055 | JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes. |
| 3056 | JDWP::Append4BE(bytes, heap->GetBytesAllocated()); |
| 3057 | JDWP::Append4BE(bytes, heap->GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 3058 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 3059 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 3060 | } |
| 3061 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3062 | enum HpsgSolidity { |
| 3063 | SOLIDITY_FREE = 0, |
| 3064 | SOLIDITY_HARD = 1, |
| 3065 | SOLIDITY_SOFT = 2, |
| 3066 | SOLIDITY_WEAK = 3, |
| 3067 | SOLIDITY_PHANTOM = 4, |
| 3068 | SOLIDITY_FINALIZABLE = 5, |
| 3069 | SOLIDITY_SWEEP = 6, |
| 3070 | }; |
| 3071 | |
| 3072 | enum HpsgKind { |
| 3073 | KIND_OBJECT = 0, |
| 3074 | KIND_CLASS_OBJECT = 1, |
| 3075 | KIND_ARRAY_1 = 2, |
| 3076 | KIND_ARRAY_2 = 3, |
| 3077 | KIND_ARRAY_4 = 4, |
| 3078 | KIND_ARRAY_8 = 5, |
| 3079 | KIND_UNKNOWN = 6, |
| 3080 | KIND_NATIVE = 7, |
| 3081 | }; |
| 3082 | |
| 3083 | #define HPSG_PARTIAL (1<<7) |
| 3084 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 3085 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3086 | class HeapChunkContext { |
| 3087 | public: |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3088 | // Maximum chunk size. Obtain this from the formula: |
| 3089 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 3090 | HeapChunkContext(bool merge, bool native) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3091 | : buf_(16384 - 16), |
| 3092 | type_(0), |
| 3093 | merge_(merge) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3094 | Reset(); |
| 3095 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3096 | type_ = CHUNK_TYPE("NHSG"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3097 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3098 | type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | ~HeapChunkContext() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3103 | if (p_ > &buf_[0]) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3104 | Flush(); |
| 3105 | } |
| 3106 | } |
| 3107 | |
| 3108 | void EnsureHeader(const void* chunk_ptr) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3109 | if (!needHeader_) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3110 | return; |
| 3111 | } |
| 3112 | |
| 3113 | // Start a new HPSx chunk. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3114 | JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap). |
| 3115 | JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3116 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3117 | JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 3118 | JDWP::Write4BE(&p_, 0); // offset of this piece (relative to the virtual address). |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3119 | // [u4]: length of piece, in allocation units |
| 3120 | // We won't know this until we're done, so save the offset and stuff in a dummy value. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3121 | pieceLenField_ = p_; |
| 3122 | JDWP::Write4BE(&p_, 0x55555555); |
| 3123 | needHeader_ = false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3124 | } |
| 3125 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3126 | void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | d636b06 | 2013-01-18 17:51:18 -0800 | [diff] [blame] | 3127 | if (pieceLenField_ == NULL) { |
| 3128 | // Flush immediately post Reset (maybe back-to-back Flush). Ignore. |
| 3129 | CHECK(needHeader_); |
| 3130 | return; |
| 3131 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3132 | // Patch the "length of piece" field. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3133 | CHECK_LE(&buf_[0], pieceLenField_); |
| 3134 | CHECK_LE(pieceLenField_, p_); |
| 3135 | JDWP::Set4BE(pieceLenField_, totalAllocationUnits_); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3136 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3137 | Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3138 | Reset(); |
| 3139 | } |
| 3140 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3141 | static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3142 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 3143 | Locks::mutator_lock_) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3144 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3145 | } |
| 3146 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3147 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3148 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 3149 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3150 | void Reset() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3151 | p_ = &buf_[0]; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3152 | startOfNextMemoryChunk_ = NULL; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3153 | totalAllocationUnits_ = 0; |
| 3154 | needHeader_ = true; |
| 3155 | pieceLenField_ = NULL; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3156 | } |
| 3157 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3158 | void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3159 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 3160 | Locks::mutator_lock_) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3161 | // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken |
| 3162 | // in the following code not to allocate memory, by ensuring buf_ is of the correct size |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3163 | if (used_bytes == 0) { |
| 3164 | if (start == NULL) { |
| 3165 | // Reset for start of new heap. |
| 3166 | startOfNextMemoryChunk_ = NULL; |
| 3167 | Flush(); |
| 3168 | } |
| 3169 | // Only process in use memory so that free region information |
| 3170 | // also includes dlmalloc book keeping. |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3171 | return; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3172 | } |
| 3173 | |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3174 | /* If we're looking at the native heap, we'll just return |
| 3175 | * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks |
| 3176 | */ |
| 3177 | bool native = type_ == CHUNK_TYPE("NHSG"); |
| 3178 | |
| 3179 | if (startOfNextMemoryChunk_ != NULL) { |
| 3180 | // Transmit any pending free memory. Native free memory of |
| 3181 | // over kMaxFreeLen could be because of the use of mmaps, so |
| 3182 | // don't report. If not free memory then start a new segment. |
| 3183 | bool flush = true; |
| 3184 | if (start > startOfNextMemoryChunk_) { |
| 3185 | const size_t kMaxFreeLen = 2 * kPageSize; |
| 3186 | void* freeStart = startOfNextMemoryChunk_; |
| 3187 | void* freeEnd = start; |
| 3188 | size_t freeLen = (char*)freeEnd - (char*)freeStart; |
| 3189 | if (!native || freeLen < kMaxFreeLen) { |
| 3190 | AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), freeStart, freeLen); |
| 3191 | flush = false; |
| 3192 | } |
| 3193 | } |
| 3194 | if (flush) { |
| 3195 | startOfNextMemoryChunk_ = NULL; |
| 3196 | Flush(); |
| 3197 | } |
| 3198 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3199 | const mirror::Object* obj = reinterpret_cast<const mirror::Object*>(start); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3200 | |
| 3201 | // Determine the type of this chunk. |
| 3202 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 3203 | // If it's the same, we should combine them. |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3204 | uint8_t state = ExamineObject(obj, native); |
| 3205 | // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an |
| 3206 | // allocation then the first sizeof(size_t) may belong to it. |
| 3207 | const size_t dlMallocOverhead = sizeof(size_t); |
| 3208 | AppendChunk(state, start, used_bytes + dlMallocOverhead); |
| 3209 | startOfNextMemoryChunk_ = (char*)start + used_bytes + dlMallocOverhead; |
| 3210 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3211 | |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3212 | void AppendChunk(uint8_t state, void* ptr, size_t length) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3213 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3214 | // Make sure there's enough room left in the buffer. |
| 3215 | // We need to use two bytes for every fractional 256 allocation units used by the chunk plus |
| 3216 | // 17 bytes for any header. |
| 3217 | size_t needed = (((length/ALLOCATION_UNIT_SIZE + 255) / 256) * 2) + 17; |
| 3218 | size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]); |
| 3219 | if (bytesLeft < needed) { |
| 3220 | Flush(); |
| 3221 | } |
| 3222 | |
| 3223 | bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]); |
| 3224 | if (bytesLeft < needed) { |
| 3225 | LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", " |
| 3226 | << needed << " bytes)"; |
| 3227 | return; |
| 3228 | } |
| 3229 | EnsureHeader(ptr); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3230 | // Write out the chunk description. |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3231 | length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units. |
| 3232 | totalAllocationUnits_ += length; |
| 3233 | while (length > 256) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3234 | *p_++ = state | HPSG_PARTIAL; |
| 3235 | *p_++ = 255; // length - 1 |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3236 | length -= 256; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3237 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3238 | *p_++ = state; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3239 | *p_++ = length - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3240 | } |
| 3241 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3242 | uint8_t ExamineObject(const mirror::Object* o, bool is_native_heap) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3243 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3244 | if (o == NULL) { |
| 3245 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 3246 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3247 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3248 | // It's an allocated chunk. Figure out what it is. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3249 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3250 | // If we're looking at the native heap, we'll just return |
| 3251 | // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3252 | if (is_native_heap) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3253 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 3254 | } |
| 3255 | |
Ian Rogers | 5bfa60f | 2012-09-02 21:17:56 -0700 | [diff] [blame] | 3256 | if (!Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3257 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3258 | } |
| 3259 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3260 | mirror::Class* c = o->GetClass(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3261 | if (c == NULL) { |
| 3262 | // The object was probably just created but hasn't been initialized yet. |
| 3263 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 3264 | } |
| 3265 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 3266 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(c)) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3267 | LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3268 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 3269 | } |
| 3270 | |
| 3271 | if (c->IsClassClass()) { |
| 3272 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 3273 | } |
| 3274 | |
| 3275 | if (c->IsArrayClass()) { |
| 3276 | if (o->IsObjectArray()) { |
| 3277 | return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 3278 | } |
| 3279 | switch (c->GetComponentSize()) { |
| 3280 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 3281 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 3282 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 3283 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 3284 | } |
| 3285 | } |
| 3286 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3287 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 3288 | } |
| 3289 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3290 | std::vector<uint8_t> buf_; |
| 3291 | uint8_t* p_; |
| 3292 | uint8_t* pieceLenField_; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 3293 | void* startOfNextMemoryChunk_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3294 | size_t totalAllocationUnits_; |
| 3295 | uint32_t type_; |
| 3296 | bool merge_; |
| 3297 | bool needHeader_; |
| 3298 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3299 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 3300 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3301 | |
| 3302 | void Dbg::DdmSendHeapSegments(bool native) { |
| 3303 | Dbg::HpsgWhen when; |
| 3304 | Dbg::HpsgWhat what; |
| 3305 | if (!native) { |
| 3306 | when = gDdmHpsgWhen; |
| 3307 | what = gDdmHpsgWhat; |
| 3308 | } else { |
| 3309 | when = gDdmNhsgWhen; |
| 3310 | what = gDdmNhsgWhat; |
| 3311 | } |
| 3312 | if (when == HPSG_WHEN_NEVER) { |
| 3313 | return; |
| 3314 | } |
| 3315 | |
| 3316 | // Figure out what kind of chunks we'll be sending. |
| 3317 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what); |
| 3318 | |
| 3319 | // First, send a heap start chunk. |
| 3320 | uint8_t heap_id[4]; |
| 3321 | JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap). |
| 3322 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id); |
| 3323 | |
| 3324 | // Send a series of heap segment chunks. |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3325 | HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native); |
| 3326 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3327 | // TODO: enable when bionic has moved to dlmalloc 2.8.5 |
| 3328 | // dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context); |
| 3329 | UNIMPLEMENTED(WARNING) << "Native heap send heap segments"; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3330 | } else { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 3331 | Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3332 | const Spaces& spaces = heap->GetSpaces(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3333 | Thread* self = Thread::Current(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 3334 | for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 3335 | if ((*cur)->IsAllocSpace()) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3336 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 3337 | (*cur)->AsAllocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context); |
| 3338 | } |
| 3339 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 3340 | // Walk the large objects, these are not in the AllocSpace. |
| 3341 | heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 3342 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3343 | |
| 3344 | // Finally, send a heap end chunk. |
| 3345 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 3346 | } |
| 3347 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3348 | void Dbg::SetAllocTrackingEnabled(bool enabled) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3349 | MutexLock mu(Thread::Current(), gAllocTrackerLock); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3350 | if (enabled) { |
| 3351 | if (recent_allocation_records_ == NULL) { |
| 3352 | LOG(INFO) << "Enabling alloc tracker (" << kNumAllocRecords << " entries, " |
| 3353 | << kMaxAllocRecordStackDepth << " frames --> " |
| 3354 | << (sizeof(AllocRecord) * kNumAllocRecords) << " bytes)"; |
| 3355 | gAllocRecordHead = gAllocRecordCount = 0; |
| 3356 | recent_allocation_records_ = new AllocRecord[kNumAllocRecords]; |
| 3357 | CHECK(recent_allocation_records_ != NULL); |
| 3358 | } |
| 3359 | } else { |
| 3360 | delete[] recent_allocation_records_; |
| 3361 | recent_allocation_records_ = NULL; |
| 3362 | } |
| 3363 | } |
| 3364 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3365 | struct AllocRecordStackVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 3366 | AllocRecordStackVisitor(Thread* thread, AllocRecord* record) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3367 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 3368 | : StackVisitor(thread, NULL), record(record), depth(0) {} |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3369 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3370 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 3371 | // annotalysis. |
| 3372 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3373 | if (depth >= kMaxAllocRecordStackDepth) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 3374 | return false; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3375 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3376 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3377 | if (!m->IsRuntimeMethod()) { |
| 3378 | record->stack[depth].method = m; |
| 3379 | record->stack[depth].dex_pc = GetDexPc(); |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 3380 | ++depth; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3381 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 3382 | return true; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3383 | } |
| 3384 | |
| 3385 | ~AllocRecordStackVisitor() { |
| 3386 | // Clear out any unused stack trace elements. |
| 3387 | for (; depth < kMaxAllocRecordStackDepth; ++depth) { |
| 3388 | record->stack[depth].method = NULL; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3389 | record->stack[depth].dex_pc = 0; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3390 | } |
| 3391 | } |
| 3392 | |
| 3393 | AllocRecord* record; |
| 3394 | size_t depth; |
| 3395 | }; |
| 3396 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3397 | void Dbg::RecordAllocation(mirror::Class* type, size_t byte_count) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3398 | Thread* self = Thread::Current(); |
| 3399 | CHECK(self != NULL); |
| 3400 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3401 | MutexLock mu(self, gAllocTrackerLock); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3402 | if (recent_allocation_records_ == NULL) { |
| 3403 | return; |
| 3404 | } |
| 3405 | |
| 3406 | // Advance and clip. |
| 3407 | if (++gAllocRecordHead == kNumAllocRecords) { |
| 3408 | gAllocRecordHead = 0; |
| 3409 | } |
| 3410 | |
| 3411 | // Fill in the basics. |
| 3412 | AllocRecord* record = &recent_allocation_records_[gAllocRecordHead]; |
| 3413 | record->type = type; |
| 3414 | record->byte_count = byte_count; |
| 3415 | record->thin_lock_id = self->GetThinLockId(); |
| 3416 | |
| 3417 | // Fill in the stack trace. |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 3418 | AllocRecordStackVisitor visitor(self, record); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3419 | visitor.WalkStack(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3420 | |
| 3421 | if (gAllocRecordCount < kNumAllocRecords) { |
| 3422 | ++gAllocRecordCount; |
| 3423 | } |
| 3424 | } |
| 3425 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3426 | // Returns the index of the head element. |
| 3427 | // |
| 3428 | // We point at the most-recently-written record, so if gAllocRecordCount is 1 |
| 3429 | // we want to use the current element. Take "head+1" and subtract count |
| 3430 | // from it. |
| 3431 | // |
| 3432 | // We need to handle underflow in our circular buffer, so we add |
| 3433 | // kNumAllocRecords and then mask it back down. |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 3434 | static inline int HeadIndex() EXCLUSIVE_LOCKS_REQUIRED(gAllocTrackerLock) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3435 | return (gAllocRecordHead+1 + kNumAllocRecords - gAllocRecordCount) & (kNumAllocRecords-1); |
| 3436 | } |
| 3437 | |
| 3438 | void Dbg::DumpRecentAllocations() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3439 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3440 | MutexLock mu(soa.Self(), gAllocTrackerLock); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3441 | if (recent_allocation_records_ == NULL) { |
| 3442 | LOG(INFO) << "Not recording tracked allocations"; |
| 3443 | return; |
| 3444 | } |
| 3445 | |
| 3446 | // "i" is the head of the list. We want to start at the end of the |
| 3447 | // list and move forward to the tail. |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3448 | size_t i = HeadIndex(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3449 | size_t count = gAllocRecordCount; |
| 3450 | |
| 3451 | LOG(INFO) << "Tracked allocations, (head=" << gAllocRecordHead << " count=" << count << ")"; |
| 3452 | while (count--) { |
| 3453 | AllocRecord* record = &recent_allocation_records_[i]; |
| 3454 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3455 | LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->thin_lock_id, record->byte_count) |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3456 | << PrettyClass(record->type); |
| 3457 | |
| 3458 | for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3459 | const mirror::AbstractMethod* m = record->stack[stack_frame].method; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3460 | if (m == NULL) { |
| 3461 | break; |
| 3462 | } |
| 3463 | LOG(INFO) << " " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber(); |
| 3464 | } |
| 3465 | |
| 3466 | // pause periodically to help logcat catch up |
| 3467 | if ((count % 5) == 0) { |
| 3468 | usleep(40000); |
| 3469 | } |
| 3470 | |
| 3471 | i = (i + 1) & (kNumAllocRecords-1); |
| 3472 | } |
| 3473 | } |
| 3474 | |
| 3475 | class StringTable { |
| 3476 | public: |
| 3477 | StringTable() { |
| 3478 | } |
| 3479 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3480 | void Add(const char* s) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3481 | table_.insert(s); |
| 3482 | } |
| 3483 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3484 | size_t IndexOf(const char* s) const { |
| 3485 | typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto |
| 3486 | It it = table_.find(s); |
| 3487 | if (it == table_.end()) { |
| 3488 | LOG(FATAL) << "IndexOf(\"" << s << "\") failed"; |
| 3489 | } |
| 3490 | return std::distance(table_.begin(), it); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3491 | } |
| 3492 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3493 | size_t Size() const { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3494 | return table_.size(); |
| 3495 | } |
| 3496 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3497 | void WriteTo(std::vector<uint8_t>& bytes) const { |
| 3498 | typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3499 | for (It it = table_.begin(); it != table_.end(); ++it) { |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3500 | const char* s = (*it).c_str(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3501 | size_t s_len = CountModifiedUtf8Chars(s); |
| 3502 | UniquePtr<uint16_t> s_utf16(new uint16_t[s_len]); |
| 3503 | ConvertModifiedUtf8ToUtf16(s_utf16.get(), s); |
| 3504 | JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3505 | } |
| 3506 | } |
| 3507 | |
| 3508 | private: |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3509 | std::set<std::string> table_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3510 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 3511 | }; |
| 3512 | |
| 3513 | /* |
| 3514 | * The data we send to DDMS contains everything we have recorded. |
| 3515 | * |
| 3516 | * Message header (all values big-endian): |
| 3517 | * (1b) message header len (to allow future expansion); includes itself |
| 3518 | * (1b) entry header len |
| 3519 | * (1b) stack frame len |
| 3520 | * (2b) number of entries |
| 3521 | * (4b) offset to string table from start of message |
| 3522 | * (2b) number of class name strings |
| 3523 | * (2b) number of method name strings |
| 3524 | * (2b) number of source file name strings |
| 3525 | * For each entry: |
| 3526 | * (4b) total allocation size |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 3527 | * (2b) thread id |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3528 | * (2b) allocated object's class name index |
| 3529 | * (1b) stack depth |
| 3530 | * For each stack frame: |
| 3531 | * (2b) method's class name |
| 3532 | * (2b) method name |
| 3533 | * (2b) method source file |
| 3534 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 3535 | * (xb) class name strings |
| 3536 | * (xb) method name strings |
| 3537 | * (xb) source file strings |
| 3538 | * |
| 3539 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 3540 | * followed by UTF-16 data. |
| 3541 | * |
| 3542 | * We send up 16-bit unsigned indexes into string tables. In theory there |
| 3543 | * can be (kMaxAllocRecordStackDepth * kNumAllocRecords) unique strings in |
| 3544 | * each table, but in practice there should be far fewer. |
| 3545 | * |
| 3546 | * The chief reason for using a string table here is to keep the size of |
| 3547 | * the DDMS message to a minimum. This is partly to make the protocol |
| 3548 | * efficient, but also because we have to form the whole thing up all at |
| 3549 | * once in a memory buffer. |
| 3550 | * |
| 3551 | * We use separate string tables for class names, method names, and source |
| 3552 | * files to keep the indexes small. There will generally be no overlap |
| 3553 | * between the contents of these tables. |
| 3554 | */ |
| 3555 | jbyteArray Dbg::GetRecentAllocations() { |
| 3556 | if (false) { |
| 3557 | DumpRecentAllocations(); |
| 3558 | } |
| 3559 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3560 | Thread* self = Thread::Current(); |
| 3561 | MutexLock mu(self, gAllocTrackerLock); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3562 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3563 | // |
| 3564 | // Part 1: generate string tables. |
| 3565 | // |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3566 | StringTable class_names; |
| 3567 | StringTable method_names; |
| 3568 | StringTable filenames; |
| 3569 | |
| 3570 | int count = gAllocRecordCount; |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3571 | int idx = HeadIndex(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3572 | while (count--) { |
| 3573 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 3574 | |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 3575 | class_names.Add(ClassHelper(record->type).GetDescriptor()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3576 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3577 | MethodHelper mh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3578 | for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3579 | mirror::AbstractMethod* m = record->stack[i].method; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3580 | if (m != NULL) { |
Ian Rogers | ba37781 | 2012-05-28 21:16:29 -0700 | [diff] [blame] | 3581 | mh.ChangeMethod(m); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3582 | class_names.Add(mh.GetDeclaringClassDescriptor()); |
| 3583 | method_names.Add(mh.GetName()); |
| 3584 | filenames.Add(mh.GetDeclaringClassSourceFile()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3585 | } |
| 3586 | } |
| 3587 | |
| 3588 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 3589 | } |
| 3590 | |
| 3591 | LOG(INFO) << "allocation records: " << gAllocRecordCount; |
| 3592 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3593 | // |
| 3594 | // Part 2: allocate a buffer and generate the output. |
| 3595 | // |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3596 | std::vector<uint8_t> bytes; |
| 3597 | |
| 3598 | // (1b) message header len (to allow future expansion); includes itself |
| 3599 | // (1b) entry header len |
| 3600 | // (1b) stack frame len |
| 3601 | const int kMessageHeaderLen = 15; |
| 3602 | const int kEntryHeaderLen = 9; |
| 3603 | const int kStackFrameLen = 8; |
| 3604 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 3605 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 3606 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 3607 | |
| 3608 | // (2b) number of entries |
| 3609 | // (4b) offset to string table from start of message |
| 3610 | // (2b) number of class name strings |
| 3611 | // (2b) number of method name strings |
| 3612 | // (2b) number of source file name strings |
| 3613 | JDWP::Append2BE(bytes, gAllocRecordCount); |
| 3614 | size_t string_table_offset = bytes.size(); |
| 3615 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
| 3616 | JDWP::Append2BE(bytes, class_names.Size()); |
| 3617 | JDWP::Append2BE(bytes, method_names.Size()); |
| 3618 | JDWP::Append2BE(bytes, filenames.Size()); |
| 3619 | |
| 3620 | count = gAllocRecordCount; |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3621 | idx = HeadIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3622 | ClassHelper kh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3623 | while (count--) { |
| 3624 | // For each entry: |
| 3625 | // (4b) total allocation size |
| 3626 | // (2b) thread id |
| 3627 | // (2b) allocated object's class name index |
| 3628 | // (1b) stack depth |
| 3629 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 3630 | size_t stack_depth = record->GetDepth(); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3631 | kh.ChangeClass(record->type); |
| 3632 | size_t allocated_object_class_name_index = class_names.IndexOf(kh.GetDescriptor()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3633 | JDWP::Append4BE(bytes, record->byte_count); |
| 3634 | JDWP::Append2BE(bytes, record->thin_lock_id); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3635 | JDWP::Append2BE(bytes, allocated_object_class_name_index); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3636 | JDWP::Append1BE(bytes, stack_depth); |
| 3637 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3638 | MethodHelper mh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3639 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 3640 | // For each stack frame: |
| 3641 | // (2b) method's class name |
| 3642 | // (2b) method name |
| 3643 | // (2b) method source file |
| 3644 | // (2b) line number, clipped to 32767; -2 if native; -1 if no source |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3645 | mh.ChangeMethod(record->stack[stack_frame].method); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3646 | size_t class_name_index = class_names.IndexOf(mh.GetDeclaringClassDescriptor()); |
| 3647 | size_t method_name_index = method_names.IndexOf(mh.GetName()); |
| 3648 | size_t file_name_index = filenames.IndexOf(mh.GetDeclaringClassSourceFile()); |
| 3649 | JDWP::Append2BE(bytes, class_name_index); |
| 3650 | JDWP::Append2BE(bytes, method_name_index); |
| 3651 | JDWP::Append2BE(bytes, file_name_index); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3652 | JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber()); |
| 3653 | } |
| 3654 | |
| 3655 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 3656 | } |
| 3657 | |
| 3658 | // (xb) class name strings |
| 3659 | // (xb) method name strings |
| 3660 | // (xb) source file strings |
| 3661 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 3662 | class_names.WriteTo(bytes); |
| 3663 | method_names.WriteTo(bytes); |
| 3664 | filenames.WriteTo(bytes); |
| 3665 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3666 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3667 | jbyteArray result = env->NewByteArray(bytes.size()); |
| 3668 | if (result != NULL) { |
| 3669 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 3670 | } |
| 3671 | return result; |
| 3672 | } |
| 3673 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3674 | } // namespace art |