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