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