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