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; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 99 | uintptr_t raw_pc; |
| 100 | |
| 101 | int32_t LineNumber() const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 102 | return MethodHelper(method).GetLineNumFromNativePC(raw_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 | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 735 | uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 736 | Object* o = gRegistry->Get<Object*>(objectId); |
| 737 | return TagFromObject(o); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 740 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 741 | switch (tag) { |
| 742 | case JDWP::JT_VOID: |
| 743 | return 0; |
| 744 | case JDWP::JT_BYTE: |
| 745 | case JDWP::JT_BOOLEAN: |
| 746 | return 1; |
| 747 | case JDWP::JT_CHAR: |
| 748 | case JDWP::JT_SHORT: |
| 749 | return 2; |
| 750 | case JDWP::JT_FLOAT: |
| 751 | case JDWP::JT_INT: |
| 752 | return 4; |
| 753 | case JDWP::JT_ARRAY: |
| 754 | case JDWP::JT_OBJECT: |
| 755 | case JDWP::JT_STRING: |
| 756 | case JDWP::JT_THREAD: |
| 757 | case JDWP::JT_THREAD_GROUP: |
| 758 | case JDWP::JT_CLASS_LOADER: |
| 759 | case JDWP::JT_CLASS_OBJECT: |
| 760 | return sizeof(JDWP::ObjectId); |
| 761 | case JDWP::JT_DOUBLE: |
| 762 | case JDWP::JT_LONG: |
| 763 | return 8; |
| 764 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 765 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 766 | return -1; |
| 767 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 770 | JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId arrayId, int& length) { |
| 771 | JDWP::JdwpError status; |
| 772 | Array* a = DecodeArray(arrayId, status); |
| 773 | if (a == NULL) { |
| 774 | return status; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 775 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 776 | length = a->GetLength(); |
| 777 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 780 | JDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId arrayId, int offset, int count, JDWP::ExpandBuf* pReply) { |
| 781 | JDWP::JdwpError status; |
| 782 | Array* a = DecodeArray(arrayId, status); |
| 783 | if (a == NULL) { |
| 784 | return status; |
| 785 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 786 | |
| 787 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 788 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 789 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 790 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 791 | std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 792 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 793 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 794 | expandBufAdd1(pReply, tag); |
| 795 | expandBufAdd4BE(pReply, count); |
| 796 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 797 | if (IsPrimitiveTag(tag)) { |
| 798 | size_t width = GetTagWidth(tag); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 799 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 800 | if (width == 8) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 801 | 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] | 802 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 803 | } else if (width == 4) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 804 | 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] | 805 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 806 | } else if (width == 2) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 807 | 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] | 808 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 809 | } else { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 810 | 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] | 811 | memcpy(dst, &src[offset * width], count * width); |
| 812 | } |
| 813 | } else { |
| 814 | ObjectArray<Object>* oa = a->AsObjectArray<Object>(); |
| 815 | for (int i = 0; i < count; ++i) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 816 | Object* element = oa->Get(offset + i); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 817 | JDWP::JdwpTag specific_tag = (element != NULL) ? TagFromObject(element) : tag; |
| 818 | expandBufAdd1(pReply, specific_tag); |
| 819 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 820 | } |
| 821 | } |
| 822 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 823 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 826 | JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId arrayId, int offset, int count, const uint8_t* src) { |
| 827 | JDWP::JdwpError status; |
| 828 | Array* a = DecodeArray(arrayId, status); |
| 829 | if (a == NULL) { |
| 830 | return status; |
| 831 | } |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 832 | |
| 833 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 834 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 835 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 836 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 837 | std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor()); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 838 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 839 | |
| 840 | if (IsPrimitiveTag(tag)) { |
| 841 | size_t width = GetTagWidth(tag); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 842 | if (width == 8) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 843 | 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] | 844 | for (int i = 0; i < count; ++i) { |
| 845 | // Handle potentially non-aligned memory access one byte at a time for ARM's benefit. |
| 846 | uint64_t value; |
| 847 | for (size_t j = 0; j < sizeof(uint64_t); ++j) reinterpret_cast<uint8_t*>(&value)[j] = src[j]; |
| 848 | src += sizeof(uint64_t); |
| 849 | JDWP::Write8BE(&dst, value); |
| 850 | } |
| 851 | } else if (width == 4) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 852 | 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] | 853 | const uint32_t* src4 = reinterpret_cast<const uint32_t*>(src); |
| 854 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[i]); |
| 855 | } else if (width == 2) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 856 | 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] | 857 | const uint16_t* src2 = reinterpret_cast<const uint16_t*>(src); |
| 858 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[i]); |
| 859 | } else { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 860 | 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] | 861 | memcpy(&dst[offset * width], src, count * width); |
| 862 | } |
| 863 | } else { |
| 864 | ObjectArray<Object>* oa = a->AsObjectArray<Object>(); |
| 865 | for (int i = 0; i < count; ++i) { |
| 866 | JDWP::ObjectId id = JDWP::ReadObjectId(&src); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 867 | Object* o = gRegistry->Get<Object*>(id); |
| 868 | if (o == kInvalidObject) { |
| 869 | return JDWP::ERR_INVALID_OBJECT; |
| 870 | } |
| 871 | oa->Set(offset + i, o); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 875 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 878 | JDWP::ObjectId Dbg::CreateString(const std::string& str) { |
| 879 | return gRegistry->Add(String::AllocFromModifiedUtf8(str.c_str())); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 882 | JDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId classId, JDWP::ObjectId& new_object) { |
| 883 | JDWP::JdwpError status; |
| 884 | Class* c = DecodeClass(classId, status); |
| 885 | if (c == NULL) { |
| 886 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 887 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 888 | new_object = gRegistry->Add(c->AllocObject()); |
| 889 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 892 | /* |
| 893 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 894 | */ |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 895 | JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId arrayClassId, uint32_t length, JDWP::ObjectId& new_array) { |
| 896 | JDWP::JdwpError status; |
| 897 | Class* c = DecodeClass(arrayClassId, status); |
| 898 | if (c == NULL) { |
| 899 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 900 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 901 | new_array = gRegistry->Add(Array::Alloc(c, length)); |
| 902 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 906 | JDWP::JdwpError status; |
| 907 | Class* c1 = DecodeClass(instClassId, status); |
Elliott Hughes | a656a0f | 2012-02-21 18:03:44 -0800 | [diff] [blame] | 908 | CHECK(c1 != NULL); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 909 | Class* c2 = DecodeClass(classId, status); |
Elliott Hughes | a656a0f | 2012-02-21 18:03:44 -0800 | [diff] [blame] | 910 | CHECK(c2 != NULL); |
| 911 | return c1->IsAssignableFrom(c2); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 914 | static JDWP::FieldId ToFieldId(const Field* f) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 915 | #ifdef MOVING_GARBAGE_COLLECTOR |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 916 | UNIMPLEMENTED(FATAL); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 917 | #else |
| 918 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
| 919 | #endif |
| 920 | } |
| 921 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 922 | static JDWP::MethodId ToMethodId(const Method* m) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 923 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 924 | UNIMPLEMENTED(FATAL); |
| 925 | #else |
| 926 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
| 927 | #endif |
| 928 | } |
| 929 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 930 | static Field* FromFieldId(JDWP::FieldId fid) { |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 931 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 932 | UNIMPLEMENTED(FATAL); |
| 933 | #else |
| 934 | return reinterpret_cast<Field*>(static_cast<uintptr_t>(fid)); |
| 935 | #endif |
| 936 | } |
| 937 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 938 | static Method* FromMethodId(JDWP::MethodId mid) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 939 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 940 | UNIMPLEMENTED(FATAL); |
| 941 | #else |
| 942 | return reinterpret_cast<Method*>(static_cast<uintptr_t>(mid)); |
| 943 | #endif |
| 944 | } |
| 945 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 946 | static void SetLocation(JDWP::JdwpLocation& location, Method* m, uintptr_t native_pc) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 947 | if (m == NULL) { |
| 948 | memset(&location, 0, sizeof(location)); |
| 949 | } else { |
| 950 | Class* c = m->GetDeclaringClass(); |
| 951 | location.typeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 952 | location.classId = gRegistry->Add(c); |
| 953 | location.methodId = ToMethodId(m); |
Elliott Hughes | 972a47b | 2012-02-21 18:16:06 -0800 | [diff] [blame] | 954 | location.dex_pc = m->IsNative() ? -1 : m->ToDexPC(native_pc); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 955 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 956 | } |
| 957 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 958 | std::string Dbg::GetMethodName(JDWP::RefTypeId, JDWP::MethodId methodId) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 959 | Method* m = FromMethodId(methodId); |
| 960 | return MethodHelper(m).GetName(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 963 | /* |
| 964 | * Augment the access flags for synthetic methods and fields by setting |
| 965 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 966 | * flags not specified by the Java programming language. |
| 967 | */ |
| 968 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 969 | accessFlags &= kAccJavaFlagsMask; |
| 970 | if ((accessFlags & kAccSynthetic) != 0) { |
| 971 | accessFlags |= 0xf0000000; |
| 972 | } |
| 973 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 976 | static const uint16_t kEclipseWorkaroundSlot = 1000; |
| 977 | |
| 978 | /* |
| 979 | * Eclipse appears to expect that the "this" reference is in slot zero. |
| 980 | * If it's not, the "variables" display will show two copies of "this", |
| 981 | * possibly because it gets "this" from SF.ThisObject and then displays |
| 982 | * all locals with nonzero slot numbers. |
| 983 | * |
| 984 | * So, we remap the item in slot 0 to 1000, and remap "this" to zero. On |
| 985 | * SF.GetValues / SF.SetValues we map them back. |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 986 | * |
| 987 | * TODO: jdb uses the value to determine whether a variable is a local or an argument, |
| 988 | * by checking whether it's less than the number of arguments. To make that work, we'd |
| 989 | * 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] | 990 | */ |
| 991 | static uint16_t MangleSlot(uint16_t slot, const char* name) { |
| 992 | uint16_t newSlot = slot; |
| 993 | if (strcmp(name, "this") == 0) { |
| 994 | newSlot = 0; |
| 995 | } else if (slot == 0) { |
| 996 | newSlot = kEclipseWorkaroundSlot; |
| 997 | } |
| 998 | return newSlot; |
| 999 | } |
| 1000 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1001 | static uint16_t DemangleSlot(uint16_t slot, Method* m) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1002 | if (slot == kEclipseWorkaroundSlot) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1003 | return 0; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1004 | } else if (slot == 0) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1005 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
| 1006 | CHECK(code_item != NULL); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1007 | return code_item->registers_size_ - code_item->ins_size_; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1008 | } |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1009 | return slot; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1012 | JDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId classId, bool with_generic, JDWP::ExpandBuf* pReply) { |
| 1013 | JDWP::JdwpError status; |
| 1014 | Class* c = DecodeClass(classId, status); |
| 1015 | if (c == NULL) { |
| 1016 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1017 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1018 | |
| 1019 | size_t instance_field_count = c->NumInstanceFields(); |
| 1020 | size_t static_field_count = c->NumStaticFields(); |
| 1021 | |
| 1022 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 1023 | |
| 1024 | for (size_t i = 0; i < instance_field_count + static_field_count; ++i) { |
| 1025 | 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] | 1026 | FieldHelper fh(f); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1027 | expandBufAddFieldId(pReply, ToFieldId(f)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1028 | expandBufAddUtf8String(pReply, fh.GetName()); |
| 1029 | expandBufAddUtf8String(pReply, fh.GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1030 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1031 | static const char genericSignature[1] = ""; |
| 1032 | expandBufAddUtf8String(pReply, genericSignature); |
| 1033 | } |
| 1034 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 1035 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1036 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1037 | } |
| 1038 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1039 | JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId classId, bool with_generic, JDWP::ExpandBuf* pReply) { |
| 1040 | JDWP::JdwpError status; |
| 1041 | Class* c = DecodeClass(classId, status); |
| 1042 | if (c == NULL) { |
| 1043 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1044 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1045 | |
| 1046 | size_t direct_method_count = c->NumDirectMethods(); |
| 1047 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 1048 | |
| 1049 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 1050 | |
| 1051 | for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) { |
| 1052 | 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] | 1053 | MethodHelper mh(m); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1054 | expandBufAddMethodId(pReply, ToMethodId(m)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1055 | expandBufAddUtf8String(pReply, mh.GetName()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1056 | expandBufAddUtf8String(pReply, mh.GetSignature()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1057 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1058 | static const char genericSignature[1] = ""; |
| 1059 | expandBufAddUtf8String(pReply, genericSignature); |
| 1060 | } |
| 1061 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 1062 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1063 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1066 | JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId classId, JDWP::ExpandBuf* pReply) { |
| 1067 | JDWP::JdwpError status; |
| 1068 | Class* c = DecodeClass(classId, status); |
| 1069 | if (c == NULL) { |
| 1070 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1071 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1072 | |
| 1073 | ClassHelper kh(c); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 1074 | size_t interface_count = kh.NumDirectInterfaces(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1075 | expandBufAdd4BE(pReply, interface_count); |
| 1076 | for (size_t i = 0; i < interface_count; ++i) { |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 1077 | expandBufAddRefTypeId(pReply, gRegistry->Add(kh.GetDirectInterface(i))); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1078 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1079 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1082 | void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1083 | struct DebugCallbackContext { |
| 1084 | int numItems; |
| 1085 | JDWP::ExpandBuf* pReply; |
| 1086 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1087 | static bool Callback(void* context, uint32_t address, uint32_t line_number) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1088 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1089 | expandBufAdd8BE(pContext->pReply, address); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1090 | expandBufAdd4BE(pContext->pReply, line_number); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1091 | pContext->numItems++; |
| 1092 | return true; |
| 1093 | } |
| 1094 | }; |
| 1095 | |
| 1096 | Method* m = FromMethodId(methodId); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1097 | MethodHelper mh(m); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1098 | uint64_t start, end; |
| 1099 | if (m->IsNative()) { |
| 1100 | start = -1; |
| 1101 | end = -1; |
| 1102 | } else { |
| 1103 | start = 0; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1104 | // TODO: what are the units supposed to be? *2? |
| 1105 | end = mh.GetCodeItem()->insns_size_in_code_units_; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | expandBufAdd8BE(pReply, start); |
| 1109 | expandBufAdd8BE(pReply, end); |
| 1110 | |
| 1111 | // Add numLines later |
| 1112 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 1113 | expandBufAdd4BE(pReply, 0); |
| 1114 | |
| 1115 | DebugCallbackContext context; |
| 1116 | context.numItems = 0; |
| 1117 | context.pReply = pReply; |
| 1118 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1119 | mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(), |
| 1120 | DebugCallbackContext::Callback, NULL, &context); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1121 | |
| 1122 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1125 | 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] | 1126 | struct DebugCallbackContext { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1127 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1128 | size_t variable_count; |
| 1129 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1130 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1131 | 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] | 1132 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1133 | |
Elliott Hughes | ad3da69 | 2012-02-24 16:51:35 -0800 | [diff] [blame] | 1134 | 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] | 1135 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1136 | slot = MangleSlot(slot, name); |
| 1137 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1138 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 1139 | expandBufAddUtf8String(pContext->pReply, name); |
| 1140 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1141 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1142 | expandBufAddUtf8String(pContext->pReply, signature); |
| 1143 | } |
| 1144 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 1145 | expandBufAdd4BE(pContext->pReply, slot); |
| 1146 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1147 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1148 | } |
| 1149 | }; |
| 1150 | |
| 1151 | Method* m = FromMethodId(methodId); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1152 | MethodHelper mh(m); |
| 1153 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1154 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1155 | // arg_count considers doubles and longs to take 2 units. |
| 1156 | // variable_count considers everything to take 1 unit. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1157 | std::string shorty(mh.GetShorty()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1158 | expandBufAdd4BE(pReply, m->NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1159 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1160 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1161 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1162 | expandBufAdd4BE(pReply, 0); |
| 1163 | |
| 1164 | DebugCallbackContext context; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1165 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1166 | context.variable_count = 0; |
| 1167 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1168 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1169 | mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL, |
| 1170 | DebugCallbackContext::Callback, &context); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1171 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1172 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1175 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId fieldId) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1176 | return BasicTagFromDescriptor(FieldHelper(FromFieldId(fieldId)).GetTypeDescriptor()); |
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::GetStaticFieldBasicTag(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 | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1183 | static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId refTypeId, JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply, bool is_static) { |
| 1184 | JDWP::JdwpError status; |
| 1185 | Class* c = DecodeClass(refTypeId, status); |
| 1186 | if (refTypeId != 0 && c == NULL) { |
| 1187 | return status; |
| 1188 | } |
| 1189 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1190 | Object* o = gRegistry->Get<Object*>(objectId); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1191 | if ((!is_static && o == NULL) || o == kInvalidObject) { |
| 1192 | return JDWP::ERR_INVALID_OBJECT; |
| 1193 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1194 | Field* f = FromFieldId(fieldId); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1195 | |
| 1196 | Class* receiver_class = c; |
| 1197 | if (receiver_class == NULL && o != NULL) { |
| 1198 | receiver_class = o->GetClass(); |
| 1199 | } |
| 1200 | // TODO: should we give up now if receiver_class is NULL? |
| 1201 | if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) { |
| 1202 | LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1203 | return JDWP::ERR_INVALID_FIELDID; |
| 1204 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1205 | |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1206 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1207 | // TODO: should we change the tests and check both? |
| 1208 | if (is_static) { |
| 1209 | if (!f->IsStatic()) { |
| 1210 | return JDWP::ERR_INVALID_FIELDID; |
| 1211 | } |
| 1212 | } else { |
| 1213 | if (f->IsStatic()) { |
| 1214 | LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f); |
| 1215 | o = NULL; |
| 1216 | } |
| 1217 | } |
| 1218 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1219 | JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1220 | |
| 1221 | if (IsPrimitiveTag(tag)) { |
| 1222 | expandBufAdd1(pReply, tag); |
| 1223 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 1224 | expandBufAdd1(pReply, f->Get32(o)); |
| 1225 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 1226 | expandBufAdd2BE(pReply, f->Get32(o)); |
| 1227 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 1228 | expandBufAdd4BE(pReply, f->Get32(o)); |
| 1229 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1230 | expandBufAdd8BE(pReply, f->Get64(o)); |
| 1231 | } else { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1232 | LOG(FATAL) << "Unknown tag: " << tag; |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1233 | } |
| 1234 | } else { |
| 1235 | Object* value = f->GetObject(o); |
| 1236 | expandBufAdd1(pReply, TagFromObject(value)); |
| 1237 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 1238 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1239 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1242 | 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] | 1243 | return GetFieldValueImpl(0, objectId, fieldId, pReply, false); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1246 | JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 1247 | return GetFieldValueImpl(refTypeId, 0, fieldId, pReply, true); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | 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] | 1251 | Object* o = gRegistry->Get<Object*>(objectId); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1252 | if ((!is_static && o == NULL) || o == kInvalidObject) { |
| 1253 | return JDWP::ERR_INVALID_OBJECT; |
| 1254 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1255 | Field* f = FromFieldId(fieldId); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1256 | |
| 1257 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1258 | // TODO: should we change the tests and check both? |
| 1259 | if (is_static) { |
| 1260 | if (!f->IsStatic()) { |
| 1261 | return JDWP::ERR_INVALID_FIELDID; |
| 1262 | } |
| 1263 | } else { |
| 1264 | if (f->IsStatic()) { |
| 1265 | LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f); |
| 1266 | o = NULL; |
| 1267 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1268 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1269 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1270 | JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1271 | |
| 1272 | if (IsPrimitiveTag(tag)) { |
| 1273 | if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1274 | CHECK_EQ(width, 8); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1275 | f->Set64(o, value); |
| 1276 | } else { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1277 | CHECK_LE(width, 4); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1278 | f->Set32(o, value); |
| 1279 | } |
| 1280 | } else { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1281 | Object* v = gRegistry->Get<Object*>(value); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1282 | if (v == kInvalidObject) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1283 | return JDWP::ERR_INVALID_OBJECT; |
| 1284 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1285 | if (v != NULL) { |
| 1286 | Class* field_type = FieldHelper(f).GetType(); |
| 1287 | if (!field_type->IsAssignableFrom(v->GetClass())) { |
| 1288 | return JDWP::ERR_INVALID_OBJECT; |
| 1289 | } |
| 1290 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1291 | f->SetObject(o, v); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1292 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1293 | |
| 1294 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1297 | JDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width) { |
| 1298 | return SetFieldValueImpl(objectId, fieldId, value, width, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1301 | JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId fieldId, uint64_t value, int width) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1302 | return SetFieldValueImpl(0, fieldId, value, width, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1305 | std::string Dbg::StringToUtf8(JDWP::ObjectId strId) { |
| 1306 | String* s = gRegistry->Get<String*>(strId); |
| 1307 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1310 | bool Dbg::GetThreadName(JDWP::ObjectId threadId, std::string& name) { |
| 1311 | ScopedThreadListLock thread_list_lock; |
| 1312 | Thread* thread = DecodeThread(threadId); |
| 1313 | if (thread == NULL) { |
| 1314 | return false; |
| 1315 | } |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 1316 | thread->GetThreadName(name); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1317 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1320 | JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId threadId, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1321 | Object* thread = gRegistry->Get<Object*>(threadId); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1322 | if (thread == kInvalidObject) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1323 | return JDWP::ERR_INVALID_OBJECT; |
| 1324 | } |
| 1325 | |
| 1326 | // Okay, so it's an object, but is it actually a thread? |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1327 | if (DecodeThread(threadId) == NULL) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1328 | return JDWP::ERR_INVALID_THREAD; |
| 1329 | } |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1330 | |
| 1331 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Thread;"); |
| 1332 | CHECK(c != NULL); |
| 1333 | Field* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;"); |
| 1334 | CHECK(f != NULL); |
| 1335 | Object* group = f->GetObject(thread); |
| 1336 | CHECK(group != NULL); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1337 | JDWP::ObjectId thread_group_id = gRegistry->Add(group); |
| 1338 | |
| 1339 | expandBufAddObjectId(pReply, thread_group_id); |
| 1340 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1343 | std::string Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) { |
| 1344 | Object* thread_group = gRegistry->Get<Object*>(threadGroupId); |
| 1345 | CHECK(thread_group != NULL); |
| 1346 | |
| 1347 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1348 | CHECK(c != NULL); |
| 1349 | Field* f = c->FindInstanceField("name", "Ljava/lang/String;"); |
| 1350 | CHECK(f != NULL); |
| 1351 | String* s = reinterpret_cast<String*>(f->GetObject(thread_group)); |
| 1352 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1356 | Object* thread_group = gRegistry->Get<Object*>(threadGroupId); |
| 1357 | CHECK(thread_group != NULL); |
| 1358 | |
| 1359 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1360 | CHECK(c != NULL); |
| 1361 | Field* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;"); |
| 1362 | CHECK(f != NULL); |
| 1363 | Object* parent = f->GetObject(thread_group); |
| 1364 | return gRegistry->Add(parent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 1368 | return gRegistry->Add(Thread::GetSystemThreadGroup()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | JDWP::ObjectId Dbg::GetMainThreadGroupId() { |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 1372 | return gRegistry->Add(Thread::GetMainThreadGroup()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1375 | bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, JDWP::JdwpThreadStatus* pThreadStatus, JDWP::JdwpSuspendStatus* pSuspendStatus) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1376 | ScopedThreadListLock thread_list_lock; |
| 1377 | |
| 1378 | Thread* thread = DecodeThread(threadId); |
| 1379 | if (thread == NULL) { |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
Elliott Hughes | 3ce4b26 | 2012-02-24 11:24:02 -0800 | [diff] [blame] | 1383 | // TODO: if we're in Thread.sleep(long), we should return TS_SLEEPING, |
| 1384 | // even if it's implemented using Object.wait(long). |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1385 | switch (thread->GetState()) { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 1386 | case kTerminated: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1387 | case kRunnable: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1388 | case kTimedWaiting: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1389 | case kBlocked: *pThreadStatus = JDWP::TS_MONITOR; break; |
| 1390 | case kWaiting: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1391 | case kStarting: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1392 | case kNative: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1393 | case kVmWait: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1394 | case kSuspended: *pThreadStatus = JDWP::TS_RUNNING; break; |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 1395 | // 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] | 1396 | } |
| 1397 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1398 | *pSuspendStatus = (thread->IsSuspended() ? JDWP::SUSPEND_STATUS_SUSPENDED : JDWP::SUSPEND_STATUS_NOT_SUSPENDED); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1399 | |
| 1400 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1403 | JDWP::JdwpError Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId, JDWP::ExpandBuf* pReply) { |
| 1404 | Thread* thread = DecodeThread(threadId); |
| 1405 | if (thread == NULL) { |
| 1406 | return JDWP::ERR_INVALID_THREAD; |
| 1407 | } |
| 1408 | expandBufAdd4BE(pReply, thread->GetSuspendCount()); |
| 1409 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | bool Dbg::ThreadExists(JDWP::ObjectId threadId) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1413 | return DecodeThread(threadId) != NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | bool Dbg::IsSuspended(JDWP::ObjectId threadId) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1417 | return DecodeThread(threadId)->IsSuspended(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1420 | void Dbg::GetThreadGroupThreadsImpl(Object* thread_group, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
| 1421 | struct ThreadListVisitor { |
| 1422 | static void Visit(Thread* t, void* arg) { |
| 1423 | reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t); |
| 1424 | } |
| 1425 | |
| 1426 | void Visit(Thread* t) { |
| 1427 | if (t == Dbg::GetDebugThread()) { |
| 1428 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 1429 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 1430 | return; |
| 1431 | } |
| 1432 | if (thread_group == NULL || t->GetThreadGroup() == thread_group) { |
| 1433 | threads.push_back(gRegistry->Add(t->GetPeer())); |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | Object* thread_group; |
| 1438 | std::vector<JDWP::ObjectId> threads; |
| 1439 | }; |
| 1440 | |
| 1441 | ThreadListVisitor tlv; |
| 1442 | tlv.thread_group = thread_group; |
| 1443 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame^] | 1444 | Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1445 | |
| 1446 | *pThreadCount = tlv.threads.size(); |
| 1447 | if (*pThreadCount == 0) { |
| 1448 | *ppThreadIds = NULL; |
| 1449 | } else { |
| 1450 | *ppThreadIds = new JDWP::ObjectId[*pThreadCount]; |
| 1451 | for (size_t i = 0; i < *pThreadCount; ++i) { |
| 1452 | (*ppThreadIds)[i] = tlv.threads[i]; |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1457 | void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1458 | GetThreadGroupThreadsImpl(gRegistry->Get<Object*>(threadGroupId), ppThreadIds, pThreadCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1462 | GetThreadGroupThreadsImpl(NULL, ppThreadIds, pThreadCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1465 | static int GetStackDepth(Thread* thread) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1466 | struct CountStackDepthVisitor : public Thread::StackVisitor { |
| 1467 | CountStackDepthVisitor() : depth(0) {} |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1468 | bool VisitFrame(const Frame& f, uintptr_t) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1469 | if (f.HasMethod()) { |
| 1470 | ++depth; |
| 1471 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1472 | return true; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1473 | } |
| 1474 | size_t depth; |
| 1475 | }; |
| 1476 | CountStackDepthVisitor visitor; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1477 | thread->WalkStack(&visitor); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1478 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1481 | int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) { |
| 1482 | ScopedThreadListLock thread_list_lock; |
| 1483 | return GetStackDepth(DecodeThread(threadId)); |
| 1484 | } |
| 1485 | |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1486 | void Dbg::GetThreadFrame(JDWP::ObjectId threadId, int desired_frame_number, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1487 | ScopedThreadListLock thread_list_lock; |
| 1488 | struct GetFrameVisitor : public Thread::StackVisitor { |
| 1489 | GetFrameVisitor(int desired_frame_number, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1490 | : depth(0), desired_frame_number(desired_frame_number), pFrameId(pFrameId), pLoc(pLoc) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1491 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1492 | bool VisitFrame(const Frame& f, uintptr_t pc) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1493 | if (!f.HasMethod()) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1494 | 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] | 1495 | } |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1496 | if (depth == desired_frame_number) { |
| 1497 | *pFrameId = reinterpret_cast<JDWP::FrameId>(f.GetSP()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1498 | SetLocation(*pLoc, f.GetMethod(), pc); |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1499 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1500 | } |
| 1501 | ++depth; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1502 | return true; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1503 | } |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1504 | int depth; |
| 1505 | int desired_frame_number; |
| 1506 | JDWP::FrameId* pFrameId; |
| 1507 | JDWP::JdwpLocation* pLoc; |
| 1508 | }; |
| 1509 | GetFrameVisitor visitor(desired_frame_number, pFrameId, pLoc); |
| 1510 | visitor.desired_frame_number = desired_frame_number; |
| 1511 | DecodeThread(threadId)->WalkStack(&visitor); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1515 | return gRegistry->Add(Thread::Current()->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1518 | void Dbg::SuspendVM() { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 1519 | 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] | 1520 | Runtime::Current()->GetThreadList()->SuspendAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | void Dbg::ResumeVM() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1524 | Runtime::Current()->GetThreadList()->ResumeAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | void Dbg::SuspendThread(JDWP::ObjectId threadId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1528 | Object* peer = gRegistry->Get<Object*>(threadId); |
| 1529 | ScopedThreadListLock thread_list_lock; |
| 1530 | Thread* thread = Thread::FromManagedThread(peer); |
| 1531 | if (thread == NULL) { |
| 1532 | LOG(WARNING) << "No such thread for suspend: " << peer; |
| 1533 | return; |
| 1534 | } |
| 1535 | Runtime::Current()->GetThreadList()->Suspend(thread, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | void Dbg::ResumeThread(JDWP::ObjectId threadId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1539 | Object* peer = gRegistry->Get<Object*>(threadId); |
| 1540 | ScopedThreadListLock thread_list_lock; |
| 1541 | Thread* thread = Thread::FromManagedThread(peer); |
| 1542 | if (thread == NULL) { |
| 1543 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 1544 | return; |
| 1545 | } |
| 1546 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1550 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1553 | static Object* GetThis(Frame& f) { |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1554 | Method* m = f.GetMethod(); |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1555 | Object* o = NULL; |
| 1556 | if (!m->IsNative() && !m->IsStatic()) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1557 | uint16_t reg = DemangleSlot(0, m); |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1558 | o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
| 1559 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1560 | return o; |
| 1561 | } |
| 1562 | |
| 1563 | void Dbg::GetThisObject(JDWP::FrameId frameId, JDWP::ObjectId* pThisId) { |
| 1564 | Method** sp = reinterpret_cast<Method**>(frameId); |
| 1565 | Frame f(sp); |
| 1566 | Object* o = GetThis(f); |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1567 | *pThisId = gRegistry->Add(o); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1570 | void Dbg::GetLocalValue(JDWP::ObjectId /*threadId*/, JDWP::FrameId frameId, int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1571 | Method** sp = reinterpret_cast<Method**>(frameId); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1572 | Frame f(sp); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1573 | Method* m = f.GetMethod(); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1574 | uint16_t reg = DemangleSlot(slot, m); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1575 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1576 | #if defined(ART_USE_LLVM_COMPILER) |
| 1577 | UNIMPLEMENTED(FATAL); |
| 1578 | #else |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1579 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 1580 | uint32_t vmap_offset; |
| 1581 | if (vmap_table.IsInContext(reg, vmap_offset)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1582 | UNIMPLEMENTED(FATAL) << "Don't know how to pull locals from callee save frames: " << vmap_offset; |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1583 | } |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1584 | #endif |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1585 | |
Elliott Hughes | ad3da69 | 2012-02-24 16:51:35 -0800 | [diff] [blame] | 1586 | // TODO: check that the tag is compatible with the actual type of the slot! |
| 1587 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1588 | switch (tag) { |
| 1589 | case JDWP::JT_BOOLEAN: |
| 1590 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1591 | CHECK_EQ(width, 1U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1592 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1593 | VLOG(jdwp) << "get boolean local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1594 | JDWP::Set1(buf+1, intVal != 0); |
| 1595 | } |
| 1596 | break; |
| 1597 | case JDWP::JT_BYTE: |
| 1598 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1599 | CHECK_EQ(width, 1U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1600 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1601 | VLOG(jdwp) << "get byte local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1602 | JDWP::Set1(buf+1, intVal); |
| 1603 | } |
| 1604 | break; |
| 1605 | case JDWP::JT_SHORT: |
| 1606 | case JDWP::JT_CHAR: |
| 1607 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1608 | CHECK_EQ(width, 2U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1609 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1610 | VLOG(jdwp) << "get short/char local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1611 | JDWP::Set2BE(buf+1, intVal); |
| 1612 | } |
| 1613 | break; |
| 1614 | case JDWP::JT_INT: |
| 1615 | case JDWP::JT_FLOAT: |
| 1616 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1617 | CHECK_EQ(width, 4U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1618 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1619 | VLOG(jdwp) << "get int/float local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1620 | JDWP::Set4BE(buf+1, intVal); |
| 1621 | } |
| 1622 | break; |
| 1623 | case JDWP::JT_ARRAY: |
| 1624 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1625 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1626 | Object* o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1627 | VLOG(jdwp) << "get array local " << reg << " = " << o; |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 1628 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1629 | LOG(FATAL) << "Register " << reg << " expected to hold array: " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1630 | } |
| 1631 | JDWP::SetObjectId(buf+1, gRegistry->Add(o)); |
| 1632 | } |
| 1633 | break; |
Elliott Hughes | ad3da69 | 2012-02-24 16:51:35 -0800 | [diff] [blame] | 1634 | case JDWP::JT_CLASS_LOADER: |
| 1635 | case JDWP::JT_CLASS_OBJECT: |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1636 | case JDWP::JT_OBJECT: |
Elliott Hughes | ad3da69 | 2012-02-24 16:51:35 -0800 | [diff] [blame] | 1637 | case JDWP::JT_STRING: |
| 1638 | case JDWP::JT_THREAD: |
| 1639 | case JDWP::JT_THREAD_GROUP: |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1640 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1641 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1642 | Object* o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1643 | VLOG(jdwp) << "get object local " << reg << " = " << o; |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 1644 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1645 | LOG(FATAL) << "Register " << reg << " expected to hold object: " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1646 | } |
| 1647 | tag = TagFromObject(o); |
| 1648 | JDWP::SetObjectId(buf+1, gRegistry->Add(o)); |
| 1649 | } |
| 1650 | break; |
| 1651 | case JDWP::JT_DOUBLE: |
| 1652 | case JDWP::JT_LONG: |
| 1653 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1654 | CHECK_EQ(width, 8U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1655 | uint32_t lo = f.GetVReg(m, reg); |
| 1656 | uint64_t hi = f.GetVReg(m, reg + 1); |
| 1657 | uint64_t longVal = (hi << 32) | lo; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1658 | VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1659 | JDWP::Set8BE(buf+1, longVal); |
| 1660 | } |
| 1661 | break; |
| 1662 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1663 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1664 | break; |
| 1665 | } |
| 1666 | |
| 1667 | // Prepend tag, which may have been updated. |
| 1668 | JDWP::Set1(buf, tag); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1671 | void Dbg::SetLocalValue(JDWP::ObjectId /*threadId*/, JDWP::FrameId frameId, int slot, JDWP::JdwpTag tag, uint64_t value, size_t width) { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1672 | Method** sp = reinterpret_cast<Method**>(frameId); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1673 | Frame f(sp); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1674 | Method* m = f.GetMethod(); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1675 | uint16_t reg = DemangleSlot(slot, m); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1676 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1677 | #if defined(ART_USE_LLVM_COMPILER) |
| 1678 | UNIMPLEMENTED(FATAL); |
| 1679 | #else |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1680 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 1681 | uint32_t vmap_offset; |
| 1682 | if (vmap_table.IsInContext(reg, vmap_offset)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1683 | UNIMPLEMENTED(FATAL) << "Don't know how to pull locals from callee save frames: " << vmap_offset; |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1684 | } |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1685 | #endif |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1686 | |
Elliott Hughes | ad3da69 | 2012-02-24 16:51:35 -0800 | [diff] [blame] | 1687 | // TODO: check that the tag is compatible with the actual type of the slot! |
| 1688 | |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1689 | switch (tag) { |
| 1690 | case JDWP::JT_BOOLEAN: |
| 1691 | case JDWP::JT_BYTE: |
| 1692 | CHECK_EQ(width, 1U); |
| 1693 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1694 | break; |
| 1695 | case JDWP::JT_SHORT: |
| 1696 | case JDWP::JT_CHAR: |
| 1697 | CHECK_EQ(width, 2U); |
| 1698 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1699 | break; |
| 1700 | case JDWP::JT_INT: |
| 1701 | case JDWP::JT_FLOAT: |
| 1702 | CHECK_EQ(width, 4U); |
| 1703 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1704 | break; |
| 1705 | case JDWP::JT_ARRAY: |
| 1706 | case JDWP::JT_OBJECT: |
| 1707 | case JDWP::JT_STRING: |
| 1708 | { |
| 1709 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
| 1710 | Object* o = gRegistry->Get<Object*>(static_cast<JDWP::ObjectId>(value)); |
Elliott Hughes | ad3da69 | 2012-02-24 16:51:35 -0800 | [diff] [blame] | 1711 | if (o == kInvalidObject) { |
| 1712 | UNIMPLEMENTED(FATAL) << "return an error code when given an invalid object to store"; |
| 1713 | } |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1714 | f.SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o))); |
| 1715 | } |
| 1716 | break; |
| 1717 | case JDWP::JT_DOUBLE: |
| 1718 | case JDWP::JT_LONG: |
| 1719 | CHECK_EQ(width, 8U); |
| 1720 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1721 | f.SetVReg(m, reg + 1, static_cast<uint32_t>(value >> 32)); |
| 1722 | break; |
| 1723 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1724 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1725 | break; |
| 1726 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1729 | void Dbg::PostLocationEvent(const Method* m, int dex_pc, Object* this_object, int event_flags) { |
| 1730 | Class* c = m->GetDeclaringClass(); |
| 1731 | |
| 1732 | JDWP::JdwpLocation location; |
| 1733 | location.typeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1734 | location.classId = gRegistry->Add(c); |
| 1735 | location.methodId = ToMethodId(m); |
Elliott Hughes | 972a47b | 2012-02-21 18:16:06 -0800 | [diff] [blame] | 1736 | location.dex_pc = m->IsNative() ? -1 : dex_pc; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1737 | |
| 1738 | // Note we use "NoReg" so we don't keep track of references that are |
| 1739 | // never actually sent to the debugger. 'this_id' is only used to |
| 1740 | // compare against registered events... |
| 1741 | JDWP::ObjectId this_id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(this_object)); |
| 1742 | if (gJdwpState->PostLocationEvent(&location, this_id, event_flags)) { |
| 1743 | // ...unless there's a registered event, in which case we |
| 1744 | // need to really track the class and 'this'. |
| 1745 | gRegistry->Add(c); |
| 1746 | gRegistry->Add(this_object); |
| 1747 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1748 | } |
| 1749 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1750 | void Dbg::PostException(Method** sp, Method* throwMethod, uintptr_t throwNativePc, Method* catchMethod, uintptr_t catchNativePc, Object* exception) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 1751 | if (!IsDebuggerActive()) { |
Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 1752 | return; |
| 1753 | } |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1754 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1755 | JDWP::JdwpLocation throw_location; |
| 1756 | SetLocation(throw_location, throwMethod, throwNativePc); |
| 1757 | JDWP::JdwpLocation catch_location; |
| 1758 | SetLocation(catch_location, catchMethod, catchNativePc); |
| 1759 | |
| 1760 | // We need 'this' for InstanceOnly filters. |
| 1761 | JDWP::ObjectId this_id; |
| 1762 | GetThisObject(reinterpret_cast<JDWP::FrameId>(sp), &this_id); |
| 1763 | |
| 1764 | /* |
| 1765 | * Hand the event to the JDWP exception handler. Note we're using the |
| 1766 | * "NoReg" objectID on the exception, which is not strictly correct -- |
| 1767 | * the exception object WILL be passed up to the debugger if the |
| 1768 | * debugger is interested in the event. We do this because the current |
| 1769 | * implementation of the debugger object registry never throws anything |
| 1770 | * away, and some people were experiencing a fatal build up of exception |
| 1771 | * objects when dealing with certain libraries. |
| 1772 | */ |
| 1773 | JDWP::ObjectId exception_id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(exception)); |
| 1774 | JDWP::RefTypeId exception_class_id = gRegistry->Add(exception->GetClass()); |
| 1775 | |
| 1776 | 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] | 1777 | } |
| 1778 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1779 | void Dbg::PostClassPrepare(Class* c) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 1780 | if (!IsDebuggerActive()) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1781 | return; |
| 1782 | } |
| 1783 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1784 | // OLD-TODO - we currently always send both "verified" and "prepared" since |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1785 | // debuggers seem to like that. There might be some advantage to honesty, |
| 1786 | // since the class may not yet be verified. |
| 1787 | int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 1788 | JDWP::JdwpTypeTag tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1789 | gJdwpState->PostClassPrepare(tag, gRegistry->Add(c), ClassHelper(c).GetDescriptor(), state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1792 | void Dbg::UpdateDebugger(int32_t dex_pc, Thread* self, Method** sp) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 1793 | if (!IsDebuggerActive() || dex_pc == -2 /* fake method exit */) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1794 | return; |
| 1795 | } |
| 1796 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1797 | Frame f(sp); |
| 1798 | f.Next(); // Skip callee save frame. |
| 1799 | Method* m = f.GetMethod(); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1800 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1801 | if (dex_pc == -1) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 1802 | // We use a pc of -1 to represent method entry, since we might branch back to pc 0 later. |
| 1803 | // This means that for this special notification, there can't be anything else interesting |
| 1804 | // going on, so we're done already. |
| 1805 | Dbg::PostLocationEvent(m, 0, GetThis(f), kMethodEntry); |
| 1806 | return; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1807 | } |
| 1808 | |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 1809 | int event_flags = 0; |
| 1810 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1811 | if (IsBreakpoint(m, dex_pc)) { |
| 1812 | event_flags |= kBreakpoint; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1813 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1814 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1815 | // If the debugger is single-stepping one of our threads, check to |
| 1816 | // 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^] | 1817 | MutexLock mu(gBreakpointsLock); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1818 | if (gSingleStepControl.is_active && gSingleStepControl.thread == self) { |
| 1819 | CHECK(!m->IsNative()); |
| 1820 | if (gSingleStepControl.step_depth == JDWP::SD_INTO) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1821 | // Step into method calls. We break when the line number |
| 1822 | // or method pointer changes. If we're in SS_MIN mode, we |
| 1823 | // always stop. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1824 | if (gSingleStepControl.method != m) { |
| 1825 | event_flags |= kSingleStep; |
| 1826 | VLOG(jdwp) << "SS new method"; |
| 1827 | } else if (gSingleStepControl.step_size == JDWP::SS_MIN) { |
| 1828 | event_flags |= kSingleStep; |
| 1829 | VLOG(jdwp) << "SS new instruction"; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1830 | } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) { |
| 1831 | event_flags |= kSingleStep; |
| 1832 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1833 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1834 | } else if (gSingleStepControl.step_depth == JDWP::SD_OVER) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1835 | // Step over method calls. We break when the line number is |
| 1836 | // different and the frame depth is <= the original frame |
| 1837 | // depth. (We can't just compare on the method, because we |
| 1838 | // might get unrolled past it by an exception, and it's tricky |
| 1839 | // to identify recursion.) |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1840 | |
| 1841 | // TODO: can we just use the value of 'sp'? |
| 1842 | int stack_depth = GetStackDepth(self); |
| 1843 | |
| 1844 | if (stack_depth < gSingleStepControl.stack_depth) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1845 | // popped up one or more frames, always trigger |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1846 | event_flags |= kSingleStep; |
| 1847 | VLOG(jdwp) << "SS method pop"; |
| 1848 | } else if (stack_depth == gSingleStepControl.stack_depth) { |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1849 | // same depth, see if we moved |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1850 | if (gSingleStepControl.step_size == JDWP::SS_MIN) { |
| 1851 | event_flags |= kSingleStep; |
| 1852 | VLOG(jdwp) << "SS new instruction"; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1853 | } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) { |
| 1854 | event_flags |= kSingleStep; |
| 1855 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1856 | } |
| 1857 | } |
| 1858 | } else { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1859 | CHECK_EQ(gSingleStepControl.step_depth, JDWP::SD_OUT); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1860 | // Return from the current method. We break when the frame |
| 1861 | // depth pops up. |
| 1862 | |
| 1863 | // This differs from the "method exit" break in that it stops |
| 1864 | // with the PC at the next instruction in the returned-to |
| 1865 | // function, rather than the end of the returning function. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1866 | |
| 1867 | // TODO: can we just use the value of 'sp'? |
| 1868 | int stack_depth = GetStackDepth(self); |
| 1869 | if (stack_depth < gSingleStepControl.stack_depth) { |
| 1870 | event_flags |= kSingleStep; |
| 1871 | VLOG(jdwp) << "SS method pop"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1872 | } |
| 1873 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1874 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1875 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1876 | // Check to see if this is a "return" instruction. JDWP says we should |
| 1877 | // send the event *after* the code has been executed, but it also says |
| 1878 | // the location we provide is the last instruction. Since the "return" |
| 1879 | // instruction has no interesting side effects, we should be safe. |
| 1880 | // (We can't just move this down to the returnFromMethod label because |
| 1881 | // we potentially need to combine it with other events.) |
| 1882 | // We're also not supposed to generate a method exit event if the method |
| 1883 | // terminates "with a thrown exception". |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1884 | if (dex_pc >= 0) { |
| 1885 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
| 1886 | CHECK(code_item != NULL); |
| 1887 | CHECK_LT(dex_pc, static_cast<int32_t>(code_item->insns_size_in_code_units_)); |
| 1888 | if (Instruction::At(&code_item->insns_[dex_pc])->IsReturn()) { |
| 1889 | event_flags |= kMethodExit; |
| 1890 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1891 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1892 | |
| 1893 | // If there's something interesting going on, see if it matches one |
| 1894 | // of the debugger filters. |
| 1895 | if (event_flags != 0) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1896 | Dbg::PostLocationEvent(m, dex_pc, GetThis(f), event_flags); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1897 | } |
| 1898 | } |
| 1899 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1900 | void Dbg::WatchLocation(const JDWP::JdwpLocation* location) { |
| 1901 | MutexLock mu(gBreakpointsLock); |
| 1902 | Method* m = FromMethodId(location->methodId); |
Elliott Hughes | 972a47b | 2012-02-21 18:16:06 -0800 | [diff] [blame] | 1903 | gBreakpoints.push_back(Breakpoint(m, location->dex_pc)); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1904 | VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " << gBreakpoints[gBreakpoints.size() - 1]; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1907 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location) { |
| 1908 | MutexLock mu(gBreakpointsLock); |
| 1909 | Method* m = FromMethodId(location->methodId); |
| 1910 | for (size_t i = 0; i < gBreakpoints.size(); ++i) { |
Elliott Hughes | 972a47b | 2012-02-21 18:16:06 -0800 | [diff] [blame] | 1911 | if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == location->dex_pc) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1912 | VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i]; |
| 1913 | gBreakpoints.erase(gBreakpoints.begin() + i); |
| 1914 | return; |
| 1915 | } |
| 1916 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1919 | 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] | 1920 | Thread* thread = DecodeThread(threadId); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1921 | if (thread == NULL) { |
| 1922 | return JDWP::ERR_INVALID_THREAD; |
| 1923 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1924 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame^] | 1925 | MutexLock mu(gBreakpointsLock); |
| 1926 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1927 | // TODO: there's no theoretical reason why we couldn't support single-stepping |
| 1928 | // of multiple threads at once, but we never did so historically. |
| 1929 | if (gSingleStepControl.thread != NULL && thread != gSingleStepControl.thread) { |
| 1930 | LOG(WARNING) << "single-step already active for " << *gSingleStepControl.thread |
| 1931 | << "; switching to " << *thread; |
| 1932 | } |
| 1933 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1934 | // |
| 1935 | // Work out what Method* we're in, the current line number, and how deep the stack currently |
| 1936 | // is for step-out. |
| 1937 | // |
| 1938 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1939 | struct SingleStepStackVisitor : public Thread::StackVisitor { |
| 1940 | SingleStepStackVisitor() { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame^] | 1941 | MutexLock mu(gBreakpointsLock); // Keep GCC happy. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1942 | gSingleStepControl.method = NULL; |
| 1943 | gSingleStepControl.stack_depth = 0; |
| 1944 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1945 | bool VisitFrame(const Frame& f, uintptr_t pc) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame^] | 1946 | MutexLock mu(gBreakpointsLock); // Keep GCC happy. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1947 | if (f.HasMethod()) { |
| 1948 | ++gSingleStepControl.stack_depth; |
| 1949 | if (gSingleStepControl.method == NULL) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1950 | const Method* m = f.GetMethod(); |
| 1951 | const DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache(); |
| 1952 | gSingleStepControl.method = m; |
| 1953 | gSingleStepControl.line_number = -1; |
| 1954 | if (dex_cache != NULL) { |
| 1955 | const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache); |
| 1956 | gSingleStepControl.line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc)); |
| 1957 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1958 | } |
| 1959 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1960 | return true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 1961 | } |
| 1962 | }; |
| 1963 | SingleStepStackVisitor visitor; |
| 1964 | thread->WalkStack(&visitor); |
| 1965 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1966 | // |
| 1967 | // Find the dex_pc values that correspond to the current line, for line-based single-stepping. |
| 1968 | // |
| 1969 | |
| 1970 | struct DebugCallbackContext { |
| 1971 | DebugCallbackContext() { |
| 1972 | last_pc_valid = false; |
| 1973 | last_pc = 0; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | 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^] | 1977 | MutexLock mu(gBreakpointsLock); // Keep GCC happy. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1978 | DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context); |
| 1979 | if (static_cast<int32_t>(line_number) == gSingleStepControl.line_number) { |
| 1980 | if (!context->last_pc_valid) { |
| 1981 | // Everything from this address until the next line change is ours. |
| 1982 | context->last_pc = address; |
| 1983 | context->last_pc_valid = true; |
| 1984 | } |
| 1985 | // Otherwise, if we're already in a valid range for this line, |
| 1986 | // just keep going (shouldn't really happen)... |
| 1987 | } else if (context->last_pc_valid) { // and the line number is new |
| 1988 | // Add everything from the last entry up until here to the set |
| 1989 | for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) { |
| 1990 | gSingleStepControl.dex_pcs.insert(dex_pc); |
| 1991 | } |
| 1992 | context->last_pc_valid = false; |
| 1993 | } |
| 1994 | return false; // There may be multiple entries for any given line. |
| 1995 | } |
| 1996 | |
| 1997 | ~DebugCallbackContext() { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame^] | 1998 | MutexLock mu(gBreakpointsLock); // Keep GCC happy. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1999 | // If the line number was the last in the position table... |
| 2000 | if (last_pc_valid) { |
| 2001 | size_t end = MethodHelper(gSingleStepControl.method).GetCodeItem()->insns_size_in_code_units_; |
| 2002 | for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) { |
| 2003 | gSingleStepControl.dex_pcs.insert(dex_pc); |
| 2004 | } |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | bool last_pc_valid; |
| 2009 | uint32_t last_pc; |
| 2010 | }; |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 2011 | gSingleStepControl.dex_pcs.clear(); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2012 | const Method* m = gSingleStepControl.method; |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 2013 | if (m->IsNative()) { |
| 2014 | gSingleStepControl.line_number = -1; |
| 2015 | } else { |
| 2016 | DebugCallbackContext context; |
| 2017 | MethodHelper mh(m); |
| 2018 | mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(), |
| 2019 | DebugCallbackContext::Callback, NULL, &context); |
| 2020 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2021 | |
| 2022 | // |
| 2023 | // Everything else... |
| 2024 | // |
| 2025 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2026 | gSingleStepControl.thread = thread; |
| 2027 | gSingleStepControl.step_size = step_size; |
| 2028 | gSingleStepControl.step_depth = step_depth; |
| 2029 | gSingleStepControl.is_active = true; |
| 2030 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2031 | if (VLOG_IS_ON(jdwp)) { |
| 2032 | VLOG(jdwp) << "Single-step thread: " << *gSingleStepControl.thread; |
| 2033 | VLOG(jdwp) << "Single-step step size: " << gSingleStepControl.step_size; |
| 2034 | VLOG(jdwp) << "Single-step step depth: " << gSingleStepControl.step_depth; |
| 2035 | VLOG(jdwp) << "Single-step current method: " << PrettyMethod(gSingleStepControl.method); |
| 2036 | VLOG(jdwp) << "Single-step current line: " << gSingleStepControl.line_number; |
| 2037 | VLOG(jdwp) << "Single-step current stack depth: " << gSingleStepControl.stack_depth; |
| 2038 | VLOG(jdwp) << "Single-step dex_pc values:"; |
| 2039 | 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] | 2040 | VLOG(jdwp) << StringPrintf(" %#x", *it); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2045 | } |
| 2046 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 2047 | void Dbg::UnconfigureStep(JDWP::ObjectId /*threadId*/) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame^] | 2048 | MutexLock mu(gBreakpointsLock); |
| 2049 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2050 | gSingleStepControl.is_active = false; |
| 2051 | gSingleStepControl.thread = NULL; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2052 | gSingleStepControl.dex_pcs.clear(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2053 | } |
| 2054 | |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2055 | static char JdwpTagToShortyChar(JDWP::JdwpTag tag) { |
| 2056 | switch (tag) { |
| 2057 | default: |
| 2058 | LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag); |
| 2059 | |
| 2060 | // Primitives. |
| 2061 | case JDWP::JT_BYTE: return 'B'; |
| 2062 | case JDWP::JT_CHAR: return 'C'; |
| 2063 | case JDWP::JT_FLOAT: return 'F'; |
| 2064 | case JDWP::JT_DOUBLE: return 'D'; |
| 2065 | case JDWP::JT_INT: return 'I'; |
| 2066 | case JDWP::JT_LONG: return 'J'; |
| 2067 | case JDWP::JT_SHORT: return 'S'; |
| 2068 | case JDWP::JT_VOID: return 'V'; |
| 2069 | case JDWP::JT_BOOLEAN: return 'Z'; |
| 2070 | |
| 2071 | // Reference types. |
| 2072 | case JDWP::JT_ARRAY: |
| 2073 | case JDWP::JT_OBJECT: |
| 2074 | case JDWP::JT_STRING: |
| 2075 | case JDWP::JT_THREAD: |
| 2076 | case JDWP::JT_THREAD_GROUP: |
| 2077 | case JDWP::JT_CLASS_LOADER: |
| 2078 | case JDWP::JT_CLASS_OBJECT: |
| 2079 | return 'L'; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | 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] | 2084 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 2085 | |
| 2086 | Thread* targetThread = NULL; |
| 2087 | DebugInvokeReq* req = NULL; |
| 2088 | { |
| 2089 | ScopedThreadListLock thread_list_lock; |
| 2090 | targetThread = DecodeThread(threadId); |
| 2091 | if (targetThread == NULL) { |
| 2092 | LOG(ERROR) << "InvokeMethod request for non-existent thread " << threadId; |
| 2093 | return JDWP::ERR_INVALID_THREAD; |
| 2094 | } |
| 2095 | req = targetThread->GetInvokeReq(); |
| 2096 | if (!req->ready) { |
| 2097 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 2098 | return JDWP::ERR_INVALID_THREAD; |
| 2099 | } |
| 2100 | |
| 2101 | /* |
| 2102 | * We currently have a bug where we don't successfully resume the |
| 2103 | * target thread if the suspend count is too deep. We're expected to |
| 2104 | * require one "resume" for each "suspend", but when asked to execute |
| 2105 | * a method we have to resume fully and then re-suspend it back to the |
| 2106 | * same level. (The easiest way to cause this is to type "suspend" |
| 2107 | * multiple times in jdb.) |
| 2108 | * |
| 2109 | * It's unclear what this means when the event specifies "resume all" |
| 2110 | * and some threads are suspended more deeply than others. This is |
| 2111 | * a rare problem, so for now we just prevent it from hanging forever |
| 2112 | * by rejecting the method invocation request. Without this, we will |
| 2113 | * be stuck waiting on a suspended thread. |
| 2114 | */ |
| 2115 | int suspend_count = targetThread->GetSuspendCount(); |
| 2116 | if (suspend_count > 1) { |
| 2117 | LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count; |
| 2118 | return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here. |
| 2119 | } |
| 2120 | |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2121 | JDWP::JdwpError status; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2122 | Object* receiver = gRegistry->Get<Object*>(objectId); |
| 2123 | if (receiver == kInvalidObject) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2124 | return JDWP::ERR_INVALID_OBJECT; |
| 2125 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2126 | |
| 2127 | Object* thread = gRegistry->Get<Object*>(threadId); |
| 2128 | if (thread == kInvalidObject) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2129 | return JDWP::ERR_INVALID_OBJECT; |
| 2130 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2131 | // TODO: check that 'thread' is actually a java.lang.Thread! |
| 2132 | |
| 2133 | Class* c = DecodeClass(classId, status); |
| 2134 | if (c == NULL) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 2135 | return status; |
| 2136 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2137 | |
| 2138 | Method* m = FromMethodId(methodId); |
| 2139 | if (m->IsStatic() != (receiver == NULL)) { |
| 2140 | return JDWP::ERR_INVALID_METHODID; |
| 2141 | } |
| 2142 | if (m->IsStatic()) { |
| 2143 | if (m->GetDeclaringClass() != c) { |
| 2144 | return JDWP::ERR_INVALID_METHODID; |
| 2145 | } |
| 2146 | } else { |
| 2147 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
| 2148 | return JDWP::ERR_INVALID_METHODID; |
| 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | // Check the argument list matches the method. |
| 2153 | MethodHelper mh(m); |
| 2154 | if (mh.GetShortyLength() - 1 != arg_count) { |
| 2155 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 2156 | } |
| 2157 | const char* shorty = mh.GetShorty(); |
| 2158 | for (size_t i = 0; i < arg_count; ++i) { |
| 2159 | if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) { |
| 2160 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | req->receiver_ = receiver; |
| 2165 | req->thread_ = thread; |
| 2166 | req->class_ = c; |
| 2167 | req->method_ = m; |
| 2168 | req->arg_count_ = arg_count; |
| 2169 | req->arg_values_ = arg_values; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2170 | req->options_ = options; |
| 2171 | req->invoke_needed_ = true; |
| 2172 | } |
| 2173 | |
| 2174 | // The fact that we've released the thread list lock is a bit risky --- if the thread goes |
| 2175 | // away we're sitting high and dry -- but we must release this before the ResumeAllThreads |
| 2176 | // call, and it's unwise to hold it during WaitForSuspend. |
| 2177 | |
| 2178 | { |
| 2179 | /* |
| 2180 | * We change our (JDWP thread) status, which should be THREAD_RUNNING, |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 2181 | * 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] | 2182 | * run out of memory. It's also a good idea to change it before locking |
| 2183 | * the invokeReq mutex, although that should never be held for long. |
| 2184 | */ |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 2185 | ScopedThreadStateChange tsc(Thread::Current(), kVmWait); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2186 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2187 | VLOG(jdwp) << " Transferring control to event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2188 | { |
| 2189 | MutexLock mu(req->lock_); |
| 2190 | |
| 2191 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2192 | VLOG(jdwp) << " Resuming all threads"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2193 | thread_list->ResumeAll(true); |
| 2194 | } else { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2195 | VLOG(jdwp) << " Resuming event thread only"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2196 | thread_list->Resume(targetThread, true); |
| 2197 | } |
| 2198 | |
| 2199 | // Wait for the request to finish executing. |
| 2200 | while (req->invoke_needed_) { |
| 2201 | req->cond_.Wait(req->lock_); |
| 2202 | } |
| 2203 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2204 | VLOG(jdwp) << " Control has returned from event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2205 | |
| 2206 | /* wait for thread to re-suspend itself */ |
| 2207 | targetThread->WaitUntilSuspended(); |
| 2208 | //dvmWaitForSuspend(targetThread); |
| 2209 | } |
| 2210 | |
| 2211 | /* |
| 2212 | * Suspend the threads. We waited for the target thread to suspend |
| 2213 | * itself, so all we need to do is suspend the others. |
| 2214 | * |
| 2215 | * The suspendAllThreads() call will double-suspend the event thread, |
| 2216 | * so we want to resume the target thread once to keep the books straight. |
| 2217 | */ |
| 2218 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2219 | VLOG(jdwp) << " Suspending all threads"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2220 | thread_list->SuspendAll(true); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2221 | VLOG(jdwp) << " Resuming event thread to balance the count"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2222 | thread_list->Resume(targetThread, true); |
| 2223 | } |
| 2224 | |
| 2225 | // Copy the result. |
| 2226 | *pResultTag = req->result_tag; |
| 2227 | if (IsPrimitiveTag(req->result_tag)) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2228 | *pResultValue = req->result_value.GetJ(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2229 | } else { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2230 | *pResultValue = gRegistry->Add(req->result_value.GetL()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2231 | } |
| 2232 | *pExceptionId = req->exception; |
| 2233 | return req->error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2237 | Thread* self = Thread::Current(); |
| 2238 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 2239 | // We can be called while an exception is pending. We need |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2240 | // to preserve that across the method invocation. |
| 2241 | SirtRef<Throwable> old_exception(self->GetException()); |
| 2242 | self->ClearException(); |
| 2243 | |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 2244 | ScopedThreadStateChange tsc(self, kRunnable); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2245 | |
| 2246 | // Translate the method through the vtable, unless the debugger wants to suppress it. |
| 2247 | Method* m = pReq->method_; |
| 2248 | if ((pReq->options_ & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver_ != NULL) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2249 | Method* actual_method = pReq->class_->FindVirtualMethodForVirtualOrInterface(pReq->method_); |
| 2250 | if (actual_method != m) { |
| 2251 | VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) << " to " << PrettyMethod(actual_method); |
| 2252 | m = actual_method; |
| 2253 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2254 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2255 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2256 | CHECK(m != NULL); |
| 2257 | |
| 2258 | CHECK_EQ(sizeof(jvalue), sizeof(uint64_t)); |
| 2259 | |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 2260 | LOG(INFO) << "self=" << self << " pReq->receiver_=" << pReq->receiver_ << " m=" << m << " #" << pReq->arg_count_ << " " << pReq->arg_values_; |
| 2261 | 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] | 2262 | |
| 2263 | pReq->exception = gRegistry->Add(self->GetException()); |
| 2264 | pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty()); |
| 2265 | if (pReq->exception != 0) { |
| 2266 | Object* exc = self->GetException(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2267 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exc << " " << PrettyTypeOf(exc); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2268 | self->ClearException(); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2269 | pReq->result_value.SetJ(0); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2270 | } else if (pReq->result_tag == JDWP::JT_OBJECT) { |
| 2271 | /* if no exception thrown, examine object result more closely */ |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2272 | JDWP::JdwpTag new_tag = TagFromObject(pReq->result_value.GetL()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2273 | if (new_tag != pReq->result_tag) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2274 | VLOG(jdwp) << " JDWP promoted result from " << pReq->result_tag << " to " << new_tag; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2275 | pReq->result_tag = new_tag; |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * Register the object. We don't actually need an ObjectId yet, |
| 2280 | * but we do need to be sure that the GC won't move or discard the |
| 2281 | * object when we switch out of RUNNING. The ObjectId conversion |
| 2282 | * will add the object to the "do not touch" list. |
| 2283 | * |
| 2284 | * We can't use the "tracked allocation" mechanism here because |
| 2285 | * the object is going to be handed off to a different thread. |
| 2286 | */ |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 2287 | gRegistry->Add(pReq->result_value.GetL()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | if (old_exception.get() != NULL) { |
| 2291 | self->SetException(old_exception.get()); |
| 2292 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2293 | } |
| 2294 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2295 | /* |
| 2296 | * Register an object ID that might not have been registered previously. |
| 2297 | * |
| 2298 | * Normally this wouldn't happen -- the conversion to an ObjectId would |
| 2299 | * have added the object to the registry -- but in some cases (e.g. |
| 2300 | * throwing exceptions) we really want to do the registration late. |
| 2301 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2302 | void Dbg::RegisterObjectId(JDWP::ObjectId id) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 2303 | gRegistry->Add(reinterpret_cast<Object*>(id)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2304 | } |
| 2305 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2306 | /* |
| 2307 | * "buf" contains a full JDWP packet, possibly with multiple chunks. We |
| 2308 | * need to process each, accumulate the replies, and ship the whole thing |
| 2309 | * back. |
| 2310 | * |
| 2311 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 2312 | * and includes the chunk type/length, followed by the data. |
| 2313 | * |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2314 | * 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] | 2315 | * chunk. If this becomes inconvenient we will need to adapt. |
| 2316 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2317 | 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] | 2318 | CHECK_GE(dataLen, 0); |
| 2319 | |
| 2320 | Thread* self = Thread::Current(); |
| 2321 | JNIEnv* env = self->GetJniEnv(); |
| 2322 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2323 | // Create a byte[] corresponding to 'buf'. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2324 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(dataLen)); |
| 2325 | if (dataArray.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2326 | LOG(WARNING) << "byte[] allocation failed: " << dataLen; |
| 2327 | env->ExceptionClear(); |
| 2328 | return false; |
| 2329 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2330 | env->SetByteArrayRegion(dataArray.get(), 0, dataLen, reinterpret_cast<const jbyte*>(buf)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2331 | |
| 2332 | const int kChunkHdrLen = 8; |
| 2333 | |
| 2334 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2335 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 2336 | jint type = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[0])); |
| 2337 | jint length = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[4])); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2338 | jint offset = kChunkHdrLen; |
| 2339 | if (offset + length > dataLen) { |
| 2340 | LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen); |
| 2341 | return false; |
| 2342 | } |
| 2343 | |
| 2344 | // 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] | 2345 | ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 2346 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch, |
| 2347 | type, dataArray.get(), offset, length)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2348 | if (env->ExceptionCheck()) { |
| 2349 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 2350 | env->ExceptionDescribe(); |
| 2351 | env->ExceptionClear(); |
| 2352 | return false; |
| 2353 | } |
| 2354 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2355 | if (chunk.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2356 | return false; |
| 2357 | } |
| 2358 | |
| 2359 | /* |
| 2360 | * Pull the pieces out of the chunk. We copy the results into a |
| 2361 | * newly-allocated buffer that the caller can free. We don't want to |
| 2362 | * continue using the Chunk object because nothing has a reference to it. |
| 2363 | * |
| 2364 | * We could avoid this by returning type/data/offset/length and having |
| 2365 | * the caller be aware of the object lifetime issues, but that |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 2366 | * 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] | 2367 | * if we have responses for multiple chunks. |
| 2368 | * |
| 2369 | * So we're pretty much stuck with copying data around multiple times. |
| 2370 | */ |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 2371 | ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data))); |
| 2372 | length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length); |
| 2373 | offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset); |
| 2374 | 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] | 2375 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2376 | 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] | 2377 | if (length == 0 || replyData.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2378 | return false; |
| 2379 | } |
| 2380 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2381 | jsize replyLength = env->GetArrayLength(replyData.get()); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2382 | if (offset + length > replyLength) { |
| 2383 | LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength); |
| 2384 | return false; |
| 2385 | } |
| 2386 | |
| 2387 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
| 2388 | if (reply == NULL) { |
| 2389 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 2390 | return false; |
| 2391 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 2392 | JDWP::Set4BE(reply + 0, type); |
| 2393 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2394 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 2395 | |
| 2396 | *pReplyBuf = reply; |
| 2397 | *pReplyLen = length + kChunkHdrLen; |
| 2398 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2399 | 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] | 2400 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2401 | } |
| 2402 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2403 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2404 | VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2405 | |
| 2406 | Thread* self = Thread::Current(); |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 2407 | if (self->GetState() != kRunnable) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2408 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 2409 | /* try anyway? */ |
| 2410 | } |
| 2411 | |
| 2412 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2413 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 2414 | env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 2415 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast, |
| 2416 | event); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2417 | if (env->ExceptionCheck()) { |
| 2418 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 2419 | env->ExceptionDescribe(); |
| 2420 | env->ExceptionClear(); |
| 2421 | } |
| 2422 | } |
| 2423 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2424 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2425 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2429 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2430 | gDdmThreadNotification = false; |
| 2431 | } |
| 2432 | |
| 2433 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2434 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2435 | * |
| 2436 | * Because we broadcast the full set of threads when the notifications are |
| 2437 | * first enabled, it's possible for "thread" to be actively executing. |
| 2438 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2439 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2440 | if (!gDdmThreadNotification) { |
| 2441 | return; |
| 2442 | } |
| 2443 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2444 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2445 | uint8_t buf[4]; |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 2446 | JDWP::Set4BE(&buf[0], t->GetThinLockId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2447 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2448 | } else { |
| 2449 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 2450 | SirtRef<String> name(t->GetThreadName()); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2451 | size_t char_count = (name.get() != NULL) ? name->GetLength() : 0; |
| 2452 | const jchar* chars = name->GetCharArray()->GetData(); |
| 2453 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2454 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2455 | JDWP::Append4BE(bytes, t->GetThinLockId()); |
| 2456 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2457 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 2458 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2462 | static void DdmSendThreadStartCallback(Thread* t, void*) { |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2463 | Dbg::DdmSendThreadNotification(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | void Dbg::DdmSetThreadNotification(bool enable) { |
| 2467 | // We lock the thread list to avoid sending duplicate events or missing |
| 2468 | // a thread change. We should be okay holding this lock while sending |
| 2469 | // 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] | 2470 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2471 | |
| 2472 | gDdmThreadNotification = enable; |
| 2473 | if (enable) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 2474 | Runtime::Current()->GetThreadList()->ForEach(DdmSendThreadStartCallback, NULL); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2475 | } |
| 2476 | } |
| 2477 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2478 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2479 | if (IsDebuggerActive()) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2480 | JDWP::ObjectId id = gRegistry->Add(t->GetPeer()); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2481 | gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2482 | // If this thread's just joined the party while we're already debugging, make sure it knows |
| 2483 | // to give us updates when it's running. |
| 2484 | t->SetDebuggerUpdatesEnabled(true); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2485 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2486 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2490 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2494 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2495 | } |
| 2496 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2497 | 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] | 2498 | CHECK(buf != NULL); |
| 2499 | iovec vec[1]; |
| 2500 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 2501 | vec[0].iov_len = byte_count; |
| 2502 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2503 | } |
| 2504 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2505 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 2506 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 2507 | } |
| 2508 | |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 2509 | 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] | 2510 | if (gJdwpState == NULL) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2511 | VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 2512 | } else { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 2513 | gJdwpState->DdmSendChunkV(type, iov, iov_count); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 2514 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 2517 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 2518 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 2519 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 2520 | return true; |
| 2521 | } |
| 2522 | |
| 2523 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 2524 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 2525 | return false; |
| 2526 | } |
| 2527 | |
| 2528 | gDdmHpifWhen = when; |
| 2529 | return true; |
| 2530 | } |
| 2531 | |
| 2532 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 2533 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 2534 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 2535 | return false; |
| 2536 | } |
| 2537 | |
| 2538 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 2539 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 2540 | return false; |
| 2541 | } |
| 2542 | |
| 2543 | if (native) { |
| 2544 | gDdmNhsgWhen = when; |
| 2545 | gDdmNhsgWhat = what; |
| 2546 | } else { |
| 2547 | gDdmHpsgWhen = when; |
| 2548 | gDdmHpsgWhat = what; |
| 2549 | } |
| 2550 | return true; |
| 2551 | } |
| 2552 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 2553 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 2554 | // If there's a one-shot 'when', reset it. |
| 2555 | if (reason == gDdmHpifWhen) { |
| 2556 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 2557 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 2558 | } |
| 2559 | } |
| 2560 | |
| 2561 | /* |
| 2562 | * Chunk HPIF (client --> server) |
| 2563 | * |
| 2564 | * Heap Info. General information about the heap, |
| 2565 | * suitable for a summary display. |
| 2566 | * |
| 2567 | * [u4]: number of heaps |
| 2568 | * |
| 2569 | * For each heap: |
| 2570 | * [u4]: heap ID |
| 2571 | * [u8]: timestamp in ms since Unix epoch |
| 2572 | * [u1]: capture reason (same as 'when' value from server) |
| 2573 | * [u4]: max heap size in bytes (-Xmx) |
| 2574 | * [u4]: current heap size in bytes |
| 2575 | * [u4]: current number of bytes allocated |
| 2576 | * [u4]: current number of objects allocated |
| 2577 | */ |
| 2578 | uint8_t heap_count = 1; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 2579 | Heap* heap = Runtime::Current()->GetHeap(); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2580 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2581 | JDWP::Append4BE(bytes, heap_count); |
| 2582 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
| 2583 | JDWP::Append8BE(bytes, MilliTime()); |
| 2584 | JDWP::Append1BE(bytes, reason); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 2585 | JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes. |
| 2586 | JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes. |
| 2587 | JDWP::Append4BE(bytes, heap->GetBytesAllocated()); |
| 2588 | JDWP::Append4BE(bytes, heap->GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2589 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 2590 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 2591 | } |
| 2592 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2593 | enum HpsgSolidity { |
| 2594 | SOLIDITY_FREE = 0, |
| 2595 | SOLIDITY_HARD = 1, |
| 2596 | SOLIDITY_SOFT = 2, |
| 2597 | SOLIDITY_WEAK = 3, |
| 2598 | SOLIDITY_PHANTOM = 4, |
| 2599 | SOLIDITY_FINALIZABLE = 5, |
| 2600 | SOLIDITY_SWEEP = 6, |
| 2601 | }; |
| 2602 | |
| 2603 | enum HpsgKind { |
| 2604 | KIND_OBJECT = 0, |
| 2605 | KIND_CLASS_OBJECT = 1, |
| 2606 | KIND_ARRAY_1 = 2, |
| 2607 | KIND_ARRAY_2 = 3, |
| 2608 | KIND_ARRAY_4 = 4, |
| 2609 | KIND_ARRAY_8 = 5, |
| 2610 | KIND_UNKNOWN = 6, |
| 2611 | KIND_NATIVE = 7, |
| 2612 | }; |
| 2613 | |
| 2614 | #define HPSG_PARTIAL (1<<7) |
| 2615 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 2616 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2617 | class HeapChunkContext { |
| 2618 | public: |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2619 | // Maximum chunk size. Obtain this from the formula: |
| 2620 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 2621 | HeapChunkContext(bool merge, bool native) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2622 | : buf_(16384 - 16), |
| 2623 | type_(0), |
| 2624 | merge_(merge) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2625 | Reset(); |
| 2626 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2627 | type_ = CHUNK_TYPE("NHSG"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2628 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2629 | type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | ~HeapChunkContext() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2634 | if (p_ > &buf_[0]) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2635 | Flush(); |
| 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | void EnsureHeader(const void* chunk_ptr) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2640 | if (!needHeader_) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2641 | return; |
| 2642 | } |
| 2643 | |
| 2644 | // Start a new HPSx chunk. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2645 | JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap). |
| 2646 | JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2647 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2648 | JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 2649 | 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] | 2650 | // [u4]: length of piece, in allocation units |
| 2651 | // 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] | 2652 | pieceLenField_ = p_; |
| 2653 | JDWP::Write4BE(&p_, 0x55555555); |
| 2654 | needHeader_ = false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | void Flush() { |
| 2658 | // Patch the "length of piece" field. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2659 | CHECK_LE(&buf_[0], pieceLenField_); |
| 2660 | CHECK_LE(pieceLenField_, p_); |
| 2661 | JDWP::Set4BE(pieceLenField_, totalAllocationUnits_); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2662 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2663 | Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2664 | Reset(); |
| 2665 | } |
| 2666 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2667 | static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 2668 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2669 | } |
| 2670 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2671 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2672 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 2673 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2674 | void Reset() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2675 | p_ = &buf_[0]; |
| 2676 | totalAllocationUnits_ = 0; |
| 2677 | needHeader_ = true; |
| 2678 | pieceLenField_ = NULL; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2679 | } |
| 2680 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 2681 | void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2682 | // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken |
| 2683 | // in the following code not to allocate memory, by ensuring buf_ is of the correct size |
| 2684 | |
Elliott Hughes | 741c9fa | 2012-06-08 15:51:32 -0700 | [diff] [blame] | 2685 | void* user_ptr = used_bytes > 0 ? start : NULL; |
| 2686 | size_t chunk_len = mspace_usable_size(user_ptr); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2687 | |
Elliott Hughes | 741c9fa | 2012-06-08 15:51:32 -0700 | [diff] [blame] | 2688 | // Make sure there's enough room left in the buffer. |
| 2689 | // 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] | 2690 | { |
| 2691 | size_t needed = (((chunk_len/ALLOCATION_UNIT_SIZE + 255) / 256) * 2); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2692 | size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2693 | if (bytesLeft < needed) { |
| 2694 | Flush(); |
| 2695 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2696 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2697 | bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2698 | if (bytesLeft < needed) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2699 | 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] | 2700 | return; |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | // 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] | 2705 | EnsureHeader(start); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2706 | |
| 2707 | // Determine the type of this chunk. |
| 2708 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 2709 | // If it's the same, we should combine them. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2710 | 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] | 2711 | |
| 2712 | // Write out the chunk description. |
| 2713 | chunk_len /= ALLOCATION_UNIT_SIZE; // convert to allocation units |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2714 | totalAllocationUnits_ += chunk_len; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2715 | while (chunk_len > 256) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2716 | *p_++ = state | HPSG_PARTIAL; |
| 2717 | *p_++ = 255; // length - 1 |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2718 | chunk_len -= 256; |
| 2719 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2720 | *p_++ = state; |
| 2721 | *p_++ = chunk_len - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2722 | } |
| 2723 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2724 | uint8_t ExamineObject(const Object* o, bool is_native_heap) { |
| 2725 | if (o == NULL) { |
| 2726 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 2727 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2728 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2729 | // It's an allocated chunk. Figure out what it is. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2730 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2731 | // If we're looking at the native heap, we'll just return |
| 2732 | // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 2733 | if (is_native_heap || !Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2734 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 2735 | } |
| 2736 | |
| 2737 | Class* c = o->GetClass(); |
| 2738 | if (c == NULL) { |
| 2739 | // The object was probably just created but hasn't been initialized yet. |
| 2740 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 2741 | } |
| 2742 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 2743 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(c)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2744 | LOG(WARNING) << "Invalid class for managed heap object: " << o << " " << c; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2745 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 2746 | } |
| 2747 | |
| 2748 | if (c->IsClassClass()) { |
| 2749 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 2750 | } |
| 2751 | |
| 2752 | if (c->IsArrayClass()) { |
| 2753 | if (o->IsObjectArray()) { |
| 2754 | return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 2755 | } |
| 2756 | switch (c->GetComponentSize()) { |
| 2757 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 2758 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 2759 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 2760 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 2761 | } |
| 2762 | } |
| 2763 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2764 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 2765 | } |
| 2766 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2767 | std::vector<uint8_t> buf_; |
| 2768 | uint8_t* p_; |
| 2769 | uint8_t* pieceLenField_; |
| 2770 | size_t totalAllocationUnits_; |
| 2771 | uint32_t type_; |
| 2772 | bool merge_; |
| 2773 | bool needHeader_; |
| 2774 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2775 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 2776 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2777 | |
| 2778 | void Dbg::DdmSendHeapSegments(bool native) { |
| 2779 | Dbg::HpsgWhen when; |
| 2780 | Dbg::HpsgWhat what; |
| 2781 | if (!native) { |
| 2782 | when = gDdmHpsgWhen; |
| 2783 | what = gDdmHpsgWhat; |
| 2784 | } else { |
| 2785 | when = gDdmNhsgWhen; |
| 2786 | what = gDdmNhsgWhat; |
| 2787 | } |
| 2788 | if (when == HPSG_WHEN_NEVER) { |
| 2789 | return; |
| 2790 | } |
| 2791 | |
| 2792 | // Figure out what kind of chunks we'll be sending. |
| 2793 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what); |
| 2794 | |
| 2795 | // First, send a heap start chunk. |
| 2796 | uint8_t heap_id[4]; |
| 2797 | JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap). |
| 2798 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id); |
| 2799 | |
| 2800 | // Send a series of heap segment chunks. |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2801 | HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native); |
| 2802 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2803 | // TODO: enable when bionic has moved to dlmalloc 2.8.5 |
| 2804 | // dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context); |
| 2805 | UNIMPLEMENTED(WARNING) << "Native heap send heap segments"; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2806 | } else { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 2807 | Heap* heap = Runtime::Current()->GetHeap(); |
| 2808 | heap->GetAllocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2809 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2810 | |
| 2811 | // Finally, send a heap end chunk. |
| 2812 | 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] | 2813 | } |
| 2814 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2815 | void Dbg::SetAllocTrackingEnabled(bool enabled) { |
| 2816 | MutexLock mu(gAllocTrackerLock); |
| 2817 | if (enabled) { |
| 2818 | if (recent_allocation_records_ == NULL) { |
| 2819 | LOG(INFO) << "Enabling alloc tracker (" << kNumAllocRecords << " entries, " |
| 2820 | << kMaxAllocRecordStackDepth << " frames --> " |
| 2821 | << (sizeof(AllocRecord) * kNumAllocRecords) << " bytes)"; |
| 2822 | gAllocRecordHead = gAllocRecordCount = 0; |
| 2823 | recent_allocation_records_ = new AllocRecord[kNumAllocRecords]; |
| 2824 | CHECK(recent_allocation_records_ != NULL); |
| 2825 | } |
| 2826 | } else { |
| 2827 | delete[] recent_allocation_records_; |
| 2828 | recent_allocation_records_ = NULL; |
| 2829 | } |
| 2830 | } |
| 2831 | |
| 2832 | struct AllocRecordStackVisitor : public Thread::StackVisitor { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2833 | explicit AllocRecordStackVisitor(AllocRecord* record) : record(record), depth(0) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2834 | } |
| 2835 | |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2836 | bool VisitFrame(const Frame& f, uintptr_t pc) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2837 | if (depth >= kMaxAllocRecordStackDepth) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2838 | return false; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2839 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2840 | if (f.HasMethod()) { |
| 2841 | record->stack[depth].method = f.GetMethod(); |
| 2842 | record->stack[depth].raw_pc = pc; |
| 2843 | ++depth; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2844 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2845 | return true; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | ~AllocRecordStackVisitor() { |
| 2849 | // Clear out any unused stack trace elements. |
| 2850 | for (; depth < kMaxAllocRecordStackDepth; ++depth) { |
| 2851 | record->stack[depth].method = NULL; |
| 2852 | record->stack[depth].raw_pc = 0; |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | AllocRecord* record; |
| 2857 | size_t depth; |
| 2858 | }; |
| 2859 | |
| 2860 | void Dbg::RecordAllocation(Class* type, size_t byte_count) { |
| 2861 | Thread* self = Thread::Current(); |
| 2862 | CHECK(self != NULL); |
| 2863 | |
| 2864 | MutexLock mu(gAllocTrackerLock); |
| 2865 | if (recent_allocation_records_ == NULL) { |
| 2866 | return; |
| 2867 | } |
| 2868 | |
| 2869 | // Advance and clip. |
| 2870 | if (++gAllocRecordHead == kNumAllocRecords) { |
| 2871 | gAllocRecordHead = 0; |
| 2872 | } |
| 2873 | |
| 2874 | // Fill in the basics. |
| 2875 | AllocRecord* record = &recent_allocation_records_[gAllocRecordHead]; |
| 2876 | record->type = type; |
| 2877 | record->byte_count = byte_count; |
| 2878 | record->thin_lock_id = self->GetThinLockId(); |
| 2879 | |
| 2880 | // Fill in the stack trace. |
| 2881 | AllocRecordStackVisitor visitor(record); |
| 2882 | self->WalkStack(&visitor); |
| 2883 | |
| 2884 | if (gAllocRecordCount < kNumAllocRecords) { |
| 2885 | ++gAllocRecordCount; |
| 2886 | } |
| 2887 | } |
| 2888 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 2889 | // Returns the index of the head element. |
| 2890 | // |
| 2891 | // We point at the most-recently-written record, so if gAllocRecordCount is 1 |
| 2892 | // we want to use the current element. Take "head+1" and subtract count |
| 2893 | // from it. |
| 2894 | // |
| 2895 | // We need to handle underflow in our circular buffer, so we add |
| 2896 | // kNumAllocRecords and then mask it back down. |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame^] | 2897 | static inline int HeadIndex() EXCLUSIVE_LOCKS_REQUIRED(gAllocTrackerLock) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2898 | return (gAllocRecordHead+1 + kNumAllocRecords - gAllocRecordCount) & (kNumAllocRecords-1); |
| 2899 | } |
| 2900 | |
| 2901 | void Dbg::DumpRecentAllocations() { |
| 2902 | MutexLock mu(gAllocTrackerLock); |
| 2903 | if (recent_allocation_records_ == NULL) { |
| 2904 | LOG(INFO) << "Not recording tracked allocations"; |
| 2905 | return; |
| 2906 | } |
| 2907 | |
| 2908 | // "i" is the head of the list. We want to start at the end of the |
| 2909 | // list and move forward to the tail. |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 2910 | size_t i = HeadIndex(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2911 | size_t count = gAllocRecordCount; |
| 2912 | |
| 2913 | LOG(INFO) << "Tracked allocations, (head=" << gAllocRecordHead << " count=" << count << ")"; |
| 2914 | while (count--) { |
| 2915 | AllocRecord* record = &recent_allocation_records_[i]; |
| 2916 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 2917 | 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] | 2918 | << PrettyClass(record->type); |
| 2919 | |
| 2920 | for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) { |
| 2921 | const Method* m = record->stack[stack_frame].method; |
| 2922 | if (m == NULL) { |
| 2923 | break; |
| 2924 | } |
| 2925 | LOG(INFO) << " " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber(); |
| 2926 | } |
| 2927 | |
| 2928 | // pause periodically to help logcat catch up |
| 2929 | if ((count % 5) == 0) { |
| 2930 | usleep(40000); |
| 2931 | } |
| 2932 | |
| 2933 | i = (i + 1) & (kNumAllocRecords-1); |
| 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | class StringTable { |
| 2938 | public: |
| 2939 | StringTable() { |
| 2940 | } |
| 2941 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2942 | void Add(const char* s) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2943 | table_.insert(s); |
| 2944 | } |
| 2945 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 2946 | size_t IndexOf(const char* s) const { |
| 2947 | typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto |
| 2948 | It it = table_.find(s); |
| 2949 | if (it == table_.end()) { |
| 2950 | LOG(FATAL) << "IndexOf(\"" << s << "\") failed"; |
| 2951 | } |
| 2952 | return std::distance(table_.begin(), it); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2953 | } |
| 2954 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 2955 | size_t Size() const { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2956 | return table_.size(); |
| 2957 | } |
| 2958 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 2959 | void WriteTo(std::vector<uint8_t>& bytes) const { |
| 2960 | typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2961 | for (It it = table_.begin(); it != table_.end(); ++it) { |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 2962 | const char* s = (*it).c_str(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2963 | size_t s_len = CountModifiedUtf8Chars(s); |
| 2964 | UniquePtr<uint16_t> s_utf16(new uint16_t[s_len]); |
| 2965 | ConvertModifiedUtf8ToUtf16(s_utf16.get(), s); |
| 2966 | JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2967 | } |
| 2968 | } |
| 2969 | |
| 2970 | private: |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 2971 | std::set<std::string> table_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2972 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 2973 | }; |
| 2974 | |
| 2975 | /* |
| 2976 | * The data we send to DDMS contains everything we have recorded. |
| 2977 | * |
| 2978 | * Message header (all values big-endian): |
| 2979 | * (1b) message header len (to allow future expansion); includes itself |
| 2980 | * (1b) entry header len |
| 2981 | * (1b) stack frame len |
| 2982 | * (2b) number of entries |
| 2983 | * (4b) offset to string table from start of message |
| 2984 | * (2b) number of class name strings |
| 2985 | * (2b) number of method name strings |
| 2986 | * (2b) number of source file name strings |
| 2987 | * For each entry: |
| 2988 | * (4b) total allocation size |
| 2989 | * (2b) threadId |
| 2990 | * (2b) allocated object's class name index |
| 2991 | * (1b) stack depth |
| 2992 | * For each stack frame: |
| 2993 | * (2b) method's class name |
| 2994 | * (2b) method name |
| 2995 | * (2b) method source file |
| 2996 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 2997 | * (xb) class name strings |
| 2998 | * (xb) method name strings |
| 2999 | * (xb) source file strings |
| 3000 | * |
| 3001 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 3002 | * followed by UTF-16 data. |
| 3003 | * |
| 3004 | * We send up 16-bit unsigned indexes into string tables. In theory there |
| 3005 | * can be (kMaxAllocRecordStackDepth * kNumAllocRecords) unique strings in |
| 3006 | * each table, but in practice there should be far fewer. |
| 3007 | * |
| 3008 | * The chief reason for using a string table here is to keep the size of |
| 3009 | * the DDMS message to a minimum. This is partly to make the protocol |
| 3010 | * efficient, but also because we have to form the whole thing up all at |
| 3011 | * once in a memory buffer. |
| 3012 | * |
| 3013 | * We use separate string tables for class names, method names, and source |
| 3014 | * files to keep the indexes small. There will generally be no overlap |
| 3015 | * between the contents of these tables. |
| 3016 | */ |
| 3017 | jbyteArray Dbg::GetRecentAllocations() { |
| 3018 | if (false) { |
| 3019 | DumpRecentAllocations(); |
| 3020 | } |
| 3021 | |
| 3022 | MutexLock mu(gAllocTrackerLock); |
| 3023 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3024 | // |
| 3025 | // Part 1: generate string tables. |
| 3026 | // |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3027 | StringTable class_names; |
| 3028 | StringTable method_names; |
| 3029 | StringTable filenames; |
| 3030 | |
| 3031 | int count = gAllocRecordCount; |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3032 | int idx = HeadIndex(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3033 | while (count--) { |
| 3034 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 3035 | |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 3036 | class_names.Add(ClassHelper(record->type).GetDescriptor()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3037 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3038 | MethodHelper mh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3039 | for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3040 | Method* m = record->stack[i].method; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3041 | if (m != NULL) { |
Ian Rogers | ba37781 | 2012-05-28 21:16:29 -0700 | [diff] [blame] | 3042 | mh.ChangeMethod(m); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3043 | class_names.Add(mh.GetDeclaringClassDescriptor()); |
| 3044 | method_names.Add(mh.GetName()); |
| 3045 | filenames.Add(mh.GetDeclaringClassSourceFile()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 3050 | } |
| 3051 | |
| 3052 | LOG(INFO) << "allocation records: " << gAllocRecordCount; |
| 3053 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3054 | // |
| 3055 | // Part 2: allocate a buffer and generate the output. |
| 3056 | // |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3057 | std::vector<uint8_t> bytes; |
| 3058 | |
| 3059 | // (1b) message header len (to allow future expansion); includes itself |
| 3060 | // (1b) entry header len |
| 3061 | // (1b) stack frame len |
| 3062 | const int kMessageHeaderLen = 15; |
| 3063 | const int kEntryHeaderLen = 9; |
| 3064 | const int kStackFrameLen = 8; |
| 3065 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 3066 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 3067 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 3068 | |
| 3069 | // (2b) number of entries |
| 3070 | // (4b) offset to string table from start of message |
| 3071 | // (2b) number of class name strings |
| 3072 | // (2b) number of method name strings |
| 3073 | // (2b) number of source file name strings |
| 3074 | JDWP::Append2BE(bytes, gAllocRecordCount); |
| 3075 | size_t string_table_offset = bytes.size(); |
| 3076 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
| 3077 | JDWP::Append2BE(bytes, class_names.Size()); |
| 3078 | JDWP::Append2BE(bytes, method_names.Size()); |
| 3079 | JDWP::Append2BE(bytes, filenames.Size()); |
| 3080 | |
| 3081 | count = gAllocRecordCount; |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3082 | idx = HeadIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3083 | ClassHelper kh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3084 | while (count--) { |
| 3085 | // For each entry: |
| 3086 | // (4b) total allocation size |
| 3087 | // (2b) thread id |
| 3088 | // (2b) allocated object's class name index |
| 3089 | // (1b) stack depth |
| 3090 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 3091 | size_t stack_depth = record->GetDepth(); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3092 | kh.ChangeClass(record->type); |
| 3093 | size_t allocated_object_class_name_index = class_names.IndexOf(kh.GetDescriptor()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3094 | JDWP::Append4BE(bytes, record->byte_count); |
| 3095 | JDWP::Append2BE(bytes, record->thin_lock_id); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3096 | JDWP::Append2BE(bytes, allocated_object_class_name_index); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3097 | JDWP::Append1BE(bytes, stack_depth); |
| 3098 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3099 | MethodHelper mh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3100 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 3101 | // For each stack frame: |
| 3102 | // (2b) method's class name |
| 3103 | // (2b) method name |
| 3104 | // (2b) method source file |
| 3105 | // (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] | 3106 | mh.ChangeMethod(record->stack[stack_frame].method); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 3107 | size_t class_name_index = class_names.IndexOf(mh.GetDeclaringClassDescriptor()); |
| 3108 | size_t method_name_index = method_names.IndexOf(mh.GetName()); |
| 3109 | size_t file_name_index = filenames.IndexOf(mh.GetDeclaringClassSourceFile()); |
| 3110 | JDWP::Append2BE(bytes, class_name_index); |
| 3111 | JDWP::Append2BE(bytes, method_name_index); |
| 3112 | JDWP::Append2BE(bytes, file_name_index); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 3113 | JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber()); |
| 3114 | } |
| 3115 | |
| 3116 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 3117 | } |
| 3118 | |
| 3119 | // (xb) class name strings |
| 3120 | // (xb) method name strings |
| 3121 | // (xb) source file strings |
| 3122 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 3123 | class_names.WriteTo(bytes); |
| 3124 | method_names.WriteTo(bytes); |
| 3125 | filenames.WriteTo(bytes); |
| 3126 | |
| 3127 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 3128 | jbyteArray result = env->NewByteArray(bytes.size()); |
| 3129 | if (result != NULL) { |
| 3130 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 3131 | } |
| 3132 | return result; |
| 3133 | } |
| 3134 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3135 | } // namespace art |