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