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