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 | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "art_method-inl.h" |
Vladimir Marko | 41b175a | 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" |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 35 | #include "jdwp/jdwp_priv.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 36 | #include "jdwp/object_registry.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "mirror/class.h" |
| 38 | #include "mirror/class-inl.h" |
| 39 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | #include "mirror/object-inl.h" |
| 41 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 42 | #include "mirror/string-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | #include "mirror/throwable.h" |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 44 | #include "quick/inline_method_analyser.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 45 | #include "reflection.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 46 | #include "safe_map.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 47 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 48 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 49 | #include "ScopedPrimitiveArray.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 50 | #include "handle_scope-inl.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 51 | #include "thread_list.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | #include "utf.h" |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 53 | #include "verifier/method_verifier-inl.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 54 | #include "well_known_classes.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 55 | |
Brian Carlstrom | 3d92d52 | 2013-07-12 09:03:08 -0700 | [diff] [blame] | 56 | #ifdef HAVE_ANDROID_OS |
| 57 | #include "cutils/properties.h" |
| 58 | #endif |
| 59 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 60 | namespace art { |
| 61 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 62 | // The key identifying the debugger to update instrumentation. |
| 63 | static constexpr const char* kDbgInstrumentationKey = "Debugger"; |
| 64 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 65 | static const size_t kMaxAllocRecordStackDepth = 16; // Max 255. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 66 | static const size_t kDefaultNumAllocRecords = 64*1024; // Must be a power of 2. 2BE can hold 64k-1. |
| 67 | |
| 68 | // Limit alloc_record_count to the 2BE value that is the limit of the current protocol. |
| 69 | static uint16_t CappedAllocRecordCount(size_t alloc_record_count) { |
| 70 | if (alloc_record_count > 0xffff) { |
| 71 | return 0xffff; |
| 72 | } |
| 73 | return alloc_record_count; |
| 74 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 75 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 76 | class AllocRecordStackTraceElement { |
| 77 | public: |
| 78 | AllocRecordStackTraceElement() : method_(nullptr), dex_pc_(0) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 81 | int32_t LineNumber() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 82 | ArtMethod* method = Method(); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 83 | DCHECK(method != nullptr); |
| 84 | return method->GetLineNumFromDexPC(DexPc()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 85 | } |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 86 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 87 | ArtMethod* Method() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 88 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 89 | return soa.DecodeMethod(method_); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 92 | void SetMethod(ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 93 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 94 | method_ = soa.EncodeMethod(m); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | uint32_t DexPc() const { |
| 98 | return dex_pc_; |
| 99 | } |
| 100 | |
| 101 | void SetDexPc(uint32_t pc) { |
| 102 | dex_pc_ = pc; |
| 103 | } |
| 104 | |
| 105 | private: |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 106 | jmethodID method_; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 107 | uint32_t dex_pc_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 110 | jobject Dbg::TypeCache::Add(mirror::Class* t) { |
| 111 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | 4c4d609 | 2015-01-22 17:02:27 -0800 | [diff] [blame] | 112 | JNIEnv* const env = soa.Env(); |
| 113 | ScopedLocalRef<jobject> local_ref(soa.Env(), soa.AddLocalReference<jobject>(t)); |
| 114 | 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] | 115 | auto range = objects_.equal_range(hash_code); |
| 116 | for (auto it = range.first; it != range.second; ++it) { |
Mathieu Chartier | 4c4d609 | 2015-01-22 17:02:27 -0800 | [diff] [blame] | 117 | 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] | 118 | // Found a matching weak global, return it. |
| 119 | return it->second; |
| 120 | } |
| 121 | } |
Mathieu Chartier | 4c4d609 | 2015-01-22 17:02:27 -0800 | [diff] [blame] | 122 | const jobject weak_global = env->NewWeakGlobalRef(local_ref.get()); |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 123 | objects_.insert(std::make_pair(hash_code, weak_global)); |
| 124 | return weak_global; |
| 125 | } |
| 126 | |
| 127 | void Dbg::TypeCache::Clear() { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 128 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 129 | Thread* self = Thread::Current(); |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 130 | for (const auto& p : objects_) { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 131 | vm->DeleteWeakGlobalRef(self, p.second); |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 132 | } |
| 133 | objects_.clear(); |
| 134 | } |
| 135 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 136 | class AllocRecord { |
| 137 | public: |
| 138 | AllocRecord() : type_(nullptr), byte_count_(0), thin_lock_id_(0) {} |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 139 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 140 | mirror::Class* Type() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 141 | return down_cast<mirror::Class*>(Thread::Current()->DecodeJObject(type_)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 144 | void SetType(mirror::Class* t) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, |
| 145 | Locks::alloc_tracker_lock_) { |
| 146 | type_ = Dbg::type_cache_.Add(t); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | size_t GetDepth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 150 | size_t depth = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 151 | while (depth < kMaxAllocRecordStackDepth && stack_[depth].Method() != nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 152 | ++depth; |
| 153 | } |
| 154 | return depth; |
| 155 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 156 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 157 | size_t ByteCount() const { |
| 158 | return byte_count_; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 159 | } |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 160 | |
| 161 | void SetByteCount(size_t count) { |
| 162 | byte_count_ = count; |
| 163 | } |
| 164 | |
| 165 | uint16_t ThinLockId() const { |
| 166 | return thin_lock_id_; |
| 167 | } |
| 168 | |
| 169 | void SetThinLockId(uint16_t id) { |
| 170 | thin_lock_id_ = id; |
| 171 | } |
| 172 | |
| 173 | AllocRecordStackTraceElement* StackElement(size_t index) { |
| 174 | DCHECK_LT(index, kMaxAllocRecordStackDepth); |
| 175 | return &stack_[index]; |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | jobject type_; // This is a weak global. |
| 180 | size_t byte_count_; |
| 181 | uint16_t thin_lock_id_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 182 | // Unused entries have null method. |
| 183 | AllocRecordStackTraceElement stack_[kMaxAllocRecordStackDepth]; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 184 | }; |
| 185 | |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 186 | class Breakpoint { |
| 187 | public: |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 188 | Breakpoint(ArtMethod* method, uint32_t dex_pc, |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 189 | DeoptimizationRequest::Kind deoptimization_kind) |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 190 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 191 | : method_(nullptr), dex_pc_(dex_pc), deoptimization_kind_(deoptimization_kind) { |
| 192 | CHECK(deoptimization_kind_ == DeoptimizationRequest::kNothing || |
| 193 | deoptimization_kind_ == DeoptimizationRequest::kSelectiveDeoptimization || |
| 194 | deoptimization_kind_ == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 195 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 196 | method_ = soa.EncodeMethod(method); |
| 197 | } |
| 198 | |
| 199 | Breakpoint(const Breakpoint& other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 200 | : method_(nullptr), dex_pc_(other.dex_pc_), |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 201 | deoptimization_kind_(other.deoptimization_kind_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 202 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 203 | method_ = soa.EncodeMethod(other.Method()); |
| 204 | } |
| 205 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 206 | ArtMethod* Method() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 207 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 208 | return soa.DecodeMethod(method_); |
| 209 | } |
| 210 | |
| 211 | uint32_t DexPc() const { |
| 212 | return dex_pc_; |
| 213 | } |
| 214 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 215 | DeoptimizationRequest::Kind GetDeoptimizationKind() const { |
| 216 | return deoptimization_kind_; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | private: |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 220 | // The location of this breakpoint. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 221 | jmethodID method_; |
| 222 | uint32_t dex_pc_; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 223 | |
| 224 | // Indicates whether breakpoint needs full deoptimization or selective deoptimization. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 225 | DeoptimizationRequest::Kind deoptimization_kind_; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 226 | }; |
| 227 | |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 228 | static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 229 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 230 | os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.Method()).c_str(), rhs.DexPc()); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 231 | return os; |
| 232 | } |
| 233 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 234 | class DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 235 | public: |
| 236 | DebugInstrumentationListener() {} |
| 237 | virtual ~DebugInstrumentationListener() {} |
| 238 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 239 | void MethodEntered(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 240 | uint32_t dex_pc) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 241 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 242 | if (method->IsNative()) { |
| 243 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 244 | return; |
| 245 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 246 | if (IsListeningToDexPcMoved()) { |
| 247 | // We also listen to kDexPcMoved instrumentation event so we know the DexPcMoved method is |
| 248 | // going to be called right after us. To avoid sending JDWP events twice for this location, |
| 249 | // we report the event in DexPcMoved. However, we must remind this is method entry so we |
| 250 | // send the METHOD_ENTRY event. And we can also group it with other events for this location |
| 251 | // like BREAKPOINT or SINGLE_STEP (or even METHOD_EXIT if this is a RETURN instruction). |
| 252 | thread->SetDebugMethodEntry(); |
| 253 | } else if (IsListeningToMethodExit() && IsReturn(method, dex_pc)) { |
| 254 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 255 | // RETURN so we know the MethodExited method is going to be called right after us. To avoid |
| 256 | // sending JDWP events twice for this location, we report the event(s) in MethodExited. |
| 257 | // However, we must remind this is method entry so we send the METHOD_ENTRY event. And we can |
| 258 | // also group it with other events for this location like BREAKPOINT or SINGLE_STEP. |
| 259 | thread->SetDebugMethodEntry(); |
| 260 | } else { |
| 261 | Dbg::UpdateDebugger(thread, this_object, method, 0, Dbg::kMethodEntry, nullptr); |
| 262 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 265 | void MethodExited(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 266 | uint32_t dex_pc, const JValue& return_value) |
| 267 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 268 | if (method->IsNative()) { |
| 269 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 270 | return; |
| 271 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 272 | uint32_t events = Dbg::kMethodExit; |
| 273 | if (thread->IsDebugMethodEntry()) { |
| 274 | // It is also the method entry. |
| 275 | DCHECK(IsReturn(method, dex_pc)); |
| 276 | events |= Dbg::kMethodEntry; |
| 277 | thread->ClearDebugMethodEntry(); |
| 278 | } |
| 279 | Dbg::UpdateDebugger(thread, this_object, method, dex_pc, events, &return_value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 282 | void MethodUnwind(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 283 | ArtMethod* method, uint32_t dex_pc) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 284 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 285 | // We're not recorded to listen to this kind of event, so complain. |
| 286 | LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method) |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 287 | << " " << dex_pc; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 290 | void DexPcMoved(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 291 | uint32_t new_dex_pc) |
| 292 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 293 | if (IsListeningToMethodExit() && IsReturn(method, new_dex_pc)) { |
| 294 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 295 | // RETURN so we know the MethodExited method is going to be called right after us. Like in |
| 296 | // MethodEntered, we delegate event reporting to MethodExited. |
| 297 | // Besides, if this RETURN instruction is the only one in the method, we can send multiple |
| 298 | // JDWP events in the same packet: METHOD_ENTRY, METHOD_EXIT, BREAKPOINT and/or SINGLE_STEP. |
| 299 | // Therefore, we must not clear the debug method entry flag here. |
| 300 | } else { |
| 301 | uint32_t events = 0; |
| 302 | if (thread->IsDebugMethodEntry()) { |
| 303 | // It is also the method entry. |
| 304 | events = Dbg::kMethodEntry; |
| 305 | thread->ClearDebugMethodEntry(); |
| 306 | } |
| 307 | Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc, events, nullptr); |
| 308 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 311 | void FieldRead(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 312 | ArtMethod* method, uint32_t dex_pc, ArtField* field) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 313 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 314 | Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 315 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 316 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 317 | void FieldWritten(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 318 | ArtMethod* method, uint32_t dex_pc, ArtField* field, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 319 | const JValue& field_value) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 320 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 321 | Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value); |
| 322 | } |
| 323 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 324 | void ExceptionCaught(Thread* thread ATTRIBUTE_UNUSED, mirror::Throwable* exception_object) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 325 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 326 | Dbg::PostException(exception_object); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 329 | // We only care about how many backward branches were executed in the Jit. |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 330 | void BackwardBranch(Thread* /*thread*/, ArtMethod* method, int32_t dex_pc_offset) |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 331 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 332 | LOG(ERROR) << "Unexpected backward branch event in debugger " << PrettyMethod(method) |
| 333 | << " " << dex_pc_offset; |
| 334 | } |
| 335 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 336 | private: |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 337 | static bool IsReturn(ArtMethod* method, uint32_t dex_pc) |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 338 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 339 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
| 340 | const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]); |
| 341 | return instruction->IsReturn(); |
| 342 | } |
| 343 | |
| 344 | static bool IsListeningToDexPcMoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 345 | return IsListeningTo(instrumentation::Instrumentation::kDexPcMoved); |
| 346 | } |
| 347 | |
| 348 | static bool IsListeningToMethodExit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 349 | return IsListeningTo(instrumentation::Instrumentation::kMethodExited); |
| 350 | } |
| 351 | |
| 352 | static bool IsListeningTo(instrumentation::Instrumentation::InstrumentationEvent event) |
| 353 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 354 | return (Dbg::GetInstrumentationEvents() & event) != 0; |
| 355 | } |
| 356 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 357 | DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 358 | } gDebugInstrumentationListener; |
| 359 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 360 | // JDWP is allowed unless the Zygote forbids it. |
| 361 | static bool gJdwpAllowed = true; |
| 362 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 363 | // Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line? |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 364 | static bool gJdwpConfigured = false; |
| 365 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 366 | // JDWP options for debugging. Only valid if IsJdwpConfigured() is true. |
| 367 | static JDWP::JdwpOptions gJdwpOptions; |
| 368 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 369 | // Runtime JDWP state. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 370 | static JDWP::JdwpState* gJdwpState = nullptr; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 371 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 372 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 373 | static bool gDdmThreadNotification = false; |
| 374 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 375 | // DDMS GC-related settings. |
| 376 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 377 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 378 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 379 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 380 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 381 | |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 382 | bool Dbg::gDebuggerActive = false; |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 383 | bool Dbg::gDisposed = false; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 384 | ObjectRegistry* Dbg::gRegistry = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 385 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 386 | // Recent allocation tracking. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 387 | AllocRecord* Dbg::recent_allocation_records_ = nullptr; // TODO: CircularBuffer<AllocRecord> |
| 388 | size_t Dbg::alloc_record_max_ = 0; |
| 389 | size_t Dbg::alloc_record_head_ = 0; |
| 390 | size_t Dbg::alloc_record_count_ = 0; |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 391 | Dbg::TypeCache Dbg::type_cache_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 392 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 393 | // Deoptimization support. |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 394 | std::vector<DeoptimizationRequest> Dbg::deoptimization_requests_; |
| 395 | size_t Dbg::full_deoptimization_event_count_ = 0; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 396 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 397 | // Instrumentation event reference counters. |
| 398 | size_t Dbg::dex_pc_change_event_ref_count_ = 0; |
| 399 | size_t Dbg::method_enter_event_ref_count_ = 0; |
| 400 | size_t Dbg::method_exit_event_ref_count_ = 0; |
| 401 | size_t Dbg::field_read_event_ref_count_ = 0; |
| 402 | size_t Dbg::field_write_event_ref_count_ = 0; |
| 403 | size_t Dbg::exception_catch_event_ref_count_ = 0; |
| 404 | uint32_t Dbg::instrumentation_events_ = 0; |
| 405 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 406 | // Breakpoints. |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 407 | static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 408 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 409 | void DebugInvokeReq::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 410 | receiver.VisitRootIfNonNull(visitor, root_info); // null for static method call. |
| 411 | klass.VisitRoot(visitor, root_info); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 414 | void SingleStepControl::AddDexPc(uint32_t dex_pc) { |
| 415 | dex_pcs_.insert(dex_pc); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 416 | } |
| 417 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 418 | bool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const { |
| 419 | return dex_pcs_.find(dex_pc) == dex_pcs_.end(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 422 | static bool IsBreakpoint(const ArtMethod* m, uint32_t dex_pc) |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 423 | LOCKS_EXCLUDED(Locks::breakpoint_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 424 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 425 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 426 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 427 | if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].Method() == m) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 428 | VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i]; |
| 429 | return true; |
| 430 | } |
| 431 | } |
| 432 | return false; |
| 433 | } |
| 434 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 435 | static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) |
| 436 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) { |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 437 | MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); |
| 438 | // A thread may be suspended for GC; in this code, we really want to know whether |
| 439 | // there's a debugger suspension active. |
| 440 | return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0; |
| 441 | } |
| 442 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 443 | static mirror::Array* DecodeNonNullArray(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 444 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 445 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 446 | if (o == nullptr) { |
| 447 | *error = JDWP::ERR_INVALID_OBJECT; |
| 448 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 449 | } |
| 450 | if (!o->IsArrayInstance()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 451 | *error = JDWP::ERR_INVALID_ARRAY; |
| 452 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 453 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 454 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 455 | return o->AsArray(); |
| 456 | } |
| 457 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 458 | static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 459 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 460 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 461 | if (o == nullptr) { |
| 462 | *error = JDWP::ERR_INVALID_OBJECT; |
| 463 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 464 | } |
| 465 | if (!o->IsClass()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 466 | *error = JDWP::ERR_INVALID_CLASS; |
| 467 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 468 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 469 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 470 | return o->AsClass(); |
| 471 | } |
| 472 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 473 | static Thread* DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, |
| 474 | JDWP::JdwpError* error) |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 475 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 476 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::thread_suspend_count_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 477 | mirror::Object* thread_peer = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 478 | if (thread_peer == nullptr) { |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 479 | // This isn't even an object. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 480 | *error = JDWP::ERR_INVALID_OBJECT; |
| 481 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 482 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 483 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 484 | 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] | 485 | if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) { |
| 486 | // This isn't a thread. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 487 | *error = JDWP::ERR_INVALID_THREAD; |
| 488 | return nullptr; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 491 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 492 | Thread* thread = Thread::FromManagedThread(soa, thread_peer); |
| 493 | // If thread is null then this a java.lang.Thread without a Thread*. Must be a un-started or a |
| 494 | // zombie. |
| 495 | *error = (thread == nullptr) ? JDWP::ERR_THREAD_NOT_ALIVE : JDWP::ERR_NONE; |
| 496 | return thread; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 499 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 500 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 501 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 502 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 503 | } |
| 504 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 505 | static JDWP::JdwpTag BasicTagFromClass(mirror::Class* klass) |
| 506 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 507 | std::string temp; |
| 508 | const char* descriptor = klass->GetDescriptor(&temp); |
| 509 | return BasicTagFromDescriptor(descriptor); |
| 510 | } |
| 511 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 512 | static JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 513 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 514 | CHECK(c != nullptr); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 515 | if (c->IsArrayClass()) { |
| 516 | return JDWP::JT_ARRAY; |
| 517 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 518 | if (c->IsStringClass()) { |
| 519 | return JDWP::JT_STRING; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 520 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 521 | if (c->IsClassClass()) { |
| 522 | return JDWP::JT_CLASS_OBJECT; |
| 523 | } |
| 524 | { |
| 525 | mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
| 526 | if (thread_class->IsAssignableFrom(c)) { |
| 527 | return JDWP::JT_THREAD; |
| 528 | } |
| 529 | } |
| 530 | { |
| 531 | mirror::Class* thread_group_class = |
| 532 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup); |
| 533 | if (thread_group_class->IsAssignableFrom(c)) { |
| 534 | return JDWP::JT_THREAD_GROUP; |
| 535 | } |
| 536 | } |
| 537 | { |
| 538 | mirror::Class* class_loader_class = |
| 539 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader); |
| 540 | if (class_loader_class->IsAssignableFrom(c)) { |
| 541 | return JDWP::JT_CLASS_LOADER; |
| 542 | } |
| 543 | } |
| 544 | return JDWP::JT_OBJECT; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Objects declared to hold Object might actually hold a more specific |
| 549 | * type. The debugger may take a special interest in these (e.g. it |
| 550 | * wants to display the contents of Strings), so we want to return an |
| 551 | * appropriate tag. |
| 552 | * |
| 553 | * Null objects are tagged JT_OBJECT. |
| 554 | */ |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 555 | JDWP::JdwpTag Dbg::TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 556 | return (o == nullptr) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 560 | switch (tag) { |
| 561 | case JDWP::JT_BOOLEAN: |
| 562 | case JDWP::JT_BYTE: |
| 563 | case JDWP::JT_CHAR: |
| 564 | case JDWP::JT_FLOAT: |
| 565 | case JDWP::JT_DOUBLE: |
| 566 | case JDWP::JT_INT: |
| 567 | case JDWP::JT_LONG: |
| 568 | case JDWP::JT_SHORT: |
| 569 | case JDWP::JT_VOID: |
| 570 | return true; |
| 571 | default: |
| 572 | return false; |
| 573 | } |
| 574 | } |
| 575 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 576 | void Dbg::StartJdwp() { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 577 | if (!gJdwpAllowed || !IsJdwpConfigured()) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 578 | // No JDWP for you! |
| 579 | return; |
| 580 | } |
| 581 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 582 | CHECK(gRegistry == nullptr); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 583 | gRegistry = new ObjectRegistry; |
| 584 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 585 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 586 | // debugger, passively listen for a debugger, or block waiting for a |
| 587 | // debugger. |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 588 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 589 | if (gJdwpState == nullptr) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 590 | // We probably failed because some other process has the port already, which means that |
| 591 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 592 | // talking to that other process. |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 593 | LOG(FATAL) << "Debugger thread failed to initialize"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | // If a debugger has already attached, send the "welcome" message. |
| 597 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 598 | if (gJdwpState->IsActive()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 599 | ScopedObjectAccess soa(Thread::Current()); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 600 | gJdwpState->PostVMStart(); |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 601 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 604 | void Dbg::StopJdwp() { |
Sebastien Hertz | c6345ef | 2014-08-18 19:26:39 +0200 | [diff] [blame] | 605 | // Post VM_DEATH event before the JDWP connection is closed (either by the JDWP thread or the |
| 606 | // destruction of gJdwpState). |
| 607 | if (gJdwpState != nullptr && gJdwpState->IsActive()) { |
| 608 | gJdwpState->PostVMDeath(); |
| 609 | } |
Sebastien Hertz | 0376e6b | 2014-02-06 18:12:59 +0100 | [diff] [blame] | 610 | // 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] | 611 | Dispose(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 612 | delete gJdwpState; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 613 | gJdwpState = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 614 | delete gRegistry; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 615 | gRegistry = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 618 | void Dbg::GcDidFinish() { |
| 619 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 620 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 621 | VLOG(jdwp) << "Sending heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 622 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 623 | } |
| 624 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 625 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 626 | VLOG(jdwp) << "Dumping heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 627 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 628 | } |
| 629 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 630 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 631 | VLOG(jdwp) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 632 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 636 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 637 | gJdwpAllowed = allowed; |
| 638 | } |
| 639 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 640 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 641 | return Thread::Current()->GetInvokeReq(); |
| 642 | } |
| 643 | |
| 644 | Thread* Dbg::GetDebugThread() { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 645 | return (gJdwpState != nullptr) ? gJdwpState->GetDebugThread() : nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | void Dbg::ClearWaitForEventThread() { |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 649 | gJdwpState->ReleaseJdwpTokenForEvent(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 653 | CHECK(!gDebuggerConnected); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 654 | VLOG(jdwp) << "JDWP has attached"; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 655 | gDebuggerConnected = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 656 | gDisposed = false; |
| 657 | } |
| 658 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 659 | bool Dbg::RequiresDeoptimization() { |
| 660 | // We don't need deoptimization if everything runs with interpreter after |
| 661 | // enabling -Xint mode. |
| 662 | return !Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly(); |
| 663 | } |
| 664 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 665 | void Dbg::GoActive() { |
| 666 | // Enable all debugging features, including scans for breakpoints. |
| 667 | // This is a no-op if we're already active. |
| 668 | // Only called from the JDWP handler thread. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 669 | if (IsDebuggerActive()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 670 | return; |
| 671 | } |
| 672 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 673 | { |
| 674 | // 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] | 675 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 676 | CHECK_EQ(gBreakpoints.size(), 0U); |
| 677 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 678 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 679 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 680 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 681 | CHECK_EQ(deoptimization_requests_.size(), 0U); |
| 682 | CHECK_EQ(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 683 | CHECK_EQ(dex_pc_change_event_ref_count_, 0U); |
| 684 | CHECK_EQ(method_enter_event_ref_count_, 0U); |
| 685 | CHECK_EQ(method_exit_event_ref_count_, 0U); |
| 686 | CHECK_EQ(field_read_event_ref_count_, 0U); |
| 687 | CHECK_EQ(field_write_event_ref_count_, 0U); |
| 688 | CHECK_EQ(exception_catch_event_ref_count_, 0U); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 691 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 692 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 693 | Thread* self = Thread::Current(); |
| 694 | ThreadState old_state = self->SetStateUnsafe(kRunnable); |
| 695 | CHECK_NE(old_state, kRunnable); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 696 | if (RequiresDeoptimization()) { |
| 697 | runtime->GetInstrumentation()->EnableDeoptimization(); |
| 698 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 699 | instrumentation_events_ = 0; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 700 | gDebuggerActive = true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 701 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 702 | runtime->GetThreadList()->ResumeAll(); |
| 703 | |
| 704 | LOG(INFO) << "Debugger is active"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 708 | CHECK(gDebuggerConnected); |
| 709 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 710 | LOG(INFO) << "Debugger is no longer active"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 711 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 712 | // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread |
| 713 | // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener |
| 714 | // and clear the object registry. |
| 715 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 716 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 717 | Thread* self = Thread::Current(); |
| 718 | ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 719 | |
| 720 | // Debugger may not be active at this point. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 721 | if (IsDebuggerActive()) { |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 722 | { |
| 723 | // Since we're going to disable deoptimization, we clear the deoptimization requests queue. |
| 724 | // This prevents us from having any pending deoptimization request when the debugger attaches |
| 725 | // to us again while no event has been requested yet. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 726 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 727 | deoptimization_requests_.clear(); |
| 728 | full_deoptimization_event_count_ = 0U; |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 729 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 730 | if (instrumentation_events_ != 0) { |
| 731 | runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener, |
| 732 | instrumentation_events_); |
| 733 | instrumentation_events_ = 0; |
| 734 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 735 | if (RequiresDeoptimization()) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 736 | runtime->GetInstrumentation()->DisableDeoptimization(kDbgInstrumentationKey); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 737 | } |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 738 | gDebuggerActive = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 739 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 740 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 741 | runtime->GetThreadList()->ResumeAll(); |
Sebastien Hertz | 55f6534 | 2015-01-13 22:48:34 +0100 | [diff] [blame] | 742 | |
| 743 | { |
| 744 | ScopedObjectAccess soa(self); |
| 745 | gRegistry->Clear(); |
| 746 | } |
| 747 | |
| 748 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 751 | void Dbg::ConfigureJdwp(const JDWP::JdwpOptions& jdwp_options) { |
| 752 | CHECK_NE(jdwp_options.transport, JDWP::kJdwpTransportUnknown); |
| 753 | gJdwpOptions = jdwp_options; |
| 754 | gJdwpConfigured = true; |
| 755 | } |
| 756 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 757 | bool Dbg::IsJdwpConfigured() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 758 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | int64_t Dbg::LastDebuggerActivity() { |
Elliott Hughes | ca95152 | 2011-12-05 12:01:32 -0800 | [diff] [blame] | 762 | return gJdwpState->LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 765 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 766 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 769 | std::string Dbg::GetClassName(JDWP::RefTypeId class_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 770 | JDWP::JdwpError error; |
| 771 | mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id, &error); |
| 772 | if (o == nullptr) { |
| 773 | if (error == JDWP::ERR_NONE) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 774 | return "null"; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 775 | } else { |
| 776 | return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id)); |
| 777 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 778 | } |
| 779 | if (!o->IsClass()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 780 | 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] | 781 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 782 | return GetClassName(o->AsClass()); |
| 783 | } |
| 784 | |
| 785 | std::string Dbg::GetClassName(mirror::Class* klass) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 786 | if (klass == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 787 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 788 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 789 | std::string temp; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 790 | return DescriptorToName(klass->GetDescriptor(&temp)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 793 | JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId* class_object_id) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 794 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 795 | mirror::Class* c = DecodeClass(id, &status); |
| 796 | if (c == nullptr) { |
| 797 | *class_object_id = 0; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 798 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 799 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 800 | *class_object_id = gRegistry->Add(c); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 801 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 804 | JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId* superclass_id) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 805 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 806 | mirror::Class* c = DecodeClass(id, &status); |
| 807 | if (c == nullptr) { |
| 808 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 809 | return status; |
| 810 | } |
| 811 | if (c->IsInterface()) { |
| 812 | // http://code.google.com/p/android/issues/detail?id=20856 |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 813 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 814 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 815 | *superclass_id = gRegistry->Add(c->GetSuperClass()); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 816 | } |
| 817 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 820 | JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 821 | JDWP::JdwpError error; |
| 822 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 823 | if (o == nullptr) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 824 | return JDWP::ERR_INVALID_OBJECT; |
| 825 | } |
| 826 | expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader())); |
| 827 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 830 | JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 831 | JDWP::JdwpError error; |
| 832 | mirror::Class* c = DecodeClass(id, &error); |
| 833 | if (c == nullptr) { |
| 834 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 835 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 836 | |
| 837 | uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask; |
| 838 | |
Yevgeny Rouban | de34eea | 2014-02-15 01:06:03 +0700 | [diff] [blame] | 839 | // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set, |
| 840 | // not interfaces. |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 841 | // 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] | 842 | if ((access_flags & kAccInterface) == 0) { |
| 843 | access_flags |= kAccSuper; |
| 844 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 845 | |
| 846 | expandBufAdd4BE(pReply, access_flags); |
| 847 | |
| 848 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 851 | JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) { |
| 852 | JDWP::JdwpError error; |
| 853 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 854 | if (o == nullptr) { |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 855 | return JDWP::ERR_INVALID_OBJECT; |
| 856 | } |
| 857 | |
| 858 | // Ensure all threads are suspended while we read objects' lock words. |
| 859 | Thread* self = Thread::Current(); |
Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 860 | CHECK_EQ(self->GetState(), kRunnable); |
| 861 | self->TransitionFromRunnableToSuspended(kSuspended); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 862 | Runtime::Current()->GetThreadList()->SuspendAll(__FUNCTION__); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 863 | |
| 864 | MonitorInfo monitor_info(o); |
| 865 | |
Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 866 | Runtime::Current()->GetThreadList()->ResumeAll(); |
| 867 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 868 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 869 | if (monitor_info.owner_ != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 870 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 871 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 872 | expandBufAddObjectId(reply, gRegistry->Add(nullptr)); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 873 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 874 | expandBufAdd4BE(reply, monitor_info.entry_count_); |
| 875 | expandBufAdd4BE(reply, monitor_info.waiters_.size()); |
| 876 | for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) { |
| 877 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 878 | } |
| 879 | return JDWP::ERR_NONE; |
| 880 | } |
| 881 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 882 | JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 883 | std::vector<JDWP::ObjectId>* monitors, |
| 884 | std::vector<uint32_t>* stack_depths) { |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 885 | struct OwnedMonitorVisitor : public StackVisitor { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 886 | OwnedMonitorVisitor(Thread* thread, Context* context, |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 887 | std::vector<JDWP::ObjectId>* monitor_vector, |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 888 | std::vector<uint32_t>* stack_depth_vector) |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 889 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 890 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 891 | current_stack_depth(0), |
| 892 | monitors(monitor_vector), |
| 893 | stack_depths(stack_depth_vector) {} |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 894 | |
| 895 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 896 | // annotalysis. |
| 897 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 898 | if (!GetMethod()->IsRuntimeMethod()) { |
| 899 | Monitor::VisitLocks(this, AppendOwnedMonitors, this); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 900 | ++current_stack_depth; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 901 | } |
| 902 | return true; |
| 903 | } |
| 904 | |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 905 | static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) |
| 906 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 907 | OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 908 | visitor->monitors->push_back(gRegistry->Add(owned_monitor)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 909 | visitor->stack_depths->push_back(visitor->current_stack_depth); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 912 | size_t current_stack_depth; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 913 | std::vector<JDWP::ObjectId>* const monitors; |
| 914 | std::vector<uint32_t>* const stack_depths; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 915 | }; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 916 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 917 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 918 | JDWP::JdwpError error; |
| 919 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 920 | if (thread == nullptr) { |
| 921 | return error; |
| 922 | } |
| 923 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 924 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 925 | } |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 926 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 927 | OwnedMonitorVisitor visitor(thread, context.get(), monitors, stack_depths); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 928 | visitor.WalkStack(); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 929 | return JDWP::ERR_NONE; |
| 930 | } |
| 931 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 932 | JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 933 | JDWP::ObjectId* contended_monitor) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 934 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 935 | *contended_monitor = 0; |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 936 | JDWP::JdwpError error; |
| 937 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 938 | if (thread == nullptr) { |
| 939 | return error; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 940 | } |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 941 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 942 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 943 | } |
| 944 | mirror::Object* contended_monitor_obj = Monitor::GetContendedMonitor(thread); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 945 | // Add() requires the thread_list_lock_ not held to avoid the lock |
| 946 | // level violation. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 947 | *contended_monitor = gRegistry->Add(contended_monitor_obj); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 948 | return JDWP::ERR_NONE; |
| 949 | } |
| 950 | |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 951 | JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 952 | std::vector<uint64_t>* counts) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 953 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 954 | heap->CollectGarbage(false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 955 | std::vector<mirror::Class*> classes; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 956 | counts->clear(); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 957 | for (size_t i = 0; i < class_ids.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 958 | JDWP::JdwpError error; |
| 959 | mirror::Class* c = DecodeClass(class_ids[i], &error); |
| 960 | if (c == nullptr) { |
| 961 | return error; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 962 | } |
| 963 | classes.push_back(c); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 964 | counts->push_back(0); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 965 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 966 | heap->CountInstances(classes, false, &(*counts)[0]); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 967 | return JDWP::ERR_NONE; |
| 968 | } |
| 969 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 970 | JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, |
| 971 | std::vector<JDWP::ObjectId>* instances) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 972 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 973 | // We only want reachable instances, so do a GC. |
| 974 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 975 | JDWP::JdwpError error; |
| 976 | mirror::Class* c = DecodeClass(class_id, &error); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 977 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 978 | return error; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 979 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 980 | std::vector<mirror::Object*> raw_instances; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 981 | Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances); |
| 982 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 983 | instances->push_back(gRegistry->Add(raw_instances[i])); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 984 | } |
| 985 | return JDWP::ERR_NONE; |
| 986 | } |
| 987 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 988 | JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 989 | std::vector<JDWP::ObjectId>* referring_objects) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 990 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 991 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 992 | JDWP::JdwpError error; |
| 993 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 994 | if (o == nullptr) { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 995 | return JDWP::ERR_INVALID_OBJECT; |
| 996 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 997 | std::vector<mirror::Object*> raw_instances; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 998 | heap->GetReferringObjects(o, max_count, raw_instances); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 999 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1000 | referring_objects->push_back(gRegistry->Add(raw_instances[i])); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1001 | } |
| 1002 | return JDWP::ERR_NONE; |
| 1003 | } |
| 1004 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1005 | JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id) { |
| 1006 | JDWP::JdwpError error; |
| 1007 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1008 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1009 | return JDWP::ERR_INVALID_OBJECT; |
| 1010 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1011 | gRegistry->DisableCollection(object_id); |
| 1012 | return JDWP::ERR_NONE; |
| 1013 | } |
| 1014 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1015 | JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id) { |
| 1016 | JDWP::JdwpError error; |
| 1017 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1018 | // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI |
| 1019 | // also ignores these cases and never return an error. However it's not obvious why this command |
| 1020 | // should behave differently from DisableCollection and IsCollected commands. So let's be more |
| 1021 | // strict and return an error if this happens. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1022 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1023 | return JDWP::ERR_INVALID_OBJECT; |
| 1024 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1025 | gRegistry->EnableCollection(object_id); |
| 1026 | return JDWP::ERR_NONE; |
| 1027 | } |
| 1028 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1029 | JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool* is_collected) { |
| 1030 | *is_collected = true; |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 1031 | if (object_id == 0) { |
| 1032 | // Null object id is invalid. |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1033 | return JDWP::ERR_INVALID_OBJECT; |
| 1034 | } |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 1035 | // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However |
| 1036 | // the RI seems to ignore this and assume object has been collected. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1037 | JDWP::JdwpError error; |
| 1038 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1039 | if (o != nullptr) { |
| 1040 | *is_collected = gRegistry->IsCollected(object_id); |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 1041 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1042 | return JDWP::ERR_NONE; |
| 1043 | } |
| 1044 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1045 | void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1046 | gRegistry->DisposeObject(object_id, reference_count); |
| 1047 | } |
| 1048 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1049 | JDWP::JdwpTypeTag Dbg::GetTypeTag(mirror::Class* klass) { |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1050 | DCHECK(klass != nullptr); |
| 1051 | if (klass->IsArrayClass()) { |
| 1052 | return JDWP::TT_ARRAY; |
| 1053 | } else if (klass->IsInterface()) { |
| 1054 | return JDWP::TT_INTERFACE; |
| 1055 | } else { |
| 1056 | return JDWP::TT_CLASS; |
| 1057 | } |
| 1058 | } |
| 1059 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1060 | JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1061 | JDWP::JdwpError error; |
| 1062 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1063 | if (c == nullptr) { |
| 1064 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1065 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1066 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1067 | JDWP::JdwpTypeTag type_tag = GetTypeTag(c); |
| 1068 | expandBufAdd1(pReply, type_tag); |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1069 | expandBufAddRefTypeId(pReply, class_id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1070 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1073 | void Dbg::GetClassList(std::vector<JDWP::RefTypeId>* classes) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1074 | // Get the complete list of reference classes (i.e. all classes except |
| 1075 | // the primitive types). |
| 1076 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 1077 | struct ClassListCreator { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1078 | explicit ClassListCreator(std::vector<JDWP::RefTypeId>* classes_in) : classes(classes_in) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1081 | static bool Visit(mirror::Class* c, void* arg) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1082 | return reinterpret_cast<ClassListCreator*>(arg)->Visit(c); |
| 1083 | } |
| 1084 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1085 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 1086 | // annotalysis. |
| 1087 | bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1088 | if (!c->IsPrimitive()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1089 | classes->push_back(gRegistry->AddRefType(c)); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1090 | } |
| 1091 | return true; |
| 1092 | } |
| 1093 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1094 | std::vector<JDWP::RefTypeId>* const classes; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1095 | }; |
| 1096 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1097 | ClassListCreator clc(classes); |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 1098 | Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(ClassListCreator::Visit, |
| 1099 | &clc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1102 | JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, |
| 1103 | uint32_t* pStatus, std::string* pDescriptor) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1104 | JDWP::JdwpError error; |
| 1105 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1106 | if (c == nullptr) { |
| 1107 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1110 | if (c->IsArrayClass()) { |
| 1111 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 1112 | *pTypeTag = JDWP::TT_ARRAY; |
| 1113 | } else { |
| 1114 | if (c->IsErroneous()) { |
| 1115 | *pStatus = JDWP::CS_ERROR; |
| 1116 | } else { |
| 1117 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 1118 | } |
| 1119 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1120 | } |
| 1121 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1122 | if (pDescriptor != nullptr) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1123 | std::string temp; |
| 1124 | *pDescriptor = c->GetDescriptor(&temp); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1125 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1126 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1129 | void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>* ids) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1130 | std::vector<mirror::Class*> classes; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1131 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1132 | ids->clear(); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1133 | for (size_t i = 0; i < classes.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1134 | ids->push_back(gRegistry->Add(classes[i])); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1135 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1138 | JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) { |
| 1139 | JDWP::JdwpError error; |
| 1140 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1141 | if (o == nullptr) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1142 | return JDWP::ERR_INVALID_OBJECT; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1143 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1144 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1145 | JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass()); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1146 | JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1147 | |
| 1148 | expandBufAdd1(pReply, type_tag); |
| 1149 | expandBufAddRefTypeId(pReply, type_id); |
| 1150 | |
| 1151 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 1154 | JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1155 | JDWP::JdwpError error; |
| 1156 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1157 | if (c == nullptr) { |
| 1158 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1159 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1160 | std::string temp; |
| 1161 | *signature = c->GetDescriptor(&temp); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 1162 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1165 | JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string* result) { |
| 1166 | JDWP::JdwpError error; |
| 1167 | mirror::Class* c = DecodeClass(class_id, &error); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1168 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1169 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1170 | } |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1171 | const char* source_file = c->GetSourceFile(); |
| 1172 | if (source_file == nullptr) { |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 1173 | return JDWP::ERR_ABSENT_INFORMATION; |
| 1174 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1175 | *result = source_file; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1176 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1179 | JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t* tag) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1180 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1181 | JDWP::JdwpError error; |
| 1182 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1183 | if (error != JDWP::ERR_NONE) { |
| 1184 | *tag = JDWP::JT_VOID; |
| 1185 | return error; |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1186 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1187 | *tag = TagFromObject(soa, o); |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1188 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1191 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1192 | switch (tag) { |
| 1193 | case JDWP::JT_VOID: |
| 1194 | return 0; |
| 1195 | case JDWP::JT_BYTE: |
| 1196 | case JDWP::JT_BOOLEAN: |
| 1197 | return 1; |
| 1198 | case JDWP::JT_CHAR: |
| 1199 | case JDWP::JT_SHORT: |
| 1200 | return 2; |
| 1201 | case JDWP::JT_FLOAT: |
| 1202 | case JDWP::JT_INT: |
| 1203 | return 4; |
| 1204 | case JDWP::JT_ARRAY: |
| 1205 | case JDWP::JT_OBJECT: |
| 1206 | case JDWP::JT_STRING: |
| 1207 | case JDWP::JT_THREAD: |
| 1208 | case JDWP::JT_THREAD_GROUP: |
| 1209 | case JDWP::JT_CLASS_LOADER: |
| 1210 | case JDWP::JT_CLASS_OBJECT: |
| 1211 | return sizeof(JDWP::ObjectId); |
| 1212 | case JDWP::JT_DOUBLE: |
| 1213 | case JDWP::JT_LONG: |
| 1214 | return 8; |
| 1215 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1216 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1217 | return -1; |
| 1218 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1221 | JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int32_t* length) { |
| 1222 | JDWP::JdwpError error; |
| 1223 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
| 1224 | if (a == nullptr) { |
| 1225 | return error; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1226 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1227 | *length = a->GetLength(); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1228 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1231 | 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] | 1232 | JDWP::JdwpError error; |
| 1233 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1234 | if (a == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1235 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1236 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1237 | |
| 1238 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 1239 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1240 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1241 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1242 | JDWP::JdwpTag element_tag = BasicTagFromClass(a->GetClass()->GetComponentType()); |
| 1243 | expandBufAdd1(pReply, element_tag); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1244 | expandBufAdd4BE(pReply, count); |
| 1245 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1246 | if (IsPrimitiveTag(element_tag)) { |
| 1247 | size_t width = GetTagWidth(element_tag); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1248 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 1249 | if (width == 8) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1250 | 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] | 1251 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 1252 | } else if (width == 4) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1253 | 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] | 1254 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 1255 | } else if (width == 2) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1256 | 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] | 1257 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 1258 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1259 | 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] | 1260 | memcpy(dst, &src[offset * width], count * width); |
| 1261 | } |
| 1262 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1263 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1264 | mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1265 | for (int i = 0; i < count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1266 | mirror::Object* element = oa->Get(offset + i); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1267 | JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element) |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1268 | : element_tag; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1269 | expandBufAdd1(pReply, specific_tag); |
| 1270 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 1271 | } |
| 1272 | } |
| 1273 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1274 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1277 | template <typename T> |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1278 | 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] | 1279 | NO_THREAD_SAFETY_ANALYSIS { |
| 1280 | // TODO: fix when annotalysis correctly handles non-member functions. |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1281 | DCHECK(a->GetClass()->IsPrimitiveArray()); |
| 1282 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1283 | T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1284 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1285 | *dst++ = src->ReadValue(sizeof(T)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1289 | JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1290 | JDWP::Request* request) { |
| 1291 | JDWP::JdwpError error; |
| 1292 | mirror::Array* dst = DecodeNonNullArray(array_id, &error); |
| 1293 | if (dst == nullptr) { |
| 1294 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1295 | } |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1296 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1297 | if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1298 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1299 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1300 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1301 | JDWP::JdwpTag element_tag = BasicTagFromClass(dst->GetClass()->GetComponentType()); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1302 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1303 | if (IsPrimitiveTag(element_tag)) { |
| 1304 | size_t width = GetTagWidth(element_tag); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1305 | if (width == 8) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1306 | CopyArrayData<uint64_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1307 | } else if (width == 4) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1308 | CopyArrayData<uint32_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1309 | } else if (width == 2) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1310 | CopyArrayData<uint16_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1311 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1312 | CopyArrayData<uint8_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1313 | } |
| 1314 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1315 | mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>(); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1316 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1317 | JDWP::ObjectId id = request->ReadObjectId(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1318 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 1319 | if (error != JDWP::ERR_NONE) { |
| 1320 | return error; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1321 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1322 | oa->Set<false>(offset + i, o); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1326 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1329 | JDWP::JdwpError Dbg::CreateString(const std::string& str, JDWP::ObjectId* new_string_id) { |
| 1330 | Thread* self = Thread::Current(); |
| 1331 | mirror::String* new_string = mirror::String::AllocFromModifiedUtf8(self, str.c_str()); |
| 1332 | if (new_string == nullptr) { |
| 1333 | DCHECK(self->IsExceptionPending()); |
| 1334 | self->ClearException(); |
| 1335 | LOG(ERROR) << "Could not allocate string"; |
| 1336 | *new_string_id = 0; |
| 1337 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1338 | } |
| 1339 | *new_string_id = gRegistry->Add(new_string); |
| 1340 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1343 | 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] | 1344 | JDWP::JdwpError error; |
| 1345 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1346 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1347 | *new_object_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1348 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1349 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1350 | Thread* self = Thread::Current(); |
| 1351 | mirror::Object* new_object = c->AllocObject(self); |
| 1352 | if (new_object == nullptr) { |
| 1353 | DCHECK(self->IsExceptionPending()); |
| 1354 | self->ClearException(); |
| 1355 | LOG(ERROR) << "Could not allocate object of type " << PrettyDescriptor(c); |
| 1356 | *new_object_id = 0; |
| 1357 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1358 | } |
| 1359 | *new_object_id = gRegistry->Add(new_object); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1360 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 1363 | /* |
| 1364 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 1365 | */ |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1366 | JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length, |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1367 | JDWP::ObjectId* new_array_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1368 | JDWP::JdwpError error; |
| 1369 | mirror::Class* c = DecodeClass(array_class_id, &error); |
| 1370 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1371 | *new_array_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1372 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1373 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1374 | Thread* self = Thread::Current(); |
| 1375 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1376 | mirror::Array* new_array = mirror::Array::Alloc<true>(self, c, length, |
| 1377 | c->GetComponentSizeShift(), |
| 1378 | heap->GetCurrentAllocator()); |
| 1379 | if (new_array == nullptr) { |
| 1380 | DCHECK(self->IsExceptionPending()); |
| 1381 | self->ClearException(); |
| 1382 | LOG(ERROR) << "Could not allocate array of type " << PrettyDescriptor(c); |
| 1383 | *new_array_id = 0; |
| 1384 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1385 | } |
| 1386 | *new_array_id = gRegistry->Add(new_array); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1387 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1390 | JDWP::FieldId Dbg::ToFieldId(const ArtField* f) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1391 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1394 | static JDWP::MethodId ToMethodId(const ArtMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1395 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1396 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1399 | static ArtField* FromFieldId(JDWP::FieldId fid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1400 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1401 | return reinterpret_cast<ArtField*>(static_cast<uintptr_t>(fid)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1402 | } |
| 1403 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1404 | static ArtMethod* FromMethodId(JDWP::MethodId mid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1405 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1406 | return reinterpret_cast<ArtMethod*>(static_cast<uintptr_t>(mid)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1407 | } |
| 1408 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1409 | bool Dbg::MatchThread(JDWP::ObjectId expected_thread_id, Thread* event_thread) { |
| 1410 | CHECK(event_thread != nullptr); |
| 1411 | JDWP::JdwpError error; |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1412 | mirror::Object* expected_thread_peer = gRegistry->Get<mirror::Object*>( |
| 1413 | expected_thread_id, &error); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1414 | return expected_thread_peer == event_thread->GetPeer(); |
| 1415 | } |
| 1416 | |
| 1417 | bool Dbg::MatchLocation(const JDWP::JdwpLocation& expected_location, |
| 1418 | const JDWP::EventLocation& event_location) { |
| 1419 | if (expected_location.dex_pc != event_location.dex_pc) { |
| 1420 | return false; |
| 1421 | } |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1422 | ArtMethod* m = FromMethodId(expected_location.method_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1423 | return m == event_location.method; |
| 1424 | } |
| 1425 | |
| 1426 | bool Dbg::MatchType(mirror::Class* event_class, JDWP::RefTypeId class_id) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1427 | if (event_class == nullptr) { |
| 1428 | return false; |
| 1429 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1430 | JDWP::JdwpError error; |
| 1431 | mirror::Class* expected_class = DecodeClass(class_id, &error); |
| 1432 | CHECK(expected_class != nullptr); |
| 1433 | return expected_class->IsAssignableFrom(event_class); |
| 1434 | } |
| 1435 | |
| 1436 | 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] | 1437 | ArtField* event_field) { |
| 1438 | ArtField* expected_field = FromFieldId(expected_field_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1439 | if (expected_field != event_field) { |
| 1440 | return false; |
| 1441 | } |
| 1442 | return Dbg::MatchType(event_field->GetDeclaringClass(), expected_type_id); |
| 1443 | } |
| 1444 | |
| 1445 | bool Dbg::MatchInstance(JDWP::ObjectId expected_instance_id, mirror::Object* event_instance) { |
| 1446 | JDWP::JdwpError error; |
| 1447 | mirror::Object* modifier_instance = gRegistry->Get<mirror::Object*>(expected_instance_id, &error); |
| 1448 | return modifier_instance == event_instance; |
| 1449 | } |
| 1450 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1451 | void Dbg::SetJdwpLocation(JDWP::JdwpLocation* location, ArtMethod* m, uint32_t dex_pc) |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 1452 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 1453 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 1454 | Locks::thread_suspend_count_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1455 | if (m == nullptr) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1456 | memset(location, 0, sizeof(*location)); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1457 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1458 | mirror::Class* c = m->GetDeclaringClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1459 | location->type_tag = GetTypeTag(c); |
| 1460 | location->class_id = gRegistry->AddRefType(c); |
| 1461 | location->method_id = ToMethodId(m); |
| 1462 | 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] | 1463 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1464 | } |
| 1465 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1466 | std::string Dbg::GetMethodName(JDWP::MethodId method_id) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1467 | ArtMethod* m = FromMethodId(method_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1468 | if (m == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1469 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1470 | } |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1471 | return m->GetInterfaceMethodIfProxy(sizeof(void*))->GetName(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1474 | std::string Dbg::GetFieldName(JDWP::FieldId field_id) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1475 | ArtField* f = FromFieldId(field_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1476 | if (f == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1477 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1478 | } |
| 1479 | return f->GetName(); |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1480 | } |
| 1481 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1482 | /* |
| 1483 | * Augment the access flags for synthetic methods and fields by setting |
| 1484 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 1485 | * flags not specified by the Java programming language. |
| 1486 | */ |
| 1487 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 1488 | accessFlags &= kAccJavaFlagsMask; |
| 1489 | if ((accessFlags & kAccSynthetic) != 0) { |
| 1490 | accessFlags |= 0xf0000000; |
| 1491 | } |
| 1492 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1495 | /* |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1496 | * Circularly shifts registers so that arguments come first. Debuggers |
| 1497 | * expect slots to begin with arguments, but dex code places them at |
| 1498 | * the end. |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1499 | */ |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1500 | static uint16_t MangleSlot(uint16_t slot, ArtMethod* m) |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1501 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1502 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1503 | if (code_item == nullptr) { |
| 1504 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1505 | // return the slot as is since all registers are arguments. |
| 1506 | LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m); |
| 1507 | return slot; |
| 1508 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1509 | uint16_t ins_size = code_item->ins_size_; |
| 1510 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1511 | if (slot >= locals_size) { |
| 1512 | return slot - locals_size; |
| 1513 | } else { |
| 1514 | return slot + ins_size; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1515 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1518 | /* |
| 1519 | * Circularly shifts registers so that arguments come last. Reverts |
| 1520 | * slots to dex style argument placement. |
| 1521 | */ |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1522 | static uint16_t DemangleSlot(uint16_t slot, ArtMethod* m, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1523 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1524 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1525 | if (code_item == nullptr) { |
| 1526 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1527 | // return the slot as is since all registers are arguments. |
| 1528 | LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1529 | uint16_t vreg_count = ArtMethod::NumArgRegisters(m->GetShorty()); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1530 | if (slot < vreg_count) { |
| 1531 | *error = JDWP::ERR_NONE; |
| 1532 | return slot; |
| 1533 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1534 | } else { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1535 | if (slot < code_item->registers_size_) { |
| 1536 | uint16_t ins_size = code_item->ins_size_; |
| 1537 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1538 | *error = JDWP::ERR_NONE; |
| 1539 | return (slot < ins_size) ? slot + locals_size : slot - ins_size; |
| 1540 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1541 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1542 | |
| 1543 | // Slot is invalid in the method. |
| 1544 | LOG(ERROR) << "Invalid local slot " << slot << " for method " << PrettyMethod(m); |
| 1545 | *error = JDWP::ERR_INVALID_SLOT; |
| 1546 | return DexFile::kDexNoIndex16; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1549 | 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] | 1550 | JDWP::JdwpError error; |
| 1551 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1552 | if (c == nullptr) { |
| 1553 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1554 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1555 | |
| 1556 | size_t instance_field_count = c->NumInstanceFields(); |
| 1557 | size_t static_field_count = c->NumStaticFields(); |
| 1558 | |
| 1559 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 1560 | |
| 1561 | 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] | 1562 | 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] | 1563 | expandBufAddFieldId(pReply, ToFieldId(f)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1564 | expandBufAddUtf8String(pReply, f->GetName()); |
| 1565 | expandBufAddUtf8String(pReply, f->GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1566 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1567 | static const char genericSignature[1] = ""; |
| 1568 | expandBufAddUtf8String(pReply, genericSignature); |
| 1569 | } |
| 1570 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 1571 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1572 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1573 | } |
| 1574 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1575 | JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1576 | JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1577 | JDWP::JdwpError error; |
| 1578 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1579 | if (c == nullptr) { |
| 1580 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1581 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1582 | |
| 1583 | size_t direct_method_count = c->NumDirectMethods(); |
| 1584 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 1585 | |
| 1586 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 1587 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1588 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 1589 | auto ptr_size = cl->GetImagePointerSize(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1590 | for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1591 | ArtMethod* m = i < direct_method_count ? |
| 1592 | 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] | 1593 | expandBufAddMethodId(pReply, ToMethodId(m)); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1594 | expandBufAddUtf8String(pReply, m->GetInterfaceMethodIfProxy(sizeof(void*))->GetName()); |
| 1595 | expandBufAddUtf8String(pReply, |
| 1596 | m->GetInterfaceMethodIfProxy(sizeof(void*))->GetSignature().ToString()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1597 | if (with_generic) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1598 | const char* generic_signature = ""; |
| 1599 | expandBufAddUtf8String(pReply, generic_signature); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1600 | } |
| 1601 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 1602 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1603 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1606 | JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1607 | JDWP::JdwpError error; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1608 | Thread* self = Thread::Current(); |
| 1609 | StackHandleScope<1> hs(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1610 | Handle<mirror::Class> c(hs.NewHandle(DecodeClass(class_id, &error))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1611 | if (c.Get() == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1612 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1613 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1614 | size_t interface_count = c->NumDirectInterfaces(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1615 | expandBufAdd4BE(pReply, interface_count); |
| 1616 | for (size_t i = 0; i < interface_count; ++i) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1617 | expandBufAddRefTypeId(pReply, |
| 1618 | gRegistry->AddRefType(mirror::Class::GetDirectInterface(self, c, i))); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1619 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1620 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1623 | void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1624 | struct DebugCallbackContext { |
| 1625 | int numItems; |
| 1626 | JDWP::ExpandBuf* pReply; |
| 1627 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1628 | static bool Callback(void* context, uint32_t address, uint32_t line_number) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1629 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1630 | expandBufAdd8BE(pContext->pReply, address); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1631 | expandBufAdd4BE(pContext->pReply, line_number); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1632 | pContext->numItems++; |
Sebastien Hertz | f2910ee | 2013-10-19 16:39:24 +0200 | [diff] [blame] | 1633 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1634 | } |
| 1635 | }; |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1636 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1637 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1638 | uint64_t start, end; |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1639 | if (code_item == nullptr) { |
| 1640 | DCHECK(m->IsNative() || m->IsProxyMethod()); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1641 | start = -1; |
| 1642 | end = -1; |
| 1643 | } else { |
| 1644 | start = 0; |
jeffhao | 14f0db9 | 2012-12-14 17:50:42 -0800 | [diff] [blame] | 1645 | // Return the index of the last instruction |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1646 | end = code_item->insns_size_in_code_units_ - 1; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | expandBufAdd8BE(pReply, start); |
| 1650 | expandBufAdd8BE(pReply, end); |
| 1651 | |
| 1652 | // Add numLines later |
| 1653 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 1654 | expandBufAdd4BE(pReply, 0); |
| 1655 | |
| 1656 | DebugCallbackContext context; |
| 1657 | context.numItems = 0; |
| 1658 | context.pReply = pReply; |
| 1659 | |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1660 | if (code_item != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1661 | m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1662 | DebugCallbackContext::Callback, nullptr, &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1663 | } |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1664 | |
| 1665 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1668 | void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, |
| 1669 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1670 | struct DebugCallbackContext { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1671 | ArtMethod* method; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1672 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1673 | size_t variable_count; |
| 1674 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1675 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1676 | static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, |
| 1677 | const char* name, const char* descriptor, const char* signature) |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1678 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1679 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1680 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1681 | VLOG(jdwp) << StringPrintf(" %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d", |
| 1682 | pContext->variable_count, startAddress, endAddress - startAddress, |
| 1683 | name, descriptor, signature, slot, |
| 1684 | MangleSlot(slot, pContext->method)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1685 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1686 | slot = MangleSlot(slot, pContext->method); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1687 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1688 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 1689 | expandBufAddUtf8String(pContext->pReply, name); |
| 1690 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1691 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1692 | expandBufAddUtf8String(pContext->pReply, signature); |
| 1693 | } |
| 1694 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 1695 | expandBufAdd4BE(pContext->pReply, slot); |
| 1696 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1697 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1698 | } |
| 1699 | }; |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1700 | ArtMethod* m = FromMethodId(method_id); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1701 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1702 | // arg_count considers doubles and longs to take 2 units. |
| 1703 | // variable_count considers everything to take 1 unit. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1704 | std::string shorty(m->GetShorty()); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1705 | expandBufAdd4BE(pReply, ArtMethod::NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1706 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1707 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1708 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1709 | expandBufAdd4BE(pReply, 0); |
| 1710 | |
| 1711 | DebugCallbackContext context; |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1712 | context.method = m; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1713 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1714 | context.variable_count = 0; |
| 1715 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1716 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1717 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1718 | if (code_item != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1719 | m->GetDexFile()->DecodeDebugInfo( |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1720 | code_item, m->IsStatic(), m->GetDexMethodIndex(), nullptr, DebugCallbackContext::Callback, |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1721 | &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1722 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1723 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1724 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1727 | void Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value, |
| 1728 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1729 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1730 | JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1731 | OutputJValue(tag, return_value, pReply); |
| 1732 | } |
| 1733 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1734 | void Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value, |
| 1735 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1736 | ArtField* f = FromFieldId(field_id); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1737 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1738 | OutputJValue(tag, field_value, pReply); |
| 1739 | } |
| 1740 | |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1741 | JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1742 | std::vector<uint8_t>* bytecodes) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1743 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1744 | if (m == nullptr) { |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1745 | return JDWP::ERR_INVALID_METHODID; |
| 1746 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1747 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1748 | size_t byte_count = code_item->insns_size_in_code_units_ * 2; |
| 1749 | const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_); |
| 1750 | const uint8_t* end = begin + byte_count; |
| 1751 | for (const uint8_t* p = begin; p != end; ++p) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1752 | bytecodes->push_back(*p); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1753 | } |
| 1754 | return JDWP::ERR_NONE; |
| 1755 | } |
| 1756 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1757 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1758 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1761 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1762 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Sebastien Hertz | 4998374 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1765 | static JValue GetArtFieldValue(ArtField* f, mirror::Object* o) |
| 1766 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1767 | Primitive::Type fieldType = f->GetTypeAsPrimitiveType(); |
| 1768 | JValue field_value; |
| 1769 | switch (fieldType) { |
| 1770 | case Primitive::kPrimBoolean: |
| 1771 | field_value.SetZ(f->GetBoolean(o)); |
| 1772 | return field_value; |
| 1773 | |
| 1774 | case Primitive::kPrimByte: |
| 1775 | field_value.SetB(f->GetByte(o)); |
| 1776 | return field_value; |
| 1777 | |
| 1778 | case Primitive::kPrimChar: |
| 1779 | field_value.SetC(f->GetChar(o)); |
| 1780 | return field_value; |
| 1781 | |
| 1782 | case Primitive::kPrimShort: |
| 1783 | field_value.SetS(f->GetShort(o)); |
| 1784 | return field_value; |
| 1785 | |
| 1786 | case Primitive::kPrimInt: |
| 1787 | case Primitive::kPrimFloat: |
| 1788 | // Int and Float must be treated as 32-bit values in JDWP. |
| 1789 | field_value.SetI(f->GetInt(o)); |
| 1790 | return field_value; |
| 1791 | |
| 1792 | case Primitive::kPrimLong: |
| 1793 | case Primitive::kPrimDouble: |
| 1794 | // Long and Double must be treated as 64-bit values in JDWP. |
| 1795 | field_value.SetJ(f->GetLong(o)); |
| 1796 | return field_value; |
| 1797 | |
| 1798 | case Primitive::kPrimNot: |
| 1799 | field_value.SetL(f->GetObject(o)); |
| 1800 | return field_value; |
| 1801 | |
| 1802 | case Primitive::kPrimVoid: |
| 1803 | LOG(FATAL) << "Attempt to read from field of type 'void'"; |
| 1804 | UNREACHABLE(); |
| 1805 | } |
| 1806 | LOG(FATAL) << "Attempt to read from field of unknown type"; |
| 1807 | UNREACHABLE(); |
| 1808 | } |
| 1809 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1810 | static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id, |
| 1811 | JDWP::FieldId field_id, JDWP::ExpandBuf* pReply, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1812 | bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1813 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1814 | JDWP::JdwpError error; |
| 1815 | mirror::Class* c = DecodeClass(ref_type_id, &error); |
| 1816 | if (ref_type_id != 0 && c == nullptr) { |
| 1817 | return error; |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1818 | } |
| 1819 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1820 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1821 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1822 | return JDWP::ERR_INVALID_OBJECT; |
| 1823 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1824 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1825 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1826 | mirror::Class* receiver_class = c; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1827 | if (receiver_class == nullptr && o != nullptr) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1828 | receiver_class = o->GetClass(); |
| 1829 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1830 | // TODO: should we give up now if receiver_class is null? |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1831 | if (receiver_class != nullptr && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1832 | LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1833 | return JDWP::ERR_INVALID_FIELDID; |
| 1834 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1835 | |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1836 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1837 | // TODO: should we change the tests and check both? |
| 1838 | if (is_static) { |
| 1839 | if (!f->IsStatic()) { |
| 1840 | return JDWP::ERR_INVALID_FIELDID; |
| 1841 | } |
| 1842 | } else { |
| 1843 | if (f->IsStatic()) { |
Sebastien Hertz | 4998374 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1844 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.GetValues" |
| 1845 | << " on static field " << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1846 | } |
| 1847 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1848 | if (f->IsStatic()) { |
| 1849 | o = f->GetDeclaringClass(); |
| 1850 | } |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1851 | |
Sebastien Hertz | 4998374 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1852 | JValue field_value(GetArtFieldValue(f, o)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1853 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1854 | Dbg::OutputJValue(tag, &field_value, pReply); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1855 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1858 | JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1859 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1860 | return GetFieldValueImpl(0, object_id, field_id, pReply, false); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1861 | } |
| 1862 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1863 | JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, |
| 1864 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1865 | return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1866 | } |
| 1867 | |
Sebastien Hertz | 4998374 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1868 | static JDWP::JdwpError SetArtFieldValue(ArtField* f, mirror::Object* o, uint64_t value, int width) |
| 1869 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1870 | Primitive::Type fieldType = f->GetTypeAsPrimitiveType(); |
| 1871 | // Debugging only happens at runtime so we know we are not running in a transaction. |
| 1872 | static constexpr bool kNoTransactionMode = false; |
| 1873 | switch (fieldType) { |
| 1874 | case Primitive::kPrimBoolean: |
| 1875 | CHECK_EQ(width, 1); |
| 1876 | f->SetBoolean<kNoTransactionMode>(o, static_cast<uint8_t>(value)); |
| 1877 | return JDWP::ERR_NONE; |
| 1878 | |
| 1879 | case Primitive::kPrimByte: |
| 1880 | CHECK_EQ(width, 1); |
| 1881 | f->SetByte<kNoTransactionMode>(o, static_cast<uint8_t>(value)); |
| 1882 | return JDWP::ERR_NONE; |
| 1883 | |
| 1884 | case Primitive::kPrimChar: |
| 1885 | CHECK_EQ(width, 2); |
| 1886 | f->SetChar<kNoTransactionMode>(o, static_cast<uint16_t>(value)); |
| 1887 | return JDWP::ERR_NONE; |
| 1888 | |
| 1889 | case Primitive::kPrimShort: |
| 1890 | CHECK_EQ(width, 2); |
| 1891 | f->SetShort<kNoTransactionMode>(o, static_cast<int16_t>(value)); |
| 1892 | return JDWP::ERR_NONE; |
| 1893 | |
| 1894 | case Primitive::kPrimInt: |
| 1895 | case Primitive::kPrimFloat: |
| 1896 | CHECK_EQ(width, 4); |
| 1897 | // Int and Float must be treated as 32-bit values in JDWP. |
| 1898 | f->SetInt<kNoTransactionMode>(o, static_cast<int32_t>(value)); |
| 1899 | return JDWP::ERR_NONE; |
| 1900 | |
| 1901 | case Primitive::kPrimLong: |
| 1902 | case Primitive::kPrimDouble: |
| 1903 | CHECK_EQ(width, 8); |
| 1904 | // Long and Double must be treated as 64-bit values in JDWP. |
| 1905 | f->SetLong<kNoTransactionMode>(o, value); |
| 1906 | return JDWP::ERR_NONE; |
| 1907 | |
| 1908 | case Primitive::kPrimNot: { |
| 1909 | JDWP::JdwpError error; |
| 1910 | mirror::Object* v = Dbg::GetObjectRegistry()->Get<mirror::Object*>(value, &error); |
| 1911 | if (error != JDWP::ERR_NONE) { |
| 1912 | return JDWP::ERR_INVALID_OBJECT; |
| 1913 | } |
| 1914 | if (v != nullptr) { |
| 1915 | mirror::Class* field_type; |
| 1916 | { |
| 1917 | StackHandleScope<2> hs(Thread::Current()); |
| 1918 | HandleWrapper<mirror::Object> h_v(hs.NewHandleWrapper(&v)); |
| 1919 | HandleWrapper<mirror::Object> h_o(hs.NewHandleWrapper(&o)); |
| 1920 | field_type = f->GetType<true>(); |
| 1921 | } |
| 1922 | if (!field_type->IsAssignableFrom(v->GetClass())) { |
| 1923 | return JDWP::ERR_INVALID_OBJECT; |
| 1924 | } |
| 1925 | } |
| 1926 | f->SetObject<kNoTransactionMode>(o, v); |
| 1927 | return JDWP::ERR_NONE; |
| 1928 | } |
| 1929 | |
| 1930 | case Primitive::kPrimVoid: |
| 1931 | LOG(FATAL) << "Attempt to write to field of type 'void'"; |
| 1932 | UNREACHABLE(); |
| 1933 | } |
| 1934 | LOG(FATAL) << "Attempt to write to field of unknown type"; |
| 1935 | UNREACHABLE(); |
| 1936 | } |
| 1937 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1938 | static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1939 | uint64_t value, int width, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1940 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1941 | JDWP::JdwpError error; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1942 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1943 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1944 | return JDWP::ERR_INVALID_OBJECT; |
| 1945 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1946 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1947 | |
| 1948 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1949 | // TODO: should we change the tests and check both? |
| 1950 | if (is_static) { |
| 1951 | if (!f->IsStatic()) { |
| 1952 | return JDWP::ERR_INVALID_FIELDID; |
| 1953 | } |
| 1954 | } else { |
| 1955 | if (f->IsStatic()) { |
Sebastien Hertz | 4998374 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1956 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues" |
| 1957 | << " on static field " << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1958 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1959 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1960 | if (f->IsStatic()) { |
| 1961 | o = f->GetDeclaringClass(); |
| 1962 | } |
Sebastien Hertz | 4998374 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1963 | return SetArtFieldValue(f, o, value, width); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1966 | 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] | 1967 | int width) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1968 | return SetFieldValueImpl(object_id, field_id, value, width, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1971 | JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) { |
| 1972 | return SetFieldValueImpl(0, field_id, value, width, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1975 | JDWP::JdwpError Dbg::StringToUtf8(JDWP::ObjectId string_id, std::string* str) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1976 | JDWP::JdwpError error; |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1977 | mirror::Object* obj = gRegistry->Get<mirror::Object*>(string_id, &error); |
| 1978 | if (error != JDWP::ERR_NONE) { |
| 1979 | return error; |
| 1980 | } |
| 1981 | if (obj == nullptr) { |
| 1982 | return JDWP::ERR_INVALID_OBJECT; |
| 1983 | } |
| 1984 | { |
| 1985 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1986 | mirror::Class* java_lang_String = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_String); |
| 1987 | if (!java_lang_String->IsAssignableFrom(obj->GetClass())) { |
| 1988 | // This isn't a string. |
| 1989 | return JDWP::ERR_INVALID_STRING; |
| 1990 | } |
| 1991 | } |
| 1992 | *str = obj->AsString()->ToModifiedUtf8(); |
| 1993 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1996 | void Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) { |
| 1997 | if (IsPrimitiveTag(tag)) { |
| 1998 | expandBufAdd1(pReply, tag); |
| 1999 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 2000 | expandBufAdd1(pReply, return_value->GetI()); |
| 2001 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 2002 | expandBufAdd2BE(pReply, return_value->GetI()); |
| 2003 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 2004 | expandBufAdd4BE(pReply, return_value->GetI()); |
| 2005 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 2006 | expandBufAdd8BE(pReply, return_value->GetJ()); |
| 2007 | } else { |
| 2008 | CHECK_EQ(tag, JDWP::JT_VOID); |
| 2009 | } |
| 2010 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2011 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2012 | mirror::Object* value = return_value->GetL(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2013 | expandBufAdd1(pReply, TagFromObject(soa, value)); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2014 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 2015 | } |
| 2016 | } |
| 2017 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2018 | JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string* name) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2019 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2020 | JDWP::JdwpError error; |
| 2021 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2022 | UNUSED(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2023 | if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2024 | return error; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2025 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2026 | |
| 2027 | // 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] | 2028 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2029 | CHECK(thread_object != nullptr) << error; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2030 | ArtField* java_lang_Thread_name_field = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2031 | soa.DecodeField(WellKnownClasses::java_lang_Thread_name); |
| 2032 | mirror::String* s = |
| 2033 | reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2034 | if (s != nullptr) { |
| 2035 | *name = s->ToModifiedUtf8(); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2036 | } |
| 2037 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2040 | JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2041 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2042 | JDWP::JdwpError error; |
| 2043 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2044 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2045 | return JDWP::ERR_INVALID_OBJECT; |
| 2046 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2047 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroup"); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2048 | // Okay, so it's an object, but is it actually a thread? |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2049 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2050 | UNUSED(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2051 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2052 | // Zombie threads are in the null group. |
| 2053 | expandBufAddObjectId(pReply, JDWP::ObjectId(0)); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2054 | error = JDWP::ERR_NONE; |
| 2055 | } else if (error == JDWP::ERR_NONE) { |
| 2056 | mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
| 2057 | CHECK(c != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2058 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 2059 | CHECK(f != nullptr); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2060 | mirror::Object* group = f->GetObject(thread_object); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 2061 | CHECK(group != nullptr); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2062 | JDWP::ObjectId thread_group_id = gRegistry->Add(group); |
| 2063 | expandBufAddObjectId(pReply, thread_group_id); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2064 | } |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2065 | return error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2068 | static mirror::Object* DecodeThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 2069 | JDWP::ObjectId thread_group_id, JDWP::JdwpError* error) |
| 2070 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2071 | mirror::Object* thread_group = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_group_id, |
| 2072 | error); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2073 | if (*error != JDWP::ERR_NONE) { |
| 2074 | return nullptr; |
| 2075 | } |
| 2076 | if (thread_group == nullptr) { |
| 2077 | *error = JDWP::ERR_INVALID_OBJECT; |
| 2078 | return nullptr; |
| 2079 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2080 | mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup); |
| 2081 | CHECK(c != nullptr); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2082 | if (!c->IsAssignableFrom(thread_group->GetClass())) { |
| 2083 | // This is not a java.lang.ThreadGroup. |
| 2084 | *error = JDWP::ERR_INVALID_THREAD_GROUP; |
| 2085 | return nullptr; |
| 2086 | } |
| 2087 | *error = JDWP::ERR_NONE; |
| 2088 | return thread_group; |
| 2089 | } |
| 2090 | |
| 2091 | JDWP::JdwpError Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
| 2092 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 2093 | JDWP::JdwpError error; |
| 2094 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2095 | if (error != JDWP::ERR_NONE) { |
| 2096 | return error; |
| 2097 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2098 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupName"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2099 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2100 | CHECK(f != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2101 | mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group)); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2102 | |
| 2103 | std::string thread_group_name(s->ToModifiedUtf8()); |
| 2104 | expandBufAddUtf8String(pReply, thread_group_name); |
| 2105 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2106 | } |
| 2107 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2108 | JDWP::JdwpError Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2109 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2110 | JDWP::JdwpError error; |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2111 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2112 | if (error != JDWP::ERR_NONE) { |
| 2113 | return error; |
| 2114 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2115 | mirror::Object* parent; |
| 2116 | { |
| 2117 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupParent"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2118 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_parent); |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2119 | CHECK(f != nullptr); |
| 2120 | parent = f->GetObject(thread_group); |
| 2121 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2122 | JDWP::ObjectId parent_group_id = gRegistry->Add(parent); |
| 2123 | expandBufAddObjectId(pReply, parent_group_id); |
| 2124 | return JDWP::ERR_NONE; |
| 2125 | } |
| 2126 | |
| 2127 | static void GetChildThreadGroups(ScopedObjectAccessUnchecked& soa, mirror::Object* thread_group, |
| 2128 | std::vector<JDWP::ObjectId>* child_thread_group_ids) |
| 2129 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2130 | CHECK(thread_group != nullptr); |
| 2131 | |
| 2132 | // Get the ArrayList<ThreadGroup> "groups" out of this thread group... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2133 | ArtField* groups_field = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_groups); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2134 | mirror::Object* groups_array_list = groups_field->GetObject(thread_group); |
Sebastien Hertz | e49e195 | 2014-10-13 11:27:13 +0200 | [diff] [blame] | 2135 | { |
| 2136 | // The "groups" field is declared as a java.util.List: check it really is |
| 2137 | // an instance of java.util.ArrayList. |
| 2138 | CHECK(groups_array_list != nullptr); |
| 2139 | mirror::Class* java_util_ArrayList_class = |
| 2140 | soa.Decode<mirror::Class*>(WellKnownClasses::java_util_ArrayList); |
| 2141 | CHECK(groups_array_list->InstanceOf(java_util_ArrayList_class)); |
| 2142 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2143 | |
| 2144 | // Get the array and size out of the ArrayList<ThreadGroup>... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2145 | ArtField* array_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_array); |
| 2146 | ArtField* size_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_size); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2147 | mirror::ObjectArray<mirror::Object>* groups_array = |
| 2148 | array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>(); |
| 2149 | const int32_t size = size_field->GetInt(groups_array_list); |
| 2150 | |
| 2151 | // Copy the first 'size' elements out of the array into the result. |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2152 | ObjectRegistry* registry = Dbg::GetObjectRegistry(); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2153 | for (int32_t i = 0; i < size; ++i) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2154 | child_thread_group_ids->push_back(registry->Add(groups_array->Get(i))); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | JDWP::JdwpError Dbg::GetThreadGroupChildren(JDWP::ObjectId thread_group_id, |
| 2159 | JDWP::ExpandBuf* pReply) { |
| 2160 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 2161 | JDWP::JdwpError error; |
| 2162 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2163 | if (error != JDWP::ERR_NONE) { |
| 2164 | return error; |
| 2165 | } |
| 2166 | |
| 2167 | // Add child threads. |
| 2168 | { |
| 2169 | std::vector<JDWP::ObjectId> child_thread_ids; |
| 2170 | GetThreads(thread_group, &child_thread_ids); |
| 2171 | expandBufAdd4BE(pReply, child_thread_ids.size()); |
| 2172 | for (JDWP::ObjectId child_thread_id : child_thread_ids) { |
| 2173 | expandBufAddObjectId(pReply, child_thread_id); |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | // Add child thread groups. |
| 2178 | { |
| 2179 | std::vector<JDWP::ObjectId> child_thread_groups_ids; |
| 2180 | GetChildThreadGroups(soa, thread_group, &child_thread_groups_ids); |
| 2181 | expandBufAdd4BE(pReply, child_thread_groups_ids.size()); |
| 2182 | for (JDWP::ObjectId child_thread_group_id : child_thread_groups_ids) { |
| 2183 | expandBufAddObjectId(pReply, child_thread_group_id); |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2191 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2192 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2193 | mirror::Object* group = f->GetObject(f->GetDeclaringClass()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2194 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2197 | JDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) { |
| 2198 | switch (state) { |
| 2199 | case kBlocked: |
| 2200 | return JDWP::TS_MONITOR; |
| 2201 | case kNative: |
| 2202 | case kRunnable: |
| 2203 | case kSuspended: |
| 2204 | return JDWP::TS_RUNNING; |
| 2205 | case kSleeping: |
| 2206 | return JDWP::TS_SLEEPING; |
| 2207 | case kStarting: |
| 2208 | case kTerminated: |
| 2209 | return JDWP::TS_ZOMBIE; |
| 2210 | case kTimedWaiting: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2211 | case kWaitingForCheckPointsToRun: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2212 | case kWaitingForDebuggerSend: |
| 2213 | case kWaitingForDebuggerSuspension: |
| 2214 | case kWaitingForDebuggerToAttach: |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 2215 | case kWaitingForDeoptimization: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2216 | case kWaitingForGcToComplete: |
Mathieu Chartier | a395c0a | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 2217 | case kWaitingForGetObjectsAllocated: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2218 | case kWaitingForJniOnLoad: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2219 | case kWaitingForMethodTracingStart: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2220 | case kWaitingForSignalCatcherOutput: |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 2221 | case kWaitingForVisitObjects: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2222 | case kWaitingInMainDebuggerLoop: |
| 2223 | case kWaitingInMainSignalCatcherLoop: |
| 2224 | case kWaitingPerformingGc: |
| 2225 | case kWaiting: |
| 2226 | return JDWP::TS_WAIT; |
| 2227 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 2228 | } |
| 2229 | LOG(FATAL) << "Unknown thread state: " << state; |
| 2230 | return JDWP::TS_ZOMBIE; |
| 2231 | } |
| 2232 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2233 | JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, |
| 2234 | JDWP::JdwpSuspendStatus* pSuspendStatus) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2235 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2236 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2237 | *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED; |
| 2238 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2239 | JDWP::JdwpError error; |
| 2240 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2241 | if (error != JDWP::ERR_NONE) { |
| 2242 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2243 | *pThreadStatus = JDWP::TS_ZOMBIE; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2244 | return JDWP::ERR_NONE; |
| 2245 | } |
| 2246 | return error; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2247 | } |
| 2248 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2249 | if (IsSuspendedForDebugger(soa, thread)) { |
| 2250 | *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2251 | } |
| 2252 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2253 | *pThreadStatus = ToJdwpThreadStatus(thread->GetState()); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2254 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2255 | } |
| 2256 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2257 | JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2258 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2259 | JDWP::JdwpError error; |
| 2260 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2261 | if (error != JDWP::ERR_NONE) { |
| 2262 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2263 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2264 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2265 | expandBufAdd4BE(pReply, thread->GetDebugSuspendCount()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2266 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2267 | } |
| 2268 | |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2269 | JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) { |
| 2270 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2271 | JDWP::JdwpError error; |
| 2272 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2273 | if (error != JDWP::ERR_NONE) { |
| 2274 | return error; |
| 2275 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 2276 | thread->Interrupt(soa.Self()); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2277 | return JDWP::ERR_NONE; |
| 2278 | } |
| 2279 | |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2280 | static bool IsInDesiredThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 2281 | mirror::Object* desired_thread_group, mirror::Object* peer) |
| 2282 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2283 | // Do we want threads from all thread groups? |
| 2284 | if (desired_thread_group == nullptr) { |
| 2285 | return true; |
| 2286 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2287 | ArtField* thread_group_field = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2288 | DCHECK(thread_group_field != nullptr); |
| 2289 | mirror::Object* group = thread_group_field->GetObject(peer); |
| 2290 | return (group == desired_thread_group); |
| 2291 | } |
| 2292 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2293 | 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] | 2294 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2295 | std::list<Thread*> all_threads_list; |
| 2296 | { |
| 2297 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 2298 | all_threads_list = Runtime::Current()->GetThreadList()->GetList(); |
| 2299 | } |
| 2300 | for (Thread* t : all_threads_list) { |
| 2301 | if (t == Dbg::GetDebugThread()) { |
| 2302 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 2303 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 2304 | continue; |
| 2305 | } |
| 2306 | if (t->IsStillStarting()) { |
| 2307 | // This thread is being started (and has been registered in the thread list). However, it is |
| 2308 | // not completely started yet so we must ignore it. |
| 2309 | continue; |
| 2310 | } |
| 2311 | mirror::Object* peer = t->GetPeer(); |
| 2312 | if (peer == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2313 | // 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] | 2314 | // this thread yet. |
| 2315 | // TODO: if we identified threads to the debugger by their Thread* |
| 2316 | // rather than their peer's mirror::Object*, we could fix this. |
| 2317 | // Doing so might help us report ZOMBIE threads too. |
| 2318 | continue; |
| 2319 | } |
| 2320 | if (IsInDesiredThreadGroup(soa, thread_group, peer)) { |
| 2321 | thread_ids->push_back(gRegistry->Add(peer)); |
| 2322 | } |
| 2323 | } |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2324 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2325 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2326 | static int GetStackDepth(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2327 | struct CountStackDepthVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2328 | explicit CountStackDepthVisitor(Thread* thread_in) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2329 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2330 | depth(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2331 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2332 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2333 | // annotalysis. |
| 2334 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2335 | if (!GetMethod()->IsRuntimeMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 2336 | ++depth; |
| 2337 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2338 | return true; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2339 | } |
| 2340 | size_t depth; |
| 2341 | }; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2342 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2343 | CountStackDepthVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2344 | visitor.WalkStack(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2345 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2348 | JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t* result) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2349 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2350 | JDWP::JdwpError error; |
| 2351 | *result = 0; |
| 2352 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2353 | if (error != JDWP::ERR_NONE) { |
| 2354 | return error; |
| 2355 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2356 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2357 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2358 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2359 | *result = GetStackDepth(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2360 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2361 | } |
| 2362 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 2363 | JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame, |
| 2364 | size_t frame_count, JDWP::ExpandBuf* buf) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2365 | class GetFrameVisitor : public StackVisitor { |
| 2366 | public: |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2367 | GetFrameVisitor(Thread* thread, size_t start_frame_in, size_t frame_count_in, |
| 2368 | JDWP::ExpandBuf* buf_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2369 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2370 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2371 | depth_(0), |
| 2372 | start_frame_(start_frame_in), |
| 2373 | frame_count_(frame_count_in), |
| 2374 | buf_(buf_in) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2375 | expandBufAdd4BE(buf_, frame_count_); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2376 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2377 | |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2378 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2379 | if (GetMethod()->IsRuntimeMethod()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2380 | 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] | 2381 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2382 | if (depth_ >= start_frame_ + frame_count_) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2383 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2384 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2385 | if (depth_ >= start_frame_) { |
| 2386 | JDWP::FrameId frame_id(GetFrameId()); |
| 2387 | JDWP::JdwpLocation location; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2388 | SetJdwpLocation(&location, GetMethod(), GetDexPc()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2389 | VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2390 | expandBufAdd8BE(buf_, frame_id); |
| 2391 | expandBufAddLocation(buf_, location); |
| 2392 | } |
| 2393 | ++depth_; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2394 | return true; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2395 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2396 | |
| 2397 | private: |
| 2398 | size_t depth_; |
| 2399 | const size_t start_frame_; |
| 2400 | const size_t frame_count_; |
| 2401 | JDWP::ExpandBuf* buf_; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2402 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2403 | |
| 2404 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2405 | JDWP::JdwpError error; |
| 2406 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2407 | if (error != JDWP::ERR_NONE) { |
| 2408 | return error; |
| 2409 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2410 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2411 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2412 | } |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2413 | GetFrameVisitor visitor(thread, start_frame, frame_count, buf); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2414 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2415 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2419 | return GetThreadId(Thread::Current()); |
| 2420 | } |
| 2421 | |
| 2422 | JDWP::ObjectId Dbg::GetThreadId(Thread* thread) { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 2423 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2424 | return gRegistry->Add(thread->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2425 | } |
| 2426 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2427 | void Dbg::SuspendVM() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2428 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | void Dbg::ResumeVM() { |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 2432 | Runtime::Current()->GetThreadList()->ResumeAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2433 | } |
| 2434 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2435 | JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2436 | Thread* self = Thread::Current(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2437 | ScopedLocalRef<jobject> peer(self->GetJniEnv(), nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2438 | { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2439 | ScopedObjectAccess soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2440 | JDWP::JdwpError error; |
| 2441 | peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id, &error))); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2442 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2443 | if (peer.get() == nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2444 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2445 | } |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 2446 | // Suspend thread to build stack trace. |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2447 | bool timed_out; |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 2448 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 2449 | Thread* thread = thread_list->SuspendThreadByPeer(peer.get(), request_suspension, true, |
| 2450 | &timed_out); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2451 | if (thread != nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2452 | return JDWP::ERR_NONE; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2453 | } else if (timed_out) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2454 | return JDWP::ERR_INTERNAL; |
| 2455 | } else { |
| 2456 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2457 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2458 | } |
| 2459 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2460 | void Dbg::ResumeThread(JDWP::ObjectId thread_id) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2461 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2462 | JDWP::JdwpError error; |
| 2463 | mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2464 | CHECK(peer != nullptr) << error; |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2465 | Thread* thread; |
| 2466 | { |
| 2467 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 2468 | thread = Thread::FromManagedThread(soa, peer); |
| 2469 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2470 | if (thread == nullptr) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2471 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 2472 | return; |
| 2473 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2474 | bool needs_resume; |
| 2475 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2476 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2477 | needs_resume = thread->GetSuspendCount() > 0; |
| 2478 | } |
| 2479 | if (needs_resume) { |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 2480 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
| 2481 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2485 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2486 | } |
| 2487 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2488 | struct GetThisVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2489 | GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2490 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2491 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2492 | this_object(nullptr), |
| 2493 | frame_id(frame_id_in) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2494 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2495 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2496 | // annotalysis. |
| 2497 | virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2498 | if (frame_id != GetFrameId()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2499 | return true; // continue |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2500 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2501 | this_object = GetThisObject(); |
| 2502 | return false; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2503 | } |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 2504 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2505 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2506 | mirror::Object* this_object; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2507 | JDWP::FrameId frame_id; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2508 | }; |
| 2509 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2510 | JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, |
| 2511 | JDWP::ObjectId* result) { |
| 2512 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2513 | JDWP::JdwpError error; |
| 2514 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2515 | if (error != JDWP::ERR_NONE) { |
| 2516 | return error; |
| 2517 | } |
| 2518 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2519 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2520 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2521 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2522 | GetThisVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2523 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2524 | *result = gRegistry->Add(visitor.this_object); |
| 2525 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2526 | } |
| 2527 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2528 | // Walks the stack until we find the frame with the given FrameId. |
| 2529 | class FindFrameVisitor FINAL : public StackVisitor { |
| 2530 | public: |
| 2531 | FindFrameVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id) |
| 2532 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2533 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2534 | frame_id_(frame_id), |
| 2535 | error_(JDWP::ERR_INVALID_FRAMEID) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2536 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2537 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2538 | // annotalysis. |
| 2539 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 2540 | if (GetFrameId() != frame_id_) { |
| 2541 | return true; // Not our frame, carry on. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2542 | } |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2543 | ArtMethod* m = GetMethod(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2544 | if (m->IsNative()) { |
| 2545 | // We can't read/write local value from/into native method. |
| 2546 | error_ = JDWP::ERR_OPAQUE_FRAME; |
| 2547 | } else { |
| 2548 | // We found our frame. |
| 2549 | error_ = JDWP::ERR_NONE; |
| 2550 | } |
| 2551 | return false; |
| 2552 | } |
| 2553 | |
| 2554 | JDWP::JdwpError GetError() const { |
| 2555 | return error_; |
| 2556 | } |
| 2557 | |
| 2558 | private: |
| 2559 | const JDWP::FrameId frame_id_; |
| 2560 | JDWP::JdwpError error_; |
| 2561 | }; |
| 2562 | |
| 2563 | JDWP::JdwpError Dbg::GetLocalValues(JDWP::Request* request, JDWP::ExpandBuf* pReply) { |
| 2564 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2565 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2566 | |
| 2567 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2568 | JDWP::JdwpError error; |
| 2569 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2570 | if (error != JDWP::ERR_NONE) { |
| 2571 | return error; |
| 2572 | } |
| 2573 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2574 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2575 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2576 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2577 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2578 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2579 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2580 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2581 | return visitor.GetError(); |
| 2582 | } |
| 2583 | |
| 2584 | // Read the values from visitor's context. |
| 2585 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2586 | expandBufAdd4BE(pReply, slot_count); /* "int values" */ |
| 2587 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2588 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2589 | JDWP::JdwpTag reqSigByte = request->ReadTag(); |
| 2590 | |
| 2591 | VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte; |
| 2592 | |
| 2593 | size_t width = Dbg::GetTagWidth(reqSigByte); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 2594 | uint8_t* ptr = expandBufAddSpace(pReply, width + 1); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2595 | error = Dbg::GetLocalValue(visitor, soa, slot, reqSigByte, ptr, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2596 | if (error != JDWP::ERR_NONE) { |
| 2597 | return error; |
| 2598 | } |
| 2599 | } |
| 2600 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2601 | } |
| 2602 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2603 | constexpr JDWP::JdwpError kStackFrameLocalAccessError = JDWP::ERR_ABSENT_INFORMATION; |
| 2604 | |
| 2605 | static std::string GetStackContextAsString(const StackVisitor& visitor) |
| 2606 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2607 | return StringPrintf(" at DEX pc 0x%08x in method %s", visitor.GetDexPc(false), |
| 2608 | PrettyMethod(visitor.GetMethod()).c_str()); |
| 2609 | } |
| 2610 | |
| 2611 | static JDWP::JdwpError FailGetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2612 | JDWP::JdwpTag tag) |
| 2613 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2614 | LOG(ERROR) << "Failed to read " << tag << " local from register v" << vreg |
| 2615 | << GetStackContextAsString(visitor); |
| 2616 | return kStackFrameLocalAccessError; |
| 2617 | } |
| 2618 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2619 | JDWP::JdwpError Dbg::GetLocalValue(const StackVisitor& visitor, ScopedObjectAccessUnchecked& soa, |
| 2620 | int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2621 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2622 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2623 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2624 | if (error != JDWP::ERR_NONE) { |
| 2625 | return error; |
| 2626 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2627 | // 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] | 2628 | switch (tag) { |
| 2629 | case JDWP::JT_BOOLEAN: { |
| 2630 | CHECK_EQ(width, 1U); |
| 2631 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2632 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2633 | return FailGetLocalValue(visitor, vreg, tag); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2634 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2635 | VLOG(jdwp) << "get boolean local " << vreg << " = " << intVal; |
| 2636 | JDWP::Set1(buf + 1, intVal != 0); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2637 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2638 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2639 | case JDWP::JT_BYTE: { |
| 2640 | CHECK_EQ(width, 1U); |
| 2641 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2642 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2643 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2644 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2645 | VLOG(jdwp) << "get byte local " << vreg << " = " << intVal; |
| 2646 | JDWP::Set1(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2647 | break; |
| 2648 | } |
| 2649 | case JDWP::JT_SHORT: |
| 2650 | case JDWP::JT_CHAR: { |
| 2651 | CHECK_EQ(width, 2U); |
| 2652 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2653 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2654 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2655 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2656 | VLOG(jdwp) << "get short/char local " << vreg << " = " << intVal; |
| 2657 | JDWP::Set2BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2658 | break; |
| 2659 | } |
| 2660 | case JDWP::JT_INT: { |
| 2661 | CHECK_EQ(width, 4U); |
| 2662 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2663 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2664 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2665 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2666 | VLOG(jdwp) << "get int local " << vreg << " = " << intVal; |
| 2667 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2668 | break; |
| 2669 | } |
| 2670 | case JDWP::JT_FLOAT: { |
| 2671 | CHECK_EQ(width, 4U); |
| 2672 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2673 | if (!visitor.GetVReg(m, vreg, kFloatVReg, &intVal)) { |
| 2674 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2675 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2676 | VLOG(jdwp) << "get float local " << vreg << " = " << intVal; |
| 2677 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2678 | break; |
| 2679 | } |
| 2680 | case JDWP::JT_ARRAY: |
| 2681 | case JDWP::JT_CLASS_LOADER: |
| 2682 | case JDWP::JT_CLASS_OBJECT: |
| 2683 | case JDWP::JT_OBJECT: |
| 2684 | case JDWP::JT_STRING: |
| 2685 | case JDWP::JT_THREAD: |
| 2686 | case JDWP::JT_THREAD_GROUP: { |
| 2687 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
| 2688 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2689 | if (!visitor.GetVReg(m, vreg, kReferenceVReg, &intVal)) { |
| 2690 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2691 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2692 | mirror::Object* o = reinterpret_cast<mirror::Object*>(intVal); |
| 2693 | VLOG(jdwp) << "get " << tag << " object local " << vreg << " = " << o; |
| 2694 | if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) { |
| 2695 | LOG(FATAL) << StringPrintf("Found invalid object %#" PRIxPTR " in register v%u", |
| 2696 | reinterpret_cast<uintptr_t>(o), vreg) |
| 2697 | << GetStackContextAsString(visitor); |
| 2698 | UNREACHABLE(); |
| 2699 | } |
| 2700 | tag = TagFromObject(soa, o); |
| 2701 | JDWP::SetObjectId(buf + 1, gRegistry->Add(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2702 | break; |
| 2703 | } |
| 2704 | case JDWP::JT_DOUBLE: { |
| 2705 | CHECK_EQ(width, 8U); |
| 2706 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2707 | if (!visitor.GetVRegPair(m, vreg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) { |
| 2708 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2709 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2710 | VLOG(jdwp) << "get double local " << vreg << " = " << longVal; |
| 2711 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2712 | break; |
| 2713 | } |
| 2714 | case JDWP::JT_LONG: { |
| 2715 | CHECK_EQ(width, 8U); |
| 2716 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2717 | if (!visitor.GetVRegPair(m, vreg, kLongLoVReg, kLongHiVReg, &longVal)) { |
| 2718 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2719 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2720 | VLOG(jdwp) << "get long local " << vreg << " = " << longVal; |
| 2721 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2722 | break; |
| 2723 | } |
| 2724 | default: |
| 2725 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2726 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2727 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2728 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2729 | // Prepend tag, which may have been updated. |
| 2730 | JDWP::Set1(buf, tag); |
| 2731 | return JDWP::ERR_NONE; |
| 2732 | } |
| 2733 | |
| 2734 | JDWP::JdwpError Dbg::SetLocalValues(JDWP::Request* request) { |
| 2735 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2736 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2737 | |
| 2738 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2739 | JDWP::JdwpError error; |
| 2740 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2741 | if (error != JDWP::ERR_NONE) { |
| 2742 | return error; |
| 2743 | } |
| 2744 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2745 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2746 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2747 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2748 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2749 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2750 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2751 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2752 | return visitor.GetError(); |
| 2753 | } |
| 2754 | |
| 2755 | // Writes the values into visitor's context. |
| 2756 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2757 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2758 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2759 | JDWP::JdwpTag sigByte = request->ReadTag(); |
| 2760 | size_t width = Dbg::GetTagWidth(sigByte); |
| 2761 | uint64_t value = request->ReadValue(width); |
| 2762 | |
| 2763 | VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value; |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2764 | error = Dbg::SetLocalValue(visitor, slot, sigByte, value, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2765 | if (error != JDWP::ERR_NONE) { |
| 2766 | return error; |
| 2767 | } |
| 2768 | } |
| 2769 | return JDWP::ERR_NONE; |
| 2770 | } |
| 2771 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2772 | template<typename T> |
| 2773 | static JDWP::JdwpError FailSetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2774 | JDWP::JdwpTag tag, T value) |
| 2775 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2776 | LOG(ERROR) << "Failed to write " << tag << " local " << value |
| 2777 | << " (0x" << std::hex << value << ") into register v" << vreg |
| 2778 | << GetStackContextAsString(visitor); |
| 2779 | return kStackFrameLocalAccessError; |
| 2780 | } |
| 2781 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2782 | JDWP::JdwpError Dbg::SetLocalValue(StackVisitor& visitor, int slot, JDWP::JdwpTag tag, |
| 2783 | uint64_t value, size_t width) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2784 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2785 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2786 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2787 | if (error != JDWP::ERR_NONE) { |
| 2788 | return error; |
| 2789 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2790 | // 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] | 2791 | switch (tag) { |
| 2792 | case JDWP::JT_BOOLEAN: |
| 2793 | case JDWP::JT_BYTE: |
| 2794 | CHECK_EQ(width, 1U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2795 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2796 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2797 | } |
| 2798 | break; |
| 2799 | case JDWP::JT_SHORT: |
| 2800 | case JDWP::JT_CHAR: |
| 2801 | CHECK_EQ(width, 2U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2802 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2803 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2804 | } |
| 2805 | break; |
| 2806 | case JDWP::JT_INT: |
| 2807 | CHECK_EQ(width, 4U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2808 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2809 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2810 | } |
| 2811 | break; |
| 2812 | case JDWP::JT_FLOAT: |
| 2813 | CHECK_EQ(width, 4U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2814 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kFloatVReg)) { |
| 2815 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2816 | } |
| 2817 | break; |
| 2818 | case JDWP::JT_ARRAY: |
| 2819 | case JDWP::JT_CLASS_LOADER: |
| 2820 | case JDWP::JT_CLASS_OBJECT: |
| 2821 | case JDWP::JT_OBJECT: |
| 2822 | case JDWP::JT_STRING: |
| 2823 | case JDWP::JT_THREAD: |
| 2824 | case JDWP::JT_THREAD_GROUP: { |
| 2825 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2826 | mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value), |
| 2827 | &error); |
| 2828 | if (error != JDWP::ERR_NONE) { |
| 2829 | VLOG(jdwp) << tag << " object " << o << " is an invalid object"; |
| 2830 | return JDWP::ERR_INVALID_OBJECT; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2831 | } |
| 2832 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), |
| 2833 | kReferenceVReg)) { |
| 2834 | return FailSetLocalValue(visitor, vreg, tag, reinterpret_cast<uintptr_t>(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2835 | } |
| 2836 | break; |
| 2837 | } |
| 2838 | case JDWP::JT_DOUBLE: { |
| 2839 | CHECK_EQ(width, 8U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2840 | if (!visitor.SetVRegPair(m, vreg, value, kDoubleLoVReg, kDoubleHiVReg)) { |
| 2841 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2842 | } |
| 2843 | break; |
| 2844 | } |
| 2845 | case JDWP::JT_LONG: { |
| 2846 | CHECK_EQ(width, 8U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2847 | if (!visitor.SetVRegPair(m, vreg, value, kLongLoVReg, kLongHiVReg)) { |
| 2848 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2849 | } |
| 2850 | break; |
| 2851 | } |
| 2852 | default: |
| 2853 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2854 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2855 | } |
| 2856 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2859 | static void SetEventLocation(JDWP::EventLocation* location, ArtMethod* m, uint32_t dex_pc) |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2860 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2861 | DCHECK(location != nullptr); |
| 2862 | if (m == nullptr) { |
| 2863 | memset(location, 0, sizeof(*location)); |
| 2864 | } else { |
| 2865 | location->method = m; |
| 2866 | 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] | 2867 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2868 | } |
| 2869 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2870 | void Dbg::PostLocationEvent(ArtMethod* m, int dex_pc, mirror::Object* this_object, |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2871 | int event_flags, const JValue* return_value) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2872 | if (!IsDebuggerActive()) { |
| 2873 | return; |
| 2874 | } |
| 2875 | DCHECK(m != nullptr); |
| 2876 | DCHECK_EQ(m->IsStatic(), this_object == nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2877 | JDWP::EventLocation location; |
| 2878 | SetEventLocation(&location, m, dex_pc); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2879 | |
Sebastien Hertz | 54d6573 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2880 | // We need to be sure no exception is pending when calling JdwpState::PostLocationEvent. |
| 2881 | // This is required to be able to call JNI functions to create JDWP ids. To achieve this, |
| 2882 | // we temporarily clear the current thread's exception (if any) and will restore it after |
| 2883 | // the call. |
| 2884 | // Note: the only way to get a pending exception here is to suspend on a move-exception |
| 2885 | // instruction. |
| 2886 | Thread* const self = Thread::Current(); |
| 2887 | StackHandleScope<1> hs(self); |
| 2888 | Handle<mirror::Throwable> pending_exception(hs.NewHandle(self->GetException())); |
| 2889 | self->ClearException(); |
| 2890 | if (kIsDebugBuild && pending_exception.Get() != nullptr) { |
| 2891 | const DexFile::CodeItem* code_item = location.method->GetCodeItem(); |
| 2892 | const Instruction* instr = Instruction::At(&code_item->insns_[location.dex_pc]); |
| 2893 | CHECK_EQ(Instruction::MOVE_EXCEPTION, instr->Opcode()); |
| 2894 | } |
| 2895 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2896 | gJdwpState->PostLocationEvent(&location, this_object, event_flags, return_value); |
Sebastien Hertz | 54d6573 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2897 | |
| 2898 | if (pending_exception.Get() != nullptr) { |
| 2899 | self->SetException(pending_exception.Get()); |
| 2900 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2901 | } |
| 2902 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2903 | void Dbg::PostFieldAccessEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2904 | mirror::Object* this_object, ArtField* f) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2905 | if (!IsDebuggerActive()) { |
| 2906 | return; |
| 2907 | } |
| 2908 | DCHECK(m != nullptr); |
| 2909 | DCHECK(f != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2910 | JDWP::EventLocation location; |
| 2911 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2912 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2913 | gJdwpState->PostFieldEvent(&location, f, this_object, nullptr, false); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2914 | } |
| 2915 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2916 | void Dbg::PostFieldModificationEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2917 | mirror::Object* this_object, ArtField* f, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2918 | const JValue* field_value) { |
| 2919 | if (!IsDebuggerActive()) { |
| 2920 | return; |
| 2921 | } |
| 2922 | DCHECK(m != nullptr); |
| 2923 | DCHECK(f != nullptr); |
| 2924 | DCHECK(field_value != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2925 | JDWP::EventLocation location; |
| 2926 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2927 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2928 | gJdwpState->PostFieldEvent(&location, f, this_object, field_value, true); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2929 | } |
| 2930 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2931 | /** |
| 2932 | * Finds the location where this exception will be caught. We search until we reach the top |
| 2933 | * frame, in which case this exception is considered uncaught. |
| 2934 | */ |
| 2935 | class CatchLocationFinder : public StackVisitor { |
| 2936 | public: |
| 2937 | CatchLocationFinder(Thread* self, const Handle<mirror::Throwable>& exception, Context* context) |
| 2938 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2939 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2940 | self_(self), |
| 2941 | exception_(exception), |
| 2942 | handle_scope_(self), |
| 2943 | this_at_throw_(handle_scope_.NewHandle<mirror::Object>(nullptr)), |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2944 | catch_method_(nullptr), |
| 2945 | throw_method_(nullptr), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2946 | catch_dex_pc_(DexFile::kDexNoIndex), |
| 2947 | throw_dex_pc_(DexFile::kDexNoIndex) { |
| 2948 | } |
| 2949 | |
| 2950 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2951 | ArtMethod* method = GetMethod(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2952 | DCHECK(method != nullptr); |
| 2953 | if (method->IsRuntimeMethod()) { |
| 2954 | // Ignore callee save method. |
| 2955 | DCHECK(method->IsCalleeSaveMethod()); |
| 2956 | return true; |
| 2957 | } |
| 2958 | |
| 2959 | uint32_t dex_pc = GetDexPc(); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2960 | if (throw_method_ == nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2961 | // First Java method found. It is either the method that threw the exception, |
| 2962 | // or the Java native method that is reporting an exception thrown by |
| 2963 | // native code. |
| 2964 | this_at_throw_.Assign(GetThisObject()); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2965 | throw_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2966 | throw_dex_pc_ = dex_pc; |
| 2967 | } |
| 2968 | |
| 2969 | if (dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2970 | StackHandleScope<1> hs(self_); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2971 | uint32_t found_dex_pc; |
| 2972 | Handle<mirror::Class> exception_class(hs.NewHandle(exception_->GetClass())); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2973 | bool unused_clear_exception; |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2974 | found_dex_pc = method->FindCatchBlock(exception_class, dex_pc, &unused_clear_exception); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2975 | if (found_dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2976 | catch_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2977 | catch_dex_pc_ = found_dex_pc; |
| 2978 | return false; // End stack walk. |
| 2979 | } |
| 2980 | } |
| 2981 | return true; // Continue stack walk. |
| 2982 | } |
| 2983 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2984 | ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2985 | return catch_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2988 | ArtMethod* GetThrowMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2989 | return throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | mirror::Object* GetThisAtThrow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2993 | return this_at_throw_.Get(); |
| 2994 | } |
| 2995 | |
| 2996 | uint32_t GetCatchDexPc() const { |
| 2997 | return catch_dex_pc_; |
| 2998 | } |
| 2999 | |
| 3000 | uint32_t GetThrowDexPc() const { |
| 3001 | return throw_dex_pc_; |
| 3002 | } |
| 3003 | |
| 3004 | private: |
| 3005 | Thread* const self_; |
| 3006 | const Handle<mirror::Throwable>& exception_; |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3007 | StackHandleScope<1> handle_scope_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3008 | MutableHandle<mirror::Object> this_at_throw_; |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3009 | ArtMethod* catch_method_; |
| 3010 | ArtMethod* throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3011 | uint32_t catch_dex_pc_; |
| 3012 | uint32_t throw_dex_pc_; |
| 3013 | |
| 3014 | DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder); |
| 3015 | }; |
| 3016 | |
| 3017 | void Dbg::PostException(mirror::Throwable* exception_object) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 3018 | if (!IsDebuggerActive()) { |
Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 3019 | return; |
| 3020 | } |
Sebastien Hertz | 6650075 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 3021 | Thread* const self = Thread::Current(); |
| 3022 | StackHandleScope<1> handle_scope(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3023 | Handle<mirror::Throwable> h_exception(handle_scope.NewHandle(exception_object)); |
| 3024 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 6650075 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 3025 | CatchLocationFinder clf(self, h_exception, context.get()); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3026 | clf.WalkStack(/* include_transitions */ false); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3027 | JDWP::EventLocation exception_throw_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3028 | SetEventLocation(&exception_throw_location, clf.GetThrowMethod(), clf.GetThrowDexPc()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3029 | JDWP::EventLocation exception_catch_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3030 | SetEventLocation(&exception_catch_location, clf.GetCatchMethod(), clf.GetCatchDexPc()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3031 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3032 | gJdwpState->PostException(&exception_throw_location, h_exception.Get(), &exception_catch_location, |
| 3033 | clf.GetThisAtThrow()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3034 | } |
| 3035 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3036 | void Dbg::PostClassPrepare(mirror::Class* c) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 3037 | if (!IsDebuggerActive()) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3038 | return; |
| 3039 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3040 | gJdwpState->PostClassPrepare(c); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3041 | } |
| 3042 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3043 | void Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3044 | ArtMethod* m, uint32_t dex_pc, |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 3045 | int event_flags, const JValue* return_value) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3046 | if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 3047 | return; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3048 | } |
| 3049 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3050 | if (IsBreakpoint(m, dex_pc)) { |
| 3051 | event_flags |= kBreakpoint; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3052 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3053 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3054 | // If the debugger is single-stepping one of our threads, check to |
| 3055 | // see if we're that thread and we've reached a step point. |
| 3056 | const SingleStepControl* single_step_control = thread->GetSingleStepControl(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3057 | if (single_step_control != nullptr) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3058 | CHECK(!m->IsNative()); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3059 | if (single_step_control->GetStepDepth() == JDWP::SD_INTO) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3060 | // Step into method calls. We break when the line number |
| 3061 | // or method pointer changes. If we're in SS_MIN mode, we |
| 3062 | // always stop. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3063 | if (single_step_control->GetMethod() != m) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3064 | event_flags |= kSingleStep; |
| 3065 | VLOG(jdwp) << "SS new method"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3066 | } else if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3067 | event_flags |= kSingleStep; |
| 3068 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3069 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3070 | event_flags |= kSingleStep; |
| 3071 | VLOG(jdwp) << "SS new line"; |
| 3072 | } |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3073 | } else if (single_step_control->GetStepDepth() == JDWP::SD_OVER) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3074 | // Step over method calls. We break when the line number is |
| 3075 | // different and the frame depth is <= the original frame |
| 3076 | // depth. (We can't just compare on the method, because we |
| 3077 | // might get unrolled past it by an exception, and it's tricky |
| 3078 | // to identify recursion.) |
| 3079 | |
| 3080 | int stack_depth = GetStackDepth(thread); |
| 3081 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3082 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3083 | // Popped up one or more frames, always trigger. |
| 3084 | event_flags |= kSingleStep; |
| 3085 | VLOG(jdwp) << "SS method pop"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3086 | } else if (stack_depth == single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3087 | // Same depth, see if we moved. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3088 | if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3089 | event_flags |= kSingleStep; |
| 3090 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3091 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3092 | event_flags |= kSingleStep; |
| 3093 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3094 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3095 | } |
| 3096 | } else { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3097 | CHECK_EQ(single_step_control->GetStepDepth(), JDWP::SD_OUT); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3098 | // Return from the current method. We break when the frame |
| 3099 | // depth pops up. |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3100 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3101 | // This differs from the "method exit" break in that it stops |
| 3102 | // with the PC at the next instruction in the returned-to |
| 3103 | // function, rather than the end of the returning function. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3104 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3105 | int stack_depth = GetStackDepth(thread); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3106 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3107 | event_flags |= kSingleStep; |
| 3108 | VLOG(jdwp) << "SS method pop"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3109 | } |
| 3110 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3111 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3112 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3113 | // If there's something interesting going on, see if it matches one |
| 3114 | // of the debugger filters. |
| 3115 | if (event_flags != 0) { |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 3116 | Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3117 | } |
| 3118 | } |
| 3119 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3120 | size_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) { |
| 3121 | switch (instrumentation_event) { |
| 3122 | case instrumentation::Instrumentation::kMethodEntered: |
| 3123 | return &method_enter_event_ref_count_; |
| 3124 | case instrumentation::Instrumentation::kMethodExited: |
| 3125 | return &method_exit_event_ref_count_; |
| 3126 | case instrumentation::Instrumentation::kDexPcMoved: |
| 3127 | return &dex_pc_change_event_ref_count_; |
| 3128 | case instrumentation::Instrumentation::kFieldRead: |
| 3129 | return &field_read_event_ref_count_; |
| 3130 | case instrumentation::Instrumentation::kFieldWritten: |
| 3131 | return &field_write_event_ref_count_; |
| 3132 | case instrumentation::Instrumentation::kExceptionCaught: |
| 3133 | return &exception_catch_event_ref_count_; |
| 3134 | default: |
| 3135 | return nullptr; |
| 3136 | } |
| 3137 | } |
| 3138 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3139 | // Process request while all mutator threads are suspended. |
| 3140 | void Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3141 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3142 | switch (request.GetKind()) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3143 | case DeoptimizationRequest::kNothing: |
| 3144 | LOG(WARNING) << "Ignoring empty deoptimization request."; |
| 3145 | break; |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3146 | case DeoptimizationRequest::kRegisterForEvent: |
| 3147 | VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3148 | request.InstrumentationEvent()); |
| 3149 | instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent()); |
| 3150 | instrumentation_events_ |= request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3151 | break; |
| 3152 | case DeoptimizationRequest::kUnregisterForEvent: |
| 3153 | VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3154 | request.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3155 | instrumentation->RemoveListener(&gDebugInstrumentationListener, |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3156 | request.InstrumentationEvent()); |
| 3157 | instrumentation_events_ &= ~request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3158 | break; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3159 | case DeoptimizationRequest::kFullDeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3160 | VLOG(jdwp) << "Deoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3161 | instrumentation->DeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3162 | VLOG(jdwp) << "Deoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3163 | break; |
| 3164 | case DeoptimizationRequest::kFullUndeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3165 | VLOG(jdwp) << "Undeoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3166 | instrumentation->UndeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3167 | VLOG(jdwp) << "Undeoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3168 | break; |
| 3169 | case DeoptimizationRequest::kSelectiveDeoptimization: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3170 | VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " ..."; |
| 3171 | instrumentation->Deoptimize(request.Method()); |
| 3172 | VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3173 | break; |
| 3174 | case DeoptimizationRequest::kSelectiveUndeoptimization: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3175 | VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " ..."; |
| 3176 | instrumentation->Undeoptimize(request.Method()); |
| 3177 | VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3178 | break; |
| 3179 | default: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3180 | LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3181 | break; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3182 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3183 | } |
| 3184 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3185 | void Dbg::RequestDeoptimization(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3186 | if (req.GetKind() == DeoptimizationRequest::kNothing) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3187 | // Nothing to do. |
| 3188 | return; |
| 3189 | } |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3190 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3191 | RequestDeoptimizationLocked(req); |
| 3192 | } |
| 3193 | |
| 3194 | void Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3195 | switch (req.GetKind()) { |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3196 | case DeoptimizationRequest::kRegisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3197 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3198 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3199 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3200 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3201 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3202 | 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] | 3203 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3204 | deoptimization_requests_.push_back(req); |
| 3205 | } |
| 3206 | *counter = *counter + 1; |
| 3207 | break; |
| 3208 | } |
| 3209 | case DeoptimizationRequest::kUnregisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3210 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3211 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3212 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3213 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3214 | *counter = *counter - 1; |
| 3215 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3216 | 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] | 3217 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3218 | deoptimization_requests_.push_back(req); |
| 3219 | } |
| 3220 | break; |
| 3221 | } |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3222 | case DeoptimizationRequest::kFullDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3223 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3224 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3225 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3226 | << " for full deoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3227 | deoptimization_requests_.push_back(req); |
| 3228 | } |
| 3229 | ++full_deoptimization_event_count_; |
| 3230 | break; |
| 3231 | } |
| 3232 | case DeoptimizationRequest::kFullUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3233 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | e713d93 | 2014-05-15 10:48:53 +0200 | [diff] [blame] | 3234 | DCHECK_GT(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3235 | --full_deoptimization_event_count_; |
| 3236 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3237 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3238 | << " for full undeoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3239 | deoptimization_requests_.push_back(req); |
| 3240 | } |
| 3241 | break; |
| 3242 | } |
| 3243 | case DeoptimizationRequest::kSelectiveDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3244 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3245 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3246 | << " for deoptimization of " << PrettyMethod(req.Method()); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3247 | deoptimization_requests_.push_back(req); |
| 3248 | break; |
| 3249 | } |
| 3250 | case DeoptimizationRequest::kSelectiveUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3251 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3252 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3253 | << " for undeoptimization of " << PrettyMethod(req.Method()); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3254 | deoptimization_requests_.push_back(req); |
| 3255 | break; |
| 3256 | } |
| 3257 | default: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3258 | LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3259 | break; |
| 3260 | } |
| 3261 | } |
| 3262 | } |
| 3263 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3264 | void Dbg::ManageDeoptimization() { |
| 3265 | Thread* const self = Thread::Current(); |
| 3266 | { |
| 3267 | // Avoid suspend/resume if there is no pending request. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3268 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3269 | if (deoptimization_requests_.empty()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3270 | return; |
| 3271 | } |
| 3272 | } |
| 3273 | CHECK_EQ(self->GetState(), kRunnable); |
| 3274 | self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization); |
| 3275 | // We need to suspend mutator threads first. |
| 3276 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 3277 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3278 | const ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3279 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3280 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3281 | size_t req_index = 0; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3282 | for (DeoptimizationRequest& request : deoptimization_requests_) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3283 | VLOG(jdwp) << "Process deoptimization request #" << req_index++; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3284 | ProcessDeoptimizationRequest(request); |
| 3285 | } |
| 3286 | deoptimization_requests_.clear(); |
| 3287 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3288 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 3289 | runtime->GetThreadList()->ResumeAll(); |
| 3290 | self->TransitionFromSuspendedToRunnable(); |
| 3291 | } |
| 3292 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3293 | static bool IsMethodPossiblyInlined(Thread* self, ArtMethod* m) |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3294 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3295 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3296 | if (code_item == nullptr) { |
| 3297 | // TODO We should not be asked to watch location in a native or abstract method so the code item |
| 3298 | // should never be null. We could just check we never encounter this case. |
| 3299 | return false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3300 | } |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3301 | // Note: method verifier may cause thread suspension. |
| 3302 | self->AssertThreadSuspensionIsAllowable(); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3303 | StackHandleScope<2> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3304 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 3305 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 3306 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3307 | verifier::MethodVerifier verifier(self, dex_cache->GetDexFile(), dex_cache, class_loader, |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3308 | &m->GetClassDef(), code_item, m->GetDexMethodIndex(), m, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 3309 | m->GetAccessFlags(), false, true, false, true); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3310 | // Note: we don't need to verify the method. |
| 3311 | return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr); |
| 3312 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3313 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3314 | static const Breakpoint* FindFirstBreakpointForMethod(ArtMethod* m) |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3315 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3316 | for (Breakpoint& breakpoint : gBreakpoints) { |
| 3317 | if (breakpoint.Method() == m) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3318 | return &breakpoint; |
| 3319 | } |
| 3320 | } |
| 3321 | return nullptr; |
| 3322 | } |
| 3323 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3324 | bool Dbg::MethodHasAnyBreakpoints(ArtMethod* method) { |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 3325 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| 3326 | return FindFirstBreakpointForMethod(method) != nullptr; |
| 3327 | } |
| 3328 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3329 | // Sanity checks all existing breakpoints on the same method. |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3330 | static void SanityCheckExistingBreakpoints(ArtMethod* m, |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3331 | DeoptimizationRequest::Kind deoptimization_kind) |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3332 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3333 | for (const Breakpoint& breakpoint : gBreakpoints) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3334 | if (breakpoint.Method() == m) { |
| 3335 | CHECK_EQ(deoptimization_kind, breakpoint.GetDeoptimizationKind()); |
| 3336 | } |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3337 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3338 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 3339 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3340 | // We should have deoptimized everything but not "selectively" deoptimized this method. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3341 | CHECK(instrumentation->AreAllMethodsDeoptimized()); |
| 3342 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3343 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3344 | // We should have "selectively" deoptimized this method. |
| 3345 | // Note: while we have not deoptimized everything for this method, we may have done it for |
| 3346 | // another event. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3347 | CHECK(instrumentation->IsDeoptimized(m)); |
| 3348 | } else { |
| 3349 | // This method does not require deoptimization. |
| 3350 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3351 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3352 | } |
| 3353 | } |
| 3354 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3355 | // Returns the deoptimization kind required to set a breakpoint in a method. |
| 3356 | // If a breakpoint has already been set, we also return the first breakpoint |
| 3357 | // through the given 'existing_brkpt' pointer. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3358 | static DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self, |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3359 | ArtMethod* m, |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3360 | const Breakpoint** existing_brkpt) |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3361 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3362 | if (!Dbg::RequiresDeoptimization()) { |
| 3363 | // We already run in interpreter-only mode so we don't need to deoptimize anything. |
| 3364 | VLOG(jdwp) << "No need for deoptimization when fully running with interpreter for method " |
| 3365 | << PrettyMethod(m); |
| 3366 | return DeoptimizationRequest::kNothing; |
| 3367 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3368 | const Breakpoint* first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3369 | { |
| 3370 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3371 | first_breakpoint = FindFirstBreakpointForMethod(m); |
| 3372 | *existing_brkpt = first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3373 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3374 | |
| 3375 | if (first_breakpoint == nullptr) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3376 | // There is no breakpoint on this method yet: we need to deoptimize. If this method may be |
| 3377 | // inlined, we deoptimize everything; otherwise we deoptimize only this method. |
| 3378 | // Note: IsMethodPossiblyInlined goes into the method verifier and may cause thread suspension. |
| 3379 | // Therefore we must not hold any lock when we call it. |
| 3380 | bool need_full_deoptimization = IsMethodPossiblyInlined(self, m); |
| 3381 | if (need_full_deoptimization) { |
| 3382 | VLOG(jdwp) << "Need full deoptimization because of possible inlining of method " |
| 3383 | << PrettyMethod(m); |
| 3384 | return DeoptimizationRequest::kFullDeoptimization; |
| 3385 | } else { |
| 3386 | // We don't need to deoptimize if the method has not been compiled. |
| 3387 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 3388 | const bool is_compiled = class_linker->GetOatMethodQuickCodeFor(m) != nullptr; |
| 3389 | if (is_compiled) { |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 3390 | // If the method may be called through its direct code pointer (without loading |
| 3391 | // its updated entrypoint), we need full deoptimization to not miss the breakpoint. |
| 3392 | if (class_linker->MayBeCalledWithDirectCodePointer(m)) { |
| 3393 | VLOG(jdwp) << "Need full deoptimization because of possible direct code call " |
| 3394 | << "into image for compiled method " << PrettyMethod(m); |
| 3395 | return DeoptimizationRequest::kFullDeoptimization; |
| 3396 | } else { |
| 3397 | VLOG(jdwp) << "Need selective deoptimization for compiled method " << PrettyMethod(m); |
| 3398 | return DeoptimizationRequest::kSelectiveDeoptimization; |
| 3399 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3400 | } else { |
| 3401 | // Method is not compiled: we don't need to deoptimize. |
| 3402 | VLOG(jdwp) << "No need for deoptimization for non-compiled method " << PrettyMethod(m); |
| 3403 | return DeoptimizationRequest::kNothing; |
| 3404 | } |
| 3405 | } |
| 3406 | } else { |
| 3407 | // There is at least one breakpoint for this method: we don't need to deoptimize. |
| 3408 | // Let's check that all breakpoints are configured the same way for deoptimization. |
| 3409 | VLOG(jdwp) << "Breakpoint already set: no deoptimization is required"; |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3410 | DeoptimizationRequest::Kind deoptimization_kind = first_breakpoint->GetDeoptimizationKind(); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3411 | if (kIsDebugBuild) { |
| 3412 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
| 3413 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
| 3414 | } |
| 3415 | return DeoptimizationRequest::kNothing; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3416 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3417 | } |
| 3418 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3419 | // Installs a breakpoint at the specified location. Also indicates through the deoptimization |
| 3420 | // request if we need to deoptimize. |
| 3421 | void Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
| 3422 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3423 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3424 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3425 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3426 | const Breakpoint* existing_breakpoint = nullptr; |
| 3427 | const DeoptimizationRequest::Kind deoptimization_kind = |
| 3428 | GetRequiredDeoptimizationKind(self, m, &existing_breakpoint); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3429 | req->SetKind(deoptimization_kind); |
| 3430 | if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
| 3431 | req->SetMethod(m); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3432 | } else { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3433 | CHECK(deoptimization_kind == DeoptimizationRequest::kNothing || |
| 3434 | deoptimization_kind == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3435 | req->SetMethod(nullptr); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3436 | } |
| 3437 | |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3438 | { |
| 3439 | WriterMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3440 | // If there is at least one existing breakpoint on the same method, the new breakpoint |
| 3441 | // must have the same deoptimization kind than the existing breakpoint(s). |
| 3442 | DeoptimizationRequest::Kind breakpoint_deoptimization_kind; |
| 3443 | if (existing_breakpoint != nullptr) { |
| 3444 | breakpoint_deoptimization_kind = existing_breakpoint->GetDeoptimizationKind(); |
| 3445 | } else { |
| 3446 | breakpoint_deoptimization_kind = deoptimization_kind; |
| 3447 | } |
| 3448 | gBreakpoints.push_back(Breakpoint(m, location->dex_pc, breakpoint_deoptimization_kind)); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3449 | VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " |
| 3450 | << gBreakpoints[gBreakpoints.size() - 1]; |
| 3451 | } |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | // Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization |
| 3455 | // request if we need to undeoptimize. |
| 3456 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3457 | WriterMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3458 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3459 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3460 | DeoptimizationRequest::Kind deoptimization_kind = DeoptimizationRequest::kNothing; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3461 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3462 | if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].Method() == m) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3463 | VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i]; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3464 | deoptimization_kind = gBreakpoints[i].GetDeoptimizationKind(); |
| 3465 | DCHECK_EQ(deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization, |
| 3466 | Runtime::Current()->GetInstrumentation()->IsDeoptimized(m)); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3467 | gBreakpoints.erase(gBreakpoints.begin() + i); |
| 3468 | break; |
| 3469 | } |
| 3470 | } |
| 3471 | const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m); |
| 3472 | if (existing_breakpoint == nullptr) { |
| 3473 | // There is no more breakpoint on this method: we need to undeoptimize. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3474 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3475 | // This method required full deoptimization: we need to undeoptimize everything. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3476 | req->SetKind(DeoptimizationRequest::kFullUndeoptimization); |
| 3477 | req->SetMethod(nullptr); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3478 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3479 | // This method required selective deoptimization: we need to undeoptimize only that method. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3480 | req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization); |
| 3481 | req->SetMethod(m); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3482 | } else { |
| 3483 | // This method had no need for deoptimization: do nothing. |
| 3484 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3485 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3486 | req->SetMethod(nullptr); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3487 | } |
| 3488 | } else { |
| 3489 | // 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] | 3490 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3491 | req->SetMethod(nullptr); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3492 | if (kIsDebugBuild) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3493 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3494 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3495 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3496 | } |
| 3497 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3498 | bool Dbg::IsForcedInterpreterNeededForCallingImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3499 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3500 | if (ssc == nullptr) { |
| 3501 | // If we are not single-stepping, then we don't have to force interpreter. |
| 3502 | return false; |
| 3503 | } |
| 3504 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
| 3505 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3506 | return false; |
| 3507 | } |
| 3508 | |
| 3509 | if (!m->IsNative() && !m->IsProxyMethod()) { |
| 3510 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3511 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3512 | return true; |
| 3513 | } |
| 3514 | } |
| 3515 | return false; |
| 3516 | } |
| 3517 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3518 | bool Dbg::IsForcedInterpreterNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3519 | instrumentation::Instrumentation* const instrumentation = |
| 3520 | Runtime::Current()->GetInstrumentation(); |
| 3521 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3522 | if (instrumentation->InterpretOnly()) { |
| 3523 | return false; |
| 3524 | } |
| 3525 | // We can only interpret pure Java method. |
| 3526 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3527 | return false; |
| 3528 | } |
| 3529 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3530 | if (ssc != nullptr) { |
| 3531 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3532 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3533 | return true; |
| 3534 | } |
| 3535 | // If we are stepping out from a static initializer, by issuing a step |
| 3536 | // in or step over, that was implicitly invoked by calling a static method, |
| 3537 | // then we need to step into that method. Having a lower stack depth than |
| 3538 | // the one the single step control has indicates that the step originates |
| 3539 | // from the static initializer. |
| 3540 | if (ssc->GetStepDepth() != JDWP::SD_OUT && |
| 3541 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3542 | return true; |
| 3543 | } |
| 3544 | } |
| 3545 | // There are cases where we have to force interpreter on deoptimized methods, |
| 3546 | // because in some cases the call will not be performed by invoking an entry |
| 3547 | // point that has been replaced by the deoptimization, but instead by directly |
| 3548 | // invoking the compiled code of the method, for example. |
| 3549 | return instrumentation->IsDeoptimized(m); |
| 3550 | } |
| 3551 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3552 | bool Dbg::IsForcedInstrumentationNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3553 | // 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] | 3554 | if (m == nullptr) { |
| 3555 | return false; |
| 3556 | } |
| 3557 | instrumentation::Instrumentation* const instrumentation = |
| 3558 | Runtime::Current()->GetInstrumentation(); |
| 3559 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3560 | if (instrumentation->InterpretOnly()) { |
| 3561 | return false; |
| 3562 | } |
| 3563 | // We can only interpret pure Java method. |
| 3564 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3565 | return false; |
| 3566 | } |
| 3567 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3568 | if (ssc != nullptr) { |
| 3569 | // If we are stepping out from a static initializer, by issuing a step |
| 3570 | // out, that was implicitly invoked by calling a static method, then we |
| 3571 | // need to step into the caller of that method. Having a lower stack |
| 3572 | // depth than the one the single step control has indicates that the |
| 3573 | // step originates from the static initializer. |
| 3574 | if (ssc->GetStepDepth() == JDWP::SD_OUT && |
| 3575 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3576 | return true; |
| 3577 | } |
| 3578 | } |
| 3579 | // If we are returning from a static intializer, that was implicitly |
| 3580 | // invoked by calling a static method and the caller is deoptimized, |
| 3581 | // then we have to deoptimize the stack without forcing interpreter |
| 3582 | // on the static method that was called originally. This problem can |
| 3583 | // be solved easily by forcing instrumentation on the called method, |
| 3584 | // because the instrumentation exit hook will recognise the need of |
| 3585 | // stack deoptimization by calling IsForcedInterpreterNeededForUpcall. |
| 3586 | return instrumentation->IsDeoptimized(m); |
| 3587 | } |
| 3588 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3589 | bool Dbg::IsForcedInterpreterNeededForUpcallImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3590 | // 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] | 3591 | if (m == nullptr) { |
| 3592 | return false; |
| 3593 | } |
| 3594 | instrumentation::Instrumentation* const instrumentation = |
| 3595 | Runtime::Current()->GetInstrumentation(); |
| 3596 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3597 | if (instrumentation->InterpretOnly()) { |
| 3598 | return false; |
| 3599 | } |
| 3600 | // We can only interpret pure Java method. |
| 3601 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3602 | return false; |
| 3603 | } |
| 3604 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3605 | if (ssc != nullptr) { |
| 3606 | // The debugger is not interested in what is happening under the level |
| 3607 | // of the step, thus we only force interpreter when we are not below of |
| 3608 | // the step. |
| 3609 | if (ssc->GetStackDepth() >= GetStackDepth(thread)) { |
| 3610 | return true; |
| 3611 | } |
| 3612 | } |
| 3613 | // We have to require stack deoptimization if the upcall is deoptimized. |
| 3614 | return instrumentation->IsDeoptimized(m); |
| 3615 | } |
| 3616 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3617 | // Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't |
| 3618 | // cause suspension if the thread is the current thread. |
| 3619 | class ScopedThreadSuspension { |
| 3620 | public: |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3621 | ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id) |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 3622 | LOCKS_EXCLUDED(Locks::thread_list_lock_) |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3623 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3624 | thread_(nullptr), |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3625 | error_(JDWP::ERR_NONE), |
| 3626 | self_suspend_(false), |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3627 | other_suspend_(false) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3628 | ScopedObjectAccessUnchecked soa(self); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 3629 | thread_ = DecodeThread(soa, thread_id, &error_); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3630 | if (error_ == JDWP::ERR_NONE) { |
| 3631 | if (thread_ == soa.Self()) { |
| 3632 | self_suspend_ = true; |
| 3633 | } else { |
| 3634 | soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3635 | jobject thread_peer = Dbg::GetObjectRegistry()->GetJObject(thread_id); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3636 | bool timed_out; |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 3637 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 3638 | Thread* suspended_thread = thread_list->SuspendThreadByPeer(thread_peer, true, true, |
| 3639 | &timed_out); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3640 | CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension); |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3641 | if (suspended_thread == nullptr) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3642 | // Thread terminated from under us while suspending. |
| 3643 | error_ = JDWP::ERR_INVALID_THREAD; |
| 3644 | } else { |
| 3645 | CHECK_EQ(suspended_thread, thread_); |
| 3646 | other_suspend_ = true; |
| 3647 | } |
| 3648 | } |
| 3649 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3650 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3651 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3652 | Thread* GetThread() const { |
| 3653 | return thread_; |
| 3654 | } |
| 3655 | |
| 3656 | JDWP::JdwpError GetError() const { |
| 3657 | return error_; |
| 3658 | } |
| 3659 | |
| 3660 | ~ScopedThreadSuspension() { |
| 3661 | if (other_suspend_) { |
| 3662 | Runtime::Current()->GetThreadList()->Resume(thread_, true); |
| 3663 | } |
| 3664 | } |
| 3665 | |
| 3666 | private: |
| 3667 | Thread* thread_; |
| 3668 | JDWP::JdwpError error_; |
| 3669 | bool self_suspend_; |
| 3670 | bool other_suspend_; |
| 3671 | }; |
| 3672 | |
| 3673 | JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size, |
| 3674 | JDWP::JdwpStepDepth step_depth) { |
| 3675 | Thread* self = Thread::Current(); |
| 3676 | ScopedThreadSuspension sts(self, thread_id); |
| 3677 | if (sts.GetError() != JDWP::ERR_NONE) { |
| 3678 | return sts.GetError(); |
| 3679 | } |
| 3680 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3681 | // 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] | 3682 | // is for step-out. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3683 | struct SingleStepStackVisitor : public StackVisitor { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3684 | explicit SingleStepStackVisitor(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 3685 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 3686 | stack_depth(0), |
| 3687 | method(nullptr), |
| 3688 | line_number(-1) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 3689 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3690 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 3691 | // annotalysis. |
| 3692 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3693 | ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3694 | if (!m->IsRuntimeMethod()) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3695 | ++stack_depth; |
| 3696 | if (method == nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3697 | mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3698 | method = m; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3699 | if (dex_cache != nullptr) { |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 3700 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3701 | line_number = dex_file.GetLineNumFromPC(m, GetDexPc()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3702 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3703 | } |
| 3704 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 3705 | return true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3706 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3707 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3708 | int stack_depth; |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3709 | ArtMethod* method; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3710 | int32_t line_number; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3711 | }; |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3712 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3713 | Thread* const thread = sts.GetThread(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3714 | SingleStepStackVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3715 | visitor.WalkStack(); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3716 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3717 | // 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] | 3718 | struct DebugCallbackContext { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3719 | explicit DebugCallbackContext(SingleStepControl* single_step_control_cb, |
| 3720 | int32_t line_number_cb, const DexFile::CodeItem* code_item) |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3721 | : single_step_control_(single_step_control_cb), line_number_(line_number_cb), |
| 3722 | code_item_(code_item), last_pc_valid(false), last_pc(0) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3723 | } |
| 3724 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3725 | 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] | 3726 | DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3727 | if (static_cast<int32_t>(line_number_cb) == context->line_number_) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3728 | if (!context->last_pc_valid) { |
| 3729 | // Everything from this address until the next line change is ours. |
| 3730 | context->last_pc = address; |
| 3731 | context->last_pc_valid = true; |
| 3732 | } |
| 3733 | // Otherwise, if we're already in a valid range for this line, |
| 3734 | // just keep going (shouldn't really happen)... |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3735 | } else if (context->last_pc_valid) { // and the line number is new |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3736 | // Add everything from the last entry up until here to the set |
| 3737 | 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] | 3738 | context->single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3739 | } |
| 3740 | context->last_pc_valid = false; |
| 3741 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3742 | return false; // There may be multiple entries for any given line. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3743 | } |
| 3744 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3745 | ~DebugCallbackContext() { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3746 | // If the line number was the last in the position table... |
| 3747 | if (last_pc_valid) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3748 | size_t end = code_item_->insns_size_in_code_units_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3749 | for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3750 | single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3751 | } |
| 3752 | } |
| 3753 | } |
| 3754 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3755 | SingleStepControl* const single_step_control_; |
| 3756 | const int32_t line_number_; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3757 | const DexFile::CodeItem* const code_item_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3758 | bool last_pc_valid; |
| 3759 | uint32_t last_pc; |
| 3760 | }; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3761 | |
| 3762 | // Allocate single step. |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3763 | SingleStepControl* single_step_control = |
| 3764 | new (std::nothrow) SingleStepControl(step_size, step_depth, |
| 3765 | visitor.stack_depth, visitor.method); |
| 3766 | if (single_step_control == nullptr) { |
| 3767 | LOG(ERROR) << "Failed to allocate SingleStepControl"; |
| 3768 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3769 | } |
| 3770 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3771 | ArtMethod* m = single_step_control->GetMethod(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3772 | const int32_t line_number = visitor.line_number; |
Sebastien Hertz | 1309ba2 | 2015-05-28 11:00:57 +0200 | [diff] [blame] | 3773 | // Note: if the thread is not running Java code (pure native thread), there is no "current" |
| 3774 | // method on the stack (and no line number either). |
| 3775 | if (m != nullptr && !m->IsNative()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3776 | const DexFile::CodeItem* const code_item = m->GetCodeItem(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3777 | DebugCallbackContext context(single_step_control, line_number, code_item); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3778 | m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3779 | DebugCallbackContext::Callback, nullptr, &context); |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 3780 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3781 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3782 | // Activate single-step in the thread. |
| 3783 | thread->ActivateSingleStepControl(single_step_control); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3784 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3785 | if (VLOG_IS_ON(jdwp)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3786 | VLOG(jdwp) << "Single-step thread: " << *thread; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3787 | VLOG(jdwp) << "Single-step step size: " << single_step_control->GetStepSize(); |
| 3788 | VLOG(jdwp) << "Single-step step depth: " << single_step_control->GetStepDepth(); |
| 3789 | VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->GetMethod()); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3790 | VLOG(jdwp) << "Single-step current line: " << line_number; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3791 | VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->GetStackDepth(); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3792 | VLOG(jdwp) << "Single-step dex_pc values:"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3793 | for (uint32_t dex_pc : single_step_control->GetDexPcs()) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3794 | VLOG(jdwp) << StringPrintf(" %#x", dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3795 | } |
| 3796 | } |
| 3797 | |
| 3798 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3799 | } |
| 3800 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3801 | void Dbg::UnconfigureStep(JDWP::ObjectId thread_id) { |
| 3802 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3803 | JDWP::JdwpError error; |
| 3804 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Sebastien Hertz | 87118ed | 2013-11-26 17:57:18 +0100 | [diff] [blame] | 3805 | if (error == JDWP::ERR_NONE) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3806 | thread->DeactivateSingleStepControl(); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3807 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3808 | } |
| 3809 | |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3810 | static char JdwpTagToShortyChar(JDWP::JdwpTag tag) { |
| 3811 | switch (tag) { |
| 3812 | default: |
| 3813 | LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag); |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 3814 | UNREACHABLE(); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3815 | |
| 3816 | // Primitives. |
| 3817 | case JDWP::JT_BYTE: return 'B'; |
| 3818 | case JDWP::JT_CHAR: return 'C'; |
| 3819 | case JDWP::JT_FLOAT: return 'F'; |
| 3820 | case JDWP::JT_DOUBLE: return 'D'; |
| 3821 | case JDWP::JT_INT: return 'I'; |
| 3822 | case JDWP::JT_LONG: return 'J'; |
| 3823 | case JDWP::JT_SHORT: return 'S'; |
| 3824 | case JDWP::JT_VOID: return 'V'; |
| 3825 | case JDWP::JT_BOOLEAN: return 'Z'; |
| 3826 | |
| 3827 | // Reference types. |
| 3828 | case JDWP::JT_ARRAY: |
| 3829 | case JDWP::JT_OBJECT: |
| 3830 | case JDWP::JT_STRING: |
| 3831 | case JDWP::JT_THREAD: |
| 3832 | case JDWP::JT_THREAD_GROUP: |
| 3833 | case JDWP::JT_CLASS_LOADER: |
| 3834 | case JDWP::JT_CLASS_OBJECT: |
| 3835 | return 'L'; |
| 3836 | } |
| 3837 | } |
| 3838 | |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3839 | JDWP::JdwpError Dbg::PrepareInvokeMethod(uint32_t request_id, JDWP::ObjectId thread_id, |
| 3840 | JDWP::ObjectId object_id, JDWP::RefTypeId class_id, |
| 3841 | JDWP::MethodId method_id, uint32_t arg_count, |
| 3842 | uint64_t arg_values[], JDWP::JdwpTag* arg_types, |
| 3843 | uint32_t options) { |
| 3844 | Thread* const self = Thread::Current(); |
| 3845 | CHECK_EQ(self, GetDebugThread()) << "This must be called by the JDWP thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3846 | |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3847 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3848 | Thread* targetThread = nullptr; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3849 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3850 | ScopedObjectAccessUnchecked soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3851 | JDWP::JdwpError error; |
| 3852 | targetThread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 3853 | if (error != JDWP::ERR_NONE) { |
| 3854 | LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id; |
| 3855 | return error; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3856 | } |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3857 | if (targetThread->GetInvokeReq() != nullptr) { |
| 3858 | // Thread is already invoking a method on behalf of the debugger. |
| 3859 | LOG(ERROR) << "InvokeMethod request for thread already invoking a method: " << *targetThread; |
| 3860 | return JDWP::ERR_ALREADY_INVOKING; |
| 3861 | } |
| 3862 | if (!targetThread->IsReadyForDebugInvoke()) { |
| 3863 | // 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] | 3864 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 3865 | return JDWP::ERR_INVALID_THREAD; |
| 3866 | } |
| 3867 | |
| 3868 | /* |
| 3869 | * We currently have a bug where we don't successfully resume the |
| 3870 | * target thread if the suspend count is too deep. We're expected to |
| 3871 | * require one "resume" for each "suspend", but when asked to execute |
| 3872 | * a method we have to resume fully and then re-suspend it back to the |
| 3873 | * same level. (The easiest way to cause this is to type "suspend" |
| 3874 | * multiple times in jdb.) |
| 3875 | * |
| 3876 | * It's unclear what this means when the event specifies "resume all" |
| 3877 | * and some threads are suspended more deeply than others. This is |
| 3878 | * a rare problem, so for now we just prevent it from hanging forever |
| 3879 | * by rejecting the method invocation request. Without this, we will |
| 3880 | * be stuck waiting on a suspended thread. |
| 3881 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3882 | int suspend_count; |
| 3883 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3884 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3885 | suspend_count = targetThread->GetSuspendCount(); |
| 3886 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3887 | if (suspend_count > 1) { |
| 3888 | LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3889 | return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here. |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3890 | } |
| 3891 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3892 | mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 3893 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3894 | return JDWP::ERR_INVALID_OBJECT; |
| 3895 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3896 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3897 | gRegistry->Get<mirror::Object*>(thread_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3898 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3899 | return JDWP::ERR_INVALID_OBJECT; |
| 3900 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3901 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3902 | mirror::Class* c = DecodeClass(class_id, &error); |
| 3903 | if (c == nullptr) { |
| 3904 | return error; |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3905 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3906 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3907 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3908 | if (m->IsStatic() != (receiver == nullptr)) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3909 | return JDWP::ERR_INVALID_METHODID; |
| 3910 | } |
| 3911 | if (m->IsStatic()) { |
| 3912 | if (m->GetDeclaringClass() != c) { |
| 3913 | return JDWP::ERR_INVALID_METHODID; |
| 3914 | } |
| 3915 | } else { |
| 3916 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
| 3917 | return JDWP::ERR_INVALID_METHODID; |
| 3918 | } |
| 3919 | } |
| 3920 | |
| 3921 | // Check the argument list matches the method. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3922 | uint32_t shorty_len = 0; |
| 3923 | const char* shorty = m->GetShorty(&shorty_len); |
| 3924 | if (shorty_len - 1 != arg_count) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3925 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3926 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3927 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3928 | { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3929 | StackHandleScope<2> hs(soa.Self()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3930 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver)); |
| 3931 | HandleWrapper<mirror::Class> h_klass(hs.NewHandleWrapper(&c)); |
| 3932 | const DexFile::TypeList* types = m->GetParameterTypeList(); |
| 3933 | for (size_t i = 0; i < arg_count; ++i) { |
| 3934 | if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) { |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3935 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3936 | } |
| 3937 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3938 | if (shorty[i + 1] == 'L') { |
| 3939 | // Did we really get an argument of an appropriate reference type? |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 3940 | mirror::Class* parameter_type = |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3941 | m->GetClassFromTypeIndex(types->GetTypeItem(i).type_idx_, true); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3942 | mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i], &error); |
| 3943 | if (error != JDWP::ERR_NONE) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3944 | return JDWP::ERR_INVALID_OBJECT; |
| 3945 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3946 | if (argument != nullptr && !argument->InstanceOf(parameter_type)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3947 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3948 | } |
| 3949 | |
| 3950 | // Turn the on-the-wire ObjectId into a jobject. |
| 3951 | jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]); |
| 3952 | v.l = gRegistry->GetJObject(arg_values[i]); |
| 3953 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3954 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3955 | } |
| 3956 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3957 | // Allocates a DebugInvokeReq. |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3958 | DebugInvokeReq* req = new (std::nothrow) DebugInvokeReq(request_id, thread_id, receiver, c, m, |
| 3959 | options, arg_values, arg_count); |
| 3960 | if (req == nullptr) { |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3961 | LOG(ERROR) << "Failed to allocate DebugInvokeReq"; |
| 3962 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3963 | } |
| 3964 | |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3965 | // Attaches the DebugInvokeReq to the target thread so it executes the method when |
| 3966 | // it is resumed. Once the invocation completes, the target thread will delete it before |
| 3967 | // suspending itself (see ThreadList::SuspendSelfForDebugger). |
| 3968 | targetThread->SetDebugInvokeReq(req); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3969 | } |
| 3970 | |
| 3971 | // The fact that we've released the thread list lock is a bit risky --- if the thread goes |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3972 | // away we're sitting high and dry -- but we must release this before the UndoDebuggerSuspensions |
| 3973 | // call. |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3974 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3975 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3976 | VLOG(jdwp) << " Resuming all threads"; |
| 3977 | thread_list->UndoDebuggerSuspensions(); |
| 3978 | } else { |
| 3979 | VLOG(jdwp) << " Resuming event thread only"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3980 | thread_list->Resume(targetThread, true); |
| 3981 | } |
| 3982 | |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3983 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3987 | Thread* const self = Thread::Current(); |
| 3988 | CHECK_NE(self, GetDebugThread()) << "This must be called by the event thread"; |
| 3989 | |
| 3990 | ScopedObjectAccess soa(self); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3991 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 3992 | // We can be called while an exception is pending. We need |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3993 | // to preserve that across the method invocation. |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3994 | StackHandleScope<1> hs(soa.Self()); |
| 3995 | Handle<mirror::Throwable> old_exception = hs.NewHandle(soa.Self()->GetException()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3996 | soa.Self()->ClearException(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3997 | |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3998 | // Execute the method then sends reply to the debugger. |
| 3999 | ExecuteMethodWithoutPendingException(soa, pReq); |
| 4000 | |
| 4001 | // If an exception was pending before the invoke, restore it now. |
| 4002 | if (old_exception.Get() != nullptr) { |
| 4003 | soa.Self()->SetException(old_exception.Get()); |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | // Helper function: write a variable-width value into the output input buffer. |
| 4008 | static void WriteValue(JDWP::ExpandBuf* pReply, int width, uint64_t value) { |
| 4009 | switch (width) { |
| 4010 | case 1: |
| 4011 | expandBufAdd1(pReply, value); |
| 4012 | break; |
| 4013 | case 2: |
| 4014 | expandBufAdd2BE(pReply, value); |
| 4015 | break; |
| 4016 | case 4: |
| 4017 | expandBufAdd4BE(pReply, value); |
| 4018 | break; |
| 4019 | case 8: |
| 4020 | expandBufAdd8BE(pReply, value); |
| 4021 | break; |
| 4022 | default: |
| 4023 | LOG(FATAL) << width; |
| 4024 | UNREACHABLE(); |
| 4025 | } |
| 4026 | } |
| 4027 | |
| 4028 | void Dbg::ExecuteMethodWithoutPendingException(ScopedObjectAccess& soa, DebugInvokeReq* pReq) { |
| 4029 | soa.Self()->AssertNoPendingException(); |
| 4030 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4031 | // Translate the method through the vtable, unless the debugger wants to suppress it. |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4032 | ArtMethod* m = pReq->method; |
| 4033 | size_t image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4034 | if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver.Read() != nullptr) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4035 | ArtMethod* actual_method = |
| 4036 | pReq->klass.Read()->FindVirtualMethodForVirtualOrInterface(m, image_pointer_size); |
| 4037 | if (actual_method != m) { |
| 4038 | VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4039 | << " to " << PrettyMethod(actual_method); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4040 | m = actual_method; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 4041 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4042 | } |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4043 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m) |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4044 | << " receiver=" << pReq->receiver.Read() |
Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 4045 | << " arg_count=" << pReq->arg_count; |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4046 | CHECK(m != nullptr); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4047 | |
| 4048 | CHECK_EQ(sizeof(jvalue), sizeof(uint64_t)); |
| 4049 | |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4050 | // Invoke the method. |
Jeff Hao | 15e9ad1 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 4051 | ScopedLocalRef<jobject> ref(soa.Env(), soa.AddLocalReference<jobject>(pReq->receiver.Read())); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4052 | JValue result = InvokeWithJValues(soa, ref.get(), soa.EncodeMethod(m), |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4053 | reinterpret_cast<jvalue*>(pReq->arg_values.get())); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4054 | |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4055 | // Prepare JDWP ids for the reply. |
| 4056 | JDWP::JdwpTag result_tag = BasicTagFromDescriptor(m->GetShorty()); |
| 4057 | const bool is_object_result = (result_tag == JDWP::JT_OBJECT); |
| 4058 | StackHandleScope<2> hs(soa.Self()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4059 | Handle<mirror::Object> object_result = hs.NewHandle(is_object_result ? result.GetL() : nullptr); |
| 4060 | Handle<mirror::Throwable> exception = hs.NewHandle(soa.Self()->GetException()); |
| 4061 | soa.Self()->ClearException(); |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4062 | |
| 4063 | if (!IsDebuggerActive()) { |
| 4064 | // The debugger detached: we must not re-suspend threads. We also don't need to fill the reply |
| 4065 | // because it won't be sent either. |
| 4066 | return; |
| 4067 | } |
| 4068 | |
| 4069 | JDWP::ObjectId exceptionObjectId = gRegistry->Add(exception); |
| 4070 | uint64_t result_value = 0; |
| 4071 | if (exceptionObjectId != 0) { |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4072 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exception.Get() |
| 4073 | << " " << exception->Dump(); |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4074 | result_value = 0; |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4075 | } else if (is_object_result) { |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4076 | /* if no exception was thrown, examine object result more closely */ |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4077 | JDWP::JdwpTag new_tag = TagFromObject(soa, object_result.Get()); |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4078 | if (new_tag != result_tag) { |
| 4079 | VLOG(jdwp) << " JDWP promoted result from " << result_tag << " to " << new_tag; |
| 4080 | result_tag = new_tag; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4081 | } |
| 4082 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4083 | // Register the object in the registry and reference its ObjectId. This ensures |
| 4084 | // GC safety and prevents from accessing stale reference if the object is moved. |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4085 | result_value = gRegistry->Add(object_result.Get()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4086 | } else { |
| 4087 | // Primitive result. |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4088 | DCHECK(IsPrimitiveTag(result_tag)); |
| 4089 | result_value = result.GetJ(); |
| 4090 | } |
| 4091 | const bool is_constructor = m->IsConstructor() && !m->IsStatic(); |
| 4092 | if (is_constructor) { |
| 4093 | // If we invoked a constructor (which actually returns void), return the receiver, |
| 4094 | // unless we threw, in which case we return null. |
| 4095 | result_tag = JDWP::JT_OBJECT; |
| 4096 | if (exceptionObjectId == 0) { |
| 4097 | // TODO we could keep the receiver ObjectId in the DebugInvokeReq to avoid looking into the |
| 4098 | // object registry. |
| 4099 | result_value = GetObjectRegistry()->Add(pReq->receiver.Read()); |
| 4100 | } else { |
| 4101 | result_value = 0; |
| 4102 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4103 | } |
| 4104 | |
Sebastien Hertz | 6ba35b5 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4105 | // Suspend other threads if the invoke is not single-threaded. |
| 4106 | if ((pReq->options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
| 4107 | soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
| 4108 | VLOG(jdwp) << " Suspending all threads"; |
| 4109 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
| 4110 | soa.Self()->TransitionFromSuspendedToRunnable(); |
| 4111 | } |
| 4112 | |
| 4113 | VLOG(jdwp) << " --> returned " << result_tag |
| 4114 | << StringPrintf(" %#" PRIx64 " (except=%#" PRIx64 ")", result_value, |
| 4115 | exceptionObjectId); |
| 4116 | |
| 4117 | // Show detailed debug output. |
| 4118 | if (result_tag == JDWP::JT_STRING && exceptionObjectId == 0) { |
| 4119 | if (result_value != 0) { |
| 4120 | if (VLOG_IS_ON(jdwp)) { |
| 4121 | std::string result_string; |
| 4122 | JDWP::JdwpError error = Dbg::StringToUtf8(result_value, &result_string); |
| 4123 | CHECK_EQ(error, JDWP::ERR_NONE); |
| 4124 | VLOG(jdwp) << " string '" << result_string << "'"; |
| 4125 | } |
| 4126 | } else { |
| 4127 | VLOG(jdwp) << " string (null)"; |
| 4128 | } |
| 4129 | } |
| 4130 | |
| 4131 | // Attach the reply to DebugInvokeReq so it can be sent to the debugger when the event thread |
| 4132 | // is ready to suspend. |
| 4133 | BuildInvokeReply(pReq->reply, pReq->request_id, result_tag, result_value, exceptionObjectId); |
| 4134 | } |
| 4135 | |
| 4136 | void Dbg::BuildInvokeReply(JDWP::ExpandBuf* pReply, uint32_t request_id, JDWP::JdwpTag result_tag, |
| 4137 | uint64_t result_value, JDWP::ObjectId exception) { |
| 4138 | // Make room for the JDWP header since we do not know the size of the reply yet. |
| 4139 | JDWP::expandBufAddSpace(pReply, kJDWPHeaderLen); |
| 4140 | |
| 4141 | size_t width = GetTagWidth(result_tag); |
| 4142 | JDWP::expandBufAdd1(pReply, result_tag); |
| 4143 | if (width != 0) { |
| 4144 | WriteValue(pReply, width, result_value); |
| 4145 | } |
| 4146 | JDWP::expandBufAdd1(pReply, JDWP::JT_OBJECT); |
| 4147 | JDWP::expandBufAddObjectId(pReply, exception); |
| 4148 | |
| 4149 | // Now we know the size, we can complete the JDWP header. |
| 4150 | uint8_t* buf = expandBufGetBuffer(pReply); |
| 4151 | JDWP::Set4BE(buf + kJDWPHeaderSizeOffset, expandBufGetLength(pReply)); |
| 4152 | JDWP::Set4BE(buf + kJDWPHeaderIdOffset, request_id); |
| 4153 | JDWP::Set1(buf + kJDWPHeaderFlagsOffset, kJDWPFlagReply); // flags |
| 4154 | JDWP::Set2BE(buf + kJDWPHeaderErrorCodeOffset, JDWP::ERR_NONE); |
| 4155 | } |
| 4156 | |
| 4157 | void Dbg::FinishInvokeMethod(DebugInvokeReq* pReq) { |
| 4158 | CHECK_NE(Thread::Current(), GetDebugThread()) << "This must be called by the event thread"; |
| 4159 | |
| 4160 | JDWP::ExpandBuf* const pReply = pReq->reply; |
| 4161 | CHECK(pReply != nullptr) << "No reply attached to DebugInvokeReq"; |
| 4162 | |
| 4163 | // We need to prevent other threads (including JDWP thread) from interacting with the debugger |
| 4164 | // while we send the reply but are not yet suspended. The JDWP token will be released just before |
| 4165 | // we suspend ourself again (see ThreadList::SuspendSelfForDebugger). |
| 4166 | gJdwpState->AcquireJdwpTokenForEvent(pReq->thread_id); |
| 4167 | |
| 4168 | // Send the reply unless the debugger detached before the completion of the method. |
| 4169 | if (IsDebuggerActive()) { |
| 4170 | const size_t replyDataLength = expandBufGetLength(pReply) - kJDWPHeaderLen; |
| 4171 | VLOG(jdwp) << StringPrintf("REPLY INVOKE id=0x%06x (length=%zu)", |
| 4172 | pReq->request_id, replyDataLength); |
| 4173 | |
| 4174 | gJdwpState->SendRequest(pReply); |
| 4175 | } else { |
| 4176 | VLOG(jdwp) << "Not sending invoke reply because debugger detached"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4177 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4178 | } |
| 4179 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4180 | /* |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4181 | * "request" contains a full JDWP packet, possibly with multiple chunks. We |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4182 | * need to process each, accumulate the replies, and ship the whole thing |
| 4183 | * back. |
| 4184 | * |
| 4185 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 4186 | * and includes the chunk type/length, followed by the data. |
| 4187 | * |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 4188 | * 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] | 4189 | * chunk. If this becomes inconvenient we will need to adapt. |
| 4190 | */ |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4191 | bool Dbg::DdmHandlePacket(JDWP::Request* request, uint8_t** pReplyBuf, int* pReplyLen) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4192 | Thread* self = Thread::Current(); |
| 4193 | JNIEnv* env = self->GetJniEnv(); |
| 4194 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4195 | uint32_t type = request->ReadUnsigned32("type"); |
| 4196 | uint32_t length = request->ReadUnsigned32("length"); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4197 | |
| 4198 | // Create a byte[] corresponding to 'request'. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4199 | size_t request_length = request->size(); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4200 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4201 | if (dataArray.get() == nullptr) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4202 | LOG(WARNING) << "byte[] allocation failed: " << request_length; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4203 | env->ExceptionClear(); |
| 4204 | return false; |
| 4205 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4206 | env->SetByteArrayRegion(dataArray.get(), 0, request_length, |
| 4207 | reinterpret_cast<const jbyte*>(request->data())); |
| 4208 | request->Skip(request_length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4209 | |
| 4210 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4211 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4212 | if (length != request_length) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4213 | 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] | 4214 | return false; |
| 4215 | } |
| 4216 | |
| 4217 | // 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] | 4218 | ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4219 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch, |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4220 | type, dataArray.get(), 0, length)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4221 | if (env->ExceptionCheck()) { |
| 4222 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 4223 | env->ExceptionDescribe(); |
| 4224 | env->ExceptionClear(); |
| 4225 | return false; |
| 4226 | } |
| 4227 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4228 | if (chunk.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4229 | return false; |
| 4230 | } |
| 4231 | |
| 4232 | /* |
| 4233 | * Pull the pieces out of the chunk. We copy the results into a |
| 4234 | * newly-allocated buffer that the caller can free. We don't want to |
| 4235 | * continue using the Chunk object because nothing has a reference to it. |
| 4236 | * |
| 4237 | * We could avoid this by returning type/data/offset/length and having |
| 4238 | * the caller be aware of the object lifetime issues, but that |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 4239 | * 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] | 4240 | * if we have responses for multiple chunks. |
| 4241 | * |
| 4242 | * So we're pretty much stuck with copying data around multiple times. |
| 4243 | */ |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4244 | 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] | 4245 | 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] | 4246 | 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] | 4247 | 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] | 4248 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4249 | 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] | 4250 | if (length == 0 || replyData.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4251 | return false; |
| 4252 | } |
| 4253 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4254 | const int kChunkHdrLen = 8; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4255 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4256 | if (reply == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4257 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 4258 | return false; |
| 4259 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 4260 | JDWP::Set4BE(reply + 0, type); |
| 4261 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4262 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4263 | |
| 4264 | *pReplyBuf = reply; |
| 4265 | *pReplyLen = length + kChunkHdrLen; |
| 4266 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4267 | 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] | 4268 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4269 | } |
| 4270 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4271 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4272 | VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4273 | |
| 4274 | Thread* self = Thread::Current(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4275 | if (self->GetState() != kRunnable) { |
| 4276 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 4277 | /* try anyway? */ |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4278 | } |
| 4279 | |
| 4280 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4281 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4282 | env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4283 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast, |
| 4284 | event); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4285 | if (env->ExceptionCheck()) { |
| 4286 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 4287 | env->ExceptionDescribe(); |
| 4288 | env->ExceptionClear(); |
| 4289 | } |
| 4290 | } |
| 4291 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4292 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4293 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4294 | } |
| 4295 | |
| 4296 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4297 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4298 | gDdmThreadNotification = false; |
| 4299 | } |
| 4300 | |
| 4301 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4302 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4303 | * |
| 4304 | * Because we broadcast the full set of threads when the notifications are |
| 4305 | * first enabled, it's possible for "thread" to be actively executing. |
| 4306 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4307 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4308 | if (!gDdmThreadNotification) { |
| 4309 | return; |
| 4310 | } |
| 4311 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4312 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4313 | uint8_t buf[4]; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4314 | JDWP::Set4BE(&buf[0], t->GetThreadId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4315 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4316 | } else { |
| 4317 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4318 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4319 | StackHandleScope<1> hs(soa.Self()); |
| 4320 | Handle<mirror::String> name(hs.NewHandle(t->GetThreadName(soa))); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4321 | size_t char_count = (name.Get() != nullptr) ? name->GetLength() : 0; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 4322 | const jchar* chars = (name.Get() != nullptr) ? name->GetValue() : nullptr; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4323 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4324 | std::vector<uint8_t> bytes; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4325 | JDWP::Append4BE(bytes, t->GetThreadId()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4326 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4327 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 4328 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4329 | } |
| 4330 | } |
| 4331 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4332 | void Dbg::DdmSetThreadNotification(bool enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4333 | // Enable/disable thread notifications. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4334 | gDdmThreadNotification = enable; |
| 4335 | if (enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4336 | // Suspend the VM then post thread start notifications for all threads. Threads attaching will |
| 4337 | // see a suspension in progress and block until that ends. They then post their own start |
| 4338 | // notification. |
| 4339 | SuspendVM(); |
| 4340 | std::list<Thread*> threads; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4341 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4342 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4343 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4344 | threads = Runtime::Current()->GetThreadList()->GetList(); |
| 4345 | } |
| 4346 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4347 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4348 | for (Thread* thread : threads) { |
| 4349 | Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR")); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4350 | } |
| 4351 | } |
| 4352 | ResumeVM(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4353 | } |
| 4354 | } |
| 4355 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4356 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 4357 | if (IsDebuggerActive()) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 4358 | gJdwpState->PostThreadChange(t, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4359 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4360 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4364 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4368 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4369 | } |
| 4370 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4371 | 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] | 4372 | CHECK(buf != nullptr); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4373 | iovec vec[1]; |
| 4374 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 4375 | vec[0].iov_len = byte_count; |
| 4376 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4377 | } |
| 4378 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4379 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 4380 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 4381 | } |
| 4382 | |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 4383 | void Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4384 | if (gJdwpState == nullptr) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4385 | VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4386 | } else { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 4387 | gJdwpState->DdmSendChunkV(type, iov, iov_count); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4388 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4389 | } |
| 4390 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 4391 | JDWP::JdwpState* Dbg::GetJdwpState() { |
| 4392 | return gJdwpState; |
| 4393 | } |
| 4394 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4395 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 4396 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4397 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4398 | return true; |
| 4399 | } |
| 4400 | |
| 4401 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 4402 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 4403 | return false; |
| 4404 | } |
| 4405 | |
| 4406 | gDdmHpifWhen = when; |
| 4407 | return true; |
| 4408 | } |
| 4409 | |
| 4410 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 4411 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 4412 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 4413 | return false; |
| 4414 | } |
| 4415 | |
| 4416 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 4417 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 4418 | return false; |
| 4419 | } |
| 4420 | |
| 4421 | if (native) { |
| 4422 | gDdmNhsgWhen = when; |
| 4423 | gDdmNhsgWhat = what; |
| 4424 | } else { |
| 4425 | gDdmHpsgWhen = when; |
| 4426 | gDdmHpsgWhat = what; |
| 4427 | } |
| 4428 | return true; |
| 4429 | } |
| 4430 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4431 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 4432 | // If there's a one-shot 'when', reset it. |
| 4433 | if (reason == gDdmHpifWhen) { |
| 4434 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 4435 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 4436 | } |
| 4437 | } |
| 4438 | |
| 4439 | /* |
| 4440 | * Chunk HPIF (client --> server) |
| 4441 | * |
| 4442 | * Heap Info. General information about the heap, |
| 4443 | * suitable for a summary display. |
| 4444 | * |
| 4445 | * [u4]: number of heaps |
| 4446 | * |
| 4447 | * For each heap: |
| 4448 | * [u4]: heap ID |
| 4449 | * [u8]: timestamp in ms since Unix epoch |
| 4450 | * [u1]: capture reason (same as 'when' value from server) |
| 4451 | * [u4]: max heap size in bytes (-Xmx) |
| 4452 | * [u4]: current heap size in bytes |
| 4453 | * [u4]: current number of bytes allocated |
| 4454 | * [u4]: current number of objects allocated |
| 4455 | */ |
| 4456 | uint8_t heap_count = 1; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4457 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4458 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4459 | JDWP::Append4BE(bytes, heap_count); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4460 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4461 | JDWP::Append8BE(bytes, MilliTime()); |
| 4462 | JDWP::Append1BE(bytes, reason); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4463 | JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes. |
| 4464 | JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4465 | JDWP::Append4BE(bytes, heap->GetBytesAllocated()); |
| 4466 | JDWP::Append4BE(bytes, heap->GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4467 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 4468 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4469 | } |
| 4470 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4471 | enum HpsgSolidity { |
| 4472 | SOLIDITY_FREE = 0, |
| 4473 | SOLIDITY_HARD = 1, |
| 4474 | SOLIDITY_SOFT = 2, |
| 4475 | SOLIDITY_WEAK = 3, |
| 4476 | SOLIDITY_PHANTOM = 4, |
| 4477 | SOLIDITY_FINALIZABLE = 5, |
| 4478 | SOLIDITY_SWEEP = 6, |
| 4479 | }; |
| 4480 | |
| 4481 | enum HpsgKind { |
| 4482 | KIND_OBJECT = 0, |
| 4483 | KIND_CLASS_OBJECT = 1, |
| 4484 | KIND_ARRAY_1 = 2, |
| 4485 | KIND_ARRAY_2 = 3, |
| 4486 | KIND_ARRAY_4 = 4, |
| 4487 | KIND_ARRAY_8 = 5, |
| 4488 | KIND_UNKNOWN = 6, |
| 4489 | KIND_NATIVE = 7, |
| 4490 | }; |
| 4491 | |
| 4492 | #define HPSG_PARTIAL (1<<7) |
| 4493 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 4494 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4495 | class HeapChunkContext { |
| 4496 | public: |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4497 | // Maximum chunk size. Obtain this from the formula: |
| 4498 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 4499 | HeapChunkContext(bool merge, bool native) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4500 | : buf_(16384 - 16), |
| 4501 | type_(0), |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4502 | chunk_overhead_(0) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4503 | Reset(); |
| 4504 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4505 | type_ = CHUNK_TYPE("NHSG"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4506 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4507 | type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4508 | } |
| 4509 | } |
| 4510 | |
| 4511 | ~HeapChunkContext() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4512 | if (p_ > &buf_[0]) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4513 | Flush(); |
| 4514 | } |
| 4515 | } |
| 4516 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4517 | void SetChunkOverhead(size_t chunk_overhead) { |
| 4518 | chunk_overhead_ = chunk_overhead; |
| 4519 | } |
| 4520 | |
| 4521 | void ResetStartOfNextChunk() { |
| 4522 | startOfNextMemoryChunk_ = nullptr; |
| 4523 | } |
| 4524 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4525 | void EnsureHeader(const void* chunk_ptr) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4526 | if (!needHeader_) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4527 | return; |
| 4528 | } |
| 4529 | |
| 4530 | // Start a new HPSx chunk. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4531 | JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap). |
| 4532 | JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4533 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4534 | JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 4535 | 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] | 4536 | // [u4]: length of piece, in allocation units |
| 4537 | // 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] | 4538 | pieceLenField_ = p_; |
| 4539 | JDWP::Write4BE(&p_, 0x55555555); |
| 4540 | needHeader_ = false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4541 | } |
| 4542 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4543 | void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4544 | if (pieceLenField_ == nullptr) { |
Ian Rogers | d636b06 | 2013-01-18 17:51:18 -0800 | [diff] [blame] | 4545 | // Flush immediately post Reset (maybe back-to-back Flush). Ignore. |
| 4546 | CHECK(needHeader_); |
| 4547 | return; |
| 4548 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4549 | // Patch the "length of piece" field. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4550 | CHECK_LE(&buf_[0], pieceLenField_); |
| 4551 | CHECK_LE(pieceLenField_, p_); |
| 4552 | JDWP::Set4BE(pieceLenField_, totalAllocationUnits_); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4553 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4554 | Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4555 | Reset(); |
| 4556 | } |
| 4557 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4558 | 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] | 4559 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 4560 | Locks::mutator_lock_) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4561 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkJavaCallback(start, end, used_bytes); |
| 4562 | } |
| 4563 | |
| 4564 | static void HeapChunkNativeCallback(void* start, void* end, size_t used_bytes, void* arg) |
| 4565 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4566 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkNativeCallback(start, end, used_bytes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4567 | } |
| 4568 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4569 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4570 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 4571 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4572 | void Reset() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4573 | p_ = &buf_[0]; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4574 | ResetStartOfNextChunk(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4575 | totalAllocationUnits_ = 0; |
| 4576 | needHeader_ = true; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4577 | pieceLenField_ = nullptr; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4578 | } |
| 4579 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4580 | bool IsNative() const { |
| 4581 | return type_ == CHUNK_TYPE("NHSG"); |
| 4582 | } |
| 4583 | |
| 4584 | // Returns true if the object is not an empty chunk. |
| 4585 | 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] | 4586 | // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken |
| 4587 | // 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] | 4588 | if (used_bytes == 0) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4589 | if (start == nullptr) { |
| 4590 | // Reset for start of new heap. |
| 4591 | startOfNextMemoryChunk_ = nullptr; |
| 4592 | Flush(); |
| 4593 | } |
| 4594 | // Only process in use memory so that free region information |
| 4595 | // also includes dlmalloc book keeping. |
| 4596 | return false; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4597 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4598 | if (startOfNextMemoryChunk_ != nullptr) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4599 | // Transmit any pending free memory. Native free memory of over kMaxFreeLen could be because |
| 4600 | // of the use of mmaps, so don't report. If not free memory then start a new segment. |
| 4601 | bool flush = true; |
| 4602 | if (start > startOfNextMemoryChunk_) { |
| 4603 | const size_t kMaxFreeLen = 2 * kPageSize; |
| 4604 | void* free_start = startOfNextMemoryChunk_; |
| 4605 | void* free_end = start; |
| 4606 | const size_t free_len = |
| 4607 | reinterpret_cast<uintptr_t>(free_end) - reinterpret_cast<uintptr_t>(free_start); |
| 4608 | if (!IsNative() || free_len < kMaxFreeLen) { |
| 4609 | AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), free_start, free_len, IsNative()); |
| 4610 | flush = false; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4611 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4612 | } |
| 4613 | if (flush) { |
| 4614 | startOfNextMemoryChunk_ = nullptr; |
| 4615 | Flush(); |
| 4616 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4617 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4618 | return true; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4619 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4620 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4621 | void HeapChunkNativeCallback(void* start, void* /*end*/, size_t used_bytes) |
| 4622 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4623 | if (ProcessRecord(start, used_bytes)) { |
| 4624 | uint8_t state = ExamineNativeObject(start); |
| 4625 | AppendChunk(state, start, used_bytes + chunk_overhead_, true /*is_native*/); |
| 4626 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4627 | } |
| 4628 | } |
| 4629 | |
| 4630 | void HeapChunkJavaCallback(void* start, void* /*end*/, size_t used_bytes) |
| 4631 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 4632 | if (ProcessRecord(start, used_bytes)) { |
| 4633 | // Determine the type of this chunk. |
| 4634 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 4635 | // If it's the same, we should combine them. |
| 4636 | uint8_t state = ExamineJavaObject(reinterpret_cast<mirror::Object*>(start)); |
| 4637 | AppendChunk(state, start, used_bytes + chunk_overhead_, false /*is_native*/); |
| 4638 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | 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] | 4643 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4644 | // Make sure there's enough room left in the buffer. |
| 4645 | // We need to use two bytes for every fractional 256 allocation units used by the chunk plus |
| 4646 | // 17 bytes for any header. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4647 | const size_t needed = ((RoundUp(length / ALLOCATION_UNIT_SIZE, 256) / 256) * 2) + 17; |
| 4648 | size_t byte_left = &buf_.back() - p_; |
| 4649 | if (byte_left < needed) { |
| 4650 | if (is_native) { |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4651 | // Cannot trigger memory allocation while walking native heap. |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4652 | return; |
| 4653 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4654 | Flush(); |
| 4655 | } |
| 4656 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4657 | byte_left = &buf_.back() - p_; |
| 4658 | if (byte_left < needed) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4659 | LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", " |
| 4660 | << needed << " bytes)"; |
| 4661 | return; |
| 4662 | } |
| 4663 | EnsureHeader(ptr); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4664 | // Write out the chunk description. |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4665 | length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units. |
| 4666 | totalAllocationUnits_ += length; |
| 4667 | while (length > 256) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4668 | *p_++ = state | HPSG_PARTIAL; |
| 4669 | *p_++ = 255; // length - 1 |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4670 | length -= 256; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4671 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4672 | *p_++ = state; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4673 | *p_++ = length - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4674 | } |
| 4675 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4676 | uint8_t ExamineNativeObject(const void* p) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4677 | return p == nullptr ? HPSG_STATE(SOLIDITY_FREE, 0) : HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4678 | } |
| 4679 | |
| 4680 | uint8_t ExamineJavaObject(mirror::Object* o) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4681 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4682 | if (o == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4683 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 4684 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4685 | // It's an allocated chunk. Figure out what it is. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4686 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 4687 | if (!heap->IsLiveObjectLocked(o)) { |
| 4688 | LOG(ERROR) << "Invalid object in managed heap: " << o; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4689 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4690 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4691 | mirror::Class* c = o->GetClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4692 | if (c == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4693 | // The object was probably just created but hasn't been initialized yet. |
| 4694 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4695 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4696 | if (!heap->IsValidObjectAddress(c)) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4697 | LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4698 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4699 | } |
Mathieu Chartier | f26e1b3 | 2015-01-29 10:47:10 -0800 | [diff] [blame] | 4700 | if (c->GetClass() == nullptr) { |
| 4701 | LOG(ERROR) << "Null class of class " << c << " for object " << o; |
| 4702 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4703 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4704 | if (c->IsClassClass()) { |
| 4705 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 4706 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4707 | if (c->IsArrayClass()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4708 | switch (c->GetComponentSize()) { |
| 4709 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 4710 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 4711 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 4712 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 4713 | } |
| 4714 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4715 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4716 | } |
| 4717 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4718 | std::vector<uint8_t> buf_; |
| 4719 | uint8_t* p_; |
| 4720 | uint8_t* pieceLenField_; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4721 | void* startOfNextMemoryChunk_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4722 | size_t totalAllocationUnits_; |
| 4723 | uint32_t type_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4724 | bool needHeader_; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4725 | size_t chunk_overhead_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4726 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4727 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 4728 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4729 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4730 | static void BumpPointerSpaceCallback(mirror::Object* obj, void* arg) |
| 4731 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 4732 | const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4733 | HeapChunkContext::HeapChunkJavaCallback( |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4734 | obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, arg); |
| 4735 | } |
| 4736 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4737 | void Dbg::DdmSendHeapSegments(bool native) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4738 | Dbg::HpsgWhen when = native ? gDdmNhsgWhen : gDdmHpsgWhen; |
| 4739 | Dbg::HpsgWhat what = native ? gDdmNhsgWhat : gDdmHpsgWhat; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4740 | if (when == HPSG_WHEN_NEVER) { |
| 4741 | return; |
| 4742 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4743 | // Figure out what kind of chunks we'll be sending. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4744 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) |
| 4745 | << static_cast<int>(what); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4746 | |
| 4747 | // First, send a heap start chunk. |
| 4748 | uint8_t heap_id[4]; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4749 | 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] | 4750 | 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] | 4751 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4752 | Locks::mutator_lock_->AssertSharedHeld(self); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4753 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4754 | // Send a series of heap segment chunks. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4755 | HeapChunkContext context(what == HPSG_WHAT_MERGED_OBJECTS, native); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4756 | if (native) { |
Ian Rogers | 872dd82 | 2014-10-30 11:19:14 -0700 | [diff] [blame] | 4757 | #if defined(HAVE_ANDROID_OS) && defined(USE_DLMALLOC) |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4758 | dlmalloc_inspect_all(HeapChunkContext::HeapChunkNativeCallback, &context); |
| 4759 | HeapChunkContext::HeapChunkNativeCallback(nullptr, nullptr, 0, &context); // Indicate end of a space. |
Christopher Ferris | c4ddc04 | 2014-05-13 14:47:50 -0700 | [diff] [blame] | 4760 | #else |
| 4761 | UNIMPLEMENTED(WARNING) << "Native heap inspection is only supported with dlmalloc"; |
| 4762 | #endif |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4763 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4764 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4765 | for (const auto& space : heap->GetContinuousSpaces()) { |
| 4766 | if (space->IsDlMallocSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4767 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4768 | // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an |
| 4769 | // allocation then the first sizeof(size_t) may belong to it. |
| 4770 | context.SetChunkOverhead(sizeof(size_t)); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4771 | space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4772 | } else if (space->IsRosAllocSpace()) { |
| 4773 | context.SetChunkOverhead(0); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4774 | // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since |
| 4775 | // RosAlloc's internal logic doesn't know to release and reacquire the heap bitmap lock. |
| 4776 | self->TransitionFromRunnableToSuspended(kSuspended); |
| 4777 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 4778 | tl->SuspendAll(__FUNCTION__); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4779 | { |
| 4780 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4781 | space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4782 | } |
| 4783 | tl->ResumeAll(); |
| 4784 | self->TransitionFromSuspendedToRunnable(); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4785 | } else if (space->IsBumpPointerSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4786 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4787 | context.SetChunkOverhead(0); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4788 | space->AsBumpPointerSpace()->Walk(BumpPointerSpaceCallback, &context); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4789 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4790 | } else if (space->IsRegionSpace()) { |
| 4791 | heap->IncrementDisableMovingGC(self); |
| 4792 | self->TransitionFromRunnableToSuspended(kSuspended); |
| 4793 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 4794 | tl->SuspendAll(__FUNCTION__); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4795 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 4796 | context.SetChunkOverhead(0); |
| 4797 | space->AsRegionSpace()->Walk(BumpPointerSpaceCallback, &context); |
| 4798 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
| 4799 | tl->ResumeAll(); |
| 4800 | self->TransitionFromSuspendedToRunnable(); |
| 4801 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4802 | } else { |
| 4803 | UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4804 | } |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4805 | context.ResetStartOfNextChunk(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4806 | } |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4807 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 4808 | // Walk the large objects, these are not in the AllocSpace. |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4809 | context.SetChunkOverhead(0); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4810 | heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4811 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4812 | |
| 4813 | // Finally, send a heap end chunk. |
| 4814 | 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] | 4815 | } |
| 4816 | |
Elliott Hughes | b1a5879 | 2013-07-11 18:10:58 -0700 | [diff] [blame] | 4817 | static size_t GetAllocTrackerMax() { |
| 4818 | #ifdef HAVE_ANDROID_OS |
| 4819 | // Check whether there's a system property overriding the number of records. |
| 4820 | const char* propertyName = "dalvik.vm.allocTrackerMax"; |
| 4821 | char allocRecordMaxString[PROPERTY_VALUE_MAX]; |
| 4822 | if (property_get(propertyName, allocRecordMaxString, "") > 0) { |
| 4823 | char* end; |
| 4824 | size_t value = strtoul(allocRecordMaxString, &end, 10); |
| 4825 | if (*end != '\0') { |
Ruben Brunk | 3e47a74 | 2013-09-09 17:56:07 -0700 | [diff] [blame] | 4826 | LOG(ERROR) << "Ignoring " << propertyName << " '" << allocRecordMaxString |
| 4827 | << "' --- invalid"; |
Elliott Hughes | b1a5879 | 2013-07-11 18:10:58 -0700 | [diff] [blame] | 4828 | return kDefaultNumAllocRecords; |
| 4829 | } |
| 4830 | if (!IsPowerOfTwo(value)) { |
Ruben Brunk | 3e47a74 | 2013-09-09 17:56:07 -0700 | [diff] [blame] | 4831 | LOG(ERROR) << "Ignoring " << propertyName << " '" << allocRecordMaxString |
| 4832 | << "' --- not power of two"; |
Elliott Hughes | b1a5879 | 2013-07-11 18:10:58 -0700 | [diff] [blame] | 4833 | return kDefaultNumAllocRecords; |
| 4834 | } |
| 4835 | return value; |
| 4836 | } |
| 4837 | #endif |
| 4838 | return kDefaultNumAllocRecords; |
| 4839 | } |
| 4840 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4841 | void Dbg::SetAllocTrackingEnabled(bool enable) { |
| 4842 | Thread* self = Thread::Current(); |
| 4843 | if (enable) { |
Sebastien Hertz | b98063a | 2014-03-26 10:57:20 +0100 | [diff] [blame] | 4844 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4845 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
| 4846 | if (recent_allocation_records_ != nullptr) { |
| 4847 | return; // Already enabled, bail. |
Sebastien Hertz | b98063a | 2014-03-26 10:57:20 +0100 | [diff] [blame] | 4848 | } |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4849 | alloc_record_max_ = GetAllocTrackerMax(); |
| 4850 | LOG(INFO) << "Enabling alloc tracker (" << alloc_record_max_ << " entries of " |
| 4851 | << kMaxAllocRecordStackDepth << " frames, taking " |
| 4852 | << PrettySize(sizeof(AllocRecord) * alloc_record_max_) << ")"; |
| 4853 | DCHECK_EQ(alloc_record_head_, 0U); |
| 4854 | DCHECK_EQ(alloc_record_count_, 0U); |
| 4855 | recent_allocation_records_ = new AllocRecord[alloc_record_max_]; |
| 4856 | CHECK(recent_allocation_records_ != nullptr); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4857 | } |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 4858 | Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4859 | } else { |
Sebastien Hertz | b98063a | 2014-03-26 10:57:20 +0100 | [diff] [blame] | 4860 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4861 | ScopedObjectAccess soa(self); // For type_cache_.Clear(); |
| 4862 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
| 4863 | if (recent_allocation_records_ == nullptr) { |
| 4864 | return; // Already disabled, bail. |
| 4865 | } |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 4866 | LOG(INFO) << "Disabling alloc tracker"; |
Sebastien Hertz | b98063a | 2014-03-26 10:57:20 +0100 | [diff] [blame] | 4867 | delete[] recent_allocation_records_; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4868 | recent_allocation_records_ = nullptr; |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4869 | alloc_record_head_ = 0; |
| 4870 | alloc_record_count_ = 0; |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 4871 | type_cache_.Clear(); |
Sebastien Hertz | b98063a | 2014-03-26 10:57:20 +0100 | [diff] [blame] | 4872 | } |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4873 | // 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] | 4874 | Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4875 | } |
| 4876 | } |
| 4877 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 4878 | struct AllocRecordStackVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 4879 | AllocRecordStackVisitor(Thread* thread, AllocRecord* record_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4880 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 4881 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 4882 | record(record_in), |
| 4883 | depth(0) {} |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4884 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4885 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 4886 | // annotalysis. |
| 4887 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4888 | if (depth >= kMaxAllocRecordStackDepth) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 4889 | return false; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4890 | } |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4891 | ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 4892 | if (!m->IsRuntimeMethod()) { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4893 | record->StackElement(depth)->SetMethod(m); |
| 4894 | record->StackElement(depth)->SetDexPc(GetDexPc()); |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 4895 | ++depth; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4896 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 4897 | return true; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | ~AllocRecordStackVisitor() { |
| 4901 | // Clear out any unused stack trace elements. |
| 4902 | for (; depth < kMaxAllocRecordStackDepth; ++depth) { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4903 | record->StackElement(depth)->SetMethod(nullptr); |
| 4904 | record->StackElement(depth)->SetDexPc(0); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4905 | } |
| 4906 | } |
| 4907 | |
| 4908 | AllocRecord* record; |
| 4909 | size_t depth; |
| 4910 | }; |
| 4911 | |
Ian Rogers | 844506b | 2014-09-12 19:59:33 -0700 | [diff] [blame] | 4912 | void Dbg::RecordAllocation(Thread* self, mirror::Class* type, size_t byte_count) { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4913 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4914 | if (recent_allocation_records_ == nullptr) { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4915 | // In the process of shutting down recording, bail. |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4916 | return; |
| 4917 | } |
| 4918 | |
| 4919 | // Advance and clip. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4920 | if (++alloc_record_head_ == alloc_record_max_) { |
| 4921 | alloc_record_head_ = 0; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4922 | } |
| 4923 | |
| 4924 | // Fill in the basics. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4925 | AllocRecord* record = &recent_allocation_records_[alloc_record_head_]; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4926 | record->SetType(type); |
| 4927 | record->SetByteCount(byte_count); |
| 4928 | record->SetThinLockId(self->GetThreadId()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4929 | |
| 4930 | // Fill in the stack trace. |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 4931 | AllocRecordStackVisitor visitor(self, record); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 4932 | visitor.WalkStack(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4933 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4934 | if (alloc_record_count_ < alloc_record_max_) { |
| 4935 | ++alloc_record_count_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4936 | } |
| 4937 | } |
| 4938 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4939 | // Returns the index of the head element. |
| 4940 | // |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4941 | // 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] | 4942 | // we want to use the current element. Take "head+1" and subtract count |
| 4943 | // from it. |
| 4944 | // |
| 4945 | // We need to handle underflow in our circular buffer, so we add |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4946 | // alloc_record_max_ and then mask it back down. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4947 | size_t Dbg::HeadIndex() { |
| 4948 | return (Dbg::alloc_record_head_ + 1 + Dbg::alloc_record_max_ - Dbg::alloc_record_count_) & |
| 4949 | (Dbg::alloc_record_max_ - 1); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | void Dbg::DumpRecentAllocations() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4953 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4954 | MutexLock mu(soa.Self(), *Locks::alloc_tracker_lock_); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4955 | if (recent_allocation_records_ == nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4956 | LOG(INFO) << "Not recording tracked allocations"; |
| 4957 | return; |
| 4958 | } |
| 4959 | |
| 4960 | // "i" is the head of the list. We want to start at the end of the |
| 4961 | // list and move forward to the tail. |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4962 | size_t i = HeadIndex(); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4963 | const uint16_t capped_count = CappedAllocRecordCount(Dbg::alloc_record_count_); |
| 4964 | uint16_t count = capped_count; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4965 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4966 | LOG(INFO) << "Tracked allocations, (head=" << alloc_record_head_ << " count=" << count << ")"; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4967 | while (count--) { |
| 4968 | AllocRecord* record = &recent_allocation_records_[i]; |
| 4969 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4970 | LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->ThinLockId(), record->ByteCount()) |
| 4971 | << PrettyClass(record->Type()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4972 | |
| 4973 | for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4974 | AllocRecordStackTraceElement* stack_element = record->StackElement(stack_frame); |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4975 | ArtMethod* m = stack_element->Method(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4976 | if (m == nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4977 | break; |
| 4978 | } |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4979 | LOG(INFO) << " " << PrettyMethod(m) << " line " << stack_element->LineNumber(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | // pause periodically to help logcat catch up |
| 4983 | if ((count % 5) == 0) { |
| 4984 | usleep(40000); |
| 4985 | } |
| 4986 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 4987 | i = (i + 1) & (alloc_record_max_ - 1); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4988 | } |
| 4989 | } |
| 4990 | |
| 4991 | class StringTable { |
| 4992 | public: |
| 4993 | StringTable() { |
| 4994 | } |
| 4995 | |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 4996 | void Add(const std::string& str) { |
| 4997 | table_.insert(str); |
| 4998 | } |
| 4999 | |
| 5000 | void Add(const char* str) { |
| 5001 | table_.insert(str); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5002 | } |
| 5003 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 5004 | size_t IndexOf(const char* s) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 5005 | auto it = table_.find(s); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 5006 | if (it == table_.end()) { |
| 5007 | LOG(FATAL) << "IndexOf(\"" << s << "\") failed"; |
| 5008 | } |
| 5009 | return std::distance(table_.begin(), it); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5010 | } |
| 5011 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 5012 | size_t Size() const { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5013 | return table_.size(); |
| 5014 | } |
| 5015 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 5016 | void WriteTo(std::vector<uint8_t>& bytes) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 5017 | for (const std::string& str : table_) { |
| 5018 | const char* s = str.c_str(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5019 | size_t s_len = CountModifiedUtf8Chars(s); |
Christopher Ferris | 8a35405 | 2015-04-24 17:23:53 -0700 | [diff] [blame] | 5020 | std::unique_ptr<uint16_t[]> s_utf16(new uint16_t[s_len]); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5021 | ConvertModifiedUtf8ToUtf16(s_utf16.get(), s); |
| 5022 | JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5023 | } |
| 5024 | } |
| 5025 | |
| 5026 | private: |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 5027 | std::set<std::string> table_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5028 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 5029 | }; |
| 5030 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5031 | static const char* GetMethodSourceFile(ArtMethod* method) |
Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 5032 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5033 | DCHECK(method != nullptr); |
| 5034 | const char* source_file = method->GetDeclaringClassSourceFile(); |
Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 5035 | return (source_file != nullptr) ? source_file : ""; |
| 5036 | } |
| 5037 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5038 | /* |
| 5039 | * The data we send to DDMS contains everything we have recorded. |
| 5040 | * |
| 5041 | * Message header (all values big-endian): |
| 5042 | * (1b) message header len (to allow future expansion); includes itself |
| 5043 | * (1b) entry header len |
| 5044 | * (1b) stack frame len |
| 5045 | * (2b) number of entries |
| 5046 | * (4b) offset to string table from start of message |
| 5047 | * (2b) number of class name strings |
| 5048 | * (2b) number of method name strings |
| 5049 | * (2b) number of source file name strings |
| 5050 | * For each entry: |
| 5051 | * (4b) total allocation size |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 5052 | * (2b) thread id |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5053 | * (2b) allocated object's class name index |
| 5054 | * (1b) stack depth |
| 5055 | * For each stack frame: |
| 5056 | * (2b) method's class name |
| 5057 | * (2b) method name |
| 5058 | * (2b) method source file |
| 5059 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 5060 | * (xb) class name strings |
| 5061 | * (xb) method name strings |
| 5062 | * (xb) source file strings |
| 5063 | * |
| 5064 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 5065 | * followed by UTF-16 data. |
| 5066 | * |
| 5067 | * 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] | 5068 | * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5069 | * each table, but in practice there should be far fewer. |
| 5070 | * |
| 5071 | * The chief reason for using a string table here is to keep the size of |
| 5072 | * the DDMS message to a minimum. This is partly to make the protocol |
| 5073 | * efficient, but also because we have to form the whole thing up all at |
| 5074 | * once in a memory buffer. |
| 5075 | * |
| 5076 | * We use separate string tables for class names, method names, and source |
| 5077 | * files to keep the indexes small. There will generally be no overlap |
| 5078 | * between the contents of these tables. |
| 5079 | */ |
| 5080 | jbyteArray Dbg::GetRecentAllocations() { |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 5081 | if ((false)) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5082 | DumpRecentAllocations(); |
| 5083 | } |
| 5084 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 5085 | Thread* self = Thread::Current(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5086 | std::vector<uint8_t> bytes; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5087 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 5088 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5089 | // |
| 5090 | // Part 1: generate string tables. |
| 5091 | // |
| 5092 | StringTable class_names; |
| 5093 | StringTable method_names; |
| 5094 | StringTable filenames; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5095 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 5096 | const uint16_t capped_count = CappedAllocRecordCount(Dbg::alloc_record_count_); |
| 5097 | uint16_t count = capped_count; |
| 5098 | size_t idx = HeadIndex(); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5099 | while (count--) { |
| 5100 | AllocRecord* record = &recent_allocation_records_[idx]; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5101 | std::string temp; |
| 5102 | class_names.Add(record->Type()->GetDescriptor(&temp)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5103 | for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) { |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5104 | ArtMethod* m = record->StackElement(i)->Method(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 5105 | if (m != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5106 | class_names.Add(m->GetDeclaringClassDescriptor()); |
| 5107 | method_names.Add(m->GetName()); |
| 5108 | filenames.Add(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5109 | } |
| 5110 | } |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5111 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 5112 | idx = (idx + 1) & (alloc_record_max_ - 1); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5113 | } |
| 5114 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 5115 | LOG(INFO) << "allocation records: " << capped_count; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5116 | |
| 5117 | // |
| 5118 | // Part 2: Generate the output and store it in the buffer. |
| 5119 | // |
| 5120 | |
| 5121 | // (1b) message header len (to allow future expansion); includes itself |
| 5122 | // (1b) entry header len |
| 5123 | // (1b) stack frame len |
| 5124 | const int kMessageHeaderLen = 15; |
| 5125 | const int kEntryHeaderLen = 9; |
| 5126 | const int kStackFrameLen = 8; |
| 5127 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 5128 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 5129 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 5130 | |
| 5131 | // (2b) number of entries |
| 5132 | // (4b) offset to string table from start of message |
| 5133 | // (2b) number of class name strings |
| 5134 | // (2b) number of method name strings |
| 5135 | // (2b) number of source file name strings |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 5136 | JDWP::Append2BE(bytes, capped_count); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5137 | size_t string_table_offset = bytes.size(); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 5138 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5139 | JDWP::Append2BE(bytes, class_names.Size()); |
| 5140 | JDWP::Append2BE(bytes, method_names.Size()); |
| 5141 | JDWP::Append2BE(bytes, filenames.Size()); |
| 5142 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5143 | idx = HeadIndex(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5144 | std::string temp; |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 5145 | for (count = capped_count; count != 0; --count) { |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5146 | // For each entry: |
| 5147 | // (4b) total allocation size |
| 5148 | // (2b) thread id |
| 5149 | // (2b) allocated object's class name index |
| 5150 | // (1b) stack depth |
| 5151 | AllocRecord* record = &recent_allocation_records_[idx]; |
| 5152 | size_t stack_depth = record->GetDepth(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5153 | size_t allocated_object_class_name_index = |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5154 | class_names.IndexOf(record->Type()->GetDescriptor(&temp)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 5155 | JDWP::Append4BE(bytes, record->ByteCount()); |
| 5156 | JDWP::Append2BE(bytes, record->ThinLockId()); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5157 | JDWP::Append2BE(bytes, allocated_object_class_name_index); |
| 5158 | JDWP::Append1BE(bytes, stack_depth); |
| 5159 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5160 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 5161 | // For each stack frame: |
| 5162 | // (2b) method's class name |
| 5163 | // (2b) method name |
| 5164 | // (2b) method source file |
| 5165 | // (2b) line number, clipped to 32767; -2 if native; -1 if no source |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5166 | ArtMethod* m = record->StackElement(stack_frame)->Method(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5167 | size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor()); |
| 5168 | size_t method_name_index = method_names.IndexOf(m->GetName()); |
| 5169 | size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5170 | JDWP::Append2BE(bytes, class_name_index); |
| 5171 | JDWP::Append2BE(bytes, method_name_index); |
| 5172 | JDWP::Append2BE(bytes, file_name_index); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 5173 | JDWP::Append2BE(bytes, record->StackElement(stack_frame)->LineNumber()); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5174 | } |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 5175 | idx = (idx + 1) & (alloc_record_max_ - 1); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | // (xb) class name strings |
| 5179 | // (xb) method name strings |
| 5180 | // (xb) source file strings |
| 5181 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 5182 | class_names.WriteTo(bytes); |
| 5183 | method_names.WriteTo(bytes); |
| 5184 | filenames.WriteTo(bytes); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5185 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 5186 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5187 | jbyteArray result = env->NewByteArray(bytes.size()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 5188 | if (result != nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5189 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 5190 | } |
| 5191 | return result; |
| 5192 | } |
| 5193 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5194 | ArtMethod* DeoptimizationRequest::Method() const { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 5195 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 5196 | return soa.DecodeMethod(method_); |
| 5197 | } |
| 5198 | |
Mathieu Chartier | 3d21bdf | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5199 | void DeoptimizationRequest::SetMethod(ArtMethod* m) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 5200 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 5201 | method_ = soa.EncodeMethod(m); |
| 5202 | } |
| 5203 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 5204 | } // namespace art |