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" |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 25 | #include "context.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 26 | #include "object_utils.h" |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 27 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 28 | #include "ScopedPrimitiveArray.h" |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 29 | #include "stack_indirect_reference_table.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 30 | #include "thread_list.h" |
| 31 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 32 | extern "C" void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*), void*); |
| 33 | #ifndef HAVE_ANDROID_OS |
| 34 | void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*), void*) { |
| 35 | // No-op for glibc. |
| 36 | } |
| 37 | #endif |
| 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 | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 44 | class ObjectRegistry { |
| 45 | public: |
| 46 | ObjectRegistry() : lock_("ObjectRegistry lock") { |
| 47 | } |
| 48 | |
| 49 | JDWP::ObjectId Add(Object* o) { |
| 50 | if (o == NULL) { |
| 51 | return 0; |
| 52 | } |
| 53 | JDWP::ObjectId id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(o)); |
| 54 | MutexLock mu(lock_); |
| 55 | map_[id] = o; |
| 56 | return id; |
| 57 | } |
| 58 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 59 | void Clear() { |
| 60 | MutexLock mu(lock_); |
| 61 | LOG(DEBUG) << "Debugger has detached; object registry had " << map_.size() << " entries"; |
| 62 | map_.clear(); |
| 63 | } |
| 64 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 65 | bool Contains(JDWP::ObjectId id) { |
| 66 | MutexLock mu(lock_); |
| 67 | return map_.find(id) != map_.end(); |
| 68 | } |
| 69 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 70 | template<typename T> T Get(JDWP::ObjectId id) { |
| 71 | MutexLock mu(lock_); |
| 72 | typedef std::map<JDWP::ObjectId, Object*>::iterator It; // C++0x auto |
| 73 | It it = map_.find(id); |
| 74 | return (it != map_.end()) ? reinterpret_cast<T>(it->second) : NULL; |
| 75 | } |
| 76 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 77 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 78 | MutexLock mu(lock_); |
| 79 | typedef std::map<JDWP::ObjectId, Object*>::iterator It; // C++0x auto |
| 80 | for (It it = map_.begin(); it != map_.end(); ++it) { |
| 81 | visitor(it->second, arg); |
| 82 | } |
| 83 | } |
| 84 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 85 | private: |
| 86 | Mutex lock_; |
| 87 | std::map<JDWP::ObjectId, Object*> map_; |
| 88 | }; |
| 89 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 90 | struct AllocRecordStackTraceElement { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 91 | Method* method; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 92 | uintptr_t raw_pc; |
| 93 | |
| 94 | int32_t LineNumber() const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 95 | return MethodHelper(method).GetLineNumFromNativePC(raw_pc); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 96 | } |
| 97 | }; |
| 98 | |
| 99 | struct AllocRecord { |
| 100 | Class* type; |
| 101 | size_t byte_count; |
| 102 | uint16_t thin_lock_id; |
| 103 | AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth]; // Unused entries have NULL method. |
| 104 | |
| 105 | size_t GetDepth() { |
| 106 | size_t depth = 0; |
| 107 | while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) { |
| 108 | ++depth; |
| 109 | } |
| 110 | return depth; |
| 111 | } |
| 112 | }; |
| 113 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 114 | // JDWP is allowed unless the Zygote forbids it. |
| 115 | static bool gJdwpAllowed = true; |
| 116 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 117 | // Was there a -Xrunjdwp or -agent argument on the command-line? |
| 118 | static bool gJdwpConfigured = false; |
| 119 | |
| 120 | // Broken-down JDWP options. (Only valid if gJdwpConfigured is true.) |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 121 | static JDWP::JdwpOptions gJdwpOptions; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 122 | |
| 123 | // Runtime JDWP state. |
| 124 | static JDWP::JdwpState* gJdwpState = NULL; |
| 125 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
| 126 | static bool gDebuggerActive; // debugger is making requests. |
| 127 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 128 | static bool gDdmThreadNotification = false; |
| 129 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 130 | // DDMS GC-related settings. |
| 131 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 132 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 133 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 134 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 135 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 136 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 137 | static ObjectRegistry* gRegistry = NULL; |
| 138 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 139 | // Recent allocation tracking. |
| 140 | static Mutex gAllocTrackerLock("AllocTracker lock"); |
| 141 | AllocRecord* Dbg::recent_allocation_records_ = NULL; // TODO: CircularBuffer<AllocRecord> |
| 142 | static size_t gAllocRecordHead = 0; |
| 143 | static size_t gAllocRecordCount = 0; |
| 144 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 145 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 146 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 147 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 148 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 149 | } |
| 150 | |
| 151 | static JDWP::JdwpTag TagFromClass(Class* c) { |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 152 | CHECK(c != NULL); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 153 | if (c->IsArrayClass()) { |
| 154 | return JDWP::JT_ARRAY; |
| 155 | } |
| 156 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 157 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 158 | if (c->IsStringClass()) { |
| 159 | return JDWP::JT_STRING; |
| 160 | } else if (c->IsClassClass()) { |
| 161 | return JDWP::JT_CLASS_OBJECT; |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 162 | } else if (c->InstanceOf(class_linker->FindSystemClass("Ljava/lang/Thread;"))) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 163 | return JDWP::JT_THREAD; |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 164 | } else if (c->InstanceOf(class_linker->FindSystemClass("Ljava/lang/ThreadGroup;"))) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 165 | return JDWP::JT_THREAD_GROUP; |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 166 | } else if (c->InstanceOf(class_linker->FindSystemClass("Ljava/lang/ClassLoader;"))) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 167 | return JDWP::JT_CLASS_LOADER; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 168 | } else { |
| 169 | return JDWP::JT_OBJECT; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Objects declared to hold Object might actually hold a more specific |
| 175 | * type. The debugger may take a special interest in these (e.g. it |
| 176 | * wants to display the contents of Strings), so we want to return an |
| 177 | * appropriate tag. |
| 178 | * |
| 179 | * Null objects are tagged JT_OBJECT. |
| 180 | */ |
| 181 | static JDWP::JdwpTag TagFromObject(const Object* o) { |
| 182 | return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(o->GetClass()); |
| 183 | } |
| 184 | |
| 185 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 186 | switch (tag) { |
| 187 | case JDWP::JT_BOOLEAN: |
| 188 | case JDWP::JT_BYTE: |
| 189 | case JDWP::JT_CHAR: |
| 190 | case JDWP::JT_FLOAT: |
| 191 | case JDWP::JT_DOUBLE: |
| 192 | case JDWP::JT_INT: |
| 193 | case JDWP::JT_LONG: |
| 194 | case JDWP::JT_SHORT: |
| 195 | case JDWP::JT_VOID: |
| 196 | return true; |
| 197 | default: |
| 198 | return false; |
| 199 | } |
| 200 | } |
| 201 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 202 | /* |
| 203 | * Handle one of the JDWP name/value pairs. |
| 204 | * |
| 205 | * JDWP options are: |
| 206 | * help: if specified, show help message and bail |
| 207 | * transport: may be dt_socket or dt_shmem |
| 208 | * address: for dt_socket, "host:port", or just "port" when listening |
| 209 | * server: if "y", wait for debugger to attach; if "n", attach to debugger |
| 210 | * timeout: how long to wait for debugger to connect / listen |
| 211 | * |
| 212 | * Useful with server=n (these aren't supported yet): |
| 213 | * onthrow=<exception-name>: connect to debugger when exception thrown |
| 214 | * onuncaught=y|n: connect to debugger when uncaught exception thrown |
| 215 | * launch=<command-line>: launch the debugger itself |
| 216 | * |
| 217 | * The "transport" option is required, as is "address" if server=n. |
| 218 | */ |
| 219 | static bool ParseJdwpOption(const std::string& name, const std::string& value) { |
| 220 | if (name == "transport") { |
| 221 | if (value == "dt_socket") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 222 | gJdwpOptions.transport = JDWP::kJdwpTransportSocket; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 223 | } else if (value == "dt_android_adb") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 224 | gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 225 | } else { |
| 226 | LOG(ERROR) << "JDWP transport not supported: " << value; |
| 227 | return false; |
| 228 | } |
| 229 | } else if (name == "server") { |
| 230 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 231 | gJdwpOptions.server = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 232 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 233 | gJdwpOptions.server = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 234 | } else { |
| 235 | LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'"; |
| 236 | return false; |
| 237 | } |
| 238 | } else if (name == "suspend") { |
| 239 | if (value == "n") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 240 | gJdwpOptions.suspend = false; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 241 | } else if (value == "y") { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 242 | gJdwpOptions.suspend = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 243 | } else { |
| 244 | LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'"; |
| 245 | return false; |
| 246 | } |
| 247 | } else if (name == "address") { |
| 248 | /* this is either <port> or <host>:<port> */ |
| 249 | std::string port_string; |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 250 | gJdwpOptions.host.clear(); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 251 | std::string::size_type colon = value.find(':'); |
| 252 | if (colon != std::string::npos) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 253 | gJdwpOptions.host = value.substr(0, colon); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 254 | port_string = value.substr(colon + 1); |
| 255 | } else { |
| 256 | port_string = value; |
| 257 | } |
| 258 | if (port_string.empty()) { |
| 259 | LOG(ERROR) << "JDWP address missing port: " << value; |
| 260 | return false; |
| 261 | } |
| 262 | char* end; |
| 263 | long port = strtol(port_string.c_str(), &end, 10); |
| 264 | if (*end != '\0') { |
| 265 | LOG(ERROR) << "JDWP address has junk in port field: " << value; |
| 266 | return false; |
| 267 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 268 | gJdwpOptions.port = port; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 269 | } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") { |
| 270 | /* valid but unsupported */ |
| 271 | LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'"; |
| 272 | } else { |
| 273 | LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'"; |
| 274 | } |
| 275 | |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.: |
| 281 | * "transport=dt_socket,address=8000,server=y,suspend=n" |
| 282 | */ |
| 283 | bool Dbg::ParseJdwpOptions(const std::string& options) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 284 | VLOG(jdwp) << "ParseJdwpOptions: " << options; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 285 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 286 | std::vector<std::string> pairs; |
| 287 | Split(options, ',', pairs); |
| 288 | |
| 289 | for (size_t i = 0; i < pairs.size(); ++i) { |
| 290 | std::string::size_type equals = pairs[i].find('='); |
| 291 | if (equals == std::string::npos) { |
| 292 | LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'"; |
| 293 | return false; |
| 294 | } |
| 295 | ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1)); |
| 296 | } |
| 297 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 298 | if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 299 | LOG(ERROR) << "Must specify JDWP transport: " << options; |
| 300 | } |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 301 | if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 302 | LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options; |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | gJdwpConfigured = true; |
| 307 | return true; |
| 308 | } |
| 309 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 310 | void Dbg::StartJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 311 | if (!gJdwpAllowed || !gJdwpConfigured) { |
| 312 | // No JDWP for you! |
| 313 | return; |
| 314 | } |
| 315 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 316 | CHECK(gRegistry == NULL); |
| 317 | gRegistry = new ObjectRegistry; |
| 318 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 319 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 320 | // debugger, passively listen for a debugger, or block waiting for a |
| 321 | // debugger. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 322 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
| 323 | if (gJdwpState == NULL) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 324 | // We probably failed because some other process has the port already, which means that |
| 325 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 326 | // talking to that other process. |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 327 | LOG(FATAL) << "Debugger thread failed to initialize"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // If a debugger has already attached, send the "welcome" message. |
| 331 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 332 | if (gJdwpState->IsActive()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 333 | //ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 334 | if (!gJdwpState->PostVMStart()) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 335 | LOG(WARNING) << "Failed to post 'start' message to debugger"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 340 | void Dbg::StopJdwp() { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 341 | delete gJdwpState; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 342 | delete gRegistry; |
| 343 | gRegistry = NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 346 | void Dbg::GcDidFinish() { |
| 347 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
| 348 | LOG(DEBUG) << "Sending VM heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 349 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 350 | } |
| 351 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
| 352 | LOG(DEBUG) << "Dumping VM heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 353 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 354 | } |
| 355 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
| 356 | LOG(DEBUG) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 357 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 361 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 362 | gJdwpAllowed = allowed; |
| 363 | } |
| 364 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 365 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 366 | return Thread::Current()->GetInvokeReq(); |
| 367 | } |
| 368 | |
| 369 | Thread* Dbg::GetDebugThread() { |
| 370 | return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL; |
| 371 | } |
| 372 | |
| 373 | void Dbg::ClearWaitForEventThread() { |
| 374 | gJdwpState->ClearWaitForEventThread(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 378 | CHECK(!gDebuggerConnected); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 379 | VLOG(jdwp) << "JDWP has attached"; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 380 | gDebuggerConnected = true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 383 | void Dbg::GoActive() { |
| 384 | // Enable all debugging features, including scans for breakpoints. |
| 385 | // This is a no-op if we're already active. |
| 386 | // Only called from the JDWP handler thread. |
| 387 | if (gDebuggerActive) { |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | LOG(INFO) << "Debugger is active"; |
| 392 | |
| 393 | // TODO: CHECK we don't have any outstanding breakpoints. |
| 394 | |
| 395 | gDebuggerActive = true; |
| 396 | |
| 397 | //dvmEnableAllSubMode(kSubModeDebuggerActive); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 401 | CHECK(gDebuggerConnected); |
| 402 | |
| 403 | gDebuggerActive = false; |
| 404 | |
| 405 | //dvmDisableAllSubMode(kSubModeDebuggerActive); |
| 406 | |
| 407 | gRegistry->Clear(); |
| 408 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | bool Dbg::IsDebuggerConnected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 412 | return gDebuggerActive; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | bool Dbg::IsDebuggingEnabled() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 416 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | int64_t Dbg::LastDebuggerActivity() { |
Elliott Hughes | ca95152 | 2011-12-05 12:01:32 -0800 | [diff] [blame] | 420 | return gJdwpState->LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | int Dbg::ThreadRunning() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 424 | return static_cast<int>(Thread::Current()->SetState(Thread::kRunnable)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | int Dbg::ThreadWaiting() { |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 428 | return static_cast<int>(Thread::Current()->SetState(Thread::kVmWait)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Elliott Hughes | 6ba581a | 2011-10-25 11:45:35 -0700 | [diff] [blame] | 431 | int Dbg::ThreadContinuing(int new_state) { |
| 432 | return static_cast<int>(Thread::Current()->SetState(static_cast<Thread::State>(new_state))); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 436 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void Dbg::Exit(int status) { |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 440 | exit(status); // This is all dalvik did. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 443 | void Dbg::VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 444 | if (gRegistry != NULL) { |
| 445 | gRegistry->VisitRoots(visitor, arg); |
| 446 | } |
| 447 | } |
| 448 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 449 | std::string Dbg::GetClassDescriptor(JDWP::RefTypeId classId) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 450 | Object* o = gRegistry->Get<Object*>(classId); |
| 451 | if (o == NULL || !o->IsClass()) { |
| 452 | return StringPrintf("non-class %p", o); // This is only used for debugging output anyway. |
| 453 | } |
| 454 | return ClassHelper(o->AsClass()).GetDescriptor(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 457 | bool Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& classObjectId) { |
| 458 | Object* o = gRegistry->Get<Object*>(id); |
| 459 | if (o == NULL || !o->IsClass()) { |
| 460 | return false; |
| 461 | } |
| 462 | classObjectId = gRegistry->Add(o); |
| 463 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 466 | bool Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclassId) { |
| 467 | Object* o = gRegistry->Get<Object*>(id); |
| 468 | if (o == NULL || !o->IsClass()) { |
| 469 | return false; |
| 470 | } |
| 471 | superclassId = gRegistry->Add(o->AsClass()->GetSuperClass()); |
| 472 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | JDWP::ObjectId Dbg::GetClassLoader(JDWP::RefTypeId id) { |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 476 | Object* o = gRegistry->Get<Object*>(id); |
| 477 | return gRegistry->Add(o->GetClass()->GetClassLoader()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 480 | bool Dbg::GetAccessFlags(JDWP::RefTypeId id, uint32_t& access_flags) { |
| 481 | Object* o = gRegistry->Get<Object*>(id); |
| 482 | if (o == NULL || !o->IsClass()) { |
| 483 | return false; |
| 484 | } |
| 485 | access_flags = o->AsClass()->GetAccessFlags() & kAccJavaFlagsMask; |
| 486 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 489 | bool Dbg::IsInterface(JDWP::RefTypeId classId, bool& is_interface) { |
| 490 | Object* o = gRegistry->Get<Object*>(classId); |
| 491 | if (o == NULL || !o->IsClass()) { |
| 492 | return false; |
| 493 | } |
| 494 | is_interface = o->AsClass()->IsInterface(); |
| 495 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 498 | void Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 499 | // Get the complete list of reference classes (i.e. all classes except |
| 500 | // the primitive types). |
| 501 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 502 | struct ClassListCreator { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 503 | ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) { |
| 504 | } |
| 505 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 506 | static bool Visit(Class* c, void* arg) { |
| 507 | return reinterpret_cast<ClassListCreator*>(arg)->Visit(c); |
| 508 | } |
| 509 | |
| 510 | bool Visit(Class* c) { |
| 511 | if (!c->IsPrimitive()) { |
| 512 | classes.push_back(static_cast<JDWP::RefTypeId>(gRegistry->Add(c))); |
| 513 | } |
| 514 | return true; |
| 515 | } |
| 516 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 517 | std::vector<JDWP::RefTypeId>& classes; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 518 | }; |
| 519 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 520 | ClassListCreator clc(classes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 521 | Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void Dbg::GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) { |
| 525 | UNIMPLEMENTED(FATAL); |
| 526 | } |
| 527 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 528 | bool Dbg::GetClassInfo(JDWP::RefTypeId classId, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) { |
| 529 | Object* o = gRegistry->Get<Object*>(classId); |
| 530 | if (o == NULL || !o->IsClass()) { |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | Class* c = o->AsClass(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 535 | if (c->IsArrayClass()) { |
| 536 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 537 | *pTypeTag = JDWP::TT_ARRAY; |
| 538 | } else { |
| 539 | if (c->IsErroneous()) { |
| 540 | *pStatus = JDWP::CS_ERROR; |
| 541 | } else { |
| 542 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 543 | } |
| 544 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 545 | } |
| 546 | |
| 547 | if (pDescriptor != NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 548 | *pDescriptor = ClassHelper(c).GetDescriptor(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 549 | } |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 550 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 553 | void Dbg::FindLoadedClassBySignature(const std::string& descriptor, std::vector<JDWP::RefTypeId>& ids) { |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 554 | std::vector<Class*> classes; |
| 555 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
| 556 | ids.clear(); |
| 557 | for (size_t i = 0; i < classes.size(); ++i) { |
| 558 | ids.push_back(gRegistry->Add(classes[i])); |
| 559 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 562 | void Dbg::GetObjectType(JDWP::ObjectId objectId, JDWP::JdwpTypeTag* pRefTypeTag, JDWP::RefTypeId* pRefTypeId) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 563 | Object* o = gRegistry->Get<Object*>(objectId); |
| 564 | if (o->GetClass()->IsArrayClass()) { |
| 565 | *pRefTypeTag = JDWP::TT_ARRAY; |
| 566 | } else if (o->GetClass()->IsInterface()) { |
| 567 | *pRefTypeTag = JDWP::TT_INTERFACE; |
| 568 | } else { |
| 569 | *pRefTypeTag = JDWP::TT_CLASS; |
| 570 | } |
| 571 | *pRefTypeId = gRegistry->Add(o->GetClass()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | uint8_t Dbg::GetClassObjectType(JDWP::RefTypeId refTypeId) { |
| 575 | UNIMPLEMENTED(FATAL); |
| 576 | return 0; |
| 577 | } |
| 578 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 579 | bool Dbg::GetSignature(JDWP::RefTypeId refTypeId, std::string& signature) { |
| 580 | Object* o = gRegistry->Get<Object*>(refTypeId); |
| 581 | if (o == NULL || !o->IsClass()) { |
| 582 | return false; |
| 583 | } |
| 584 | signature = ClassHelper(o->AsClass()).GetDescriptor(); |
| 585 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 588 | bool Dbg::GetSourceFile(JDWP::RefTypeId refTypeId, std::string& result) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 589 | Object* o = gRegistry->Get<Object*>(refTypeId); |
| 590 | if (o == NULL || !o->IsClass()) { |
| 591 | return false; |
| 592 | } |
| 593 | result = ClassHelper(o->AsClass()).GetSourceFile(); |
| 594 | return result != NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 597 | uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 598 | Object* o = gRegistry->Get<Object*>(objectId); |
| 599 | return TagFromObject(o); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 602 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 603 | switch (tag) { |
| 604 | case JDWP::JT_VOID: |
| 605 | return 0; |
| 606 | case JDWP::JT_BYTE: |
| 607 | case JDWP::JT_BOOLEAN: |
| 608 | return 1; |
| 609 | case JDWP::JT_CHAR: |
| 610 | case JDWP::JT_SHORT: |
| 611 | return 2; |
| 612 | case JDWP::JT_FLOAT: |
| 613 | case JDWP::JT_INT: |
| 614 | return 4; |
| 615 | case JDWP::JT_ARRAY: |
| 616 | case JDWP::JT_OBJECT: |
| 617 | case JDWP::JT_STRING: |
| 618 | case JDWP::JT_THREAD: |
| 619 | case JDWP::JT_THREAD_GROUP: |
| 620 | case JDWP::JT_CLASS_LOADER: |
| 621 | case JDWP::JT_CLASS_OBJECT: |
| 622 | return sizeof(JDWP::ObjectId); |
| 623 | case JDWP::JT_DOUBLE: |
| 624 | case JDWP::JT_LONG: |
| 625 | return 8; |
| 626 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 627 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 628 | return -1; |
| 629 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | int Dbg::GetArrayLength(JDWP::ObjectId arrayId) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 633 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 634 | Array* a = o->AsArray(); |
| 635 | return a->GetLength(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | uint8_t Dbg::GetArrayElementTag(JDWP::ObjectId arrayId) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 639 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 640 | Array* a = o->AsArray(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 641 | std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 642 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 643 | if (!IsPrimitiveTag(tag)) { |
| 644 | tag = TagFromClass(a->GetClass()->GetComponentType()); |
| 645 | } |
| 646 | return tag; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 649 | bool Dbg::OutputArray(JDWP::ObjectId arrayId, int offset, int count, JDWP::ExpandBuf* pReply) { |
| 650 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 651 | Array* a = o->AsArray(); |
| 652 | |
| 653 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 654 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
| 655 | return false; |
| 656 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 657 | std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 658 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 659 | |
| 660 | if (IsPrimitiveTag(tag)) { |
| 661 | size_t width = GetTagWidth(tag); |
| 662 | const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData()); |
| 663 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 664 | if (width == 8) { |
| 665 | const uint64_t* src8 = reinterpret_cast<const uint64_t*>(src); |
| 666 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 667 | } else if (width == 4) { |
| 668 | const uint32_t* src4 = reinterpret_cast<const uint32_t*>(src); |
| 669 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 670 | } else if (width == 2) { |
| 671 | const uint16_t* src2 = reinterpret_cast<const uint16_t*>(src); |
| 672 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 673 | } else { |
| 674 | memcpy(dst, &src[offset * width], count * width); |
| 675 | } |
| 676 | } else { |
| 677 | ObjectArray<Object>* oa = a->AsObjectArray<Object>(); |
| 678 | for (int i = 0; i < count; ++i) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 679 | Object* element = oa->Get(offset + i); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 680 | JDWP::JdwpTag specific_tag = (element != NULL) ? TagFromObject(element) : tag; |
| 681 | expandBufAdd1(pReply, specific_tag); |
| 682 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 689 | bool Dbg::SetArrayElements(JDWP::ObjectId arrayId, int offset, int count, const uint8_t* src) { |
| 690 | Object* o = gRegistry->Get<Object*>(arrayId); |
| 691 | Array* a = o->AsArray(); |
| 692 | |
| 693 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 694 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
| 695 | return false; |
| 696 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 697 | std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor()); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 698 | JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1); |
| 699 | |
| 700 | if (IsPrimitiveTag(tag)) { |
| 701 | size_t width = GetTagWidth(tag); |
| 702 | uint8_t* dst = &(reinterpret_cast<uint8_t*>(a->GetRawData())[offset * width]); |
| 703 | if (width == 8) { |
| 704 | for (int i = 0; i < count; ++i) { |
| 705 | // Handle potentially non-aligned memory access one byte at a time for ARM's benefit. |
| 706 | uint64_t value; |
| 707 | for (size_t j = 0; j < sizeof(uint64_t); ++j) reinterpret_cast<uint8_t*>(&value)[j] = src[j]; |
| 708 | src += sizeof(uint64_t); |
| 709 | JDWP::Write8BE(&dst, value); |
| 710 | } |
| 711 | } else if (width == 4) { |
| 712 | const uint32_t* src4 = reinterpret_cast<const uint32_t*>(src); |
| 713 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[i]); |
| 714 | } else if (width == 2) { |
| 715 | const uint16_t* src2 = reinterpret_cast<const uint16_t*>(src); |
| 716 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[i]); |
| 717 | } else { |
| 718 | memcpy(&dst[offset * width], src, count * width); |
| 719 | } |
| 720 | } else { |
| 721 | ObjectArray<Object>* oa = a->AsObjectArray<Object>(); |
| 722 | for (int i = 0; i < count; ++i) { |
| 723 | JDWP::ObjectId id = JDWP::ReadObjectId(&src); |
| 724 | oa->Set(offset + i, gRegistry->Get<Object*>(id)); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 731 | JDWP::ObjectId Dbg::CreateString(const std::string& str) { |
| 732 | return gRegistry->Add(String::AllocFromModifiedUtf8(str.c_str())); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 735 | bool Dbg::CreateObject(JDWP::RefTypeId classId, JDWP::ObjectId& new_object) { |
| 736 | Object* o = gRegistry->Get<Object*>(classId); |
| 737 | if (o == NULL || !o->IsClass()) { |
| 738 | return false; |
| 739 | } |
| 740 | new_object = gRegistry->Add(o->AsClass()->AllocObject()); |
| 741 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 744 | /* |
| 745 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 746 | */ |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 747 | bool Dbg::CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length, JDWP::ObjectId& new_array) { |
| 748 | Object* o = gRegistry->Get<Object*>(arrayTypeId); |
| 749 | if (o == NULL || !o->IsClass()) { |
| 750 | return false; |
| 751 | } |
| 752 | new_array = gRegistry->Add(Array::Alloc(o->AsClass(), length)); |
| 753 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 757 | // TODO: error handling if the RefTypeIds aren't actually Class*s. |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 758 | return gRegistry->Get<Class*>(instClassId)->InstanceOf(gRegistry->Get<Class*>(classId)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 761 | JDWP::FieldId ToFieldId(Field* f) { |
| 762 | #ifdef MOVING_GARBAGE_COLLECTOR |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 763 | UNIMPLEMENTED(FATAL); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 764 | #else |
| 765 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
| 766 | #endif |
| 767 | } |
| 768 | |
| 769 | JDWP::MethodId ToMethodId(Method* m) { |
| 770 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 771 | UNIMPLEMENTED(FATAL); |
| 772 | #else |
| 773 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
| 774 | #endif |
| 775 | } |
| 776 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 777 | Field* FromFieldId(JDWP::FieldId fid) { |
| 778 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 779 | UNIMPLEMENTED(FATAL); |
| 780 | #else |
| 781 | return reinterpret_cast<Field*>(static_cast<uintptr_t>(fid)); |
| 782 | #endif |
| 783 | } |
| 784 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 785 | Method* FromMethodId(JDWP::MethodId mid) { |
| 786 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 787 | UNIMPLEMENTED(FATAL); |
| 788 | #else |
| 789 | return reinterpret_cast<Method*>(static_cast<uintptr_t>(mid)); |
| 790 | #endif |
| 791 | } |
| 792 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 793 | void SetLocation(JDWP::JdwpLocation& location, Method* m, uintptr_t native_pc) { |
| 794 | Class* c = m->GetDeclaringClass(); |
| 795 | location.typeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 796 | location.classId = gRegistry->Add(c); |
| 797 | location.methodId = ToMethodId(m); |
| 798 | location.idx = m->IsNative() ? -1 : m->ToDexPC(native_pc); |
| 799 | } |
| 800 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 801 | std::string Dbg::GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 802 | Method* m = FromMethodId(methodId); |
| 803 | return MethodHelper(m).GetName(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 806 | /* |
| 807 | * Augment the access flags for synthetic methods and fields by setting |
| 808 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 809 | * flags not specified by the Java programming language. |
| 810 | */ |
| 811 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 812 | accessFlags &= kAccJavaFlagsMask; |
| 813 | if ((accessFlags & kAccSynthetic) != 0) { |
| 814 | accessFlags |= 0xf0000000; |
| 815 | } |
| 816 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 819 | static const uint16_t kEclipseWorkaroundSlot = 1000; |
| 820 | |
| 821 | /* |
| 822 | * Eclipse appears to expect that the "this" reference is in slot zero. |
| 823 | * If it's not, the "variables" display will show two copies of "this", |
| 824 | * possibly because it gets "this" from SF.ThisObject and then displays |
| 825 | * all locals with nonzero slot numbers. |
| 826 | * |
| 827 | * So, we remap the item in slot 0 to 1000, and remap "this" to zero. On |
| 828 | * SF.GetValues / SF.SetValues we map them back. |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 829 | * |
| 830 | * TODO: jdb uses the value to determine whether a variable is a local or an argument, |
| 831 | * by checking whether it's less than the number of arguments. To make that work, we'd |
| 832 | * 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] | 833 | */ |
| 834 | static uint16_t MangleSlot(uint16_t slot, const char* name) { |
| 835 | uint16_t newSlot = slot; |
| 836 | if (strcmp(name, "this") == 0) { |
| 837 | newSlot = 0; |
| 838 | } else if (slot == 0) { |
| 839 | newSlot = kEclipseWorkaroundSlot; |
| 840 | } |
| 841 | return newSlot; |
| 842 | } |
| 843 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 844 | static uint16_t DemangleSlot(uint16_t slot, Frame& f) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 845 | if (slot == kEclipseWorkaroundSlot) { |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 846 | return 0; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 847 | } else if (slot == 0) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 848 | const DexFile::CodeItem* code_item = MethodHelper(f.GetMethod()).GetCodeItem(); |
| 849 | return code_item->registers_size_ - code_item->ins_size_; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 850 | } |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 851 | return slot; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 854 | bool Dbg::OutputDeclaredFields(JDWP::RefTypeId refTypeId, bool with_generic, JDWP::ExpandBuf* pReply) { |
| 855 | Object* o = gRegistry->Get<Object*>(refTypeId); |
| 856 | if (o == NULL || !o->IsClass()) { |
| 857 | return false; |
| 858 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 859 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 860 | Class* c = o->AsClass(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 861 | size_t instance_field_count = c->NumInstanceFields(); |
| 862 | size_t static_field_count = c->NumStaticFields(); |
| 863 | |
| 864 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 865 | |
| 866 | for (size_t i = 0; i < instance_field_count + static_field_count; ++i) { |
| 867 | 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] | 868 | FieldHelper fh(f); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 869 | expandBufAddFieldId(pReply, ToFieldId(f)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 870 | expandBufAddUtf8String(pReply, fh.GetName()); |
| 871 | expandBufAddUtf8String(pReply, fh.GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 872 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 873 | static const char genericSignature[1] = ""; |
| 874 | expandBufAddUtf8String(pReply, genericSignature); |
| 875 | } |
| 876 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 877 | } |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 878 | return true; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 881 | bool Dbg::OutputDeclaredMethods(JDWP::RefTypeId refTypeId, bool with_generic, JDWP::ExpandBuf* pReply) { |
| 882 | Object* o = gRegistry->Get<Object*>(refTypeId); |
| 883 | if (o == NULL || !o->IsClass()) { |
| 884 | return false; |
| 885 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 886 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 887 | Class* c = o->AsClass(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 888 | size_t direct_method_count = c->NumDirectMethods(); |
| 889 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 890 | |
| 891 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 892 | |
| 893 | for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) { |
| 894 | 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] | 895 | MethodHelper mh(m); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 896 | expandBufAddMethodId(pReply, ToMethodId(m)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 897 | expandBufAddUtf8String(pReply, mh.GetName()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 898 | expandBufAddUtf8String(pReply, mh.GetSignature()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 899 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 900 | static const char genericSignature[1] = ""; |
| 901 | expandBufAddUtf8String(pReply, genericSignature); |
| 902 | } |
| 903 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 904 | } |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 905 | return true; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 906 | } |
| 907 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 908 | bool Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply) { |
| 909 | Object* o = gRegistry->Get<Object*>(refTypeId); |
| 910 | if (o == NULL || !o->IsClass()) { |
| 911 | return false; |
| 912 | } |
| 913 | ClassHelper kh(o->AsClass()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 914 | size_t interface_count = kh.NumInterfaces(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 915 | expandBufAdd4BE(pReply, interface_count); |
| 916 | for (size_t i = 0; i < interface_count; ++i) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 917 | expandBufAddRefTypeId(pReply, gRegistry->Add(kh.GetInterface(i))); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 918 | } |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 919 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | void Dbg::OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 923 | struct DebugCallbackContext { |
| 924 | int numItems; |
| 925 | JDWP::ExpandBuf* pReply; |
| 926 | |
| 927 | static bool Callback(void* context, uint32_t address, uint32_t lineNum) { |
| 928 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 929 | expandBufAdd8BE(pContext->pReply, address); |
| 930 | expandBufAdd4BE(pContext->pReply, lineNum); |
| 931 | pContext->numItems++; |
| 932 | return true; |
| 933 | } |
| 934 | }; |
| 935 | |
| 936 | Method* m = FromMethodId(methodId); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 937 | MethodHelper mh(m); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 938 | uint64_t start, end; |
| 939 | if (m->IsNative()) { |
| 940 | start = -1; |
| 941 | end = -1; |
| 942 | } else { |
| 943 | start = 0; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 944 | // TODO: what are the units supposed to be? *2? |
| 945 | end = mh.GetCodeItem()->insns_size_in_code_units_; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | expandBufAdd8BE(pReply, start); |
| 949 | expandBufAdd8BE(pReply, end); |
| 950 | |
| 951 | // Add numLines later |
| 952 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 953 | expandBufAdd4BE(pReply, 0); |
| 954 | |
| 955 | DebugCallbackContext context; |
| 956 | context.numItems = 0; |
| 957 | context.pReply = pReply; |
| 958 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 959 | mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(), |
| 960 | DebugCallbackContext::Callback, NULL, &context); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 961 | |
| 962 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 965 | void Dbg::OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, bool with_generic, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 966 | struct DebugCallbackContext { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 967 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 968 | size_t variable_count; |
| 969 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 970 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 971 | 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] | 972 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 973 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 974 | VLOG(jdwp) << StringPrintf(" %2d: %d(%d) '%s' '%s' '%s' slot=%d", pContext->variable_count, startAddress, endAddress - startAddress, name, descriptor, signature, slot); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 975 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 976 | slot = MangleSlot(slot, name); |
| 977 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 978 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 979 | expandBufAddUtf8String(pContext->pReply, name); |
| 980 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 981 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 982 | expandBufAddUtf8String(pContext->pReply, signature); |
| 983 | } |
| 984 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 985 | expandBufAdd4BE(pContext->pReply, slot); |
| 986 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 987 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 988 | } |
| 989 | }; |
| 990 | |
| 991 | Method* m = FromMethodId(methodId); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 992 | MethodHelper mh(m); |
| 993 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 994 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 995 | // arg_count considers doubles and longs to take 2 units. |
| 996 | // variable_count considers everything to take 1 unit. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 997 | std::string shorty(mh.GetShorty()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 998 | expandBufAdd4BE(pReply, m->NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 999 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1000 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1001 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1002 | expandBufAdd4BE(pReply, 0); |
| 1003 | |
| 1004 | DebugCallbackContext context; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1005 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1006 | context.variable_count = 0; |
| 1007 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1008 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1009 | mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL, |
| 1010 | DebugCallbackContext::Callback, &context); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1011 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1012 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1015 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId fieldId) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1016 | return BasicTagFromDescriptor(FieldHelper(FromFieldId(fieldId)).GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1019 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId fieldId) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1020 | return BasicTagFromDescriptor(FieldHelper(FromFieldId(fieldId)).GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | void Dbg::GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1024 | Object* o = gRegistry->Get<Object*>(objectId); |
| 1025 | Field* f = FromFieldId(fieldId); |
| 1026 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1027 | JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1028 | |
| 1029 | if (IsPrimitiveTag(tag)) { |
| 1030 | expandBufAdd1(pReply, tag); |
| 1031 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 1032 | expandBufAdd1(pReply, f->Get32(o)); |
| 1033 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 1034 | expandBufAdd2BE(pReply, f->Get32(o)); |
| 1035 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 1036 | expandBufAdd4BE(pReply, f->Get32(o)); |
| 1037 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1038 | expandBufAdd8BE(pReply, f->Get64(o)); |
| 1039 | } else { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1040 | LOG(FATAL) << "Unknown tag: " << tag; |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1041 | } |
| 1042 | } else { |
| 1043 | Object* value = f->GetObject(o); |
| 1044 | expandBufAdd1(pReply, TagFromObject(value)); |
| 1045 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 1046 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | void Dbg::SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width) { |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1050 | Object* o = gRegistry->Get<Object*>(objectId); |
| 1051 | Field* f = FromFieldId(fieldId); |
| 1052 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1053 | JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1054 | |
| 1055 | if (IsPrimitiveTag(tag)) { |
| 1056 | if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1057 | f->Set64(o, value); |
| 1058 | } else { |
| 1059 | f->Set32(o, value); |
| 1060 | } |
| 1061 | } else { |
| 1062 | f->SetObject(o, gRegistry->Get<Object*>(value)); |
| 1063 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1066 | void Dbg::GetStaticFieldValue(JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) { |
| 1067 | GetFieldValue(0, fieldId, pReply); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1070 | void Dbg::SetStaticFieldValue(JDWP::FieldId fieldId, uint64_t value, int width) { |
| 1071 | SetFieldValue(0, fieldId, value, width); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1074 | std::string Dbg::StringToUtf8(JDWP::ObjectId strId) { |
| 1075 | String* s = gRegistry->Get<String*>(strId); |
| 1076 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1079 | Thread* DecodeThread(JDWP::ObjectId threadId) { |
| 1080 | Object* thread_peer = gRegistry->Get<Object*>(threadId); |
| 1081 | CHECK(thread_peer != NULL); |
| 1082 | return Thread::FromManagedThread(thread_peer); |
| 1083 | } |
| 1084 | |
| 1085 | bool Dbg::GetThreadName(JDWP::ObjectId threadId, std::string& name) { |
| 1086 | ScopedThreadListLock thread_list_lock; |
| 1087 | Thread* thread = DecodeThread(threadId); |
| 1088 | if (thread == NULL) { |
| 1089 | return false; |
| 1090 | } |
| 1091 | StringAppendF(&name, "<%d> %s", thread->GetThinLockId(), thread->GetName()->ToModifiedUtf8().c_str()); |
| 1092 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | JDWP::ObjectId Dbg::GetThreadGroup(JDWP::ObjectId threadId) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1096 | Object* thread = gRegistry->Get<Object*>(threadId); |
| 1097 | CHECK(thread != NULL); |
| 1098 | |
| 1099 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Thread;"); |
| 1100 | CHECK(c != NULL); |
| 1101 | Field* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;"); |
| 1102 | CHECK(f != NULL); |
| 1103 | Object* group = f->GetObject(thread); |
| 1104 | CHECK(group != NULL); |
| 1105 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1108 | std::string Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) { |
| 1109 | Object* thread_group = gRegistry->Get<Object*>(threadGroupId); |
| 1110 | CHECK(thread_group != NULL); |
| 1111 | |
| 1112 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1113 | CHECK(c != NULL); |
| 1114 | Field* f = c->FindInstanceField("name", "Ljava/lang/String;"); |
| 1115 | CHECK(f != NULL); |
| 1116 | String* s = reinterpret_cast<String*>(f->GetObject(thread_group)); |
| 1117 | return s->ToModifiedUtf8(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1121 | Object* thread_group = gRegistry->Get<Object*>(threadGroupId); |
| 1122 | CHECK(thread_group != NULL); |
| 1123 | |
| 1124 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1125 | CHECK(c != NULL); |
| 1126 | Field* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;"); |
| 1127 | CHECK(f != NULL); |
| 1128 | Object* parent = f->GetObject(thread_group); |
| 1129 | return gRegistry->Add(parent); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1132 | static Object* GetStaticThreadGroup(const char* field_name) { |
| 1133 | Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 1134 | CHECK(c != NULL); |
| 1135 | Field* f = c->FindStaticField(field_name, "Ljava/lang/ThreadGroup;"); |
| 1136 | CHECK(f != NULL); |
| 1137 | Object* group = f->GetObject(NULL); |
| 1138 | CHECK(group != NULL); |
| 1139 | return group; |
| 1140 | } |
| 1141 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1142 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1143 | return gRegistry->Add(GetStaticThreadGroup("mSystem")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | JDWP::ObjectId Dbg::GetMainThreadGroupId() { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1147 | return gRegistry->Add(GetStaticThreadGroup("mMain")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1150 | bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, JDWP::JdwpThreadStatus* pThreadStatus, JDWP::JdwpSuspendStatus* pSuspendStatus) { |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1151 | ScopedThreadListLock thread_list_lock; |
| 1152 | |
| 1153 | Thread* thread = DecodeThread(threadId); |
| 1154 | if (thread == NULL) { |
| 1155 | return false; |
| 1156 | } |
| 1157 | |
| 1158 | switch (thread->GetState()) { |
| 1159 | case Thread::kTerminated: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1160 | case Thread::kRunnable: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1161 | case Thread::kTimedWaiting: *pThreadStatus = JDWP::TS_SLEEPING; break; |
| 1162 | case Thread::kBlocked: *pThreadStatus = JDWP::TS_MONITOR; break; |
| 1163 | case Thread::kWaiting: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1164 | case Thread::kInitializing: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1165 | case Thread::kStarting: *pThreadStatus = JDWP::TS_ZOMBIE; break; |
| 1166 | case Thread::kNative: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1167 | case Thread::kVmWait: *pThreadStatus = JDWP::TS_WAIT; break; |
| 1168 | case Thread::kSuspended: *pThreadStatus = JDWP::TS_RUNNING; break; |
| 1169 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1170 | LOG(FATAL) << "Unknown thread state " << thread->GetState(); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1171 | } |
| 1172 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1173 | *pSuspendStatus = (thread->IsSuspended() ? JDWP::SUSPEND_STATUS_SUSPENDED : JDWP::SUSPEND_STATUS_NOT_SUSPENDED); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1174 | |
| 1175 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | uint32_t Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1179 | return DecodeThread(threadId)->GetSuspendCount(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | bool Dbg::ThreadExists(JDWP::ObjectId threadId) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1183 | return DecodeThread(threadId) != NULL; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | bool Dbg::IsSuspended(JDWP::ObjectId threadId) { |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 1187 | return DecodeThread(threadId)->IsSuspended(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1190 | void Dbg::GetThreadGroupThreadsImpl(Object* thread_group, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
| 1191 | struct ThreadListVisitor { |
| 1192 | static void Visit(Thread* t, void* arg) { |
| 1193 | reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t); |
| 1194 | } |
| 1195 | |
| 1196 | void Visit(Thread* t) { |
| 1197 | if (t == Dbg::GetDebugThread()) { |
| 1198 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 1199 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 1200 | return; |
| 1201 | } |
| 1202 | if (thread_group == NULL || t->GetThreadGroup() == thread_group) { |
| 1203 | threads.push_back(gRegistry->Add(t->GetPeer())); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | Object* thread_group; |
| 1208 | std::vector<JDWP::ObjectId> threads; |
| 1209 | }; |
| 1210 | |
| 1211 | ThreadListVisitor tlv; |
| 1212 | tlv.thread_group = thread_group; |
| 1213 | |
| 1214 | { |
| 1215 | ScopedThreadListLock thread_list_lock; |
| 1216 | Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv); |
| 1217 | } |
| 1218 | |
| 1219 | *pThreadCount = tlv.threads.size(); |
| 1220 | if (*pThreadCount == 0) { |
| 1221 | *ppThreadIds = NULL; |
| 1222 | } else { |
| 1223 | *ppThreadIds = new JDWP::ObjectId[*pThreadCount]; |
| 1224 | for (size_t i = 0; i < *pThreadCount; ++i) { |
| 1225 | (*ppThreadIds)[i] = tlv.threads[i]; |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1230 | void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1231 | GetThreadGroupThreadsImpl(gRegistry->Get<Object*>(threadGroupId), ppThreadIds, pThreadCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1235 | GetThreadGroupThreadsImpl(NULL, ppThreadIds, pThreadCount); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1239 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1240 | struct CountStackDepthVisitor : public Thread::StackVisitor { |
| 1241 | CountStackDepthVisitor() : depth(0) {} |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1242 | virtual void VisitFrame(const Frame& f, uintptr_t) { |
| 1243 | // TODO: we'll need to skip callee-save frames too. |
| 1244 | if (f.HasMethod()) { |
| 1245 | ++depth; |
| 1246 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1247 | } |
| 1248 | size_t depth; |
| 1249 | }; |
| 1250 | CountStackDepthVisitor visitor; |
| 1251 | DecodeThread(threadId)->WalkStack(&visitor); |
| 1252 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1255 | bool Dbg::GetThreadFrame(JDWP::ObjectId threadId, int desired_frame_number, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) { |
| 1256 | ScopedThreadListLock thread_list_lock; |
| 1257 | struct GetFrameVisitor : public Thread::StackVisitor { |
| 1258 | GetFrameVisitor(int desired_frame_number, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) |
| 1259 | : found(false) ,depth(0), desired_frame_number(desired_frame_number), pFrameId(pFrameId), pLoc(pLoc) { |
| 1260 | } |
| 1261 | virtual void VisitFrame(const Frame& f, uintptr_t pc) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1262 | // TODO: we'll need to skip callee-save frames too. |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1263 | if (!f.HasMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 1264 | return; // 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] | 1265 | } |
| 1266 | |
| 1267 | if (depth == desired_frame_number) { |
| 1268 | *pFrameId = reinterpret_cast<JDWP::FrameId>(f.GetSP()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1269 | SetLocation(*pLoc, f.GetMethod(), pc); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1270 | found = true; |
| 1271 | } |
| 1272 | ++depth; |
| 1273 | } |
| 1274 | bool found; |
| 1275 | int depth; |
| 1276 | int desired_frame_number; |
| 1277 | JDWP::FrameId* pFrameId; |
| 1278 | JDWP::JdwpLocation* pLoc; |
| 1279 | }; |
| 1280 | GetFrameVisitor visitor(desired_frame_number, pFrameId, pLoc); |
| 1281 | visitor.desired_frame_number = desired_frame_number; |
| 1282 | DecodeThread(threadId)->WalkStack(&visitor); |
| 1283 | return visitor.found; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1287 | return gRegistry->Add(Thread::Current()->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1290 | void Dbg::SuspendVM() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1291 | ScopedThreadStateChange tsc(Thread::Current(), Thread::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] | 1292 | Runtime::Current()->GetThreadList()->SuspendAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | void Dbg::ResumeVM() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1296 | Runtime::Current()->GetThreadList()->ResumeAll(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | void Dbg::SuspendThread(JDWP::ObjectId threadId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1300 | Object* peer = gRegistry->Get<Object*>(threadId); |
| 1301 | ScopedThreadListLock thread_list_lock; |
| 1302 | Thread* thread = Thread::FromManagedThread(peer); |
| 1303 | if (thread == NULL) { |
| 1304 | LOG(WARNING) << "No such thread for suspend: " << peer; |
| 1305 | return; |
| 1306 | } |
| 1307 | Runtime::Current()->GetThreadList()->Suspend(thread, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | void Dbg::ResumeThread(JDWP::ObjectId threadId) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 1311 | Object* peer = gRegistry->Get<Object*>(threadId); |
| 1312 | ScopedThreadListLock thread_list_lock; |
| 1313 | Thread* thread = Thread::FromManagedThread(peer); |
| 1314 | if (thread == NULL) { |
| 1315 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 1316 | return; |
| 1317 | } |
| 1318 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1322 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1325 | bool Dbg::GetThisObject(JDWP::FrameId frameId, JDWP::ObjectId* pThisId) { |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1326 | Method** sp = reinterpret_cast<Method**>(frameId); |
| 1327 | Frame f; |
| 1328 | f.SetSP(sp); |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1329 | Method* m = f.GetMethod(); |
| 1330 | |
| 1331 | Object* o = NULL; |
| 1332 | if (!m->IsNative() && !m->IsStatic()) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1333 | uint16_t reg = DemangleSlot(0, f); |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 1334 | o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
| 1335 | } |
| 1336 | *pThisId = gRegistry->Add(o); |
| 1337 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1340 | 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] | 1341 | Method** sp = reinterpret_cast<Method**>(frameId); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1342 | Frame f; |
| 1343 | f.SetSP(sp); |
| 1344 | uint16_t reg = DemangleSlot(slot, f); |
| 1345 | Method* m = f.GetMethod(); |
| 1346 | |
| 1347 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 1348 | uint32_t vmap_offset; |
| 1349 | if (vmap_table.IsInContext(reg, vmap_offset)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1350 | 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] | 1351 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1352 | |
| 1353 | switch (tag) { |
| 1354 | case JDWP::JT_BOOLEAN: |
| 1355 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1356 | CHECK_EQ(width, 1U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1357 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1358 | VLOG(jdwp) << "get boolean local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1359 | JDWP::Set1(buf+1, intVal != 0); |
| 1360 | } |
| 1361 | break; |
| 1362 | case JDWP::JT_BYTE: |
| 1363 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1364 | CHECK_EQ(width, 1U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1365 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1366 | VLOG(jdwp) << "get byte local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1367 | JDWP::Set1(buf+1, intVal); |
| 1368 | } |
| 1369 | break; |
| 1370 | case JDWP::JT_SHORT: |
| 1371 | case JDWP::JT_CHAR: |
| 1372 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1373 | CHECK_EQ(width, 2U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1374 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1375 | VLOG(jdwp) << "get short/char local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1376 | JDWP::Set2BE(buf+1, intVal); |
| 1377 | } |
| 1378 | break; |
| 1379 | case JDWP::JT_INT: |
| 1380 | case JDWP::JT_FLOAT: |
| 1381 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1382 | CHECK_EQ(width, 4U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1383 | uint32_t intVal = f.GetVReg(m, reg); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1384 | VLOG(jdwp) << "get int/float local " << reg << " = " << intVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1385 | JDWP::Set4BE(buf+1, intVal); |
| 1386 | } |
| 1387 | break; |
| 1388 | case JDWP::JT_ARRAY: |
| 1389 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1390 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1391 | Object* o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1392 | VLOG(jdwp) << "get array local " << reg << " = " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1393 | if (o != NULL && !Heap::IsHeapAddress(o)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1394 | LOG(FATAL) << "Register " << reg << " expected to hold array: " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1395 | } |
| 1396 | JDWP::SetObjectId(buf+1, gRegistry->Add(o)); |
| 1397 | } |
| 1398 | break; |
| 1399 | case JDWP::JT_OBJECT: |
| 1400 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1401 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1402 | Object* o = reinterpret_cast<Object*>(f.GetVReg(m, reg)); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1403 | VLOG(jdwp) << "get object local " << reg << " = " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1404 | if (o != NULL && !Heap::IsHeapAddress(o)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1405 | LOG(FATAL) << "Register " << reg << " expected to hold object: " << o; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1406 | } |
| 1407 | tag = TagFromObject(o); |
| 1408 | JDWP::SetObjectId(buf+1, gRegistry->Add(o)); |
| 1409 | } |
| 1410 | break; |
| 1411 | case JDWP::JT_DOUBLE: |
| 1412 | case JDWP::JT_LONG: |
| 1413 | { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1414 | CHECK_EQ(width, 8U); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 1415 | uint32_t lo = f.GetVReg(m, reg); |
| 1416 | uint64_t hi = f.GetVReg(m, reg + 1); |
| 1417 | uint64_t longVal = (hi << 32) | lo; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1418 | VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1419 | JDWP::Set8BE(buf+1, longVal); |
| 1420 | } |
| 1421 | break; |
| 1422 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1423 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1424 | break; |
| 1425 | } |
| 1426 | |
| 1427 | // Prepend tag, which may have been updated. |
| 1428 | JDWP::Set1(buf, tag); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1431 | 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] | 1432 | Method** sp = reinterpret_cast<Method**>(frameId); |
| 1433 | Frame f; |
| 1434 | f.SetSP(sp); |
| 1435 | uint16_t reg = DemangleSlot(slot, f); |
| 1436 | Method* m = f.GetMethod(); |
| 1437 | |
| 1438 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 1439 | uint32_t vmap_offset; |
| 1440 | if (vmap_table.IsInContext(reg, vmap_offset)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1441 | 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] | 1442 | } |
| 1443 | |
| 1444 | switch (tag) { |
| 1445 | case JDWP::JT_BOOLEAN: |
| 1446 | case JDWP::JT_BYTE: |
| 1447 | CHECK_EQ(width, 1U); |
| 1448 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1449 | break; |
| 1450 | case JDWP::JT_SHORT: |
| 1451 | case JDWP::JT_CHAR: |
| 1452 | CHECK_EQ(width, 2U); |
| 1453 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1454 | break; |
| 1455 | case JDWP::JT_INT: |
| 1456 | case JDWP::JT_FLOAT: |
| 1457 | CHECK_EQ(width, 4U); |
| 1458 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1459 | break; |
| 1460 | case JDWP::JT_ARRAY: |
| 1461 | case JDWP::JT_OBJECT: |
| 1462 | case JDWP::JT_STRING: |
| 1463 | { |
| 1464 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
| 1465 | Object* o = gRegistry->Get<Object*>(static_cast<JDWP::ObjectId>(value)); |
| 1466 | f.SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o))); |
| 1467 | } |
| 1468 | break; |
| 1469 | case JDWP::JT_DOUBLE: |
| 1470 | case JDWP::JT_LONG: |
| 1471 | CHECK_EQ(width, 8U); |
| 1472 | f.SetVReg(m, reg, static_cast<uint32_t>(value)); |
| 1473 | f.SetVReg(m, reg + 1, static_cast<uint32_t>(value >> 32)); |
| 1474 | break; |
| 1475 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1476 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1477 | break; |
| 1478 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | void Dbg::PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags) { |
| 1482 | UNIMPLEMENTED(FATAL); |
| 1483 | } |
| 1484 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1485 | void Dbg::PostException(Method** sp, Method* throwMethod, uintptr_t throwNativePc, Method* catchMethod, uintptr_t catchNativePc, Object* exception) { |
Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 1486 | if (!gDebuggerActive) { |
| 1487 | return; |
| 1488 | } |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1489 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1490 | JDWP::JdwpLocation throw_location; |
| 1491 | SetLocation(throw_location, throwMethod, throwNativePc); |
| 1492 | JDWP::JdwpLocation catch_location; |
| 1493 | SetLocation(catch_location, catchMethod, catchNativePc); |
| 1494 | |
| 1495 | // We need 'this' for InstanceOnly filters. |
| 1496 | JDWP::ObjectId this_id; |
| 1497 | GetThisObject(reinterpret_cast<JDWP::FrameId>(sp), &this_id); |
| 1498 | |
| 1499 | /* |
| 1500 | * Hand the event to the JDWP exception handler. Note we're using the |
| 1501 | * "NoReg" objectID on the exception, which is not strictly correct -- |
| 1502 | * the exception object WILL be passed up to the debugger if the |
| 1503 | * debugger is interested in the event. We do this because the current |
| 1504 | * implementation of the debugger object registry never throws anything |
| 1505 | * away, and some people were experiencing a fatal build up of exception |
| 1506 | * objects when dealing with certain libraries. |
| 1507 | */ |
| 1508 | JDWP::ObjectId exception_id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(exception)); |
| 1509 | JDWP::RefTypeId exception_class_id = gRegistry->Add(exception->GetClass()); |
| 1510 | |
| 1511 | 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] | 1512 | } |
| 1513 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1514 | void Dbg::PostClassPrepare(Class* c) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1515 | if (!gDebuggerActive) { |
| 1516 | return; |
| 1517 | } |
| 1518 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1519 | // OLD-TODO - we currently always send both "verified" and "prepared" since |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1520 | // debuggers seem to like that. There might be some advantage to honesty, |
| 1521 | // since the class may not yet be verified. |
| 1522 | int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 1523 | JDWP::JdwpTypeTag tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1524 | gJdwpState->PostClassPrepare(tag, gRegistry->Add(c), ClassHelper(c).GetDescriptor(), state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | bool Dbg::WatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 1528 | UNIMPLEMENTED(FATAL); |
| 1529 | return false; |
| 1530 | } |
| 1531 | |
| 1532 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* pLoc) { |
| 1533 | UNIMPLEMENTED(FATAL); |
| 1534 | } |
| 1535 | |
| 1536 | bool Dbg::ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth) { |
| 1537 | UNIMPLEMENTED(FATAL); |
| 1538 | return false; |
| 1539 | } |
| 1540 | |
| 1541 | void Dbg::UnconfigureStep(JDWP::ObjectId threadId) { |
| 1542 | UNIMPLEMENTED(FATAL); |
| 1543 | } |
| 1544 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1545 | JDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId threadId, JDWP::ObjectId objectId, JDWP::RefTypeId classId, JDWP::MethodId methodId, uint32_t numArgs, uint64_t* argArray, uint32_t options, JDWP::JdwpTag* pResultTag, uint64_t* pResultValue, JDWP::ObjectId* pExceptionId) { |
| 1546 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 1547 | |
| 1548 | Thread* targetThread = NULL; |
| 1549 | DebugInvokeReq* req = NULL; |
| 1550 | { |
| 1551 | ScopedThreadListLock thread_list_lock; |
| 1552 | targetThread = DecodeThread(threadId); |
| 1553 | if (targetThread == NULL) { |
| 1554 | LOG(ERROR) << "InvokeMethod request for non-existent thread " << threadId; |
| 1555 | return JDWP::ERR_INVALID_THREAD; |
| 1556 | } |
| 1557 | req = targetThread->GetInvokeReq(); |
| 1558 | if (!req->ready) { |
| 1559 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 1560 | return JDWP::ERR_INVALID_THREAD; |
| 1561 | } |
| 1562 | |
| 1563 | /* |
| 1564 | * We currently have a bug where we don't successfully resume the |
| 1565 | * target thread if the suspend count is too deep. We're expected to |
| 1566 | * require one "resume" for each "suspend", but when asked to execute |
| 1567 | * a method we have to resume fully and then re-suspend it back to the |
| 1568 | * same level. (The easiest way to cause this is to type "suspend" |
| 1569 | * multiple times in jdb.) |
| 1570 | * |
| 1571 | * It's unclear what this means when the event specifies "resume all" |
| 1572 | * and some threads are suspended more deeply than others. This is |
| 1573 | * a rare problem, so for now we just prevent it from hanging forever |
| 1574 | * by rejecting the method invocation request. Without this, we will |
| 1575 | * be stuck waiting on a suspended thread. |
| 1576 | */ |
| 1577 | int suspend_count = targetThread->GetSuspendCount(); |
| 1578 | if (suspend_count > 1) { |
| 1579 | LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count; |
| 1580 | return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here. |
| 1581 | } |
| 1582 | |
| 1583 | /* |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1584 | * OLD-TODO: ought to screen the various IDs, and verify that the argument |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1585 | * list is valid. |
| 1586 | */ |
| 1587 | req->receiver_ = gRegistry->Get<Object*>(objectId); |
| 1588 | req->thread_ = gRegistry->Get<Object*>(threadId); |
| 1589 | req->class_ = gRegistry->Get<Class*>(classId); |
| 1590 | req->method_ = FromMethodId(methodId); |
| 1591 | req->num_args_ = numArgs; |
| 1592 | req->arg_array_ = argArray; |
| 1593 | req->options_ = options; |
| 1594 | req->invoke_needed_ = true; |
| 1595 | } |
| 1596 | |
| 1597 | // The fact that we've released the thread list lock is a bit risky --- if the thread goes |
| 1598 | // away we're sitting high and dry -- but we must release this before the ResumeAllThreads |
| 1599 | // call, and it's unwise to hold it during WaitForSuspend. |
| 1600 | |
| 1601 | { |
| 1602 | /* |
| 1603 | * We change our (JDWP thread) status, which should be THREAD_RUNNING, |
| 1604 | * so the VM can suspend for a GC if the invoke request causes us to |
| 1605 | * run out of memory. It's also a good idea to change it before locking |
| 1606 | * the invokeReq mutex, although that should never be held for long. |
| 1607 | */ |
| 1608 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); |
| 1609 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1610 | VLOG(jdwp) << " Transferring control to event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1611 | { |
| 1612 | MutexLock mu(req->lock_); |
| 1613 | |
| 1614 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1615 | VLOG(jdwp) << " Resuming all threads"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1616 | thread_list->ResumeAll(true); |
| 1617 | } else { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1618 | VLOG(jdwp) << " Resuming event thread only"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1619 | thread_list->Resume(targetThread, true); |
| 1620 | } |
| 1621 | |
| 1622 | // Wait for the request to finish executing. |
| 1623 | while (req->invoke_needed_) { |
| 1624 | req->cond_.Wait(req->lock_); |
| 1625 | } |
| 1626 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1627 | VLOG(jdwp) << " Control has returned from event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1628 | |
| 1629 | /* wait for thread to re-suspend itself */ |
| 1630 | targetThread->WaitUntilSuspended(); |
| 1631 | //dvmWaitForSuspend(targetThread); |
| 1632 | } |
| 1633 | |
| 1634 | /* |
| 1635 | * Suspend the threads. We waited for the target thread to suspend |
| 1636 | * itself, so all we need to do is suspend the others. |
| 1637 | * |
| 1638 | * The suspendAllThreads() call will double-suspend the event thread, |
| 1639 | * so we want to resume the target thread once to keep the books straight. |
| 1640 | */ |
| 1641 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1642 | VLOG(jdwp) << " Suspending all threads"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1643 | thread_list->SuspendAll(true); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1644 | VLOG(jdwp) << " Resuming event thread to balance the count"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1645 | thread_list->Resume(targetThread, true); |
| 1646 | } |
| 1647 | |
| 1648 | // Copy the result. |
| 1649 | *pResultTag = req->result_tag; |
| 1650 | if (IsPrimitiveTag(req->result_tag)) { |
| 1651 | *pResultValue = req->result_value.j; |
| 1652 | } else { |
| 1653 | *pResultValue = gRegistry->Add(req->result_value.l); |
| 1654 | } |
| 1655 | *pExceptionId = req->exception; |
| 1656 | return req->error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1660 | Thread* self = Thread::Current(); |
| 1661 | |
| 1662 | // We can be called while an exception is pending in the VM. We need |
| 1663 | // to preserve that across the method invocation. |
| 1664 | SirtRef<Throwable> old_exception(self->GetException()); |
| 1665 | self->ClearException(); |
| 1666 | |
| 1667 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
| 1668 | |
| 1669 | // Translate the method through the vtable, unless the debugger wants to suppress it. |
| 1670 | Method* m = pReq->method_; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1671 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1672 | if ((pReq->options_ & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver_ != NULL) { |
| 1673 | m = pReq->class_->FindVirtualMethodForVirtualOrInterface(pReq->method_); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1674 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1675 | } |
| 1676 | CHECK(m != NULL); |
| 1677 | |
| 1678 | CHECK_EQ(sizeof(jvalue), sizeof(uint64_t)); |
| 1679 | |
| 1680 | pReq->result_value = InvokeWithJValues(self, pReq->receiver_, m, reinterpret_cast<JValue*>(pReq->arg_array_)); |
| 1681 | |
| 1682 | pReq->exception = gRegistry->Add(self->GetException()); |
| 1683 | pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty()); |
| 1684 | if (pReq->exception != 0) { |
| 1685 | Object* exc = self->GetException(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1686 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exc << " " << PrettyTypeOf(exc); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1687 | self->ClearException(); |
| 1688 | pReq->result_value.j = 0; |
| 1689 | } else if (pReq->result_tag == JDWP::JT_OBJECT) { |
| 1690 | /* if no exception thrown, examine object result more closely */ |
| 1691 | JDWP::JdwpTag new_tag = TagFromObject(pReq->result_value.l); |
| 1692 | if (new_tag != pReq->result_tag) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1693 | VLOG(jdwp) << " JDWP promoted result from " << pReq->result_tag << " to " << new_tag; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1694 | pReq->result_tag = new_tag; |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * Register the object. We don't actually need an ObjectId yet, |
| 1699 | * but we do need to be sure that the GC won't move or discard the |
| 1700 | * object when we switch out of RUNNING. The ObjectId conversion |
| 1701 | * will add the object to the "do not touch" list. |
| 1702 | * |
| 1703 | * We can't use the "tracked allocation" mechanism here because |
| 1704 | * the object is going to be handed off to a different thread. |
| 1705 | */ |
| 1706 | gRegistry->Add(pReq->result_value.l); |
| 1707 | } |
| 1708 | |
| 1709 | if (old_exception.get() != NULL) { |
| 1710 | self->SetException(old_exception.get()); |
| 1711 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1714 | /* |
| 1715 | * Register an object ID that might not have been registered previously. |
| 1716 | * |
| 1717 | * Normally this wouldn't happen -- the conversion to an ObjectId would |
| 1718 | * have added the object to the registry -- but in some cases (e.g. |
| 1719 | * throwing exceptions) we really want to do the registration late. |
| 1720 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1721 | void Dbg::RegisterObjectId(JDWP::ObjectId id) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1722 | gRegistry->Add(reinterpret_cast<Object*>(id)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1725 | /* |
| 1726 | * "buf" contains a full JDWP packet, possibly with multiple chunks. We |
| 1727 | * need to process each, accumulate the replies, and ship the whole thing |
| 1728 | * back. |
| 1729 | * |
| 1730 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 1731 | * and includes the chunk type/length, followed by the data. |
| 1732 | * |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1733 | * 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] | 1734 | * chunk. If this becomes inconvenient we will need to adapt. |
| 1735 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1736 | 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] | 1737 | CHECK_GE(dataLen, 0); |
| 1738 | |
| 1739 | Thread* self = Thread::Current(); |
| 1740 | JNIEnv* env = self->GetJniEnv(); |
| 1741 | |
| 1742 | static jclass Chunk_class = env->FindClass("org/apache/harmony/dalvik/ddmc/Chunk"); |
| 1743 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 1744 | static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch", |
| 1745 | "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;"); |
| 1746 | static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B"); |
| 1747 | static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I"); |
| 1748 | static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I"); |
| 1749 | static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I"); |
| 1750 | |
| 1751 | // Create a byte[] corresponding to 'buf'. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1752 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(dataLen)); |
| 1753 | if (dataArray.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1754 | LOG(WARNING) << "byte[] allocation failed: " << dataLen; |
| 1755 | env->ExceptionClear(); |
| 1756 | return false; |
| 1757 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1758 | env->SetByteArrayRegion(dataArray.get(), 0, dataLen, reinterpret_cast<const jbyte*>(buf)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1759 | |
| 1760 | const int kChunkHdrLen = 8; |
| 1761 | |
| 1762 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1763 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1764 | jint type = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[0])); |
| 1765 | jint length = JDWP::Get4BE(reinterpret_cast<const uint8_t*>(&contents[4])); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1766 | jint offset = kChunkHdrLen; |
| 1767 | if (offset + length > dataLen) { |
| 1768 | LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen); |
| 1769 | return false; |
| 1770 | } |
| 1771 | |
| 1772 | // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)". |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1773 | ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(DdmServer_class, dispatch_mid, type, dataArray.get(), offset, length)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1774 | if (env->ExceptionCheck()) { |
| 1775 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 1776 | env->ExceptionDescribe(); |
| 1777 | env->ExceptionClear(); |
| 1778 | return false; |
| 1779 | } |
| 1780 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1781 | if (chunk.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1782 | return false; |
| 1783 | } |
| 1784 | |
| 1785 | /* |
| 1786 | * Pull the pieces out of the chunk. We copy the results into a |
| 1787 | * newly-allocated buffer that the caller can free. We don't want to |
| 1788 | * continue using the Chunk object because nothing has a reference to it. |
| 1789 | * |
| 1790 | * We could avoid this by returning type/data/offset/length and having |
| 1791 | * the caller be aware of the object lifetime issues, but that |
| 1792 | * integrates the JDWP code more tightly into the VM, and doesn't work |
| 1793 | * if we have responses for multiple chunks. |
| 1794 | * |
| 1795 | * So we're pretty much stuck with copying data around multiple times. |
| 1796 | */ |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1797 | ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), data_fid))); |
| 1798 | length = env->GetIntField(chunk.get(), length_fid); |
| 1799 | offset = env->GetIntField(chunk.get(), offset_fid); |
| 1800 | type = env->GetIntField(chunk.get(), type_fid); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1801 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1802 | 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] | 1803 | if (length == 0 || replyData.get() == NULL) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1804 | return false; |
| 1805 | } |
| 1806 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1807 | jsize replyLength = env->GetArrayLength(replyData.get()); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1808 | if (offset + length > replyLength) { |
| 1809 | LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength); |
| 1810 | return false; |
| 1811 | } |
| 1812 | |
| 1813 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
| 1814 | if (reply == NULL) { |
| 1815 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 1816 | return false; |
| 1817 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1818 | JDWP::Set4BE(reply + 0, type); |
| 1819 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 1820 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1821 | |
| 1822 | *pReplyBuf = reply; |
| 1823 | *pReplyLen = length + kChunkHdrLen; |
| 1824 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1825 | VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%p len=%d", (char*) reply, reply, length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 1826 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1829 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1830 | VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1831 | |
| 1832 | Thread* self = Thread::Current(); |
| 1833 | if (self->GetState() != Thread::kRunnable) { |
| 1834 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 1835 | /* try anyway? */ |
| 1836 | } |
| 1837 | |
| 1838 | JNIEnv* env = self->GetJniEnv(); |
| 1839 | static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer"); |
| 1840 | static jmethodID broadcast_mid = env->GetStaticMethodID(DdmServer_class, "broadcast", "(I)V"); |
| 1841 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
| 1842 | env->CallStaticVoidMethod(DdmServer_class, broadcast_mid, event); |
| 1843 | if (env->ExceptionCheck()) { |
| 1844 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 1845 | env->ExceptionDescribe(); |
| 1846 | env->ExceptionClear(); |
| 1847 | } |
| 1848 | } |
| 1849 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1850 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1851 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1855 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1856 | gDdmThreadNotification = false; |
| 1857 | } |
| 1858 | |
| 1859 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1860 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1861 | * |
| 1862 | * Because we broadcast the full set of threads when the notifications are |
| 1863 | * first enabled, it's possible for "thread" to be actively executing. |
| 1864 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1865 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1866 | if (!gDdmThreadNotification) { |
| 1867 | return; |
| 1868 | } |
| 1869 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1870 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1871 | uint8_t buf[4]; |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 1872 | JDWP::Set4BE(&buf[0], t->GetThinLockId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1873 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1874 | } else { |
| 1875 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
| 1876 | SirtRef<String> name(t->GetName()); |
| 1877 | size_t char_count = (name.get() != NULL) ? name->GetLength() : 0; |
| 1878 | const jchar* chars = name->GetCharArray()->GetData(); |
| 1879 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1880 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 1881 | JDWP::Append4BE(bytes, t->GetThinLockId()); |
| 1882 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1883 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 1884 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1885 | } |
| 1886 | } |
| 1887 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1888 | static void DdmSendThreadStartCallback(Thread* t, void*) { |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1889 | Dbg::DdmSendThreadNotification(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | void Dbg::DdmSetThreadNotification(bool enable) { |
| 1893 | // We lock the thread list to avoid sending duplicate events or missing |
| 1894 | // a thread change. We should be okay holding this lock while sending |
| 1895 | // 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] | 1896 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1897 | |
| 1898 | gDdmThreadNotification = enable; |
| 1899 | if (enable) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 1900 | Runtime::Current()->GetThreadList()->ForEach(DdmSendThreadStartCallback, NULL); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1901 | } |
| 1902 | } |
| 1903 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1904 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1905 | if (gDebuggerActive) { |
| 1906 | JDWP::ObjectId id = gRegistry->Add(t->GetPeer()); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1907 | gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1908 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1909 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1913 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1917 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 1920 | 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] | 1921 | CHECK(buf != NULL); |
| 1922 | iovec vec[1]; |
| 1923 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 1924 | vec[0].iov_len = byte_count; |
| 1925 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1926 | } |
| 1927 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 1928 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 1929 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 1930 | } |
| 1931 | |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1932 | 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] | 1933 | if (gJdwpState == NULL) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1934 | VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 1935 | } else { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 1936 | gJdwpState->DdmSendChunkV(type, iov, iov_count); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 1937 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 1940 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 1941 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 1942 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 1943 | return true; |
| 1944 | } |
| 1945 | |
| 1946 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 1947 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 1948 | return false; |
| 1949 | } |
| 1950 | |
| 1951 | gDdmHpifWhen = when; |
| 1952 | return true; |
| 1953 | } |
| 1954 | |
| 1955 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 1956 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 1957 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 1958 | return false; |
| 1959 | } |
| 1960 | |
| 1961 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 1962 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 1963 | return false; |
| 1964 | } |
| 1965 | |
| 1966 | if (native) { |
| 1967 | gDdmNhsgWhen = when; |
| 1968 | gDdmNhsgWhat = what; |
| 1969 | } else { |
| 1970 | gDdmHpsgWhen = when; |
| 1971 | gDdmHpsgWhat = what; |
| 1972 | } |
| 1973 | return true; |
| 1974 | } |
| 1975 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 1976 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 1977 | // If there's a one-shot 'when', reset it. |
| 1978 | if (reason == gDdmHpifWhen) { |
| 1979 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 1980 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | /* |
| 1985 | * Chunk HPIF (client --> server) |
| 1986 | * |
| 1987 | * Heap Info. General information about the heap, |
| 1988 | * suitable for a summary display. |
| 1989 | * |
| 1990 | * [u4]: number of heaps |
| 1991 | * |
| 1992 | * For each heap: |
| 1993 | * [u4]: heap ID |
| 1994 | * [u8]: timestamp in ms since Unix epoch |
| 1995 | * [u1]: capture reason (same as 'when' value from server) |
| 1996 | * [u4]: max heap size in bytes (-Xmx) |
| 1997 | * [u4]: current heap size in bytes |
| 1998 | * [u4]: current number of bytes allocated |
| 1999 | * [u4]: current number of objects allocated |
| 2000 | */ |
| 2001 | uint8_t heap_count = 1; |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2002 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2003 | JDWP::Append4BE(bytes, heap_count); |
| 2004 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
| 2005 | JDWP::Append8BE(bytes, MilliTime()); |
| 2006 | JDWP::Append1BE(bytes, reason); |
| 2007 | JDWP::Append4BE(bytes, Heap::GetMaxMemory()); // Max allowed heap size in bytes. |
| 2008 | JDWP::Append4BE(bytes, Heap::GetTotalMemory()); // Current heap size in bytes. |
| 2009 | JDWP::Append4BE(bytes, Heap::GetBytesAllocated()); |
| 2010 | JDWP::Append4BE(bytes, Heap::GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 2011 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 2012 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2015 | enum HpsgSolidity { |
| 2016 | SOLIDITY_FREE = 0, |
| 2017 | SOLIDITY_HARD = 1, |
| 2018 | SOLIDITY_SOFT = 2, |
| 2019 | SOLIDITY_WEAK = 3, |
| 2020 | SOLIDITY_PHANTOM = 4, |
| 2021 | SOLIDITY_FINALIZABLE = 5, |
| 2022 | SOLIDITY_SWEEP = 6, |
| 2023 | }; |
| 2024 | |
| 2025 | enum HpsgKind { |
| 2026 | KIND_OBJECT = 0, |
| 2027 | KIND_CLASS_OBJECT = 1, |
| 2028 | KIND_ARRAY_1 = 2, |
| 2029 | KIND_ARRAY_2 = 3, |
| 2030 | KIND_ARRAY_4 = 4, |
| 2031 | KIND_ARRAY_8 = 5, |
| 2032 | KIND_UNKNOWN = 6, |
| 2033 | KIND_NATIVE = 7, |
| 2034 | }; |
| 2035 | |
| 2036 | #define HPSG_PARTIAL (1<<7) |
| 2037 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 2038 | |
| 2039 | struct HeapChunkContext { |
| 2040 | std::vector<uint8_t> buf; |
| 2041 | uint8_t* p; |
| 2042 | uint8_t* pieceLenField; |
| 2043 | size_t totalAllocationUnits; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 2044 | uint32_t type; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2045 | bool merge; |
| 2046 | bool needHeader; |
| 2047 | |
| 2048 | // Maximum chunk size. Obtain this from the formula: |
| 2049 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 2050 | HeapChunkContext(bool merge, bool native) |
| 2051 | : buf(16384 - 16), |
| 2052 | type(0), |
| 2053 | merge(merge) { |
| 2054 | Reset(); |
| 2055 | if (native) { |
| 2056 | type = CHUNK_TYPE("NHSG"); |
| 2057 | } else { |
| 2058 | type = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | ~HeapChunkContext() { |
| 2063 | if (p > &buf[0]) { |
| 2064 | Flush(); |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | void EnsureHeader(const void* chunk_ptr) { |
| 2069 | if (!needHeader) { |
| 2070 | return; |
| 2071 | } |
| 2072 | |
| 2073 | // Start a new HPSx chunk. |
| 2074 | JDWP::Write4BE(&p, 1); // Heap id (bogus; we only have one heap). |
| 2075 | JDWP::Write1BE(&p, 8); // Size of allocation unit, in bytes. |
| 2076 | |
| 2077 | JDWP::Write4BE(&p, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 2078 | JDWP::Write4BE(&p, 0); // offset of this piece (relative to the virtual address). |
| 2079 | // [u4]: length of piece, in allocation units |
| 2080 | // We won't know this until we're done, so save the offset and stuff in a dummy value. |
| 2081 | pieceLenField = p; |
| 2082 | JDWP::Write4BE(&p, 0x55555555); |
| 2083 | needHeader = false; |
| 2084 | } |
| 2085 | |
| 2086 | void Flush() { |
| 2087 | // Patch the "length of piece" field. |
| 2088 | CHECK_LE(&buf[0], pieceLenField); |
| 2089 | CHECK_LE(pieceLenField, p); |
| 2090 | JDWP::Set4BE(pieceLenField, totalAllocationUnits); |
| 2091 | |
| 2092 | Dbg::DdmSendChunk(type, p - &buf[0], &buf[0]); |
| 2093 | Reset(); |
| 2094 | } |
| 2095 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2096 | static void HeapChunkCallback(const void* chunk_ptr, size_t chunk_len, const void* user_ptr, size_t user_len, void* arg) { |
| 2097 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(chunk_ptr, chunk_len, user_ptr, user_len); |
| 2098 | } |
| 2099 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2100 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2101 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 2102 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2103 | void Reset() { |
| 2104 | p = &buf[0]; |
| 2105 | totalAllocationUnits = 0; |
| 2106 | needHeader = true; |
| 2107 | pieceLenField = NULL; |
| 2108 | } |
| 2109 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2110 | void HeapChunkCallback(const void* chunk_ptr, size_t chunk_len, const void* user_ptr, size_t user_len) { |
| 2111 | CHECK_EQ((chunk_len & (ALLOCATION_UNIT_SIZE-1)), 0U); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2112 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2113 | /* Make sure there's enough room left in the buffer. |
| 2114 | * We need to use two bytes for every fractional 256 |
| 2115 | * allocation units used by the chunk. |
| 2116 | */ |
| 2117 | { |
| 2118 | size_t needed = (((chunk_len/ALLOCATION_UNIT_SIZE + 255) / 256) * 2); |
| 2119 | size_t bytesLeft = buf.size() - (size_t)(p - &buf[0]); |
| 2120 | if (bytesLeft < needed) { |
| 2121 | Flush(); |
| 2122 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2123 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2124 | bytesLeft = buf.size() - (size_t)(p - &buf[0]); |
| 2125 | if (bytesLeft < needed) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2126 | 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] | 2127 | return; |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | // OLD-TODO: notice when there's a gap and start a new heap, or at least a new range. |
| 2132 | EnsureHeader(chunk_ptr); |
| 2133 | |
| 2134 | // Determine the type of this chunk. |
| 2135 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 2136 | // If it's the same, we should combine them. |
| 2137 | uint8_t state = ExamineObject(reinterpret_cast<const Object*>(user_ptr), (type == CHUNK_TYPE("NHSG"))); |
| 2138 | |
| 2139 | // Write out the chunk description. |
| 2140 | chunk_len /= ALLOCATION_UNIT_SIZE; // convert to allocation units |
| 2141 | totalAllocationUnits += chunk_len; |
| 2142 | while (chunk_len > 256) { |
| 2143 | *p++ = state | HPSG_PARTIAL; |
| 2144 | *p++ = 255; // length - 1 |
| 2145 | chunk_len -= 256; |
| 2146 | } |
| 2147 | *p++ = state; |
| 2148 | *p++ = chunk_len - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2149 | } |
| 2150 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2151 | uint8_t ExamineObject(const Object* o, bool is_native_heap) { |
| 2152 | if (o == NULL) { |
| 2153 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 2154 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2155 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2156 | // It's an allocated chunk. Figure out what it is. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2157 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2158 | // If we're looking at the native heap, we'll just return |
| 2159 | // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks. |
| 2160 | if (is_native_heap || !Heap::IsLiveObjectLocked(o)) { |
| 2161 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 2162 | } |
| 2163 | |
| 2164 | Class* c = o->GetClass(); |
| 2165 | if (c == NULL) { |
| 2166 | // The object was probably just created but hasn't been initialized yet. |
| 2167 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 2168 | } |
| 2169 | |
| 2170 | if (!Heap::IsHeapAddress(c)) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 2171 | LOG(WARNING) << "Invalid class for managed heap object: " << o << " " << c; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2172 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 2173 | } |
| 2174 | |
| 2175 | if (c->IsClassClass()) { |
| 2176 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 2177 | } |
| 2178 | |
| 2179 | if (c->IsArrayClass()) { |
| 2180 | if (o->IsObjectArray()) { |
| 2181 | return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 2182 | } |
| 2183 | switch (c->GetComponentSize()) { |
| 2184 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 2185 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 2186 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 2187 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 2188 | } |
| 2189 | } |
| 2190 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2191 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 2192 | } |
| 2193 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2194 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 2195 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2196 | |
| 2197 | void Dbg::DdmSendHeapSegments(bool native) { |
| 2198 | Dbg::HpsgWhen when; |
| 2199 | Dbg::HpsgWhat what; |
| 2200 | if (!native) { |
| 2201 | when = gDdmHpsgWhen; |
| 2202 | what = gDdmHpsgWhat; |
| 2203 | } else { |
| 2204 | when = gDdmNhsgWhen; |
| 2205 | what = gDdmNhsgWhat; |
| 2206 | } |
| 2207 | if (when == HPSG_WHEN_NEVER) { |
| 2208 | return; |
| 2209 | } |
| 2210 | |
| 2211 | // Figure out what kind of chunks we'll be sending. |
| 2212 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what); |
| 2213 | |
| 2214 | // First, send a heap start chunk. |
| 2215 | uint8_t heap_id[4]; |
| 2216 | JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap). |
| 2217 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id); |
| 2218 | |
| 2219 | // Send a series of heap segment chunks. |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2220 | HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native); |
| 2221 | if (native) { |
| 2222 | dlmalloc_walk_heap(HeapChunkContext::HeapChunkCallback, &context); |
| 2223 | } else { |
| 2224 | Heap::WalkHeap(HeapChunkContext::HeapChunkCallback, &context); |
| 2225 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 2226 | |
| 2227 | // Finally, send a heap end chunk. |
| 2228 | 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] | 2229 | } |
| 2230 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2231 | void Dbg::SetAllocTrackingEnabled(bool enabled) { |
| 2232 | MutexLock mu(gAllocTrackerLock); |
| 2233 | if (enabled) { |
| 2234 | if (recent_allocation_records_ == NULL) { |
| 2235 | LOG(INFO) << "Enabling alloc tracker (" << kNumAllocRecords << " entries, " |
| 2236 | << kMaxAllocRecordStackDepth << " frames --> " |
| 2237 | << (sizeof(AllocRecord) * kNumAllocRecords) << " bytes)"; |
| 2238 | gAllocRecordHead = gAllocRecordCount = 0; |
| 2239 | recent_allocation_records_ = new AllocRecord[kNumAllocRecords]; |
| 2240 | CHECK(recent_allocation_records_ != NULL); |
| 2241 | } |
| 2242 | } else { |
| 2243 | delete[] recent_allocation_records_; |
| 2244 | recent_allocation_records_ = NULL; |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | struct AllocRecordStackVisitor : public Thread::StackVisitor { |
| 2249 | AllocRecordStackVisitor(AllocRecord* record) : record(record), depth(0) { |
| 2250 | } |
| 2251 | |
| 2252 | virtual void VisitFrame(const Frame& f, uintptr_t pc) { |
| 2253 | if (depth >= kMaxAllocRecordStackDepth) { |
| 2254 | return; |
| 2255 | } |
| 2256 | Method* m = f.GetMethod(); |
| 2257 | if (m == NULL || m->IsCalleeSaveMethod()) { |
| 2258 | return; |
| 2259 | } |
| 2260 | record->stack[depth].method = m; |
| 2261 | record->stack[depth].raw_pc = pc; |
| 2262 | ++depth; |
| 2263 | } |
| 2264 | |
| 2265 | ~AllocRecordStackVisitor() { |
| 2266 | // Clear out any unused stack trace elements. |
| 2267 | for (; depth < kMaxAllocRecordStackDepth; ++depth) { |
| 2268 | record->stack[depth].method = NULL; |
| 2269 | record->stack[depth].raw_pc = 0; |
| 2270 | } |
| 2271 | } |
| 2272 | |
| 2273 | AllocRecord* record; |
| 2274 | size_t depth; |
| 2275 | }; |
| 2276 | |
| 2277 | void Dbg::RecordAllocation(Class* type, size_t byte_count) { |
| 2278 | Thread* self = Thread::Current(); |
| 2279 | CHECK(self != NULL); |
| 2280 | |
| 2281 | MutexLock mu(gAllocTrackerLock); |
| 2282 | if (recent_allocation_records_ == NULL) { |
| 2283 | return; |
| 2284 | } |
| 2285 | |
| 2286 | // Advance and clip. |
| 2287 | if (++gAllocRecordHead == kNumAllocRecords) { |
| 2288 | gAllocRecordHead = 0; |
| 2289 | } |
| 2290 | |
| 2291 | // Fill in the basics. |
| 2292 | AllocRecord* record = &recent_allocation_records_[gAllocRecordHead]; |
| 2293 | record->type = type; |
| 2294 | record->byte_count = byte_count; |
| 2295 | record->thin_lock_id = self->GetThinLockId(); |
| 2296 | |
| 2297 | // Fill in the stack trace. |
| 2298 | AllocRecordStackVisitor visitor(record); |
| 2299 | self->WalkStack(&visitor); |
| 2300 | |
| 2301 | if (gAllocRecordCount < kNumAllocRecords) { |
| 2302 | ++gAllocRecordCount; |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | /* |
| 2307 | * Return the index of the head element. |
| 2308 | * |
| 2309 | * We point at the most-recently-written record, so if allocRecordCount is 1 |
| 2310 | * we want to use the current element. Take "head+1" and subtract count |
| 2311 | * from it. |
| 2312 | * |
| 2313 | * We need to handle underflow in our circular buffer, so we add |
| 2314 | * kNumAllocRecords and then mask it back down. |
| 2315 | */ |
| 2316 | inline static int headIndex() { |
| 2317 | return (gAllocRecordHead+1 + kNumAllocRecords - gAllocRecordCount) & (kNumAllocRecords-1); |
| 2318 | } |
| 2319 | |
| 2320 | void Dbg::DumpRecentAllocations() { |
| 2321 | MutexLock mu(gAllocTrackerLock); |
| 2322 | if (recent_allocation_records_ == NULL) { |
| 2323 | LOG(INFO) << "Not recording tracked allocations"; |
| 2324 | return; |
| 2325 | } |
| 2326 | |
| 2327 | // "i" is the head of the list. We want to start at the end of the |
| 2328 | // list and move forward to the tail. |
| 2329 | size_t i = headIndex(); |
| 2330 | size_t count = gAllocRecordCount; |
| 2331 | |
| 2332 | LOG(INFO) << "Tracked allocations, (head=" << gAllocRecordHead << " count=" << count << ")"; |
| 2333 | while (count--) { |
| 2334 | AllocRecord* record = &recent_allocation_records_[i]; |
| 2335 | |
| 2336 | LOG(INFO) << StringPrintf(" T=%-2d %6d ", record->thin_lock_id, record->byte_count) |
| 2337 | << PrettyClass(record->type); |
| 2338 | |
| 2339 | for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) { |
| 2340 | const Method* m = record->stack[stack_frame].method; |
| 2341 | if (m == NULL) { |
| 2342 | break; |
| 2343 | } |
| 2344 | LOG(INFO) << " " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber(); |
| 2345 | } |
| 2346 | |
| 2347 | // pause periodically to help logcat catch up |
| 2348 | if ((count % 5) == 0) { |
| 2349 | usleep(40000); |
| 2350 | } |
| 2351 | |
| 2352 | i = (i + 1) & (kNumAllocRecords-1); |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | class StringTable { |
| 2357 | public: |
| 2358 | StringTable() { |
| 2359 | } |
| 2360 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2361 | void Add(const char* s) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2362 | table_.insert(s); |
| 2363 | } |
| 2364 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2365 | size_t IndexOf(const char* s) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2366 | return std::distance(table_.begin(), table_.find(s)); |
| 2367 | } |
| 2368 | |
| 2369 | size_t Size() { |
| 2370 | return table_.size(); |
| 2371 | } |
| 2372 | |
| 2373 | void WriteTo(std::vector<uint8_t>& bytes) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2374 | typedef std::set<const char*>::const_iterator It; // TODO: C++0x auto |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2375 | for (It it = table_.begin(); it != table_.end(); ++it) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2376 | const char* s = *it; |
| 2377 | size_t s_len = CountModifiedUtf8Chars(s); |
| 2378 | UniquePtr<uint16_t> s_utf16(new uint16_t[s_len]); |
| 2379 | ConvertModifiedUtf8ToUtf16(s_utf16.get(), s); |
| 2380 | JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2381 | } |
| 2382 | } |
| 2383 | |
| 2384 | private: |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2385 | std::set<const char*> table_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2386 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 2387 | }; |
| 2388 | |
| 2389 | /* |
| 2390 | * The data we send to DDMS contains everything we have recorded. |
| 2391 | * |
| 2392 | * Message header (all values big-endian): |
| 2393 | * (1b) message header len (to allow future expansion); includes itself |
| 2394 | * (1b) entry header len |
| 2395 | * (1b) stack frame len |
| 2396 | * (2b) number of entries |
| 2397 | * (4b) offset to string table from start of message |
| 2398 | * (2b) number of class name strings |
| 2399 | * (2b) number of method name strings |
| 2400 | * (2b) number of source file name strings |
| 2401 | * For each entry: |
| 2402 | * (4b) total allocation size |
| 2403 | * (2b) threadId |
| 2404 | * (2b) allocated object's class name index |
| 2405 | * (1b) stack depth |
| 2406 | * For each stack frame: |
| 2407 | * (2b) method's class name |
| 2408 | * (2b) method name |
| 2409 | * (2b) method source file |
| 2410 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 2411 | * (xb) class name strings |
| 2412 | * (xb) method name strings |
| 2413 | * (xb) source file strings |
| 2414 | * |
| 2415 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 2416 | * followed by UTF-16 data. |
| 2417 | * |
| 2418 | * We send up 16-bit unsigned indexes into string tables. In theory there |
| 2419 | * can be (kMaxAllocRecordStackDepth * kNumAllocRecords) unique strings in |
| 2420 | * each table, but in practice there should be far fewer. |
| 2421 | * |
| 2422 | * The chief reason for using a string table here is to keep the size of |
| 2423 | * the DDMS message to a minimum. This is partly to make the protocol |
| 2424 | * efficient, but also because we have to form the whole thing up all at |
| 2425 | * once in a memory buffer. |
| 2426 | * |
| 2427 | * We use separate string tables for class names, method names, and source |
| 2428 | * files to keep the indexes small. There will generally be no overlap |
| 2429 | * between the contents of these tables. |
| 2430 | */ |
| 2431 | jbyteArray Dbg::GetRecentAllocations() { |
| 2432 | if (false) { |
| 2433 | DumpRecentAllocations(); |
| 2434 | } |
| 2435 | |
| 2436 | MutexLock mu(gAllocTrackerLock); |
| 2437 | |
| 2438 | /* |
| 2439 | * Part 1: generate string tables. |
| 2440 | */ |
| 2441 | StringTable class_names; |
| 2442 | StringTable method_names; |
| 2443 | StringTable filenames; |
| 2444 | |
| 2445 | int count = gAllocRecordCount; |
| 2446 | int idx = headIndex(); |
| 2447 | while (count--) { |
| 2448 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 2449 | |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame^] | 2450 | class_names.Add(ClassHelper(record->type).GetDescriptor()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2451 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2452 | MethodHelper mh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2453 | for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2454 | Method* m = record->stack[i].method; |
| 2455 | mh.ChangeMethod(m); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2456 | if (m != NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2457 | class_names.Add(mh.GetDeclaringClassDescriptor()); |
| 2458 | method_names.Add(mh.GetName()); |
| 2459 | filenames.Add(mh.GetDeclaringClassSourceFile()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 2464 | } |
| 2465 | |
| 2466 | LOG(INFO) << "allocation records: " << gAllocRecordCount; |
| 2467 | |
| 2468 | /* |
| 2469 | * Part 2: allocate a buffer and generate the output. |
| 2470 | */ |
| 2471 | std::vector<uint8_t> bytes; |
| 2472 | |
| 2473 | // (1b) message header len (to allow future expansion); includes itself |
| 2474 | // (1b) entry header len |
| 2475 | // (1b) stack frame len |
| 2476 | const int kMessageHeaderLen = 15; |
| 2477 | const int kEntryHeaderLen = 9; |
| 2478 | const int kStackFrameLen = 8; |
| 2479 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 2480 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 2481 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 2482 | |
| 2483 | // (2b) number of entries |
| 2484 | // (4b) offset to string table from start of message |
| 2485 | // (2b) number of class name strings |
| 2486 | // (2b) number of method name strings |
| 2487 | // (2b) number of source file name strings |
| 2488 | JDWP::Append2BE(bytes, gAllocRecordCount); |
| 2489 | size_t string_table_offset = bytes.size(); |
| 2490 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
| 2491 | JDWP::Append2BE(bytes, class_names.Size()); |
| 2492 | JDWP::Append2BE(bytes, method_names.Size()); |
| 2493 | JDWP::Append2BE(bytes, filenames.Size()); |
| 2494 | |
| 2495 | count = gAllocRecordCount; |
| 2496 | idx = headIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2497 | ClassHelper kh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2498 | while (count--) { |
| 2499 | // For each entry: |
| 2500 | // (4b) total allocation size |
| 2501 | // (2b) thread id |
| 2502 | // (2b) allocated object's class name index |
| 2503 | // (1b) stack depth |
| 2504 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 2505 | size_t stack_depth = record->GetDepth(); |
| 2506 | JDWP::Append4BE(bytes, record->byte_count); |
| 2507 | JDWP::Append2BE(bytes, record->thin_lock_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2508 | kh.ChangeClass(record->type); |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame^] | 2509 | JDWP::Append2BE(bytes, class_names.IndexOf(kh.GetDescriptor())); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2510 | JDWP::Append1BE(bytes, stack_depth); |
| 2511 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2512 | MethodHelper mh; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2513 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 2514 | // For each stack frame: |
| 2515 | // (2b) method's class name |
| 2516 | // (2b) method name |
| 2517 | // (2b) method source file |
| 2518 | // (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] | 2519 | mh.ChangeMethod(record->stack[stack_frame].method); |
| 2520 | JDWP::Append2BE(bytes, class_names.IndexOf(mh.GetDeclaringClassDescriptor())); |
| 2521 | JDWP::Append2BE(bytes, method_names.IndexOf(mh.GetName())); |
| 2522 | JDWP::Append2BE(bytes, filenames.IndexOf(mh.GetDeclaringClassSourceFile())); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 2523 | JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber()); |
| 2524 | } |
| 2525 | |
| 2526 | idx = (idx + 1) & (kNumAllocRecords-1); |
| 2527 | } |
| 2528 | |
| 2529 | // (xb) class name strings |
| 2530 | // (xb) method name strings |
| 2531 | // (xb) source file strings |
| 2532 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 2533 | class_names.WriteTo(bytes); |
| 2534 | method_names.WriteTo(bytes); |
| 2535 | filenames.WriteTo(bytes); |
| 2536 | |
| 2537 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 2538 | jbyteArray result = env->NewByteArray(bytes.size()); |
| 2539 | if (result != NULL) { |
| 2540 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 2541 | } |
| 2542 | return result; |
| 2543 | } |
| 2544 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2545 | } // namespace art |