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