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