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