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" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 24 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "art_method-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 26 | #include "base/time_utils.h" |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 27 | #include "class_linker.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 29 | #include "dex_file-inl.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 30 | #include "dex_instruction.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 31 | #include "gc/accounting/card_table-inl.h" |
| 32 | #include "gc/space/large_object_space.h" |
| 33 | #include "gc/space/space-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 34 | #include "handle_scope.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 35 | #include "jdwp/object_registry.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | #include "mirror/class.h" |
| 37 | #include "mirror/class-inl.h" |
| 38 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | #include "mirror/object-inl.h" |
| 40 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 41 | #include "mirror/string-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 42 | #include "mirror/throwable.h" |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 43 | #include "quick/inline_method_analyser.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 44 | #include "reflection.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 45 | #include "safe_map.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 46 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 47 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 48 | #include "ScopedPrimitiveArray.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 49 | #include "handle_scope-inl.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 50 | #include "thread_list.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 51 | #include "utf.h" |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 52 | #include "verifier/method_verifier-inl.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 53 | #include "well_known_classes.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 54 | |
Brian Carlstrom | 3d92d52 | 2013-07-12 09:03:08 -0700 | [diff] [blame] | 55 | #ifdef HAVE_ANDROID_OS |
| 56 | #include "cutils/properties.h" |
| 57 | #endif |
| 58 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 59 | namespace art { |
| 60 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 61 | // The key identifying the debugger to update instrumentation. |
| 62 | static constexpr const char* kDbgInstrumentationKey = "Debugger"; |
| 63 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 64 | static const size_t kMaxAllocRecordStackDepth = 16; // Max 255. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 65 | static const size_t kDefaultNumAllocRecords = 64*1024; // Must be a power of 2. 2BE can hold 64k-1. |
| 66 | |
| 67 | // Limit alloc_record_count to the 2BE value that is the limit of the current protocol. |
| 68 | static uint16_t CappedAllocRecordCount(size_t alloc_record_count) { |
| 69 | if (alloc_record_count > 0xffff) { |
| 70 | return 0xffff; |
| 71 | } |
| 72 | return alloc_record_count; |
| 73 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 74 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 75 | class AllocRecordStackTraceElement { |
| 76 | public: |
| 77 | AllocRecordStackTraceElement() : method_(nullptr), dex_pc_(0) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 80 | int32_t LineNumber() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 81 | ArtMethod* method = Method(); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 82 | DCHECK(method != nullptr); |
| 83 | return method->GetLineNumFromDexPC(DexPc()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 84 | } |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 85 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 86 | ArtMethod* Method() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 87 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 88 | return soa.DecodeMethod(method_); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 91 | void SetMethod(ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 92 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 93 | method_ = soa.EncodeMethod(m); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | uint32_t DexPc() const { |
| 97 | return dex_pc_; |
| 98 | } |
| 99 | |
| 100 | void SetDexPc(uint32_t pc) { |
| 101 | dex_pc_ = pc; |
| 102 | } |
| 103 | |
| 104 | private: |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 105 | jmethodID method_; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 106 | uint32_t dex_pc_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 109 | jobject Dbg::TypeCache::Add(mirror::Class* t) { |
| 110 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | 4c4d609 | 2015-01-22 17:02:27 -0800 | [diff] [blame] | 111 | JNIEnv* const env = soa.Env(); |
| 112 | ScopedLocalRef<jobject> local_ref(soa.Env(), soa.AddLocalReference<jobject>(t)); |
| 113 | const int32_t hash_code = soa.Decode<mirror::Class*>(local_ref.get())->IdentityHashCode(); |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 114 | auto range = objects_.equal_range(hash_code); |
| 115 | for (auto it = range.first; it != range.second; ++it) { |
Mathieu Chartier | 4c4d609 | 2015-01-22 17:02:27 -0800 | [diff] [blame] | 116 | if (soa.Decode<mirror::Class*>(it->second) == soa.Decode<mirror::Class*>(local_ref.get())) { |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 117 | // Found a matching weak global, return it. |
| 118 | return it->second; |
| 119 | } |
| 120 | } |
Mathieu Chartier | 4c4d609 | 2015-01-22 17:02:27 -0800 | [diff] [blame] | 121 | const jobject weak_global = env->NewWeakGlobalRef(local_ref.get()); |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 122 | objects_.insert(std::make_pair(hash_code, weak_global)); |
| 123 | return weak_global; |
| 124 | } |
| 125 | |
| 126 | void Dbg::TypeCache::Clear() { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 127 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 128 | Thread* self = Thread::Current(); |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 129 | for (const auto& p : objects_) { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 130 | vm->DeleteWeakGlobalRef(self, p.second); |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 131 | } |
| 132 | objects_.clear(); |
| 133 | } |
| 134 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 135 | class AllocRecord { |
| 136 | public: |
| 137 | AllocRecord() : type_(nullptr), byte_count_(0), thin_lock_id_(0) {} |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 138 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 139 | mirror::Class* Type() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 140 | return down_cast<mirror::Class*>(Thread::Current()->DecodeJObject(type_)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 143 | void SetType(mirror::Class* t) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, |
| 144 | Locks::alloc_tracker_lock_) { |
| 145 | type_ = Dbg::type_cache_.Add(t); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | size_t GetDepth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 149 | size_t depth = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 150 | while (depth < kMaxAllocRecordStackDepth && stack_[depth].Method() != nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 151 | ++depth; |
| 152 | } |
| 153 | return depth; |
| 154 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 155 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 156 | size_t ByteCount() const { |
| 157 | return byte_count_; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 158 | } |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 159 | |
| 160 | void SetByteCount(size_t count) { |
| 161 | byte_count_ = count; |
| 162 | } |
| 163 | |
| 164 | uint16_t ThinLockId() const { |
| 165 | return thin_lock_id_; |
| 166 | } |
| 167 | |
| 168 | void SetThinLockId(uint16_t id) { |
| 169 | thin_lock_id_ = id; |
| 170 | } |
| 171 | |
| 172 | AllocRecordStackTraceElement* StackElement(size_t index) { |
| 173 | DCHECK_LT(index, kMaxAllocRecordStackDepth); |
| 174 | return &stack_[index]; |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | jobject type_; // This is a weak global. |
| 179 | size_t byte_count_; |
| 180 | uint16_t thin_lock_id_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 181 | // Unused entries have null method. |
| 182 | AllocRecordStackTraceElement stack_[kMaxAllocRecordStackDepth]; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 183 | }; |
| 184 | |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 185 | class Breakpoint { |
| 186 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 187 | Breakpoint(ArtMethod* method, uint32_t dex_pc, |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 188 | DeoptimizationRequest::Kind deoptimization_kind) |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 189 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 190 | : method_(nullptr), dex_pc_(dex_pc), deoptimization_kind_(deoptimization_kind) { |
| 191 | CHECK(deoptimization_kind_ == DeoptimizationRequest::kNothing || |
| 192 | deoptimization_kind_ == DeoptimizationRequest::kSelectiveDeoptimization || |
| 193 | deoptimization_kind_ == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 194 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 195 | method_ = soa.EncodeMethod(method); |
| 196 | } |
| 197 | |
| 198 | Breakpoint(const Breakpoint& other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 199 | : method_(nullptr), dex_pc_(other.dex_pc_), |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 200 | deoptimization_kind_(other.deoptimization_kind_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 201 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 202 | method_ = soa.EncodeMethod(other.Method()); |
| 203 | } |
| 204 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 205 | ArtMethod* Method() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 206 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 207 | return soa.DecodeMethod(method_); |
| 208 | } |
| 209 | |
| 210 | uint32_t DexPc() const { |
| 211 | return dex_pc_; |
| 212 | } |
| 213 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 214 | DeoptimizationRequest::Kind GetDeoptimizationKind() const { |
| 215 | return deoptimization_kind_; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | private: |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 219 | // The location of this breakpoint. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 220 | jmethodID method_; |
| 221 | uint32_t dex_pc_; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 222 | |
| 223 | // Indicates whether breakpoint needs full deoptimization or selective deoptimization. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 224 | DeoptimizationRequest::Kind deoptimization_kind_; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 227 | static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 228 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 229 | os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.Method()).c_str(), rhs.DexPc()); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 230 | return os; |
| 231 | } |
| 232 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 233 | class DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 234 | public: |
| 235 | DebugInstrumentationListener() {} |
| 236 | virtual ~DebugInstrumentationListener() {} |
| 237 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 238 | void MethodEntered(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 239 | uint32_t dex_pc) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 240 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 241 | if (method->IsNative()) { |
| 242 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 243 | return; |
| 244 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 245 | if (IsListeningToDexPcMoved()) { |
| 246 | // We also listen to kDexPcMoved instrumentation event so we know the DexPcMoved method is |
| 247 | // going to be called right after us. To avoid sending JDWP events twice for this location, |
| 248 | // we report the event in DexPcMoved. However, we must remind this is method entry so we |
| 249 | // send the METHOD_ENTRY event. And we can also group it with other events for this location |
| 250 | // like BREAKPOINT or SINGLE_STEP (or even METHOD_EXIT if this is a RETURN instruction). |
| 251 | thread->SetDebugMethodEntry(); |
| 252 | } else if (IsListeningToMethodExit() && IsReturn(method, dex_pc)) { |
| 253 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 254 | // RETURN so we know the MethodExited method is going to be called right after us. To avoid |
| 255 | // sending JDWP events twice for this location, we report the event(s) in MethodExited. |
| 256 | // However, we must remind this is method entry so we send the METHOD_ENTRY event. And we can |
| 257 | // also group it with other events for this location like BREAKPOINT or SINGLE_STEP. |
| 258 | thread->SetDebugMethodEntry(); |
| 259 | } else { |
| 260 | Dbg::UpdateDebugger(thread, this_object, method, 0, Dbg::kMethodEntry, nullptr); |
| 261 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 264 | void MethodExited(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 265 | uint32_t dex_pc, const JValue& return_value) |
| 266 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 267 | if (method->IsNative()) { |
| 268 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 269 | return; |
| 270 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 271 | uint32_t events = Dbg::kMethodExit; |
| 272 | if (thread->IsDebugMethodEntry()) { |
| 273 | // It is also the method entry. |
| 274 | DCHECK(IsReturn(method, dex_pc)); |
| 275 | events |= Dbg::kMethodEntry; |
| 276 | thread->ClearDebugMethodEntry(); |
| 277 | } |
| 278 | Dbg::UpdateDebugger(thread, this_object, method, dex_pc, events, &return_value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 281 | void MethodUnwind(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 282 | ArtMethod* method, uint32_t dex_pc) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 283 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 284 | // We're not recorded to listen to this kind of event, so complain. |
| 285 | LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method) |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 286 | << " " << dex_pc; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 289 | void DexPcMoved(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 290 | uint32_t new_dex_pc) |
| 291 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 292 | if (IsListeningToMethodExit() && IsReturn(method, new_dex_pc)) { |
| 293 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 294 | // RETURN so we know the MethodExited method is going to be called right after us. Like in |
| 295 | // MethodEntered, we delegate event reporting to MethodExited. |
| 296 | // Besides, if this RETURN instruction is the only one in the method, we can send multiple |
| 297 | // JDWP events in the same packet: METHOD_ENTRY, METHOD_EXIT, BREAKPOINT and/or SINGLE_STEP. |
| 298 | // Therefore, we must not clear the debug method entry flag here. |
| 299 | } else { |
| 300 | uint32_t events = 0; |
| 301 | if (thread->IsDebugMethodEntry()) { |
| 302 | // It is also the method entry. |
| 303 | events = Dbg::kMethodEntry; |
| 304 | thread->ClearDebugMethodEntry(); |
| 305 | } |
| 306 | Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc, events, nullptr); |
| 307 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 310 | void FieldRead(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 311 | ArtMethod* method, uint32_t dex_pc, ArtField* field) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 312 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 313 | Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 314 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 315 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 316 | void FieldWritten(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 317 | ArtMethod* method, uint32_t dex_pc, ArtField* field, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 318 | const JValue& field_value) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 319 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 320 | Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value); |
| 321 | } |
| 322 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 323 | void ExceptionCaught(Thread* thread ATTRIBUTE_UNUSED, mirror::Throwable* exception_object) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 324 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 325 | Dbg::PostException(exception_object); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 328 | // We only care about how many backward branches were executed in the Jit. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 329 | void BackwardBranch(Thread* /*thread*/, ArtMethod* method, int32_t dex_pc_offset) |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 330 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 331 | LOG(ERROR) << "Unexpected backward branch event in debugger " << PrettyMethod(method) |
| 332 | << " " << dex_pc_offset; |
| 333 | } |
| 334 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 335 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 336 | static bool IsReturn(ArtMethod* method, uint32_t dex_pc) |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 337 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 338 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
| 339 | const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]); |
| 340 | return instruction->IsReturn(); |
| 341 | } |
| 342 | |
| 343 | static bool IsListeningToDexPcMoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 344 | return IsListeningTo(instrumentation::Instrumentation::kDexPcMoved); |
| 345 | } |
| 346 | |
| 347 | static bool IsListeningToMethodExit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 348 | return IsListeningTo(instrumentation::Instrumentation::kMethodExited); |
| 349 | } |
| 350 | |
| 351 | static bool IsListeningTo(instrumentation::Instrumentation::InstrumentationEvent event) |
| 352 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 353 | return (Dbg::GetInstrumentationEvents() & event) != 0; |
| 354 | } |
| 355 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 356 | DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 357 | } gDebugInstrumentationListener; |
| 358 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 359 | // JDWP is allowed unless the Zygote forbids it. |
| 360 | static bool gJdwpAllowed = true; |
| 361 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 362 | // Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line? |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 363 | static bool gJdwpConfigured = false; |
| 364 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 365 | // JDWP options for debugging. Only valid if IsJdwpConfigured() is true. |
| 366 | static JDWP::JdwpOptions gJdwpOptions; |
| 367 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 368 | // Runtime JDWP state. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 369 | static JDWP::JdwpState* gJdwpState = nullptr; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 370 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 371 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 372 | static bool gDdmThreadNotification = false; |
| 373 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 374 | // DDMS GC-related settings. |
| 375 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 376 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 377 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 378 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 379 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 380 | |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 381 | bool Dbg::gDebuggerActive = false; |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 382 | bool Dbg::gDisposed = false; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 383 | ObjectRegistry* Dbg::gRegistry = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 384 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 385 | // Recent allocation tracking. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 386 | AllocRecord* Dbg::recent_allocation_records_ = nullptr; // TODO: CircularBuffer<AllocRecord> |
| 387 | size_t Dbg::alloc_record_max_ = 0; |
| 388 | size_t Dbg::alloc_record_head_ = 0; |
| 389 | size_t Dbg::alloc_record_count_ = 0; |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 390 | Dbg::TypeCache Dbg::type_cache_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 391 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 392 | // Deoptimization support. |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 393 | std::vector<DeoptimizationRequest> Dbg::deoptimization_requests_; |
| 394 | size_t Dbg::full_deoptimization_event_count_ = 0; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 395 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 396 | // Instrumentation event reference counters. |
| 397 | size_t Dbg::dex_pc_change_event_ref_count_ = 0; |
| 398 | size_t Dbg::method_enter_event_ref_count_ = 0; |
| 399 | size_t Dbg::method_exit_event_ref_count_ = 0; |
| 400 | size_t Dbg::field_read_event_ref_count_ = 0; |
| 401 | size_t Dbg::field_write_event_ref_count_ = 0; |
| 402 | size_t Dbg::exception_catch_event_ref_count_ = 0; |
| 403 | uint32_t Dbg::instrumentation_events_ = 0; |
| 404 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 405 | // Breakpoints. |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 406 | static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 407 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 408 | void DebugInvokeReq::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 409 | receiver.VisitRootIfNonNull(visitor, root_info); // null for static method call. |
| 410 | klass.VisitRoot(visitor, root_info); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 413 | void SingleStepControl::AddDexPc(uint32_t dex_pc) { |
| 414 | dex_pcs_.insert(dex_pc); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 417 | bool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const { |
| 418 | return dex_pcs_.find(dex_pc) == dex_pcs_.end(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 421 | static bool IsBreakpoint(const ArtMethod* m, uint32_t dex_pc) |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 422 | LOCKS_EXCLUDED(Locks::breakpoint_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 423 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 424 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 425 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 426 | if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].Method() == m) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 427 | VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i]; |
| 428 | return true; |
| 429 | } |
| 430 | } |
| 431 | return false; |
| 432 | } |
| 433 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 434 | static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) |
| 435 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) { |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 436 | MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); |
| 437 | // A thread may be suspended for GC; in this code, we really want to know whether |
| 438 | // there's a debugger suspension active. |
| 439 | return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0; |
| 440 | } |
| 441 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 442 | static mirror::Array* DecodeNonNullArray(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 443 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 444 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 445 | if (o == nullptr) { |
| 446 | *error = JDWP::ERR_INVALID_OBJECT; |
| 447 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 448 | } |
| 449 | if (!o->IsArrayInstance()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 450 | *error = JDWP::ERR_INVALID_ARRAY; |
| 451 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 452 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 453 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 454 | return o->AsArray(); |
| 455 | } |
| 456 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 457 | static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 458 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 459 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 460 | if (o == nullptr) { |
| 461 | *error = JDWP::ERR_INVALID_OBJECT; |
| 462 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 463 | } |
| 464 | if (!o->IsClass()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 465 | *error = JDWP::ERR_INVALID_CLASS; |
| 466 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 467 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 468 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 469 | return o->AsClass(); |
| 470 | } |
| 471 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 472 | static Thread* DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, |
| 473 | JDWP::JdwpError* error) |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 474 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 475 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::thread_suspend_count_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 476 | mirror::Object* thread_peer = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 477 | if (thread_peer == nullptr) { |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 478 | // This isn't even an object. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 479 | *error = JDWP::ERR_INVALID_OBJECT; |
| 480 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 481 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 482 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 483 | 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] | 484 | if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) { |
| 485 | // This isn't a thread. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 486 | *error = JDWP::ERR_INVALID_THREAD; |
| 487 | return nullptr; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 490 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 491 | Thread* thread = Thread::FromManagedThread(soa, thread_peer); |
| 492 | // If thread is null then this a java.lang.Thread without a Thread*. Must be a un-started or a |
| 493 | // zombie. |
| 494 | *error = (thread == nullptr) ? JDWP::ERR_THREAD_NOT_ALIVE : JDWP::ERR_NONE; |
| 495 | return thread; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 498 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 499 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 500 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 501 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 502 | } |
| 503 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 504 | static JDWP::JdwpTag BasicTagFromClass(mirror::Class* klass) |
| 505 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 506 | std::string temp; |
| 507 | const char* descriptor = klass->GetDescriptor(&temp); |
| 508 | return BasicTagFromDescriptor(descriptor); |
| 509 | } |
| 510 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 511 | static JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 512 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 513 | CHECK(c != nullptr); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 514 | if (c->IsArrayClass()) { |
| 515 | return JDWP::JT_ARRAY; |
| 516 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 517 | if (c->IsStringClass()) { |
| 518 | return JDWP::JT_STRING; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 519 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 520 | if (c->IsClassClass()) { |
| 521 | return JDWP::JT_CLASS_OBJECT; |
| 522 | } |
| 523 | { |
| 524 | mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
| 525 | if (thread_class->IsAssignableFrom(c)) { |
| 526 | return JDWP::JT_THREAD; |
| 527 | } |
| 528 | } |
| 529 | { |
| 530 | mirror::Class* thread_group_class = |
| 531 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup); |
| 532 | if (thread_group_class->IsAssignableFrom(c)) { |
| 533 | return JDWP::JT_THREAD_GROUP; |
| 534 | } |
| 535 | } |
| 536 | { |
| 537 | mirror::Class* class_loader_class = |
| 538 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader); |
| 539 | if (class_loader_class->IsAssignableFrom(c)) { |
| 540 | return JDWP::JT_CLASS_LOADER; |
| 541 | } |
| 542 | } |
| 543 | return JDWP::JT_OBJECT; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* |
| 547 | * Objects declared to hold Object might actually hold a more specific |
| 548 | * type. The debugger may take a special interest in these (e.g. it |
| 549 | * wants to display the contents of Strings), so we want to return an |
| 550 | * appropriate tag. |
| 551 | * |
| 552 | * Null objects are tagged JT_OBJECT. |
| 553 | */ |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 554 | JDWP::JdwpTag Dbg::TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 555 | return (o == nullptr) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 559 | switch (tag) { |
| 560 | case JDWP::JT_BOOLEAN: |
| 561 | case JDWP::JT_BYTE: |
| 562 | case JDWP::JT_CHAR: |
| 563 | case JDWP::JT_FLOAT: |
| 564 | case JDWP::JT_DOUBLE: |
| 565 | case JDWP::JT_INT: |
| 566 | case JDWP::JT_LONG: |
| 567 | case JDWP::JT_SHORT: |
| 568 | case JDWP::JT_VOID: |
| 569 | return true; |
| 570 | default: |
| 571 | return false; |
| 572 | } |
| 573 | } |
| 574 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 575 | void Dbg::StartJdwp() { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 576 | if (!gJdwpAllowed || !IsJdwpConfigured()) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 577 | // No JDWP for you! |
| 578 | return; |
| 579 | } |
| 580 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 581 | CHECK(gRegistry == nullptr); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 582 | gRegistry = new ObjectRegistry; |
| 583 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 584 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 585 | // debugger, passively listen for a debugger, or block waiting for a |
| 586 | // debugger. |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 587 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 588 | if (gJdwpState == nullptr) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 589 | // We probably failed because some other process has the port already, which means that |
| 590 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 591 | // talking to that other process. |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 592 | LOG(FATAL) << "Debugger thread failed to initialize"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // If a debugger has already attached, send the "welcome" message. |
| 596 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 597 | if (gJdwpState->IsActive()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 598 | ScopedObjectAccess soa(Thread::Current()); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 599 | gJdwpState->PostVMStart(); |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 600 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 603 | void Dbg::StopJdwp() { |
Sebastien Hertz | c6345ef | 2014-08-18 19:26:39 +0200 | [diff] [blame] | 604 | // Post VM_DEATH event before the JDWP connection is closed (either by the JDWP thread or the |
| 605 | // destruction of gJdwpState). |
| 606 | if (gJdwpState != nullptr && gJdwpState->IsActive()) { |
| 607 | gJdwpState->PostVMDeath(); |
| 608 | } |
Sebastien Hertz | 0376e6b | 2014-02-06 18:12:59 +0100 | [diff] [blame] | 609 | // Prevent the JDWP thread from processing JDWP incoming packets after we close the connection. |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 610 | Dispose(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 611 | delete gJdwpState; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 612 | gJdwpState = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 613 | delete gRegistry; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 614 | gRegistry = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 617 | void Dbg::GcDidFinish() { |
| 618 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 619 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 620 | VLOG(jdwp) << "Sending heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 621 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 622 | } |
| 623 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 624 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 625 | VLOG(jdwp) << "Dumping heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 626 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 627 | } |
| 628 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 629 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 630 | VLOG(jdwp) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 631 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 635 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 636 | gJdwpAllowed = allowed; |
| 637 | } |
| 638 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 639 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 640 | return Thread::Current()->GetInvokeReq(); |
| 641 | } |
| 642 | |
| 643 | Thread* Dbg::GetDebugThread() { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 644 | return (gJdwpState != nullptr) ? gJdwpState->GetDebugThread() : nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void Dbg::ClearWaitForEventThread() { |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 648 | gJdwpState->ReleaseJdwpTokenForEvent(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 652 | CHECK(!gDebuggerConnected); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 653 | VLOG(jdwp) << "JDWP has attached"; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 654 | gDebuggerConnected = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 655 | gDisposed = false; |
| 656 | } |
| 657 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 658 | bool Dbg::RequiresDeoptimization() { |
| 659 | // We don't need deoptimization if everything runs with interpreter after |
| 660 | // enabling -Xint mode. |
| 661 | return !Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly(); |
| 662 | } |
| 663 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 664 | void Dbg::GoActive() { |
| 665 | // Enable all debugging features, including scans for breakpoints. |
| 666 | // This is a no-op if we're already active. |
| 667 | // Only called from the JDWP handler thread. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 668 | if (IsDebuggerActive()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 669 | return; |
| 670 | } |
| 671 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 672 | { |
| 673 | // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected? |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 674 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 675 | CHECK_EQ(gBreakpoints.size(), 0U); |
| 676 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 677 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 678 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 679 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 680 | CHECK_EQ(deoptimization_requests_.size(), 0U); |
| 681 | CHECK_EQ(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 682 | CHECK_EQ(dex_pc_change_event_ref_count_, 0U); |
| 683 | CHECK_EQ(method_enter_event_ref_count_, 0U); |
| 684 | CHECK_EQ(method_exit_event_ref_count_, 0U); |
| 685 | CHECK_EQ(field_read_event_ref_count_, 0U); |
| 686 | CHECK_EQ(field_write_event_ref_count_, 0U); |
| 687 | CHECK_EQ(exception_catch_event_ref_count_, 0U); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 690 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 691 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 692 | Thread* self = Thread::Current(); |
| 693 | ThreadState old_state = self->SetStateUnsafe(kRunnable); |
| 694 | CHECK_NE(old_state, kRunnable); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 695 | if (RequiresDeoptimization()) { |
| 696 | runtime->GetInstrumentation()->EnableDeoptimization(); |
| 697 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 698 | instrumentation_events_ = 0; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 699 | gDebuggerActive = true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 700 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 701 | runtime->GetThreadList()->ResumeAll(); |
| 702 | |
| 703 | LOG(INFO) << "Debugger is active"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 707 | CHECK(gDebuggerConnected); |
| 708 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 709 | LOG(INFO) << "Debugger is no longer active"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 710 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 711 | // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread |
| 712 | // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener |
| 713 | // and clear the object registry. |
| 714 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 715 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 716 | Thread* self = Thread::Current(); |
| 717 | ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 718 | |
| 719 | // Debugger may not be active at this point. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 720 | if (IsDebuggerActive()) { |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 721 | { |
| 722 | // Since we're going to disable deoptimization, we clear the deoptimization requests queue. |
| 723 | // This prevents us from having any pending deoptimization request when the debugger attaches |
| 724 | // to us again while no event has been requested yet. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 725 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 726 | deoptimization_requests_.clear(); |
| 727 | full_deoptimization_event_count_ = 0U; |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 728 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 729 | if (instrumentation_events_ != 0) { |
| 730 | runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener, |
| 731 | instrumentation_events_); |
| 732 | instrumentation_events_ = 0; |
| 733 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 734 | if (RequiresDeoptimization()) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 735 | runtime->GetInstrumentation()->DisableDeoptimization(kDbgInstrumentationKey); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 736 | } |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 737 | gDebuggerActive = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 738 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 739 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 740 | runtime->GetThreadList()->ResumeAll(); |
Sebastien Hertz | 55f6534 | 2015-01-13 22:48:34 +0100 | [diff] [blame] | 741 | |
| 742 | { |
| 743 | ScopedObjectAccess soa(self); |
| 744 | gRegistry->Clear(); |
| 745 | } |
| 746 | |
| 747 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 750 | void Dbg::ConfigureJdwp(const JDWP::JdwpOptions& jdwp_options) { |
| 751 | CHECK_NE(jdwp_options.transport, JDWP::kJdwpTransportUnknown); |
| 752 | gJdwpOptions = jdwp_options; |
| 753 | gJdwpConfigured = true; |
| 754 | } |
| 755 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 756 | bool Dbg::IsJdwpConfigured() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 757 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | int64_t Dbg::LastDebuggerActivity() { |
Elliott Hughes | ca95152 | 2011-12-05 12:01:32 -0800 | [diff] [blame] | 761 | return gJdwpState->LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 764 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 765 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 768 | std::string Dbg::GetClassName(JDWP::RefTypeId class_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 769 | JDWP::JdwpError error; |
| 770 | mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id, &error); |
| 771 | if (o == nullptr) { |
| 772 | if (error == JDWP::ERR_NONE) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 773 | return "null"; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 774 | } else { |
| 775 | return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id)); |
| 776 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 777 | } |
| 778 | if (!o->IsClass()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 779 | 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] | 780 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 781 | return GetClassName(o->AsClass()); |
| 782 | } |
| 783 | |
| 784 | std::string Dbg::GetClassName(mirror::Class* klass) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 785 | if (klass == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 786 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 787 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 788 | std::string temp; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 789 | return DescriptorToName(klass->GetDescriptor(&temp)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 792 | JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId* class_object_id) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 793 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 794 | mirror::Class* c = DecodeClass(id, &status); |
| 795 | if (c == nullptr) { |
| 796 | *class_object_id = 0; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 797 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 798 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 799 | *class_object_id = gRegistry->Add(c); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 800 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 803 | JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId* superclass_id) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 804 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 805 | mirror::Class* c = DecodeClass(id, &status); |
| 806 | if (c == nullptr) { |
| 807 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 808 | return status; |
| 809 | } |
| 810 | if (c->IsInterface()) { |
| 811 | // http://code.google.com/p/android/issues/detail?id=20856 |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 812 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 813 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 814 | *superclass_id = gRegistry->Add(c->GetSuperClass()); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 815 | } |
| 816 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 819 | JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 820 | JDWP::JdwpError error; |
| 821 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 822 | if (o == nullptr) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 823 | return JDWP::ERR_INVALID_OBJECT; |
| 824 | } |
| 825 | expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader())); |
| 826 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 829 | JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 830 | JDWP::JdwpError error; |
| 831 | mirror::Class* c = DecodeClass(id, &error); |
| 832 | if (c == nullptr) { |
| 833 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 834 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 835 | |
| 836 | uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask; |
| 837 | |
Yevgeny Rouban | de34eea | 2014-02-15 01:06:03 +0700 | [diff] [blame] | 838 | // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set, |
| 839 | // not interfaces. |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 840 | // 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] | 841 | if ((access_flags & kAccInterface) == 0) { |
| 842 | access_flags |= kAccSuper; |
| 843 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 844 | |
| 845 | expandBufAdd4BE(pReply, access_flags); |
| 846 | |
| 847 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 850 | JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) { |
| 851 | JDWP::JdwpError error; |
| 852 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 853 | if (o == nullptr) { |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 854 | return JDWP::ERR_INVALID_OBJECT; |
| 855 | } |
| 856 | |
| 857 | // Ensure all threads are suspended while we read objects' lock words. |
| 858 | Thread* self = Thread::Current(); |
Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 859 | CHECK_EQ(self->GetState(), kRunnable); |
| 860 | self->TransitionFromRunnableToSuspended(kSuspended); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 861 | Runtime::Current()->GetThreadList()->SuspendAll(__FUNCTION__); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 862 | |
| 863 | MonitorInfo monitor_info(o); |
| 864 | |
Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 865 | Runtime::Current()->GetThreadList()->ResumeAll(); |
| 866 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 867 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 868 | if (monitor_info.owner_ != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 869 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 870 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 871 | expandBufAddObjectId(reply, gRegistry->Add(nullptr)); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 872 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 873 | expandBufAdd4BE(reply, monitor_info.entry_count_); |
| 874 | expandBufAdd4BE(reply, monitor_info.waiters_.size()); |
| 875 | for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) { |
| 876 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 877 | } |
| 878 | return JDWP::ERR_NONE; |
| 879 | } |
| 880 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 881 | JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 882 | std::vector<JDWP::ObjectId>* monitors, |
| 883 | std::vector<uint32_t>* stack_depths) { |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 884 | struct OwnedMonitorVisitor : public StackVisitor { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 885 | OwnedMonitorVisitor(Thread* thread, Context* context, |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 886 | std::vector<JDWP::ObjectId>* monitor_vector, |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 887 | std::vector<uint32_t>* stack_depth_vector) |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 888 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 889 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 890 | current_stack_depth(0), |
| 891 | monitors(monitor_vector), |
| 892 | stack_depths(stack_depth_vector) {} |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 893 | |
| 894 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 895 | // annotalysis. |
| 896 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 897 | if (!GetMethod()->IsRuntimeMethod()) { |
| 898 | Monitor::VisitLocks(this, AppendOwnedMonitors, this); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 899 | ++current_stack_depth; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 900 | } |
| 901 | return true; |
| 902 | } |
| 903 | |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 904 | static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) |
| 905 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 906 | OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 907 | visitor->monitors->push_back(gRegistry->Add(owned_monitor)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 908 | visitor->stack_depths->push_back(visitor->current_stack_depth); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 909 | } |
| 910 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 911 | size_t current_stack_depth; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 912 | std::vector<JDWP::ObjectId>* const monitors; |
| 913 | std::vector<uint32_t>* const stack_depths; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 914 | }; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 915 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 916 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 917 | JDWP::JdwpError error; |
| 918 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 919 | if (thread == nullptr) { |
| 920 | return error; |
| 921 | } |
| 922 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 923 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 924 | } |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 925 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 926 | OwnedMonitorVisitor visitor(thread, context.get(), monitors, stack_depths); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 927 | visitor.WalkStack(); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 928 | return JDWP::ERR_NONE; |
| 929 | } |
| 930 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 931 | JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 932 | JDWP::ObjectId* contended_monitor) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 933 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 934 | *contended_monitor = 0; |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 935 | JDWP::JdwpError error; |
| 936 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 937 | if (thread == nullptr) { |
| 938 | return error; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 939 | } |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 940 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 941 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 942 | } |
| 943 | mirror::Object* contended_monitor_obj = Monitor::GetContendedMonitor(thread); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 944 | // Add() requires the thread_list_lock_ not held to avoid the lock |
| 945 | // level violation. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 946 | *contended_monitor = gRegistry->Add(contended_monitor_obj); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 947 | return JDWP::ERR_NONE; |
| 948 | } |
| 949 | |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 950 | JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 951 | std::vector<uint64_t>* counts) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 952 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 953 | heap->CollectGarbage(false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 954 | std::vector<mirror::Class*> classes; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 955 | counts->clear(); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 956 | for (size_t i = 0; i < class_ids.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 957 | JDWP::JdwpError error; |
| 958 | mirror::Class* c = DecodeClass(class_ids[i], &error); |
| 959 | if (c == nullptr) { |
| 960 | return error; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 961 | } |
| 962 | classes.push_back(c); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 963 | counts->push_back(0); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 964 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 965 | heap->CountInstances(classes, false, &(*counts)[0]); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 966 | return JDWP::ERR_NONE; |
| 967 | } |
| 968 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 969 | JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, |
| 970 | std::vector<JDWP::ObjectId>* instances) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 971 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 972 | // We only want reachable instances, so do a GC. |
| 973 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 974 | JDWP::JdwpError error; |
| 975 | mirror::Class* c = DecodeClass(class_id, &error); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 976 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 977 | return error; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 978 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 979 | std::vector<mirror::Object*> raw_instances; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 980 | Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances); |
| 981 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 982 | instances->push_back(gRegistry->Add(raw_instances[i])); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 983 | } |
| 984 | return JDWP::ERR_NONE; |
| 985 | } |
| 986 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 987 | JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 988 | std::vector<JDWP::ObjectId>* referring_objects) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 989 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 990 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 991 | JDWP::JdwpError error; |
| 992 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 993 | if (o == nullptr) { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 994 | return JDWP::ERR_INVALID_OBJECT; |
| 995 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 996 | std::vector<mirror::Object*> raw_instances; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 997 | heap->GetReferringObjects(o, max_count, raw_instances); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 998 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 999 | referring_objects->push_back(gRegistry->Add(raw_instances[i])); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1000 | } |
| 1001 | return JDWP::ERR_NONE; |
| 1002 | } |
| 1003 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1004 | JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id) { |
| 1005 | JDWP::JdwpError error; |
| 1006 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1007 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1008 | return JDWP::ERR_INVALID_OBJECT; |
| 1009 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1010 | gRegistry->DisableCollection(object_id); |
| 1011 | return JDWP::ERR_NONE; |
| 1012 | } |
| 1013 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1014 | JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id) { |
| 1015 | JDWP::JdwpError error; |
| 1016 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1017 | // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI |
| 1018 | // also ignores these cases and never return an error. However it's not obvious why this command |
| 1019 | // should behave differently from DisableCollection and IsCollected commands. So let's be more |
| 1020 | // strict and return an error if this happens. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1021 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1022 | return JDWP::ERR_INVALID_OBJECT; |
| 1023 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1024 | gRegistry->EnableCollection(object_id); |
| 1025 | return JDWP::ERR_NONE; |
| 1026 | } |
| 1027 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1028 | JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool* is_collected) { |
| 1029 | *is_collected = true; |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 1030 | if (object_id == 0) { |
| 1031 | // Null object id is invalid. |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1032 | return JDWP::ERR_INVALID_OBJECT; |
| 1033 | } |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 1034 | // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However |
| 1035 | // the RI seems to ignore this and assume object has been collected. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1036 | JDWP::JdwpError error; |
| 1037 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1038 | if (o != nullptr) { |
| 1039 | *is_collected = gRegistry->IsCollected(object_id); |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 1040 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1041 | return JDWP::ERR_NONE; |
| 1042 | } |
| 1043 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1044 | void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1045 | gRegistry->DisposeObject(object_id, reference_count); |
| 1046 | } |
| 1047 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1048 | JDWP::JdwpTypeTag Dbg::GetTypeTag(mirror::Class* klass) { |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1049 | DCHECK(klass != nullptr); |
| 1050 | if (klass->IsArrayClass()) { |
| 1051 | return JDWP::TT_ARRAY; |
| 1052 | } else if (klass->IsInterface()) { |
| 1053 | return JDWP::TT_INTERFACE; |
| 1054 | } else { |
| 1055 | return JDWP::TT_CLASS; |
| 1056 | } |
| 1057 | } |
| 1058 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1059 | JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1060 | JDWP::JdwpError error; |
| 1061 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1062 | if (c == nullptr) { |
| 1063 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1064 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1065 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1066 | JDWP::JdwpTypeTag type_tag = GetTypeTag(c); |
| 1067 | expandBufAdd1(pReply, type_tag); |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1068 | expandBufAddRefTypeId(pReply, class_id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1069 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1072 | void Dbg::GetClassList(std::vector<JDWP::RefTypeId>* classes) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1073 | // Get the complete list of reference classes (i.e. all classes except |
| 1074 | // the primitive types). |
| 1075 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 1076 | struct ClassListCreator { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1077 | explicit ClassListCreator(std::vector<JDWP::RefTypeId>* classes_in) : classes(classes_in) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1080 | static bool Visit(mirror::Class* c, void* arg) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1081 | return reinterpret_cast<ClassListCreator*>(arg)->Visit(c); |
| 1082 | } |
| 1083 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1084 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1085 | // annotalysis. |
| 1086 | bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1087 | if (!c->IsPrimitive()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1088 | classes->push_back(gRegistry->AddRefType(c)); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1089 | } |
| 1090 | return true; |
| 1091 | } |
| 1092 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1093 | std::vector<JDWP::RefTypeId>* const classes; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1094 | }; |
| 1095 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1096 | ClassListCreator clc(classes); |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 1097 | Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(ClassListCreator::Visit, |
| 1098 | &clc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1101 | JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, |
| 1102 | uint32_t* pStatus, std::string* pDescriptor) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1103 | JDWP::JdwpError error; |
| 1104 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1105 | if (c == nullptr) { |
| 1106 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1107 | } |
| 1108 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1109 | if (c->IsArrayClass()) { |
| 1110 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 1111 | *pTypeTag = JDWP::TT_ARRAY; |
| 1112 | } else { |
| 1113 | if (c->IsErroneous()) { |
| 1114 | *pStatus = JDWP::CS_ERROR; |
| 1115 | } else { |
| 1116 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 1117 | } |
| 1118 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1119 | } |
| 1120 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1121 | if (pDescriptor != nullptr) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1122 | std::string temp; |
| 1123 | *pDescriptor = c->GetDescriptor(&temp); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1124 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1125 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1128 | void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>* ids) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1129 | std::vector<mirror::Class*> classes; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1130 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1131 | ids->clear(); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1132 | for (size_t i = 0; i < classes.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1133 | ids->push_back(gRegistry->Add(classes[i])); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1134 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1137 | JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) { |
| 1138 | JDWP::JdwpError error; |
| 1139 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1140 | if (o == nullptr) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1141 | return JDWP::ERR_INVALID_OBJECT; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1142 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1143 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1144 | JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass()); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1145 | JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1146 | |
| 1147 | expandBufAdd1(pReply, type_tag); |
| 1148 | expandBufAddRefTypeId(pReply, type_id); |
| 1149 | |
| 1150 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 1153 | JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1154 | JDWP::JdwpError error; |
| 1155 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1156 | if (c == nullptr) { |
| 1157 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1158 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1159 | std::string temp; |
| 1160 | *signature = c->GetDescriptor(&temp); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 1161 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1164 | JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string* result) { |
| 1165 | JDWP::JdwpError error; |
| 1166 | mirror::Class* c = DecodeClass(class_id, &error); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1167 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1168 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1169 | } |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1170 | const char* source_file = c->GetSourceFile(); |
| 1171 | if (source_file == nullptr) { |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 1172 | return JDWP::ERR_ABSENT_INFORMATION; |
| 1173 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1174 | *result = source_file; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1175 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1178 | JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t* tag) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1179 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1180 | JDWP::JdwpError error; |
| 1181 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1182 | if (error != JDWP::ERR_NONE) { |
| 1183 | *tag = JDWP::JT_VOID; |
| 1184 | return error; |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1185 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1186 | *tag = TagFromObject(soa, o); |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1187 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1190 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1191 | switch (tag) { |
| 1192 | case JDWP::JT_VOID: |
| 1193 | return 0; |
| 1194 | case JDWP::JT_BYTE: |
| 1195 | case JDWP::JT_BOOLEAN: |
| 1196 | return 1; |
| 1197 | case JDWP::JT_CHAR: |
| 1198 | case JDWP::JT_SHORT: |
| 1199 | return 2; |
| 1200 | case JDWP::JT_FLOAT: |
| 1201 | case JDWP::JT_INT: |
| 1202 | return 4; |
| 1203 | case JDWP::JT_ARRAY: |
| 1204 | case JDWP::JT_OBJECT: |
| 1205 | case JDWP::JT_STRING: |
| 1206 | case JDWP::JT_THREAD: |
| 1207 | case JDWP::JT_THREAD_GROUP: |
| 1208 | case JDWP::JT_CLASS_LOADER: |
| 1209 | case JDWP::JT_CLASS_OBJECT: |
| 1210 | return sizeof(JDWP::ObjectId); |
| 1211 | case JDWP::JT_DOUBLE: |
| 1212 | case JDWP::JT_LONG: |
| 1213 | return 8; |
| 1214 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1215 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1216 | return -1; |
| 1217 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1220 | JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int32_t* length) { |
| 1221 | JDWP::JdwpError error; |
| 1222 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
| 1223 | if (a == nullptr) { |
| 1224 | return error; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1225 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1226 | *length = a->GetLength(); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1227 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1230 | JDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1231 | JDWP::JdwpError error; |
| 1232 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1233 | if (a == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1234 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1235 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1236 | |
| 1237 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 1238 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1239 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1240 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1241 | JDWP::JdwpTag element_tag = BasicTagFromClass(a->GetClass()->GetComponentType()); |
| 1242 | expandBufAdd1(pReply, element_tag); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1243 | expandBufAdd4BE(pReply, count); |
| 1244 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1245 | if (IsPrimitiveTag(element_tag)) { |
| 1246 | size_t width = GetTagWidth(element_tag); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1247 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 1248 | if (width == 8) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1249 | 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] | 1250 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 1251 | } else if (width == 4) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1252 | 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] | 1253 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 1254 | } else if (width == 2) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1255 | 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] | 1256 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 1257 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1258 | 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] | 1259 | memcpy(dst, &src[offset * width], count * width); |
| 1260 | } |
| 1261 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1262 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1263 | mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1264 | for (int i = 0; i < count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1265 | mirror::Object* element = oa->Get(offset + i); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1266 | JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element) |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1267 | : element_tag; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1268 | expandBufAdd1(pReply, specific_tag); |
| 1269 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 1270 | } |
| 1271 | } |
| 1272 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1273 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1276 | template <typename T> |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1277 | static void CopyArrayData(mirror::Array* a, JDWP::Request* src, int offset, int count) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1278 | NO_THREAD_SAFETY_ANALYSIS { |
| 1279 | // TODO: fix when annotalysis correctly handles non-member functions. |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1280 | DCHECK(a->GetClass()->IsPrimitiveArray()); |
| 1281 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1282 | T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1283 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1284 | *dst++ = src->ReadValue(sizeof(T)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1288 | JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1289 | JDWP::Request* request) { |
| 1290 | JDWP::JdwpError error; |
| 1291 | mirror::Array* dst = DecodeNonNullArray(array_id, &error); |
| 1292 | if (dst == nullptr) { |
| 1293 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1294 | } |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1295 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1296 | if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1297 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1298 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1299 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1300 | JDWP::JdwpTag element_tag = BasicTagFromClass(dst->GetClass()->GetComponentType()); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1301 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1302 | if (IsPrimitiveTag(element_tag)) { |
| 1303 | size_t width = GetTagWidth(element_tag); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1304 | if (width == 8) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1305 | CopyArrayData<uint64_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1306 | } else if (width == 4) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1307 | CopyArrayData<uint32_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1308 | } else if (width == 2) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1309 | CopyArrayData<uint16_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1310 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1311 | CopyArrayData<uint8_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1312 | } |
| 1313 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1314 | mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>(); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1315 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1316 | JDWP::ObjectId id = request->ReadObjectId(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1317 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 1318 | if (error != JDWP::ERR_NONE) { |
| 1319 | return error; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1320 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1321 | oa->Set<false>(offset + i, o); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1322 | } |
| 1323 | } |
| 1324 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1325 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1328 | JDWP::JdwpError Dbg::CreateString(const std::string& str, JDWP::ObjectId* new_string_id) { |
| 1329 | Thread* self = Thread::Current(); |
| 1330 | mirror::String* new_string = mirror::String::AllocFromModifiedUtf8(self, str.c_str()); |
| 1331 | if (new_string == nullptr) { |
| 1332 | DCHECK(self->IsExceptionPending()); |
| 1333 | self->ClearException(); |
| 1334 | LOG(ERROR) << "Could not allocate string"; |
| 1335 | *new_string_id = 0; |
| 1336 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1337 | } |
| 1338 | *new_string_id = gRegistry->Add(new_string); |
| 1339 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1342 | JDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId* new_object_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1343 | JDWP::JdwpError error; |
| 1344 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1345 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1346 | *new_object_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1347 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1348 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1349 | Thread* self = Thread::Current(); |
| 1350 | mirror::Object* new_object = c->AllocObject(self); |
| 1351 | if (new_object == nullptr) { |
| 1352 | DCHECK(self->IsExceptionPending()); |
| 1353 | self->ClearException(); |
| 1354 | LOG(ERROR) << "Could not allocate object of type " << PrettyDescriptor(c); |
| 1355 | *new_object_id = 0; |
| 1356 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1357 | } |
| 1358 | *new_object_id = gRegistry->Add(new_object); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1359 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 1362 | /* |
| 1363 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 1364 | */ |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1365 | JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length, |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1366 | JDWP::ObjectId* new_array_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1367 | JDWP::JdwpError error; |
| 1368 | mirror::Class* c = DecodeClass(array_class_id, &error); |
| 1369 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1370 | *new_array_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1371 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1372 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1373 | Thread* self = Thread::Current(); |
| 1374 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1375 | mirror::Array* new_array = mirror::Array::Alloc<true>(self, c, length, |
| 1376 | c->GetComponentSizeShift(), |
| 1377 | heap->GetCurrentAllocator()); |
| 1378 | if (new_array == nullptr) { |
| 1379 | DCHECK(self->IsExceptionPending()); |
| 1380 | self->ClearException(); |
| 1381 | LOG(ERROR) << "Could not allocate array of type " << PrettyDescriptor(c); |
| 1382 | *new_array_id = 0; |
| 1383 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1384 | } |
| 1385 | *new_array_id = gRegistry->Add(new_array); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1386 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1389 | JDWP::FieldId Dbg::ToFieldId(const ArtField* f) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1390 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1393 | static JDWP::MethodId ToMethodId(const ArtMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1394 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1395 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1396 | } |
| 1397 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1398 | static ArtField* FromFieldId(JDWP::FieldId fid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1399 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1400 | return reinterpret_cast<ArtField*>(static_cast<uintptr_t>(fid)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1403 | static ArtMethod* FromMethodId(JDWP::MethodId mid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1404 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1405 | return reinterpret_cast<ArtMethod*>(static_cast<uintptr_t>(mid)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1406 | } |
| 1407 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1408 | bool Dbg::MatchThread(JDWP::ObjectId expected_thread_id, Thread* event_thread) { |
| 1409 | CHECK(event_thread != nullptr); |
| 1410 | JDWP::JdwpError error; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1411 | mirror::Object* expected_thread_peer = gRegistry->Get<mirror::Object*>( |
| 1412 | expected_thread_id, &error); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1413 | return expected_thread_peer == event_thread->GetPeer(); |
| 1414 | } |
| 1415 | |
| 1416 | bool Dbg::MatchLocation(const JDWP::JdwpLocation& expected_location, |
| 1417 | const JDWP::EventLocation& event_location) { |
| 1418 | if (expected_location.dex_pc != event_location.dex_pc) { |
| 1419 | return false; |
| 1420 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1421 | ArtMethod* m = FromMethodId(expected_location.method_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1422 | return m == event_location.method; |
| 1423 | } |
| 1424 | |
| 1425 | bool Dbg::MatchType(mirror::Class* event_class, JDWP::RefTypeId class_id) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1426 | if (event_class == nullptr) { |
| 1427 | return false; |
| 1428 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1429 | JDWP::JdwpError error; |
| 1430 | mirror::Class* expected_class = DecodeClass(class_id, &error); |
| 1431 | CHECK(expected_class != nullptr); |
| 1432 | return expected_class->IsAssignableFrom(event_class); |
| 1433 | } |
| 1434 | |
| 1435 | bool Dbg::MatchField(JDWP::RefTypeId expected_type_id, JDWP::FieldId expected_field_id, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1436 | ArtField* event_field) { |
| 1437 | ArtField* expected_field = FromFieldId(expected_field_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1438 | if (expected_field != event_field) { |
| 1439 | return false; |
| 1440 | } |
| 1441 | return Dbg::MatchType(event_field->GetDeclaringClass(), expected_type_id); |
| 1442 | } |
| 1443 | |
| 1444 | bool Dbg::MatchInstance(JDWP::ObjectId expected_instance_id, mirror::Object* event_instance) { |
| 1445 | JDWP::JdwpError error; |
| 1446 | mirror::Object* modifier_instance = gRegistry->Get<mirror::Object*>(expected_instance_id, &error); |
| 1447 | return modifier_instance == event_instance; |
| 1448 | } |
| 1449 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1450 | void Dbg::SetJdwpLocation(JDWP::JdwpLocation* location, ArtMethod* m, uint32_t dex_pc) |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 1451 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 1452 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 1453 | Locks::thread_suspend_count_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1454 | if (m == nullptr) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1455 | memset(location, 0, sizeof(*location)); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1456 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1457 | mirror::Class* c = m->GetDeclaringClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1458 | location->type_tag = GetTypeTag(c); |
| 1459 | location->class_id = gRegistry->AddRefType(c); |
| 1460 | location->method_id = ToMethodId(m); |
| 1461 | 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] | 1462 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1465 | std::string Dbg::GetMethodName(JDWP::MethodId method_id) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1466 | ArtMethod* m = FromMethodId(method_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1467 | if (m == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1468 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1469 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1470 | return m->GetName(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1473 | std::string Dbg::GetFieldName(JDWP::FieldId field_id) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1474 | ArtField* f = FromFieldId(field_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1475 | if (f == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1476 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1477 | } |
| 1478 | return f->GetName(); |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1481 | /* |
| 1482 | * Augment the access flags for synthetic methods and fields by setting |
| 1483 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 1484 | * flags not specified by the Java programming language. |
| 1485 | */ |
| 1486 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 1487 | accessFlags &= kAccJavaFlagsMask; |
| 1488 | if ((accessFlags & kAccSynthetic) != 0) { |
| 1489 | accessFlags |= 0xf0000000; |
| 1490 | } |
| 1491 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1494 | /* |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1495 | * Circularly shifts registers so that arguments come first. Debuggers |
| 1496 | * expect slots to begin with arguments, but dex code places them at |
| 1497 | * the end. |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1498 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1499 | static uint16_t MangleSlot(uint16_t slot, ArtMethod* m) |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1500 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1501 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1502 | if (code_item == nullptr) { |
| 1503 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1504 | // return the slot as is since all registers are arguments. |
| 1505 | LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m); |
| 1506 | return slot; |
| 1507 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1508 | uint16_t ins_size = code_item->ins_size_; |
| 1509 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1510 | if (slot >= locals_size) { |
| 1511 | return slot - locals_size; |
| 1512 | } else { |
| 1513 | return slot + ins_size; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1514 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1517 | /* |
| 1518 | * Circularly shifts registers so that arguments come last. Reverts |
| 1519 | * slots to dex style argument placement. |
| 1520 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1521 | static uint16_t DemangleSlot(uint16_t slot, ArtMethod* m, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1522 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1523 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1524 | if (code_item == nullptr) { |
| 1525 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1526 | // return the slot as is since all registers are arguments. |
| 1527 | LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1528 | uint16_t vreg_count = ArtMethod::NumArgRegisters(m->GetShorty()); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1529 | if (slot < vreg_count) { |
| 1530 | *error = JDWP::ERR_NONE; |
| 1531 | return slot; |
| 1532 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1533 | } else { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1534 | if (slot < code_item->registers_size_) { |
| 1535 | uint16_t ins_size = code_item->ins_size_; |
| 1536 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1537 | *error = JDWP::ERR_NONE; |
| 1538 | return (slot < ins_size) ? slot + locals_size : slot - ins_size; |
| 1539 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1540 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1541 | |
| 1542 | // Slot is invalid in the method. |
| 1543 | LOG(ERROR) << "Invalid local slot " << slot << " for method " << PrettyMethod(m); |
| 1544 | *error = JDWP::ERR_INVALID_SLOT; |
| 1545 | return DexFile::kDexNoIndex16; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1546 | } |
| 1547 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1548 | JDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1549 | JDWP::JdwpError error; |
| 1550 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1551 | if (c == nullptr) { |
| 1552 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1553 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1554 | |
| 1555 | size_t instance_field_count = c->NumInstanceFields(); |
| 1556 | size_t static_field_count = c->NumStaticFields(); |
| 1557 | |
| 1558 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 1559 | |
| 1560 | for (size_t i = 0; i < instance_field_count + static_field_count; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1561 | ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1562 | expandBufAddFieldId(pReply, ToFieldId(f)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1563 | expandBufAddUtf8String(pReply, f->GetName()); |
| 1564 | expandBufAddUtf8String(pReply, f->GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1565 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1566 | static const char genericSignature[1] = ""; |
| 1567 | expandBufAddUtf8String(pReply, genericSignature); |
| 1568 | } |
| 1569 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 1570 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1571 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1572 | } |
| 1573 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1574 | JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1575 | JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1576 | JDWP::JdwpError error; |
| 1577 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1578 | if (c == nullptr) { |
| 1579 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1580 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1581 | |
| 1582 | size_t direct_method_count = c->NumDirectMethods(); |
| 1583 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 1584 | |
| 1585 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 1586 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1587 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 1588 | auto ptr_size = cl->GetImagePointerSize(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1589 | for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1590 | ArtMethod* m = i < direct_method_count ? |
| 1591 | c->GetDirectMethod(i, ptr_size) : c->GetVirtualMethod(i - direct_method_count, ptr_size); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1592 | expandBufAddMethodId(pReply, ToMethodId(m)); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1593 | expandBufAddUtf8String(pReply, m->GetName()); |
| 1594 | expandBufAddUtf8String(pReply, m->GetSignature().ToString()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1595 | if (with_generic) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1596 | const char* generic_signature = ""; |
| 1597 | expandBufAddUtf8String(pReply, generic_signature); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1598 | } |
| 1599 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 1600 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1601 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1602 | } |
| 1603 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1604 | JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1605 | JDWP::JdwpError error; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1606 | Thread* self = Thread::Current(); |
| 1607 | StackHandleScope<1> hs(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1608 | Handle<mirror::Class> c(hs.NewHandle(DecodeClass(class_id, &error))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1609 | if (c.Get() == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1610 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1611 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1612 | size_t interface_count = c->NumDirectInterfaces(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1613 | expandBufAdd4BE(pReply, interface_count); |
| 1614 | for (size_t i = 0; i < interface_count; ++i) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1615 | expandBufAddRefTypeId(pReply, |
| 1616 | gRegistry->AddRefType(mirror::Class::GetDirectInterface(self, c, i))); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1617 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1618 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1621 | void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1622 | struct DebugCallbackContext { |
| 1623 | int numItems; |
| 1624 | JDWP::ExpandBuf* pReply; |
| 1625 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1626 | static bool Callback(void* context, uint32_t address, uint32_t line_number) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1627 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1628 | expandBufAdd8BE(pContext->pReply, address); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1629 | expandBufAdd4BE(pContext->pReply, line_number); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1630 | pContext->numItems++; |
Sebastien Hertz | f2910ee | 2013-10-19 16:39:24 +0200 | [diff] [blame] | 1631 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1632 | } |
| 1633 | }; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1634 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1635 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1636 | uint64_t start, end; |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1637 | if (code_item == nullptr) { |
| 1638 | DCHECK(m->IsNative() || m->IsProxyMethod()); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1639 | start = -1; |
| 1640 | end = -1; |
| 1641 | } else { |
| 1642 | start = 0; |
jeffhao | 14f0db9 | 2012-12-14 17:50:42 -0800 | [diff] [blame] | 1643 | // Return the index of the last instruction |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1644 | end = code_item->insns_size_in_code_units_ - 1; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | expandBufAdd8BE(pReply, start); |
| 1648 | expandBufAdd8BE(pReply, end); |
| 1649 | |
| 1650 | // Add numLines later |
| 1651 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 1652 | expandBufAdd4BE(pReply, 0); |
| 1653 | |
| 1654 | DebugCallbackContext context; |
| 1655 | context.numItems = 0; |
| 1656 | context.pReply = pReply; |
| 1657 | |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1658 | if (code_item != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1659 | m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1660 | DebugCallbackContext::Callback, nullptr, &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1661 | } |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1662 | |
| 1663 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1666 | void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, |
| 1667 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1668 | struct DebugCallbackContext { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1669 | ArtMethod* method; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1670 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1671 | size_t variable_count; |
| 1672 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1673 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1674 | static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, |
| 1675 | const char* name, const char* descriptor, const char* signature) |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1676 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1677 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1678 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1679 | VLOG(jdwp) << StringPrintf(" %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d", |
| 1680 | pContext->variable_count, startAddress, endAddress - startAddress, |
| 1681 | name, descriptor, signature, slot, |
| 1682 | MangleSlot(slot, pContext->method)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1683 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1684 | slot = MangleSlot(slot, pContext->method); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1685 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1686 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 1687 | expandBufAddUtf8String(pContext->pReply, name); |
| 1688 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1689 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1690 | expandBufAddUtf8String(pContext->pReply, signature); |
| 1691 | } |
| 1692 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 1693 | expandBufAdd4BE(pContext->pReply, slot); |
| 1694 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1695 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1696 | } |
| 1697 | }; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1698 | ArtMethod* m = FromMethodId(method_id); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1699 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1700 | // arg_count considers doubles and longs to take 2 units. |
| 1701 | // variable_count considers everything to take 1 unit. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1702 | std::string shorty(m->GetShorty()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1703 | expandBufAdd4BE(pReply, ArtMethod::NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1704 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1705 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1706 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1707 | expandBufAdd4BE(pReply, 0); |
| 1708 | |
| 1709 | DebugCallbackContext context; |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1710 | context.method = m; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1711 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1712 | context.variable_count = 0; |
| 1713 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1714 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1715 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1716 | if (code_item != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1717 | m->GetDexFile()->DecodeDebugInfo( |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1718 | code_item, m->IsStatic(), m->GetDexMethodIndex(), nullptr, DebugCallbackContext::Callback, |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1719 | &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1720 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1721 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1722 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1725 | void Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value, |
| 1726 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1727 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1728 | JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1729 | OutputJValue(tag, return_value, pReply); |
| 1730 | } |
| 1731 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1732 | void Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value, |
| 1733 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1734 | ArtField* f = FromFieldId(field_id); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1735 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1736 | OutputJValue(tag, field_value, pReply); |
| 1737 | } |
| 1738 | |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1739 | JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1740 | std::vector<uint8_t>* bytecodes) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1741 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1742 | if (m == nullptr) { |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1743 | return JDWP::ERR_INVALID_METHODID; |
| 1744 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1745 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1746 | size_t byte_count = code_item->insns_size_in_code_units_ * 2; |
| 1747 | const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_); |
| 1748 | const uint8_t* end = begin + byte_count; |
| 1749 | for (const uint8_t* p = begin; p != end; ++p) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1750 | bytecodes->push_back(*p); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1751 | } |
| 1752 | return JDWP::ERR_NONE; |
| 1753 | } |
| 1754 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1755 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1756 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1759 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1760 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1763 | static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id, |
| 1764 | JDWP::FieldId field_id, JDWP::ExpandBuf* pReply, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1765 | bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1766 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1767 | JDWP::JdwpError error; |
| 1768 | mirror::Class* c = DecodeClass(ref_type_id, &error); |
| 1769 | if (ref_type_id != 0 && c == nullptr) { |
| 1770 | return error; |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1771 | } |
| 1772 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1773 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1774 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1775 | return JDWP::ERR_INVALID_OBJECT; |
| 1776 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1777 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1778 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1779 | mirror::Class* receiver_class = c; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1780 | if (receiver_class == nullptr && o != nullptr) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1781 | receiver_class = o->GetClass(); |
| 1782 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1783 | // TODO: should we give up now if receiver_class is null? |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1784 | if (receiver_class != nullptr && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1785 | LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1786 | return JDWP::ERR_INVALID_FIELDID; |
| 1787 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1788 | |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1789 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1790 | // TODO: should we change the tests and check both? |
| 1791 | if (is_static) { |
| 1792 | if (!f->IsStatic()) { |
| 1793 | return JDWP::ERR_INVALID_FIELDID; |
| 1794 | } |
| 1795 | } else { |
| 1796 | if (f->IsStatic()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1797 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues on static field " |
| 1798 | << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1799 | } |
| 1800 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1801 | if (f->IsStatic()) { |
| 1802 | o = f->GetDeclaringClass(); |
| 1803 | } |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1804 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1805 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1806 | JValue field_value; |
| 1807 | if (tag == JDWP::JT_VOID) { |
| 1808 | LOG(FATAL) << "Unknown tag: " << tag; |
| 1809 | } else if (!IsPrimitiveTag(tag)) { |
| 1810 | field_value.SetL(f->GetObject(o)); |
| 1811 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1812 | field_value.SetJ(f->Get64(o)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1813 | } else { |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1814 | field_value.SetI(f->Get32(o)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1815 | } |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1816 | Dbg::OutputJValue(tag, &field_value, pReply); |
| 1817 | |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1818 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1821 | JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1822 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1823 | return GetFieldValueImpl(0, object_id, field_id, pReply, false); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1824 | } |
| 1825 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1826 | JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, |
| 1827 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1828 | return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1829 | } |
| 1830 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1831 | static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1832 | uint64_t value, int width, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1833 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1834 | JDWP::JdwpError error; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1835 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1836 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1837 | return JDWP::ERR_INVALID_OBJECT; |
| 1838 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1839 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1840 | |
| 1841 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1842 | // TODO: should we change the tests and check both? |
| 1843 | if (is_static) { |
| 1844 | if (!f->IsStatic()) { |
| 1845 | return JDWP::ERR_INVALID_FIELDID; |
| 1846 | } |
| 1847 | } else { |
| 1848 | if (f->IsStatic()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1849 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues on static field " << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1850 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1851 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1852 | if (f->IsStatic()) { |
| 1853 | o = f->GetDeclaringClass(); |
| 1854 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1855 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1856 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1857 | |
| 1858 | if (IsPrimitiveTag(tag)) { |
| 1859 | if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1860 | CHECK_EQ(width, 8); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1861 | // Debugging can't use transactional mode (runtime only). |
| 1862 | f->Set64<false>(o, value); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1863 | } else { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1864 | CHECK_LE(width, 4); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1865 | // Debugging can't use transactional mode (runtime only). |
| 1866 | f->Set32<false>(o, value); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1867 | } |
| 1868 | } else { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1869 | mirror::Object* v = Dbg::GetObjectRegistry()->Get<mirror::Object*>(value, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1870 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1871 | return JDWP::ERR_INVALID_OBJECT; |
| 1872 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1873 | if (v != nullptr) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1874 | mirror::Class* field_type; |
| 1875 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1876 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1877 | HandleWrapper<mirror::Object> h_v(hs.NewHandleWrapper(&v)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1878 | HandleWrapper<mirror::Object> h_o(hs.NewHandleWrapper(&o)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1879 | field_type = f->GetType<true>(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1880 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1881 | if (!field_type->IsAssignableFrom(v->GetClass())) { |
| 1882 | return JDWP::ERR_INVALID_OBJECT; |
| 1883 | } |
| 1884 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1885 | // Debugging can't use transactional mode (runtime only). |
| 1886 | f->SetObject<false>(o, v); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1887 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1888 | |
| 1889 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1892 | 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] | 1893 | int width) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1894 | return SetFieldValueImpl(object_id, field_id, value, width, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1897 | JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) { |
| 1898 | return SetFieldValueImpl(0, field_id, value, width, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1901 | JDWP::JdwpError Dbg::StringToUtf8(JDWP::ObjectId string_id, std::string* str) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1902 | JDWP::JdwpError error; |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1903 | mirror::Object* obj = gRegistry->Get<mirror::Object*>(string_id, &error); |
| 1904 | if (error != JDWP::ERR_NONE) { |
| 1905 | return error; |
| 1906 | } |
| 1907 | if (obj == nullptr) { |
| 1908 | return JDWP::ERR_INVALID_OBJECT; |
| 1909 | } |
| 1910 | { |
| 1911 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1912 | mirror::Class* java_lang_String = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_String); |
| 1913 | if (!java_lang_String->IsAssignableFrom(obj->GetClass())) { |
| 1914 | // This isn't a string. |
| 1915 | return JDWP::ERR_INVALID_STRING; |
| 1916 | } |
| 1917 | } |
| 1918 | *str = obj->AsString()->ToModifiedUtf8(); |
| 1919 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1922 | void Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) { |
| 1923 | if (IsPrimitiveTag(tag)) { |
| 1924 | expandBufAdd1(pReply, tag); |
| 1925 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 1926 | expandBufAdd1(pReply, return_value->GetI()); |
| 1927 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 1928 | expandBufAdd2BE(pReply, return_value->GetI()); |
| 1929 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 1930 | expandBufAdd4BE(pReply, return_value->GetI()); |
| 1931 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1932 | expandBufAdd8BE(pReply, return_value->GetJ()); |
| 1933 | } else { |
| 1934 | CHECK_EQ(tag, JDWP::JT_VOID); |
| 1935 | } |
| 1936 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1937 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1938 | mirror::Object* value = return_value->GetL(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1939 | expandBufAdd1(pReply, TagFromObject(soa, value)); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1940 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 1941 | } |
| 1942 | } |
| 1943 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1944 | JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string* name) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1945 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1946 | JDWP::JdwpError error; |
| 1947 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 1948 | UNUSED(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1949 | if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1950 | return error; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1951 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1952 | |
| 1953 | // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1954 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 1955 | CHECK(thread_object != nullptr) << error; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1956 | ArtField* java_lang_Thread_name_field = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1957 | soa.DecodeField(WellKnownClasses::java_lang_Thread_name); |
| 1958 | mirror::String* s = |
| 1959 | reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1960 | if (s != nullptr) { |
| 1961 | *name = s->ToModifiedUtf8(); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1962 | } |
| 1963 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1966 | JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1967 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1968 | JDWP::JdwpError error; |
| 1969 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 1970 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1971 | return JDWP::ERR_INVALID_OBJECT; |
| 1972 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1973 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroup"); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1974 | // Okay, so it's an object, but is it actually a thread? |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 1975 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 1976 | UNUSED(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1977 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1978 | // Zombie threads are in the null group. |
| 1979 | expandBufAddObjectId(pReply, JDWP::ObjectId(0)); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1980 | error = JDWP::ERR_NONE; |
| 1981 | } else if (error == JDWP::ERR_NONE) { |
| 1982 | mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
| 1983 | CHECK(c != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1984 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 1985 | CHECK(f != nullptr); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1986 | mirror::Object* group = f->GetObject(thread_object); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 1987 | CHECK(group != nullptr); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1988 | JDWP::ObjectId thread_group_id = gRegistry->Add(group); |
| 1989 | expandBufAddObjectId(pReply, thread_group_id); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1990 | } |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1991 | return error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1992 | } |
| 1993 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1994 | static mirror::Object* DecodeThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 1995 | JDWP::ObjectId thread_group_id, JDWP::JdwpError* error) |
| 1996 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1997 | mirror::Object* thread_group = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_group_id, |
| 1998 | error); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1999 | if (*error != JDWP::ERR_NONE) { |
| 2000 | return nullptr; |
| 2001 | } |
| 2002 | if (thread_group == nullptr) { |
| 2003 | *error = JDWP::ERR_INVALID_OBJECT; |
| 2004 | return nullptr; |
| 2005 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2006 | mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup); |
| 2007 | CHECK(c != nullptr); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2008 | if (!c->IsAssignableFrom(thread_group->GetClass())) { |
| 2009 | // This is not a java.lang.ThreadGroup. |
| 2010 | *error = JDWP::ERR_INVALID_THREAD_GROUP; |
| 2011 | return nullptr; |
| 2012 | } |
| 2013 | *error = JDWP::ERR_NONE; |
| 2014 | return thread_group; |
| 2015 | } |
| 2016 | |
| 2017 | JDWP::JdwpError Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
| 2018 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 2019 | JDWP::JdwpError error; |
| 2020 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2021 | if (error != JDWP::ERR_NONE) { |
| 2022 | return error; |
| 2023 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2024 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupName"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2025 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2026 | CHECK(f != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2027 | mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group)); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2028 | |
| 2029 | std::string thread_group_name(s->ToModifiedUtf8()); |
| 2030 | expandBufAddUtf8String(pReply, thread_group_name); |
| 2031 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2034 | JDWP::JdwpError Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2035 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2036 | JDWP::JdwpError error; |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2037 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2038 | if (error != JDWP::ERR_NONE) { |
| 2039 | return error; |
| 2040 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2041 | mirror::Object* parent; |
| 2042 | { |
| 2043 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupParent"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2044 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_parent); |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2045 | CHECK(f != nullptr); |
| 2046 | parent = f->GetObject(thread_group); |
| 2047 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2048 | JDWP::ObjectId parent_group_id = gRegistry->Add(parent); |
| 2049 | expandBufAddObjectId(pReply, parent_group_id); |
| 2050 | return JDWP::ERR_NONE; |
| 2051 | } |
| 2052 | |
| 2053 | static void GetChildThreadGroups(ScopedObjectAccessUnchecked& soa, mirror::Object* thread_group, |
| 2054 | std::vector<JDWP::ObjectId>* child_thread_group_ids) |
| 2055 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2056 | CHECK(thread_group != nullptr); |
| 2057 | |
| 2058 | // Get the ArrayList<ThreadGroup> "groups" out of this thread group... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2059 | ArtField* groups_field = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_groups); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2060 | mirror::Object* groups_array_list = groups_field->GetObject(thread_group); |
Sebastien Hertz | e49e195 | 2014-10-13 11:27:13 +0200 | [diff] [blame] | 2061 | { |
| 2062 | // The "groups" field is declared as a java.util.List: check it really is |
| 2063 | // an instance of java.util.ArrayList. |
| 2064 | CHECK(groups_array_list != nullptr); |
| 2065 | mirror::Class* java_util_ArrayList_class = |
| 2066 | soa.Decode<mirror::Class*>(WellKnownClasses::java_util_ArrayList); |
| 2067 | CHECK(groups_array_list->InstanceOf(java_util_ArrayList_class)); |
| 2068 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2069 | |
| 2070 | // Get the array and size out of the ArrayList<ThreadGroup>... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2071 | ArtField* array_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_array); |
| 2072 | ArtField* size_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_size); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2073 | mirror::ObjectArray<mirror::Object>* groups_array = |
| 2074 | array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>(); |
| 2075 | const int32_t size = size_field->GetInt(groups_array_list); |
| 2076 | |
| 2077 | // Copy the first 'size' elements out of the array into the result. |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2078 | ObjectRegistry* registry = Dbg::GetObjectRegistry(); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2079 | for (int32_t i = 0; i < size; ++i) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2080 | child_thread_group_ids->push_back(registry->Add(groups_array->Get(i))); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | JDWP::JdwpError Dbg::GetThreadGroupChildren(JDWP::ObjectId thread_group_id, |
| 2085 | JDWP::ExpandBuf* pReply) { |
| 2086 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 2087 | JDWP::JdwpError error; |
| 2088 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2089 | if (error != JDWP::ERR_NONE) { |
| 2090 | return error; |
| 2091 | } |
| 2092 | |
| 2093 | // Add child threads. |
| 2094 | { |
| 2095 | std::vector<JDWP::ObjectId> child_thread_ids; |
| 2096 | GetThreads(thread_group, &child_thread_ids); |
| 2097 | expandBufAdd4BE(pReply, child_thread_ids.size()); |
| 2098 | for (JDWP::ObjectId child_thread_id : child_thread_ids) { |
| 2099 | expandBufAddObjectId(pReply, child_thread_id); |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | // Add child thread groups. |
| 2104 | { |
| 2105 | std::vector<JDWP::ObjectId> child_thread_groups_ids; |
| 2106 | GetChildThreadGroups(soa, thread_group, &child_thread_groups_ids); |
| 2107 | expandBufAdd4BE(pReply, child_thread_groups_ids.size()); |
| 2108 | for (JDWP::ObjectId child_thread_group_id : child_thread_groups_ids) { |
| 2109 | expandBufAddObjectId(pReply, child_thread_group_id); |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2117 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2118 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2119 | mirror::Object* group = f->GetObject(f->GetDeclaringClass()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2120 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2123 | JDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) { |
| 2124 | switch (state) { |
| 2125 | case kBlocked: |
| 2126 | return JDWP::TS_MONITOR; |
| 2127 | case kNative: |
| 2128 | case kRunnable: |
| 2129 | case kSuspended: |
| 2130 | return JDWP::TS_RUNNING; |
| 2131 | case kSleeping: |
| 2132 | return JDWP::TS_SLEEPING; |
| 2133 | case kStarting: |
| 2134 | case kTerminated: |
| 2135 | return JDWP::TS_ZOMBIE; |
| 2136 | case kTimedWaiting: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2137 | case kWaitingForCheckPointsToRun: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2138 | case kWaitingForDebuggerSend: |
| 2139 | case kWaitingForDebuggerSuspension: |
| 2140 | case kWaitingForDebuggerToAttach: |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 2141 | case kWaitingForDeoptimization: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2142 | case kWaitingForGcToComplete: |
Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 2143 | case kWaitingForGetObjectsAllocated: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2144 | case kWaitingForJniOnLoad: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2145 | case kWaitingForMethodTracingStart: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2146 | case kWaitingForSignalCatcherOutput: |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 2147 | case kWaitingForVisitObjects: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2148 | case kWaitingInMainDebuggerLoop: |
| 2149 | case kWaitingInMainSignalCatcherLoop: |
| 2150 | case kWaitingPerformingGc: |
| 2151 | case kWaiting: |
| 2152 | return JDWP::TS_WAIT; |
| 2153 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 2154 | } |
| 2155 | LOG(FATAL) << "Unknown thread state: " << state; |
| 2156 | return JDWP::TS_ZOMBIE; |
| 2157 | } |
| 2158 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2159 | JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, |
| 2160 | JDWP::JdwpSuspendStatus* pSuspendStatus) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2161 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2162 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2163 | *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED; |
| 2164 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2165 | JDWP::JdwpError error; |
| 2166 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2167 | if (error != JDWP::ERR_NONE) { |
| 2168 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2169 | *pThreadStatus = JDWP::TS_ZOMBIE; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2170 | return JDWP::ERR_NONE; |
| 2171 | } |
| 2172 | return error; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2173 | } |
| 2174 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2175 | if (IsSuspendedForDebugger(soa, thread)) { |
| 2176 | *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2177 | } |
| 2178 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2179 | *pThreadStatus = ToJdwpThreadStatus(thread->GetState()); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2180 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2181 | } |
| 2182 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2183 | JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2184 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2185 | JDWP::JdwpError error; |
| 2186 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2187 | if (error != JDWP::ERR_NONE) { |
| 2188 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2189 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2190 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2191 | expandBufAdd4BE(pReply, thread->GetDebugSuspendCount()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2192 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2195 | JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) { |
| 2196 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2197 | JDWP::JdwpError error; |
| 2198 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2199 | if (error != JDWP::ERR_NONE) { |
| 2200 | return error; |
| 2201 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 2202 | thread->Interrupt(soa.Self()); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2203 | return JDWP::ERR_NONE; |
| 2204 | } |
| 2205 | |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2206 | static bool IsInDesiredThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 2207 | mirror::Object* desired_thread_group, mirror::Object* peer) |
| 2208 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2209 | // Do we want threads from all thread groups? |
| 2210 | if (desired_thread_group == nullptr) { |
| 2211 | return true; |
| 2212 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2213 | ArtField* thread_group_field = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2214 | DCHECK(thread_group_field != nullptr); |
| 2215 | mirror::Object* group = thread_group_field->GetObject(peer); |
| 2216 | return (group == desired_thread_group); |
| 2217 | } |
| 2218 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2219 | void Dbg::GetThreads(mirror::Object* thread_group, std::vector<JDWP::ObjectId>* thread_ids) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2220 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2221 | std::list<Thread*> all_threads_list; |
| 2222 | { |
| 2223 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 2224 | all_threads_list = Runtime::Current()->GetThreadList()->GetList(); |
| 2225 | } |
| 2226 | for (Thread* t : all_threads_list) { |
| 2227 | if (t == Dbg::GetDebugThread()) { |
| 2228 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 2229 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 2230 | continue; |
| 2231 | } |
| 2232 | if (t->IsStillStarting()) { |
| 2233 | // This thread is being started (and has been registered in the thread list). However, it is |
| 2234 | // not completely started yet so we must ignore it. |
| 2235 | continue; |
| 2236 | } |
| 2237 | mirror::Object* peer = t->GetPeer(); |
| 2238 | if (peer == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2239 | // peer might be null if the thread is still starting up. We can't tell the debugger about |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2240 | // this thread yet. |
| 2241 | // TODO: if we identified threads to the debugger by their Thread* |
| 2242 | // rather than their peer's mirror::Object*, we could fix this. |
| 2243 | // Doing so might help us report ZOMBIE threads too. |
| 2244 | continue; |
| 2245 | } |
| 2246 | if (IsInDesiredThreadGroup(soa, thread_group, peer)) { |
| 2247 | thread_ids->push_back(gRegistry->Add(peer)); |
| 2248 | } |
| 2249 | } |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2250 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2251 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2252 | static int GetStackDepth(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2253 | struct CountStackDepthVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2254 | explicit CountStackDepthVisitor(Thread* thread_in) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2255 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2256 | depth(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2257 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2258 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2259 | // annotalysis. |
| 2260 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2261 | if (!GetMethod()->IsRuntimeMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 2262 | ++depth; |
| 2263 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2264 | return true; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2265 | } |
| 2266 | size_t depth; |
| 2267 | }; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2268 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2269 | CountStackDepthVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2270 | visitor.WalkStack(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2271 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2272 | } |
| 2273 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2274 | JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t* result) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2275 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2276 | JDWP::JdwpError error; |
| 2277 | *result = 0; |
| 2278 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2279 | if (error != JDWP::ERR_NONE) { |
| 2280 | return error; |
| 2281 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2282 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2283 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2284 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2285 | *result = GetStackDepth(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2286 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2287 | } |
| 2288 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 2289 | JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame, |
| 2290 | size_t frame_count, JDWP::ExpandBuf* buf) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2291 | class GetFrameVisitor : public StackVisitor { |
| 2292 | public: |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2293 | GetFrameVisitor(Thread* thread, size_t start_frame_in, size_t frame_count_in, |
| 2294 | JDWP::ExpandBuf* buf_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2295 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2296 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2297 | depth_(0), |
| 2298 | start_frame_(start_frame_in), |
| 2299 | frame_count_(frame_count_in), |
| 2300 | buf_(buf_in) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2301 | expandBufAdd4BE(buf_, frame_count_); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2302 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2303 | |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2304 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2305 | if (GetMethod()->IsRuntimeMethod()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2306 | 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] | 2307 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2308 | if (depth_ >= start_frame_ + frame_count_) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2309 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2310 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2311 | if (depth_ >= start_frame_) { |
| 2312 | JDWP::FrameId frame_id(GetFrameId()); |
| 2313 | JDWP::JdwpLocation location; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2314 | SetJdwpLocation(&location, GetMethod(), GetDexPc()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2315 | VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2316 | expandBufAdd8BE(buf_, frame_id); |
| 2317 | expandBufAddLocation(buf_, location); |
| 2318 | } |
| 2319 | ++depth_; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2320 | return true; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2321 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2322 | |
| 2323 | private: |
| 2324 | size_t depth_; |
| 2325 | const size_t start_frame_; |
| 2326 | const size_t frame_count_; |
| 2327 | JDWP::ExpandBuf* buf_; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2328 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2329 | |
| 2330 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2331 | JDWP::JdwpError error; |
| 2332 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2333 | if (error != JDWP::ERR_NONE) { |
| 2334 | return error; |
| 2335 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2336 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2337 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2338 | } |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2339 | GetFrameVisitor visitor(thread, start_frame, frame_count, buf); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2340 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2341 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2345 | return GetThreadId(Thread::Current()); |
| 2346 | } |
| 2347 | |
| 2348 | JDWP::ObjectId Dbg::GetThreadId(Thread* thread) { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 2349 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2350 | return gRegistry->Add(thread->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2353 | void Dbg::SuspendVM() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2354 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2355 | } |
| 2356 | |
| 2357 | void Dbg::ResumeVM() { |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 2358 | Runtime::Current()->GetThreadList()->ResumeAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2361 | JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2362 | Thread* self = Thread::Current(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2363 | ScopedLocalRef<jobject> peer(self->GetJniEnv(), nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2364 | { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2365 | ScopedObjectAccess soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2366 | JDWP::JdwpError error; |
| 2367 | peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id, &error))); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2368 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2369 | if (peer.get() == nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2370 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2371 | } |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 2372 | // Suspend thread to build stack trace. |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2373 | bool timed_out; |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 2374 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 2375 | Thread* thread = thread_list->SuspendThreadByPeer(peer.get(), request_suspension, true, |
| 2376 | &timed_out); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2377 | if (thread != nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2378 | return JDWP::ERR_NONE; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2379 | } else if (timed_out) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2380 | return JDWP::ERR_INTERNAL; |
| 2381 | } else { |
| 2382 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2383 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2384 | } |
| 2385 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2386 | void Dbg::ResumeThread(JDWP::ObjectId thread_id) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2387 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2388 | JDWP::JdwpError error; |
| 2389 | mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2390 | CHECK(peer != nullptr) << error; |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2391 | Thread* thread; |
| 2392 | { |
| 2393 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 2394 | thread = Thread::FromManagedThread(soa, peer); |
| 2395 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2396 | if (thread == nullptr) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2397 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 2398 | return; |
| 2399 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2400 | bool needs_resume; |
| 2401 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2402 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2403 | needs_resume = thread->GetSuspendCount() > 0; |
| 2404 | } |
| 2405 | if (needs_resume) { |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 2406 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
| 2407 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2411 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2412 | } |
| 2413 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2414 | struct GetThisVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2415 | GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2416 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2417 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2418 | this_object(nullptr), |
| 2419 | frame_id(frame_id_in) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2420 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2421 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2422 | // annotalysis. |
| 2423 | virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2424 | if (frame_id != GetFrameId()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2425 | return true; // continue |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2426 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2427 | this_object = GetThisObject(); |
| 2428 | return false; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2429 | } |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 2430 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2431 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2432 | mirror::Object* this_object; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2433 | JDWP::FrameId frame_id; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2434 | }; |
| 2435 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2436 | JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, |
| 2437 | JDWP::ObjectId* result) { |
| 2438 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2439 | JDWP::JdwpError error; |
| 2440 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2441 | if (error != JDWP::ERR_NONE) { |
| 2442 | return error; |
| 2443 | } |
| 2444 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2445 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2446 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2447 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2448 | GetThisVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2449 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2450 | *result = gRegistry->Add(visitor.this_object); |
| 2451 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2452 | } |
| 2453 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2454 | // Walks the stack until we find the frame with the given FrameId. |
| 2455 | class FindFrameVisitor FINAL : public StackVisitor { |
| 2456 | public: |
| 2457 | FindFrameVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id) |
| 2458 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2459 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2460 | frame_id_(frame_id), |
| 2461 | error_(JDWP::ERR_INVALID_FRAMEID) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2462 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2463 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2464 | // annotalysis. |
| 2465 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 2466 | if (GetFrameId() != frame_id_) { |
| 2467 | return true; // Not our frame, carry on. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2468 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2469 | ArtMethod* m = GetMethod(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2470 | if (m->IsNative()) { |
| 2471 | // We can't read/write local value from/into native method. |
| 2472 | error_ = JDWP::ERR_OPAQUE_FRAME; |
| 2473 | } else { |
| 2474 | // We found our frame. |
| 2475 | error_ = JDWP::ERR_NONE; |
| 2476 | } |
| 2477 | return false; |
| 2478 | } |
| 2479 | |
| 2480 | JDWP::JdwpError GetError() const { |
| 2481 | return error_; |
| 2482 | } |
| 2483 | |
| 2484 | private: |
| 2485 | const JDWP::FrameId frame_id_; |
| 2486 | JDWP::JdwpError error_; |
| 2487 | }; |
| 2488 | |
| 2489 | JDWP::JdwpError Dbg::GetLocalValues(JDWP::Request* request, JDWP::ExpandBuf* pReply) { |
| 2490 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2491 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2492 | |
| 2493 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2494 | JDWP::JdwpError error; |
| 2495 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2496 | if (error != JDWP::ERR_NONE) { |
| 2497 | return error; |
| 2498 | } |
| 2499 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2500 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2501 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2502 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2503 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2504 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2505 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2506 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2507 | return visitor.GetError(); |
| 2508 | } |
| 2509 | |
| 2510 | // Read the values from visitor's context. |
| 2511 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2512 | expandBufAdd4BE(pReply, slot_count); /* "int values" */ |
| 2513 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2514 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2515 | JDWP::JdwpTag reqSigByte = request->ReadTag(); |
| 2516 | |
| 2517 | VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte; |
| 2518 | |
| 2519 | size_t width = Dbg::GetTagWidth(reqSigByte); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 2520 | uint8_t* ptr = expandBufAddSpace(pReply, width + 1); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2521 | error = Dbg::GetLocalValue(visitor, soa, slot, reqSigByte, ptr, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2522 | if (error != JDWP::ERR_NONE) { |
| 2523 | return error; |
| 2524 | } |
| 2525 | } |
| 2526 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2527 | } |
| 2528 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2529 | constexpr JDWP::JdwpError kStackFrameLocalAccessError = JDWP::ERR_ABSENT_INFORMATION; |
| 2530 | |
| 2531 | static std::string GetStackContextAsString(const StackVisitor& visitor) |
| 2532 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2533 | return StringPrintf(" at DEX pc 0x%08x in method %s", visitor.GetDexPc(false), |
| 2534 | PrettyMethod(visitor.GetMethod()).c_str()); |
| 2535 | } |
| 2536 | |
| 2537 | static JDWP::JdwpError FailGetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2538 | JDWP::JdwpTag tag) |
| 2539 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2540 | LOG(ERROR) << "Failed to read " << tag << " local from register v" << vreg |
| 2541 | << GetStackContextAsString(visitor); |
| 2542 | return kStackFrameLocalAccessError; |
| 2543 | } |
| 2544 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2545 | JDWP::JdwpError Dbg::GetLocalValue(const StackVisitor& visitor, ScopedObjectAccessUnchecked& soa, |
| 2546 | int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2547 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2548 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2549 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2550 | if (error != JDWP::ERR_NONE) { |
| 2551 | return error; |
| 2552 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2553 | // TODO: check that the tag is compatible with the actual type of the slot! |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2554 | switch (tag) { |
| 2555 | case JDWP::JT_BOOLEAN: { |
| 2556 | CHECK_EQ(width, 1U); |
| 2557 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2558 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2559 | return FailGetLocalValue(visitor, vreg, tag); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2560 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2561 | VLOG(jdwp) << "get boolean local " << vreg << " = " << intVal; |
| 2562 | JDWP::Set1(buf + 1, intVal != 0); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2563 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2564 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2565 | case JDWP::JT_BYTE: { |
| 2566 | CHECK_EQ(width, 1U); |
| 2567 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2568 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2569 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2570 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2571 | VLOG(jdwp) << "get byte local " << vreg << " = " << intVal; |
| 2572 | JDWP::Set1(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2573 | break; |
| 2574 | } |
| 2575 | case JDWP::JT_SHORT: |
| 2576 | case JDWP::JT_CHAR: { |
| 2577 | CHECK_EQ(width, 2U); |
| 2578 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2579 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2580 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2581 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2582 | VLOG(jdwp) << "get short/char local " << vreg << " = " << intVal; |
| 2583 | JDWP::Set2BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2584 | break; |
| 2585 | } |
| 2586 | case JDWP::JT_INT: { |
| 2587 | CHECK_EQ(width, 4U); |
| 2588 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2589 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2590 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2591 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2592 | VLOG(jdwp) << "get int local " << vreg << " = " << intVal; |
| 2593 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2594 | break; |
| 2595 | } |
| 2596 | case JDWP::JT_FLOAT: { |
| 2597 | CHECK_EQ(width, 4U); |
| 2598 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2599 | if (!visitor.GetVReg(m, vreg, kFloatVReg, &intVal)) { |
| 2600 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2601 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2602 | VLOG(jdwp) << "get float local " << vreg << " = " << intVal; |
| 2603 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2604 | break; |
| 2605 | } |
| 2606 | case JDWP::JT_ARRAY: |
| 2607 | case JDWP::JT_CLASS_LOADER: |
| 2608 | case JDWP::JT_CLASS_OBJECT: |
| 2609 | case JDWP::JT_OBJECT: |
| 2610 | case JDWP::JT_STRING: |
| 2611 | case JDWP::JT_THREAD: |
| 2612 | case JDWP::JT_THREAD_GROUP: { |
| 2613 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
| 2614 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2615 | if (!visitor.GetVReg(m, vreg, kReferenceVReg, &intVal)) { |
| 2616 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2617 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2618 | mirror::Object* o = reinterpret_cast<mirror::Object*>(intVal); |
| 2619 | VLOG(jdwp) << "get " << tag << " object local " << vreg << " = " << o; |
| 2620 | if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) { |
| 2621 | LOG(FATAL) << StringPrintf("Found invalid object %#" PRIxPTR " in register v%u", |
| 2622 | reinterpret_cast<uintptr_t>(o), vreg) |
| 2623 | << GetStackContextAsString(visitor); |
| 2624 | UNREACHABLE(); |
| 2625 | } |
| 2626 | tag = TagFromObject(soa, o); |
| 2627 | JDWP::SetObjectId(buf + 1, gRegistry->Add(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2628 | break; |
| 2629 | } |
| 2630 | case JDWP::JT_DOUBLE: { |
| 2631 | CHECK_EQ(width, 8U); |
| 2632 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2633 | if (!visitor.GetVRegPair(m, vreg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) { |
| 2634 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2635 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2636 | VLOG(jdwp) << "get double local " << vreg << " = " << longVal; |
| 2637 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2638 | break; |
| 2639 | } |
| 2640 | case JDWP::JT_LONG: { |
| 2641 | CHECK_EQ(width, 8U); |
| 2642 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2643 | if (!visitor.GetVRegPair(m, vreg, kLongLoVReg, kLongHiVReg, &longVal)) { |
| 2644 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2645 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2646 | VLOG(jdwp) << "get long local " << vreg << " = " << longVal; |
| 2647 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2648 | break; |
| 2649 | } |
| 2650 | default: |
| 2651 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2652 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2653 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2654 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2655 | // Prepend tag, which may have been updated. |
| 2656 | JDWP::Set1(buf, tag); |
| 2657 | return JDWP::ERR_NONE; |
| 2658 | } |
| 2659 | |
| 2660 | JDWP::JdwpError Dbg::SetLocalValues(JDWP::Request* request) { |
| 2661 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2662 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2663 | |
| 2664 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2665 | JDWP::JdwpError error; |
| 2666 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2667 | if (error != JDWP::ERR_NONE) { |
| 2668 | return error; |
| 2669 | } |
| 2670 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2671 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2672 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2673 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2674 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2675 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2676 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2677 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2678 | return visitor.GetError(); |
| 2679 | } |
| 2680 | |
| 2681 | // Writes the values into visitor's context. |
| 2682 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2683 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2684 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2685 | JDWP::JdwpTag sigByte = request->ReadTag(); |
| 2686 | size_t width = Dbg::GetTagWidth(sigByte); |
| 2687 | uint64_t value = request->ReadValue(width); |
| 2688 | |
| 2689 | VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value; |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2690 | error = Dbg::SetLocalValue(visitor, slot, sigByte, value, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2691 | if (error != JDWP::ERR_NONE) { |
| 2692 | return error; |
| 2693 | } |
| 2694 | } |
| 2695 | return JDWP::ERR_NONE; |
| 2696 | } |
| 2697 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2698 | template<typename T> |
| 2699 | static JDWP::JdwpError FailSetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2700 | JDWP::JdwpTag tag, T value) |
| 2701 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2702 | LOG(ERROR) << "Failed to write " << tag << " local " << value |
| 2703 | << " (0x" << std::hex << value << ") into register v" << vreg |
| 2704 | << GetStackContextAsString(visitor); |
| 2705 | return kStackFrameLocalAccessError; |
| 2706 | } |
| 2707 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2708 | JDWP::JdwpError Dbg::SetLocalValue(StackVisitor& visitor, int slot, JDWP::JdwpTag tag, |
| 2709 | uint64_t value, size_t width) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2710 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2711 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2712 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2713 | if (error != JDWP::ERR_NONE) { |
| 2714 | return error; |
| 2715 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2716 | // TODO: check that the tag is compatible with the actual type of the slot! |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2717 | switch (tag) { |
| 2718 | case JDWP::JT_BOOLEAN: |
| 2719 | case JDWP::JT_BYTE: |
| 2720 | CHECK_EQ(width, 1U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2721 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2722 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2723 | } |
| 2724 | break; |
| 2725 | case JDWP::JT_SHORT: |
| 2726 | case JDWP::JT_CHAR: |
| 2727 | CHECK_EQ(width, 2U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2728 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2729 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2730 | } |
| 2731 | break; |
| 2732 | case JDWP::JT_INT: |
| 2733 | CHECK_EQ(width, 4U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2734 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2735 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2736 | } |
| 2737 | break; |
| 2738 | case JDWP::JT_FLOAT: |
| 2739 | CHECK_EQ(width, 4U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2740 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kFloatVReg)) { |
| 2741 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2742 | } |
| 2743 | break; |
| 2744 | case JDWP::JT_ARRAY: |
| 2745 | case JDWP::JT_CLASS_LOADER: |
| 2746 | case JDWP::JT_CLASS_OBJECT: |
| 2747 | case JDWP::JT_OBJECT: |
| 2748 | case JDWP::JT_STRING: |
| 2749 | case JDWP::JT_THREAD: |
| 2750 | case JDWP::JT_THREAD_GROUP: { |
| 2751 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2752 | mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value), |
| 2753 | &error); |
| 2754 | if (error != JDWP::ERR_NONE) { |
| 2755 | VLOG(jdwp) << tag << " object " << o << " is an invalid object"; |
| 2756 | return JDWP::ERR_INVALID_OBJECT; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2757 | } |
| 2758 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), |
| 2759 | kReferenceVReg)) { |
| 2760 | return FailSetLocalValue(visitor, vreg, tag, reinterpret_cast<uintptr_t>(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2761 | } |
| 2762 | break; |
| 2763 | } |
| 2764 | case JDWP::JT_DOUBLE: { |
| 2765 | CHECK_EQ(width, 8U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2766 | if (!visitor.SetVRegPair(m, vreg, value, kDoubleLoVReg, kDoubleHiVReg)) { |
| 2767 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2768 | } |
| 2769 | break; |
| 2770 | } |
| 2771 | case JDWP::JT_LONG: { |
| 2772 | CHECK_EQ(width, 8U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2773 | if (!visitor.SetVRegPair(m, vreg, value, kLongLoVReg, kLongHiVReg)) { |
| 2774 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2775 | } |
| 2776 | break; |
| 2777 | } |
| 2778 | default: |
| 2779 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2780 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2781 | } |
| 2782 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2785 | static void SetEventLocation(JDWP::EventLocation* location, ArtMethod* m, uint32_t dex_pc) |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2786 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2787 | DCHECK(location != nullptr); |
| 2788 | if (m == nullptr) { |
| 2789 | memset(location, 0, sizeof(*location)); |
| 2790 | } else { |
| 2791 | location->method = m; |
| 2792 | location->dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint32_t>(-1) : dex_pc; |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2793 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2794 | } |
| 2795 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2796 | void Dbg::PostLocationEvent(ArtMethod* m, int dex_pc, mirror::Object* this_object, |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2797 | int event_flags, const JValue* return_value) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2798 | if (!IsDebuggerActive()) { |
| 2799 | return; |
| 2800 | } |
| 2801 | DCHECK(m != nullptr); |
| 2802 | DCHECK_EQ(m->IsStatic(), this_object == nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2803 | JDWP::EventLocation location; |
| 2804 | SetEventLocation(&location, m, dex_pc); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2805 | |
Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2806 | // We need to be sure no exception is pending when calling JdwpState::PostLocationEvent. |
| 2807 | // This is required to be able to call JNI functions to create JDWP ids. To achieve this, |
| 2808 | // we temporarily clear the current thread's exception (if any) and will restore it after |
| 2809 | // the call. |
| 2810 | // Note: the only way to get a pending exception here is to suspend on a move-exception |
| 2811 | // instruction. |
| 2812 | Thread* const self = Thread::Current(); |
| 2813 | StackHandleScope<1> hs(self); |
| 2814 | Handle<mirror::Throwable> pending_exception(hs.NewHandle(self->GetException())); |
| 2815 | self->ClearException(); |
| 2816 | if (kIsDebugBuild && pending_exception.Get() != nullptr) { |
| 2817 | const DexFile::CodeItem* code_item = location.method->GetCodeItem(); |
| 2818 | const Instruction* instr = Instruction::At(&code_item->insns_[location.dex_pc]); |
| 2819 | CHECK_EQ(Instruction::MOVE_EXCEPTION, instr->Opcode()); |
| 2820 | } |
| 2821 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2822 | gJdwpState->PostLocationEvent(&location, this_object, event_flags, return_value); |
Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2823 | |
| 2824 | if (pending_exception.Get() != nullptr) { |
| 2825 | self->SetException(pending_exception.Get()); |
| 2826 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2827 | } |
| 2828 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2829 | void Dbg::PostFieldAccessEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2830 | mirror::Object* this_object, ArtField* f) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2831 | if (!IsDebuggerActive()) { |
| 2832 | return; |
| 2833 | } |
| 2834 | DCHECK(m != nullptr); |
| 2835 | DCHECK(f != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2836 | JDWP::EventLocation location; |
| 2837 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2838 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2839 | gJdwpState->PostFieldEvent(&location, f, this_object, nullptr, false); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2840 | } |
| 2841 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2842 | void Dbg::PostFieldModificationEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2843 | mirror::Object* this_object, ArtField* f, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2844 | const JValue* field_value) { |
| 2845 | if (!IsDebuggerActive()) { |
| 2846 | return; |
| 2847 | } |
| 2848 | DCHECK(m != nullptr); |
| 2849 | DCHECK(f != nullptr); |
| 2850 | DCHECK(field_value != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2851 | JDWP::EventLocation location; |
| 2852 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2853 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2854 | gJdwpState->PostFieldEvent(&location, f, this_object, field_value, true); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2855 | } |
| 2856 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2857 | /** |
| 2858 | * Finds the location where this exception will be caught. We search until we reach the top |
| 2859 | * frame, in which case this exception is considered uncaught. |
| 2860 | */ |
| 2861 | class CatchLocationFinder : public StackVisitor { |
| 2862 | public: |
| 2863 | CatchLocationFinder(Thread* self, const Handle<mirror::Throwable>& exception, Context* context) |
| 2864 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2865 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2866 | self_(self), |
| 2867 | exception_(exception), |
| 2868 | handle_scope_(self), |
| 2869 | this_at_throw_(handle_scope_.NewHandle<mirror::Object>(nullptr)), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2870 | catch_method_(nullptr), |
| 2871 | throw_method_(nullptr), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2872 | catch_dex_pc_(DexFile::kDexNoIndex), |
| 2873 | throw_dex_pc_(DexFile::kDexNoIndex) { |
| 2874 | } |
| 2875 | |
| 2876 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2877 | ArtMethod* method = GetMethod(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2878 | DCHECK(method != nullptr); |
| 2879 | if (method->IsRuntimeMethod()) { |
| 2880 | // Ignore callee save method. |
| 2881 | DCHECK(method->IsCalleeSaveMethod()); |
| 2882 | return true; |
| 2883 | } |
| 2884 | |
| 2885 | uint32_t dex_pc = GetDexPc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2886 | if (throw_method_ == nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2887 | // First Java method found. It is either the method that threw the exception, |
| 2888 | // or the Java native method that is reporting an exception thrown by |
| 2889 | // native code. |
| 2890 | this_at_throw_.Assign(GetThisObject()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2891 | throw_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2892 | throw_dex_pc_ = dex_pc; |
| 2893 | } |
| 2894 | |
| 2895 | if (dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2896 | StackHandleScope<1> hs(self_); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2897 | uint32_t found_dex_pc; |
| 2898 | Handle<mirror::Class> exception_class(hs.NewHandle(exception_->GetClass())); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2899 | bool unused_clear_exception; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2900 | found_dex_pc = method->FindCatchBlock(exception_class, dex_pc, &unused_clear_exception); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2901 | if (found_dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2902 | catch_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2903 | catch_dex_pc_ = found_dex_pc; |
| 2904 | return false; // End stack walk. |
| 2905 | } |
| 2906 | } |
| 2907 | return true; // Continue stack walk. |
| 2908 | } |
| 2909 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2910 | ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2911 | return catch_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2914 | ArtMethod* GetThrowMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2915 | return throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | mirror::Object* GetThisAtThrow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2919 | return this_at_throw_.Get(); |
| 2920 | } |
| 2921 | |
| 2922 | uint32_t GetCatchDexPc() const { |
| 2923 | return catch_dex_pc_; |
| 2924 | } |
| 2925 | |
| 2926 | uint32_t GetThrowDexPc() const { |
| 2927 | return throw_dex_pc_; |
| 2928 | } |
| 2929 | |
| 2930 | private: |
| 2931 | Thread* const self_; |
| 2932 | const Handle<mirror::Throwable>& exception_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2933 | StackHandleScope<1> handle_scope_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2934 | MutableHandle<mirror::Object> this_at_throw_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2935 | ArtMethod* catch_method_; |
| 2936 | ArtMethod* throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2937 | uint32_t catch_dex_pc_; |
| 2938 | uint32_t throw_dex_pc_; |
| 2939 | |
| 2940 | DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder); |
| 2941 | }; |
| 2942 | |
| 2943 | void Dbg::PostException(mirror::Throwable* exception_object) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2944 | if (!IsDebuggerActive()) { |
Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 2945 | return; |
| 2946 | } |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 2947 | Thread* const self = Thread::Current(); |
| 2948 | StackHandleScope<1> handle_scope(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2949 | Handle<mirror::Throwable> h_exception(handle_scope.NewHandle(exception_object)); |
| 2950 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 2951 | CatchLocationFinder clf(self, h_exception, context.get()); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2952 | clf.WalkStack(/* include_transitions */ false); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2953 | JDWP::EventLocation exception_throw_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2954 | SetEventLocation(&exception_throw_location, clf.GetThrowMethod(), clf.GetThrowDexPc()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2955 | JDWP::EventLocation exception_catch_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2956 | SetEventLocation(&exception_catch_location, clf.GetCatchMethod(), clf.GetCatchDexPc()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2957 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2958 | gJdwpState->PostException(&exception_throw_location, h_exception.Get(), &exception_catch_location, |
| 2959 | clf.GetThisAtThrow()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2960 | } |
| 2961 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2962 | void Dbg::PostClassPrepare(mirror::Class* c) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2963 | if (!IsDebuggerActive()) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2964 | return; |
| 2965 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2966 | gJdwpState->PostClassPrepare(c); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2967 | } |
| 2968 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2969 | void Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2970 | ArtMethod* m, uint32_t dex_pc, |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 2971 | int event_flags, const JValue* return_value) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2972 | if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 2973 | return; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2974 | } |
| 2975 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2976 | if (IsBreakpoint(m, dex_pc)) { |
| 2977 | event_flags |= kBreakpoint; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2978 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2979 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2980 | // If the debugger is single-stepping one of our threads, check to |
| 2981 | // see if we're that thread and we've reached a step point. |
| 2982 | const SingleStepControl* single_step_control = thread->GetSingleStepControl(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2983 | if (single_step_control != nullptr) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2984 | CHECK(!m->IsNative()); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2985 | if (single_step_control->GetStepDepth() == JDWP::SD_INTO) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2986 | // Step into method calls. We break when the line number |
| 2987 | // or method pointer changes. If we're in SS_MIN mode, we |
| 2988 | // always stop. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2989 | if (single_step_control->GetMethod() != m) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2990 | event_flags |= kSingleStep; |
| 2991 | VLOG(jdwp) << "SS new method"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2992 | } else if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2993 | event_flags |= kSingleStep; |
| 2994 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 2995 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2996 | event_flags |= kSingleStep; |
| 2997 | VLOG(jdwp) << "SS new line"; |
| 2998 | } |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2999 | } else if (single_step_control->GetStepDepth() == JDWP::SD_OVER) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3000 | // Step over method calls. We break when the line number is |
| 3001 | // different and the frame depth is <= the original frame |
| 3002 | // depth. (We can't just compare on the method, because we |
| 3003 | // might get unrolled past it by an exception, and it's tricky |
| 3004 | // to identify recursion.) |
| 3005 | |
| 3006 | int stack_depth = GetStackDepth(thread); |
| 3007 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3008 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3009 | // Popped up one or more frames, always trigger. |
| 3010 | event_flags |= kSingleStep; |
| 3011 | VLOG(jdwp) << "SS method pop"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3012 | } else if (stack_depth == single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3013 | // Same depth, see if we moved. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3014 | if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3015 | event_flags |= kSingleStep; |
| 3016 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3017 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3018 | event_flags |= kSingleStep; |
| 3019 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3020 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3021 | } |
| 3022 | } else { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3023 | CHECK_EQ(single_step_control->GetStepDepth(), JDWP::SD_OUT); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3024 | // Return from the current method. We break when the frame |
| 3025 | // depth pops up. |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3026 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3027 | // This differs from the "method exit" break in that it stops |
| 3028 | // with the PC at the next instruction in the returned-to |
| 3029 | // function, rather than the end of the returning function. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3030 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3031 | int stack_depth = GetStackDepth(thread); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3032 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3033 | event_flags |= kSingleStep; |
| 3034 | VLOG(jdwp) << "SS method pop"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3035 | } |
| 3036 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3037 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3038 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3039 | // If there's something interesting going on, see if it matches one |
| 3040 | // of the debugger filters. |
| 3041 | if (event_flags != 0) { |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 3042 | Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3043 | } |
| 3044 | } |
| 3045 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3046 | size_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) { |
| 3047 | switch (instrumentation_event) { |
| 3048 | case instrumentation::Instrumentation::kMethodEntered: |
| 3049 | return &method_enter_event_ref_count_; |
| 3050 | case instrumentation::Instrumentation::kMethodExited: |
| 3051 | return &method_exit_event_ref_count_; |
| 3052 | case instrumentation::Instrumentation::kDexPcMoved: |
| 3053 | return &dex_pc_change_event_ref_count_; |
| 3054 | case instrumentation::Instrumentation::kFieldRead: |
| 3055 | return &field_read_event_ref_count_; |
| 3056 | case instrumentation::Instrumentation::kFieldWritten: |
| 3057 | return &field_write_event_ref_count_; |
| 3058 | case instrumentation::Instrumentation::kExceptionCaught: |
| 3059 | return &exception_catch_event_ref_count_; |
| 3060 | default: |
| 3061 | return nullptr; |
| 3062 | } |
| 3063 | } |
| 3064 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3065 | // Process request while all mutator threads are suspended. |
| 3066 | void Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3067 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3068 | switch (request.GetKind()) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3069 | case DeoptimizationRequest::kNothing: |
| 3070 | LOG(WARNING) << "Ignoring empty deoptimization request."; |
| 3071 | break; |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3072 | case DeoptimizationRequest::kRegisterForEvent: |
| 3073 | VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3074 | request.InstrumentationEvent()); |
| 3075 | instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent()); |
| 3076 | instrumentation_events_ |= request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3077 | break; |
| 3078 | case DeoptimizationRequest::kUnregisterForEvent: |
| 3079 | VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3080 | request.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3081 | instrumentation->RemoveListener(&gDebugInstrumentationListener, |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3082 | request.InstrumentationEvent()); |
| 3083 | instrumentation_events_ &= ~request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3084 | break; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3085 | case DeoptimizationRequest::kFullDeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3086 | VLOG(jdwp) << "Deoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3087 | instrumentation->DeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3088 | VLOG(jdwp) << "Deoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3089 | break; |
| 3090 | case DeoptimizationRequest::kFullUndeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3091 | VLOG(jdwp) << "Undeoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3092 | instrumentation->UndeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3093 | VLOG(jdwp) << "Undeoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3094 | break; |
| 3095 | case DeoptimizationRequest::kSelectiveDeoptimization: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3096 | VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " ..."; |
| 3097 | instrumentation->Deoptimize(request.Method()); |
| 3098 | VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3099 | break; |
| 3100 | case DeoptimizationRequest::kSelectiveUndeoptimization: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3101 | VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " ..."; |
| 3102 | instrumentation->Undeoptimize(request.Method()); |
| 3103 | VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3104 | break; |
| 3105 | default: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3106 | LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3107 | break; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3108 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3109 | } |
| 3110 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3111 | void Dbg::RequestDeoptimization(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3112 | if (req.GetKind() == DeoptimizationRequest::kNothing) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3113 | // Nothing to do. |
| 3114 | return; |
| 3115 | } |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3116 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3117 | RequestDeoptimizationLocked(req); |
| 3118 | } |
| 3119 | |
| 3120 | void Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3121 | switch (req.GetKind()) { |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3122 | case DeoptimizationRequest::kRegisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3123 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3124 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3125 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3126 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3127 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3128 | VLOG(jdwp) << StringPrintf("Queue request #%zd to start listening to instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3129 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3130 | deoptimization_requests_.push_back(req); |
| 3131 | } |
| 3132 | *counter = *counter + 1; |
| 3133 | break; |
| 3134 | } |
| 3135 | case DeoptimizationRequest::kUnregisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3136 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3137 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3138 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3139 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3140 | *counter = *counter - 1; |
| 3141 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3142 | VLOG(jdwp) << StringPrintf("Queue request #%zd to stop listening to instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3143 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3144 | deoptimization_requests_.push_back(req); |
| 3145 | } |
| 3146 | break; |
| 3147 | } |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3148 | case DeoptimizationRequest::kFullDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3149 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3150 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3151 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3152 | << " for full deoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3153 | deoptimization_requests_.push_back(req); |
| 3154 | } |
| 3155 | ++full_deoptimization_event_count_; |
| 3156 | break; |
| 3157 | } |
| 3158 | case DeoptimizationRequest::kFullUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3159 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | e713d93 | 2014-05-15 10:48:53 +0200 | [diff] [blame] | 3160 | DCHECK_GT(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3161 | --full_deoptimization_event_count_; |
| 3162 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3163 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3164 | << " for full undeoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3165 | deoptimization_requests_.push_back(req); |
| 3166 | } |
| 3167 | break; |
| 3168 | } |
| 3169 | case DeoptimizationRequest::kSelectiveDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3170 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3171 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3172 | << " for deoptimization of " << PrettyMethod(req.Method()); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3173 | deoptimization_requests_.push_back(req); |
| 3174 | break; |
| 3175 | } |
| 3176 | case DeoptimizationRequest::kSelectiveUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3177 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3178 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3179 | << " for undeoptimization of " << PrettyMethod(req.Method()); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3180 | deoptimization_requests_.push_back(req); |
| 3181 | break; |
| 3182 | } |
| 3183 | default: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3184 | LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3185 | break; |
| 3186 | } |
| 3187 | } |
| 3188 | } |
| 3189 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3190 | void Dbg::ManageDeoptimization() { |
| 3191 | Thread* const self = Thread::Current(); |
| 3192 | { |
| 3193 | // Avoid suspend/resume if there is no pending request. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3194 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3195 | if (deoptimization_requests_.empty()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3196 | return; |
| 3197 | } |
| 3198 | } |
| 3199 | CHECK_EQ(self->GetState(), kRunnable); |
| 3200 | self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization); |
| 3201 | // We need to suspend mutator threads first. |
| 3202 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 3203 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3204 | const ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3205 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3206 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3207 | size_t req_index = 0; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3208 | for (DeoptimizationRequest& request : deoptimization_requests_) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3209 | VLOG(jdwp) << "Process deoptimization request #" << req_index++; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3210 | ProcessDeoptimizationRequest(request); |
| 3211 | } |
| 3212 | deoptimization_requests_.clear(); |
| 3213 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3214 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 3215 | runtime->GetThreadList()->ResumeAll(); |
| 3216 | self->TransitionFromSuspendedToRunnable(); |
| 3217 | } |
| 3218 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3219 | static bool IsMethodPossiblyInlined(Thread* self, ArtMethod* m) |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3220 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3221 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3222 | if (code_item == nullptr) { |
| 3223 | // TODO We should not be asked to watch location in a native or abstract method so the code item |
| 3224 | // should never be null. We could just check we never encounter this case. |
| 3225 | return false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3226 | } |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3227 | // Note: method verifier may cause thread suspension. |
| 3228 | self->AssertThreadSuspensionIsAllowable(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3229 | StackHandleScope<2> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3230 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 3231 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 3232 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3233 | verifier::MethodVerifier verifier(self, dex_cache->GetDexFile(), dex_cache, class_loader, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3234 | &m->GetClassDef(), code_item, m->GetDexMethodIndex(), m, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 3235 | m->GetAccessFlags(), false, true, false, true); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3236 | // Note: we don't need to verify the method. |
| 3237 | return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr); |
| 3238 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3239 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3240 | static const Breakpoint* FindFirstBreakpointForMethod(ArtMethod* m) |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3241 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3242 | for (Breakpoint& breakpoint : gBreakpoints) { |
| 3243 | if (breakpoint.Method() == m) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3244 | return &breakpoint; |
| 3245 | } |
| 3246 | } |
| 3247 | return nullptr; |
| 3248 | } |
| 3249 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3250 | bool Dbg::MethodHasAnyBreakpoints(ArtMethod* method) { |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 3251 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| 3252 | return FindFirstBreakpointForMethod(method) != nullptr; |
| 3253 | } |
| 3254 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3255 | // Sanity checks all existing breakpoints on the same method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3256 | static void SanityCheckExistingBreakpoints(ArtMethod* m, |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3257 | DeoptimizationRequest::Kind deoptimization_kind) |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3258 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3259 | for (const Breakpoint& breakpoint : gBreakpoints) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3260 | if (breakpoint.Method() == m) { |
| 3261 | CHECK_EQ(deoptimization_kind, breakpoint.GetDeoptimizationKind()); |
| 3262 | } |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3263 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3264 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 3265 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3266 | // We should have deoptimized everything but not "selectively" deoptimized this method. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3267 | CHECK(instrumentation->AreAllMethodsDeoptimized()); |
| 3268 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3269 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3270 | // We should have "selectively" deoptimized this method. |
| 3271 | // Note: while we have not deoptimized everything for this method, we may have done it for |
| 3272 | // another event. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3273 | CHECK(instrumentation->IsDeoptimized(m)); |
| 3274 | } else { |
| 3275 | // This method does not require deoptimization. |
| 3276 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3277 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3278 | } |
| 3279 | } |
| 3280 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3281 | // Returns the deoptimization kind required to set a breakpoint in a method. |
| 3282 | // If a breakpoint has already been set, we also return the first breakpoint |
| 3283 | // through the given 'existing_brkpt' pointer. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3284 | static DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3285 | ArtMethod* m, |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3286 | const Breakpoint** existing_brkpt) |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3287 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3288 | if (!Dbg::RequiresDeoptimization()) { |
| 3289 | // We already run in interpreter-only mode so we don't need to deoptimize anything. |
| 3290 | VLOG(jdwp) << "No need for deoptimization when fully running with interpreter for method " |
| 3291 | << PrettyMethod(m); |
| 3292 | return DeoptimizationRequest::kNothing; |
| 3293 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3294 | const Breakpoint* first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3295 | { |
| 3296 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3297 | first_breakpoint = FindFirstBreakpointForMethod(m); |
| 3298 | *existing_brkpt = first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3299 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3300 | |
| 3301 | if (first_breakpoint == nullptr) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3302 | // There is no breakpoint on this method yet: we need to deoptimize. If this method may be |
| 3303 | // inlined, we deoptimize everything; otherwise we deoptimize only this method. |
| 3304 | // Note: IsMethodPossiblyInlined goes into the method verifier and may cause thread suspension. |
| 3305 | // Therefore we must not hold any lock when we call it. |
| 3306 | bool need_full_deoptimization = IsMethodPossiblyInlined(self, m); |
| 3307 | if (need_full_deoptimization) { |
| 3308 | VLOG(jdwp) << "Need full deoptimization because of possible inlining of method " |
| 3309 | << PrettyMethod(m); |
| 3310 | return DeoptimizationRequest::kFullDeoptimization; |
| 3311 | } else { |
| 3312 | // We don't need to deoptimize if the method has not been compiled. |
| 3313 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 3314 | const bool is_compiled = class_linker->GetOatMethodQuickCodeFor(m) != nullptr; |
| 3315 | if (is_compiled) { |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 3316 | // If the method may be called through its direct code pointer (without loading |
| 3317 | // its updated entrypoint), we need full deoptimization to not miss the breakpoint. |
| 3318 | if (class_linker->MayBeCalledWithDirectCodePointer(m)) { |
| 3319 | VLOG(jdwp) << "Need full deoptimization because of possible direct code call " |
| 3320 | << "into image for compiled method " << PrettyMethod(m); |
| 3321 | return DeoptimizationRequest::kFullDeoptimization; |
| 3322 | } else { |
| 3323 | VLOG(jdwp) << "Need selective deoptimization for compiled method " << PrettyMethod(m); |
| 3324 | return DeoptimizationRequest::kSelectiveDeoptimization; |
| 3325 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3326 | } else { |
| 3327 | // Method is not compiled: we don't need to deoptimize. |
| 3328 | VLOG(jdwp) << "No need for deoptimization for non-compiled method " << PrettyMethod(m); |
| 3329 | return DeoptimizationRequest::kNothing; |
| 3330 | } |
| 3331 | } |
| 3332 | } else { |
| 3333 | // There is at least one breakpoint for this method: we don't need to deoptimize. |
| 3334 | // Let's check that all breakpoints are configured the same way for deoptimization. |
| 3335 | VLOG(jdwp) << "Breakpoint already set: no deoptimization is required"; |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3336 | DeoptimizationRequest::Kind deoptimization_kind = first_breakpoint->GetDeoptimizationKind(); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3337 | if (kIsDebugBuild) { |
| 3338 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
| 3339 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
| 3340 | } |
| 3341 | return DeoptimizationRequest::kNothing; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3342 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3343 | } |
| 3344 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3345 | // Installs a breakpoint at the specified location. Also indicates through the deoptimization |
| 3346 | // request if we need to deoptimize. |
| 3347 | void Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
| 3348 | Thread* const self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3349 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3350 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3351 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3352 | const Breakpoint* existing_breakpoint = nullptr; |
| 3353 | const DeoptimizationRequest::Kind deoptimization_kind = |
| 3354 | GetRequiredDeoptimizationKind(self, m, &existing_breakpoint); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3355 | req->SetKind(deoptimization_kind); |
| 3356 | if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
| 3357 | req->SetMethod(m); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3358 | } else { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3359 | CHECK(deoptimization_kind == DeoptimizationRequest::kNothing || |
| 3360 | deoptimization_kind == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3361 | req->SetMethod(nullptr); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3362 | } |
| 3363 | |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3364 | { |
| 3365 | WriterMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3366 | // If there is at least one existing breakpoint on the same method, the new breakpoint |
| 3367 | // must have the same deoptimization kind than the existing breakpoint(s). |
| 3368 | DeoptimizationRequest::Kind breakpoint_deoptimization_kind; |
| 3369 | if (existing_breakpoint != nullptr) { |
| 3370 | breakpoint_deoptimization_kind = existing_breakpoint->GetDeoptimizationKind(); |
| 3371 | } else { |
| 3372 | breakpoint_deoptimization_kind = deoptimization_kind; |
| 3373 | } |
| 3374 | gBreakpoints.push_back(Breakpoint(m, location->dex_pc, breakpoint_deoptimization_kind)); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3375 | VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " |
| 3376 | << gBreakpoints[gBreakpoints.size() - 1]; |
| 3377 | } |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | // Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization |
| 3381 | // request if we need to undeoptimize. |
| 3382 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3383 | WriterMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3384 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3385 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3386 | DeoptimizationRequest::Kind deoptimization_kind = DeoptimizationRequest::kNothing; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3387 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3388 | if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].Method() == m) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3389 | VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i]; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3390 | deoptimization_kind = gBreakpoints[i].GetDeoptimizationKind(); |
| 3391 | DCHECK_EQ(deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization, |
| 3392 | Runtime::Current()->GetInstrumentation()->IsDeoptimized(m)); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3393 | gBreakpoints.erase(gBreakpoints.begin() + i); |
| 3394 | break; |
| 3395 | } |
| 3396 | } |
| 3397 | const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m); |
| 3398 | if (existing_breakpoint == nullptr) { |
| 3399 | // There is no more breakpoint on this method: we need to undeoptimize. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3400 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3401 | // This method required full deoptimization: we need to undeoptimize everything. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3402 | req->SetKind(DeoptimizationRequest::kFullUndeoptimization); |
| 3403 | req->SetMethod(nullptr); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3404 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3405 | // This method required selective deoptimization: we need to undeoptimize only that method. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3406 | req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization); |
| 3407 | req->SetMethod(m); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3408 | } else { |
| 3409 | // This method had no need for deoptimization: do nothing. |
| 3410 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3411 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3412 | req->SetMethod(nullptr); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3413 | } |
| 3414 | } else { |
| 3415 | // There is at least one breakpoint for this method: we don't need to undeoptimize. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3416 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3417 | req->SetMethod(nullptr); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3418 | if (kIsDebugBuild) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3419 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3420 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3421 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3422 | } |
| 3423 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3424 | bool Dbg::IsForcedInterpreterNeededForCallingImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3425 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3426 | if (ssc == nullptr) { |
| 3427 | // If we are not single-stepping, then we don't have to force interpreter. |
| 3428 | return false; |
| 3429 | } |
| 3430 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
| 3431 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3432 | return false; |
| 3433 | } |
| 3434 | |
| 3435 | if (!m->IsNative() && !m->IsProxyMethod()) { |
| 3436 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3437 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3438 | return true; |
| 3439 | } |
| 3440 | } |
| 3441 | return false; |
| 3442 | } |
| 3443 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3444 | bool Dbg::IsForcedInterpreterNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3445 | instrumentation::Instrumentation* const instrumentation = |
| 3446 | Runtime::Current()->GetInstrumentation(); |
| 3447 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3448 | if (instrumentation->InterpretOnly()) { |
| 3449 | return false; |
| 3450 | } |
| 3451 | // We can only interpret pure Java method. |
| 3452 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3453 | return false; |
| 3454 | } |
| 3455 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3456 | if (ssc != nullptr) { |
| 3457 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3458 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3459 | return true; |
| 3460 | } |
| 3461 | // If we are stepping out from a static initializer, by issuing a step |
| 3462 | // in or step over, that was implicitly invoked by calling a static method, |
| 3463 | // then we need to step into that method. Having a lower stack depth than |
| 3464 | // the one the single step control has indicates that the step originates |
| 3465 | // from the static initializer. |
| 3466 | if (ssc->GetStepDepth() != JDWP::SD_OUT && |
| 3467 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3468 | return true; |
| 3469 | } |
| 3470 | } |
| 3471 | // There are cases where we have to force interpreter on deoptimized methods, |
| 3472 | // because in some cases the call will not be performed by invoking an entry |
| 3473 | // point that has been replaced by the deoptimization, but instead by directly |
| 3474 | // invoking the compiled code of the method, for example. |
| 3475 | return instrumentation->IsDeoptimized(m); |
| 3476 | } |
| 3477 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3478 | bool Dbg::IsForcedInstrumentationNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3479 | // The upcall can be null and in that case we don't need to do anything. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3480 | if (m == nullptr) { |
| 3481 | return false; |
| 3482 | } |
| 3483 | instrumentation::Instrumentation* const instrumentation = |
| 3484 | Runtime::Current()->GetInstrumentation(); |
| 3485 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3486 | if (instrumentation->InterpretOnly()) { |
| 3487 | return false; |
| 3488 | } |
| 3489 | // We can only interpret pure Java method. |
| 3490 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3491 | return false; |
| 3492 | } |
| 3493 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3494 | if (ssc != nullptr) { |
| 3495 | // If we are stepping out from a static initializer, by issuing a step |
| 3496 | // out, that was implicitly invoked by calling a static method, then we |
| 3497 | // need to step into the caller of that method. Having a lower stack |
| 3498 | // depth than the one the single step control has indicates that the |
| 3499 | // step originates from the static initializer. |
| 3500 | if (ssc->GetStepDepth() == JDWP::SD_OUT && |
| 3501 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3502 | return true; |
| 3503 | } |
| 3504 | } |
| 3505 | // If we are returning from a static intializer, that was implicitly |
| 3506 | // invoked by calling a static method and the caller is deoptimized, |
| 3507 | // then we have to deoptimize the stack without forcing interpreter |
| 3508 | // on the static method that was called originally. This problem can |
| 3509 | // be solved easily by forcing instrumentation on the called method, |
| 3510 | // because the instrumentation exit hook will recognise the need of |
| 3511 | // stack deoptimization by calling IsForcedInterpreterNeededForUpcall. |
| 3512 | return instrumentation->IsDeoptimized(m); |
| 3513 | } |
| 3514 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3515 | bool Dbg::IsForcedInterpreterNeededForUpcallImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3516 | // The upcall can be null and in that case we don't need to do anything. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3517 | if (m == nullptr) { |
| 3518 | return false; |
| 3519 | } |
| 3520 | instrumentation::Instrumentation* const instrumentation = |
| 3521 | Runtime::Current()->GetInstrumentation(); |
| 3522 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3523 | if (instrumentation->InterpretOnly()) { |
| 3524 | return false; |
| 3525 | } |
| 3526 | // We can only interpret pure Java method. |
| 3527 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3528 | return false; |
| 3529 | } |
| 3530 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3531 | if (ssc != nullptr) { |
| 3532 | // The debugger is not interested in what is happening under the level |
| 3533 | // of the step, thus we only force interpreter when we are not below of |
| 3534 | // the step. |
| 3535 | if (ssc->GetStackDepth() >= GetStackDepth(thread)) { |
| 3536 | return true; |
| 3537 | } |
| 3538 | } |
| 3539 | // We have to require stack deoptimization if the upcall is deoptimized. |
| 3540 | return instrumentation->IsDeoptimized(m); |
| 3541 | } |
| 3542 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3543 | // Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't |
| 3544 | // cause suspension if the thread is the current thread. |
| 3545 | class ScopedThreadSuspension { |
| 3546 | public: |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3547 | ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id) |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 3548 | LOCKS_EXCLUDED(Locks::thread_list_lock_) |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3549 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3550 | thread_(nullptr), |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3551 | error_(JDWP::ERR_NONE), |
| 3552 | self_suspend_(false), |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3553 | other_suspend_(false) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3554 | ScopedObjectAccessUnchecked soa(self); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 3555 | thread_ = DecodeThread(soa, thread_id, &error_); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3556 | if (error_ == JDWP::ERR_NONE) { |
| 3557 | if (thread_ == soa.Self()) { |
| 3558 | self_suspend_ = true; |
| 3559 | } else { |
| 3560 | soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3561 | jobject thread_peer = Dbg::GetObjectRegistry()->GetJObject(thread_id); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3562 | bool timed_out; |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 3563 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 3564 | Thread* suspended_thread = thread_list->SuspendThreadByPeer(thread_peer, true, true, |
| 3565 | &timed_out); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3566 | CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension); |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3567 | if (suspended_thread == nullptr) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3568 | // Thread terminated from under us while suspending. |
| 3569 | error_ = JDWP::ERR_INVALID_THREAD; |
| 3570 | } else { |
| 3571 | CHECK_EQ(suspended_thread, thread_); |
| 3572 | other_suspend_ = true; |
| 3573 | } |
| 3574 | } |
| 3575 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3576 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3577 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3578 | Thread* GetThread() const { |
| 3579 | return thread_; |
| 3580 | } |
| 3581 | |
| 3582 | JDWP::JdwpError GetError() const { |
| 3583 | return error_; |
| 3584 | } |
| 3585 | |
| 3586 | ~ScopedThreadSuspension() { |
| 3587 | if (other_suspend_) { |
| 3588 | Runtime::Current()->GetThreadList()->Resume(thread_, true); |
| 3589 | } |
| 3590 | } |
| 3591 | |
| 3592 | private: |
| 3593 | Thread* thread_; |
| 3594 | JDWP::JdwpError error_; |
| 3595 | bool self_suspend_; |
| 3596 | bool other_suspend_; |
| 3597 | }; |
| 3598 | |
| 3599 | JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size, |
| 3600 | JDWP::JdwpStepDepth step_depth) { |
| 3601 | Thread* self = Thread::Current(); |
| 3602 | ScopedThreadSuspension sts(self, thread_id); |
| 3603 | if (sts.GetError() != JDWP::ERR_NONE) { |
| 3604 | return sts.GetError(); |
| 3605 | } |
| 3606 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3607 | // Work out what ArtMethod* we're in, the current line number, and how deep the stack currently |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3608 | // is for step-out. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3609 | struct SingleStepStackVisitor : public StackVisitor { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3610 | explicit SingleStepStackVisitor(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 3611 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 3612 | stack_depth(0), |
| 3613 | method(nullptr), |
| 3614 | line_number(-1) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 3615 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3616 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 3617 | // annotalysis. |
| 3618 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3619 | ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3620 | if (!m->IsRuntimeMethod()) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3621 | ++stack_depth; |
| 3622 | if (method == nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3623 | mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3624 | method = m; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3625 | if (dex_cache != nullptr) { |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 3626 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3627 | line_number = dex_file.GetLineNumFromPC(m, GetDexPc()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3628 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3629 | } |
| 3630 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 3631 | return true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3632 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3633 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3634 | int stack_depth; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3635 | ArtMethod* method; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3636 | int32_t line_number; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3637 | }; |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3638 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3639 | Thread* const thread = sts.GetThread(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3640 | SingleStepStackVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3641 | visitor.WalkStack(); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3642 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3643 | // Find the dex_pc values that correspond to the current line, for line-based single-stepping. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3644 | struct DebugCallbackContext { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3645 | explicit DebugCallbackContext(SingleStepControl* single_step_control_cb, |
| 3646 | int32_t line_number_cb, const DexFile::CodeItem* code_item) |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3647 | : single_step_control_(single_step_control_cb), line_number_(line_number_cb), |
| 3648 | code_item_(code_item), last_pc_valid(false), last_pc(0) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3649 | } |
| 3650 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3651 | static bool Callback(void* raw_context, uint32_t address, uint32_t line_number_cb) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3652 | DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3653 | if (static_cast<int32_t>(line_number_cb) == context->line_number_) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3654 | if (!context->last_pc_valid) { |
| 3655 | // Everything from this address until the next line change is ours. |
| 3656 | context->last_pc = address; |
| 3657 | context->last_pc_valid = true; |
| 3658 | } |
| 3659 | // Otherwise, if we're already in a valid range for this line, |
| 3660 | // just keep going (shouldn't really happen)... |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3661 | } else if (context->last_pc_valid) { // and the line number is new |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3662 | // Add everything from the last entry up until here to the set |
| 3663 | for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3664 | context->single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3665 | } |
| 3666 | context->last_pc_valid = false; |
| 3667 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3668 | return false; // There may be multiple entries for any given line. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3669 | } |
| 3670 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3671 | ~DebugCallbackContext() { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3672 | // If the line number was the last in the position table... |
| 3673 | if (last_pc_valid) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3674 | size_t end = code_item_->insns_size_in_code_units_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3675 | for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3676 | single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3677 | } |
| 3678 | } |
| 3679 | } |
| 3680 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3681 | SingleStepControl* const single_step_control_; |
| 3682 | const int32_t line_number_; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3683 | const DexFile::CodeItem* const code_item_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3684 | bool last_pc_valid; |
| 3685 | uint32_t last_pc; |
| 3686 | }; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3687 | |
| 3688 | // Allocate single step. |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3689 | SingleStepControl* single_step_control = |
| 3690 | new (std::nothrow) SingleStepControl(step_size, step_depth, |
| 3691 | visitor.stack_depth, visitor.method); |
| 3692 | if (single_step_control == nullptr) { |
| 3693 | LOG(ERROR) << "Failed to allocate SingleStepControl"; |
| 3694 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3695 | } |
| 3696 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3697 | ArtMethod* m = single_step_control->GetMethod(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3698 | const int32_t line_number = visitor.line_number; |
Sebastien Hertz | 52f5f93 | 2015-05-28 11:00:57 +0200 | [diff] [blame] | 3699 | // Note: if the thread is not running Java code (pure native thread), there is no "current" |
| 3700 | // method on the stack (and no line number either). |
| 3701 | if (m != nullptr && !m->IsNative()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3702 | const DexFile::CodeItem* const code_item = m->GetCodeItem(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3703 | DebugCallbackContext context(single_step_control, line_number, code_item); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3704 | m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3705 | DebugCallbackContext::Callback, nullptr, &context); |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 3706 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3707 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3708 | // Activate single-step in the thread. |
| 3709 | thread->ActivateSingleStepControl(single_step_control); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3710 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3711 | if (VLOG_IS_ON(jdwp)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3712 | VLOG(jdwp) << "Single-step thread: " << *thread; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3713 | VLOG(jdwp) << "Single-step step size: " << single_step_control->GetStepSize(); |
| 3714 | VLOG(jdwp) << "Single-step step depth: " << single_step_control->GetStepDepth(); |
| 3715 | VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->GetMethod()); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3716 | VLOG(jdwp) << "Single-step current line: " << line_number; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3717 | VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->GetStackDepth(); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3718 | VLOG(jdwp) << "Single-step dex_pc values:"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3719 | for (uint32_t dex_pc : single_step_control->GetDexPcs()) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3720 | VLOG(jdwp) << StringPrintf(" %#x", dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3721 | } |
| 3722 | } |
| 3723 | |
| 3724 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3725 | } |
| 3726 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3727 | void Dbg::UnconfigureStep(JDWP::ObjectId thread_id) { |
| 3728 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3729 | JDWP::JdwpError error; |
| 3730 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Sebastien Hertz | 87118ed | 2013-11-26 17:57:18 +0100 | [diff] [blame] | 3731 | if (error == JDWP::ERR_NONE) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3732 | thread->DeactivateSingleStepControl(); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3733 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3734 | } |
| 3735 | |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3736 | static char JdwpTagToShortyChar(JDWP::JdwpTag tag) { |
| 3737 | switch (tag) { |
| 3738 | default: |
| 3739 | LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag); |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 3740 | UNREACHABLE(); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3741 | |
| 3742 | // Primitives. |
| 3743 | case JDWP::JT_BYTE: return 'B'; |
| 3744 | case JDWP::JT_CHAR: return 'C'; |
| 3745 | case JDWP::JT_FLOAT: return 'F'; |
| 3746 | case JDWP::JT_DOUBLE: return 'D'; |
| 3747 | case JDWP::JT_INT: return 'I'; |
| 3748 | case JDWP::JT_LONG: return 'J'; |
| 3749 | case JDWP::JT_SHORT: return 'S'; |
| 3750 | case JDWP::JT_VOID: return 'V'; |
| 3751 | case JDWP::JT_BOOLEAN: return 'Z'; |
| 3752 | |
| 3753 | // Reference types. |
| 3754 | case JDWP::JT_ARRAY: |
| 3755 | case JDWP::JT_OBJECT: |
| 3756 | case JDWP::JT_STRING: |
| 3757 | case JDWP::JT_THREAD: |
| 3758 | case JDWP::JT_THREAD_GROUP: |
| 3759 | case JDWP::JT_CLASS_LOADER: |
| 3760 | case JDWP::JT_CLASS_OBJECT: |
| 3761 | return 'L'; |
| 3762 | } |
| 3763 | } |
| 3764 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 3765 | JDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id, |
| 3766 | JDWP::RefTypeId class_id, JDWP::MethodId method_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3767 | uint32_t arg_count, uint64_t* arg_values, |
| 3768 | JDWP::JdwpTag* arg_types, uint32_t options, |
| 3769 | JDWP::JdwpTag* pResultTag, uint64_t* pResultValue, |
| 3770 | JDWP::ObjectId* pExceptionId) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3771 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 3772 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3773 | Thread* targetThread = nullptr; |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3774 | std::unique_ptr<DebugInvokeReq> req; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3775 | Thread* self = Thread::Current(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3776 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3777 | ScopedObjectAccessUnchecked soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3778 | JDWP::JdwpError error; |
| 3779 | targetThread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 3780 | if (error != JDWP::ERR_NONE) { |
| 3781 | LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id; |
| 3782 | return error; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3783 | } |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3784 | if (targetThread->GetInvokeReq() != nullptr) { |
| 3785 | // Thread is already invoking a method on behalf of the debugger. |
| 3786 | LOG(ERROR) << "InvokeMethod request for thread already invoking a method: " << *targetThread; |
| 3787 | return JDWP::ERR_ALREADY_INVOKING; |
| 3788 | } |
| 3789 | if (!targetThread->IsReadyForDebugInvoke()) { |
| 3790 | // Thread is not suspended by an event so it cannot invoke a method. |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3791 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 3792 | return JDWP::ERR_INVALID_THREAD; |
| 3793 | } |
| 3794 | |
| 3795 | /* |
| 3796 | * We currently have a bug where we don't successfully resume the |
| 3797 | * target thread if the suspend count is too deep. We're expected to |
| 3798 | * require one "resume" for each "suspend", but when asked to execute |
| 3799 | * a method we have to resume fully and then re-suspend it back to the |
| 3800 | * same level. (The easiest way to cause this is to type "suspend" |
| 3801 | * multiple times in jdb.) |
| 3802 | * |
| 3803 | * It's unclear what this means when the event specifies "resume all" |
| 3804 | * and some threads are suspended more deeply than others. This is |
| 3805 | * a rare problem, so for now we just prevent it from hanging forever |
| 3806 | * by rejecting the method invocation request. Without this, we will |
| 3807 | * be stuck waiting on a suspended thread. |
| 3808 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3809 | int suspend_count; |
| 3810 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3811 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3812 | suspend_count = targetThread->GetSuspendCount(); |
| 3813 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3814 | if (suspend_count > 1) { |
| 3815 | LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3816 | return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here. |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3817 | } |
| 3818 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3819 | mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 3820 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3821 | return JDWP::ERR_INVALID_OBJECT; |
| 3822 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3823 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3824 | gRegistry->Get<mirror::Object*>(thread_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3825 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3826 | return JDWP::ERR_INVALID_OBJECT; |
| 3827 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3828 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3829 | mirror::Class* c = DecodeClass(class_id, &error); |
| 3830 | if (c == nullptr) { |
| 3831 | return error; |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3832 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3833 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3834 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3835 | if (m->IsStatic() != (receiver == nullptr)) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3836 | return JDWP::ERR_INVALID_METHODID; |
| 3837 | } |
| 3838 | if (m->IsStatic()) { |
| 3839 | if (m->GetDeclaringClass() != c) { |
| 3840 | return JDWP::ERR_INVALID_METHODID; |
| 3841 | } |
| 3842 | } else { |
| 3843 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
| 3844 | return JDWP::ERR_INVALID_METHODID; |
| 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | // Check the argument list matches the method. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3849 | uint32_t shorty_len = 0; |
| 3850 | const char* shorty = m->GetShorty(&shorty_len); |
| 3851 | if (shorty_len - 1 != arg_count) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3852 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3853 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3854 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3855 | { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3856 | StackHandleScope<2> hs(soa.Self()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3857 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver)); |
| 3858 | HandleWrapper<mirror::Class> h_klass(hs.NewHandleWrapper(&c)); |
| 3859 | const DexFile::TypeList* types = m->GetParameterTypeList(); |
| 3860 | for (size_t i = 0; i < arg_count; ++i) { |
| 3861 | if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) { |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3862 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3863 | } |
| 3864 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3865 | if (shorty[i + 1] == 'L') { |
| 3866 | // Did we really get an argument of an appropriate reference type? |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 3867 | mirror::Class* parameter_type = |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3868 | m->GetClassFromTypeIndex(types->GetTypeItem(i).type_idx_, true); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3869 | mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i], &error); |
| 3870 | if (error != JDWP::ERR_NONE) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3871 | return JDWP::ERR_INVALID_OBJECT; |
| 3872 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3873 | if (argument != nullptr && !argument->InstanceOf(parameter_type)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3874 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3875 | } |
| 3876 | |
| 3877 | // Turn the on-the-wire ObjectId into a jobject. |
| 3878 | jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]); |
| 3879 | v.l = gRegistry->GetJObject(arg_values[i]); |
| 3880 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3881 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3882 | } |
| 3883 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3884 | // Allocates a DebugInvokeReq. |
| 3885 | req.reset(new (std::nothrow) DebugInvokeReq(receiver, c, m, options, arg_values, arg_count)); |
| 3886 | if (req.get() == nullptr) { |
| 3887 | LOG(ERROR) << "Failed to allocate DebugInvokeReq"; |
| 3888 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3889 | } |
| 3890 | |
| 3891 | // Attach the DebugInvokeReq to the target thread so it executes the method when |
| 3892 | // it is resumed. Once the invocation completes, it will detach it and signal us |
| 3893 | // before suspending itself. |
| 3894 | targetThread->SetDebugInvokeReq(req.get()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | // The fact that we've released the thread list lock is a bit risky --- if the thread goes |
| 3898 | // away we're sitting high and dry -- but we must release this before the ResumeAllThreads |
| 3899 | // call, and it's unwise to hold it during WaitForSuspend. |
| 3900 | |
| 3901 | { |
| 3902 | /* |
| 3903 | * We change our (JDWP thread) status, which should be THREAD_RUNNING, |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 3904 | * 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] | 3905 | * run out of memory. It's also a good idea to change it before locking |
| 3906 | * the invokeReq mutex, although that should never be held for long. |
| 3907 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3908 | self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3909 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3910 | VLOG(jdwp) << " Transferring control to event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3911 | { |
Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 3912 | MutexLock mu(self, req->lock); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3913 | |
| 3914 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3915 | VLOG(jdwp) << " Resuming all threads"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3916 | thread_list->UndoDebuggerSuspensions(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3917 | } else { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3918 | VLOG(jdwp) << " Resuming event thread only"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3919 | thread_list->Resume(targetThread, true); |
| 3920 | } |
| 3921 | |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 3922 | // The target thread is resumed but needs the JDWP token we're holding. |
| 3923 | // We release it now and will acquire it again when the invocation is |
| 3924 | // complete and the target thread suspends itself. |
| 3925 | gJdwpState->ReleaseJdwpTokenForCommand(); |
| 3926 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3927 | // Wait for the request to finish executing. |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3928 | while (targetThread->GetInvokeReq() != nullptr) { |
Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 3929 | req->cond.Wait(self); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3930 | } |
| 3931 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3932 | VLOG(jdwp) << " Control has returned from event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3933 | |
| 3934 | /* wait for thread to re-suspend itself */ |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 3935 | SuspendThread(thread_id, false /* request_suspension */); |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 3936 | |
| 3937 | // Now the thread is suspended again, we can re-acquire the JDWP token. |
| 3938 | gJdwpState->AcquireJdwpTokenForCommand(); |
| 3939 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3940 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | /* |
| 3944 | * Suspend the threads. We waited for the target thread to suspend |
| 3945 | * itself, so all we need to do is suspend the others. |
| 3946 | * |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 3947 | * The SuspendAllForDebugger() call will double-suspend the event thread, |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3948 | * so we want to resume the target thread once to keep the books straight. |
| 3949 | */ |
| 3950 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3951 | self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3952 | VLOG(jdwp) << " Suspending all threads"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3953 | thread_list->SuspendAllForDebugger(); |
| 3954 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3955 | VLOG(jdwp) << " Resuming event thread to balance the count"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3956 | thread_list->Resume(targetThread, true); |
| 3957 | } |
| 3958 | |
| 3959 | // Copy the result. |
| 3960 | *pResultTag = req->result_tag; |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3961 | *pResultValue = req->result_value; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3962 | *pExceptionId = req->exception; |
| 3963 | return req->error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3967 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3968 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 3969 | // We can be called while an exception is pending. We need |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3970 | // to preserve that across the method invocation. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3971 | StackHandleScope<3> hs(soa.Self()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3972 | auto old_exception = hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()); |
| 3973 | soa.Self()->ClearException(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3974 | |
| 3975 | // Translate the method through the vtable, unless the debugger wants to suppress it. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3976 | auto* m = pReq->method; |
| 3977 | auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3978 | if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver.Read() != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3979 | ArtMethod* actual_method = |
| 3980 | pReq->klass.Read()->FindVirtualMethodForVirtualOrInterface(m, image_pointer_size); |
| 3981 | if (actual_method != m) { |
| 3982 | VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3983 | << " to " << PrettyMethod(actual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3984 | m = actual_method; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3985 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3986 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3987 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m) |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3988 | << " receiver=" << pReq->receiver.Read() |
Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 3989 | << " arg_count=" << pReq->arg_count; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3990 | CHECK(m != nullptr); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3991 | |
| 3992 | CHECK_EQ(sizeof(jvalue), sizeof(uint64_t)); |
| 3993 | |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 3994 | ScopedLocalRef<jobject> ref(soa.Env(), soa.AddLocalReference<jobject>(pReq->receiver.Read())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3995 | JValue result = InvokeWithJValues(soa, ref.get(), soa.EncodeMethod(m), |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3996 | reinterpret_cast<jvalue*>(pReq->arg_values)); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3997 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3998 | pReq->result_tag = BasicTagFromDescriptor(m->GetShorty()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3999 | const bool is_object_result = (pReq->result_tag == JDWP::JT_OBJECT); |
| 4000 | Handle<mirror::Object> object_result = hs.NewHandle(is_object_result ? result.GetL() : nullptr); |
| 4001 | Handle<mirror::Throwable> exception = hs.NewHandle(soa.Self()->GetException()); |
| 4002 | soa.Self()->ClearException(); |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 4003 | pReq->exception = gRegistry->Add(exception); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4004 | if (pReq->exception != 0) { |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4005 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exception.Get() |
| 4006 | << " " << exception->Dump(); |
| 4007 | pReq->result_value = 0; |
| 4008 | } else if (is_object_result) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4009 | /* if no exception thrown, examine object result more closely */ |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4010 | JDWP::JdwpTag new_tag = TagFromObject(soa, object_result.Get()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4011 | if (new_tag != pReq->result_tag) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4012 | VLOG(jdwp) << " JDWP promoted result from " << pReq->result_tag << " to " << new_tag; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4013 | pReq->result_tag = new_tag; |
| 4014 | } |
| 4015 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4016 | // Register the object in the registry and reference its ObjectId. This ensures |
| 4017 | // GC safety and prevents from accessing stale reference if the object is moved. |
| 4018 | pReq->result_value = gRegistry->Add(object_result.Get()); |
| 4019 | } else { |
| 4020 | // Primitive result. |
| 4021 | DCHECK(IsPrimitiveTag(pReq->result_tag)); |
| 4022 | pReq->result_value = result.GetJ(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4023 | } |
| 4024 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4025 | if (old_exception.Get() != nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 4026 | soa.Self()->SetException(old_exception.Get()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4027 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4028 | } |
| 4029 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4030 | /* |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4031 | * "request" contains a full JDWP packet, possibly with multiple chunks. We |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4032 | * need to process each, accumulate the replies, and ship the whole thing |
| 4033 | * back. |
| 4034 | * |
| 4035 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 4036 | * and includes the chunk type/length, followed by the data. |
| 4037 | * |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 4038 | * 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] | 4039 | * chunk. If this becomes inconvenient we will need to adapt. |
| 4040 | */ |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4041 | bool Dbg::DdmHandlePacket(JDWP::Request* request, uint8_t** pReplyBuf, int* pReplyLen) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4042 | Thread* self = Thread::Current(); |
| 4043 | JNIEnv* env = self->GetJniEnv(); |
| 4044 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4045 | uint32_t type = request->ReadUnsigned32("type"); |
| 4046 | uint32_t length = request->ReadUnsigned32("length"); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4047 | |
| 4048 | // Create a byte[] corresponding to 'request'. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4049 | size_t request_length = request->size(); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4050 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4051 | if (dataArray.get() == nullptr) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4052 | LOG(WARNING) << "byte[] allocation failed: " << request_length; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4053 | env->ExceptionClear(); |
| 4054 | return false; |
| 4055 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4056 | env->SetByteArrayRegion(dataArray.get(), 0, request_length, |
| 4057 | reinterpret_cast<const jbyte*>(request->data())); |
| 4058 | request->Skip(request_length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4059 | |
| 4060 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4061 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4062 | if (length != request_length) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4063 | 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] | 4064 | return false; |
| 4065 | } |
| 4066 | |
| 4067 | // 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] | 4068 | ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4069 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch, |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4070 | type, dataArray.get(), 0, length)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4071 | if (env->ExceptionCheck()) { |
| 4072 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 4073 | env->ExceptionDescribe(); |
| 4074 | env->ExceptionClear(); |
| 4075 | return false; |
| 4076 | } |
| 4077 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4078 | if (chunk.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4079 | return false; |
| 4080 | } |
| 4081 | |
| 4082 | /* |
| 4083 | * Pull the pieces out of the chunk. We copy the results into a |
| 4084 | * newly-allocated buffer that the caller can free. We don't want to |
| 4085 | * continue using the Chunk object because nothing has a reference to it. |
| 4086 | * |
| 4087 | * We could avoid this by returning type/data/offset/length and having |
| 4088 | * the caller be aware of the object lifetime issues, but that |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 4089 | * 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] | 4090 | * if we have responses for multiple chunks. |
| 4091 | * |
| 4092 | * So we're pretty much stuck with copying data around multiple times. |
| 4093 | */ |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4094 | 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] | 4095 | 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] | 4096 | 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] | 4097 | 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] | 4098 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4099 | VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4100 | if (length == 0 || replyData.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4101 | return false; |
| 4102 | } |
| 4103 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4104 | const int kChunkHdrLen = 8; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4105 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4106 | if (reply == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4107 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 4108 | return false; |
| 4109 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 4110 | JDWP::Set4BE(reply + 0, type); |
| 4111 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4112 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4113 | |
| 4114 | *pReplyBuf = reply; |
| 4115 | *pReplyLen = length + kChunkHdrLen; |
| 4116 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4117 | 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] | 4118 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4119 | } |
| 4120 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4121 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4122 | VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4123 | |
| 4124 | Thread* self = Thread::Current(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4125 | if (self->GetState() != kRunnable) { |
| 4126 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 4127 | /* try anyway? */ |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4128 | } |
| 4129 | |
| 4130 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4131 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4132 | env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4133 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast, |
| 4134 | event); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4135 | if (env->ExceptionCheck()) { |
| 4136 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 4137 | env->ExceptionDescribe(); |
| 4138 | env->ExceptionClear(); |
| 4139 | } |
| 4140 | } |
| 4141 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4142 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4143 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4147 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4148 | gDdmThreadNotification = false; |
| 4149 | } |
| 4150 | |
| 4151 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4152 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4153 | * |
| 4154 | * Because we broadcast the full set of threads when the notifications are |
| 4155 | * first enabled, it's possible for "thread" to be actively executing. |
| 4156 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4157 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4158 | if (!gDdmThreadNotification) { |
| 4159 | return; |
| 4160 | } |
| 4161 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4162 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4163 | uint8_t buf[4]; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4164 | JDWP::Set4BE(&buf[0], t->GetThreadId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4165 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4166 | } else { |
| 4167 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4168 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4169 | StackHandleScope<1> hs(soa.Self()); |
| 4170 | Handle<mirror::String> name(hs.NewHandle(t->GetThreadName(soa))); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4171 | size_t char_count = (name.Get() != nullptr) ? name->GetLength() : 0; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 4172 | const jchar* chars = (name.Get() != nullptr) ? name->GetValue() : nullptr; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4173 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4174 | std::vector<uint8_t> bytes; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4175 | JDWP::Append4BE(bytes, t->GetThreadId()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4176 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4177 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 4178 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4179 | } |
| 4180 | } |
| 4181 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4182 | void Dbg::DdmSetThreadNotification(bool enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4183 | // Enable/disable thread notifications. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4184 | gDdmThreadNotification = enable; |
| 4185 | if (enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4186 | // Suspend the VM then post thread start notifications for all threads. Threads attaching will |
| 4187 | // see a suspension in progress and block until that ends. They then post their own start |
| 4188 | // notification. |
| 4189 | SuspendVM(); |
| 4190 | std::list<Thread*> threads; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4191 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4192 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4193 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4194 | threads = Runtime::Current()->GetThreadList()->GetList(); |
| 4195 | } |
| 4196 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4197 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4198 | for (Thread* thread : threads) { |
| 4199 | Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR")); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4200 | } |
| 4201 | } |
| 4202 | ResumeVM(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4203 | } |
| 4204 | } |
| 4205 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4206 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 4207 | if (IsDebuggerActive()) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 4208 | gJdwpState->PostThreadChange(t, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4209 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4210 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4214 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4218 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4219 | } |
| 4220 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4221 | void Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4222 | CHECK(buf != nullptr); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4223 | iovec vec[1]; |
| 4224 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 4225 | vec[0].iov_len = byte_count; |
| 4226 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4227 | } |
| 4228 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4229 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 4230 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 4231 | } |
| 4232 | |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 4233 | void Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4234 | if (gJdwpState == nullptr) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4235 | VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4236 | } else { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 4237 | gJdwpState->DdmSendChunkV(type, iov, iov_count); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4238 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4239 | } |
| 4240 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 4241 | JDWP::JdwpState* Dbg::GetJdwpState() { |
| 4242 | return gJdwpState; |
| 4243 | } |
| 4244 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4245 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 4246 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4247 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4248 | return true; |
| 4249 | } |
| 4250 | |
| 4251 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 4252 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 4253 | return false; |
| 4254 | } |
| 4255 | |
| 4256 | gDdmHpifWhen = when; |
| 4257 | return true; |
| 4258 | } |
| 4259 | |
| 4260 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 4261 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 4262 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 4263 | return false; |
| 4264 | } |
| 4265 | |
| 4266 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 4267 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 4268 | return false; |
| 4269 | } |
| 4270 | |
| 4271 | if (native) { |
| 4272 | gDdmNhsgWhen = when; |
| 4273 | gDdmNhsgWhat = what; |
| 4274 | } else { |
| 4275 | gDdmHpsgWhen = when; |
| 4276 | gDdmHpsgWhat = what; |
| 4277 | } |
| 4278 | return true; |
| 4279 | } |
| 4280 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4281 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 4282 | // If there's a one-shot 'when', reset it. |
| 4283 | if (reason == gDdmHpifWhen) { |
| 4284 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 4285 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 4286 | } |
| 4287 | } |
| 4288 | |
| 4289 | /* |
| 4290 | * Chunk HPIF (client --> server) |
| 4291 | * |
| 4292 | * Heap Info. General information about the heap, |
| 4293 | * suitable for a summary display. |
| 4294 | * |
| 4295 | * [u4]: number of heaps |
| 4296 | * |
| 4297 | * For each heap: |
| 4298 | * [u4]: heap ID |
| 4299 | * [u8]: timestamp in ms since Unix epoch |
| 4300 | * [u1]: capture reason (same as 'when' value from server) |
| 4301 | * [u4]: max heap size in bytes (-Xmx) |
| 4302 | * [u4]: current heap size in bytes |
| 4303 | * [u4]: current number of bytes allocated |
| 4304 | * [u4]: current number of objects allocated |
| 4305 | */ |
| 4306 | uint8_t heap_count = 1; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4307 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4308 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4309 | JDWP::Append4BE(bytes, heap_count); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4310 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4311 | JDWP::Append8BE(bytes, MilliTime()); |
| 4312 | JDWP::Append1BE(bytes, reason); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4313 | JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes. |
| 4314 | JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4315 | JDWP::Append4BE(bytes, heap->GetBytesAllocated()); |
| 4316 | JDWP::Append4BE(bytes, heap->GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4317 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 4318 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4319 | } |
| 4320 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4321 | enum HpsgSolidity { |
| 4322 | SOLIDITY_FREE = 0, |
| 4323 | SOLIDITY_HARD = 1, |
| 4324 | SOLIDITY_SOFT = 2, |
| 4325 | SOLIDITY_WEAK = 3, |
| 4326 | SOLIDITY_PHANTOM = 4, |
| 4327 | SOLIDITY_FINALIZABLE = 5, |
| 4328 | SOLIDITY_SWEEP = 6, |
| 4329 | }; |
| 4330 | |
| 4331 | enum HpsgKind { |
| 4332 | KIND_OBJECT = 0, |
| 4333 | KIND_CLASS_OBJECT = 1, |
| 4334 | KIND_ARRAY_1 = 2, |
| 4335 | KIND_ARRAY_2 = 3, |
| 4336 | KIND_ARRAY_4 = 4, |
| 4337 | KIND_ARRAY_8 = 5, |
| 4338 | KIND_UNKNOWN = 6, |
| 4339 | KIND_NATIVE = 7, |
| 4340 | }; |
| 4341 | |
| 4342 | #define HPSG_PARTIAL (1<<7) |
| 4343 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 4344 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4345 | class HeapChunkContext { |
| 4346 | public: |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4347 | // Maximum chunk size. Obtain this from the formula: |
| 4348 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 4349 | HeapChunkContext(bool merge, bool native) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4350 | : buf_(16384 - 16), |
| 4351 | type_(0), |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4352 | chunk_overhead_(0) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4353 | Reset(); |
| 4354 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4355 | type_ = CHUNK_TYPE("NHSG"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4356 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4357 | type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4358 | } |
| 4359 | } |
| 4360 | |
| 4361 | ~HeapChunkContext() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4362 | if (p_ > &buf_[0]) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4363 | Flush(); |
| 4364 | } |
| 4365 | } |
| 4366 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4367 | void SetChunkOverhead(size_t chunk_overhead) { |
| 4368 | chunk_overhead_ = chunk_overhead; |
| 4369 | } |
| 4370 | |
| 4371 | void ResetStartOfNextChunk() { |
| 4372 | startOfNextMemoryChunk_ = nullptr; |
| 4373 | } |
| 4374 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4375 | void EnsureHeader(const void* chunk_ptr) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4376 | if (!needHeader_) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4377 | return; |
| 4378 | } |
| 4379 | |
| 4380 | // Start a new HPSx chunk. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4381 | JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap). |
| 4382 | JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4383 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4384 | JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 4385 | 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] | 4386 | // [u4]: length of piece, in allocation units |
| 4387 | // 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] | 4388 | pieceLenField_ = p_; |
| 4389 | JDWP::Write4BE(&p_, 0x55555555); |
| 4390 | needHeader_ = false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4391 | } |
| 4392 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4393 | void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4394 | if (pieceLenField_ == nullptr) { |
Ian Rogers | d636b06 | 2013-01-18 17:51:18 -0800 | [diff] [blame] | 4395 | // Flush immediately post Reset (maybe back-to-back Flush). Ignore. |
| 4396 | CHECK(needHeader_); |
| 4397 | return; |
| 4398 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4399 | // Patch the "length of piece" field. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4400 | CHECK_LE(&buf_[0], pieceLenField_); |
| 4401 | CHECK_LE(pieceLenField_, p_); |
| 4402 | JDWP::Set4BE(pieceLenField_, totalAllocationUnits_); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4403 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4404 | Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4405 | Reset(); |
| 4406 | } |
| 4407 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4408 | static void HeapChunkJavaCallback(void* start, void* end, size_t used_bytes, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4409 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 4410 | Locks::mutator_lock_) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4411 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkJavaCallback(start, end, used_bytes); |
| 4412 | } |
| 4413 | |
| 4414 | static void HeapChunkNativeCallback(void* start, void* end, size_t used_bytes, void* arg) |
| 4415 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4416 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkNativeCallback(start, end, used_bytes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4417 | } |
| 4418 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4419 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4420 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 4421 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4422 | void Reset() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4423 | p_ = &buf_[0]; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4424 | ResetStartOfNextChunk(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4425 | totalAllocationUnits_ = 0; |
| 4426 | needHeader_ = true; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4427 | pieceLenField_ = nullptr; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4428 | } |
| 4429 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4430 | bool IsNative() const { |
| 4431 | return type_ == CHUNK_TYPE("NHSG"); |
| 4432 | } |
| 4433 | |
| 4434 | // Returns true if the object is not an empty chunk. |
| 4435 | bool ProcessRecord(void* start, size_t used_bytes) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4436 | // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken |
| 4437 | // 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] | 4438 | if (used_bytes == 0) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4439 | if (start == nullptr) { |
| 4440 | // Reset for start of new heap. |
| 4441 | startOfNextMemoryChunk_ = nullptr; |
| 4442 | Flush(); |
| 4443 | } |
| 4444 | // Only process in use memory so that free region information |
| 4445 | // also includes dlmalloc book keeping. |
| 4446 | return false; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4447 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4448 | if (startOfNextMemoryChunk_ != nullptr) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4449 | // Transmit any pending free memory. Native free memory of over kMaxFreeLen could be because |
| 4450 | // of the use of mmaps, so don't report. If not free memory then start a new segment. |
| 4451 | bool flush = true; |
| 4452 | if (start > startOfNextMemoryChunk_) { |
| 4453 | const size_t kMaxFreeLen = 2 * kPageSize; |
| 4454 | void* free_start = startOfNextMemoryChunk_; |
| 4455 | void* free_end = start; |
| 4456 | const size_t free_len = |
| 4457 | reinterpret_cast<uintptr_t>(free_end) - reinterpret_cast<uintptr_t>(free_start); |
| 4458 | if (!IsNative() || free_len < kMaxFreeLen) { |
| 4459 | AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), free_start, free_len, IsNative()); |
| 4460 | flush = false; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4461 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4462 | } |
| 4463 | if (flush) { |
| 4464 | startOfNextMemoryChunk_ = nullptr; |
| 4465 | Flush(); |
| 4466 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4467 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4468 | return true; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4469 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4470 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4471 | void HeapChunkNativeCallback(void* start, void* /*end*/, size_t used_bytes) |
| 4472 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4473 | if (ProcessRecord(start, used_bytes)) { |
| 4474 | uint8_t state = ExamineNativeObject(start); |
| 4475 | AppendChunk(state, start, used_bytes + chunk_overhead_, true /*is_native*/); |
| 4476 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4477 | } |
| 4478 | } |
| 4479 | |
| 4480 | void HeapChunkJavaCallback(void* start, void* /*end*/, size_t used_bytes) |
| 4481 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 4482 | if (ProcessRecord(start, used_bytes)) { |
| 4483 | // Determine the type of this chunk. |
| 4484 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 4485 | // If it's the same, we should combine them. |
| 4486 | uint8_t state = ExamineJavaObject(reinterpret_cast<mirror::Object*>(start)); |
| 4487 | AppendChunk(state, start, used_bytes + chunk_overhead_, false /*is_native*/); |
| 4488 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4489 | } |
| 4490 | } |
| 4491 | |
| 4492 | void AppendChunk(uint8_t state, void* ptr, size_t length, bool is_native) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4493 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4494 | // Make sure there's enough room left in the buffer. |
| 4495 | // We need to use two bytes for every fractional 256 allocation units used by the chunk plus |
| 4496 | // 17 bytes for any header. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4497 | const size_t needed = ((RoundUp(length / ALLOCATION_UNIT_SIZE, 256) / 256) * 2) + 17; |
| 4498 | size_t byte_left = &buf_.back() - p_; |
| 4499 | if (byte_left < needed) { |
| 4500 | if (is_native) { |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4501 | // Cannot trigger memory allocation while walking native heap. |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4502 | return; |
| 4503 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4504 | Flush(); |
| 4505 | } |
| 4506 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4507 | byte_left = &buf_.back() - p_; |
| 4508 | if (byte_left < needed) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4509 | LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", " |
| 4510 | << needed << " bytes)"; |
| 4511 | return; |
| 4512 | } |
| 4513 | EnsureHeader(ptr); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4514 | // Write out the chunk description. |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4515 | length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units. |
| 4516 | totalAllocationUnits_ += length; |
| 4517 | while (length > 256) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4518 | *p_++ = state | HPSG_PARTIAL; |
| 4519 | *p_++ = 255; // length - 1 |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4520 | length -= 256; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4521 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4522 | *p_++ = state; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4523 | *p_++ = length - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4524 | } |
| 4525 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4526 | uint8_t ExamineNativeObject(const void* p) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4527 | return p == nullptr ? HPSG_STATE(SOLIDITY_FREE, 0) : HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4528 | } |
| 4529 | |
| 4530 | uint8_t ExamineJavaObject(mirror::Object* o) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4531 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4532 | if (o == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4533 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 4534 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4535 | // It's an allocated chunk. Figure out what it is. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4536 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 4537 | if (!heap->IsLiveObjectLocked(o)) { |
| 4538 | LOG(ERROR) << "Invalid object in managed heap: " << o; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4539 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4540 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4541 | mirror::Class* c = o->GetClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4542 | if (c == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4543 | // The object was probably just created but hasn't been initialized yet. |
| 4544 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4545 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4546 | if (!heap->IsValidObjectAddress(c)) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4547 | LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4548 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4549 | } |
Mathieu Chartier | f26e1b3 | 2015-01-29 10:47:10 -0800 | [diff] [blame] | 4550 | if (c->GetClass() == nullptr) { |
| 4551 | LOG(ERROR) << "Null class of class " << c << " for object " << o; |
| 4552 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4553 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4554 | if (c->IsClassClass()) { |
| 4555 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 4556 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4557 | if (c->IsArrayClass()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4558 | switch (c->GetComponentSize()) { |
| 4559 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 4560 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 4561 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 4562 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 4563 | } |
| 4564 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4565 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4566 | } |
| 4567 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4568 | std::vector<uint8_t> buf_; |
| 4569 | uint8_t* p_; |
| 4570 | uint8_t* pieceLenField_; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4571 | void* startOfNextMemoryChunk_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4572 | size_t totalAllocationUnits_; |
| 4573 | uint32_t type_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4574 | bool needHeader_; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4575 | size_t chunk_overhead_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4576 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4577 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 4578 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4579 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4580 | static void BumpPointerSpaceCallback(mirror::Object* obj, void* arg) |
| 4581 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 4582 | const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4583 | HeapChunkContext::HeapChunkJavaCallback( |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4584 | obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, arg); |
| 4585 | } |
| 4586 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4587 | void Dbg::DdmSendHeapSegments(bool native) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4588 | Dbg::HpsgWhen when = native ? gDdmNhsgWhen : gDdmHpsgWhen; |
| 4589 | Dbg::HpsgWhat what = native ? gDdmNhsgWhat : gDdmHpsgWhat; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4590 | if (when == HPSG_WHEN_NEVER) { |
| 4591 | return; |
| 4592 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4593 | // Figure out what kind of chunks we'll be sending. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4594 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) |
| 4595 | << static_cast<int>(what); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4596 | |
| 4597 | // First, send a heap start chunk. |
| 4598 | uint8_t heap_id[4]; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4599 | 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] | 4600 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4601 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4602 | Locks::mutator_lock_->AssertSharedHeld(self); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4603 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4604 | // Send a series of heap segment chunks. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4605 | HeapChunkContext context(what == HPSG_WHAT_MERGED_OBJECTS, native); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4606 | if (native) { |
Ian Rogers | 872dd82 | 2014-10-30 11:19:14 -0700 | [diff] [blame] | 4607 | #if defined(HAVE_ANDROID_OS) && defined(USE_DLMALLOC) |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4608 | dlmalloc_inspect_all(HeapChunkContext::HeapChunkNativeCallback, &context); |
| 4609 | HeapChunkContext::HeapChunkNativeCallback(nullptr, nullptr, 0, &context); // Indicate end of a space. |
Christopher Ferris | c4ddc04 | 2014-05-13 14:47:50 -0700 | [diff] [blame] | 4610 | #else |
| 4611 | UNIMPLEMENTED(WARNING) << "Native heap inspection is only supported with dlmalloc"; |
| 4612 | #endif |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4613 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4614 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4615 | for (const auto& space : heap->GetContinuousSpaces()) { |
| 4616 | if (space->IsDlMallocSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4617 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4618 | // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an |
| 4619 | // allocation then the first sizeof(size_t) may belong to it. |
| 4620 | context.SetChunkOverhead(sizeof(size_t)); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4621 | space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4622 | } else if (space->IsRosAllocSpace()) { |
| 4623 | context.SetChunkOverhead(0); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4624 | // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since |
| 4625 | // RosAlloc's internal logic doesn't know to release and reacquire the heap bitmap lock. |
| 4626 | self->TransitionFromRunnableToSuspended(kSuspended); |
| 4627 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 4628 | tl->SuspendAll(__FUNCTION__); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4629 | { |
| 4630 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4631 | space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4632 | } |
| 4633 | tl->ResumeAll(); |
| 4634 | self->TransitionFromSuspendedToRunnable(); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4635 | } else if (space->IsBumpPointerSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4636 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4637 | context.SetChunkOverhead(0); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4638 | space->AsBumpPointerSpace()->Walk(BumpPointerSpaceCallback, &context); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4639 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4640 | } else if (space->IsRegionSpace()) { |
| 4641 | heap->IncrementDisableMovingGC(self); |
| 4642 | self->TransitionFromRunnableToSuspended(kSuspended); |
| 4643 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 4644 | tl->SuspendAll(__FUNCTION__); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4645 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 4646 | context.SetChunkOverhead(0); |
| 4647 | space->AsRegionSpace()->Walk(BumpPointerSpaceCallback, &context); |
| 4648 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
| 4649 | tl->ResumeAll(); |
| 4650 | self->TransitionFromSuspendedToRunnable(); |
| 4651 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4652 | } else { |
| 4653 | UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4654 | } |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4655 | context.ResetStartOfNextChunk(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4656 | } |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4657 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 4658 | // Walk the large objects, these are not in the AllocSpace. |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4659 | context.SetChunkOverhead(0); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4660 | heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4661 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4662 | |
| 4663 | // Finally, send a heap end chunk. |
| 4664 | 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] | 4665 | } |
| 4666 | |
Elliott Hughes | b1a5879 | 2013-07-11 18:10:58 -0700 | [diff] [blame] | 4667 | static size_t GetAllocTrackerMax() { |
| 4668 | #ifdef HAVE_ANDROID_OS |
| 4669 | // Check whether there's a system property overriding the number of records. |
| 4670 | const char* propertyName = "dalvik.vm.allocTrackerMax"; |
| 4671 | char allocRecordMaxString[PROPERTY_VALUE_MAX]; |
| 4672 | if (property_get(propertyName, allocRecordMaxString, "") > 0) { |
| 4673 | char* end; |
| 4674 | size_t value = strtoul(allocRecordMaxString, &end, 10); |
| 4675 | if (*end != '\0') { |
Ruben Brunk | 3e47a74 | 2013-09-09 17:56:07 -0700 | [diff] [blame] | 4676 | LOG(ERROR) << "Ignoring " << propertyName << " '" << allocRecordMaxString |
| 4677 | << "' --- invalid"; |
Elliott Hughes | b1a5879 | 2013-07-11 18:10:58 -0700 | [diff] [blame] | 4678 | return kDefaultNumAllocRecords; |
| 4679 | } |
| 4680 | if (!IsPowerOfTwo(value)) { |
Ruben Brunk | 3e47a74 | 2013-09-09 17:56:07 -0700 | [diff] [blame] | 4681 | LOG(ERROR) << "Ignoring " << propertyName << " '" << allocRecordMaxString |
| 4682 | << "' --- not power of two"; |
Elliott Hughes | b1a5879 | 2013-07-11 18:10:58 -0700 | [diff] [blame] | 4683 | return kDefaultNumAllocRecords; |
| 4684 | } |
| 4685 | return value; |
| 4686 | } |
| 4687 | #endif |
| 4688 | return kDefaultNumAllocRecords; |
| 4689 | } |
| 4690 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4691 | void Dbg::SetAllocTrackingEnabled(bool enable) { |
| 4692 | Thread* self = Thread::Current(); |
| 4693 | if (enable) { |
Sebastien Hertz | b98063a | 2014-03-26 10:57:20 +0100 | [diff] [blame] | 4694 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4695 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
| 4696 | if (recent_allocation_records_ != nullptr) { |
| 4697 | return; // Already enabled, bail. |
Sebastien Hertz | b98063a | 2014-03-26 10:57:20 +0100 | [diff] [blame] | 4698 | } |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4699 | alloc_record_max_ = GetAllocTrackerMax(); |
| 4700 | LOG(INFO) << "Enabling alloc tracker (" << alloc_record_max_ << " entries of " |
| 4701 | << kMaxAllocRecordStackDepth << " frames, taking " |
| 4702 | << PrettySize(sizeof(AllocRecord) * alloc_record_max_) << ")"; |
| 4703 | DCHECK_EQ(alloc_record_head_, 0U); |
| 4704 | DCHECK_EQ(alloc_record_count_, 0U); |
| 4705 | recent_allocation_records_ = new AllocRecord[alloc_record_max_]; |
| 4706 | CHECK(recent_allocation_records_ != nullptr); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4707 | } |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 4708 | Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4709 | } else { |
Sebastien Hertz | b98063a | 2014-03-26 10:57:20 +0100 | [diff] [blame] | 4710 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4711 | ScopedObjectAccess soa(self); // For type_cache_.Clear(); |
| 4712 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
| 4713 | if (recent_allocation_records_ == nullptr) { |
| 4714 | return; // Already disabled, bail. |
| 4715 | } |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 4716 | LOG(INFO) << "Disabling alloc tracker"; |
Sebastien Hertz | b98063a | 2014-03-26 10:57:20 +0100 | [diff] [blame] | 4717 | delete[] recent_allocation_records_; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4718 | recent_allocation_records_ = nullptr; |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4719 | alloc_record_head_ = 0; |
| 4720 | alloc_record_count_ = 0; |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 4721 | type_cache_.Clear(); |
Sebastien Hertz | b98063a | 2014-03-26 10:57:20 +0100 | [diff] [blame] | 4722 | } |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4723 | // If an allocation comes in before we uninstrument, we will safely drop it on the floor. |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 4724 | Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4725 | } |
| 4726 | } |
| 4727 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 4728 | struct AllocRecordStackVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 4729 | AllocRecordStackVisitor(Thread* thread, AllocRecord* record_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4730 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 4731 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 4732 | record(record_in), |
| 4733 | depth(0) {} |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4734 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4735 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 4736 | // annotalysis. |
| 4737 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4738 | if (depth >= kMaxAllocRecordStackDepth) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 4739 | return false; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4740 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4741 | ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 4742 | if (!m->IsRuntimeMethod()) { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4743 | record->StackElement(depth)->SetMethod(m); |
| 4744 | record->StackElement(depth)->SetDexPc(GetDexPc()); |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 4745 | ++depth; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4746 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 4747 | return true; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | ~AllocRecordStackVisitor() { |
| 4751 | // Clear out any unused stack trace elements. |
| 4752 | for (; depth < kMaxAllocRecordStackDepth; ++depth) { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4753 | record->StackElement(depth)->SetMethod(nullptr); |
| 4754 | record->StackElement(depth)->SetDexPc(0); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4755 | } |
| 4756 | } |
| 4757 | |
| 4758 | AllocRecord* record; |
| 4759 | size_t depth; |
| 4760 | }; |
| 4761 | |
Ian Rogers | 844506b | 2014-09-12 19:59:33 -0700 | [diff] [blame] | 4762 | void Dbg::RecordAllocation(Thread* self, mirror::Class* type, size_t byte_count) { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4763 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4764 | if (recent_allocation_records_ == nullptr) { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4765 | // In the process of shutting down recording, bail. |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4766 | return; |
| 4767 | } |
| 4768 | |
| 4769 | // Advance and clip. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4770 | if (++alloc_record_head_ == alloc_record_max_) { |
| 4771 | alloc_record_head_ = 0; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | // Fill in the basics. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4775 | AllocRecord* record = &recent_allocation_records_[alloc_record_head_]; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4776 | record->SetType(type); |
| 4777 | record->SetByteCount(byte_count); |
| 4778 | record->SetThinLockId(self->GetThreadId()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4779 | |
| 4780 | // Fill in the stack trace. |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 4781 | AllocRecordStackVisitor visitor(self, record); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 4782 | visitor.WalkStack(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4783 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4784 | if (alloc_record_count_ < alloc_record_max_) { |
| 4785 | ++alloc_record_count_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4786 | } |
| 4787 | } |
| 4788 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4789 | // Returns the index of the head element. |
| 4790 | // |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4791 | // We point at the most-recently-written record, so if alloc_record_count_ is 1 |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4792 | // we want to use the current element. Take "head+1" and subtract count |
| 4793 | // from it. |
| 4794 | // |
| 4795 | // We need to handle underflow in our circular buffer, so we add |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4796 | // alloc_record_max_ and then mask it back down. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4797 | size_t Dbg::HeadIndex() { |
| 4798 | return (Dbg::alloc_record_head_ + 1 + Dbg::alloc_record_max_ - Dbg::alloc_record_count_) & |
| 4799 | (Dbg::alloc_record_max_ - 1); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | void Dbg::DumpRecentAllocations() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4803 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4804 | MutexLock mu(soa.Self(), *Locks::alloc_tracker_lock_); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4805 | if (recent_allocation_records_ == nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4806 | LOG(INFO) << "Not recording tracked allocations"; |
| 4807 | return; |
| 4808 | } |
| 4809 | |
| 4810 | // "i" is the head of the list. We want to start at the end of the |
| 4811 | // list and move forward to the tail. |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4812 | size_t i = HeadIndex(); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4813 | const uint16_t capped_count = CappedAllocRecordCount(Dbg::alloc_record_count_); |
| 4814 | uint16_t count = capped_count; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4815 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4816 | LOG(INFO) << "Tracked allocations, (head=" << alloc_record_head_ << " count=" << count << ")"; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4817 | while (count--) { |
| 4818 | AllocRecord* record = &recent_allocation_records_[i]; |
| 4819 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4820 | LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->ThinLockId(), record->ByteCount()) |
| 4821 | << PrettyClass(record->Type()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4822 | |
| 4823 | for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4824 | AllocRecordStackTraceElement* stack_element = record->StackElement(stack_frame); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4825 | ArtMethod* m = stack_element->Method(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4826 | if (m == nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4827 | break; |
| 4828 | } |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4829 | LOG(INFO) << " " << PrettyMethod(m) << " line " << stack_element->LineNumber(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | // pause periodically to help logcat catch up |
| 4833 | if ((count % 5) == 0) { |
| 4834 | usleep(40000); |
| 4835 | } |
| 4836 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4837 | i = (i + 1) & (alloc_record_max_ - 1); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4838 | } |
| 4839 | } |
| 4840 | |
| 4841 | class StringTable { |
| 4842 | public: |
| 4843 | StringTable() { |
| 4844 | } |
| 4845 | |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 4846 | void Add(const std::string& str) { |
| 4847 | table_.insert(str); |
| 4848 | } |
| 4849 | |
| 4850 | void Add(const char* str) { |
| 4851 | table_.insert(str); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4852 | } |
| 4853 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4854 | size_t IndexOf(const char* s) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4855 | auto it = table_.find(s); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4856 | if (it == table_.end()) { |
| 4857 | LOG(FATAL) << "IndexOf(\"" << s << "\") failed"; |
| 4858 | } |
| 4859 | return std::distance(table_.begin(), it); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4860 | } |
| 4861 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4862 | size_t Size() const { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4863 | return table_.size(); |
| 4864 | } |
| 4865 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4866 | void WriteTo(std::vector<uint8_t>& bytes) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4867 | for (const std::string& str : table_) { |
| 4868 | const char* s = str.c_str(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4869 | size_t s_len = CountModifiedUtf8Chars(s); |
Christopher Ferris | 8a35405 | 2015-04-24 17:23:53 -0700 | [diff] [blame] | 4870 | std::unique_ptr<uint16_t[]> s_utf16(new uint16_t[s_len]); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4871 | ConvertModifiedUtf8ToUtf16(s_utf16.get(), s); |
| 4872 | JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4873 | } |
| 4874 | } |
| 4875 | |
| 4876 | private: |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4877 | std::set<std::string> table_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4878 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 4879 | }; |
| 4880 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4881 | static const char* GetMethodSourceFile(ArtMethod* method) |
Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 4882 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4883 | DCHECK(method != nullptr); |
| 4884 | const char* source_file = method->GetDeclaringClassSourceFile(); |
Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 4885 | return (source_file != nullptr) ? source_file : ""; |
| 4886 | } |
| 4887 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4888 | /* |
| 4889 | * The data we send to DDMS contains everything we have recorded. |
| 4890 | * |
| 4891 | * Message header (all values big-endian): |
| 4892 | * (1b) message header len (to allow future expansion); includes itself |
| 4893 | * (1b) entry header len |
| 4894 | * (1b) stack frame len |
| 4895 | * (2b) number of entries |
| 4896 | * (4b) offset to string table from start of message |
| 4897 | * (2b) number of class name strings |
| 4898 | * (2b) number of method name strings |
| 4899 | * (2b) number of source file name strings |
| 4900 | * For each entry: |
| 4901 | * (4b) total allocation size |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 4902 | * (2b) thread id |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4903 | * (2b) allocated object's class name index |
| 4904 | * (1b) stack depth |
| 4905 | * For each stack frame: |
| 4906 | * (2b) method's class name |
| 4907 | * (2b) method name |
| 4908 | * (2b) method source file |
| 4909 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 4910 | * (xb) class name strings |
| 4911 | * (xb) method name strings |
| 4912 | * (xb) source file strings |
| 4913 | * |
| 4914 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 4915 | * followed by UTF-16 data. |
| 4916 | * |
| 4917 | * We send up 16-bit unsigned indexes into string tables. In theory there |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4918 | * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4919 | * each table, but in practice there should be far fewer. |
| 4920 | * |
| 4921 | * The chief reason for using a string table here is to keep the size of |
| 4922 | * the DDMS message to a minimum. This is partly to make the protocol |
| 4923 | * efficient, but also because we have to form the whole thing up all at |
| 4924 | * once in a memory buffer. |
| 4925 | * |
| 4926 | * We use separate string tables for class names, method names, and source |
| 4927 | * files to keep the indexes small. There will generally be no overlap |
| 4928 | * between the contents of these tables. |
| 4929 | */ |
| 4930 | jbyteArray Dbg::GetRecentAllocations() { |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 4931 | if ((false)) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4932 | DumpRecentAllocations(); |
| 4933 | } |
| 4934 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4935 | Thread* self = Thread::Current(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4936 | std::vector<uint8_t> bytes; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4937 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4938 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4939 | // |
| 4940 | // Part 1: generate string tables. |
| 4941 | // |
| 4942 | StringTable class_names; |
| 4943 | StringTable method_names; |
| 4944 | StringTable filenames; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4945 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4946 | const uint16_t capped_count = CappedAllocRecordCount(Dbg::alloc_record_count_); |
| 4947 | uint16_t count = capped_count; |
| 4948 | size_t idx = HeadIndex(); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4949 | while (count--) { |
| 4950 | AllocRecord* record = &recent_allocation_records_[idx]; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4951 | std::string temp; |
| 4952 | class_names.Add(record->Type()->GetDescriptor(&temp)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4953 | for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4954 | ArtMethod* m = record->StackElement(i)->Method(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4955 | if (m != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4956 | class_names.Add(m->GetDeclaringClassDescriptor()); |
| 4957 | method_names.Add(m->GetName()); |
| 4958 | filenames.Add(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4959 | } |
| 4960 | } |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4961 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4962 | idx = (idx + 1) & (alloc_record_max_ - 1); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4963 | } |
| 4964 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4965 | LOG(INFO) << "allocation records: " << capped_count; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4966 | |
| 4967 | // |
| 4968 | // Part 2: Generate the output and store it in the buffer. |
| 4969 | // |
| 4970 | |
| 4971 | // (1b) message header len (to allow future expansion); includes itself |
| 4972 | // (1b) entry header len |
| 4973 | // (1b) stack frame len |
| 4974 | const int kMessageHeaderLen = 15; |
| 4975 | const int kEntryHeaderLen = 9; |
| 4976 | const int kStackFrameLen = 8; |
| 4977 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 4978 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 4979 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 4980 | |
| 4981 | // (2b) number of entries |
| 4982 | // (4b) offset to string table from start of message |
| 4983 | // (2b) number of class name strings |
| 4984 | // (2b) number of method name strings |
| 4985 | // (2b) number of source file name strings |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4986 | JDWP::Append2BE(bytes, capped_count); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4987 | size_t string_table_offset = bytes.size(); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4988 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4989 | JDWP::Append2BE(bytes, class_names.Size()); |
| 4990 | JDWP::Append2BE(bytes, method_names.Size()); |
| 4991 | JDWP::Append2BE(bytes, filenames.Size()); |
| 4992 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4993 | idx = HeadIndex(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4994 | std::string temp; |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4995 | for (count = capped_count; count != 0; --count) { |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4996 | // For each entry: |
| 4997 | // (4b) total allocation size |
| 4998 | // (2b) thread id |
| 4999 | // (2b) allocated object's class name index |
| 5000 | // (1b) stack depth |
| 5001 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 5002 | size_t stack_depth = record->GetDepth(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5003 | size_t allocated_object_class_name_index = |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5004 | class_names.IndexOf(record->Type()->GetDescriptor(&temp)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 5005 | JDWP::Append4BE(bytes, record->ByteCount()); |
| 5006 | JDWP::Append2BE(bytes, record->ThinLockId()); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5007 | JDWP::Append2BE(bytes, allocated_object_class_name_index); |
| 5008 | JDWP::Append1BE(bytes, stack_depth); |
| 5009 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5010 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 5011 | // For each stack frame: |
| 5012 | // (2b) method's class name |
| 5013 | // (2b) method name |
| 5014 | // (2b) method source file |
| 5015 | // (2b) line number, clipped to 32767; -2 if native; -1 if no source |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5016 | ArtMethod* m = record->StackElement(stack_frame)->Method(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5017 | size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor()); |
| 5018 | size_t method_name_index = method_names.IndexOf(m->GetName()); |
| 5019 | size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5020 | JDWP::Append2BE(bytes, class_name_index); |
| 5021 | JDWP::Append2BE(bytes, method_name_index); |
| 5022 | JDWP::Append2BE(bytes, file_name_index); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 5023 | JDWP::Append2BE(bytes, record->StackElement(stack_frame)->LineNumber()); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5024 | } |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 5025 | idx = (idx + 1) & (alloc_record_max_ - 1); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | // (xb) class name strings |
| 5029 | // (xb) method name strings |
| 5030 | // (xb) source file strings |
| 5031 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 5032 | class_names.WriteTo(bytes); |
| 5033 | method_names.WriteTo(bytes); |
| 5034 | filenames.WriteTo(bytes); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5035 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 5036 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5037 | jbyteArray result = env->NewByteArray(bytes.size()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 5038 | if (result != nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5039 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 5040 | } |
| 5041 | return result; |
| 5042 | } |
| 5043 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5044 | ArtMethod* DeoptimizationRequest::Method() const { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 5045 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 5046 | return soa.DecodeMethod(method_); |
| 5047 | } |
| 5048 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5049 | void DeoptimizationRequest::SetMethod(ArtMethod* m) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 5050 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 5051 | method_ = soa.EncodeMethod(m); |
| 5052 | } |
| 5053 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 5054 | } // namespace art |