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 | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 25 | #include "dex_instruction.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "gc/card_table-inl.h" |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 27 | #include "gc/large_object_space.h" |
| 28 | #include "gc/space.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 29 | #include "jdwp/object_registry.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "mirror/abstract_method-inl.h" |
| 31 | #include "mirror/class.h" |
| 32 | #include "mirror/class-inl.h" |
| 33 | #include "mirror/class_loader.h" |
| 34 | #include "mirror/field-inl.h" |
| 35 | #include "mirror/object-inl.h" |
| 36 | #include "mirror/object_array-inl.h" |
| 37 | #include "mirror/throwable.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 38 | #include "oat/runtime/context.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 39 | #include "object_utils.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 40 | #include "safe_map.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 41 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 42 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 43 | #include "ScopedPrimitiveArray.h" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 44 | #include "sirt_ref.h" |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 45 | #include "stack_indirect_reference_table.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 46 | #include "thread_list.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | #include "utf.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 48 | #include "well_known_classes.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 50 | namespace art { |
| 51 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 52 | static const size_t kMaxAllocRecordStackDepth = 16; // Max 255. |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 53 | static const size_t kNumAllocRecords = 512; // Must be a power of 2. |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 54 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 55 | struct AllocRecordStackTraceElement { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | mirror::AbstractMethod* method; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 57 | uint32_t dex_pc; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 58 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 59 | int32_t LineNumber() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 60 | return MethodHelper(method).GetLineNumFromDexPC(dex_pc); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 61 | } |
| 62 | }; |
| 63 | |
| 64 | struct AllocRecord { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | mirror::Class* type; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 66 | size_t byte_count; |
| 67 | uint16_t thin_lock_id; |
| 68 | AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth]; // Unused entries have NULL method. |
| 69 | |
| 70 | size_t GetDepth() { |
| 71 | size_t depth = 0; |
| 72 | while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) { |
| 73 | ++depth; |
| 74 | } |
| 75 | return depth; |
| 76 | } |
| 77 | }; |
| 78 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 79 | struct Breakpoint { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 80 | mirror::AbstractMethod* method; |
Elliott Hughes | a656a0f | 2012-02-21 18:03:44 -0800 | [diff] [blame] | 81 | uint32_t dex_pc; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 82 | 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] | 83 | }; |
| 84 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 85 | static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 86 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 229feb7 | 2012-02-23 13:33:29 -0800 | [diff] [blame] | 87 | 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] | 88 | return os; |
| 89 | } |
| 90 | |
| 91 | struct SingleStepControl { |
| 92 | // Are we single-stepping right now? |
| 93 | bool is_active; |
| 94 | Thread* thread; |
| 95 | |
| 96 | JDWP::JdwpStepSize step_size; |
| 97 | JDWP::JdwpStepDepth step_depth; |
| 98 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 99 | const mirror::AbstractMethod* method; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 100 | int32_t line_number; // Or -1 for native methods. |
| 101 | std::set<uint32_t> dex_pcs; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 102 | int stack_depth; |
| 103 | }; |
| 104 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 105 | // JDWP is allowed unless the Zygote forbids it. |
| 106 | static bool gJdwpAllowed = true; |
| 107 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 108 | // Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line? |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 109 | static bool gJdwpConfigured = false; |
| 110 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 111 | // Broken-down JDWP options. (Only valid if IsJdwpConfigured() is true.) |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 112 | static JDWP::JdwpOptions gJdwpOptions; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 113 | |
| 114 | // Runtime JDWP state. |
| 115 | static JDWP::JdwpState* gJdwpState = NULL; |
| 116 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
| 117 | static bool gDebuggerActive; // debugger is making requests. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 118 | 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] | 119 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 120 | static bool gDdmThreadNotification = false; |
| 121 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 122 | // DDMS GC-related settings. |
| 123 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 124 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 125 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 126 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 127 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 128 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 129 | static ObjectRegistry* gRegistry = NULL; |
| 130 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 131 | // Recent allocation tracking. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 132 | static Mutex gAllocTrackerLock DEFAULT_MUTEX_ACQUIRED_AFTER ("AllocTracker lock"); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 133 | AllocRecord* Dbg::recent_allocation_records_ PT_GUARDED_BY(gAllocTrackerLock) = NULL; // TODO: CircularBuffer<AllocRecord> |
| 134 | static size_t gAllocRecordHead GUARDED_BY(gAllocTrackerLock) = 0; |
| 135 | static size_t gAllocRecordCount GUARDED_BY(gAllocTrackerLock) = 0; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 136 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 137 | // Breakpoints and single-stepping. |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 138 | static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_); |
| 139 | static SingleStepControl gSingleStepControl GUARDED_BY(Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 140 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 141 | static bool IsBreakpoint(mirror::AbstractMethod* m, uint32_t dex_pc) |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 142 | LOCKS_EXCLUDED(Locks::breakpoint_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 143 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 144 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 145 | for (size_t i = 0; i < gBreakpoints.size(); ++i) { |
Elliott Hughes | a656a0f | 2012-02-21 18:03:44 -0800 | [diff] [blame] | 146 | if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == dex_pc) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 147 | VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i]; |
| 148 | return true; |
| 149 | } |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 154 | static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) { |
| 155 | MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); |
| 156 | // A thread may be suspended for GC; in this code, we really want to know whether |
| 157 | // there's a debugger suspension active. |
| 158 | return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0; |
| 159 | } |
| 160 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 161 | static mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 162 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 163 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 164 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 165 | status = JDWP::ERR_INVALID_OBJECT; |
| 166 | return NULL; |
| 167 | } |
| 168 | if (!o->IsArrayInstance()) { |
| 169 | status = JDWP::ERR_INVALID_ARRAY; |
| 170 | return NULL; |
| 171 | } |
| 172 | status = JDWP::ERR_NONE; |
| 173 | return o->AsArray(); |
| 174 | } |
| 175 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 176 | static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 177 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 178 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 179 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 180 | status = JDWP::ERR_INVALID_OBJECT; |
| 181 | return NULL; |
| 182 | } |
| 183 | if (!o->IsClass()) { |
| 184 | status = JDWP::ERR_INVALID_CLASS; |
| 185 | return NULL; |
| 186 | } |
| 187 | status = JDWP::ERR_NONE; |
| 188 | return o->AsClass(); |
| 189 | } |
| 190 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 191 | static JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread) |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 192 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 193 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) |
| 194 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 195 | mirror::Object* thread_peer = gRegistry->Get<mirror::Object*>(thread_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 196 | if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 197 | // This isn't even an object. |
| 198 | return JDWP::ERR_INVALID_OBJECT; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 199 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 200 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 201 | 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] | 202 | if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) { |
| 203 | // This isn't a thread. |
| 204 | return JDWP::ERR_INVALID_THREAD; |
| 205 | } |
| 206 | |
| 207 | thread = Thread::FromManagedThread(soa, thread_peer); |
| 208 | if (thread == NULL) { |
| 209 | // This is a java.lang.Thread without a Thread*. Must be a zombie. |
| 210 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 211 | } |
| 212 | return JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 215 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 216 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 217 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 218 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 219 | } |
| 220 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 221 | static JDWP::JdwpTag TagFromClass(mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 222 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 223 | CHECK(c != NULL); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 224 | if (c->IsArrayClass()) { |
| 225 | return JDWP::JT_ARRAY; |
| 226 | } |
| 227 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 228 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 229 | if (c->IsStringClass()) { |
| 230 | return JDWP::JT_STRING; |
| 231 | } else if (c->IsClassClass()) { |
| 232 | return JDWP::JT_CLASS_OBJECT; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 233 | } else if (class_linker->FindSystemClass("Ljava/lang/Thread;")->IsAssignableFrom(c)) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 234 | return JDWP::JT_THREAD; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 235 | } else if (class_linker->FindSystemClass("Ljava/lang/ThreadGroup;")->IsAssignableFrom(c)) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 236 | return JDWP::JT_THREAD_GROUP; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 237 | } else if (class_linker->FindSystemClass("Ljava/lang/ClassLoader;")->IsAssignableFrom(c)) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 238 | return JDWP::JT_CLASS_LOADER; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 239 | } else { |
| 240 | return JDWP::JT_OBJECT; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Objects declared to hold Object might actually hold a more specific |
| 246 | * type. The debugger may take a special interest in these (e.g. it |
| 247 | * wants to display the contents of Strings), so we want to return an |
| 248 | * appropriate tag. |
| 249 | * |
| 250 | * Null objects are tagged JT_OBJECT. |
| 251 | */ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 252 | static JDWP::JdwpTag TagFromObject(const mirror::Object* o) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 253 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 254 | return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(o->GetClass()); |
| 255 | } |
| 256 | |
| 257 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 258 | switch (tag) { |
| 259 | case JDWP::JT_BOOLEAN: |
| 260 | case JDWP::JT_BYTE: |
| 261 | case JDWP::JT_CHAR: |
| 262 | case JDWP::JT_FLOAT: |
| 263 | case JDWP::JT_DOUBLE: |
| 264 | case JDWP::JT_INT: |
| 265 | case JDWP::JT_LONG: |
| 266 | case JDWP::JT_SHORT: |
| 267 | case JDWP::JT_VOID: |
| 268 | return true; |
| 269 | default: |
| 270 | return false; |
| 271 | } |
| 272 | } |
| 273 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 274 | /* |
| 275 | * Handle one of the JDWP name/value pairs. |
| 276 | * |
| 277 | * JDWP options are: |
| 278 | * help: if specified, show help message and bail |
| 279 | * transport: may be dt_socket or dt_shmem |
| 280 | * address: for dt_socket, "host:port", or just "port" when listening |
| 281 | * server: if "y", wait for debugger to attach; if "n", attach to debugger |
| 282 | * timeout: how long to wait for debugger to connect / listen |
| 283 | * |
| 284 | * Useful with server=n (these aren't supported yet): |
| 285 | * onthrow=<exception-name>: connect to debugger when exception thrown |
| 286 | * onuncaught=y|n: connect to debugger when uncaught exception thrown |
| 287 | * launch=<command-line>: launch the debugger itself |
| 288 | * |
| 289 | * The "transport" option is required, as is "address" if server=n. |
| 290 | */ |
| 291 | static bool ParseJdwpOption(const std::string& name, const std::string& value) { |
| 292 | if (name == "transport") { |
| 293 | if (value == "dt_socket") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 294 | gJdwpOptions.transport = JDWP::kJdwpTransportSocket; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 295 | } else if (value == "dt_android_adb") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 296 | gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 297 | } else { |
| 298 | LOG(ERROR) << "JDWP transport not supported: " << value; |
| 299 | return false; |
| 300 | } |
| 301 | } else if (name == "server") { |
| 302 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 303 | gJdwpOptions.server = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 304 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 305 | gJdwpOptions.server = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 306 | } else { |
| 307 | LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'"; |
| 308 | return false; |
| 309 | } |
| 310 | } else if (name == "suspend") { |
| 311 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 312 | gJdwpOptions.suspend = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 313 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 314 | gJdwpOptions.suspend = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 315 | } else { |
| 316 | LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'"; |
| 317 | return false; |
| 318 | } |
| 319 | } else if (name == "address") { |
| 320 | /* this is either <port> or <host>:<port> */ |
| 321 | std::string port_string; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 322 | gJdwpOptions.host.clear(); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 323 | std::string::size_type colon = value.find(':'); |
| 324 | if (colon != std::string::npos) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 325 | gJdwpOptions.host = value.substr(0, colon); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 326 | port_string = value.substr(colon + 1); |
| 327 | } else { |
| 328 | port_string = value; |
| 329 | } |
| 330 | if (port_string.empty()) { |
| 331 | LOG(ERROR) << "JDWP address missing port: " << value; |
| 332 | return false; |
| 333 | } |
| 334 | char* end; |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 335 | uint64_t port = strtoul(port_string.c_str(), &end, 10); |
| 336 | if (*end != '\0' || port > 0xffff) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 337 | LOG(ERROR) << "JDWP address has junk in port field: " << value; |
| 338 | return false; |
| 339 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 340 | gJdwpOptions.port = port; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 341 | } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") { |
| 342 | /* valid but unsupported */ |
| 343 | LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'"; |
| 344 | } else { |
| 345 | LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'"; |
| 346 | } |
| 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.: |
| 353 | * "transport=dt_socket,address=8000,server=y,suspend=n" |
| 354 | */ |
| 355 | bool Dbg::ParseJdwpOptions(const std::string& options) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 356 | VLOG(jdwp) << "ParseJdwpOptions: " << options; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 357 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 358 | std::vector<std::string> pairs; |
| 359 | Split(options, ',', pairs); |
| 360 | |
| 361 | for (size_t i = 0; i < pairs.size(); ++i) { |
| 362 | std::string::size_type equals = pairs[i].find('='); |
| 363 | if (equals == std::string::npos) { |
| 364 | LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'"; |
| 365 | return false; |
| 366 | } |
| 367 | ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1)); |
| 368 | } |
| 369 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 370 | if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 371 | LOG(ERROR) << "Must specify JDWP transport: " << options; |
| 372 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 373 | if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 374 | LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options; |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | gJdwpConfigured = true; |
| 379 | return true; |
| 380 | } |
| 381 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 382 | void Dbg::StartJdwp() { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 383 | if (!gJdwpAllowed || !IsJdwpConfigured()) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 384 | // No JDWP for you! |
| 385 | return; |
| 386 | } |
| 387 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 388 | CHECK(gRegistry == NULL); |
| 389 | gRegistry = new ObjectRegistry; |
| 390 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 391 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 392 | // debugger, passively listen for a debugger, or block waiting for a |
| 393 | // debugger. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 394 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
| 395 | if (gJdwpState == NULL) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 396 | // We probably failed because some other process has the port already, which means that |
| 397 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 398 | // talking to that other process. |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 399 | LOG(FATAL) << "Debugger thread failed to initialize"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // If a debugger has already attached, send the "welcome" message. |
| 403 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 404 | if (gJdwpState->IsActive()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 405 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 406 | if (!gJdwpState->PostVMStart()) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 407 | LOG(WARNING) << "Failed to post 'start' message to debugger"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 412 | void Dbg::StopJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 413 | delete gJdwpState; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 414 | delete gRegistry; |
| 415 | gRegistry = NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 418 | void Dbg::GcDidFinish() { |
| 419 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 420 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 421 | LOG(DEBUG) << "Sending heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 422 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 423 | } |
| 424 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 425 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 426 | LOG(DEBUG) << "Dumping heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 427 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 428 | } |
| 429 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 430 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 431 | LOG(DEBUG) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 432 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 436 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 437 | gJdwpAllowed = allowed; |
| 438 | } |
| 439 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 440 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 441 | return Thread::Current()->GetInvokeReq(); |
| 442 | } |
| 443 | |
| 444 | Thread* Dbg::GetDebugThread() { |
| 445 | return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL; |
| 446 | } |
| 447 | |
| 448 | void Dbg::ClearWaitForEventThread() { |
| 449 | gJdwpState->ClearWaitForEventThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 453 | CHECK(!gDebuggerConnected); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 454 | VLOG(jdwp) << "JDWP has attached"; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 455 | gDebuggerConnected = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 456 | gDisposed = false; |
| 457 | } |
| 458 | |
| 459 | void Dbg::Disposed() { |
| 460 | gDisposed = true; |
| 461 | } |
| 462 | |
| 463 | bool Dbg::IsDisposed() { |
| 464 | return gDisposed; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 467 | static void SetDebuggerUpdatesEnabledCallback(Thread* t, void* user_data) { |
| 468 | t->SetDebuggerUpdatesEnabled(*reinterpret_cast<bool*>(user_data)); |
| 469 | } |
| 470 | |
| 471 | static void SetDebuggerUpdatesEnabled(bool enabled) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 472 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 473 | Runtime::Current()->GetThreadList()->ForEach(SetDebuggerUpdatesEnabledCallback, &enabled); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 476 | void Dbg::GoActive() { |
| 477 | // Enable all debugging features, including scans for breakpoints. |
| 478 | // This is a no-op if we're already active. |
| 479 | // Only called from the JDWP handler thread. |
| 480 | if (gDebuggerActive) { |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | LOG(INFO) << "Debugger is active"; |
| 485 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 486 | { |
| 487 | // 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] | 488 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 489 | CHECK_EQ(gBreakpoints.size(), 0U); |
| 490 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 491 | |
| 492 | gDebuggerActive = true; |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 493 | SetDebuggerUpdatesEnabled(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 497 | CHECK(gDebuggerConnected); |
| 498 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 499 | LOG(INFO) << "Debugger is no longer active"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 500 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 501 | gDebuggerActive = false; |
| 502 | SetDebuggerUpdatesEnabled(false); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 503 | |
| 504 | gRegistry->Clear(); |
| 505 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 508 | bool Dbg::IsDebuggerActive() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 509 | return gDebuggerActive; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 512 | bool Dbg::IsJdwpConfigured() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 513 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | int64_t Dbg::LastDebuggerActivity() { |
Elliott Hughes | ca95152 | 2011-12-05 12:01:32 -0800 | [diff] [blame] | 517 | return gJdwpState->LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 520 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 521 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 524 | std::string Dbg::GetClassName(JDWP::RefTypeId class_id) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 525 | mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 526 | if (o == NULL) { |
| 527 | return "NULL"; |
| 528 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 529 | if (o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 530 | return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id)); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 531 | } |
| 532 | if (!o->IsClass()) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 533 | return StringPrintf("non-class %p", o); // This is only used for debugging output anyway. |
| 534 | } |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame] | 535 | return DescriptorToName(ClassHelper(o->AsClass()).GetDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 538 | JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 539 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 540 | mirror::Class* c = DecodeClass(id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 541 | if (c == NULL) { |
| 542 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 543 | } |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 544 | class_object_id = gRegistry->Add(c); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 545 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 548 | JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 549 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 550 | mirror::Class* c = DecodeClass(id, status); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 551 | if (c == NULL) { |
| 552 | return status; |
| 553 | } |
| 554 | if (c->IsInterface()) { |
| 555 | // http://code.google.com/p/android/issues/detail?id=20856 |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 556 | superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 557 | } else { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 558 | superclass_id = gRegistry->Add(c->GetSuperClass()); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 559 | } |
| 560 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 563 | JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 564 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 565 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 566 | return JDWP::ERR_INVALID_OBJECT; |
| 567 | } |
| 568 | expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader())); |
| 569 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 572 | JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
| 573 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 574 | mirror::Class* c = DecodeClass(id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 575 | if (c == NULL) { |
| 576 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 577 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 578 | |
| 579 | uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask; |
| 580 | |
| 581 | // Set ACC_SUPER; dex files don't contain this flag, but all classes are supposed to have it set. |
| 582 | // Class.getModifiers doesn't return it, but JDWP does, so we set it here. |
| 583 | access_flags |= kAccSuper; |
| 584 | |
| 585 | expandBufAdd4BE(pReply, access_flags); |
| 586 | |
| 587 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 590 | JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) |
| 591 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 592 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 593 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 594 | return JDWP::ERR_INVALID_OBJECT; |
| 595 | } |
| 596 | |
| 597 | // Ensure all threads are suspended while we read objects' lock words. |
| 598 | Thread* self = Thread::Current(); |
| 599 | Locks::mutator_lock_->SharedUnlock(self); |
| 600 | Locks::mutator_lock_->ExclusiveLock(self); |
| 601 | |
| 602 | MonitorInfo monitor_info(o); |
| 603 | |
| 604 | Locks::mutator_lock_->ExclusiveUnlock(self); |
| 605 | Locks::mutator_lock_->SharedLock(self); |
| 606 | |
| 607 | if (monitor_info.owner != NULL) { |
| 608 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner->GetPeer())); |
| 609 | } else { |
| 610 | expandBufAddObjectId(reply, gRegistry->Add(NULL)); |
| 611 | } |
| 612 | expandBufAdd4BE(reply, monitor_info.entry_count); |
| 613 | expandBufAdd4BE(reply, monitor_info.waiters.size()); |
| 614 | for (size_t i = 0; i < monitor_info.waiters.size(); ++i) { |
| 615 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters[i]->GetPeer())); |
| 616 | } |
| 617 | return JDWP::ERR_NONE; |
| 618 | } |
| 619 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 620 | JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id, |
| 621 | std::vector<JDWP::ObjectId>& monitors, |
| 622 | std::vector<uint32_t>& stack_depths) |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 623 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 624 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 625 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 626 | Thread* thread; |
| 627 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 628 | if (error != JDWP::ERR_NONE) { |
| 629 | return error; |
| 630 | } |
| 631 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 632 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 633 | } |
| 634 | |
| 635 | struct OwnedMonitorVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 636 | OwnedMonitorVisitor(Thread* thread, Context* context) |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 637 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 638 | : StackVisitor(thread, context), current_stack_depth(0) {} |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 639 | |
| 640 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 641 | // annotalysis. |
| 642 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 643 | if (!GetMethod()->IsRuntimeMethod()) { |
| 644 | Monitor::VisitLocks(this, AppendOwnedMonitors, this); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 645 | ++current_stack_depth; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 646 | } |
| 647 | return true; |
| 648 | } |
| 649 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 650 | static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 651 | OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 652 | visitor->monitors.push_back(owned_monitor); |
| 653 | visitor->stack_depths.push_back(visitor->current_stack_depth); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 656 | size_t current_stack_depth; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 657 | std::vector<mirror::Object*> monitors; |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 658 | std::vector<uint32_t> stack_depths; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 659 | }; |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 660 | UniquePtr<Context> context(Context::Create()); |
| 661 | OwnedMonitorVisitor visitor(thread, context.get()); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 662 | visitor.WalkStack(); |
| 663 | |
| 664 | for (size_t i = 0; i < visitor.monitors.size(); ++i) { |
| 665 | monitors.push_back(gRegistry->Add(visitor.monitors[i])); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 666 | stack_depths.push_back(visitor.stack_depths[i]); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | return JDWP::ERR_NONE; |
| 670 | } |
| 671 | |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 672 | JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, JDWP::ObjectId& contended_monitor) |
| 673 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 674 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 675 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 676 | Thread* thread; |
| 677 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 678 | if (error != JDWP::ERR_NONE) { |
| 679 | return error; |
| 680 | } |
| 681 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 682 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 683 | } |
| 684 | |
| 685 | contended_monitor = gRegistry->Add(Monitor::GetContendedMonitor(thread)); |
| 686 | |
| 687 | return JDWP::ERR_NONE; |
| 688 | } |
| 689 | |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 690 | JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids, |
| 691 | std::vector<uint64_t>& counts) |
| 692 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 693 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 694 | std::vector<mirror::Class*> classes; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 695 | counts.clear(); |
| 696 | for (size_t i = 0; i < class_ids.size(); ++i) { |
| 697 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 698 | mirror::Class* c = DecodeClass(class_ids[i], status); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 699 | if (c == NULL) { |
| 700 | return status; |
| 701 | } |
| 702 | classes.push_back(c); |
| 703 | counts.push_back(0); |
| 704 | } |
| 705 | |
| 706 | Runtime::Current()->GetHeap()->CountInstances(classes, false, &counts[0]); |
| 707 | return JDWP::ERR_NONE; |
| 708 | } |
| 709 | |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 710 | JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances) |
| 711 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 712 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 713 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 714 | if (c == NULL) { |
| 715 | return status; |
| 716 | } |
| 717 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 718 | std::vector<mirror::Object*> raw_instances; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 719 | Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances); |
| 720 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
| 721 | instances.push_back(gRegistry->Add(raw_instances[i])); |
| 722 | } |
| 723 | return JDWP::ERR_NONE; |
| 724 | } |
| 725 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 726 | JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count, |
| 727 | std::vector<JDWP::ObjectId>& referring_objects) |
| 728 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 729 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 730 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 731 | return JDWP::ERR_INVALID_OBJECT; |
| 732 | } |
| 733 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 734 | std::vector<mirror::Object*> raw_instances; |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 735 | Runtime::Current()->GetHeap()->GetReferringObjects(o, max_count, raw_instances); |
| 736 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
| 737 | referring_objects.push_back(gRegistry->Add(raw_instances[i])); |
| 738 | } |
| 739 | return JDWP::ERR_NONE; |
| 740 | } |
| 741 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 742 | JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id) |
| 743 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 744 | gRegistry->DisableCollection(object_id); |
| 745 | return JDWP::ERR_NONE; |
| 746 | } |
| 747 | |
| 748 | JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id) |
| 749 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 750 | gRegistry->EnableCollection(object_id); |
| 751 | return JDWP::ERR_NONE; |
| 752 | } |
| 753 | |
| 754 | JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected) |
| 755 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 756 | is_collected = gRegistry->IsCollected(object_id); |
| 757 | return JDWP::ERR_NONE; |
| 758 | } |
| 759 | |
| 760 | void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) |
| 761 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 762 | gRegistry->DisposeObject(object_id, reference_count); |
| 763 | } |
| 764 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 765 | JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 766 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 767 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 768 | if (c == NULL) { |
| 769 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 770 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 771 | |
| 772 | expandBufAdd1(pReply, c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS); |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 773 | expandBufAddRefTypeId(pReply, class_id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 774 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 777 | void Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 778 | // Get the complete list of reference classes (i.e. all classes except |
| 779 | // the primitive types). |
| 780 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 781 | struct ClassListCreator { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 782 | explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 785 | static bool Visit(mirror::Class* c, void* arg) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 786 | return reinterpret_cast<ClassListCreator*>(arg)->Visit(c); |
| 787 | } |
| 788 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 789 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 790 | // annotalysis. |
| 791 | bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 792 | if (!c->IsPrimitive()) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 793 | classes.push_back(gRegistry->AddRefType(c)); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 794 | } |
| 795 | return true; |
| 796 | } |
| 797 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 798 | std::vector<JDWP::RefTypeId>& classes; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 799 | }; |
| 800 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 801 | ClassListCreator clc(classes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 802 | Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 805 | 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] | 806 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 807 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 808 | if (c == NULL) { |
| 809 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 812 | if (c->IsArrayClass()) { |
| 813 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 814 | *pTypeTag = JDWP::TT_ARRAY; |
| 815 | } else { |
| 816 | if (c->IsErroneous()) { |
| 817 | *pStatus = JDWP::CS_ERROR; |
| 818 | } else { |
| 819 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 820 | } |
| 821 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 822 | } |
| 823 | |
| 824 | if (pDescriptor != NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 825 | *pDescriptor = ClassHelper(c).GetDescriptor(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 826 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 827 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 830 | void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 831 | std::vector<mirror::Class*> classes; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 832 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
| 833 | ids.clear(); |
| 834 | for (size_t i = 0; i < classes.size(); ++i) { |
| 835 | ids.push_back(gRegistry->Add(classes[i])); |
| 836 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 839 | JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) |
| 840 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 841 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 842 | if (o == NULL || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 843 | return JDWP::ERR_INVALID_OBJECT; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 844 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 845 | |
| 846 | JDWP::JdwpTypeTag type_tag; |
| 847 | if (o->GetClass()->IsArrayClass()) { |
| 848 | type_tag = JDWP::TT_ARRAY; |
| 849 | } else if (o->GetClass()->IsInterface()) { |
| 850 | type_tag = JDWP::TT_INTERFACE; |
| 851 | } else { |
| 852 | type_tag = JDWP::TT_CLASS; |
| 853 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 854 | JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 855 | |
| 856 | expandBufAdd1(pReply, type_tag); |
| 857 | expandBufAddRefTypeId(pReply, type_id); |
| 858 | |
| 859 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 862 | JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string& signature) { |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 863 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 864 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 865 | if (c == NULL) { |
| 866 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 867 | } |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 868 | signature = ClassHelper(c).GetDescriptor(); |
| 869 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 872 | JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 873 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 874 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 875 | if (c == NULL) { |
| 876 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 877 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 878 | result = ClassHelper(c).GetSourceFile(); |
| 879 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 882 | JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 883 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 884 | if (o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 885 | return JDWP::ERR_INVALID_OBJECT; |
| 886 | } |
| 887 | tag = TagFromObject(o); |
| 888 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 891 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 892 | switch (tag) { |
| 893 | case JDWP::JT_VOID: |
| 894 | return 0; |
| 895 | case JDWP::JT_BYTE: |
| 896 | case JDWP::JT_BOOLEAN: |
| 897 | return 1; |
| 898 | case JDWP::JT_CHAR: |
| 899 | case JDWP::JT_SHORT: |
| 900 | return 2; |
| 901 | case JDWP::JT_FLOAT: |
| 902 | case JDWP::JT_INT: |
| 903 | return 4; |
| 904 | case JDWP::JT_ARRAY: |
| 905 | case JDWP::JT_OBJECT: |
| 906 | case JDWP::JT_STRING: |
| 907 | case JDWP::JT_THREAD: |
| 908 | case JDWP::JT_THREAD_GROUP: |
| 909 | case JDWP::JT_CLASS_LOADER: |
| 910 | case JDWP::JT_CLASS_OBJECT: |
| 911 | return sizeof(JDWP::ObjectId); |
| 912 | case JDWP::JT_DOUBLE: |
| 913 | case JDWP::JT_LONG: |
| 914 | return 8; |
| 915 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 916 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 917 | return -1; |
| 918 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 921 | JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 922 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 923 | mirror::Array* a = DecodeArray(array_id, status); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 924 | if (a == NULL) { |
| 925 | return status; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 926 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 927 | length = a->GetLength(); |
| 928 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 931 | 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] | 932 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 933 | mirror::Array* a = DecodeArray(array_id, status); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 934 | if (a == NULL) { |
| 935 | return status; |
| 936 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 937 | |
| 938 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 939 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 940 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 941 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 942 | std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 943 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 944 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 945 | expandBufAdd1(pReply, tag); |
| 946 | expandBufAdd4BE(pReply, count); |
| 947 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 948 | if (IsPrimitiveTag(tag)) { |
| 949 | size_t width = GetTagWidth(tag); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 950 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 951 | if (width == 8) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 952 | 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] | 953 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 954 | } else if (width == 4) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 955 | 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] | 956 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 957 | } else if (width == 2) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 958 | 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] | 959 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 960 | } else { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 961 | 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] | 962 | memcpy(dst, &src[offset * width], count * width); |
| 963 | } |
| 964 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 965 | mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 966 | for (int i = 0; i < count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 967 | mirror::Object* element = oa->Get(offset + i); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 968 | JDWP::JdwpTag specific_tag = (element != NULL) ? TagFromObject(element) : tag; |
| 969 | expandBufAdd1(pReply, specific_tag); |
| 970 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 971 | } |
| 972 | } |
| 973 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 974 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 977 | JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 978 | const uint8_t* src) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 979 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 980 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 981 | mirror::Array* a = DecodeArray(array_id, status); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 982 | if (a == NULL) { |
| 983 | return status; |
| 984 | } |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 985 | |
| 986 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 987 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 988 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 989 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 990 | std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor()); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 991 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 992 | |
| 993 | if (IsPrimitiveTag(tag)) { |
| 994 | size_t width = GetTagWidth(tag); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 995 | if (width == 8) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 996 | uint8_t* dst = &(reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint64_t)))[offset * width]); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 997 | for (int i = 0; i < count; ++i) { |
| 998 | // Handle potentially non-aligned memory access one byte at a time for ARM's benefit. |
| 999 | uint64_t value; |
| 1000 | for (size_t j = 0; j < sizeof(uint64_t); ++j) reinterpret_cast<uint8_t*>(&value)[j] = src[j]; |
| 1001 | src += sizeof(uint64_t); |
| 1002 | JDWP::Write8BE(&dst, value); |
| 1003 | } |
| 1004 | } else if (width == 4) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1005 | uint8_t* dst = &(reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint32_t)))[offset * width]); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1006 | const uint32_t* src4 = reinterpret_cast<const uint32_t*>(src); |
| 1007 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[i]); |
| 1008 | } else if (width == 2) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1009 | uint8_t* dst = &(reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint16_t)))[offset * width]); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1010 | const uint16_t* src2 = reinterpret_cast<const uint16_t*>(src); |
| 1011 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[i]); |
| 1012 | } else { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1013 | uint8_t* dst = &(reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t)))[offset * width]); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1014 | memcpy(&dst[offset * width], src, count * width); |
| 1015 | } |
| 1016 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1017 | mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>(); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1018 | for (int i = 0; i < count; ++i) { |
| 1019 | JDWP::ObjectId id = JDWP::ReadObjectId(&src); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1020 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 1021 | if (o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1022 | return JDWP::ERR_INVALID_OBJECT; |
| 1023 | } |
| 1024 | oa->Set(offset + i, o); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1028 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1031 | JDWP::ObjectId Dbg::CreateString(const std::string& str) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1032 | return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str())); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1035 | JDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1036 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1037 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1038 | if (c == NULL) { |
| 1039 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1040 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1041 | new_object = gRegistry->Add(c->AllocObject(Thread::Current())); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1042 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 1045 | /* |
| 1046 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 1047 | */ |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1048 | JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1049 | JDWP::ObjectId& new_array) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1050 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1051 | mirror::Class* c = DecodeClass(array_class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1052 | if (c == NULL) { |
| 1053 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1054 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1055 | new_array = gRegistry->Add(mirror::Array::Alloc(Thread::Current(), c, length)); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1056 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1059 | bool Dbg::MatchType(JDWP::RefTypeId instance_class_id, JDWP::RefTypeId class_id) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1060 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1061 | mirror::Class* c1 = DecodeClass(instance_class_id, status); |
Elliott Hughes | a656a0f | 2012-02-21 18:03:44 -0800 | [diff] [blame] | 1062 | CHECK(c1 != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1063 | mirror::Class* c2 = DecodeClass(class_id, status); |
Elliott Hughes | a656a0f | 2012-02-21 18:03:44 -0800 | [diff] [blame] | 1064 | CHECK(c2 != NULL); |
| 1065 | return c1->IsAssignableFrom(c2); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1068 | static JDWP::FieldId ToFieldId(const mirror::Field* f) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1069 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1070 | #ifdef MOVING_GARBAGE_COLLECTOR |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1071 | UNIMPLEMENTED(FATAL); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1072 | #else |
| 1073 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
| 1074 | #endif |
| 1075 | } |
| 1076 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1077 | static JDWP::MethodId ToMethodId(const mirror::AbstractMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1078 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1079 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 1080 | UNIMPLEMENTED(FATAL); |
| 1081 | #else |
| 1082 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
| 1083 | #endif |
| 1084 | } |
| 1085 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1086 | static mirror::Field* FromFieldId(JDWP::FieldId fid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1087 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1088 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 1089 | UNIMPLEMENTED(FATAL); |
| 1090 | #else |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1091 | return reinterpret_cast<mirror::Field*>(static_cast<uintptr_t>(fid)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1092 | #endif |
| 1093 | } |
| 1094 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1095 | static mirror::AbstractMethod* FromMethodId(JDWP::MethodId mid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1096 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1097 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 1098 | UNIMPLEMENTED(FATAL); |
| 1099 | #else |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1100 | return reinterpret_cast<mirror::AbstractMethod*>(static_cast<uintptr_t>(mid)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1101 | #endif |
| 1102 | } |
| 1103 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1104 | 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] | 1105 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1106 | if (m == NULL) { |
| 1107 | memset(&location, 0, sizeof(location)); |
| 1108 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1109 | mirror::Class* c = m->GetDeclaringClass(); |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 1110 | location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1111 | location.class_id = gRegistry->Add(c); |
| 1112 | location.method_id = ToMethodId(m); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1113 | location.dex_pc = dex_pc; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1114 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1117 | std::string Dbg::GetMethodName(JDWP::MethodId method_id) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1118 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1119 | mirror::AbstractMethod* m = FromMethodId(method_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1120 | return MethodHelper(m).GetName(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1123 | std::string Dbg::GetFieldName(JDWP::FieldId field_id) |
| 1124 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1125 | mirror::Field* f = FromFieldId(field_id); |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1126 | return FieldHelper(f).GetName(); |
| 1127 | } |
| 1128 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1129 | /* |
| 1130 | * Augment the access flags for synthetic methods and fields by setting |
| 1131 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 1132 | * flags not specified by the Java programming language. |
| 1133 | */ |
| 1134 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 1135 | accessFlags &= kAccJavaFlagsMask; |
| 1136 | if ((accessFlags & kAccSynthetic) != 0) { |
| 1137 | accessFlags |= 0xf0000000; |
| 1138 | } |
| 1139 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1142 | static const uint16_t kEclipseWorkaroundSlot = 1000; |
| 1143 | |
| 1144 | /* |
| 1145 | * Eclipse appears to expect that the "this" reference is in slot zero. |
| 1146 | * If it's not, the "variables" display will show two copies of "this", |
| 1147 | * possibly because it gets "this" from SF.ThisObject and then displays |
| 1148 | * all locals with nonzero slot numbers. |
| 1149 | * |
| 1150 | * So, we remap the item in slot 0 to 1000, and remap "this" to zero. On |
| 1151 | * SF.GetValues / SF.SetValues we map them back. |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1152 | * |
| 1153 | * TODO: jdb uses the value to determine whether a variable is a local or an argument, |
| 1154 | * by checking whether it's less than the number of arguments. To make that work, we'd |
| 1155 | * 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] | 1156 | */ |
| 1157 | static uint16_t MangleSlot(uint16_t slot, const char* name) { |
| 1158 | uint16_t newSlot = slot; |
| 1159 | if (strcmp(name, "this") == 0) { |
| 1160 | newSlot = 0; |
| 1161 | } else if (slot == 0) { |
| 1162 | newSlot = kEclipseWorkaroundSlot; |
| 1163 | } |
| 1164 | return newSlot; |
| 1165 | } |
| 1166 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1167 | static uint16_t DemangleSlot(uint16_t slot, mirror::AbstractMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1168 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1169 | if (slot == kEclipseWorkaroundSlot) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1170 | return 0; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1171 | } else if (slot == 0) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1172 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1173 | CHECK(code_item != NULL) << PrettyMethod(m); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1174 | return code_item->registers_size_ - code_item->ins_size_; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1175 | } |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1176 | return slot; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1179 | 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] | 1180 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1181 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1182 | if (c == NULL) { |
| 1183 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1184 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1185 | |
| 1186 | size_t instance_field_count = c->NumInstanceFields(); |
| 1187 | size_t static_field_count = c->NumStaticFields(); |
| 1188 | |
| 1189 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 1190 | |
| 1191 | 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] | 1192 | 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] | 1193 | FieldHelper fh(f); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1194 | expandBufAddFieldId(pReply, ToFieldId(f)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1195 | expandBufAddUtf8String(pReply, fh.GetName()); |
| 1196 | expandBufAddUtf8String(pReply, fh.GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1197 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1198 | static const char genericSignature[1] = ""; |
| 1199 | expandBufAddUtf8String(pReply, genericSignature); |
| 1200 | } |
| 1201 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 1202 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1203 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1206 | JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1207 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1208 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1209 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1210 | if (c == NULL) { |
| 1211 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1212 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1213 | |
| 1214 | size_t direct_method_count = c->NumDirectMethods(); |
| 1215 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 1216 | |
| 1217 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 1218 | |
| 1219 | 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] | 1220 | 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] | 1221 | MethodHelper mh(m); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1222 | expandBufAddMethodId(pReply, ToMethodId(m)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1223 | expandBufAddUtf8String(pReply, mh.GetName()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1224 | expandBufAddUtf8String(pReply, mh.GetSignature()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1225 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1226 | static const char genericSignature[1] = ""; |
| 1227 | expandBufAddUtf8String(pReply, genericSignature); |
| 1228 | } |
| 1229 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 1230 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1231 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1234 | JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1235 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1236 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1237 | if (c == NULL) { |
| 1238 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1239 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1240 | |
| 1241 | ClassHelper kh(c); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 1242 | size_t interface_count = kh.NumDirectInterfaces(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1243 | expandBufAdd4BE(pReply, interface_count); |
| 1244 | for (size_t i = 0; i < interface_count; ++i) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 1245 | expandBufAddRefTypeId(pReply, gRegistry->AddRefType(kh.GetDirectInterface(i))); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1246 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1247 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1250 | void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1251 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1252 | struct DebugCallbackContext { |
| 1253 | int numItems; |
| 1254 | JDWP::ExpandBuf* pReply; |
| 1255 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1256 | static bool Callback(void* context, uint32_t address, uint32_t line_number) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1257 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1258 | expandBufAdd8BE(pContext->pReply, address); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1259 | expandBufAdd4BE(pContext->pReply, line_number); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1260 | pContext->numItems++; |
| 1261 | return true; |
| 1262 | } |
| 1263 | }; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1264 | mirror::AbstractMethod* m = FromMethodId(method_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1265 | MethodHelper mh(m); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1266 | uint64_t start, end; |
| 1267 | if (m->IsNative()) { |
| 1268 | start = -1; |
| 1269 | end = -1; |
| 1270 | } else { |
| 1271 | start = 0; |
jeffhao | 14f0db9 | 2012-12-14 17:50:42 -0800 | [diff] [blame] | 1272 | // Return the index of the last instruction |
| 1273 | end = mh.GetCodeItem()->insns_size_in_code_units_ - 1; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | expandBufAdd8BE(pReply, start); |
| 1277 | expandBufAdd8BE(pReply, end); |
| 1278 | |
| 1279 | // Add numLines later |
| 1280 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 1281 | expandBufAdd4BE(pReply, 0); |
| 1282 | |
| 1283 | DebugCallbackContext context; |
| 1284 | context.numItems = 0; |
| 1285 | context.pReply = pReply; |
| 1286 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1287 | mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(), |
| 1288 | DebugCallbackContext::Callback, NULL, &context); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1289 | |
| 1290 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1293 | 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] | 1294 | struct DebugCallbackContext { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1295 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1296 | size_t variable_count; |
| 1297 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1298 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1299 | 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] | 1300 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1301 | |
Elliott Hughes | ad3da69 | 2012-02-24 16:51:35 -0800 | [diff] [blame] | 1302 | 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] | 1303 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1304 | slot = MangleSlot(slot, name); |
| 1305 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1306 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 1307 | expandBufAddUtf8String(pContext->pReply, name); |
| 1308 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1309 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1310 | expandBufAddUtf8String(pContext->pReply, signature); |
| 1311 | } |
| 1312 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 1313 | expandBufAdd4BE(pContext->pReply, slot); |
| 1314 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1315 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1316 | } |
| 1317 | }; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1318 | mirror::AbstractMethod* m = FromMethodId(method_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1319 | MethodHelper mh(m); |
| 1320 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1321 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1322 | // arg_count considers doubles and longs to take 2 units. |
| 1323 | // variable_count considers everything to take 1 unit. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1324 | std::string shorty(mh.GetShorty()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1325 | expandBufAdd4BE(pReply, mirror::AbstractMethod::NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1326 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1327 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1328 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1329 | expandBufAdd4BE(pReply, 0); |
| 1330 | |
| 1331 | DebugCallbackContext context; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1332 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1333 | context.variable_count = 0; |
| 1334 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1335 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1336 | mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL, |
| 1337 | DebugCallbackContext::Callback, &context); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1338 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1339 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1342 | JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id, |
| 1343 | std::vector<uint8_t>& bytecodes) |
| 1344 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1345 | mirror::AbstractMethod* m = FromMethodId(method_id); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1346 | if (m == NULL) { |
| 1347 | return JDWP::ERR_INVALID_METHODID; |
| 1348 | } |
| 1349 | MethodHelper mh(m); |
| 1350 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
| 1351 | size_t byte_count = code_item->insns_size_in_code_units_ * 2; |
| 1352 | const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_); |
| 1353 | const uint8_t* end = begin + byte_count; |
| 1354 | for (const uint8_t* p = begin; p != end; ++p) { |
| 1355 | bytecodes.push_back(*p); |
| 1356 | } |
| 1357 | return JDWP::ERR_NONE; |
| 1358 | } |
| 1359 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1360 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) { |
| 1361 | return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1364 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) { |
| 1365 | return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1368 | static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id, |
| 1369 | JDWP::FieldId field_id, JDWP::ExpandBuf* pReply, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1370 | bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1371 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1372 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1373 | mirror::Class* c = DecodeClass(ref_type_id, status); |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1374 | if (ref_type_id != 0 && c == NULL) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1375 | return status; |
| 1376 | } |
| 1377 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1378 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 1379 | if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1380 | return JDWP::ERR_INVALID_OBJECT; |
| 1381 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1382 | mirror::Field* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1383 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1384 | mirror::Class* receiver_class = c; |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1385 | if (receiver_class == NULL && o != NULL) { |
| 1386 | receiver_class = o->GetClass(); |
| 1387 | } |
| 1388 | // TODO: should we give up now if receiver_class is NULL? |
| 1389 | if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) { |
| 1390 | LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1391 | return JDWP::ERR_INVALID_FIELDID; |
| 1392 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1393 | |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1394 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1395 | // TODO: should we change the tests and check both? |
| 1396 | if (is_static) { |
| 1397 | if (!f->IsStatic()) { |
| 1398 | return JDWP::ERR_INVALID_FIELDID; |
| 1399 | } |
| 1400 | } else { |
| 1401 | if (f->IsStatic()) { |
| 1402 | 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] | 1403 | } |
| 1404 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1405 | if (f->IsStatic()) { |
| 1406 | o = f->GetDeclaringClass(); |
| 1407 | } |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1408 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1409 | JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1410 | |
| 1411 | if (IsPrimitiveTag(tag)) { |
| 1412 | expandBufAdd1(pReply, tag); |
| 1413 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 1414 | expandBufAdd1(pReply, f->Get32(o)); |
| 1415 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 1416 | expandBufAdd2BE(pReply, f->Get32(o)); |
| 1417 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 1418 | expandBufAdd4BE(pReply, f->Get32(o)); |
| 1419 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1420 | expandBufAdd8BE(pReply, f->Get64(o)); |
| 1421 | } else { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1422 | LOG(FATAL) << "Unknown tag: " << tag; |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1423 | } |
| 1424 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1425 | mirror::Object* value = f->GetObject(o); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1426 | expandBufAdd1(pReply, TagFromObject(value)); |
| 1427 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 1428 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1429 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1432 | JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1433 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1434 | return GetFieldValueImpl(0, object_id, field_id, pReply, false); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1435 | } |
| 1436 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1437 | JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) { |
| 1438 | return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1439 | } |
| 1440 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1441 | static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1442 | uint64_t value, int width, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1443 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1444 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 1445 | if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1446 | return JDWP::ERR_INVALID_OBJECT; |
| 1447 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1448 | mirror::Field* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1449 | |
| 1450 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1451 | // TODO: should we change the tests and check both? |
| 1452 | if (is_static) { |
| 1453 | if (!f->IsStatic()) { |
| 1454 | return JDWP::ERR_INVALID_FIELDID; |
| 1455 | } |
| 1456 | } else { |
| 1457 | if (f->IsStatic()) { |
| 1458 | 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] | 1459 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1460 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1461 | if (f->IsStatic()) { |
| 1462 | o = f->GetDeclaringClass(); |
| 1463 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1464 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1465 | JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1466 | |
| 1467 | if (IsPrimitiveTag(tag)) { |
| 1468 | if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1469 | CHECK_EQ(width, 8); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1470 | f->Set64(o, value); |
| 1471 | } else { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1472 | CHECK_LE(width, 4); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1473 | f->Set32(o, value); |
| 1474 | } |
| 1475 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1476 | mirror::Object* v = gRegistry->Get<mirror::Object*>(value); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 1477 | if (v == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1478 | return JDWP::ERR_INVALID_OBJECT; |
| 1479 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1480 | if (v != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1481 | mirror::Class* field_type = FieldHelper(f).GetType(); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1482 | if (!field_type->IsAssignableFrom(v->GetClass())) { |
| 1483 | return JDWP::ERR_INVALID_OBJECT; |
| 1484 | } |
| 1485 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1486 | f->SetObject(o, v); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1487 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1488 | |
| 1489 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1492 | 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] | 1493 | int width) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1494 | return SetFieldValueImpl(object_id, field_id, value, width, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1495 | } |
| 1496 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1497 | JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) { |
| 1498 | return SetFieldValueImpl(0, field_id, value, width, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1501 | std::string Dbg::StringToUtf8(JDWP::ObjectId string_id) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1502 | mirror::String* s = gRegistry->Get<mirror::String*>(string_id); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1503 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1506 | JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1507 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1508 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1509 | Thread* thread; |
| 1510 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1511 | if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1512 | return error; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1513 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1514 | |
| 1515 | // 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] | 1516 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id); |
| 1517 | mirror::Field* java_lang_Thread_name_field = |
| 1518 | soa.DecodeField(WellKnownClasses::java_lang_Thread_name); |
| 1519 | mirror::String* s = |
| 1520 | reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object)); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1521 | if (s != NULL) { |
| 1522 | name = s->ToModifiedUtf8(); |
| 1523 | } |
| 1524 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1527 | JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1528 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1529 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 1530 | if (thread_object == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1531 | return JDWP::ERR_INVALID_OBJECT; |
| 1532 | } |
| 1533 | |
| 1534 | // Okay, so it's an object, but is it actually a thread? |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1535 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1536 | Thread* thread; |
| 1537 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1538 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1539 | // Zombie threads are in the null group. |
| 1540 | expandBufAddObjectId(pReply, JDWP::ObjectId(0)); |
| 1541 | return JDWP::ERR_NONE; |
| 1542 | } |
| 1543 | if (error != JDWP::ERR_NONE) { |
| 1544 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1545 | } |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1546 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1547 | mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Thread;"); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1548 | CHECK(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1549 | mirror::Field* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;"); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1550 | CHECK(f != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1551 | mirror::Object* group = f->GetObject(thread_object); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1552 | CHECK(group != NULL); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1553 | JDWP::ObjectId thread_group_id = gRegistry->Add(group); |
| 1554 | |
| 1555 | expandBufAddObjectId(pReply, thread_group_id); |
| 1556 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1559 | std::string Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1560 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1561 | mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1562 | CHECK(thread_group != NULL); |
| 1563 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1564 | mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1565 | CHECK(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1566 | mirror::Field* f = c->FindInstanceField("name", "Ljava/lang/String;"); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1567 | CHECK(f != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1568 | mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group)); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1569 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1572 | JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1573 | mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1574 | CHECK(thread_group != NULL); |
| 1575 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1576 | mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1577 | CHECK(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1578 | mirror::Field* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;"); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1579 | CHECK(f != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1580 | mirror::Object* parent = f->GetObject(thread_group); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1581 | return gRegistry->Add(parent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1585 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1586 | mirror::Field* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup); |
| 1587 | mirror::Object* group = f->GetObject(f->GetDeclaringClass()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1588 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | JDWP::ObjectId Dbg::GetMainThreadGroupId() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1592 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1593 | mirror::Field* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup); |
| 1594 | mirror::Object* group = f->GetObject(f->GetDeclaringClass()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1595 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1598 | 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] | 1599 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1600 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 1601 | *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED; |
| 1602 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1603 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1604 | Thread* thread; |
| 1605 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1606 | if (error != JDWP::ERR_NONE) { |
| 1607 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1608 | *pThreadStatus = JDWP::TS_ZOMBIE; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1609 | return JDWP::ERR_NONE; |
| 1610 | } |
| 1611 | return error; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1612 | } |
| 1613 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 1614 | if (IsSuspendedForDebugger(soa, thread)) { |
| 1615 | *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 1618 | switch (thread->GetState()) { |
| 1619 | case kBlocked: *pThreadStatus = JDWP::TS_MONITOR; break; |
| 1620 | case kNative: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1621 | case kRunnable: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1622 | case kSleeping: *pThreadStatus = JDWP::TS_SLEEPING; break; |
| 1623 | case kStarting: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1624 | case kSuspended: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1625 | case kTerminated: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1626 | case kTimedWaiting: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1627 | case kWaitingForDebuggerSend: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1628 | case kWaitingForDebuggerSuspension: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1629 | case kWaitingForDebuggerToAttach: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1630 | case kWaitingForGcToComplete: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1631 | case kWaitingForJniOnLoad: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1632 | case kWaitingForSignalCatcherOutput: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1633 | case kWaitingInMainDebuggerLoop: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1634 | case kWaitingInMainSignalCatcherLoop: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1635 | case kWaitingPerformingGc: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1636 | case kWaiting: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1637 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 1638 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1639 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1642 | JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1643 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1644 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1645 | Thread* thread; |
| 1646 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1647 | if (error != JDWP::ERR_NONE) { |
| 1648 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1649 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1650 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1651 | expandBufAdd4BE(pReply, thread->GetDebugSuspendCount()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1652 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 1655 | JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) { |
| 1656 | ScopedObjectAccess soa(Thread::Current()); |
| 1657 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 1658 | Thread* thread; |
| 1659 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1660 | if (error != JDWP::ERR_NONE) { |
| 1661 | return error; |
| 1662 | } |
| 1663 | thread->Interrupt(); |
| 1664 | return JDWP::ERR_NONE; |
| 1665 | } |
| 1666 | |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1667 | 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] | 1668 | class ThreadListVisitor { |
| 1669 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1670 | ThreadListVisitor(const ScopedObjectAccessUnchecked& soa, mirror::Object* desired_thread_group, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1671 | std::vector<JDWP::ObjectId>& thread_ids) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1672 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1673 | : soa_(soa), desired_thread_group_(desired_thread_group), thread_ids_(thread_ids) {} |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1674 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1675 | static void Visit(Thread* t, void* arg) { |
| 1676 | reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t); |
| 1677 | } |
| 1678 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1679 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1680 | // annotalysis. |
| 1681 | void Visit(Thread* t) NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1682 | if (t == Dbg::GetDebugThread()) { |
| 1683 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 1684 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 1685 | return; |
| 1686 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1687 | mirror::Object* peer = t->GetPeer(); |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1688 | if (IsInDesiredThreadGroup(peer)) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1689 | thread_ids_.push_back(gRegistry->Add(peer)); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1693 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1694 | bool IsInDesiredThreadGroup(mirror::Object* peer) |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1695 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1696 | // peer might be NULL if the thread is still starting up. |
| 1697 | if (peer == NULL) { |
| 1698 | // We can't tell the debugger about this thread yet. |
| 1699 | // TODO: if we identified threads to the debugger by their Thread* |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1700 | // rather than their peer's mirror::Object*, we could fix this. |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1701 | // Doing so might help us report ZOMBIE threads too. |
| 1702 | return false; |
| 1703 | } |
jeffhao | c1e0490 | 2012-12-13 12:41:10 -0800 | [diff] [blame] | 1704 | // Do we want threads from all thread groups? |
| 1705 | if (desired_thread_group_ == NULL) { |
| 1706 | return true; |
| 1707 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1708 | mirror::Object* group = soa_.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(peer); |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1709 | return (group == desired_thread_group_); |
| 1710 | } |
| 1711 | |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 1712 | const ScopedObjectAccessUnchecked& soa_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1713 | mirror::Object* const desired_thread_group_; |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1714 | std::vector<JDWP::ObjectId>& thread_ids_; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1715 | }; |
| 1716 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1717 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1718 | mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1719 | ThreadListVisitor tlv(soa, thread_group, thread_ids); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1720 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1721 | Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1722 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1723 | |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1724 | 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] | 1725 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1726 | mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1727 | |
| 1728 | // Get the ArrayList<ThreadGroup> "groups" out of this thread group... |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1729 | mirror::Field* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;"); |
| 1730 | mirror::Object* groups_array_list = groups_field->GetObject(thread_group); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1731 | |
| 1732 | // Get the array and size out of the ArrayList<ThreadGroup>... |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1733 | mirror::Field* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;"); |
| 1734 | mirror::Field* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I"); |
| 1735 | mirror::ObjectArray<mirror::Object>* groups_array = |
| 1736 | array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1737 | const int32_t size = size_field->GetInt(groups_array_list); |
| 1738 | |
| 1739 | // Copy the first 'size' elements out of the array into the result. |
| 1740 | for (int32_t i = 0; i < size; ++i) { |
| 1741 | child_thread_group_ids.push_back(gRegistry->Add(groups_array->Get(i))); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1745 | static int GetStackDepth(Thread* thread) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1746 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1747 | struct CountStackDepthVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1748 | CountStackDepthVisitor(Thread* thread) |
| 1749 | : StackVisitor(thread, NULL), depth(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1750 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 1751 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1752 | // annotalysis. |
| 1753 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1754 | if (!GetMethod()->IsRuntimeMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1755 | ++depth; |
| 1756 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1757 | return true; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1758 | } |
| 1759 | size_t depth; |
| 1760 | }; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1761 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1762 | CountStackDepthVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1763 | visitor.WalkStack(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1764 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1767 | JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1768 | ScopedObjectAccess soa(Thread::Current()); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1769 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1770 | Thread* thread; |
| 1771 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1772 | if (error != JDWP::ERR_NONE) { |
| 1773 | return error; |
| 1774 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 1775 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 1776 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 1777 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1778 | result = GetStackDepth(thread); |
| 1779 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 1782 | JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame, |
| 1783 | size_t frame_count, JDWP::ExpandBuf* buf) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1784 | class GetFrameVisitor : public StackVisitor { |
| 1785 | public: |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1786 | 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] | 1787 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1788 | : StackVisitor(thread, NULL), depth_(0), |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1789 | start_frame_(start_frame), frame_count_(frame_count), buf_(buf) { |
| 1790 | expandBufAdd4BE(buf_, frame_count_); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1791 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1792 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1793 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1794 | // annotalysis. |
| 1795 | virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1796 | if (GetMethod()->IsRuntimeMethod()) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1797 | 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] | 1798 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1799 | if (depth_ >= start_frame_ + frame_count_) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1800 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1801 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1802 | if (depth_ >= start_frame_) { |
| 1803 | JDWP::FrameId frame_id(GetFrameId()); |
| 1804 | JDWP::JdwpLocation location; |
| 1805 | SetLocation(location, GetMethod(), GetDexPc()); |
Elliott Hughes | 7baf96f | 2012-06-22 16:33:50 -0700 | [diff] [blame] | 1806 | VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3lld ", depth_, frame_id) << location; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1807 | expandBufAdd8BE(buf_, frame_id); |
| 1808 | expandBufAddLocation(buf_, location); |
| 1809 | } |
| 1810 | ++depth_; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1811 | return true; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1812 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1813 | |
| 1814 | private: |
| 1815 | size_t depth_; |
| 1816 | const size_t start_frame_; |
| 1817 | const size_t frame_count_; |
| 1818 | JDWP::ExpandBuf* buf_; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1819 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1820 | |
| 1821 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1822 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1823 | Thread* thread; |
| 1824 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1825 | if (error != JDWP::ERR_NONE) { |
| 1826 | return error; |
| 1827 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 1828 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 1829 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 1830 | } |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1831 | GetFrameVisitor visitor(thread, start_frame, frame_count, buf); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1832 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1833 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 1837 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1838 | return gRegistry->Add(soa.Self()->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1841 | void Dbg::SuspendVM() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1842 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | void Dbg::ResumeVM() { |
Elliott Hughes | c61a267 | 2012-06-21 14:52:29 -0700 | [diff] [blame] | 1846 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1849 | JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1850 | ScopedLocalRef<jobject> peer(Thread::Current()->GetJniEnv(), NULL); |
| 1851 | { |
| 1852 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1853 | peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id))); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1854 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1855 | if (peer.get() == NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1856 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 1857 | } |
| 1858 | // Suspend thread to build stack trace. |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 1859 | bool timed_out; |
| 1860 | Thread* thread = Thread::SuspendForDebugger(peer.get(), request_suspension, &timed_out); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1861 | if (thread != NULL) { |
| 1862 | return JDWP::ERR_NONE; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 1863 | } else if (timed_out) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1864 | return JDWP::ERR_INTERNAL; |
| 1865 | } else { |
| 1866 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 1867 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1870 | void Dbg::ResumeThread(JDWP::ObjectId thread_id) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1871 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1872 | mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1873 | Thread* thread; |
| 1874 | { |
| 1875 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 1876 | thread = Thread::FromManagedThread(soa, peer); |
| 1877 | } |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1878 | if (thread == NULL) { |
| 1879 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 1880 | return; |
| 1881 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1882 | bool needs_resume; |
| 1883 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1884 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1885 | needs_resume = thread->GetSuspendCount() > 0; |
| 1886 | } |
| 1887 | if (needs_resume) { |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1888 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
| 1889 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1893 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1896 | struct GetThisVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1897 | GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1898 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1899 | : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1900 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1901 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1902 | // annotalysis. |
| 1903 | virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1904 | if (frame_id != GetFrameId()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1905 | return true; // continue |
| 1906 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1907 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1908 | if (m->IsNative() || m->IsStatic()) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1909 | this_object = NULL; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1910 | } else { |
| 1911 | uint16_t reg = DemangleSlot(0, m); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1912 | this_object = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg)); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1913 | } |
| 1914 | return false; |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1915 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1916 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1917 | mirror::Object* this_object; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1918 | JDWP::FrameId frame_id; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1919 | }; |
| 1920 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1921 | 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] | 1922 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1923 | // 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] | 1924 | if (m->IsNative() || m->IsStatic()) { |
| 1925 | return NULL; |
| 1926 | } |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1927 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1928 | UniquePtr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1929 | GetThisVisitor visitor(self, context.get(), frame_id); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1930 | visitor.WalkStack(); |
| 1931 | return visitor.this_object; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1932 | } |
| 1933 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1934 | JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, |
| 1935 | JDWP::ObjectId* result) { |
| 1936 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1937 | Thread* thread; |
| 1938 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1939 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1940 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 1941 | if (error != JDWP::ERR_NONE) { |
| 1942 | return error; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1943 | } |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 1944 | if (!IsSuspendedForDebugger(soa, thread)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1945 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 1946 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1947 | } |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1948 | UniquePtr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1949 | GetThisVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1950 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1951 | *result = gRegistry->Add(visitor.this_object); |
| 1952 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1953 | } |
| 1954 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1955 | 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] | 1956 | uint8_t* buf, size_t width) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1957 | struct GetLocalVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1958 | GetLocalVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id, int slot, |
| 1959 | JDWP::JdwpTag tag, uint8_t* buf, size_t width) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1960 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1961 | : StackVisitor(thread, context), frame_id_(frame_id), slot_(slot), tag_(tag), |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 1962 | buf_(buf), width_(width) {} |
| 1963 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1964 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1965 | // annotalysis. |
| 1966 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1967 | if (GetFrameId() != frame_id_) { |
| 1968 | return true; // Not our frame, carry on. |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1969 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1970 | // 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] | 1971 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1972 | uint16_t reg = DemangleSlot(slot_, m); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1973 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1974 | switch (tag_) { |
| 1975 | case JDWP::JT_BOOLEAN: |
| 1976 | { |
| 1977 | CHECK_EQ(width_, 1U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1978 | uint32_t intVal = GetVReg(m, reg, kIntVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1979 | VLOG(jdwp) << "get boolean local " << reg << " = " << intVal; |
| 1980 | JDWP::Set1(buf_+1, intVal != 0); |
| 1981 | } |
| 1982 | break; |
| 1983 | case JDWP::JT_BYTE: |
| 1984 | { |
| 1985 | CHECK_EQ(width_, 1U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1986 | uint32_t intVal = GetVReg(m, reg, kIntVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1987 | VLOG(jdwp) << "get byte local " << reg << " = " << intVal; |
| 1988 | JDWP::Set1(buf_+1, intVal); |
| 1989 | } |
| 1990 | break; |
| 1991 | case JDWP::JT_SHORT: |
| 1992 | case JDWP::JT_CHAR: |
| 1993 | { |
| 1994 | CHECK_EQ(width_, 2U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1995 | uint32_t intVal = GetVReg(m, reg, kIntVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1996 | VLOG(jdwp) << "get short/char local " << reg << " = " << intVal; |
| 1997 | JDWP::Set2BE(buf_+1, intVal); |
| 1998 | } |
| 1999 | break; |
| 2000 | case JDWP::JT_INT: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2001 | { |
| 2002 | CHECK_EQ(width_, 4U); |
| 2003 | uint32_t intVal = GetVReg(m, reg, kIntVReg); |
| 2004 | VLOG(jdwp) << "get int local " << reg << " = " << intVal; |
| 2005 | JDWP::Set4BE(buf_+1, intVal); |
| 2006 | } |
| 2007 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2008 | case JDWP::JT_FLOAT: |
| 2009 | { |
| 2010 | CHECK_EQ(width_, 4U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2011 | uint32_t intVal = GetVReg(m, reg, kFloatVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2012 | VLOG(jdwp) << "get int/float local " << reg << " = " << intVal; |
| 2013 | JDWP::Set4BE(buf_+1, intVal); |
| 2014 | } |
| 2015 | break; |
| 2016 | case JDWP::JT_ARRAY: |
| 2017 | { |
| 2018 | CHECK_EQ(width_, sizeof(JDWP::ObjectId)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2019 | mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg)); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2020 | VLOG(jdwp) << "get array local " << reg << " = " << o; |
| 2021 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) { |
| 2022 | LOG(FATAL) << "Register " << reg << " expected to hold array: " << o; |
| 2023 | } |
| 2024 | JDWP::SetObjectId(buf_+1, gRegistry->Add(o)); |
| 2025 | } |
| 2026 | break; |
| 2027 | case JDWP::JT_CLASS_LOADER: |
| 2028 | case JDWP::JT_CLASS_OBJECT: |
| 2029 | case JDWP::JT_OBJECT: |
| 2030 | case JDWP::JT_STRING: |
| 2031 | case JDWP::JT_THREAD: |
| 2032 | case JDWP::JT_THREAD_GROUP: |
| 2033 | { |
| 2034 | CHECK_EQ(width_, sizeof(JDWP::ObjectId)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2035 | mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg)); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2036 | VLOG(jdwp) << "get object local " << reg << " = " << o; |
| 2037 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) { |
| 2038 | LOG(FATAL) << "Register " << reg << " expected to hold object: " << o; |
| 2039 | } |
| 2040 | tag_ = TagFromObject(o); |
| 2041 | JDWP::SetObjectId(buf_+1, gRegistry->Add(o)); |
| 2042 | } |
| 2043 | break; |
| 2044 | case JDWP::JT_DOUBLE: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2045 | { |
| 2046 | CHECK_EQ(width_, 8U); |
| 2047 | uint32_t lo = GetVReg(m, reg, kDoubleLoVReg); |
| 2048 | uint64_t hi = GetVReg(m, reg + 1, kDoubleHiVReg); |
| 2049 | uint64_t longVal = (hi << 32) | lo; |
| 2050 | VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal; |
| 2051 | JDWP::Set8BE(buf_+1, longVal); |
| 2052 | } |
| 2053 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2054 | case JDWP::JT_LONG: |
| 2055 | { |
| 2056 | CHECK_EQ(width_, 8U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2057 | uint32_t lo = GetVReg(m, reg, kLongLoVReg); |
| 2058 | uint64_t hi = GetVReg(m, reg + 1, kLongHiVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2059 | uint64_t longVal = (hi << 32) | lo; |
| 2060 | VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal; |
| 2061 | JDWP::Set8BE(buf_+1, longVal); |
| 2062 | } |
| 2063 | break; |
| 2064 | default: |
| 2065 | LOG(FATAL) << "Unknown tag " << tag_; |
| 2066 | break; |
| 2067 | } |
| 2068 | |
| 2069 | // Prepend tag, which may have been updated. |
| 2070 | JDWP::Set1(buf_, tag_); |
| 2071 | return false; |
| 2072 | } |
| 2073 | |
| 2074 | const JDWP::FrameId frame_id_; |
| 2075 | const int slot_; |
| 2076 | JDWP::JdwpTag tag_; |
| 2077 | uint8_t* const buf_; |
| 2078 | const size_t width_; |
| 2079 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2080 | |
| 2081 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2082 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2083 | Thread* thread; |
| 2084 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 2085 | if (error != JDWP::ERR_NONE) { |
| 2086 | return; |
| 2087 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2088 | UniquePtr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2089 | GetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, buf, width); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2090 | visitor.WalkStack(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 2093 | 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] | 2094 | uint64_t value, size_t width) { |
| 2095 | struct SetLocalVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2096 | SetLocalVisitor(Thread* thread, Context* context, |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2097 | JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint64_t value, |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2098 | size_t width) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2099 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2100 | : StackVisitor(thread, context), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2101 | frame_id_(frame_id), slot_(slot), tag_(tag), value_(value), width_(width) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2102 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2103 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2104 | // annotalysis. |
| 2105 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2106 | if (GetFrameId() != frame_id_) { |
| 2107 | return true; // Not our frame, carry on. |
| 2108 | } |
| 2109 | // 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] | 2110 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2111 | uint16_t reg = DemangleSlot(slot_, m); |
| 2112 | |
| 2113 | switch (tag_) { |
| 2114 | case JDWP::JT_BOOLEAN: |
| 2115 | case JDWP::JT_BYTE: |
| 2116 | CHECK_EQ(width_, 1U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2117 | SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2118 | break; |
| 2119 | case JDWP::JT_SHORT: |
| 2120 | case JDWP::JT_CHAR: |
| 2121 | CHECK_EQ(width_, 2U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2122 | SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2123 | break; |
| 2124 | case JDWP::JT_INT: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2125 | CHECK_EQ(width_, 4U); |
| 2126 | SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg); |
| 2127 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2128 | case JDWP::JT_FLOAT: |
| 2129 | CHECK_EQ(width_, 4U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2130 | SetVReg(m, reg, static_cast<uint32_t>(value_), kFloatVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2131 | break; |
| 2132 | case JDWP::JT_ARRAY: |
| 2133 | case JDWP::JT_OBJECT: |
| 2134 | case JDWP::JT_STRING: |
| 2135 | { |
| 2136 | CHECK_EQ(width_, sizeof(JDWP::ObjectId)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2137 | mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value_)); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 2138 | if (o == ObjectRegistry::kInvalidObject) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2139 | UNIMPLEMENTED(FATAL) << "return an error code when given an invalid object to store"; |
| 2140 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2141 | 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] | 2142 | } |
| 2143 | break; |
| 2144 | case JDWP::JT_DOUBLE: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2145 | CHECK_EQ(width_, 8U); |
| 2146 | SetVReg(m, reg, static_cast<uint32_t>(value_), kDoubleLoVReg); |
| 2147 | SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kDoubleHiVReg); |
| 2148 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2149 | case JDWP::JT_LONG: |
| 2150 | CHECK_EQ(width_, 8U); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2151 | SetVReg(m, reg, static_cast<uint32_t>(value_), kLongLoVReg); |
| 2152 | SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kLongHiVReg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2153 | break; |
| 2154 | default: |
| 2155 | LOG(FATAL) << "Unknown tag " << tag_; |
| 2156 | break; |
| 2157 | } |
| 2158 | return false; |
| 2159 | } |
| 2160 | |
| 2161 | const JDWP::FrameId frame_id_; |
| 2162 | const int slot_; |
| 2163 | const JDWP::JdwpTag tag_; |
| 2164 | const uint64_t value_; |
| 2165 | const size_t width_; |
| 2166 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2167 | |
| 2168 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2169 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2170 | Thread* thread; |
| 2171 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 2172 | if (error != JDWP::ERR_NONE) { |
| 2173 | return; |
| 2174 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2175 | UniquePtr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2176 | SetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, value, width); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2177 | visitor.WalkStack(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2180 | void Dbg::PostLocationEvent(const mirror::AbstractMethod* m, int dex_pc, mirror::Object* this_object, int event_flags) { |
| 2181 | mirror::Class* c = m->GetDeclaringClass(); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2182 | |
| 2183 | JDWP::JdwpLocation location; |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 2184 | location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 2185 | location.class_id = gRegistry->AddRefType(c); |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 2186 | location.method_id = ToMethodId(m); |
Elliott Hughes | 972a47b | 2012-02-21 18:16:06 -0800 | [diff] [blame] | 2187 | location.dex_pc = m->IsNative() ? -1 : dex_pc; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2188 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 2189 | // If 'this_object' isn't already in the registry, we know that we're not looking for it, |
| 2190 | // so there's no point adding it to the registry and burning through ids. |
| 2191 | JDWP::ObjectId this_id = 0; |
| 2192 | if (gRegistry->Contains(this_object)) { |
| 2193 | this_id = gRegistry->Add(this_object); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2194 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 2195 | gJdwpState->PostLocationEvent(&location, this_id, event_flags); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2198 | void Dbg::PostException(Thread* thread, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2199 | JDWP::FrameId throw_frame_id, mirror::AbstractMethod* throw_method, |
| 2200 | uint32_t throw_dex_pc, mirror::AbstractMethod* catch_method, |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 2201 | uint32_t catch_dex_pc, mirror::Throwable* exception_object) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2202 | if (!IsDebuggerActive()) { |
Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 2203 | return; |
| 2204 | } |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2205 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2206 | JDWP::JdwpLocation throw_location; |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2207 | SetLocation(throw_location, throw_method, throw_dex_pc); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2208 | JDWP::JdwpLocation catch_location; |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2209 | SetLocation(catch_location, catch_method, catch_dex_pc); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2210 | |
| 2211 | // We need 'this' for InstanceOnly filters. |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2212 | UniquePtr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2213 | GetThisVisitor visitor(thread, context.get(), throw_frame_id); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2214 | visitor.WalkStack(); |
| 2215 | JDWP::ObjectId this_id = gRegistry->Add(visitor.this_object); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2216 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 2217 | JDWP::ObjectId exception_id = gRegistry->Add(exception_object); |
| 2218 | JDWP::RefTypeId exception_class_id = gRegistry->AddRefType(exception_object->GetClass()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2219 | |
| 2220 | 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] | 2221 | } |
| 2222 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2223 | void Dbg::PostClassPrepare(mirror::Class* c) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2224 | if (!IsDebuggerActive()) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2225 | return; |
| 2226 | } |
| 2227 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2228 | // OLD-TODO - we currently always send both "verified" and "prepared" since |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2229 | // debuggers seem to like that. There might be some advantage to honesty, |
| 2230 | // since the class may not yet be verified. |
| 2231 | int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 2232 | JDWP::JdwpTypeTag tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 2233 | gJdwpState->PostClassPrepare(tag, gRegistry->Add(c), ClassHelper(c).GetDescriptor(), state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2234 | } |
| 2235 | |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2236 | void Dbg::UpdateDebugger(int32_t dex_pc, Thread* self) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2237 | if (!IsDebuggerActive() || dex_pc == -2 /* fake method exit */) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2238 | return; |
| 2239 | } |
| 2240 | |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2241 | size_t frame_id; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2242 | mirror::AbstractMethod* m = self->GetCurrentMethod(NULL, &frame_id); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2243 | //LOG(INFO) << "UpdateDebugger " << PrettyMethod(m) << "@" << dex_pc << " frame " << frame_id; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2244 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2245 | if (dex_pc == -1) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 2246 | // We use a pc of -1 to represent method entry, since we might branch back to pc 0 later. |
| 2247 | // This means that for this special notification, there can't be anything else interesting |
| 2248 | // going on, so we're done already. |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2249 | Dbg::PostLocationEvent(m, 0, GetThis(self, m, frame_id), kMethodEntry); |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 2250 | return; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2251 | } |
| 2252 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 2253 | int event_flags = 0; |
| 2254 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2255 | if (IsBreakpoint(m, dex_pc)) { |
| 2256 | event_flags |= kBreakpoint; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2257 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2258 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2259 | { |
| 2260 | // If the debugger is single-stepping one of our threads, check to |
| 2261 | // see if we're that thread and we've reached a step point. |
| 2262 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| 2263 | if (gSingleStepControl.is_active && gSingleStepControl.thread == self) { |
| 2264 | CHECK(!m->IsNative()); |
| 2265 | if (gSingleStepControl.step_depth == JDWP::SD_INTO) { |
| 2266 | // Step into method calls. We break when the line number |
| 2267 | // or method pointer changes. If we're in SS_MIN mode, we |
| 2268 | // always stop. |
| 2269 | if (gSingleStepControl.method != m) { |
| 2270 | event_flags |= kSingleStep; |
| 2271 | VLOG(jdwp) << "SS new method"; |
| 2272 | } else if (gSingleStepControl.step_size == JDWP::SS_MIN) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2273 | event_flags |= kSingleStep; |
| 2274 | VLOG(jdwp) << "SS new instruction"; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2275 | } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) { |
| 2276 | event_flags |= kSingleStep; |
| 2277 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2278 | } |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2279 | } else if (gSingleStepControl.step_depth == JDWP::SD_OVER) { |
| 2280 | // Step over method calls. We break when the line number is |
| 2281 | // different and the frame depth is <= the original frame |
| 2282 | // depth. (We can't just compare on the method, because we |
| 2283 | // might get unrolled past it by an exception, and it's tricky |
| 2284 | // to identify recursion.) |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2285 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2286 | int stack_depth = GetStackDepth(self); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2287 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2288 | if (stack_depth < gSingleStepControl.stack_depth) { |
| 2289 | // popped up one or more frames, always trigger |
| 2290 | event_flags |= kSingleStep; |
| 2291 | VLOG(jdwp) << "SS method pop"; |
| 2292 | } else if (stack_depth == gSingleStepControl.stack_depth) { |
| 2293 | // same depth, see if we moved |
| 2294 | if (gSingleStepControl.step_size == JDWP::SS_MIN) { |
| 2295 | event_flags |= kSingleStep; |
| 2296 | VLOG(jdwp) << "SS new instruction"; |
| 2297 | } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) { |
| 2298 | event_flags |= kSingleStep; |
| 2299 | VLOG(jdwp) << "SS new line"; |
| 2300 | } |
| 2301 | } |
| 2302 | } else { |
| 2303 | CHECK_EQ(gSingleStepControl.step_depth, JDWP::SD_OUT); |
| 2304 | // Return from the current method. We break when the frame |
| 2305 | // depth pops up. |
| 2306 | |
| 2307 | // This differs from the "method exit" break in that it stops |
| 2308 | // with the PC at the next instruction in the returned-to |
| 2309 | // function, rather than the end of the returning function. |
| 2310 | |
| 2311 | int stack_depth = GetStackDepth(self); |
| 2312 | if (stack_depth < gSingleStepControl.stack_depth) { |
| 2313 | event_flags |= kSingleStep; |
| 2314 | VLOG(jdwp) << "SS method pop"; |
| 2315 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2316 | } |
| 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 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2320 | // Check to see if this is a "return" instruction. JDWP says we should |
| 2321 | // send the event *after* the code has been executed, but it also says |
| 2322 | // the location we provide is the last instruction. Since the "return" |
| 2323 | // instruction has no interesting side effects, we should be safe. |
| 2324 | // (We can't just move this down to the returnFromMethod label because |
| 2325 | // we potentially need to combine it with other events.) |
| 2326 | // We're also not supposed to generate a method exit event if the method |
| 2327 | // terminates "with a thrown exception". |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2328 | if (dex_pc >= 0) { |
| 2329 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2330 | CHECK(code_item != NULL) << PrettyMethod(m) << " @" << dex_pc; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2331 | CHECK_LT(dex_pc, static_cast<int32_t>(code_item->insns_size_in_code_units_)); |
| 2332 | if (Instruction::At(&code_item->insns_[dex_pc])->IsReturn()) { |
| 2333 | event_flags |= kMethodExit; |
| 2334 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2335 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2336 | |
| 2337 | // If there's something interesting going on, see if it matches one |
| 2338 | // of the debugger filters. |
| 2339 | if (event_flags != 0) { |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2340 | Dbg::PostLocationEvent(m, dex_pc, GetThis(self, m, frame_id), event_flags); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2344 | void Dbg::WatchLocation(const JDWP::JdwpLocation* location) { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2345 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2346 | mirror::AbstractMethod* m = FromMethodId(location->method_id); |
Elliott Hughes | 972a47b | 2012-02-21 18:16:06 -0800 | [diff] [blame] | 2347 | gBreakpoints.push_back(Breakpoint(m, location->dex_pc)); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2348 | VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " << gBreakpoints[gBreakpoints.size() - 1]; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2349 | } |
| 2350 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2351 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location) { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2352 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2353 | mirror::AbstractMethod* m = FromMethodId(location->method_id); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2354 | for (size_t i = 0; i < gBreakpoints.size(); ++i) { |
Elliott Hughes | 972a47b | 2012-02-21 18:16:06 -0800 | [diff] [blame] | 2355 | if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == location->dex_pc) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2356 | VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i]; |
| 2357 | gBreakpoints.erase(gBreakpoints.begin() + i); |
| 2358 | return; |
| 2359 | } |
| 2360 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2363 | JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2364 | JDWP::JdwpStepDepth step_depth) { |
| 2365 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2366 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2367 | Thread* thread; |
| 2368 | JDWP::JdwpError error = DecodeThread(soa, thread_id, thread); |
| 2369 | if (error != JDWP::ERR_NONE) { |
| 2370 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2371 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2372 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2373 | MutexLock mu2(soa.Self(), *Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2374 | // TODO: there's no theoretical reason why we couldn't support single-stepping |
| 2375 | // of multiple threads at once, but we never did so historically. |
| 2376 | if (gSingleStepControl.thread != NULL && thread != gSingleStepControl.thread) { |
| 2377 | LOG(WARNING) << "single-step already active for " << *gSingleStepControl.thread |
| 2378 | << "; switching to " << *thread; |
| 2379 | } |
| 2380 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2381 | // |
| 2382 | // Work out what Method* we're in, the current line number, and how deep the stack currently |
| 2383 | // is for step-out. |
| 2384 | // |
| 2385 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2386 | struct SingleStepStackVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2387 | SingleStepStackVisitor(Thread* thread) |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2388 | EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2389 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2390 | : StackVisitor(thread, NULL) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2391 | gSingleStepControl.method = NULL; |
| 2392 | gSingleStepControl.stack_depth = 0; |
| 2393 | } |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2394 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2395 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2396 | // annotalysis. |
| 2397 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2398 | Locks::breakpoint_lock_->AssertHeld(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2399 | const mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2400 | if (!m->IsRuntimeMethod()) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2401 | ++gSingleStepControl.stack_depth; |
| 2402 | if (gSingleStepControl.method == NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2403 | const mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache(); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2404 | gSingleStepControl.method = m; |
| 2405 | gSingleStepControl.line_number = -1; |
| 2406 | if (dex_cache != NULL) { |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 2407 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2408 | gSingleStepControl.line_number = dex_file.GetLineNumFromPC(m, GetDexPc()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2409 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2410 | } |
| 2411 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2412 | return true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2413 | } |
| 2414 | }; |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2415 | SingleStepStackVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2416 | visitor.WalkStack(); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2417 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2418 | // |
| 2419 | // Find the dex_pc values that correspond to the current line, for line-based single-stepping. |
| 2420 | // |
| 2421 | |
| 2422 | struct DebugCallbackContext { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2423 | DebugCallbackContext() EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2424 | last_pc_valid = false; |
| 2425 | last_pc = 0; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2426 | } |
| 2427 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2428 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2429 | // annotalysis. |
| 2430 | static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) NO_THREAD_SAFETY_ANALYSIS { |
| 2431 | Locks::breakpoint_lock_->AssertHeld(Thread::Current()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2432 | DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context); |
| 2433 | if (static_cast<int32_t>(line_number) == gSingleStepControl.line_number) { |
| 2434 | if (!context->last_pc_valid) { |
| 2435 | // Everything from this address until the next line change is ours. |
| 2436 | context->last_pc = address; |
| 2437 | context->last_pc_valid = true; |
| 2438 | } |
| 2439 | // Otherwise, if we're already in a valid range for this line, |
| 2440 | // just keep going (shouldn't really happen)... |
| 2441 | } else if (context->last_pc_valid) { // and the line number is new |
| 2442 | // Add everything from the last entry up until here to the set |
| 2443 | for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) { |
| 2444 | gSingleStepControl.dex_pcs.insert(dex_pc); |
| 2445 | } |
| 2446 | context->last_pc_valid = false; |
| 2447 | } |
| 2448 | return false; // There may be multiple entries for any given line. |
| 2449 | } |
| 2450 | |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2451 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2452 | // annotalysis. |
| 2453 | ~DebugCallbackContext() NO_THREAD_SAFETY_ANALYSIS { |
| 2454 | Locks::breakpoint_lock_->AssertHeld(Thread::Current()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2455 | // If the line number was the last in the position table... |
| 2456 | if (last_pc_valid) { |
| 2457 | size_t end = MethodHelper(gSingleStepControl.method).GetCodeItem()->insns_size_in_code_units_; |
| 2458 | for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) { |
| 2459 | gSingleStepControl.dex_pcs.insert(dex_pc); |
| 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | bool last_pc_valid; |
| 2465 | uint32_t last_pc; |
| 2466 | }; |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 2467 | gSingleStepControl.dex_pcs.clear(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2468 | const mirror::AbstractMethod* m = gSingleStepControl.method; |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 2469 | if (m->IsNative()) { |
| 2470 | gSingleStepControl.line_number = -1; |
| 2471 | } else { |
| 2472 | DebugCallbackContext context; |
| 2473 | MethodHelper mh(m); |
| 2474 | mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(), |
| 2475 | DebugCallbackContext::Callback, NULL, &context); |
| 2476 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2477 | |
| 2478 | // |
| 2479 | // Everything else... |
| 2480 | // |
| 2481 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2482 | gSingleStepControl.thread = thread; |
| 2483 | gSingleStepControl.step_size = step_size; |
| 2484 | gSingleStepControl.step_depth = step_depth; |
| 2485 | gSingleStepControl.is_active = true; |
| 2486 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2487 | if (VLOG_IS_ON(jdwp)) { |
| 2488 | VLOG(jdwp) << "Single-step thread: " << *gSingleStepControl.thread; |
| 2489 | VLOG(jdwp) << "Single-step step size: " << gSingleStepControl.step_size; |
| 2490 | VLOG(jdwp) << "Single-step step depth: " << gSingleStepControl.step_depth; |
| 2491 | VLOG(jdwp) << "Single-step current method: " << PrettyMethod(gSingleStepControl.method); |
| 2492 | VLOG(jdwp) << "Single-step current line: " << gSingleStepControl.line_number; |
| 2493 | VLOG(jdwp) << "Single-step current stack depth: " << gSingleStepControl.stack_depth; |
| 2494 | VLOG(jdwp) << "Single-step dex_pc values:"; |
| 2495 | 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] | 2496 | VLOG(jdwp) << StringPrintf(" %#x", *it); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2503 | void Dbg::UnconfigureStep(JDWP::ObjectId /*thread_id*/) { |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 2504 | MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 2505 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2506 | gSingleStepControl.is_active = false; |
| 2507 | gSingleStepControl.thread = NULL; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2508 | gSingleStepControl.dex_pcs.clear(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2509 | } |
| 2510 | |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2511 | static char JdwpTagToShortyChar(JDWP::JdwpTag tag) { |
| 2512 | switch (tag) { |
| 2513 | default: |
| 2514 | LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag); |
| 2515 | |
| 2516 | // Primitives. |
| 2517 | case JDWP::JT_BYTE: return 'B'; |
| 2518 | case JDWP::JT_CHAR: return 'C'; |
| 2519 | case JDWP::JT_FLOAT: return 'F'; |
| 2520 | case JDWP::JT_DOUBLE: return 'D'; |
| 2521 | case JDWP::JT_INT: return 'I'; |
| 2522 | case JDWP::JT_LONG: return 'J'; |
| 2523 | case JDWP::JT_SHORT: return 'S'; |
| 2524 | case JDWP::JT_VOID: return 'V'; |
| 2525 | case JDWP::JT_BOOLEAN: return 'Z'; |
| 2526 | |
| 2527 | // Reference types. |
| 2528 | case JDWP::JT_ARRAY: |
| 2529 | case JDWP::JT_OBJECT: |
| 2530 | case JDWP::JT_STRING: |
| 2531 | case JDWP::JT_THREAD: |
| 2532 | case JDWP::JT_THREAD_GROUP: |
| 2533 | case JDWP::JT_CLASS_LOADER: |
| 2534 | case JDWP::JT_CLASS_OBJECT: |
| 2535 | return 'L'; |
| 2536 | } |
| 2537 | } |
| 2538 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 2539 | JDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id, |
| 2540 | JDWP::RefTypeId class_id, JDWP::MethodId method_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2541 | uint32_t arg_count, uint64_t* arg_values, |
| 2542 | JDWP::JdwpTag* arg_types, uint32_t options, |
| 2543 | JDWP::JdwpTag* pResultTag, uint64_t* pResultValue, |
| 2544 | JDWP::ObjectId* pExceptionId) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2545 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 2546 | |
| 2547 | Thread* targetThread = NULL; |
| 2548 | DebugInvokeReq* req = NULL; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2549 | Thread* self = Thread::Current(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2550 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2551 | ScopedObjectAccessUnchecked soa(self); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2552 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2553 | JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread); |
| 2554 | if (error != JDWP::ERR_NONE) { |
| 2555 | LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id; |
| 2556 | return error; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2557 | } |
| 2558 | req = targetThread->GetInvokeReq(); |
| 2559 | if (!req->ready) { |
| 2560 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 2561 | return JDWP::ERR_INVALID_THREAD; |
| 2562 | } |
| 2563 | |
| 2564 | /* |
| 2565 | * We currently have a bug where we don't successfully resume the |
| 2566 | * target thread if the suspend count is too deep. We're expected to |
| 2567 | * require one "resume" for each "suspend", but when asked to execute |
| 2568 | * a method we have to resume fully and then re-suspend it back to the |
| 2569 | * same level. (The easiest way to cause this is to type "suspend" |
| 2570 | * multiple times in jdb.) |
| 2571 | * |
| 2572 | * It's unclear what this means when the event specifies "resume all" |
| 2573 | * and some threads are suspended more deeply than others. This is |
| 2574 | * a rare problem, so for now we just prevent it from hanging forever |
| 2575 | * by rejecting the method invocation request. Without this, we will |
| 2576 | * be stuck waiting on a suspended thread. |
| 2577 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2578 | int suspend_count; |
| 2579 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2580 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2581 | suspend_count = targetThread->GetSuspendCount(); |
| 2582 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2583 | if (suspend_count > 1) { |
| 2584 | LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count; |
| 2585 | return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here. |
| 2586 | } |
| 2587 | |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2588 | JDWP::JdwpError status; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2589 | mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 2590 | if (receiver == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2591 | return JDWP::ERR_INVALID_OBJECT; |
| 2592 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2593 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2594 | mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame^] | 2595 | if (thread == ObjectRegistry::kInvalidObject) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2596 | return JDWP::ERR_INVALID_OBJECT; |
| 2597 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2598 | // TODO: check that 'thread' is actually a java.lang.Thread! |
| 2599 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2600 | mirror::Class* c = DecodeClass(class_id, status); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2601 | if (c == NULL) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2602 | return status; |
| 2603 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2604 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2605 | mirror::AbstractMethod* m = FromMethodId(method_id); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2606 | if (m->IsStatic() != (receiver == NULL)) { |
| 2607 | return JDWP::ERR_INVALID_METHODID; |
| 2608 | } |
| 2609 | if (m->IsStatic()) { |
| 2610 | if (m->GetDeclaringClass() != c) { |
| 2611 | return JDWP::ERR_INVALID_METHODID; |
| 2612 | } |
| 2613 | } else { |
| 2614 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
| 2615 | return JDWP::ERR_INVALID_METHODID; |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | // Check the argument list matches the method. |
| 2620 | MethodHelper mh(m); |
| 2621 | if (mh.GetShortyLength() - 1 != arg_count) { |
| 2622 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 2623 | } |
| 2624 | const char* shorty = mh.GetShorty(); |
| 2625 | for (size_t i = 0; i < arg_count; ++i) { |
| 2626 | if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) { |
| 2627 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | req->receiver_ = receiver; |
| 2632 | req->thread_ = thread; |
| 2633 | req->class_ = c; |
| 2634 | req->method_ = m; |
| 2635 | req->arg_count_ = arg_count; |
| 2636 | req->arg_values_ = arg_values; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2637 | req->options_ = options; |
| 2638 | req->invoke_needed_ = true; |
| 2639 | } |
| 2640 | |
| 2641 | // The fact that we've released the thread list lock is a bit risky --- if the thread goes |
| 2642 | // away we're sitting high and dry -- but we must release this before the ResumeAllThreads |
| 2643 | // call, and it's unwise to hold it during WaitForSuspend. |
| 2644 | |
| 2645 | { |
| 2646 | /* |
| 2647 | * We change our (JDWP thread) status, which should be THREAD_RUNNING, |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 2648 | * 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] | 2649 | * run out of memory. It's also a good idea to change it before locking |
| 2650 | * the invokeReq mutex, although that should never be held for long. |
| 2651 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2652 | self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2653 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2654 | VLOG(jdwp) << " Transferring control to event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2655 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2656 | MutexLock mu(self, req->lock_); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2657 | |
| 2658 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2659 | VLOG(jdwp) << " Resuming all threads"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2660 | thread_list->UndoDebuggerSuspensions(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2661 | } else { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2662 | VLOG(jdwp) << " Resuming event thread only"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2663 | thread_list->Resume(targetThread, true); |
| 2664 | } |
| 2665 | |
| 2666 | // Wait for the request to finish executing. |
| 2667 | while (req->invoke_needed_) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 2668 | req->cond_.Wait(self); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2669 | } |
| 2670 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2671 | VLOG(jdwp) << " Control has returned from event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2672 | |
| 2673 | /* wait for thread to re-suspend itself */ |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2674 | SuspendThread(thread_id, false /* request_suspension */ ); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2675 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | /* |
| 2679 | * Suspend the threads. We waited for the target thread to suspend |
| 2680 | * itself, so all we need to do is suspend the others. |
| 2681 | * |
| 2682 | * The suspendAllThreads() call will double-suspend the event thread, |
| 2683 | * so we want to resume the target thread once to keep the books straight. |
| 2684 | */ |
| 2685 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2686 | self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2687 | VLOG(jdwp) << " Suspending all threads"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2688 | thread_list->SuspendAllForDebugger(); |
| 2689 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2690 | VLOG(jdwp) << " Resuming event thread to balance the count"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2691 | thread_list->Resume(targetThread, true); |
| 2692 | } |
| 2693 | |
| 2694 | // Copy the result. |
| 2695 | *pResultTag = req->result_tag; |
| 2696 | if (IsPrimitiveTag(req->result_tag)) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2697 | *pResultValue = req->result_value.GetJ(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2698 | } else { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2699 | *pResultValue = gRegistry->Add(req->result_value.GetL()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2700 | } |
| 2701 | *pExceptionId = req->exception; |
| 2702 | return req->error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2706 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2707 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 2708 | // We can be called while an exception is pending. We need |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2709 | // to preserve that across the method invocation. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2710 | SirtRef<mirror::Throwable> old_exception(soa.Self(), soa.Self()->GetException()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2711 | soa.Self()->ClearException(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2712 | |
| 2713 | // 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] | 2714 | mirror::AbstractMethod* m = pReq->method_; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2715 | if ((pReq->options_ & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver_ != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2716 | mirror::AbstractMethod* actual_method = pReq->class_->FindVirtualMethodForVirtualOrInterface(pReq->method_); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2717 | if (actual_method != m) { |
| 2718 | VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) << " to " << PrettyMethod(actual_method); |
| 2719 | m = actual_method; |
| 2720 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2721 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2722 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2723 | CHECK(m != NULL); |
| 2724 | |
| 2725 | CHECK_EQ(sizeof(jvalue), sizeof(uint64_t)); |
| 2726 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2727 | LOG(INFO) << "self=" << soa.Self() << " pReq->receiver_=" << pReq->receiver_ << " m=" << m |
| 2728 | << " #" << pReq->arg_count_ << " " << pReq->arg_values_; |
| 2729 | pReq->result_value = InvokeWithJValues(soa, pReq->receiver_, m, |
| 2730 | reinterpret_cast<JValue*>(pReq->arg_values_)); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2731 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2732 | pReq->exception = gRegistry->Add(soa.Self()->GetException()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2733 | pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty()); |
| 2734 | if (pReq->exception != 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2735 | mirror::Object* exc = soa.Self()->GetException(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2736 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exc << " " << PrettyTypeOf(exc); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2737 | soa.Self()->ClearException(); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2738 | pReq->result_value.SetJ(0); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2739 | } else if (pReq->result_tag == JDWP::JT_OBJECT) { |
| 2740 | /* if no exception thrown, examine object result more closely */ |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2741 | JDWP::JdwpTag new_tag = TagFromObject(pReq->result_value.GetL()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2742 | if (new_tag != pReq->result_tag) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2743 | VLOG(jdwp) << " JDWP promoted result from " << pReq->result_tag << " to " << new_tag; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2744 | pReq->result_tag = new_tag; |
| 2745 | } |
| 2746 | |
| 2747 | /* |
| 2748 | * Register the object. We don't actually need an ObjectId yet, |
| 2749 | * but we do need to be sure that the GC won't move or discard the |
| 2750 | * object when we switch out of RUNNING. The ObjectId conversion |
| 2751 | * will add the object to the "do not touch" list. |
| 2752 | * |
| 2753 | * We can't use the "tracked allocation" mechanism here because |
| 2754 | * the object is going to be handed off to a different thread. |
| 2755 | */ |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2756 | gRegistry->Add(pReq->result_value.GetL()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | if (old_exception.get() != NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2760 | soa.Self()->SetException(old_exception.get()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2761 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2764 | /* |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2765 | * "buf" contains a full JDWP packet, possibly with multiple chunks. We |
| 2766 | * need to process each, accumulate the replies, and ship the whole thing |
| 2767 | * back. |
| 2768 | * |
| 2769 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 2770 | * and includes the chunk type/length, followed by the data. |
| 2771 | * |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2772 | * 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] | 2773 | * chunk. If this becomes inconvenient we will need to adapt. |
| 2774 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2775 | bool Dbg::DdmHandlePacket(const uint8_t* buf, int dataLen, uint8_t** pReplyBuf, int* pReplyLen) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2776 | CHECK_GE(dataLen, 0); |
| 2777 | |
| 2778 | Thread* self = Thread::Current(); |
| 2779 | JNIEnv* env = self->GetJniEnv(); |
| 2780 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2781 | // Create a byte[] corresponding to 'buf'. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2782 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(dataLen)); |
| 2783 | if (dataArray.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2784 | LOG(WARNING) << "byte[] allocation failed: " << dataLen; |
| 2785 | env->ExceptionClear(); |
| 2786 | return false; |
| 2787 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2788 | env->SetByteArrayRegion(dataArray.get(), 0, dataLen, reinterpret_cast<const jbyte*>(buf)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2789 | |
| 2790 | const int kChunkHdrLen = 8; |
| 2791 | |
| 2792 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2793 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 2794 | jint type = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[0])); |
| 2795 | jint length = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[4])); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2796 | jint offset = kChunkHdrLen; |
| 2797 | if (offset + length > dataLen) { |
| 2798 | LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen); |
| 2799 | return false; |
| 2800 | } |
| 2801 | |
| 2802 | // 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] | 2803 | ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 2804 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch, |
| 2805 | type, dataArray.get(), offset, length)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2806 | if (env->ExceptionCheck()) { |
| 2807 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 2808 | env->ExceptionDescribe(); |
| 2809 | env->ExceptionClear(); |
| 2810 | return false; |
| 2811 | } |
| 2812 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2813 | if (chunk.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2814 | return false; |
| 2815 | } |
| 2816 | |
| 2817 | /* |
| 2818 | * Pull the pieces out of the chunk. We copy the results into a |
| 2819 | * newly-allocated buffer that the caller can free. We don't want to |
| 2820 | * continue using the Chunk object because nothing has a reference to it. |
| 2821 | * |
| 2822 | * We could avoid this by returning type/data/offset/length and having |
| 2823 | * the caller be aware of the object lifetime issues, but that |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 2824 | * 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] | 2825 | * if we have responses for multiple chunks. |
| 2826 | * |
| 2827 | * So we're pretty much stuck with copying data around multiple times. |
| 2828 | */ |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 2829 | ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data))); |
| 2830 | length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length); |
| 2831 | offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset); |
| 2832 | 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] | 2833 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2834 | 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] | 2835 | if (length == 0 || replyData.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2836 | return false; |
| 2837 | } |
| 2838 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2839 | jsize replyLength = env->GetArrayLength(replyData.get()); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2840 | if (offset + length > replyLength) { |
| 2841 | LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength); |
| 2842 | return false; |
| 2843 | } |
| 2844 | |
| 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 | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2857 | VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%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 |