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