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