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