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