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