Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "debugger.h" |
| 18 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 19 | #include <sys/uio.h> |
| 20 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 21 | #include <set> |
| 22 | |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 23 | #include "arch/context.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 24 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 26 | #include "base/enums.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 27 | #include "base/time_utils.h" |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 28 | #include "class_linker.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 30 | #include "dex_file-inl.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 31 | #include "dex_file_annotations.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 32 | #include "dex_instruction.h" |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 33 | #include "entrypoints/runtime_asm_entrypoints.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 34 | #include "gc/accounting/card_table-inl.h" |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 35 | #include "gc/allocation_record.h" |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 36 | #include "gc/scoped_gc_critical_section.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 37 | #include "gc/space/large_object_space.h" |
| 38 | #include "gc/space/space-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 39 | #include "handle_scope.h" |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 40 | #include "jdwp/jdwp_priv.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 41 | #include "jdwp/object_registry.h" |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 42 | #include "jvalue-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | #include "mirror/class.h" |
| 44 | #include "mirror/class-inl.h" |
| 45 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 46 | #include "mirror/object-inl.h" |
| 47 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 48 | #include "mirror/string-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | #include "mirror/throwable.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 50 | #include "obj_ptr-inl.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 51 | #include "reflection.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 52 | #include "safe_map.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 53 | #include "scoped_thread_state_change-inl.h" |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 54 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 55 | #include "ScopedPrimitiveArray.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 56 | #include "handle_scope-inl.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 57 | #include "thread_list.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | #include "utf.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 59 | #include "well_known_classes.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 60 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 61 | namespace art { |
| 62 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 63 | // The key identifying the debugger to update instrumentation. |
| 64 | static constexpr const char* kDbgInstrumentationKey = "Debugger"; |
| 65 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 66 | // Limit alloc_record_count to the 2BE value (64k-1) that is the limit of the current protocol. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 67 | static uint16_t CappedAllocRecordCount(size_t alloc_record_count) { |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 68 | const size_t cap = 0xffff; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 69 | if (alloc_record_count > cap) { |
| 70 | return cap; |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 71 | } |
| 72 | return alloc_record_count; |
| 73 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 74 | |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 75 | // Takes a method and returns a 'canonical' one if the method is default (and therefore potentially |
| 76 | // copied from some other class). This ensures that the debugger does not get confused as to which |
| 77 | // method we are in. |
| 78 | static ArtMethod* GetCanonicalMethod(ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 79 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 80 | if (LIKELY(!m->IsDefault())) { |
| 81 | return m; |
| 82 | } else { |
| 83 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 84 | return declaring_class->FindDeclaredVirtualMethod(declaring_class->GetDexCache(), |
| 85 | m->GetDexMethodIndex(), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 86 | kRuntimePointerSize); |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 90 | class Breakpoint : public ValueObject { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 91 | public: |
Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 92 | Breakpoint(ArtMethod* method, uint32_t dex_pc, DeoptimizationRequest::Kind deoptimization_kind) |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 93 | : method_(GetCanonicalMethod(method)), |
Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 94 | dex_pc_(dex_pc), |
| 95 | deoptimization_kind_(deoptimization_kind) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 96 | CHECK(deoptimization_kind_ == DeoptimizationRequest::kNothing || |
| 97 | deoptimization_kind_ == DeoptimizationRequest::kSelectiveDeoptimization || |
| 98 | deoptimization_kind_ == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 101 | Breakpoint(const Breakpoint& other) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 102 | : method_(other.method_), |
| 103 | dex_pc_(other.dex_pc_), |
| 104 | deoptimization_kind_(other.deoptimization_kind_) {} |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 105 | |
Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 106 | // Method() is called from root visiting, do not use ScopedObjectAccess here or it can cause |
| 107 | // GC to deadlock if another thread tries to call SuspendAll while the GC is in a runnable state. |
| 108 | ArtMethod* Method() const { |
| 109 | return method_; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | uint32_t DexPc() const { |
| 113 | return dex_pc_; |
| 114 | } |
| 115 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 116 | DeoptimizationRequest::Kind GetDeoptimizationKind() const { |
| 117 | return deoptimization_kind_; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 120 | // Returns true if the method of this breakpoint and the passed in method should be considered the |
| 121 | // same. That is, they are either the same method or they are copied from the same method. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 122 | bool IsInMethod(ArtMethod* m) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 123 | return method_ == GetCanonicalMethod(m); |
| 124 | } |
| 125 | |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 126 | private: |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 127 | // The location of this breakpoint. |
Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 128 | ArtMethod* method_; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 129 | uint32_t dex_pc_; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 130 | |
| 131 | // Indicates whether breakpoint needs full deoptimization or selective deoptimization. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 132 | DeoptimizationRequest::Kind deoptimization_kind_; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 135 | static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 136 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 137 | os << StringPrintf("Breakpoint[%s @%#x]", ArtMethod::PrettyMethod(rhs.Method()).c_str(), |
| 138 | rhs.DexPc()); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 139 | return os; |
| 140 | } |
| 141 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 142 | class DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 143 | public: |
| 144 | DebugInstrumentationListener() {} |
| 145 | virtual ~DebugInstrumentationListener() {} |
| 146 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 147 | void MethodEntered(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 148 | uint32_t dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 149 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 150 | if (method->IsNative()) { |
| 151 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 152 | return; |
| 153 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 154 | if (IsListeningToDexPcMoved()) { |
| 155 | // We also listen to kDexPcMoved instrumentation event so we know the DexPcMoved method is |
| 156 | // going to be called right after us. To avoid sending JDWP events twice for this location, |
| 157 | // we report the event in DexPcMoved. However, we must remind this is method entry so we |
| 158 | // send the METHOD_ENTRY event. And we can also group it with other events for this location |
| 159 | // like BREAKPOINT or SINGLE_STEP (or even METHOD_EXIT if this is a RETURN instruction). |
| 160 | thread->SetDebugMethodEntry(); |
| 161 | } else if (IsListeningToMethodExit() && IsReturn(method, dex_pc)) { |
| 162 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 163 | // RETURN so we know the MethodExited method is going to be called right after us. To avoid |
| 164 | // sending JDWP events twice for this location, we report the event(s) in MethodExited. |
| 165 | // However, we must remind this is method entry so we send the METHOD_ENTRY event. And we can |
| 166 | // also group it with other events for this location like BREAKPOINT or SINGLE_STEP. |
| 167 | thread->SetDebugMethodEntry(); |
| 168 | } else { |
| 169 | Dbg::UpdateDebugger(thread, this_object, method, 0, Dbg::kMethodEntry, nullptr); |
| 170 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 173 | void MethodExited(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 174 | uint32_t dex_pc, const JValue& return_value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 175 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 176 | if (method->IsNative()) { |
| 177 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 178 | return; |
| 179 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 180 | uint32_t events = Dbg::kMethodExit; |
| 181 | if (thread->IsDebugMethodEntry()) { |
| 182 | // It is also the method entry. |
| 183 | DCHECK(IsReturn(method, dex_pc)); |
| 184 | events |= Dbg::kMethodEntry; |
| 185 | thread->ClearDebugMethodEntry(); |
| 186 | } |
| 187 | Dbg::UpdateDebugger(thread, this_object, method, dex_pc, events, &return_value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 190 | void MethodUnwind(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 191 | ArtMethod* method, uint32_t dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 192 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 193 | // We're not recorded to listen to this kind of event, so complain. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 194 | LOG(ERROR) << "Unexpected method unwind event in debugger " << ArtMethod::PrettyMethod(method) |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 195 | << " " << dex_pc; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 198 | void DexPcMoved(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 199 | uint32_t new_dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 200 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 201 | if (IsListeningToMethodExit() && IsReturn(method, new_dex_pc)) { |
| 202 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 203 | // RETURN so we know the MethodExited method is going to be called right after us. Like in |
| 204 | // MethodEntered, we delegate event reporting to MethodExited. |
| 205 | // Besides, if this RETURN instruction is the only one in the method, we can send multiple |
| 206 | // JDWP events in the same packet: METHOD_ENTRY, METHOD_EXIT, BREAKPOINT and/or SINGLE_STEP. |
| 207 | // Therefore, we must not clear the debug method entry flag here. |
| 208 | } else { |
| 209 | uint32_t events = 0; |
| 210 | if (thread->IsDebugMethodEntry()) { |
| 211 | // It is also the method entry. |
| 212 | events = Dbg::kMethodEntry; |
| 213 | thread->ClearDebugMethodEntry(); |
| 214 | } |
| 215 | Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc, events, nullptr); |
| 216 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 219 | void FieldRead(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 220 | ArtMethod* method, uint32_t dex_pc, ArtField* field) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 221 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 222 | Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 223 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 224 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 225 | void FieldWritten(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 226 | ArtMethod* method, uint32_t dex_pc, ArtField* field, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 227 | const JValue& field_value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 228 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 229 | Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value); |
| 230 | } |
| 231 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 232 | void ExceptionCaught(Thread* thread ATTRIBUTE_UNUSED, mirror::Throwable* exception_object) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 233 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 234 | Dbg::PostException(exception_object); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 237 | // We only care about branches in the Jit. |
| 238 | void Branch(Thread* /*thread*/, ArtMethod* method, uint32_t dex_pc, int32_t dex_pc_offset) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 239 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 240 | LOG(ERROR) << "Unexpected branch event in debugger " << ArtMethod::PrettyMethod(method) |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 241 | << " " << dex_pc << ", " << dex_pc_offset; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 244 | // We only care about invokes in the Jit. |
| 245 | void InvokeVirtualOrInterface(Thread* thread ATTRIBUTE_UNUSED, |
| 246 | mirror::Object*, |
| 247 | ArtMethod* method, |
| 248 | uint32_t dex_pc, |
| 249 | ArtMethod*) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 250 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 251 | LOG(ERROR) << "Unexpected invoke event in debugger " << ArtMethod::PrettyMethod(method) |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 252 | << " " << dex_pc; |
| 253 | } |
| 254 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 255 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 256 | static bool IsReturn(ArtMethod* method, uint32_t dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 257 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 258 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
| 259 | const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]); |
| 260 | return instruction->IsReturn(); |
| 261 | } |
| 262 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 263 | static bool IsListeningToDexPcMoved() REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 264 | return IsListeningTo(instrumentation::Instrumentation::kDexPcMoved); |
| 265 | } |
| 266 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 267 | static bool IsListeningToMethodExit() REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 268 | return IsListeningTo(instrumentation::Instrumentation::kMethodExited); |
| 269 | } |
| 270 | |
| 271 | static bool IsListeningTo(instrumentation::Instrumentation::InstrumentationEvent event) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 272 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 273 | return (Dbg::GetInstrumentationEvents() & event) != 0; |
| 274 | } |
| 275 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 276 | DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 277 | } gDebugInstrumentationListener; |
| 278 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 279 | // JDWP is allowed unless the Zygote forbids it. |
| 280 | static bool gJdwpAllowed = true; |
| 281 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 282 | // Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line? |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 283 | static bool gJdwpConfigured = false; |
| 284 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 285 | // JDWP options for debugging. Only valid if IsJdwpConfigured() is true. |
| 286 | static JDWP::JdwpOptions gJdwpOptions; |
| 287 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 288 | // Runtime JDWP state. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 289 | static JDWP::JdwpState* gJdwpState = nullptr; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 290 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 291 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 292 | static bool gDdmThreadNotification = false; |
| 293 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 294 | // DDMS GC-related settings. |
| 295 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 296 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 297 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 298 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 299 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 300 | |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 301 | bool Dbg::gDebuggerActive = false; |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 302 | bool Dbg::gDisposed = false; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 303 | ObjectRegistry* Dbg::gRegistry = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 304 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 305 | // Deoptimization support. |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 306 | std::vector<DeoptimizationRequest> Dbg::deoptimization_requests_; |
| 307 | size_t Dbg::full_deoptimization_event_count_ = 0; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 308 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 309 | // Instrumentation event reference counters. |
| 310 | size_t Dbg::dex_pc_change_event_ref_count_ = 0; |
| 311 | size_t Dbg::method_enter_event_ref_count_ = 0; |
| 312 | size_t Dbg::method_exit_event_ref_count_ = 0; |
| 313 | size_t Dbg::field_read_event_ref_count_ = 0; |
| 314 | size_t Dbg::field_write_event_ref_count_ = 0; |
| 315 | size_t Dbg::exception_catch_event_ref_count_ = 0; |
| 316 | uint32_t Dbg::instrumentation_events_ = 0; |
| 317 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 318 | // Breakpoints. |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 319 | static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 320 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 321 | void DebugInvokeReq::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 322 | receiver.VisitRootIfNonNull(visitor, root_info); // null for static method call. |
| 323 | klass.VisitRoot(visitor, root_info); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 326 | void SingleStepControl::AddDexPc(uint32_t dex_pc) { |
| 327 | dex_pcs_.insert(dex_pc); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 330 | bool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const { |
| 331 | return dex_pcs_.find(dex_pc) == dex_pcs_.end(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 334 | static bool IsBreakpoint(ArtMethod* m, uint32_t dex_pc) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 335 | REQUIRES(!Locks::breakpoint_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 336 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 337 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 338 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 339 | if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].IsInMethod(m)) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 340 | VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i]; |
| 341 | return true; |
| 342 | } |
| 343 | } |
| 344 | return false; |
| 345 | } |
| 346 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 347 | static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 348 | REQUIRES(!Locks::thread_suspend_count_lock_) { |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 349 | MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); |
| 350 | // A thread may be suspended for GC; in this code, we really want to know whether |
| 351 | // there's a debugger suspension active. |
| 352 | return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0; |
| 353 | } |
| 354 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 355 | static mirror::Array* DecodeNonNullArray(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 356 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 357 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 358 | if (o == nullptr) { |
| 359 | *error = JDWP::ERR_INVALID_OBJECT; |
| 360 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 361 | } |
| 362 | if (!o->IsArrayInstance()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 363 | *error = JDWP::ERR_INVALID_ARRAY; |
| 364 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 365 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 366 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 367 | return o->AsArray(); |
| 368 | } |
| 369 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 370 | static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 371 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 372 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 373 | if (o == nullptr) { |
| 374 | *error = JDWP::ERR_INVALID_OBJECT; |
| 375 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 376 | } |
| 377 | if (!o->IsClass()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 378 | *error = JDWP::ERR_INVALID_CLASS; |
| 379 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 380 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 381 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 382 | return o->AsClass(); |
| 383 | } |
| 384 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 385 | static Thread* DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, |
| 386 | JDWP::JdwpError* error) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 387 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 388 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 389 | mirror::Object* thread_peer = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 390 | if (thread_peer == nullptr) { |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 391 | // This isn't even an object. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 392 | *error = JDWP::ERR_INVALID_OBJECT; |
| 393 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 394 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 395 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 396 | ObjPtr<mirror::Class> java_lang_Thread = |
| 397 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_Thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 398 | if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) { |
| 399 | // This isn't a thread. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 400 | *error = JDWP::ERR_INVALID_THREAD; |
| 401 | return nullptr; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 404 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 405 | Thread* thread = Thread::FromManagedThread(soa, thread_peer); |
| 406 | // If thread is null then this a java.lang.Thread without a Thread*. Must be a un-started or a |
| 407 | // zombie. |
| 408 | *error = (thread == nullptr) ? JDWP::ERR_THREAD_NOT_ALIVE : JDWP::ERR_NONE; |
| 409 | return thread; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 412 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 413 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 414 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 415 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 416 | } |
| 417 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 418 | static JDWP::JdwpTag BasicTagFromClass(mirror::Class* klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 419 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 420 | std::string temp; |
| 421 | const char* descriptor = klass->GetDescriptor(&temp); |
| 422 | return BasicTagFromDescriptor(descriptor); |
| 423 | } |
| 424 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 425 | static JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 426 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 427 | CHECK(c != nullptr); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 428 | if (c->IsArrayClass()) { |
| 429 | return JDWP::JT_ARRAY; |
| 430 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 431 | if (c->IsStringClass()) { |
| 432 | return JDWP::JT_STRING; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 433 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 434 | if (c->IsClassClass()) { |
| 435 | return JDWP::JT_CLASS_OBJECT; |
| 436 | } |
| 437 | { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 438 | ObjPtr<mirror::Class> thread_class = |
| 439 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_Thread); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 440 | if (thread_class->IsAssignableFrom(c)) { |
| 441 | return JDWP::JT_THREAD; |
| 442 | } |
| 443 | } |
| 444 | { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 445 | ObjPtr<mirror::Class> thread_group_class = |
| 446 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_ThreadGroup); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 447 | if (thread_group_class->IsAssignableFrom(c)) { |
| 448 | return JDWP::JT_THREAD_GROUP; |
| 449 | } |
| 450 | } |
| 451 | { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 452 | ObjPtr<mirror::Class> class_loader_class = |
| 453 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_ClassLoader); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 454 | if (class_loader_class->IsAssignableFrom(c)) { |
| 455 | return JDWP::JT_CLASS_LOADER; |
| 456 | } |
| 457 | } |
| 458 | return JDWP::JT_OBJECT; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Objects declared to hold Object might actually hold a more specific |
| 463 | * type. The debugger may take a special interest in these (e.g. it |
| 464 | * wants to display the contents of Strings), so we want to return an |
| 465 | * appropriate tag. |
| 466 | * |
| 467 | * Null objects are tagged JT_OBJECT. |
| 468 | */ |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 469 | JDWP::JdwpTag Dbg::TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 470 | return (o == nullptr) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 474 | switch (tag) { |
| 475 | case JDWP::JT_BOOLEAN: |
| 476 | case JDWP::JT_BYTE: |
| 477 | case JDWP::JT_CHAR: |
| 478 | case JDWP::JT_FLOAT: |
| 479 | case JDWP::JT_DOUBLE: |
| 480 | case JDWP::JT_INT: |
| 481 | case JDWP::JT_LONG: |
| 482 | case JDWP::JT_SHORT: |
| 483 | case JDWP::JT_VOID: |
| 484 | return true; |
| 485 | default: |
| 486 | return false; |
| 487 | } |
| 488 | } |
| 489 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 490 | void Dbg::StartJdwp() { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 491 | if (!gJdwpAllowed || !IsJdwpConfigured()) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 492 | // No JDWP for you! |
| 493 | return; |
| 494 | } |
| 495 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 496 | CHECK(gRegistry == nullptr); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 497 | gRegistry = new ObjectRegistry; |
| 498 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 499 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 500 | // debugger, passively listen for a debugger, or block waiting for a |
| 501 | // debugger. |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 502 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 503 | if (gJdwpState == nullptr) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 504 | // We probably failed because some other process has the port already, which means that |
| 505 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 506 | // talking to that other process. |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 507 | LOG(FATAL) << "Debugger thread failed to initialize"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | // If a debugger has already attached, send the "welcome" message. |
| 511 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 512 | if (gJdwpState->IsActive()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 513 | ScopedObjectAccess soa(Thread::Current()); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 514 | gJdwpState->PostVMStart(); |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 515 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 518 | void Dbg::StopJdwp() { |
Sebastien Hertz | c6345ef | 2014-08-18 19:26:39 +0200 | [diff] [blame] | 519 | // Post VM_DEATH event before the JDWP connection is closed (either by the JDWP thread or the |
| 520 | // destruction of gJdwpState). |
| 521 | if (gJdwpState != nullptr && gJdwpState->IsActive()) { |
| 522 | gJdwpState->PostVMDeath(); |
| 523 | } |
Sebastien Hertz | 0376e6b | 2014-02-06 18:12:59 +0100 | [diff] [blame] | 524 | // 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] | 525 | Dispose(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 526 | delete gJdwpState; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 527 | gJdwpState = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 528 | delete gRegistry; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 529 | gRegistry = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 532 | void Dbg::GcDidFinish() { |
| 533 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 534 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 535 | VLOG(jdwp) << "Sending heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 536 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 537 | } |
| 538 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 539 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 540 | VLOG(jdwp) << "Dumping heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 541 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 542 | } |
| 543 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 544 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 545 | VLOG(jdwp) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 546 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 550 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 551 | gJdwpAllowed = allowed; |
| 552 | } |
| 553 | |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame^] | 554 | bool Dbg::IsJdwpAllowed() { |
| 555 | return gJdwpAllowed; |
| 556 | } |
| 557 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 558 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 559 | return Thread::Current()->GetInvokeReq(); |
| 560 | } |
| 561 | |
| 562 | Thread* Dbg::GetDebugThread() { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 563 | return (gJdwpState != nullptr) ? gJdwpState->GetDebugThread() : nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | void Dbg::ClearWaitForEventThread() { |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 567 | gJdwpState->ReleaseJdwpTokenForEvent(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 571 | CHECK(!gDebuggerConnected); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 572 | VLOG(jdwp) << "JDWP has attached"; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 573 | gDebuggerConnected = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 574 | gDisposed = false; |
| 575 | } |
| 576 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 577 | bool Dbg::RequiresDeoptimization() { |
| 578 | // We don't need deoptimization if everything runs with interpreter after |
| 579 | // enabling -Xint mode. |
| 580 | return !Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly(); |
| 581 | } |
| 582 | |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 583 | // Used to patch boot image method entry point to interpreter bridge. |
| 584 | class UpdateEntryPointsClassVisitor : public ClassVisitor { |
| 585 | public: |
| 586 | explicit UpdateEntryPointsClassVisitor(instrumentation::Instrumentation* instrumentation) |
| 587 | : instrumentation_(instrumentation) {} |
| 588 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 589 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES(Locks::mutator_lock_) { |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 590 | auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
| 591 | for (auto& m : klass->GetMethods(pointer_size)) { |
| 592 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
| 593 | if (Runtime::Current()->GetHeap()->IsInBootImageOatFile(code) && |
| 594 | !m.IsNative() && |
| 595 | !m.IsProxyMethod()) { |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 596 | instrumentation_->UpdateMethodsCodeFromDebugger(&m, GetQuickToInterpreterBridge()); |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | return true; |
| 600 | } |
| 601 | |
| 602 | private: |
| 603 | instrumentation::Instrumentation* const instrumentation_; |
| 604 | }; |
| 605 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 606 | void Dbg::GoActive() { |
| 607 | // Enable all debugging features, including scans for breakpoints. |
| 608 | // This is a no-op if we're already active. |
| 609 | // Only called from the JDWP handler thread. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 610 | if (IsDebuggerActive()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 611 | return; |
| 612 | } |
| 613 | |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 614 | Thread* const self = Thread::Current(); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 615 | { |
| 616 | // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected? |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 617 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 618 | CHECK_EQ(gBreakpoints.size(), 0U); |
| 619 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 620 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 621 | { |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 622 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 623 | CHECK_EQ(deoptimization_requests_.size(), 0U); |
| 624 | CHECK_EQ(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 625 | CHECK_EQ(dex_pc_change_event_ref_count_, 0U); |
| 626 | CHECK_EQ(method_enter_event_ref_count_, 0U); |
| 627 | CHECK_EQ(method_exit_event_ref_count_, 0U); |
| 628 | CHECK_EQ(field_read_event_ref_count_, 0U); |
| 629 | CHECK_EQ(field_write_event_ref_count_, 0U); |
| 630 | CHECK_EQ(exception_catch_event_ref_count_, 0U); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 631 | } |
| 632 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 633 | Runtime* runtime = Runtime::Current(); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 634 | // Since boot image code may be AOT compiled as not debuggable, we need to patch |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 635 | // entry points of methods in boot image to interpreter bridge. |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 636 | // However, the performance cost of this is non-negligible during native-debugging due to the |
| 637 | // forced JIT, so we keep the AOT code in that case in exchange for limited native debugging. |
| 638 | if (!runtime->GetInstrumentation()->IsForcedInterpretOnly() && !runtime->IsNativeDebuggable()) { |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 639 | ScopedObjectAccess soa(self); |
| 640 | UpdateEntryPointsClassVisitor visitor(runtime->GetInstrumentation()); |
| 641 | runtime->GetClassLinker()->VisitClasses(&visitor); |
| 642 | } |
| 643 | |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 644 | ScopedSuspendAll ssa(__FUNCTION__); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 645 | if (RequiresDeoptimization()) { |
| 646 | runtime->GetInstrumentation()->EnableDeoptimization(); |
| 647 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 648 | instrumentation_events_ = 0; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 649 | gDebuggerActive = true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 650 | LOG(INFO) << "Debugger is active"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 654 | CHECK(gDebuggerConnected); |
| 655 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 656 | LOG(INFO) << "Debugger is no longer active"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 657 | |
Hiroshi Yamauchi | 98810e3 | 2016-05-24 14:55:40 -0700 | [diff] [blame] | 658 | // Suspend all threads and exclusively acquire the mutator lock. Remove the debugger as a listener |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 659 | // and clear the object registry. |
| 660 | Runtime* runtime = Runtime::Current(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 661 | Thread* self = Thread::Current(); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 662 | { |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 663 | // Required for DisableDeoptimization. |
| 664 | gc::ScopedGCCriticalSection gcs(self, |
| 665 | gc::kGcCauseInstrumentation, |
| 666 | gc::kCollectorTypeInstrumentation); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 667 | ScopedSuspendAll ssa(__FUNCTION__); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 668 | // Debugger may not be active at this point. |
| 669 | if (IsDebuggerActive()) { |
| 670 | { |
| 671 | // Since we're going to disable deoptimization, we clear the deoptimization requests queue. |
| 672 | // This prevents us from having any pending deoptimization request when the debugger attaches |
| 673 | // to us again while no event has been requested yet. |
Mathieu Chartier | a6b1ead | 2015-10-06 10:32:38 -0700 | [diff] [blame] | 674 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 675 | deoptimization_requests_.clear(); |
| 676 | full_deoptimization_event_count_ = 0U; |
| 677 | } |
| 678 | if (instrumentation_events_ != 0) { |
| 679 | runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener, |
| 680 | instrumentation_events_); |
| 681 | instrumentation_events_ = 0; |
| 682 | } |
| 683 | if (RequiresDeoptimization()) { |
| 684 | runtime->GetInstrumentation()->DisableDeoptimization(kDbgInstrumentationKey); |
| 685 | } |
| 686 | gDebuggerActive = false; |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 687 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 688 | } |
Sebastien Hertz | 55f6534 | 2015-01-13 22:48:34 +0100 | [diff] [blame] | 689 | |
| 690 | { |
| 691 | ScopedObjectAccess soa(self); |
| 692 | gRegistry->Clear(); |
| 693 | } |
| 694 | |
| 695 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 698 | void Dbg::ConfigureJdwp(const JDWP::JdwpOptions& jdwp_options) { |
| 699 | CHECK_NE(jdwp_options.transport, JDWP::kJdwpTransportUnknown); |
| 700 | gJdwpOptions = jdwp_options; |
| 701 | gJdwpConfigured = true; |
| 702 | } |
| 703 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 704 | bool Dbg::IsJdwpConfigured() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 705 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | int64_t Dbg::LastDebuggerActivity() { |
Elliott Hughes | ca95152 | 2011-12-05 12:01:32 -0800 | [diff] [blame] | 709 | return gJdwpState->LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 712 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 713 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 716 | std::string Dbg::GetClassName(JDWP::RefTypeId class_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 717 | JDWP::JdwpError error; |
| 718 | mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id, &error); |
| 719 | if (o == nullptr) { |
| 720 | if (error == JDWP::ERR_NONE) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 721 | return "null"; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 722 | } else { |
| 723 | return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id)); |
| 724 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 725 | } |
| 726 | if (!o->IsClass()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 727 | 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] | 728 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 729 | return GetClassName(o->AsClass()); |
| 730 | } |
| 731 | |
| 732 | std::string Dbg::GetClassName(mirror::Class* klass) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 733 | if (klass == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 734 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 735 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 736 | std::string temp; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 737 | return DescriptorToName(klass->GetDescriptor(&temp)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 740 | JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId* class_object_id) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 741 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 742 | mirror::Class* c = DecodeClass(id, &status); |
| 743 | if (c == nullptr) { |
| 744 | *class_object_id = 0; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 745 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 746 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 747 | *class_object_id = gRegistry->Add(c); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 748 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 749 | } |
| 750 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 751 | JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId* superclass_id) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 752 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 753 | mirror::Class* c = DecodeClass(id, &status); |
| 754 | if (c == nullptr) { |
| 755 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 756 | return status; |
| 757 | } |
| 758 | if (c->IsInterface()) { |
| 759 | // http://code.google.com/p/android/issues/detail?id=20856 |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 760 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 761 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 762 | *superclass_id = gRegistry->Add(c->GetSuperClass()); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 763 | } |
| 764 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 767 | JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 768 | JDWP::JdwpError error; |
Andreas Gampe | 7929a48 | 2015-12-30 19:33:49 -0800 | [diff] [blame] | 769 | mirror::Class* c = DecodeClass(id, &error); |
| 770 | if (c == nullptr) { |
| 771 | return error; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 772 | } |
Andreas Gampe | 7929a48 | 2015-12-30 19:33:49 -0800 | [diff] [blame] | 773 | expandBufAddObjectId(pReply, gRegistry->Add(c->GetClassLoader())); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 774 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 777 | JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 778 | JDWP::JdwpError error; |
| 779 | mirror::Class* c = DecodeClass(id, &error); |
| 780 | if (c == nullptr) { |
| 781 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 782 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 783 | |
| 784 | uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask; |
| 785 | |
Yevgeny Rouban | de34eea | 2014-02-15 01:06:03 +0700 | [diff] [blame] | 786 | // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set, |
| 787 | // not interfaces. |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 788 | // 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] | 789 | if ((access_flags & kAccInterface) == 0) { |
| 790 | access_flags |= kAccSuper; |
| 791 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 792 | |
| 793 | expandBufAdd4BE(pReply, access_flags); |
| 794 | |
| 795 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 798 | JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) { |
| 799 | JDWP::JdwpError error; |
| 800 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 801 | if (o == nullptr) { |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 802 | return JDWP::ERR_INVALID_OBJECT; |
| 803 | } |
| 804 | |
| 805 | // Ensure all threads are suspended while we read objects' lock words. |
| 806 | Thread* self = Thread::Current(); |
Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 807 | CHECK_EQ(self->GetState(), kRunnable); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 808 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 809 | MonitorInfo monitor_info; |
| 810 | { |
| 811 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 812 | ScopedSuspendAll ssa(__FUNCTION__); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 813 | monitor_info = MonitorInfo(o); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 814 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 815 | if (monitor_info.owner_ != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 816 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 817 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 818 | expandBufAddObjectId(reply, gRegistry->Add(nullptr)); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 819 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 820 | expandBufAdd4BE(reply, monitor_info.entry_count_); |
| 821 | expandBufAdd4BE(reply, monitor_info.waiters_.size()); |
| 822 | for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) { |
| 823 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 824 | } |
| 825 | return JDWP::ERR_NONE; |
| 826 | } |
| 827 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 828 | JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 829 | std::vector<JDWP::ObjectId>* monitors, |
| 830 | std::vector<uint32_t>* stack_depths) { |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 831 | struct OwnedMonitorVisitor : public StackVisitor { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 832 | OwnedMonitorVisitor(Thread* thread, Context* context, |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 833 | std::vector<JDWP::ObjectId>* monitor_vector, |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 834 | std::vector<uint32_t>* stack_depth_vector) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 835 | REQUIRES_SHARED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 836 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 837 | current_stack_depth(0), |
| 838 | monitors(monitor_vector), |
| 839 | stack_depths(stack_depth_vector) {} |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 840 | |
| 841 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 842 | // annotalysis. |
| 843 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 844 | if (!GetMethod()->IsRuntimeMethod()) { |
| 845 | Monitor::VisitLocks(this, AppendOwnedMonitors, this); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 846 | ++current_stack_depth; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 847 | } |
| 848 | return true; |
| 849 | } |
| 850 | |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 851 | static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 852 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 853 | OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 854 | visitor->monitors->push_back(gRegistry->Add(owned_monitor)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 855 | visitor->stack_depths->push_back(visitor->current_stack_depth); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 856 | } |
| 857 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 858 | size_t current_stack_depth; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 859 | std::vector<JDWP::ObjectId>* const monitors; |
| 860 | std::vector<uint32_t>* const stack_depths; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 861 | }; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 862 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 863 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 864 | JDWP::JdwpError error; |
| 865 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 866 | if (thread == nullptr) { |
| 867 | return error; |
| 868 | } |
| 869 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 870 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 871 | } |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 872 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 873 | OwnedMonitorVisitor visitor(thread, context.get(), monitors, stack_depths); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 874 | visitor.WalkStack(); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 875 | return JDWP::ERR_NONE; |
| 876 | } |
| 877 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 878 | JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 879 | JDWP::ObjectId* contended_monitor) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 880 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 881 | *contended_monitor = 0; |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 882 | JDWP::JdwpError error; |
| 883 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 884 | if (thread == nullptr) { |
| 885 | return error; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 886 | } |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 887 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 888 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 889 | } |
| 890 | mirror::Object* contended_monitor_obj = Monitor::GetContendedMonitor(thread); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 891 | // Add() requires the thread_list_lock_ not held to avoid the lock |
| 892 | // level violation. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 893 | *contended_monitor = gRegistry->Add(contended_monitor_obj); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 894 | return JDWP::ERR_NONE; |
| 895 | } |
| 896 | |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 897 | JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 898 | std::vector<uint64_t>* counts) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 899 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 900 | heap->CollectGarbage(false); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 901 | VariableSizedHandleScope hs(Thread::Current()); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 902 | std::vector<Handle<mirror::Class>> classes; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 903 | counts->clear(); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 904 | for (size_t i = 0; i < class_ids.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 905 | JDWP::JdwpError error; |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 906 | ObjPtr<mirror::Class> c = DecodeClass(class_ids[i], &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 907 | if (c == nullptr) { |
| 908 | return error; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 909 | } |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 910 | classes.push_back(hs.NewHandle(c)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 911 | counts->push_back(0); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 912 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 913 | heap->CountInstances(classes, false, &(*counts)[0]); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 914 | return JDWP::ERR_NONE; |
| 915 | } |
| 916 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 917 | JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, |
| 918 | std::vector<JDWP::ObjectId>* instances) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 919 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 920 | // We only want reachable instances, so do a GC. |
| 921 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 922 | JDWP::JdwpError error; |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 923 | ObjPtr<mirror::Class> c = DecodeClass(class_id, &error); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 924 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 925 | return error; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 926 | } |
Mathieu Chartier | 2d85595 | 2016-10-12 19:37:59 -0700 | [diff] [blame] | 927 | VariableSizedHandleScope hs(Thread::Current()); |
| 928 | std::vector<Handle<mirror::Object>> raw_instances; |
| 929 | Runtime::Current()->GetHeap()->GetInstances(hs, hs.NewHandle(c), max_count, raw_instances); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 930 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Mathieu Chartier | 2d85595 | 2016-10-12 19:37:59 -0700 | [diff] [blame] | 931 | instances->push_back(gRegistry->Add(raw_instances[i].Get())); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 932 | } |
| 933 | return JDWP::ERR_NONE; |
| 934 | } |
| 935 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 936 | JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 937 | std::vector<JDWP::ObjectId>* referring_objects) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 938 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 939 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 940 | JDWP::JdwpError error; |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 941 | ObjPtr<mirror::Object> o = gRegistry->Get<mirror::Object*>(object_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 942 | if (o == nullptr) { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 943 | return JDWP::ERR_INVALID_OBJECT; |
| 944 | } |
Mathieu Chartier | aea9bfb | 2016-10-12 19:19:56 -0700 | [diff] [blame] | 945 | VariableSizedHandleScope hs(Thread::Current()); |
| 946 | std::vector<Handle<mirror::Object>> raw_instances; |
| 947 | heap->GetReferringObjects(hs, hs.NewHandle(o), max_count, raw_instances); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 948 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Mathieu Chartier | aea9bfb | 2016-10-12 19:19:56 -0700 | [diff] [blame] | 949 | referring_objects->push_back(gRegistry->Add(raw_instances[i].Get())); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 950 | } |
| 951 | return JDWP::ERR_NONE; |
| 952 | } |
| 953 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 954 | JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id) { |
| 955 | JDWP::JdwpError error; |
| 956 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 957 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 958 | return JDWP::ERR_INVALID_OBJECT; |
| 959 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 960 | gRegistry->DisableCollection(object_id); |
| 961 | return JDWP::ERR_NONE; |
| 962 | } |
| 963 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 964 | JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id) { |
| 965 | JDWP::JdwpError error; |
| 966 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 967 | // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI |
| 968 | // also ignores these cases and never return an error. However it's not obvious why this command |
| 969 | // should behave differently from DisableCollection and IsCollected commands. So let's be more |
| 970 | // strict and return an error if this happens. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 971 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 972 | return JDWP::ERR_INVALID_OBJECT; |
| 973 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 974 | gRegistry->EnableCollection(object_id); |
| 975 | return JDWP::ERR_NONE; |
| 976 | } |
| 977 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 978 | JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool* is_collected) { |
| 979 | *is_collected = true; |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 980 | if (object_id == 0) { |
| 981 | // Null object id is invalid. |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 982 | return JDWP::ERR_INVALID_OBJECT; |
| 983 | } |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 984 | // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However |
| 985 | // the RI seems to ignore this and assume object has been collected. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 986 | JDWP::JdwpError error; |
| 987 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 988 | if (o != nullptr) { |
| 989 | *is_collected = gRegistry->IsCollected(object_id); |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 990 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 991 | return JDWP::ERR_NONE; |
| 992 | } |
| 993 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 994 | void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 995 | gRegistry->DisposeObject(object_id, reference_count); |
| 996 | } |
| 997 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 998 | JDWP::JdwpTypeTag Dbg::GetTypeTag(ObjPtr<mirror::Class> klass) { |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 999 | DCHECK(klass != nullptr); |
| 1000 | if (klass->IsArrayClass()) { |
| 1001 | return JDWP::TT_ARRAY; |
| 1002 | } else if (klass->IsInterface()) { |
| 1003 | return JDWP::TT_INTERFACE; |
| 1004 | } else { |
| 1005 | return JDWP::TT_CLASS; |
| 1006 | } |
| 1007 | } |
| 1008 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1009 | JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1010 | JDWP::JdwpError error; |
| 1011 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1012 | if (c == nullptr) { |
| 1013 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1014 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1015 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1016 | JDWP::JdwpTypeTag type_tag = GetTypeTag(c); |
| 1017 | expandBufAdd1(pReply, type_tag); |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1018 | expandBufAddRefTypeId(pReply, class_id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1019 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1022 | // Get the complete list of reference classes (i.e. all classes except |
| 1023 | // the primitive types). |
| 1024 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 1025 | class ClassListCreator : public ClassVisitor { |
| 1026 | public: |
| 1027 | explicit ClassListCreator(std::vector<JDWP::RefTypeId>* classes) : classes_(classes) {} |
| 1028 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1029 | bool operator()(ObjPtr<mirror::Class> c) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1030 | if (!c->IsPrimitive()) { |
| 1031 | classes_->push_back(Dbg::GetObjectRegistry()->AddRefType(c)); |
| 1032 | } |
| 1033 | return true; |
| 1034 | } |
| 1035 | |
| 1036 | private: |
| 1037 | std::vector<JDWP::RefTypeId>* const classes_; |
| 1038 | }; |
| 1039 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1040 | void Dbg::GetClassList(std::vector<JDWP::RefTypeId>* classes) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1041 | ClassListCreator clc(classes); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1042 | Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(&clc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1045 | JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, |
| 1046 | uint32_t* pStatus, std::string* pDescriptor) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1047 | JDWP::JdwpError error; |
| 1048 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1049 | if (c == nullptr) { |
| 1050 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1053 | if (c->IsArrayClass()) { |
| 1054 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 1055 | *pTypeTag = JDWP::TT_ARRAY; |
| 1056 | } else { |
| 1057 | if (c->IsErroneous()) { |
| 1058 | *pStatus = JDWP::CS_ERROR; |
| 1059 | } else { |
| 1060 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 1061 | } |
| 1062 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1063 | } |
| 1064 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1065 | if (pDescriptor != nullptr) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1066 | std::string temp; |
| 1067 | *pDescriptor = c->GetDescriptor(&temp); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1068 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1069 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1072 | void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>* ids) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1073 | std::vector<ObjPtr<mirror::Class>> classes; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1074 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1075 | ids->clear(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1076 | for (ObjPtr<mirror::Class> c : classes) { |
| 1077 | ids->push_back(gRegistry->Add(c)); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1078 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1081 | JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) { |
| 1082 | JDWP::JdwpError error; |
| 1083 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1084 | if (o == nullptr) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1085 | return JDWP::ERR_INVALID_OBJECT; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1086 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1087 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1088 | JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass()); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1089 | JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1090 | |
| 1091 | expandBufAdd1(pReply, type_tag); |
| 1092 | expandBufAddRefTypeId(pReply, type_id); |
| 1093 | |
| 1094 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 1097 | JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1098 | JDWP::JdwpError error; |
| 1099 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1100 | if (c == nullptr) { |
| 1101 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1102 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1103 | std::string temp; |
| 1104 | *signature = c->GetDescriptor(&temp); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 1105 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1108 | JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string* result) { |
| 1109 | JDWP::JdwpError error; |
| 1110 | mirror::Class* c = DecodeClass(class_id, &error); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1111 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1112 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1113 | } |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1114 | const char* source_file = c->GetSourceFile(); |
| 1115 | if (source_file == nullptr) { |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 1116 | return JDWP::ERR_ABSENT_INFORMATION; |
| 1117 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1118 | *result = source_file; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1119 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1122 | JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t* tag) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1123 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1124 | JDWP::JdwpError error; |
| 1125 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1126 | if (error != JDWP::ERR_NONE) { |
| 1127 | *tag = JDWP::JT_VOID; |
| 1128 | return error; |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1129 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1130 | *tag = TagFromObject(soa, o); |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1131 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1134 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1135 | switch (tag) { |
| 1136 | case JDWP::JT_VOID: |
| 1137 | return 0; |
| 1138 | case JDWP::JT_BYTE: |
| 1139 | case JDWP::JT_BOOLEAN: |
| 1140 | return 1; |
| 1141 | case JDWP::JT_CHAR: |
| 1142 | case JDWP::JT_SHORT: |
| 1143 | return 2; |
| 1144 | case JDWP::JT_FLOAT: |
| 1145 | case JDWP::JT_INT: |
| 1146 | return 4; |
| 1147 | case JDWP::JT_ARRAY: |
| 1148 | case JDWP::JT_OBJECT: |
| 1149 | case JDWP::JT_STRING: |
| 1150 | case JDWP::JT_THREAD: |
| 1151 | case JDWP::JT_THREAD_GROUP: |
| 1152 | case JDWP::JT_CLASS_LOADER: |
| 1153 | case JDWP::JT_CLASS_OBJECT: |
| 1154 | return sizeof(JDWP::ObjectId); |
| 1155 | case JDWP::JT_DOUBLE: |
| 1156 | case JDWP::JT_LONG: |
| 1157 | return 8; |
| 1158 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1159 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1160 | return -1; |
| 1161 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1164 | JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int32_t* length) { |
| 1165 | JDWP::JdwpError error; |
| 1166 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
| 1167 | if (a == nullptr) { |
| 1168 | return error; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1169 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1170 | *length = a->GetLength(); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1171 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1174 | 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] | 1175 | JDWP::JdwpError error; |
| 1176 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1177 | if (a == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1178 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1179 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1180 | |
| 1181 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 1182 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1183 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1184 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1185 | JDWP::JdwpTag element_tag = BasicTagFromClass(a->GetClass()->GetComponentType()); |
| 1186 | expandBufAdd1(pReply, element_tag); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1187 | expandBufAdd4BE(pReply, count); |
| 1188 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1189 | if (IsPrimitiveTag(element_tag)) { |
| 1190 | size_t width = GetTagWidth(element_tag); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1191 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 1192 | if (width == 8) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1193 | 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] | 1194 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 1195 | } else if (width == 4) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1196 | 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] | 1197 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 1198 | } else if (width == 2) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1199 | 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] | 1200 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 1201 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1202 | 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] | 1203 | memcpy(dst, &src[offset * width], count * width); |
| 1204 | } |
| 1205 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1206 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1207 | mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1208 | for (int i = 0; i < count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1209 | mirror::Object* element = oa->Get(offset + i); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1210 | JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element) |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1211 | : element_tag; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1212 | expandBufAdd1(pReply, specific_tag); |
| 1213 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 1214 | } |
| 1215 | } |
| 1216 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1217 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1220 | template <typename T> |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1221 | 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] | 1222 | NO_THREAD_SAFETY_ANALYSIS { |
| 1223 | // TODO: fix when annotalysis correctly handles non-member functions. |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1224 | DCHECK(a->GetClass()->IsPrimitiveArray()); |
| 1225 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1226 | T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1227 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1228 | *dst++ = src->ReadValue(sizeof(T)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1232 | JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1233 | JDWP::Request* request) { |
| 1234 | JDWP::JdwpError error; |
| 1235 | mirror::Array* dst = DecodeNonNullArray(array_id, &error); |
| 1236 | if (dst == nullptr) { |
| 1237 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1238 | } |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1239 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1240 | if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1241 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1242 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1243 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1244 | JDWP::JdwpTag element_tag = BasicTagFromClass(dst->GetClass()->GetComponentType()); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 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 | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1248 | if (width == 8) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1249 | CopyArrayData<uint64_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1250 | } else if (width == 4) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1251 | CopyArrayData<uint32_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1252 | } else if (width == 2) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1253 | CopyArrayData<uint16_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1254 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1255 | CopyArrayData<uint8_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1256 | } |
| 1257 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1258 | mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>(); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1259 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1260 | JDWP::ObjectId id = request->ReadObjectId(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1261 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 1262 | if (error != JDWP::ERR_NONE) { |
| 1263 | return error; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1264 | } |
Sebastien Hertz | 2e1c16d | 2015-08-28 11:57:49 +0200 | [diff] [blame] | 1265 | // Check if the object's type is compatible with the array's type. |
| 1266 | if (o != nullptr && !o->InstanceOf(oa->GetClass()->GetComponentType())) { |
| 1267 | return JDWP::ERR_TYPE_MISMATCH; |
| 1268 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1269 | oa->Set<false>(offset + i, o); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1273 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1276 | JDWP::JdwpError Dbg::CreateString(const std::string& str, JDWP::ObjectId* new_string_id) { |
| 1277 | Thread* self = Thread::Current(); |
| 1278 | mirror::String* new_string = mirror::String::AllocFromModifiedUtf8(self, str.c_str()); |
| 1279 | if (new_string == nullptr) { |
| 1280 | DCHECK(self->IsExceptionPending()); |
| 1281 | self->ClearException(); |
| 1282 | LOG(ERROR) << "Could not allocate string"; |
| 1283 | *new_string_id = 0; |
| 1284 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1285 | } |
| 1286 | *new_string_id = gRegistry->Add(new_string); |
| 1287 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1290 | 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] | 1291 | JDWP::JdwpError error; |
| 1292 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1293 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1294 | *new_object_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1295 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1296 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1297 | Thread* self = Thread::Current(); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1298 | ObjPtr<mirror::Object> new_object; |
Sebastien Hertz | 56d5e50 | 2015-11-03 17:38:35 +0100 | [diff] [blame] | 1299 | if (c->IsStringClass()) { |
| 1300 | // Special case for java.lang.String. |
| 1301 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 1302 | new_object = mirror::String::AllocEmptyString<true>(self, allocator_type); |
Sebastien Hertz | 56d5e50 | 2015-11-03 17:38:35 +0100 | [diff] [blame] | 1303 | } else { |
| 1304 | new_object = c->AllocObject(self); |
| 1305 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1306 | if (new_object == nullptr) { |
| 1307 | DCHECK(self->IsExceptionPending()); |
| 1308 | self->ClearException(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1309 | LOG(ERROR) << "Could not allocate object of type " << mirror::Class::PrettyDescriptor(c); |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1310 | *new_object_id = 0; |
| 1311 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1312 | } |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1313 | *new_object_id = gRegistry->Add(new_object.Ptr()); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1314 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 1317 | /* |
| 1318 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 1319 | */ |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1320 | JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length, |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1321 | JDWP::ObjectId* new_array_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1322 | JDWP::JdwpError error; |
| 1323 | mirror::Class* c = DecodeClass(array_class_id, &error); |
| 1324 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1325 | *new_array_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1326 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1327 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1328 | Thread* self = Thread::Current(); |
| 1329 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1330 | mirror::Array* new_array = mirror::Array::Alloc<true>(self, c, length, |
| 1331 | c->GetComponentSizeShift(), |
| 1332 | heap->GetCurrentAllocator()); |
| 1333 | if (new_array == nullptr) { |
| 1334 | DCHECK(self->IsExceptionPending()); |
| 1335 | self->ClearException(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1336 | LOG(ERROR) << "Could not allocate array of type " << mirror::Class::PrettyDescriptor(c); |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1337 | *new_array_id = 0; |
| 1338 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1339 | } |
| 1340 | *new_array_id = gRegistry->Add(new_array); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1341 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1344 | JDWP::FieldId Dbg::ToFieldId(const ArtField* f) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1345 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1346 | } |
| 1347 | |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 1348 | static JDWP::MethodId ToMethodId(ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1349 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 1350 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(GetCanonicalMethod(m))); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1351 | } |
| 1352 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1353 | static ArtField* FromFieldId(JDWP::FieldId fid) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1354 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1355 | return reinterpret_cast<ArtField*>(static_cast<uintptr_t>(fid)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1356 | } |
| 1357 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1358 | static ArtMethod* FromMethodId(JDWP::MethodId mid) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1359 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1360 | return reinterpret_cast<ArtMethod*>(static_cast<uintptr_t>(mid)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1361 | } |
| 1362 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1363 | bool Dbg::MatchThread(JDWP::ObjectId expected_thread_id, Thread* event_thread) { |
| 1364 | CHECK(event_thread != nullptr); |
| 1365 | JDWP::JdwpError error; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1366 | mirror::Object* expected_thread_peer = gRegistry->Get<mirror::Object*>( |
| 1367 | expected_thread_id, &error); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1368 | return expected_thread_peer == event_thread->GetPeer(); |
| 1369 | } |
| 1370 | |
| 1371 | bool Dbg::MatchLocation(const JDWP::JdwpLocation& expected_location, |
| 1372 | const JDWP::EventLocation& event_location) { |
| 1373 | if (expected_location.dex_pc != event_location.dex_pc) { |
| 1374 | return false; |
| 1375 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1376 | ArtMethod* m = FromMethodId(expected_location.method_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1377 | return m == event_location.method; |
| 1378 | } |
| 1379 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1380 | bool Dbg::MatchType(ObjPtr<mirror::Class> event_class, JDWP::RefTypeId class_id) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1381 | if (event_class == nullptr) { |
| 1382 | return false; |
| 1383 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1384 | JDWP::JdwpError error; |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1385 | ObjPtr<mirror::Class> expected_class = DecodeClass(class_id, &error); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1386 | CHECK(expected_class != nullptr); |
| 1387 | return expected_class->IsAssignableFrom(event_class); |
| 1388 | } |
| 1389 | |
| 1390 | 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] | 1391 | ArtField* event_field) { |
| 1392 | ArtField* expected_field = FromFieldId(expected_field_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1393 | if (expected_field != event_field) { |
| 1394 | return false; |
| 1395 | } |
| 1396 | return Dbg::MatchType(event_field->GetDeclaringClass(), expected_type_id); |
| 1397 | } |
| 1398 | |
| 1399 | bool Dbg::MatchInstance(JDWP::ObjectId expected_instance_id, mirror::Object* event_instance) { |
| 1400 | JDWP::JdwpError error; |
| 1401 | mirror::Object* modifier_instance = gRegistry->Get<mirror::Object*>(expected_instance_id, &error); |
| 1402 | return modifier_instance == event_instance; |
| 1403 | } |
| 1404 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1405 | void Dbg::SetJdwpLocation(JDWP::JdwpLocation* location, ArtMethod* m, uint32_t dex_pc) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1406 | if (m == nullptr) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1407 | memset(location, 0, sizeof(*location)); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1408 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1409 | mirror::Class* c = m->GetDeclaringClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1410 | location->type_tag = GetTypeTag(c); |
| 1411 | location->class_id = gRegistry->AddRefType(c); |
| 1412 | location->method_id = ToMethodId(m); |
| 1413 | 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] | 1414 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1415 | } |
| 1416 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1417 | std::string Dbg::GetMethodName(JDWP::MethodId method_id) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1418 | ArtMethod* m = FromMethodId(method_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1419 | if (m == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1420 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1421 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1422 | return m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetName(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1425 | std::string Dbg::GetFieldName(JDWP::FieldId field_id) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1426 | ArtField* f = FromFieldId(field_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1427 | if (f == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1428 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1429 | } |
| 1430 | return f->GetName(); |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1433 | /* |
| 1434 | * Augment the access flags for synthetic methods and fields by setting |
| 1435 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 1436 | * flags not specified by the Java programming language. |
| 1437 | */ |
| 1438 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 1439 | accessFlags &= kAccJavaFlagsMask; |
| 1440 | if ((accessFlags & kAccSynthetic) != 0) { |
| 1441 | accessFlags |= 0xf0000000; |
| 1442 | } |
| 1443 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1446 | /* |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1447 | * Circularly shifts registers so that arguments come first. Debuggers |
| 1448 | * expect slots to begin with arguments, but dex code places them at |
| 1449 | * the end. |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1450 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1451 | static uint16_t MangleSlot(uint16_t slot, ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1452 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1453 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1454 | if (code_item == nullptr) { |
| 1455 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1456 | // return the slot as is since all registers are arguments. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1457 | LOG(WARNING) << "Trying to mangle slot for method without code " << m->PrettyMethod(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1458 | return slot; |
| 1459 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1460 | uint16_t ins_size = code_item->ins_size_; |
| 1461 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1462 | if (slot >= locals_size) { |
| 1463 | return slot - locals_size; |
| 1464 | } else { |
| 1465 | return slot + ins_size; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1466 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1467 | } |
| 1468 | |
Sebastien Hertz | aef0c91 | 2016-08-08 10:20:28 +0200 | [diff] [blame] | 1469 | static size_t GetMethodNumArgRegistersIncludingThis(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1470 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | aef0c91 | 2016-08-08 10:20:28 +0200 | [diff] [blame] | 1471 | uint32_t num_registers = ArtMethod::NumArgRegisters(method->GetShorty()); |
| 1472 | if (!method->IsStatic()) { |
| 1473 | ++num_registers; |
| 1474 | } |
| 1475 | return num_registers; |
| 1476 | } |
| 1477 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1478 | /* |
| 1479 | * Circularly shifts registers so that arguments come last. Reverts |
| 1480 | * slots to dex style argument placement. |
| 1481 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1482 | static uint16_t DemangleSlot(uint16_t slot, ArtMethod* m, JDWP::JdwpError* error) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1483 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1484 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1485 | if (code_item == nullptr) { |
| 1486 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1487 | // return the slot as is since all registers are arguments. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1488 | LOG(WARNING) << "Trying to demangle slot for method without code " |
| 1489 | << m->PrettyMethod(); |
Sebastien Hertz | aef0c91 | 2016-08-08 10:20:28 +0200 | [diff] [blame] | 1490 | uint16_t vreg_count = GetMethodNumArgRegistersIncludingThis(m); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1491 | if (slot < vreg_count) { |
| 1492 | *error = JDWP::ERR_NONE; |
| 1493 | return slot; |
| 1494 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1495 | } else { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1496 | if (slot < code_item->registers_size_) { |
| 1497 | uint16_t ins_size = code_item->ins_size_; |
| 1498 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1499 | *error = JDWP::ERR_NONE; |
| 1500 | return (slot < ins_size) ? slot + locals_size : slot - ins_size; |
| 1501 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1502 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1503 | |
| 1504 | // Slot is invalid in the method. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1505 | LOG(ERROR) << "Invalid local slot " << slot << " for method " << m->PrettyMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1506 | *error = JDWP::ERR_INVALID_SLOT; |
| 1507 | return DexFile::kDexNoIndex16; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1508 | } |
| 1509 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1510 | JDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, |
| 1511 | JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1512 | JDWP::JdwpError error; |
| 1513 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1514 | if (c == nullptr) { |
| 1515 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1516 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1517 | |
| 1518 | size_t instance_field_count = c->NumInstanceFields(); |
| 1519 | size_t static_field_count = c->NumStaticFields(); |
| 1520 | |
| 1521 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 1522 | |
| 1523 | for (size_t i = 0; i < instance_field_count + static_field_count; ++i) { |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1524 | ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : |
| 1525 | c->GetStaticField(i - instance_field_count); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1526 | expandBufAddFieldId(pReply, ToFieldId(f)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1527 | expandBufAddUtf8String(pReply, f->GetName()); |
| 1528 | expandBufAddUtf8String(pReply, f->GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1529 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1530 | static const char genericSignature[1] = ""; |
| 1531 | expandBufAddUtf8String(pReply, genericSignature); |
| 1532 | } |
| 1533 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 1534 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1535 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1536 | } |
| 1537 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1538 | JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1539 | JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1540 | JDWP::JdwpError error; |
| 1541 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1542 | if (c == nullptr) { |
| 1543 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1544 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1545 | |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 1546 | expandBufAdd4BE(pReply, c->NumMethods()); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1547 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1548 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 1549 | auto ptr_size = cl->GetImagePointerSize(); |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 1550 | for (ArtMethod& m : c->GetMethods(ptr_size)) { |
| 1551 | expandBufAddMethodId(pReply, ToMethodId(&m)); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1552 | expandBufAddUtf8String(pReply, m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetName()); |
| 1553 | expandBufAddUtf8String( |
| 1554 | pReply, m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetSignature().ToString()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1555 | if (with_generic) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1556 | const char* generic_signature = ""; |
| 1557 | expandBufAddUtf8String(pReply, generic_signature); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1558 | } |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 1559 | expandBufAdd4BE(pReply, MangleAccessFlags(m.GetAccessFlags())); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1560 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1561 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1562 | } |
| 1563 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1564 | JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1565 | JDWP::JdwpError error; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1566 | Thread* self = Thread::Current(); |
| 1567 | StackHandleScope<1> hs(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1568 | Handle<mirror::Class> c(hs.NewHandle(DecodeClass(class_id, &error))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1569 | if (c.Get() == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1570 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1571 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1572 | size_t interface_count = c->NumDirectInterfaces(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1573 | expandBufAdd4BE(pReply, interface_count); |
| 1574 | for (size_t i = 0; i < interface_count; ++i) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1575 | expandBufAddRefTypeId(pReply, |
| 1576 | gRegistry->AddRefType(mirror::Class::GetDirectInterface(self, c, i))); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1577 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1578 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1581 | void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1582 | struct DebugCallbackContext { |
| 1583 | int numItems; |
| 1584 | JDWP::ExpandBuf* pReply; |
| 1585 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1586 | static bool Callback(void* context, const DexFile::PositionInfo& entry) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1587 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1588 | expandBufAdd8BE(pContext->pReply, entry.address_); |
| 1589 | expandBufAdd4BE(pContext->pReply, entry.line_); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1590 | pContext->numItems++; |
Sebastien Hertz | f2910ee | 2013-10-19 16:39:24 +0200 | [diff] [blame] | 1591 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1592 | } |
| 1593 | }; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1594 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1595 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1596 | uint64_t start, end; |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1597 | if (code_item == nullptr) { |
| 1598 | DCHECK(m->IsNative() || m->IsProxyMethod()); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1599 | start = -1; |
| 1600 | end = -1; |
| 1601 | } else { |
| 1602 | start = 0; |
jeffhao | 14f0db9 | 2012-12-14 17:50:42 -0800 | [diff] [blame] | 1603 | // Return the index of the last instruction |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1604 | end = code_item->insns_size_in_code_units_ - 1; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | expandBufAdd8BE(pReply, start); |
| 1608 | expandBufAdd8BE(pReply, end); |
| 1609 | |
| 1610 | // Add numLines later |
| 1611 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 1612 | expandBufAdd4BE(pReply, 0); |
| 1613 | |
| 1614 | DebugCallbackContext context; |
| 1615 | context.numItems = 0; |
| 1616 | context.pReply = pReply; |
| 1617 | |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1618 | if (code_item != nullptr) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1619 | m->GetDexFile()->DecodeDebugPositionInfo(code_item, DebugCallbackContext::Callback, &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1620 | } |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1621 | |
| 1622 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1625 | void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, |
| 1626 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1627 | struct DebugCallbackContext { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1628 | ArtMethod* method; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1629 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1630 | size_t variable_count; |
| 1631 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1632 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1633 | static void Callback(void* context, const DexFile::LocalInfo& entry) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1634 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1635 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1636 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1637 | uint16_t slot = entry.reg_; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1638 | VLOG(jdwp) << StringPrintf(" %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d", |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1639 | pContext->variable_count, entry.start_address_, |
| 1640 | entry.end_address_ - entry.start_address_, |
| 1641 | entry.name_, entry.descriptor_, entry.signature_, slot, |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1642 | MangleSlot(slot, pContext->method)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1643 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1644 | slot = MangleSlot(slot, pContext->method); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1645 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1646 | expandBufAdd8BE(pContext->pReply, entry.start_address_); |
| 1647 | expandBufAddUtf8String(pContext->pReply, entry.name_); |
| 1648 | expandBufAddUtf8String(pContext->pReply, entry.descriptor_); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1649 | if (pContext->with_generic) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1650 | expandBufAddUtf8String(pContext->pReply, entry.signature_); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1651 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1652 | expandBufAdd4BE(pContext->pReply, entry.end_address_- entry.start_address_); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1653 | expandBufAdd4BE(pContext->pReply, slot); |
| 1654 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1655 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1656 | } |
| 1657 | }; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1658 | ArtMethod* m = FromMethodId(method_id); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1659 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1660 | // arg_count considers doubles and longs to take 2 units. |
| 1661 | // variable_count considers everything to take 1 unit. |
Sebastien Hertz | aef0c91 | 2016-08-08 10:20:28 +0200 | [diff] [blame] | 1662 | expandBufAdd4BE(pReply, GetMethodNumArgRegistersIncludingThis(m)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1663 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1664 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1665 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1666 | expandBufAdd4BE(pReply, 0); |
| 1667 | |
| 1668 | DebugCallbackContext context; |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1669 | context.method = m; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1670 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1671 | context.variable_count = 0; |
| 1672 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1673 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1674 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1675 | if (code_item != nullptr) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1676 | m->GetDexFile()->DecodeDebugLocalInfo( |
| 1677 | code_item, m->IsStatic(), m->GetDexMethodIndex(), DebugCallbackContext::Callback, |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1678 | &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1679 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1680 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1681 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1684 | void Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value, |
| 1685 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1686 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1687 | JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1688 | OutputJValue(tag, return_value, pReply); |
| 1689 | } |
| 1690 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1691 | void Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value, |
| 1692 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1693 | ArtField* f = FromFieldId(field_id); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1694 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1695 | OutputJValue(tag, field_value, pReply); |
| 1696 | } |
| 1697 | |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1698 | JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1699 | std::vector<uint8_t>* bytecodes) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1700 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1701 | if (m == nullptr) { |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1702 | return JDWP::ERR_INVALID_METHODID; |
| 1703 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1704 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1705 | size_t byte_count = code_item->insns_size_in_code_units_ * 2; |
| 1706 | const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_); |
| 1707 | const uint8_t* end = begin + byte_count; |
| 1708 | for (const uint8_t* p = begin; p != end; ++p) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1709 | bytecodes->push_back(*p); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1710 | } |
| 1711 | return JDWP::ERR_NONE; |
| 1712 | } |
| 1713 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1714 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1715 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1718 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1719 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1722 | static JValue GetArtFieldValue(ArtField* f, mirror::Object* o) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1723 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1724 | Primitive::Type fieldType = f->GetTypeAsPrimitiveType(); |
| 1725 | JValue field_value; |
| 1726 | switch (fieldType) { |
| 1727 | case Primitive::kPrimBoolean: |
| 1728 | field_value.SetZ(f->GetBoolean(o)); |
| 1729 | return field_value; |
| 1730 | |
| 1731 | case Primitive::kPrimByte: |
| 1732 | field_value.SetB(f->GetByte(o)); |
| 1733 | return field_value; |
| 1734 | |
| 1735 | case Primitive::kPrimChar: |
| 1736 | field_value.SetC(f->GetChar(o)); |
| 1737 | return field_value; |
| 1738 | |
| 1739 | case Primitive::kPrimShort: |
| 1740 | field_value.SetS(f->GetShort(o)); |
| 1741 | return field_value; |
| 1742 | |
| 1743 | case Primitive::kPrimInt: |
| 1744 | case Primitive::kPrimFloat: |
| 1745 | // Int and Float must be treated as 32-bit values in JDWP. |
| 1746 | field_value.SetI(f->GetInt(o)); |
| 1747 | return field_value; |
| 1748 | |
| 1749 | case Primitive::kPrimLong: |
| 1750 | case Primitive::kPrimDouble: |
| 1751 | // Long and Double must be treated as 64-bit values in JDWP. |
| 1752 | field_value.SetJ(f->GetLong(o)); |
| 1753 | return field_value; |
| 1754 | |
| 1755 | case Primitive::kPrimNot: |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 1756 | field_value.SetL(f->GetObject(o).Ptr()); |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1757 | return field_value; |
| 1758 | |
| 1759 | case Primitive::kPrimVoid: |
| 1760 | LOG(FATAL) << "Attempt to read from field of type 'void'"; |
| 1761 | UNREACHABLE(); |
| 1762 | } |
| 1763 | LOG(FATAL) << "Attempt to read from field of unknown type"; |
| 1764 | UNREACHABLE(); |
| 1765 | } |
| 1766 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1767 | static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id, |
| 1768 | JDWP::FieldId field_id, JDWP::ExpandBuf* pReply, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1769 | bool is_static) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1770 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1771 | JDWP::JdwpError error; |
| 1772 | mirror::Class* c = DecodeClass(ref_type_id, &error); |
| 1773 | if (ref_type_id != 0 && c == nullptr) { |
| 1774 | return error; |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1775 | } |
| 1776 | |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1777 | Thread* self = Thread::Current(); |
| 1778 | StackHandleScope<2> hs(self); |
| 1779 | MutableHandle<mirror::Object> |
| 1780 | o(hs.NewHandle(Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error))); |
| 1781 | if ((!is_static && o.Get() == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1782 | return JDWP::ERR_INVALID_OBJECT; |
| 1783 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1784 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1785 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1786 | mirror::Class* receiver_class = c; |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1787 | if (receiver_class == nullptr && o.Get() != nullptr) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1788 | receiver_class = o->GetClass(); |
| 1789 | } |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1790 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1791 | // TODO: should we give up now if receiver_class is null? |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1792 | if (receiver_class != nullptr && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1793 | LOG(INFO) << "ERR_INVALID_FIELDID: " << f->PrettyField() << " " |
| 1794 | << receiver_class->PrettyClass(); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1795 | return JDWP::ERR_INVALID_FIELDID; |
| 1796 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1797 | |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1798 | // Ensure the field's class is initialized. |
| 1799 | Handle<mirror::Class> klass(hs.NewHandle(f->GetDeclaringClass())); |
| 1800 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, klass, true, false)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1801 | LOG(WARNING) << "Not able to initialize class for SetValues: " |
| 1802 | << mirror::Class::PrettyClass(klass.Get()); |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1805 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1806 | // TODO: should we change the tests and check both? |
| 1807 | if (is_static) { |
| 1808 | if (!f->IsStatic()) { |
| 1809 | return JDWP::ERR_INVALID_FIELDID; |
| 1810 | } |
| 1811 | } else { |
| 1812 | if (f->IsStatic()) { |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1813 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.GetValues" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1814 | << " on static field " << f->PrettyField(); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1815 | } |
| 1816 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1817 | if (f->IsStatic()) { |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1818 | o.Assign(f->GetDeclaringClass()); |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1819 | } |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1820 | |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1821 | JValue field_value(GetArtFieldValue(f, o.Get())); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1822 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1823 | Dbg::OutputJValue(tag, &field_value, pReply); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1824 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1827 | JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1828 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1829 | return GetFieldValueImpl(0, object_id, field_id, pReply, false); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1830 | } |
| 1831 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1832 | JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, |
| 1833 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1834 | return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1835 | } |
| 1836 | |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1837 | static JDWP::JdwpError SetArtFieldValue(ArtField* f, mirror::Object* o, uint64_t value, int width) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1838 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1839 | Primitive::Type fieldType = f->GetTypeAsPrimitiveType(); |
| 1840 | // Debugging only happens at runtime so we know we are not running in a transaction. |
| 1841 | static constexpr bool kNoTransactionMode = false; |
| 1842 | switch (fieldType) { |
| 1843 | case Primitive::kPrimBoolean: |
| 1844 | CHECK_EQ(width, 1); |
| 1845 | f->SetBoolean<kNoTransactionMode>(o, static_cast<uint8_t>(value)); |
| 1846 | return JDWP::ERR_NONE; |
| 1847 | |
| 1848 | case Primitive::kPrimByte: |
| 1849 | CHECK_EQ(width, 1); |
| 1850 | f->SetByte<kNoTransactionMode>(o, static_cast<uint8_t>(value)); |
| 1851 | return JDWP::ERR_NONE; |
| 1852 | |
| 1853 | case Primitive::kPrimChar: |
| 1854 | CHECK_EQ(width, 2); |
| 1855 | f->SetChar<kNoTransactionMode>(o, static_cast<uint16_t>(value)); |
| 1856 | return JDWP::ERR_NONE; |
| 1857 | |
| 1858 | case Primitive::kPrimShort: |
| 1859 | CHECK_EQ(width, 2); |
| 1860 | f->SetShort<kNoTransactionMode>(o, static_cast<int16_t>(value)); |
| 1861 | return JDWP::ERR_NONE; |
| 1862 | |
| 1863 | case Primitive::kPrimInt: |
| 1864 | case Primitive::kPrimFloat: |
| 1865 | CHECK_EQ(width, 4); |
| 1866 | // Int and Float must be treated as 32-bit values in JDWP. |
| 1867 | f->SetInt<kNoTransactionMode>(o, static_cast<int32_t>(value)); |
| 1868 | return JDWP::ERR_NONE; |
| 1869 | |
| 1870 | case Primitive::kPrimLong: |
| 1871 | case Primitive::kPrimDouble: |
| 1872 | CHECK_EQ(width, 8); |
| 1873 | // Long and Double must be treated as 64-bit values in JDWP. |
| 1874 | f->SetLong<kNoTransactionMode>(o, value); |
| 1875 | return JDWP::ERR_NONE; |
| 1876 | |
| 1877 | case Primitive::kPrimNot: { |
| 1878 | JDWP::JdwpError error; |
| 1879 | mirror::Object* v = Dbg::GetObjectRegistry()->Get<mirror::Object*>(value, &error); |
| 1880 | if (error != JDWP::ERR_NONE) { |
| 1881 | return JDWP::ERR_INVALID_OBJECT; |
| 1882 | } |
| 1883 | if (v != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1884 | ObjPtr<mirror::Class> field_type; |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1885 | { |
| 1886 | StackHandleScope<2> hs(Thread::Current()); |
| 1887 | HandleWrapper<mirror::Object> h_v(hs.NewHandleWrapper(&v)); |
| 1888 | HandleWrapper<mirror::Object> h_o(hs.NewHandleWrapper(&o)); |
| 1889 | field_type = f->GetType<true>(); |
| 1890 | } |
| 1891 | if (!field_type->IsAssignableFrom(v->GetClass())) { |
| 1892 | return JDWP::ERR_INVALID_OBJECT; |
| 1893 | } |
| 1894 | } |
| 1895 | f->SetObject<kNoTransactionMode>(o, v); |
| 1896 | return JDWP::ERR_NONE; |
| 1897 | } |
| 1898 | |
| 1899 | case Primitive::kPrimVoid: |
| 1900 | LOG(FATAL) << "Attempt to write to field of type 'void'"; |
| 1901 | UNREACHABLE(); |
| 1902 | } |
| 1903 | LOG(FATAL) << "Attempt to write to field of unknown type"; |
| 1904 | UNREACHABLE(); |
| 1905 | } |
| 1906 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1907 | static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1908 | uint64_t value, int width, bool is_static) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1909 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1910 | JDWP::JdwpError error; |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1911 | Thread* self = Thread::Current(); |
| 1912 | StackHandleScope<2> hs(self); |
| 1913 | MutableHandle<mirror::Object> |
| 1914 | o(hs.NewHandle(Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error))); |
| 1915 | if ((!is_static && o.Get() == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1916 | return JDWP::ERR_INVALID_OBJECT; |
| 1917 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1918 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1919 | |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1920 | // Ensure the field's class is initialized. |
| 1921 | Handle<mirror::Class> klass(hs.NewHandle(f->GetDeclaringClass())); |
| 1922 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, klass, true, false)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1923 | LOG(WARNING) << "Not able to initialize class for SetValues: " |
| 1924 | << mirror::Class::PrettyClass(klass.Get()); |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1927 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1928 | // TODO: should we change the tests and check both? |
| 1929 | if (is_static) { |
| 1930 | if (!f->IsStatic()) { |
| 1931 | return JDWP::ERR_INVALID_FIELDID; |
| 1932 | } |
| 1933 | } else { |
| 1934 | if (f->IsStatic()) { |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1935 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1936 | << " on static field " << f->PrettyField(); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1937 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1938 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1939 | if (f->IsStatic()) { |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1940 | o.Assign(f->GetDeclaringClass()); |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1941 | } |
Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1942 | return SetArtFieldValue(f, o.Get(), value, width); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1943 | } |
| 1944 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1945 | 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] | 1946 | int width) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1947 | return SetFieldValueImpl(object_id, field_id, value, width, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1948 | } |
| 1949 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1950 | JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) { |
| 1951 | return SetFieldValueImpl(0, field_id, value, width, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1954 | JDWP::JdwpError Dbg::StringToUtf8(JDWP::ObjectId string_id, std::string* str) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1955 | JDWP::JdwpError error; |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1956 | mirror::Object* obj = gRegistry->Get<mirror::Object*>(string_id, &error); |
| 1957 | if (error != JDWP::ERR_NONE) { |
| 1958 | return error; |
| 1959 | } |
| 1960 | if (obj == nullptr) { |
| 1961 | return JDWP::ERR_INVALID_OBJECT; |
| 1962 | } |
| 1963 | { |
| 1964 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1965 | ObjPtr<mirror::Class> java_lang_String = |
| 1966 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_String); |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1967 | if (!java_lang_String->IsAssignableFrom(obj->GetClass())) { |
| 1968 | // This isn't a string. |
| 1969 | return JDWP::ERR_INVALID_STRING; |
| 1970 | } |
| 1971 | } |
| 1972 | *str = obj->AsString()->ToModifiedUtf8(); |
| 1973 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1976 | void Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) { |
| 1977 | if (IsPrimitiveTag(tag)) { |
| 1978 | expandBufAdd1(pReply, tag); |
| 1979 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 1980 | expandBufAdd1(pReply, return_value->GetI()); |
| 1981 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 1982 | expandBufAdd2BE(pReply, return_value->GetI()); |
| 1983 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 1984 | expandBufAdd4BE(pReply, return_value->GetI()); |
| 1985 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1986 | expandBufAdd8BE(pReply, return_value->GetJ()); |
| 1987 | } else { |
| 1988 | CHECK_EQ(tag, JDWP::JT_VOID); |
| 1989 | } |
| 1990 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1991 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1992 | mirror::Object* value = return_value->GetL(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1993 | expandBufAdd1(pReply, TagFromObject(soa, value)); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1994 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 1995 | } |
| 1996 | } |
| 1997 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1998 | JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string* name) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1999 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2000 | JDWP::JdwpError error; |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2001 | DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2002 | if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2003 | return error; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2004 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2005 | |
| 2006 | // 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] | 2007 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2008 | CHECK(thread_object != nullptr) << error; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2009 | ArtField* java_lang_Thread_name_field = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2010 | soa.DecodeField(WellKnownClasses::java_lang_Thread_name); |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 2011 | ObjPtr<mirror::String> s(java_lang_Thread_name_field->GetObject(thread_object)->AsString()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2012 | if (s != nullptr) { |
| 2013 | *name = s->ToModifiedUtf8(); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2014 | } |
| 2015 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2016 | } |
| 2017 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2018 | JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2019 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2020 | JDWP::JdwpError error; |
| 2021 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2022 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2023 | return JDWP::ERR_INVALID_OBJECT; |
| 2024 | } |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2025 | ScopedAssertNoThreadSuspension ants("Debugger: GetThreadGroup"); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2026 | // Okay, so it's an object, but is it actually a thread? |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2027 | DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2028 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2029 | // Zombie threads are in the null group. |
| 2030 | expandBufAddObjectId(pReply, JDWP::ObjectId(0)); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2031 | error = JDWP::ERR_NONE; |
| 2032 | } else if (error == JDWP::ERR_NONE) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 2033 | ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(WellKnownClasses::java_lang_Thread); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2034 | CHECK(c != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2035 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 2036 | CHECK(f != nullptr); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2037 | ObjPtr<mirror::Object> group = f->GetObject(thread_object); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 2038 | CHECK(group != nullptr); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2039 | JDWP::ObjectId thread_group_id = gRegistry->Add(group); |
| 2040 | expandBufAddObjectId(pReply, thread_group_id); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2041 | } |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2042 | return error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2043 | } |
| 2044 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2045 | static mirror::Object* DecodeThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 2046 | JDWP::ObjectId thread_group_id, JDWP::JdwpError* error) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2047 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2048 | mirror::Object* thread_group = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_group_id, |
| 2049 | error); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2050 | if (*error != JDWP::ERR_NONE) { |
| 2051 | return nullptr; |
| 2052 | } |
| 2053 | if (thread_group == nullptr) { |
| 2054 | *error = JDWP::ERR_INVALID_OBJECT; |
| 2055 | return nullptr; |
| 2056 | } |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 2057 | ObjPtr<mirror::Class> c = |
| 2058 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_ThreadGroup); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2059 | CHECK(c != nullptr); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2060 | if (!c->IsAssignableFrom(thread_group->GetClass())) { |
| 2061 | // This is not a java.lang.ThreadGroup. |
| 2062 | *error = JDWP::ERR_INVALID_THREAD_GROUP; |
| 2063 | return nullptr; |
| 2064 | } |
| 2065 | *error = JDWP::ERR_NONE; |
| 2066 | return thread_group; |
| 2067 | } |
| 2068 | |
| 2069 | JDWP::JdwpError Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
| 2070 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 2071 | JDWP::JdwpError error; |
| 2072 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2073 | if (error != JDWP::ERR_NONE) { |
| 2074 | return error; |
| 2075 | } |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2076 | ScopedAssertNoThreadSuspension ants("Debugger: GetThreadGroupName"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2077 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2078 | CHECK(f != nullptr); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2079 | ObjPtr<mirror::String> s = f->GetObject(thread_group)->AsString(); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2080 | |
| 2081 | std::string thread_group_name(s->ToModifiedUtf8()); |
| 2082 | expandBufAddUtf8String(pReply, thread_group_name); |
| 2083 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2086 | JDWP::JdwpError Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2087 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2088 | JDWP::JdwpError error; |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2089 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2090 | if (error != JDWP::ERR_NONE) { |
| 2091 | return error; |
| 2092 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2093 | ObjPtr<mirror::Object> parent; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2094 | { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2095 | ScopedAssertNoThreadSuspension ants("Debugger: GetThreadGroupParent"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2096 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_parent); |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2097 | CHECK(f != nullptr); |
| 2098 | parent = f->GetObject(thread_group); |
| 2099 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2100 | JDWP::ObjectId parent_group_id = gRegistry->Add(parent); |
| 2101 | expandBufAddObjectId(pReply, parent_group_id); |
| 2102 | return JDWP::ERR_NONE; |
| 2103 | } |
| 2104 | |
| 2105 | static void GetChildThreadGroups(ScopedObjectAccessUnchecked& soa, mirror::Object* thread_group, |
| 2106 | std::vector<JDWP::ObjectId>* child_thread_group_ids) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2107 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2108 | CHECK(thread_group != nullptr); |
| 2109 | |
Przemyslaw Szczepaniak | 464595f | 2015-11-24 11:59:59 +0000 | [diff] [blame] | 2110 | // Get the int "ngroups" count of this thread group... |
| 2111 | ArtField* ngroups_field = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_ngroups); |
| 2112 | CHECK(ngroups_field != nullptr); |
| 2113 | const int32_t size = ngroups_field->GetInt(thread_group); |
| 2114 | if (size == 0) { |
| 2115 | return; |
Sebastien Hertz | e49e195 | 2014-10-13 11:27:13 +0200 | [diff] [blame] | 2116 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2117 | |
Przemyslaw Szczepaniak | 464595f | 2015-11-24 11:59:59 +0000 | [diff] [blame] | 2118 | // Get the ThreadGroup[] "groups" out of this thread group... |
| 2119 | ArtField* groups_field = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_groups); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2120 | ObjPtr<mirror::Object> groups_array = groups_field->GetObject(thread_group); |
Przemyslaw Szczepaniak | 464595f | 2015-11-24 11:59:59 +0000 | [diff] [blame] | 2121 | |
| 2122 | CHECK(groups_array != nullptr); |
| 2123 | CHECK(groups_array->IsObjectArray()); |
| 2124 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2125 | ObjPtr<mirror::ObjectArray<mirror::Object>> groups_array_as_array = |
Przemyslaw Szczepaniak | 464595f | 2015-11-24 11:59:59 +0000 | [diff] [blame] | 2126 | groups_array->AsObjectArray<mirror::Object>(); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2127 | |
| 2128 | // Copy the first 'size' elements out of the array into the result. |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2129 | ObjectRegistry* registry = Dbg::GetObjectRegistry(); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2130 | for (int32_t i = 0; i < size; ++i) { |
Przemyslaw Szczepaniak | 464595f | 2015-11-24 11:59:59 +0000 | [diff] [blame] | 2131 | child_thread_group_ids->push_back(registry->Add(groups_array_as_array->Get(i))); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | JDWP::JdwpError Dbg::GetThreadGroupChildren(JDWP::ObjectId thread_group_id, |
| 2136 | JDWP::ExpandBuf* pReply) { |
| 2137 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 2138 | JDWP::JdwpError error; |
| 2139 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2140 | if (error != JDWP::ERR_NONE) { |
| 2141 | return error; |
| 2142 | } |
| 2143 | |
| 2144 | // Add child threads. |
| 2145 | { |
| 2146 | std::vector<JDWP::ObjectId> child_thread_ids; |
| 2147 | GetThreads(thread_group, &child_thread_ids); |
| 2148 | expandBufAdd4BE(pReply, child_thread_ids.size()); |
| 2149 | for (JDWP::ObjectId child_thread_id : child_thread_ids) { |
| 2150 | expandBufAddObjectId(pReply, child_thread_id); |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | // Add child thread groups. |
| 2155 | { |
| 2156 | std::vector<JDWP::ObjectId> child_thread_groups_ids; |
| 2157 | GetChildThreadGroups(soa, thread_group, &child_thread_groups_ids); |
| 2158 | expandBufAdd4BE(pReply, child_thread_groups_ids.size()); |
| 2159 | for (JDWP::ObjectId child_thread_group_id : child_thread_groups_ids) { |
| 2160 | expandBufAddObjectId(pReply, child_thread_group_id); |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2168 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2169 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2170 | ObjPtr<mirror::Object> group = f->GetObject(f->GetDeclaringClass()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2171 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2174 | JDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) { |
| 2175 | switch (state) { |
| 2176 | case kBlocked: |
| 2177 | return JDWP::TS_MONITOR; |
| 2178 | case kNative: |
| 2179 | case kRunnable: |
| 2180 | case kSuspended: |
| 2181 | return JDWP::TS_RUNNING; |
| 2182 | case kSleeping: |
| 2183 | return JDWP::TS_SLEEPING; |
| 2184 | case kStarting: |
| 2185 | case kTerminated: |
| 2186 | return JDWP::TS_ZOMBIE; |
| 2187 | case kTimedWaiting: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2188 | case kWaitingForCheckPointsToRun: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2189 | case kWaitingForDebuggerSend: |
| 2190 | case kWaitingForDebuggerSuspension: |
| 2191 | case kWaitingForDebuggerToAttach: |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 2192 | case kWaitingForDeoptimization: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2193 | case kWaitingForGcToComplete: |
Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 2194 | case kWaitingForGetObjectsAllocated: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2195 | case kWaitingForJniOnLoad: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2196 | case kWaitingForMethodTracingStart: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2197 | case kWaitingForSignalCatcherOutput: |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 2198 | case kWaitingForVisitObjects: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2199 | case kWaitingInMainDebuggerLoop: |
| 2200 | case kWaitingInMainSignalCatcherLoop: |
| 2201 | case kWaitingPerformingGc: |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 2202 | case kWaitingWeakGcRootRead: |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 2203 | case kWaitingForGcThreadFlip: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2204 | case kWaiting: |
| 2205 | return JDWP::TS_WAIT; |
| 2206 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 2207 | } |
| 2208 | LOG(FATAL) << "Unknown thread state: " << state; |
| 2209 | return JDWP::TS_ZOMBIE; |
| 2210 | } |
| 2211 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2212 | JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, |
| 2213 | JDWP::JdwpSuspendStatus* pSuspendStatus) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2214 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2215 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2216 | *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED; |
| 2217 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2218 | JDWP::JdwpError error; |
| 2219 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2220 | if (error != JDWP::ERR_NONE) { |
| 2221 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2222 | *pThreadStatus = JDWP::TS_ZOMBIE; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2223 | return JDWP::ERR_NONE; |
| 2224 | } |
| 2225 | return error; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2226 | } |
| 2227 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2228 | if (IsSuspendedForDebugger(soa, thread)) { |
| 2229 | *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2232 | *pThreadStatus = ToJdwpThreadStatus(thread->GetState()); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2233 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2234 | } |
| 2235 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2236 | JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2237 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2238 | JDWP::JdwpError error; |
| 2239 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2240 | if (error != JDWP::ERR_NONE) { |
| 2241 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2242 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2243 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2244 | expandBufAdd4BE(pReply, thread->GetDebugSuspendCount()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2245 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2248 | JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) { |
| 2249 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2250 | JDWP::JdwpError error; |
| 2251 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2252 | if (error != JDWP::ERR_NONE) { |
| 2253 | return error; |
| 2254 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 2255 | thread->Interrupt(soa.Self()); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2256 | return JDWP::ERR_NONE; |
| 2257 | } |
| 2258 | |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2259 | static bool IsInDesiredThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 2260 | mirror::Object* desired_thread_group, mirror::Object* peer) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2261 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2262 | // Do we want threads from all thread groups? |
| 2263 | if (desired_thread_group == nullptr) { |
| 2264 | return true; |
| 2265 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2266 | ArtField* thread_group_field = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2267 | DCHECK(thread_group_field != nullptr); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2268 | ObjPtr<mirror::Object> group = thread_group_field->GetObject(peer); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2269 | return (group == desired_thread_group); |
| 2270 | } |
| 2271 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2272 | 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] | 2273 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2274 | std::list<Thread*> all_threads_list; |
| 2275 | { |
| 2276 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 2277 | all_threads_list = Runtime::Current()->GetThreadList()->GetList(); |
| 2278 | } |
| 2279 | for (Thread* t : all_threads_list) { |
| 2280 | if (t == Dbg::GetDebugThread()) { |
| 2281 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 2282 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 2283 | continue; |
| 2284 | } |
| 2285 | if (t->IsStillStarting()) { |
| 2286 | // This thread is being started (and has been registered in the thread list). However, it is |
| 2287 | // not completely started yet so we must ignore it. |
| 2288 | continue; |
| 2289 | } |
| 2290 | mirror::Object* peer = t->GetPeer(); |
| 2291 | if (peer == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2292 | // 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] | 2293 | // this thread yet. |
| 2294 | // TODO: if we identified threads to the debugger by their Thread* |
| 2295 | // rather than their peer's mirror::Object*, we could fix this. |
| 2296 | // Doing so might help us report ZOMBIE threads too. |
| 2297 | continue; |
| 2298 | } |
| 2299 | if (IsInDesiredThreadGroup(soa, thread_group, peer)) { |
| 2300 | thread_ids->push_back(gRegistry->Add(peer)); |
| 2301 | } |
| 2302 | } |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2303 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2304 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2305 | static int GetStackDepth(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2306 | struct CountStackDepthVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2307 | explicit CountStackDepthVisitor(Thread* thread_in) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2308 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2309 | depth(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2310 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2311 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2312 | // annotalysis. |
| 2313 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2314 | if (!GetMethod()->IsRuntimeMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 2315 | ++depth; |
| 2316 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2317 | return true; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2318 | } |
| 2319 | size_t depth; |
| 2320 | }; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2321 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2322 | CountStackDepthVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2323 | visitor.WalkStack(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2324 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2325 | } |
| 2326 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2327 | JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t* result) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2328 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2329 | JDWP::JdwpError error; |
| 2330 | *result = 0; |
| 2331 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2332 | if (error != JDWP::ERR_NONE) { |
| 2333 | return error; |
| 2334 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2335 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2336 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2337 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2338 | *result = GetStackDepth(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2339 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2340 | } |
| 2341 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 2342 | JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame, |
| 2343 | size_t frame_count, JDWP::ExpandBuf* buf) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2344 | class GetFrameVisitor : public StackVisitor { |
| 2345 | public: |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2346 | GetFrameVisitor(Thread* thread, size_t start_frame_in, size_t frame_count_in, |
| 2347 | JDWP::ExpandBuf* buf_in) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2348 | REQUIRES_SHARED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2349 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2350 | depth_(0), |
| 2351 | start_frame_(start_frame_in), |
| 2352 | frame_count_(frame_count_in), |
| 2353 | buf_(buf_in) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2354 | expandBufAdd4BE(buf_, frame_count_); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2355 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2356 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2357 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2358 | if (GetMethod()->IsRuntimeMethod()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2359 | 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] | 2360 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2361 | if (depth_ >= start_frame_ + frame_count_) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2362 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2363 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2364 | if (depth_ >= start_frame_) { |
| 2365 | JDWP::FrameId frame_id(GetFrameId()); |
| 2366 | JDWP::JdwpLocation location; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2367 | SetJdwpLocation(&location, GetMethod(), GetDexPc()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2368 | VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2369 | expandBufAdd8BE(buf_, frame_id); |
| 2370 | expandBufAddLocation(buf_, location); |
| 2371 | } |
| 2372 | ++depth_; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2373 | return true; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2374 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2375 | |
| 2376 | private: |
| 2377 | size_t depth_; |
| 2378 | const size_t start_frame_; |
| 2379 | const size_t frame_count_; |
| 2380 | JDWP::ExpandBuf* buf_; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2381 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2382 | |
| 2383 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2384 | JDWP::JdwpError error; |
| 2385 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2386 | if (error != JDWP::ERR_NONE) { |
| 2387 | return error; |
| 2388 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2389 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2390 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2391 | } |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2392 | GetFrameVisitor visitor(thread, start_frame, frame_count, buf); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2393 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2394 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2398 | return GetThreadId(Thread::Current()); |
| 2399 | } |
| 2400 | |
| 2401 | JDWP::ObjectId Dbg::GetThreadId(Thread* thread) { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 2402 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2403 | return gRegistry->Add(thread->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2406 | void Dbg::SuspendVM() { |
Hiroshi Yamauchi | 8f95cf3 | 2016-04-19 11:14:06 -0700 | [diff] [blame] | 2407 | // Avoid a deadlock between GC and debugger where GC gets suspended during GC. b/25800335. |
| 2408 | gc::ScopedGCCriticalSection gcs(Thread::Current(), |
| 2409 | gc::kGcCauseDebugger, |
| 2410 | gc::kCollectorTypeDebugger); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2411 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | void Dbg::ResumeVM() { |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 2415 | Runtime::Current()->GetThreadList()->ResumeAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2418 | JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2419 | Thread* self = Thread::Current(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2420 | ScopedLocalRef<jobject> peer(self->GetJniEnv(), nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2421 | { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2422 | ScopedObjectAccess soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2423 | JDWP::JdwpError error; |
| 2424 | peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id, &error))); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2425 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2426 | if (peer.get() == nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2427 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2428 | } |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 2429 | // Suspend thread to build stack trace. |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2430 | bool timed_out; |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 2431 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 2432 | Thread* thread = thread_list->SuspendThreadByPeer(peer.get(), request_suspension, true, |
| 2433 | &timed_out); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2434 | if (thread != nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2435 | return JDWP::ERR_NONE; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2436 | } else if (timed_out) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2437 | return JDWP::ERR_INTERNAL; |
| 2438 | } else { |
| 2439 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2440 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2441 | } |
| 2442 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2443 | void Dbg::ResumeThread(JDWP::ObjectId thread_id) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2444 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2445 | JDWP::JdwpError error; |
| 2446 | mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2447 | CHECK(peer != nullptr) << error; |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2448 | Thread* thread; |
| 2449 | { |
| 2450 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 2451 | thread = Thread::FromManagedThread(soa, peer); |
| 2452 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2453 | if (thread == nullptr) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2454 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 2455 | return; |
| 2456 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2457 | bool needs_resume; |
| 2458 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2459 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2460 | needs_resume = thread->GetSuspendCount() > 0; |
| 2461 | } |
| 2462 | if (needs_resume) { |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 2463 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
| 2464 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2468 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2471 | struct GetThisVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2472 | GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id_in) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2473 | REQUIRES_SHARED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2474 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2475 | this_object(nullptr), |
| 2476 | frame_id(frame_id_in) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2477 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2478 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2479 | // annotalysis. |
| 2480 | virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2481 | if (frame_id != GetFrameId()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2482 | return true; // continue |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2483 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2484 | this_object = GetThisObject(); |
| 2485 | return false; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2486 | } |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 2487 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2488 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2489 | mirror::Object* this_object; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2490 | JDWP::FrameId frame_id; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2491 | }; |
| 2492 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2493 | JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, |
| 2494 | JDWP::ObjectId* result) { |
| 2495 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2496 | JDWP::JdwpError error; |
| 2497 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2498 | if (error != JDWP::ERR_NONE) { |
| 2499 | return error; |
| 2500 | } |
| 2501 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2502 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2503 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2504 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2505 | GetThisVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2506 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2507 | *result = gRegistry->Add(visitor.this_object); |
| 2508 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2509 | } |
| 2510 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2511 | // Walks the stack until we find the frame with the given FrameId. |
| 2512 | class FindFrameVisitor FINAL : public StackVisitor { |
| 2513 | public: |
| 2514 | FindFrameVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2515 | REQUIRES_SHARED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2516 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2517 | frame_id_(frame_id), |
| 2518 | error_(JDWP::ERR_INVALID_FRAMEID) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2519 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2520 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2521 | // annotalysis. |
| 2522 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 2523 | if (GetFrameId() != frame_id_) { |
| 2524 | return true; // Not our frame, carry on. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2525 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2526 | ArtMethod* m = GetMethod(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2527 | if (m->IsNative()) { |
| 2528 | // We can't read/write local value from/into native method. |
| 2529 | error_ = JDWP::ERR_OPAQUE_FRAME; |
| 2530 | } else { |
| 2531 | // We found our frame. |
| 2532 | error_ = JDWP::ERR_NONE; |
| 2533 | } |
| 2534 | return false; |
| 2535 | } |
| 2536 | |
| 2537 | JDWP::JdwpError GetError() const { |
| 2538 | return error_; |
| 2539 | } |
| 2540 | |
| 2541 | private: |
| 2542 | const JDWP::FrameId frame_id_; |
| 2543 | JDWP::JdwpError error_; |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame] | 2544 | |
| 2545 | DISALLOW_COPY_AND_ASSIGN(FindFrameVisitor); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2546 | }; |
| 2547 | |
| 2548 | JDWP::JdwpError Dbg::GetLocalValues(JDWP::Request* request, JDWP::ExpandBuf* pReply) { |
| 2549 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2550 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2551 | |
| 2552 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2553 | JDWP::JdwpError error; |
| 2554 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2555 | if (error != JDWP::ERR_NONE) { |
| 2556 | return error; |
| 2557 | } |
| 2558 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2559 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2560 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2561 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2562 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2563 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2564 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2565 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2566 | return visitor.GetError(); |
| 2567 | } |
| 2568 | |
| 2569 | // Read the values from visitor's context. |
| 2570 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2571 | expandBufAdd4BE(pReply, slot_count); /* "int values" */ |
| 2572 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2573 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2574 | JDWP::JdwpTag reqSigByte = request->ReadTag(); |
| 2575 | |
| 2576 | VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte; |
| 2577 | |
| 2578 | size_t width = Dbg::GetTagWidth(reqSigByte); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 2579 | uint8_t* ptr = expandBufAddSpace(pReply, width + 1); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2580 | error = Dbg::GetLocalValue(visitor, soa, slot, reqSigByte, ptr, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2581 | if (error != JDWP::ERR_NONE) { |
| 2582 | return error; |
| 2583 | } |
| 2584 | } |
| 2585 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2586 | } |
| 2587 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2588 | constexpr JDWP::JdwpError kStackFrameLocalAccessError = JDWP::ERR_ABSENT_INFORMATION; |
| 2589 | |
| 2590 | static std::string GetStackContextAsString(const StackVisitor& visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2591 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2592 | return StringPrintf(" at DEX pc 0x%08x in method %s", visitor.GetDexPc(false), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2593 | ArtMethod::PrettyMethod(visitor.GetMethod()).c_str()); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | static JDWP::JdwpError FailGetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2597 | JDWP::JdwpTag tag) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2598 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2599 | LOG(ERROR) << "Failed to read " << tag << " local from register v" << vreg |
| 2600 | << GetStackContextAsString(visitor); |
| 2601 | return kStackFrameLocalAccessError; |
| 2602 | } |
| 2603 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2604 | JDWP::JdwpError Dbg::GetLocalValue(const StackVisitor& visitor, ScopedObjectAccessUnchecked& soa, |
| 2605 | int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2606 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2607 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2608 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2609 | if (error != JDWP::ERR_NONE) { |
| 2610 | return error; |
| 2611 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2612 | // 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] | 2613 | switch (tag) { |
| 2614 | case JDWP::JT_BOOLEAN: { |
| 2615 | CHECK_EQ(width, 1U); |
| 2616 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2617 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2618 | return FailGetLocalValue(visitor, vreg, tag); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2619 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2620 | VLOG(jdwp) << "get boolean local " << vreg << " = " << intVal; |
| 2621 | JDWP::Set1(buf + 1, intVal != 0); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2622 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2623 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2624 | case JDWP::JT_BYTE: { |
| 2625 | CHECK_EQ(width, 1U); |
| 2626 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2627 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2628 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2629 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2630 | VLOG(jdwp) << "get byte local " << vreg << " = " << intVal; |
| 2631 | JDWP::Set1(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2632 | break; |
| 2633 | } |
| 2634 | case JDWP::JT_SHORT: |
| 2635 | case JDWP::JT_CHAR: { |
| 2636 | CHECK_EQ(width, 2U); |
| 2637 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2638 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2639 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2640 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2641 | VLOG(jdwp) << "get short/char local " << vreg << " = " << intVal; |
| 2642 | JDWP::Set2BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2643 | break; |
| 2644 | } |
| 2645 | case JDWP::JT_INT: { |
| 2646 | CHECK_EQ(width, 4U); |
| 2647 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2648 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2649 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2650 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2651 | VLOG(jdwp) << "get int local " << vreg << " = " << intVal; |
| 2652 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2653 | break; |
| 2654 | } |
| 2655 | case JDWP::JT_FLOAT: { |
| 2656 | CHECK_EQ(width, 4U); |
| 2657 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2658 | if (!visitor.GetVReg(m, vreg, kFloatVReg, &intVal)) { |
| 2659 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2660 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2661 | VLOG(jdwp) << "get float local " << vreg << " = " << intVal; |
| 2662 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2663 | break; |
| 2664 | } |
| 2665 | case JDWP::JT_ARRAY: |
| 2666 | case JDWP::JT_CLASS_LOADER: |
| 2667 | case JDWP::JT_CLASS_OBJECT: |
| 2668 | case JDWP::JT_OBJECT: |
| 2669 | case JDWP::JT_STRING: |
| 2670 | case JDWP::JT_THREAD: |
| 2671 | case JDWP::JT_THREAD_GROUP: { |
| 2672 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
| 2673 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2674 | if (!visitor.GetVReg(m, vreg, kReferenceVReg, &intVal)) { |
| 2675 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2676 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2677 | mirror::Object* o = reinterpret_cast<mirror::Object*>(intVal); |
| 2678 | VLOG(jdwp) << "get " << tag << " object local " << vreg << " = " << o; |
| 2679 | if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) { |
| 2680 | LOG(FATAL) << StringPrintf("Found invalid object %#" PRIxPTR " in register v%u", |
| 2681 | reinterpret_cast<uintptr_t>(o), vreg) |
| 2682 | << GetStackContextAsString(visitor); |
| 2683 | UNREACHABLE(); |
| 2684 | } |
| 2685 | tag = TagFromObject(soa, o); |
| 2686 | JDWP::SetObjectId(buf + 1, gRegistry->Add(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2687 | break; |
| 2688 | } |
| 2689 | case JDWP::JT_DOUBLE: { |
| 2690 | CHECK_EQ(width, 8U); |
| 2691 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2692 | if (!visitor.GetVRegPair(m, vreg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) { |
| 2693 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2694 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2695 | VLOG(jdwp) << "get double local " << vreg << " = " << longVal; |
| 2696 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2697 | break; |
| 2698 | } |
| 2699 | case JDWP::JT_LONG: { |
| 2700 | CHECK_EQ(width, 8U); |
| 2701 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2702 | if (!visitor.GetVRegPair(m, vreg, kLongLoVReg, kLongHiVReg, &longVal)) { |
| 2703 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2704 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2705 | VLOG(jdwp) << "get long local " << vreg << " = " << longVal; |
| 2706 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2707 | break; |
| 2708 | } |
| 2709 | default: |
| 2710 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2711 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2712 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2713 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2714 | // Prepend tag, which may have been updated. |
| 2715 | JDWP::Set1(buf, tag); |
| 2716 | return JDWP::ERR_NONE; |
| 2717 | } |
| 2718 | |
| 2719 | JDWP::JdwpError Dbg::SetLocalValues(JDWP::Request* request) { |
| 2720 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2721 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2722 | |
| 2723 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2724 | JDWP::JdwpError error; |
| 2725 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2726 | if (error != JDWP::ERR_NONE) { |
| 2727 | return error; |
| 2728 | } |
| 2729 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2730 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2731 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2732 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2733 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2734 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2735 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2736 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2737 | return visitor.GetError(); |
| 2738 | } |
| 2739 | |
| 2740 | // Writes the values into visitor's context. |
| 2741 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2742 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2743 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2744 | JDWP::JdwpTag sigByte = request->ReadTag(); |
| 2745 | size_t width = Dbg::GetTagWidth(sigByte); |
| 2746 | uint64_t value = request->ReadValue(width); |
| 2747 | |
| 2748 | VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value; |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 2749 | error = Dbg::SetLocalValue(thread, visitor, slot, sigByte, value, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2750 | if (error != JDWP::ERR_NONE) { |
| 2751 | return error; |
| 2752 | } |
| 2753 | } |
| 2754 | return JDWP::ERR_NONE; |
| 2755 | } |
| 2756 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2757 | template<typename T> |
| 2758 | static JDWP::JdwpError FailSetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2759 | JDWP::JdwpTag tag, T value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2760 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2761 | LOG(ERROR) << "Failed to write " << tag << " local " << value |
| 2762 | << " (0x" << std::hex << value << ") into register v" << vreg |
| 2763 | << GetStackContextAsString(visitor); |
| 2764 | return kStackFrameLocalAccessError; |
| 2765 | } |
| 2766 | |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 2767 | JDWP::JdwpError Dbg::SetLocalValue(Thread* thread, StackVisitor& visitor, int slot, |
| 2768 | JDWP::JdwpTag tag, uint64_t value, size_t width) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2769 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2770 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2771 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2772 | if (error != JDWP::ERR_NONE) { |
| 2773 | return error; |
| 2774 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2775 | // 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] | 2776 | switch (tag) { |
| 2777 | case JDWP::JT_BOOLEAN: |
| 2778 | case JDWP::JT_BYTE: |
| 2779 | CHECK_EQ(width, 1U); |
Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2780 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2781 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2782 | } |
| 2783 | break; |
| 2784 | case JDWP::JT_SHORT: |
| 2785 | case JDWP::JT_CHAR: |
| 2786 | CHECK_EQ(width, 2U); |
Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2787 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2788 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2789 | } |
| 2790 | break; |
| 2791 | case JDWP::JT_INT: |
| 2792 | CHECK_EQ(width, 4U); |
Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2793 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2794 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2795 | } |
| 2796 | break; |
| 2797 | case JDWP::JT_FLOAT: |
| 2798 | CHECK_EQ(width, 4U); |
Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2799 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kFloatVReg)) { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2800 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2801 | } |
| 2802 | break; |
| 2803 | case JDWP::JT_ARRAY: |
| 2804 | case JDWP::JT_CLASS_LOADER: |
| 2805 | case JDWP::JT_CLASS_OBJECT: |
| 2806 | case JDWP::JT_OBJECT: |
| 2807 | case JDWP::JT_STRING: |
| 2808 | case JDWP::JT_THREAD: |
| 2809 | case JDWP::JT_THREAD_GROUP: { |
| 2810 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2811 | mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value), |
| 2812 | &error); |
| 2813 | if (error != JDWP::ERR_NONE) { |
| 2814 | VLOG(jdwp) << tag << " object " << o << " is an invalid object"; |
| 2815 | return JDWP::ERR_INVALID_OBJECT; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2816 | } |
Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2817 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2818 | kReferenceVReg)) { |
| 2819 | return FailSetLocalValue(visitor, vreg, tag, reinterpret_cast<uintptr_t>(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2820 | } |
| 2821 | break; |
| 2822 | } |
| 2823 | case JDWP::JT_DOUBLE: { |
| 2824 | CHECK_EQ(width, 8U); |
Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2825 | if (!visitor.SetVRegPair(m, vreg, value, kDoubleLoVReg, kDoubleHiVReg)) { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2826 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2827 | } |
| 2828 | break; |
| 2829 | } |
| 2830 | case JDWP::JT_LONG: { |
| 2831 | CHECK_EQ(width, 8U); |
Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2832 | if (!visitor.SetVRegPair(m, vreg, value, kLongLoVReg, kLongHiVReg)) { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2833 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2834 | } |
| 2835 | break; |
| 2836 | } |
| 2837 | default: |
| 2838 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2839 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2840 | } |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 2841 | |
| 2842 | // If we set the local variable in a compiled frame, we need to trigger a deoptimization of |
| 2843 | // the stack so we continue execution with the interpreter using the new value(s) of the updated |
| 2844 | // local variable(s). To achieve this, we install instrumentation exit stub on each method of the |
| 2845 | // thread's stack. The stub will cause the deoptimization to happen. |
| 2846 | if (!visitor.IsShadowFrame() && thread->HasDebuggerShadowFrames()) { |
| 2847 | Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(thread); |
| 2848 | } |
| 2849 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2850 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2851 | } |
| 2852 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2853 | static void SetEventLocation(JDWP::EventLocation* location, ArtMethod* m, uint32_t dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2854 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2855 | DCHECK(location != nullptr); |
| 2856 | if (m == nullptr) { |
| 2857 | memset(location, 0, sizeof(*location)); |
| 2858 | } else { |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 2859 | location->method = GetCanonicalMethod(m); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2860 | 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] | 2861 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2862 | } |
| 2863 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2864 | void Dbg::PostLocationEvent(ArtMethod* m, int dex_pc, mirror::Object* this_object, |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2865 | int event_flags, const JValue* return_value) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2866 | if (!IsDebuggerActive()) { |
| 2867 | return; |
| 2868 | } |
| 2869 | DCHECK(m != nullptr); |
| 2870 | DCHECK_EQ(m->IsStatic(), this_object == nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2871 | JDWP::EventLocation location; |
| 2872 | SetEventLocation(&location, m, dex_pc); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2873 | |
Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2874 | // We need to be sure no exception is pending when calling JdwpState::PostLocationEvent. |
| 2875 | // This is required to be able to call JNI functions to create JDWP ids. To achieve this, |
| 2876 | // we temporarily clear the current thread's exception (if any) and will restore it after |
| 2877 | // the call. |
| 2878 | // Note: the only way to get a pending exception here is to suspend on a move-exception |
| 2879 | // instruction. |
| 2880 | Thread* const self = Thread::Current(); |
| 2881 | StackHandleScope<1> hs(self); |
| 2882 | Handle<mirror::Throwable> pending_exception(hs.NewHandle(self->GetException())); |
| 2883 | self->ClearException(); |
| 2884 | if (kIsDebugBuild && pending_exception.Get() != nullptr) { |
| 2885 | const DexFile::CodeItem* code_item = location.method->GetCodeItem(); |
| 2886 | const Instruction* instr = Instruction::At(&code_item->insns_[location.dex_pc]); |
| 2887 | CHECK_EQ(Instruction::MOVE_EXCEPTION, instr->Opcode()); |
| 2888 | } |
| 2889 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2890 | gJdwpState->PostLocationEvent(&location, this_object, event_flags, return_value); |
Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2891 | |
| 2892 | if (pending_exception.Get() != nullptr) { |
| 2893 | self->SetException(pending_exception.Get()); |
| 2894 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2895 | } |
| 2896 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2897 | void Dbg::PostFieldAccessEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2898 | mirror::Object* this_object, ArtField* f) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2899 | if (!IsDebuggerActive()) { |
| 2900 | return; |
| 2901 | } |
| 2902 | DCHECK(m != nullptr); |
| 2903 | DCHECK(f != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2904 | JDWP::EventLocation location; |
| 2905 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2906 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2907 | gJdwpState->PostFieldEvent(&location, f, this_object, nullptr, false); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2908 | } |
| 2909 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2910 | void Dbg::PostFieldModificationEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2911 | mirror::Object* this_object, ArtField* f, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2912 | const JValue* field_value) { |
| 2913 | if (!IsDebuggerActive()) { |
| 2914 | return; |
| 2915 | } |
| 2916 | DCHECK(m != nullptr); |
| 2917 | DCHECK(f != nullptr); |
| 2918 | DCHECK(field_value != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2919 | JDWP::EventLocation location; |
| 2920 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2921 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2922 | gJdwpState->PostFieldEvent(&location, f, this_object, field_value, true); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2923 | } |
| 2924 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2925 | /** |
| 2926 | * Finds the location where this exception will be caught. We search until we reach the top |
| 2927 | * frame, in which case this exception is considered uncaught. |
| 2928 | */ |
| 2929 | class CatchLocationFinder : public StackVisitor { |
| 2930 | public: |
| 2931 | CatchLocationFinder(Thread* self, const Handle<mirror::Throwable>& exception, Context* context) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2932 | REQUIRES_SHARED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2933 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2934 | exception_(exception), |
| 2935 | handle_scope_(self), |
| 2936 | this_at_throw_(handle_scope_.NewHandle<mirror::Object>(nullptr)), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2937 | catch_method_(nullptr), |
| 2938 | throw_method_(nullptr), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2939 | catch_dex_pc_(DexFile::kDexNoIndex), |
| 2940 | throw_dex_pc_(DexFile::kDexNoIndex) { |
| 2941 | } |
| 2942 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2943 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2944 | ArtMethod* method = GetMethod(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2945 | DCHECK(method != nullptr); |
| 2946 | if (method->IsRuntimeMethod()) { |
| 2947 | // Ignore callee save method. |
| 2948 | DCHECK(method->IsCalleeSaveMethod()); |
| 2949 | return true; |
| 2950 | } |
| 2951 | |
| 2952 | uint32_t dex_pc = GetDexPc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2953 | if (throw_method_ == nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2954 | // First Java method found. It is either the method that threw the exception, |
| 2955 | // or the Java native method that is reporting an exception thrown by |
| 2956 | // native code. |
| 2957 | this_at_throw_.Assign(GetThisObject()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2958 | throw_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2959 | throw_dex_pc_ = dex_pc; |
| 2960 | } |
| 2961 | |
| 2962 | if (dex_pc != DexFile::kDexNoIndex) { |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame] | 2963 | StackHandleScope<1> hs(GetThread()); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2964 | uint32_t found_dex_pc; |
| 2965 | Handle<mirror::Class> exception_class(hs.NewHandle(exception_->GetClass())); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2966 | bool unused_clear_exception; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2967 | found_dex_pc = method->FindCatchBlock(exception_class, dex_pc, &unused_clear_exception); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2968 | if (found_dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2969 | catch_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2970 | catch_dex_pc_ = found_dex_pc; |
| 2971 | return false; // End stack walk. |
| 2972 | } |
| 2973 | } |
| 2974 | return true; // Continue stack walk. |
| 2975 | } |
| 2976 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2977 | ArtMethod* GetCatchMethod() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2978 | return catch_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2981 | ArtMethod* GetThrowMethod() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2982 | return throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2985 | mirror::Object* GetThisAtThrow() REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2986 | return this_at_throw_.Get(); |
| 2987 | } |
| 2988 | |
| 2989 | uint32_t GetCatchDexPc() const { |
| 2990 | return catch_dex_pc_; |
| 2991 | } |
| 2992 | |
| 2993 | uint32_t GetThrowDexPc() const { |
| 2994 | return throw_dex_pc_; |
| 2995 | } |
| 2996 | |
| 2997 | private: |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2998 | const Handle<mirror::Throwable>& exception_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2999 | StackHandleScope<1> handle_scope_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3000 | MutableHandle<mirror::Object> this_at_throw_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3001 | ArtMethod* catch_method_; |
| 3002 | ArtMethod* throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3003 | uint32_t catch_dex_pc_; |
| 3004 | uint32_t throw_dex_pc_; |
| 3005 | |
| 3006 | DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder); |
| 3007 | }; |
| 3008 | |
| 3009 | void Dbg::PostException(mirror::Throwable* exception_object) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 3010 | if (!IsDebuggerActive()) { |
Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 3011 | return; |
| 3012 | } |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 3013 | Thread* const self = Thread::Current(); |
| 3014 | StackHandleScope<1> handle_scope(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3015 | Handle<mirror::Throwable> h_exception(handle_scope.NewHandle(exception_object)); |
| 3016 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 3017 | CatchLocationFinder clf(self, h_exception, context.get()); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3018 | clf.WalkStack(/* include_transitions */ false); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3019 | JDWP::EventLocation exception_throw_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3020 | SetEventLocation(&exception_throw_location, clf.GetThrowMethod(), clf.GetThrowDexPc()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3021 | JDWP::EventLocation exception_catch_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3022 | SetEventLocation(&exception_catch_location, clf.GetCatchMethod(), clf.GetCatchDexPc()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3023 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3024 | gJdwpState->PostException(&exception_throw_location, h_exception.Get(), &exception_catch_location, |
| 3025 | clf.GetThisAtThrow()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3026 | } |
| 3027 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3028 | void Dbg::PostClassPrepare(mirror::Class* c) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 3029 | if (!IsDebuggerActive()) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3030 | return; |
| 3031 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3032 | gJdwpState->PostClassPrepare(c); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3033 | } |
| 3034 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3035 | void Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3036 | ArtMethod* m, uint32_t dex_pc, |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 3037 | int event_flags, const JValue* return_value) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3038 | if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 3039 | return; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3040 | } |
| 3041 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3042 | if (IsBreakpoint(m, dex_pc)) { |
| 3043 | event_flags |= kBreakpoint; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3044 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3045 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3046 | // If the debugger is single-stepping one of our threads, check to |
| 3047 | // see if we're that thread and we've reached a step point. |
| 3048 | const SingleStepControl* single_step_control = thread->GetSingleStepControl(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3049 | if (single_step_control != nullptr) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3050 | CHECK(!m->IsNative()); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3051 | if (single_step_control->GetStepDepth() == JDWP::SD_INTO) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3052 | // Step into method calls. We break when the line number |
| 3053 | // or method pointer changes. If we're in SS_MIN mode, we |
| 3054 | // always stop. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3055 | if (single_step_control->GetMethod() != m) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3056 | event_flags |= kSingleStep; |
| 3057 | VLOG(jdwp) << "SS new method"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3058 | } else if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3059 | event_flags |= kSingleStep; |
| 3060 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3061 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3062 | event_flags |= kSingleStep; |
| 3063 | VLOG(jdwp) << "SS new line"; |
| 3064 | } |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3065 | } else if (single_step_control->GetStepDepth() == JDWP::SD_OVER) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3066 | // Step over method calls. We break when the line number is |
| 3067 | // different and the frame depth is <= the original frame |
| 3068 | // depth. (We can't just compare on the method, because we |
| 3069 | // might get unrolled past it by an exception, and it's tricky |
| 3070 | // to identify recursion.) |
| 3071 | |
| 3072 | int stack_depth = GetStackDepth(thread); |
| 3073 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3074 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3075 | // Popped up one or more frames, always trigger. |
| 3076 | event_flags |= kSingleStep; |
| 3077 | VLOG(jdwp) << "SS method pop"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3078 | } else if (stack_depth == single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3079 | // Same depth, see if we moved. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3080 | if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3081 | event_flags |= kSingleStep; |
| 3082 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3083 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3084 | event_flags |= kSingleStep; |
| 3085 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3086 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3087 | } |
| 3088 | } else { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3089 | CHECK_EQ(single_step_control->GetStepDepth(), JDWP::SD_OUT); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3090 | // Return from the current method. We break when the frame |
| 3091 | // depth pops up. |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3092 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3093 | // This differs from the "method exit" break in that it stops |
| 3094 | // with the PC at the next instruction in the returned-to |
| 3095 | // function, rather than the end of the returning function. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3096 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3097 | int stack_depth = GetStackDepth(thread); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3098 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3099 | event_flags |= kSingleStep; |
| 3100 | VLOG(jdwp) << "SS method pop"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3101 | } |
| 3102 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3103 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3104 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3105 | // If there's something interesting going on, see if it matches one |
| 3106 | // of the debugger filters. |
| 3107 | if (event_flags != 0) { |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 3108 | Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3109 | } |
| 3110 | } |
| 3111 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3112 | size_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) { |
| 3113 | switch (instrumentation_event) { |
| 3114 | case instrumentation::Instrumentation::kMethodEntered: |
| 3115 | return &method_enter_event_ref_count_; |
| 3116 | case instrumentation::Instrumentation::kMethodExited: |
| 3117 | return &method_exit_event_ref_count_; |
| 3118 | case instrumentation::Instrumentation::kDexPcMoved: |
| 3119 | return &dex_pc_change_event_ref_count_; |
| 3120 | case instrumentation::Instrumentation::kFieldRead: |
| 3121 | return &field_read_event_ref_count_; |
| 3122 | case instrumentation::Instrumentation::kFieldWritten: |
| 3123 | return &field_write_event_ref_count_; |
| 3124 | case instrumentation::Instrumentation::kExceptionCaught: |
| 3125 | return &exception_catch_event_ref_count_; |
| 3126 | default: |
| 3127 | return nullptr; |
| 3128 | } |
| 3129 | } |
| 3130 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3131 | // Process request while all mutator threads are suspended. |
| 3132 | void Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3133 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3134 | switch (request.GetKind()) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3135 | case DeoptimizationRequest::kNothing: |
| 3136 | LOG(WARNING) << "Ignoring empty deoptimization request."; |
| 3137 | break; |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3138 | case DeoptimizationRequest::kRegisterForEvent: |
| 3139 | VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3140 | request.InstrumentationEvent()); |
| 3141 | instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent()); |
| 3142 | instrumentation_events_ |= request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3143 | break; |
| 3144 | case DeoptimizationRequest::kUnregisterForEvent: |
| 3145 | VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3146 | request.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3147 | instrumentation->RemoveListener(&gDebugInstrumentationListener, |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3148 | request.InstrumentationEvent()); |
| 3149 | instrumentation_events_ &= ~request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3150 | break; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3151 | case DeoptimizationRequest::kFullDeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3152 | VLOG(jdwp) << "Deoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3153 | instrumentation->DeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3154 | VLOG(jdwp) << "Deoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3155 | break; |
| 3156 | case DeoptimizationRequest::kFullUndeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3157 | VLOG(jdwp) << "Undeoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3158 | instrumentation->UndeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3159 | VLOG(jdwp) << "Undeoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3160 | break; |
| 3161 | case DeoptimizationRequest::kSelectiveDeoptimization: |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3162 | VLOG(jdwp) << "Deoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " ..."; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3163 | instrumentation->Deoptimize(request.Method()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3164 | VLOG(jdwp) << "Deoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3165 | break; |
| 3166 | case DeoptimizationRequest::kSelectiveUndeoptimization: |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3167 | VLOG(jdwp) << "Undeoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " ..."; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3168 | instrumentation->Undeoptimize(request.Method()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3169 | VLOG(jdwp) << "Undeoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3170 | break; |
| 3171 | default: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3172 | LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3173 | break; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3174 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3175 | } |
| 3176 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3177 | void Dbg::RequestDeoptimization(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3178 | if (req.GetKind() == DeoptimizationRequest::kNothing) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3179 | // Nothing to do. |
| 3180 | return; |
| 3181 | } |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3182 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3183 | RequestDeoptimizationLocked(req); |
| 3184 | } |
| 3185 | |
| 3186 | void Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3187 | switch (req.GetKind()) { |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3188 | case DeoptimizationRequest::kRegisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3189 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3190 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3191 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3192 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3193 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3194 | 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] | 3195 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3196 | deoptimization_requests_.push_back(req); |
| 3197 | } |
| 3198 | *counter = *counter + 1; |
| 3199 | break; |
| 3200 | } |
| 3201 | case DeoptimizationRequest::kUnregisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3202 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3203 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3204 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3205 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3206 | *counter = *counter - 1; |
| 3207 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3208 | 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] | 3209 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3210 | deoptimization_requests_.push_back(req); |
| 3211 | } |
| 3212 | break; |
| 3213 | } |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3214 | case DeoptimizationRequest::kFullDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3215 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3216 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3217 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3218 | << " for full deoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3219 | deoptimization_requests_.push_back(req); |
| 3220 | } |
| 3221 | ++full_deoptimization_event_count_; |
| 3222 | break; |
| 3223 | } |
| 3224 | case DeoptimizationRequest::kFullUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3225 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | e713d93 | 2014-05-15 10:48:53 +0200 | [diff] [blame] | 3226 | DCHECK_GT(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3227 | --full_deoptimization_event_count_; |
| 3228 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3229 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3230 | << " for full undeoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3231 | deoptimization_requests_.push_back(req); |
| 3232 | } |
| 3233 | break; |
| 3234 | } |
| 3235 | case DeoptimizationRequest::kSelectiveDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3236 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3237 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3238 | << " for deoptimization of " << req.Method()->PrettyMethod(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3239 | deoptimization_requests_.push_back(req); |
| 3240 | break; |
| 3241 | } |
| 3242 | case DeoptimizationRequest::kSelectiveUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3243 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3244 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3245 | << " for undeoptimization of " << req.Method()->PrettyMethod(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3246 | deoptimization_requests_.push_back(req); |
| 3247 | break; |
| 3248 | } |
| 3249 | default: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3250 | LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3251 | break; |
| 3252 | } |
| 3253 | } |
| 3254 | } |
| 3255 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3256 | void Dbg::ManageDeoptimization() { |
| 3257 | Thread* const self = Thread::Current(); |
| 3258 | { |
| 3259 | // Avoid suspend/resume if there is no pending request. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3260 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3261 | if (deoptimization_requests_.empty()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3262 | return; |
| 3263 | } |
| 3264 | } |
| 3265 | CHECK_EQ(self->GetState(), kRunnable); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3266 | ScopedThreadSuspension sts(self, kWaitingForDeoptimization); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 3267 | // Required for ProcessDeoptimizationRequest. |
| 3268 | gc::ScopedGCCriticalSection gcs(self, |
| 3269 | gc::kGcCauseInstrumentation, |
| 3270 | gc::kCollectorTypeInstrumentation); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3271 | // We need to suspend mutator threads first. |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 3272 | ScopedSuspendAll ssa(__FUNCTION__); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3273 | const ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3274 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3275 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3276 | size_t req_index = 0; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3277 | for (DeoptimizationRequest& request : deoptimization_requests_) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3278 | VLOG(jdwp) << "Process deoptimization request #" << req_index++; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3279 | ProcessDeoptimizationRequest(request); |
| 3280 | } |
| 3281 | deoptimization_requests_.clear(); |
| 3282 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3283 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3284 | } |
| 3285 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3286 | static const Breakpoint* FindFirstBreakpointForMethod(ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3287 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3288 | for (Breakpoint& breakpoint : gBreakpoints) { |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 3289 | if (breakpoint.IsInMethod(m)) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3290 | return &breakpoint; |
| 3291 | } |
| 3292 | } |
| 3293 | return nullptr; |
| 3294 | } |
| 3295 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3296 | bool Dbg::MethodHasAnyBreakpoints(ArtMethod* method) { |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 3297 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| 3298 | return FindFirstBreakpointForMethod(method) != nullptr; |
| 3299 | } |
| 3300 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3301 | // Sanity checks all existing breakpoints on the same method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3302 | static void SanityCheckExistingBreakpoints(ArtMethod* m, |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3303 | DeoptimizationRequest::Kind deoptimization_kind) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3304 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3305 | for (const Breakpoint& breakpoint : gBreakpoints) { |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 3306 | if (breakpoint.IsInMethod(m)) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3307 | CHECK_EQ(deoptimization_kind, breakpoint.GetDeoptimizationKind()); |
| 3308 | } |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3309 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3310 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 3311 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3312 | // We should have deoptimized everything but not "selectively" deoptimized this method. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3313 | CHECK(instrumentation->AreAllMethodsDeoptimized()); |
| 3314 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3315 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3316 | // We should have "selectively" deoptimized this method. |
| 3317 | // Note: while we have not deoptimized everything for this method, we may have done it for |
| 3318 | // another event. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3319 | CHECK(instrumentation->IsDeoptimized(m)); |
| 3320 | } else { |
| 3321 | // This method does not require deoptimization. |
| 3322 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3323 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3324 | } |
| 3325 | } |
| 3326 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3327 | // Returns the deoptimization kind required to set a breakpoint in a method. |
| 3328 | // If a breakpoint has already been set, we also return the first breakpoint |
| 3329 | // through the given 'existing_brkpt' pointer. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3330 | static DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3331 | ArtMethod* m, |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3332 | const Breakpoint** existing_brkpt) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3333 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3334 | if (!Dbg::RequiresDeoptimization()) { |
| 3335 | // We already run in interpreter-only mode so we don't need to deoptimize anything. |
| 3336 | VLOG(jdwp) << "No need for deoptimization when fully running with interpreter for method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3337 | << ArtMethod::PrettyMethod(m); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3338 | return DeoptimizationRequest::kNothing; |
| 3339 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3340 | const Breakpoint* first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3341 | { |
| 3342 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3343 | first_breakpoint = FindFirstBreakpointForMethod(m); |
| 3344 | *existing_brkpt = first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3345 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3346 | |
| 3347 | if (first_breakpoint == nullptr) { |
Nicolas Geoffray | 6300fd7 | 2016-03-18 09:40:17 +0000 | [diff] [blame] | 3348 | // There is no breakpoint on this method yet: we need to deoptimize. If this method is default, |
| 3349 | // we deoptimize everything; otherwise we deoptimize only this method. We |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 3350 | // deoptimize with defaults because we do not know everywhere they are used. It is possible some |
Nicolas Geoffray | 6300fd7 | 2016-03-18 09:40:17 +0000 | [diff] [blame] | 3351 | // of the copies could be missed. |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 3352 | // TODO Deoptimizing on default methods might not be necessary in all cases. |
Nicolas Geoffray | 6300fd7 | 2016-03-18 09:40:17 +0000 | [diff] [blame] | 3353 | bool need_full_deoptimization = m->IsDefault(); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3354 | if (need_full_deoptimization) { |
Nicolas Geoffray | 6300fd7 | 2016-03-18 09:40:17 +0000 | [diff] [blame] | 3355 | VLOG(jdwp) << "Need full deoptimization because of copying of method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3356 | << ArtMethod::PrettyMethod(m); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3357 | return DeoptimizationRequest::kFullDeoptimization; |
| 3358 | } else { |
| 3359 | // We don't need to deoptimize if the method has not been compiled. |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 3360 | const bool is_compiled = m->HasAnyCompiledCode(); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3361 | if (is_compiled) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3362 | VLOG(jdwp) << "Need selective deoptimization for compiled method " |
| 3363 | << ArtMethod::PrettyMethod(m); |
Nicolas Geoffray | 6300fd7 | 2016-03-18 09:40:17 +0000 | [diff] [blame] | 3364 | return DeoptimizationRequest::kSelectiveDeoptimization; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3365 | } else { |
| 3366 | // Method is not compiled: we don't need to deoptimize. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3367 | VLOG(jdwp) << "No need for deoptimization for non-compiled method " |
| 3368 | << ArtMethod::PrettyMethod(m); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3369 | return DeoptimizationRequest::kNothing; |
| 3370 | } |
| 3371 | } |
| 3372 | } else { |
| 3373 | // There is at least one breakpoint for this method: we don't need to deoptimize. |
| 3374 | // Let's check that all breakpoints are configured the same way for deoptimization. |
| 3375 | VLOG(jdwp) << "Breakpoint already set: no deoptimization is required"; |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3376 | DeoptimizationRequest::Kind deoptimization_kind = first_breakpoint->GetDeoptimizationKind(); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3377 | if (kIsDebugBuild) { |
| 3378 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
| 3379 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
| 3380 | } |
| 3381 | return DeoptimizationRequest::kNothing; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3382 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3383 | } |
| 3384 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3385 | // Installs a breakpoint at the specified location. Also indicates through the deoptimization |
| 3386 | // request if we need to deoptimize. |
| 3387 | void Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
| 3388 | Thread* const self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3389 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3390 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3391 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3392 | const Breakpoint* existing_breakpoint = nullptr; |
| 3393 | const DeoptimizationRequest::Kind deoptimization_kind = |
| 3394 | GetRequiredDeoptimizationKind(self, m, &existing_breakpoint); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3395 | req->SetKind(deoptimization_kind); |
| 3396 | if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
| 3397 | req->SetMethod(m); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3398 | } else { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3399 | CHECK(deoptimization_kind == DeoptimizationRequest::kNothing || |
| 3400 | deoptimization_kind == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3401 | req->SetMethod(nullptr); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3402 | } |
| 3403 | |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3404 | { |
| 3405 | WriterMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3406 | // If there is at least one existing breakpoint on the same method, the new breakpoint |
| 3407 | // must have the same deoptimization kind than the existing breakpoint(s). |
| 3408 | DeoptimizationRequest::Kind breakpoint_deoptimization_kind; |
| 3409 | if (existing_breakpoint != nullptr) { |
| 3410 | breakpoint_deoptimization_kind = existing_breakpoint->GetDeoptimizationKind(); |
| 3411 | } else { |
| 3412 | breakpoint_deoptimization_kind = deoptimization_kind; |
| 3413 | } |
| 3414 | gBreakpoints.push_back(Breakpoint(m, location->dex_pc, breakpoint_deoptimization_kind)); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3415 | VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " |
| 3416 | << gBreakpoints[gBreakpoints.size() - 1]; |
| 3417 | } |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | // Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization |
| 3421 | // request if we need to undeoptimize. |
| 3422 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3423 | WriterMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3424 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3425 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3426 | DeoptimizationRequest::Kind deoptimization_kind = DeoptimizationRequest::kNothing; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3427 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 3428 | if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].IsInMethod(m)) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3429 | VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i]; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3430 | deoptimization_kind = gBreakpoints[i].GetDeoptimizationKind(); |
| 3431 | DCHECK_EQ(deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization, |
| 3432 | Runtime::Current()->GetInstrumentation()->IsDeoptimized(m)); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3433 | gBreakpoints.erase(gBreakpoints.begin() + i); |
| 3434 | break; |
| 3435 | } |
| 3436 | } |
| 3437 | const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m); |
| 3438 | if (existing_breakpoint == nullptr) { |
| 3439 | // There is no more breakpoint on this method: we need to undeoptimize. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3440 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3441 | // This method required full deoptimization: we need to undeoptimize everything. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3442 | req->SetKind(DeoptimizationRequest::kFullUndeoptimization); |
| 3443 | req->SetMethod(nullptr); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3444 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3445 | // This method required selective deoptimization: we need to undeoptimize only that method. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3446 | req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization); |
| 3447 | req->SetMethod(m); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3448 | } else { |
| 3449 | // This method had no need for deoptimization: do nothing. |
| 3450 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3451 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3452 | req->SetMethod(nullptr); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3453 | } |
| 3454 | } else { |
| 3455 | // 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] | 3456 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3457 | req->SetMethod(nullptr); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3458 | if (kIsDebugBuild) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3459 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3460 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3461 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3464 | bool Dbg::IsForcedInterpreterNeededForCallingImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3465 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3466 | if (ssc == nullptr) { |
| 3467 | // If we are not single-stepping, then we don't have to force interpreter. |
| 3468 | return false; |
| 3469 | } |
| 3470 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
| 3471 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3472 | return false; |
| 3473 | } |
| 3474 | |
| 3475 | if (!m->IsNative() && !m->IsProxyMethod()) { |
| 3476 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3477 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3478 | return true; |
| 3479 | } |
| 3480 | } |
| 3481 | return false; |
| 3482 | } |
| 3483 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3484 | bool Dbg::IsForcedInterpreterNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3485 | instrumentation::Instrumentation* const instrumentation = |
| 3486 | Runtime::Current()->GetInstrumentation(); |
| 3487 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3488 | if (instrumentation->InterpretOnly()) { |
| 3489 | return false; |
| 3490 | } |
| 3491 | // We can only interpret pure Java method. |
| 3492 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3493 | return false; |
| 3494 | } |
| 3495 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3496 | if (ssc != nullptr) { |
| 3497 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3498 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3499 | return true; |
| 3500 | } |
| 3501 | // If we are stepping out from a static initializer, by issuing a step |
| 3502 | // in or step over, that was implicitly invoked by calling a static method, |
| 3503 | // then we need to step into that method. Having a lower stack depth than |
| 3504 | // the one the single step control has indicates that the step originates |
| 3505 | // from the static initializer. |
| 3506 | if (ssc->GetStepDepth() != JDWP::SD_OUT && |
| 3507 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3508 | return true; |
| 3509 | } |
| 3510 | } |
| 3511 | // There are cases where we have to force interpreter on deoptimized methods, |
| 3512 | // because in some cases the call will not be performed by invoking an entry |
| 3513 | // point that has been replaced by the deoptimization, but instead by directly |
| 3514 | // invoking the compiled code of the method, for example. |
| 3515 | return instrumentation->IsDeoptimized(m); |
| 3516 | } |
| 3517 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3518 | bool Dbg::IsForcedInstrumentationNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3519 | // 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] | 3520 | if (m == nullptr) { |
| 3521 | return false; |
| 3522 | } |
| 3523 | instrumentation::Instrumentation* const instrumentation = |
| 3524 | Runtime::Current()->GetInstrumentation(); |
| 3525 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3526 | if (instrumentation->InterpretOnly()) { |
| 3527 | return false; |
| 3528 | } |
| 3529 | // We can only interpret pure Java method. |
| 3530 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3531 | return false; |
| 3532 | } |
| 3533 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3534 | if (ssc != nullptr) { |
| 3535 | // If we are stepping out from a static initializer, by issuing a step |
| 3536 | // out, that was implicitly invoked by calling a static method, then we |
| 3537 | // need to step into the caller of that method. Having a lower stack |
| 3538 | // depth than the one the single step control has indicates that the |
| 3539 | // step originates from the static initializer. |
| 3540 | if (ssc->GetStepDepth() == JDWP::SD_OUT && |
| 3541 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3542 | return true; |
| 3543 | } |
| 3544 | } |
| 3545 | // If we are returning from a static intializer, that was implicitly |
| 3546 | // invoked by calling a static method and the caller is deoptimized, |
| 3547 | // then we have to deoptimize the stack without forcing interpreter |
| 3548 | // on the static method that was called originally. This problem can |
| 3549 | // be solved easily by forcing instrumentation on the called method, |
| 3550 | // because the instrumentation exit hook will recognise the need of |
| 3551 | // stack deoptimization by calling IsForcedInterpreterNeededForUpcall. |
| 3552 | return instrumentation->IsDeoptimized(m); |
| 3553 | } |
| 3554 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3555 | bool Dbg::IsForcedInterpreterNeededForUpcallImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3556 | // 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] | 3557 | if (m == nullptr) { |
| 3558 | return false; |
| 3559 | } |
| 3560 | instrumentation::Instrumentation* const instrumentation = |
| 3561 | Runtime::Current()->GetInstrumentation(); |
| 3562 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3563 | if (instrumentation->InterpretOnly()) { |
| 3564 | return false; |
| 3565 | } |
| 3566 | // We can only interpret pure Java method. |
| 3567 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3568 | return false; |
| 3569 | } |
| 3570 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3571 | if (ssc != nullptr) { |
| 3572 | // The debugger is not interested in what is happening under the level |
| 3573 | // of the step, thus we only force interpreter when we are not below of |
| 3574 | // the step. |
| 3575 | if (ssc->GetStackDepth() >= GetStackDepth(thread)) { |
| 3576 | return true; |
| 3577 | } |
| 3578 | } |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 3579 | if (thread->HasDebuggerShadowFrames()) { |
| 3580 | // We need to deoptimize the stack for the exception handling flow so that |
| 3581 | // we don't miss any deoptimization that should be done when there are |
| 3582 | // debugger shadow frames. |
| 3583 | return true; |
| 3584 | } |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3585 | // We have to require stack deoptimization if the upcall is deoptimized. |
| 3586 | return instrumentation->IsDeoptimized(m); |
| 3587 | } |
| 3588 | |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 3589 | class NeedsDeoptimizationVisitor : public StackVisitor { |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 3590 | public: |
| 3591 | explicit NeedsDeoptimizationVisitor(Thread* self) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3592 | REQUIRES_SHARED(Locks::mutator_lock_) |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 3593 | : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 3594 | needs_deoptimization_(false) {} |
| 3595 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3596 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 3597 | // The visitor is meant to be used when handling exception from compiled code only. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3598 | CHECK(!IsShadowFrame()) << "We only expect to visit compiled frame: " |
| 3599 | << ArtMethod::PrettyMethod(GetMethod()); |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 3600 | ArtMethod* method = GetMethod(); |
| 3601 | if (method == nullptr) { |
| 3602 | // We reach an upcall and don't need to deoptimize this part of the stack (ManagedFragment) |
| 3603 | // so we can stop the visit. |
| 3604 | DCHECK(!needs_deoptimization_); |
| 3605 | return false; |
| 3606 | } |
| 3607 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
| 3608 | // We found a compiled frame in the stack but instrumentation is set to interpret |
| 3609 | // everything: we need to deoptimize. |
| 3610 | needs_deoptimization_ = true; |
| 3611 | return false; |
| 3612 | } |
| 3613 | if (Runtime::Current()->GetInstrumentation()->IsDeoptimized(method)) { |
| 3614 | // We found a deoptimized method in the stack. |
| 3615 | needs_deoptimization_ = true; |
| 3616 | return false; |
| 3617 | } |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 3618 | ShadowFrame* frame = GetThread()->FindDebuggerShadowFrame(GetFrameId()); |
| 3619 | if (frame != nullptr) { |
| 3620 | // The debugger allocated a ShadowFrame to update a variable in the stack: we need to |
| 3621 | // deoptimize the stack to execute (and deallocate) this frame. |
| 3622 | needs_deoptimization_ = true; |
| 3623 | return false; |
| 3624 | } |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 3625 | return true; |
| 3626 | } |
| 3627 | |
| 3628 | bool NeedsDeoptimization() const { |
| 3629 | return needs_deoptimization_; |
| 3630 | } |
| 3631 | |
| 3632 | private: |
| 3633 | // Do we need to deoptimize the stack? |
| 3634 | bool needs_deoptimization_; |
| 3635 | |
| 3636 | DISALLOW_COPY_AND_ASSIGN(NeedsDeoptimizationVisitor); |
| 3637 | }; |
| 3638 | |
| 3639 | // Do we need to deoptimize the stack to handle an exception? |
| 3640 | bool Dbg::IsForcedInterpreterNeededForExceptionImpl(Thread* thread) { |
| 3641 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3642 | if (ssc != nullptr) { |
| 3643 | // We deopt to step into the catch handler. |
| 3644 | return true; |
| 3645 | } |
| 3646 | // Deoptimization is required if at least one method in the stack needs it. However we |
| 3647 | // skip frames that will be unwound (thus not executed). |
| 3648 | NeedsDeoptimizationVisitor visitor(thread); |
| 3649 | visitor.WalkStack(true); // includes upcall. |
| 3650 | return visitor.NeedsDeoptimization(); |
| 3651 | } |
| 3652 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3653 | // Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't |
| 3654 | // cause suspension if the thread is the current thread. |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3655 | class ScopedDebuggerThreadSuspension { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3656 | public: |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3657 | ScopedDebuggerThreadSuspension(Thread* self, JDWP::ObjectId thread_id) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3658 | REQUIRES(!Locks::thread_list_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3659 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3660 | thread_(nullptr), |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3661 | error_(JDWP::ERR_NONE), |
| 3662 | self_suspend_(false), |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3663 | other_suspend_(false) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3664 | ScopedObjectAccessUnchecked soa(self); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 3665 | thread_ = DecodeThread(soa, thread_id, &error_); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3666 | if (error_ == JDWP::ERR_NONE) { |
| 3667 | if (thread_ == soa.Self()) { |
| 3668 | self_suspend_ = true; |
| 3669 | } else { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3670 | Thread* suspended_thread; |
| 3671 | { |
| 3672 | ScopedThreadSuspension sts(self, kWaitingForDebuggerSuspension); |
| 3673 | jobject thread_peer = Dbg::GetObjectRegistry()->GetJObject(thread_id); |
| 3674 | bool timed_out; |
| 3675 | ThreadList* const thread_list = Runtime::Current()->GetThreadList(); |
| 3676 | suspended_thread = thread_list->SuspendThreadByPeer(thread_peer, true, true, &timed_out); |
| 3677 | } |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3678 | if (suspended_thread == nullptr) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3679 | // Thread terminated from under us while suspending. |
| 3680 | error_ = JDWP::ERR_INVALID_THREAD; |
| 3681 | } else { |
| 3682 | CHECK_EQ(suspended_thread, thread_); |
| 3683 | other_suspend_ = true; |
| 3684 | } |
| 3685 | } |
| 3686 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3687 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3688 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3689 | Thread* GetThread() const { |
| 3690 | return thread_; |
| 3691 | } |
| 3692 | |
| 3693 | JDWP::JdwpError GetError() const { |
| 3694 | return error_; |
| 3695 | } |
| 3696 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3697 | ~ScopedDebuggerThreadSuspension() { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3698 | if (other_suspend_) { |
| 3699 | Runtime::Current()->GetThreadList()->Resume(thread_, true); |
| 3700 | } |
| 3701 | } |
| 3702 | |
| 3703 | private: |
| 3704 | Thread* thread_; |
| 3705 | JDWP::JdwpError error_; |
| 3706 | bool self_suspend_; |
| 3707 | bool other_suspend_; |
| 3708 | }; |
| 3709 | |
| 3710 | JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size, |
| 3711 | JDWP::JdwpStepDepth step_depth) { |
| 3712 | Thread* self = Thread::Current(); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3713 | ScopedDebuggerThreadSuspension sts(self, thread_id); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3714 | if (sts.GetError() != JDWP::ERR_NONE) { |
| 3715 | return sts.GetError(); |
| 3716 | } |
| 3717 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3718 | // 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] | 3719 | // is for step-out. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3720 | struct SingleStepStackVisitor : public StackVisitor { |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3721 | explicit SingleStepStackVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 3722 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 3723 | stack_depth(0), |
| 3724 | method(nullptr), |
| 3725 | line_number(-1) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 3726 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3727 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 3728 | // annotalysis. |
| 3729 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3730 | ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3731 | if (!m->IsRuntimeMethod()) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3732 | ++stack_depth; |
| 3733 | if (method == nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3734 | mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3735 | method = m; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3736 | if (dex_cache != nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 3737 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3738 | line_number = annotations::GetLineNumFromPC(dex_file, m, GetDexPc()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3739 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3740 | } |
| 3741 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 3742 | return true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3743 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3744 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3745 | int stack_depth; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3746 | ArtMethod* method; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3747 | int32_t line_number; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3748 | }; |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3749 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3750 | Thread* const thread = sts.GetThread(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3751 | SingleStepStackVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3752 | visitor.WalkStack(); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3753 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3754 | // 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] | 3755 | struct DebugCallbackContext { |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 3756 | DebugCallbackContext(SingleStepControl* single_step_control_cb, |
| 3757 | int32_t line_number_cb, const DexFile::CodeItem* code_item) |
| 3758 | : single_step_control_(single_step_control_cb), line_number_(line_number_cb), |
| 3759 | code_item_(code_item), last_pc_valid(false), last_pc(0) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3760 | } |
| 3761 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 3762 | static bool Callback(void* raw_context, const DexFile::PositionInfo& entry) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3763 | DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 3764 | if (static_cast<int32_t>(entry.line_) == context->line_number_) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3765 | if (!context->last_pc_valid) { |
| 3766 | // Everything from this address until the next line change is ours. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 3767 | context->last_pc = entry.address_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3768 | context->last_pc_valid = true; |
| 3769 | } |
| 3770 | // Otherwise, if we're already in a valid range for this line, |
| 3771 | // just keep going (shouldn't really happen)... |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3772 | } else if (context->last_pc_valid) { // and the line number is new |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3773 | // Add everything from the last entry up until here to the set |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 3774 | for (uint32_t dex_pc = context->last_pc; dex_pc < entry.address_; ++dex_pc) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3775 | context->single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3776 | } |
| 3777 | context->last_pc_valid = false; |
| 3778 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3779 | return false; // There may be multiple entries for any given line. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3780 | } |
| 3781 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3782 | ~DebugCallbackContext() { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3783 | // If the line number was the last in the position table... |
| 3784 | if (last_pc_valid) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3785 | size_t end = code_item_->insns_size_in_code_units_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3786 | for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3787 | single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3788 | } |
| 3789 | } |
| 3790 | } |
| 3791 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3792 | SingleStepControl* const single_step_control_; |
| 3793 | const int32_t line_number_; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3794 | const DexFile::CodeItem* const code_item_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3795 | bool last_pc_valid; |
| 3796 | uint32_t last_pc; |
| 3797 | }; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3798 | |
| 3799 | // Allocate single step. |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3800 | SingleStepControl* single_step_control = |
| 3801 | new (std::nothrow) SingleStepControl(step_size, step_depth, |
| 3802 | visitor.stack_depth, visitor.method); |
| 3803 | if (single_step_control == nullptr) { |
| 3804 | LOG(ERROR) << "Failed to allocate SingleStepControl"; |
| 3805 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3806 | } |
| 3807 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3808 | ArtMethod* m = single_step_control->GetMethod(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3809 | const int32_t line_number = visitor.line_number; |
Sebastien Hertz | 52f5f93 | 2015-05-28 11:00:57 +0200 | [diff] [blame] | 3810 | // Note: if the thread is not running Java code (pure native thread), there is no "current" |
| 3811 | // method on the stack (and no line number either). |
| 3812 | if (m != nullptr && !m->IsNative()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3813 | const DexFile::CodeItem* const code_item = m->GetCodeItem(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3814 | DebugCallbackContext context(single_step_control, line_number, code_item); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 3815 | m->GetDexFile()->DecodeDebugPositionInfo(code_item, DebugCallbackContext::Callback, &context); |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 3816 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3817 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3818 | // Activate single-step in the thread. |
| 3819 | thread->ActivateSingleStepControl(single_step_control); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3820 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3821 | if (VLOG_IS_ON(jdwp)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3822 | VLOG(jdwp) << "Single-step thread: " << *thread; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3823 | VLOG(jdwp) << "Single-step step size: " << single_step_control->GetStepSize(); |
| 3824 | VLOG(jdwp) << "Single-step step depth: " << single_step_control->GetStepDepth(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3825 | VLOG(jdwp) << "Single-step current method: " |
| 3826 | << ArtMethod::PrettyMethod(single_step_control->GetMethod()); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3827 | VLOG(jdwp) << "Single-step current line: " << line_number; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3828 | VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->GetStackDepth(); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3829 | VLOG(jdwp) << "Single-step dex_pc values:"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3830 | for (uint32_t dex_pc : single_step_control->GetDexPcs()) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3831 | VLOG(jdwp) << StringPrintf(" %#x", dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3832 | } |
| 3833 | } |
| 3834 | |
| 3835 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3836 | } |
| 3837 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3838 | void Dbg::UnconfigureStep(JDWP::ObjectId thread_id) { |
| 3839 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3840 | JDWP::JdwpError error; |
| 3841 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Sebastien Hertz | 87118ed | 2013-11-26 17:57:18 +0100 | [diff] [blame] | 3842 | if (error == JDWP::ERR_NONE) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3843 | thread->DeactivateSingleStepControl(); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3844 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3845 | } |
| 3846 | |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3847 | static char JdwpTagToShortyChar(JDWP::JdwpTag tag) { |
| 3848 | switch (tag) { |
| 3849 | default: |
| 3850 | LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag); |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 3851 | UNREACHABLE(); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3852 | |
| 3853 | // Primitives. |
| 3854 | case JDWP::JT_BYTE: return 'B'; |
| 3855 | case JDWP::JT_CHAR: return 'C'; |
| 3856 | case JDWP::JT_FLOAT: return 'F'; |
| 3857 | case JDWP::JT_DOUBLE: return 'D'; |
| 3858 | case JDWP::JT_INT: return 'I'; |
| 3859 | case JDWP::JT_LONG: return 'J'; |
| 3860 | case JDWP::JT_SHORT: return 'S'; |
| 3861 | case JDWP::JT_VOID: return 'V'; |
| 3862 | case JDWP::JT_BOOLEAN: return 'Z'; |
| 3863 | |
| 3864 | // Reference types. |
| 3865 | case JDWP::JT_ARRAY: |
| 3866 | case JDWP::JT_OBJECT: |
| 3867 | case JDWP::JT_STRING: |
| 3868 | case JDWP::JT_THREAD: |
| 3869 | case JDWP::JT_THREAD_GROUP: |
| 3870 | case JDWP::JT_CLASS_LOADER: |
| 3871 | case JDWP::JT_CLASS_OBJECT: |
| 3872 | return 'L'; |
| 3873 | } |
| 3874 | } |
| 3875 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3876 | JDWP::JdwpError Dbg::PrepareInvokeMethod(uint32_t request_id, JDWP::ObjectId thread_id, |
| 3877 | JDWP::ObjectId object_id, JDWP::RefTypeId class_id, |
| 3878 | JDWP::MethodId method_id, uint32_t arg_count, |
| 3879 | uint64_t arg_values[], JDWP::JdwpTag* arg_types, |
| 3880 | uint32_t options) { |
| 3881 | Thread* const self = Thread::Current(); |
| 3882 | CHECK_EQ(self, GetDebugThread()) << "This must be called by the JDWP thread"; |
Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame] | 3883 | const bool resume_all_threads = ((options & JDWP::INVOKE_SINGLE_THREADED) == 0); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3884 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3885 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3886 | Thread* targetThread = nullptr; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3887 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3888 | ScopedObjectAccessUnchecked soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3889 | JDWP::JdwpError error; |
| 3890 | targetThread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 3891 | if (error != JDWP::ERR_NONE) { |
| 3892 | LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id; |
| 3893 | return error; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3894 | } |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3895 | if (targetThread->GetInvokeReq() != nullptr) { |
| 3896 | // Thread is already invoking a method on behalf of the debugger. |
| 3897 | LOG(ERROR) << "InvokeMethod request for thread already invoking a method: " << *targetThread; |
| 3898 | return JDWP::ERR_ALREADY_INVOKING; |
| 3899 | } |
| 3900 | if (!targetThread->IsReadyForDebugInvoke()) { |
| 3901 | // 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] | 3902 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 3903 | return JDWP::ERR_INVALID_THREAD; |
| 3904 | } |
| 3905 | |
| 3906 | /* |
Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame] | 3907 | * According to the JDWP specs, we are expected to resume all threads (or only the |
| 3908 | * target thread) once. So if a thread has been suspended more than once (either by |
| 3909 | * the debugger for an event or by the runtime for GC), it will remain suspended before |
| 3910 | * the invoke is executed. This means the debugger is responsible to properly resume all |
| 3911 | * the threads it has suspended so the target thread can execute the method. |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3912 | * |
Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame] | 3913 | * However, for compatibility reason with older versions of debuggers (like Eclipse), we |
| 3914 | * fully resume all threads (by canceling *all* debugger suspensions) when the debugger |
| 3915 | * wants us to resume all threads. This is to avoid ending up in deadlock situation. |
| 3916 | * |
| 3917 | * On the other hand, if we are asked to only resume the target thread, then we follow the |
| 3918 | * JDWP specs by resuming that thread only once. This means the thread will remain suspended |
| 3919 | * if it has been suspended more than once before the invoke (and again, this is the |
| 3920 | * responsibility of the debugger to properly resume that thread before invoking a method). |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3921 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3922 | int suspend_count; |
| 3923 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3924 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3925 | suspend_count = targetThread->GetSuspendCount(); |
| 3926 | } |
Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame] | 3927 | if (suspend_count > 1 && resume_all_threads) { |
| 3928 | // The target thread will remain suspended even after we resume it. Let's emit a warning |
| 3929 | // to indicate the invoke won't be executed until the thread is resumed. |
| 3930 | LOG(WARNING) << *targetThread << " suspended more than once (suspend count == " |
| 3931 | << suspend_count << "). This thread will invoke the method only once " |
| 3932 | << "it is fully resumed."; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3933 | } |
| 3934 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3935 | mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 3936 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3937 | return JDWP::ERR_INVALID_OBJECT; |
| 3938 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3939 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3940 | gRegistry->Get<mirror::Object*>(thread_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3941 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3942 | return JDWP::ERR_INVALID_OBJECT; |
| 3943 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3944 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3945 | mirror::Class* c = DecodeClass(class_id, &error); |
| 3946 | if (c == nullptr) { |
| 3947 | return error; |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3948 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3949 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3950 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3951 | if (m->IsStatic() != (receiver == nullptr)) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3952 | return JDWP::ERR_INVALID_METHODID; |
| 3953 | } |
| 3954 | if (m->IsStatic()) { |
| 3955 | if (m->GetDeclaringClass() != c) { |
| 3956 | return JDWP::ERR_INVALID_METHODID; |
| 3957 | } |
| 3958 | } else { |
| 3959 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
| 3960 | return JDWP::ERR_INVALID_METHODID; |
| 3961 | } |
| 3962 | } |
| 3963 | |
| 3964 | // Check the argument list matches the method. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3965 | uint32_t shorty_len = 0; |
| 3966 | const char* shorty = m->GetShorty(&shorty_len); |
| 3967 | if (shorty_len - 1 != arg_count) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3968 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3969 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3970 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3971 | { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3972 | StackHandleScope<2> hs(soa.Self()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3973 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver)); |
| 3974 | HandleWrapper<mirror::Class> h_klass(hs.NewHandleWrapper(&c)); |
| 3975 | const DexFile::TypeList* types = m->GetParameterTypeList(); |
| 3976 | for (size_t i = 0; i < arg_count; ++i) { |
| 3977 | if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) { |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3978 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3979 | } |
| 3980 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3981 | if (shorty[i + 1] == 'L') { |
| 3982 | // Did we really get an argument of an appropriate reference type? |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 3983 | mirror::Class* parameter_type = |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 3984 | m->GetClassFromTypeIndex(types->GetTypeItem(i).type_idx_, |
| 3985 | true /* resolve */, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 3986 | kRuntimePointerSize); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3987 | mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i], &error); |
| 3988 | if (error != JDWP::ERR_NONE) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3989 | return JDWP::ERR_INVALID_OBJECT; |
| 3990 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3991 | if (argument != nullptr && !argument->InstanceOf(parameter_type)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3992 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3993 | } |
| 3994 | |
| 3995 | // Turn the on-the-wire ObjectId into a jobject. |
| 3996 | jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]); |
| 3997 | v.l = gRegistry->GetJObject(arg_values[i]); |
| 3998 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3999 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 4000 | } |
| 4001 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4002 | // Allocates a DebugInvokeReq. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4003 | DebugInvokeReq* req = new (std::nothrow) DebugInvokeReq(request_id, thread_id, receiver, c, m, |
| 4004 | options, arg_values, arg_count); |
| 4005 | if (req == nullptr) { |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4006 | LOG(ERROR) << "Failed to allocate DebugInvokeReq"; |
| 4007 | return JDWP::ERR_OUT_OF_MEMORY; |
| 4008 | } |
| 4009 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4010 | // Attaches the DebugInvokeReq to the target thread so it executes the method when |
| 4011 | // it is resumed. Once the invocation completes, the target thread will delete it before |
| 4012 | // suspending itself (see ThreadList::SuspendSelfForDebugger). |
| 4013 | targetThread->SetDebugInvokeReq(req); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | // The fact that we've released the thread list lock is a bit risky --- if the thread goes |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4017 | // away we're sitting high and dry -- but we must release this before the UndoDebuggerSuspensions |
| 4018 | // call. |
Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame] | 4019 | if (resume_all_threads) { |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4020 | VLOG(jdwp) << " Resuming all threads"; |
| 4021 | thread_list->UndoDebuggerSuspensions(); |
| 4022 | } else { |
| 4023 | VLOG(jdwp) << " Resuming event thread only"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4024 | thread_list->Resume(targetThread, true); |
| 4025 | } |
| 4026 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4027 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4031 | Thread* const self = Thread::Current(); |
| 4032 | CHECK_NE(self, GetDebugThread()) << "This must be called by the event thread"; |
| 4033 | |
| 4034 | ScopedObjectAccess soa(self); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4035 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 4036 | // We can be called while an exception is pending. We need |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4037 | // to preserve that across the method invocation. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4038 | StackHandleScope<1> hs(soa.Self()); |
| 4039 | Handle<mirror::Throwable> old_exception = hs.NewHandle(soa.Self()->GetException()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4040 | soa.Self()->ClearException(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4041 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4042 | // Execute the method then sends reply to the debugger. |
| 4043 | ExecuteMethodWithoutPendingException(soa, pReq); |
| 4044 | |
| 4045 | // If an exception was pending before the invoke, restore it now. |
| 4046 | if (old_exception.Get() != nullptr) { |
| 4047 | soa.Self()->SetException(old_exception.Get()); |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | // Helper function: write a variable-width value into the output input buffer. |
| 4052 | static void WriteValue(JDWP::ExpandBuf* pReply, int width, uint64_t value) { |
| 4053 | switch (width) { |
| 4054 | case 1: |
| 4055 | expandBufAdd1(pReply, value); |
| 4056 | break; |
| 4057 | case 2: |
| 4058 | expandBufAdd2BE(pReply, value); |
| 4059 | break; |
| 4060 | case 4: |
| 4061 | expandBufAdd4BE(pReply, value); |
| 4062 | break; |
| 4063 | case 8: |
| 4064 | expandBufAdd8BE(pReply, value); |
| 4065 | break; |
| 4066 | default: |
| 4067 | LOG(FATAL) << width; |
| 4068 | UNREACHABLE(); |
| 4069 | } |
| 4070 | } |
| 4071 | |
| 4072 | void Dbg::ExecuteMethodWithoutPendingException(ScopedObjectAccess& soa, DebugInvokeReq* pReq) { |
| 4073 | soa.Self()->AssertNoPendingException(); |
| 4074 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4075 | // Translate the method through the vtable, unless the debugger wants to suppress it. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4076 | ArtMethod* m = pReq->method; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 4077 | PointerSize image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4078 | if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver.Read() != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4079 | ArtMethod* actual_method = |
| 4080 | pReq->klass.Read()->FindVirtualMethodForVirtualOrInterface(m, image_pointer_size); |
| 4081 | if (actual_method != m) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4082 | VLOG(jdwp) << "ExecuteMethod translated " << ArtMethod::PrettyMethod(m) |
| 4083 | << " to " << ArtMethod::PrettyMethod(actual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4084 | m = actual_method; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 4085 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4086 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4087 | VLOG(jdwp) << "ExecuteMethod " << ArtMethod::PrettyMethod(m) |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4088 | << " receiver=" << pReq->receiver.Read() |
Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 4089 | << " arg_count=" << pReq->arg_count; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4090 | CHECK(m != nullptr); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4091 | |
Roland Levillain | 33d6903 | 2015-06-18 18:20:59 +0100 | [diff] [blame] | 4092 | static_assert(sizeof(jvalue) == sizeof(uint64_t), "jvalue and uint64_t have different sizes."); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4093 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4094 | // Invoke the method. |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 4095 | ScopedLocalRef<jobject> ref(soa.Env(), soa.AddLocalReference<jobject>(pReq->receiver.Read())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4096 | JValue result = InvokeWithJValues(soa, ref.get(), soa.EncodeMethod(m), |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4097 | reinterpret_cast<jvalue*>(pReq->arg_values.get())); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4098 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4099 | // Prepare JDWP ids for the reply. |
| 4100 | JDWP::JdwpTag result_tag = BasicTagFromDescriptor(m->GetShorty()); |
| 4101 | const bool is_object_result = (result_tag == JDWP::JT_OBJECT); |
Jeff Hao | 064d24e | 2016-08-25 03:52:40 +0000 | [diff] [blame] | 4102 | StackHandleScope<3> hs(soa.Self()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4103 | Handle<mirror::Object> object_result = hs.NewHandle(is_object_result ? result.GetL() : nullptr); |
| 4104 | Handle<mirror::Throwable> exception = hs.NewHandle(soa.Self()->GetException()); |
| 4105 | soa.Self()->ClearException(); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4106 | |
| 4107 | if (!IsDebuggerActive()) { |
| 4108 | // The debugger detached: we must not re-suspend threads. We also don't need to fill the reply |
| 4109 | // because it won't be sent either. |
| 4110 | return; |
| 4111 | } |
| 4112 | |
| 4113 | JDWP::ObjectId exceptionObjectId = gRegistry->Add(exception); |
| 4114 | uint64_t result_value = 0; |
| 4115 | if (exceptionObjectId != 0) { |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4116 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exception.Get() |
| 4117 | << " " << exception->Dump(); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4118 | result_value = 0; |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4119 | } else if (is_object_result) { |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4120 | /* if no exception was thrown, examine object result more closely */ |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4121 | JDWP::JdwpTag new_tag = TagFromObject(soa, object_result.Get()); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4122 | if (new_tag != result_tag) { |
| 4123 | VLOG(jdwp) << " JDWP promoted result from " << result_tag << " to " << new_tag; |
| 4124 | result_tag = new_tag; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4125 | } |
| 4126 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4127 | // Register the object in the registry and reference its ObjectId. This ensures |
| 4128 | // GC safety and prevents from accessing stale reference if the object is moved. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4129 | result_value = gRegistry->Add(object_result.Get()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4130 | } else { |
| 4131 | // Primitive result. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4132 | DCHECK(IsPrimitiveTag(result_tag)); |
| 4133 | result_value = result.GetJ(); |
| 4134 | } |
| 4135 | const bool is_constructor = m->IsConstructor() && !m->IsStatic(); |
| 4136 | if (is_constructor) { |
| 4137 | // If we invoked a constructor (which actually returns void), return the receiver, |
| 4138 | // unless we threw, in which case we return null. |
Sebastien Hertz | a3e1377 | 2015-11-05 12:09:44 +0100 | [diff] [blame] | 4139 | DCHECK_EQ(JDWP::JT_VOID, result_tag); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4140 | if (exceptionObjectId == 0) { |
Jeff Hao | 064d24e | 2016-08-25 03:52:40 +0000 | [diff] [blame] | 4141 | if (m->GetDeclaringClass()->IsStringClass()) { |
| 4142 | // For string constructors, the new string is remapped to the receiver (stored in ref). |
| 4143 | Handle<mirror::Object> decoded_ref = hs.NewHandle(soa.Self()->DecodeJObject(ref.get())); |
| 4144 | result_value = gRegistry->Add(decoded_ref); |
| 4145 | result_tag = TagFromObject(soa, decoded_ref.Get()); |
| 4146 | } else { |
| 4147 | // TODO we could keep the receiver ObjectId in the DebugInvokeReq to avoid looking into the |
| 4148 | // object registry. |
| 4149 | result_value = GetObjectRegistry()->Add(pReq->receiver.Read()); |
| 4150 | result_tag = TagFromObject(soa, pReq->receiver.Read()); |
| 4151 | } |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4152 | } else { |
| 4153 | result_value = 0; |
Sebastien Hertz | a3e1377 | 2015-11-05 12:09:44 +0100 | [diff] [blame] | 4154 | result_tag = JDWP::JT_OBJECT; |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4155 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4156 | } |
| 4157 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4158 | // Suspend other threads if the invoke is not single-threaded. |
| 4159 | if ((pReq->options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 4160 | ScopedThreadSuspension sts(soa.Self(), kWaitingForDebuggerSuspension); |
Hiroshi Yamauchi | 8f95cf3 | 2016-04-19 11:14:06 -0700 | [diff] [blame] | 4161 | // Avoid a deadlock between GC and debugger where GC gets suspended during GC. b/25800335. |
| 4162 | gc::ScopedGCCriticalSection gcs(soa.Self(), gc::kGcCauseDebugger, gc::kCollectorTypeDebugger); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4163 | VLOG(jdwp) << " Suspending all threads"; |
| 4164 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4165 | } |
| 4166 | |
| 4167 | VLOG(jdwp) << " --> returned " << result_tag |
| 4168 | << StringPrintf(" %#" PRIx64 " (except=%#" PRIx64 ")", result_value, |
| 4169 | exceptionObjectId); |
| 4170 | |
| 4171 | // Show detailed debug output. |
| 4172 | if (result_tag == JDWP::JT_STRING && exceptionObjectId == 0) { |
| 4173 | if (result_value != 0) { |
| 4174 | if (VLOG_IS_ON(jdwp)) { |
| 4175 | std::string result_string; |
| 4176 | JDWP::JdwpError error = Dbg::StringToUtf8(result_value, &result_string); |
| 4177 | CHECK_EQ(error, JDWP::ERR_NONE); |
| 4178 | VLOG(jdwp) << " string '" << result_string << "'"; |
| 4179 | } |
| 4180 | } else { |
| 4181 | VLOG(jdwp) << " string (null)"; |
| 4182 | } |
| 4183 | } |
| 4184 | |
| 4185 | // Attach the reply to DebugInvokeReq so it can be sent to the debugger when the event thread |
| 4186 | // is ready to suspend. |
| 4187 | BuildInvokeReply(pReq->reply, pReq->request_id, result_tag, result_value, exceptionObjectId); |
| 4188 | } |
| 4189 | |
| 4190 | void Dbg::BuildInvokeReply(JDWP::ExpandBuf* pReply, uint32_t request_id, JDWP::JdwpTag result_tag, |
| 4191 | uint64_t result_value, JDWP::ObjectId exception) { |
| 4192 | // Make room for the JDWP header since we do not know the size of the reply yet. |
| 4193 | JDWP::expandBufAddSpace(pReply, kJDWPHeaderLen); |
| 4194 | |
| 4195 | size_t width = GetTagWidth(result_tag); |
| 4196 | JDWP::expandBufAdd1(pReply, result_tag); |
| 4197 | if (width != 0) { |
| 4198 | WriteValue(pReply, width, result_value); |
| 4199 | } |
| 4200 | JDWP::expandBufAdd1(pReply, JDWP::JT_OBJECT); |
| 4201 | JDWP::expandBufAddObjectId(pReply, exception); |
| 4202 | |
| 4203 | // Now we know the size, we can complete the JDWP header. |
| 4204 | uint8_t* buf = expandBufGetBuffer(pReply); |
| 4205 | JDWP::Set4BE(buf + kJDWPHeaderSizeOffset, expandBufGetLength(pReply)); |
| 4206 | JDWP::Set4BE(buf + kJDWPHeaderIdOffset, request_id); |
| 4207 | JDWP::Set1(buf + kJDWPHeaderFlagsOffset, kJDWPFlagReply); // flags |
| 4208 | JDWP::Set2BE(buf + kJDWPHeaderErrorCodeOffset, JDWP::ERR_NONE); |
| 4209 | } |
| 4210 | |
| 4211 | void Dbg::FinishInvokeMethod(DebugInvokeReq* pReq) { |
| 4212 | CHECK_NE(Thread::Current(), GetDebugThread()) << "This must be called by the event thread"; |
| 4213 | |
| 4214 | JDWP::ExpandBuf* const pReply = pReq->reply; |
| 4215 | CHECK(pReply != nullptr) << "No reply attached to DebugInvokeReq"; |
| 4216 | |
| 4217 | // We need to prevent other threads (including JDWP thread) from interacting with the debugger |
| 4218 | // while we send the reply but are not yet suspended. The JDWP token will be released just before |
| 4219 | // we suspend ourself again (see ThreadList::SuspendSelfForDebugger). |
| 4220 | gJdwpState->AcquireJdwpTokenForEvent(pReq->thread_id); |
| 4221 | |
| 4222 | // Send the reply unless the debugger detached before the completion of the method. |
| 4223 | if (IsDebuggerActive()) { |
| 4224 | const size_t replyDataLength = expandBufGetLength(pReply) - kJDWPHeaderLen; |
| 4225 | VLOG(jdwp) << StringPrintf("REPLY INVOKE id=0x%06x (length=%zu)", |
| 4226 | pReq->request_id, replyDataLength); |
| 4227 | |
| 4228 | gJdwpState->SendRequest(pReply); |
| 4229 | } else { |
| 4230 | VLOG(jdwp) << "Not sending invoke reply because debugger detached"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4231 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4232 | } |
| 4233 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4234 | /* |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4235 | * "request" contains a full JDWP packet, possibly with multiple chunks. We |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4236 | * need to process each, accumulate the replies, and ship the whole thing |
| 4237 | * back. |
| 4238 | * |
| 4239 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 4240 | * and includes the chunk type/length, followed by the data. |
| 4241 | * |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 4242 | * 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] | 4243 | * chunk. If this becomes inconvenient we will need to adapt. |
| 4244 | */ |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4245 | bool Dbg::DdmHandlePacket(JDWP::Request* request, uint8_t** pReplyBuf, int* pReplyLen) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4246 | Thread* self = Thread::Current(); |
| 4247 | JNIEnv* env = self->GetJniEnv(); |
| 4248 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4249 | uint32_t type = request->ReadUnsigned32("type"); |
| 4250 | uint32_t length = request->ReadUnsigned32("length"); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4251 | |
| 4252 | // Create a byte[] corresponding to 'request'. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4253 | size_t request_length = request->size(); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4254 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4255 | if (dataArray.get() == nullptr) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4256 | LOG(WARNING) << "byte[] allocation failed: " << request_length; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4257 | env->ExceptionClear(); |
| 4258 | return false; |
| 4259 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4260 | env->SetByteArrayRegion(dataArray.get(), 0, request_length, |
| 4261 | reinterpret_cast<const jbyte*>(request->data())); |
| 4262 | request->Skip(request_length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4263 | |
| 4264 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4265 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4266 | if (length != request_length) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4267 | 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] | 4268 | return false; |
| 4269 | } |
| 4270 | |
| 4271 | // 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] | 4272 | ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4273 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch, |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4274 | type, dataArray.get(), 0, length)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4275 | if (env->ExceptionCheck()) { |
| 4276 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 4277 | env->ExceptionDescribe(); |
| 4278 | env->ExceptionClear(); |
| 4279 | return false; |
| 4280 | } |
| 4281 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4282 | if (chunk.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4283 | return false; |
| 4284 | } |
| 4285 | |
| 4286 | /* |
| 4287 | * Pull the pieces out of the chunk. We copy the results into a |
| 4288 | * newly-allocated buffer that the caller can free. We don't want to |
| 4289 | * continue using the Chunk object because nothing has a reference to it. |
| 4290 | * |
| 4291 | * We could avoid this by returning type/data/offset/length and having |
| 4292 | * the caller be aware of the object lifetime issues, but that |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 4293 | * 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] | 4294 | * if we have responses for multiple chunks. |
| 4295 | * |
| 4296 | * So we're pretty much stuck with copying data around multiple times. |
| 4297 | */ |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4298 | 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] | 4299 | 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] | 4300 | 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] | 4301 | 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] | 4302 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4303 | 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] | 4304 | if (length == 0 || replyData.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4305 | return false; |
| 4306 | } |
| 4307 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4308 | const int kChunkHdrLen = 8; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4309 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4310 | if (reply == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4311 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 4312 | return false; |
| 4313 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 4314 | JDWP::Set4BE(reply + 0, type); |
| 4315 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4316 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4317 | |
| 4318 | *pReplyBuf = reply; |
| 4319 | *pReplyLen = length + kChunkHdrLen; |
| 4320 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4321 | 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] | 4322 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4323 | } |
| 4324 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4325 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4326 | VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4327 | |
| 4328 | Thread* self = Thread::Current(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4329 | if (self->GetState() != kRunnable) { |
| 4330 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 4331 | /* try anyway? */ |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4335 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4336 | env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4337 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast, |
| 4338 | event); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4339 | if (env->ExceptionCheck()) { |
| 4340 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 4341 | env->ExceptionDescribe(); |
| 4342 | env->ExceptionClear(); |
| 4343 | } |
| 4344 | } |
| 4345 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4346 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4347 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4351 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4352 | gDdmThreadNotification = false; |
| 4353 | } |
| 4354 | |
| 4355 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4356 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4357 | * |
| 4358 | * Because we broadcast the full set of threads when the notifications are |
| 4359 | * first enabled, it's possible for "thread" to be actively executing. |
| 4360 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4361 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4362 | if (!gDdmThreadNotification) { |
| 4363 | return; |
| 4364 | } |
| 4365 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4366 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4367 | uint8_t buf[4]; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4368 | JDWP::Set4BE(&buf[0], t->GetThreadId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4369 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4370 | } else { |
| 4371 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4372 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4373 | StackHandleScope<1> hs(soa.Self()); |
| 4374 | Handle<mirror::String> name(hs.NewHandle(t->GetThreadName(soa))); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4375 | size_t char_count = (name.Get() != nullptr) ? name->GetLength() : 0; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 4376 | const jchar* chars = (name.Get() != nullptr) ? name->GetValue() : nullptr; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 4377 | bool is_compressed = (name.Get() != nullptr) ? name->IsCompressed() : false; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4378 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4379 | std::vector<uint8_t> bytes; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4380 | JDWP::Append4BE(bytes, t->GetThreadId()); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 4381 | if (is_compressed) { |
| 4382 | const uint8_t* chars_compressed = name->GetValueCompressed(); |
| 4383 | JDWP::AppendUtf16CompressedBE(bytes, chars_compressed, char_count); |
| 4384 | } else { |
| 4385 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
| 4386 | } |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4387 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 4388 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4389 | } |
| 4390 | } |
| 4391 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4392 | void Dbg::DdmSetThreadNotification(bool enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4393 | // Enable/disable thread notifications. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4394 | gDdmThreadNotification = enable; |
| 4395 | if (enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4396 | // Suspend the VM then post thread start notifications for all threads. Threads attaching will |
| 4397 | // see a suspension in progress and block until that ends. They then post their own start |
| 4398 | // notification. |
| 4399 | SuspendVM(); |
| 4400 | std::list<Thread*> threads; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4401 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4402 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4403 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4404 | threads = Runtime::Current()->GetThreadList()->GetList(); |
| 4405 | } |
| 4406 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4407 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4408 | for (Thread* thread : threads) { |
| 4409 | Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR")); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4410 | } |
| 4411 | } |
| 4412 | ResumeVM(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4413 | } |
| 4414 | } |
| 4415 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4416 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 4417 | if (IsDebuggerActive()) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 4418 | gJdwpState->PostThreadChange(t, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4419 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4420 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4421 | } |
| 4422 | |
| 4423 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4424 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4428 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4429 | } |
| 4430 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4431 | 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] | 4432 | CHECK(buf != nullptr); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4433 | iovec vec[1]; |
| 4434 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 4435 | vec[0].iov_len = byte_count; |
| 4436 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4437 | } |
| 4438 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4439 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 4440 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 4441 | } |
| 4442 | |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 4443 | void Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4444 | if (gJdwpState == nullptr) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4445 | VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4446 | } else { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 4447 | gJdwpState->DdmSendChunkV(type, iov, iov_count); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4448 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4449 | } |
| 4450 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 4451 | JDWP::JdwpState* Dbg::GetJdwpState() { |
| 4452 | return gJdwpState; |
| 4453 | } |
| 4454 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4455 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 4456 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4457 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4458 | return true; |
| 4459 | } |
| 4460 | |
| 4461 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 4462 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 4463 | return false; |
| 4464 | } |
| 4465 | |
| 4466 | gDdmHpifWhen = when; |
| 4467 | return true; |
| 4468 | } |
| 4469 | |
| 4470 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 4471 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 4472 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 4473 | return false; |
| 4474 | } |
| 4475 | |
| 4476 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 4477 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 4478 | return false; |
| 4479 | } |
| 4480 | |
| 4481 | if (native) { |
| 4482 | gDdmNhsgWhen = when; |
| 4483 | gDdmNhsgWhat = what; |
| 4484 | } else { |
| 4485 | gDdmHpsgWhen = when; |
| 4486 | gDdmHpsgWhat = what; |
| 4487 | } |
| 4488 | return true; |
| 4489 | } |
| 4490 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4491 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 4492 | // If there's a one-shot 'when', reset it. |
| 4493 | if (reason == gDdmHpifWhen) { |
| 4494 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 4495 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 4496 | } |
| 4497 | } |
| 4498 | |
| 4499 | /* |
| 4500 | * Chunk HPIF (client --> server) |
| 4501 | * |
| 4502 | * Heap Info. General information about the heap, |
| 4503 | * suitable for a summary display. |
| 4504 | * |
| 4505 | * [u4]: number of heaps |
| 4506 | * |
| 4507 | * For each heap: |
| 4508 | * [u4]: heap ID |
| 4509 | * [u8]: timestamp in ms since Unix epoch |
| 4510 | * [u1]: capture reason (same as 'when' value from server) |
| 4511 | * [u4]: max heap size in bytes (-Xmx) |
| 4512 | * [u4]: current heap size in bytes |
| 4513 | * [u4]: current number of bytes allocated |
| 4514 | * [u4]: current number of objects allocated |
| 4515 | */ |
| 4516 | uint8_t heap_count = 1; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4517 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4518 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4519 | JDWP::Append4BE(bytes, heap_count); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4520 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4521 | JDWP::Append8BE(bytes, MilliTime()); |
| 4522 | JDWP::Append1BE(bytes, reason); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4523 | JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes. |
| 4524 | JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4525 | JDWP::Append4BE(bytes, heap->GetBytesAllocated()); |
| 4526 | JDWP::Append4BE(bytes, heap->GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4527 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 4528 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4529 | } |
| 4530 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4531 | enum HpsgSolidity { |
| 4532 | SOLIDITY_FREE = 0, |
| 4533 | SOLIDITY_HARD = 1, |
| 4534 | SOLIDITY_SOFT = 2, |
| 4535 | SOLIDITY_WEAK = 3, |
| 4536 | SOLIDITY_PHANTOM = 4, |
| 4537 | SOLIDITY_FINALIZABLE = 5, |
| 4538 | SOLIDITY_SWEEP = 6, |
| 4539 | }; |
| 4540 | |
| 4541 | enum HpsgKind { |
| 4542 | KIND_OBJECT = 0, |
| 4543 | KIND_CLASS_OBJECT = 1, |
| 4544 | KIND_ARRAY_1 = 2, |
| 4545 | KIND_ARRAY_2 = 3, |
| 4546 | KIND_ARRAY_4 = 4, |
| 4547 | KIND_ARRAY_8 = 5, |
| 4548 | KIND_UNKNOWN = 6, |
| 4549 | KIND_NATIVE = 7, |
| 4550 | }; |
| 4551 | |
| 4552 | #define HPSG_PARTIAL (1<<7) |
| 4553 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 4554 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4555 | class HeapChunkContext { |
| 4556 | public: |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4557 | // Maximum chunk size. Obtain this from the formula: |
| 4558 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 4559 | HeapChunkContext(bool merge, bool native) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4560 | : buf_(16384 - 16), |
| 4561 | type_(0), |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4562 | chunk_overhead_(0) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4563 | Reset(); |
| 4564 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4565 | type_ = CHUNK_TYPE("NHSG"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4566 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4567 | type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4568 | } |
| 4569 | } |
| 4570 | |
| 4571 | ~HeapChunkContext() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4572 | if (p_ > &buf_[0]) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4573 | Flush(); |
| 4574 | } |
| 4575 | } |
| 4576 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4577 | void SetChunkOverhead(size_t chunk_overhead) { |
| 4578 | chunk_overhead_ = chunk_overhead; |
| 4579 | } |
| 4580 | |
| 4581 | void ResetStartOfNextChunk() { |
| 4582 | startOfNextMemoryChunk_ = nullptr; |
| 4583 | } |
| 4584 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4585 | void EnsureHeader(const void* chunk_ptr) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4586 | if (!needHeader_) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4587 | return; |
| 4588 | } |
| 4589 | |
| 4590 | // Start a new HPSx chunk. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4591 | JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap). |
| 4592 | JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4593 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4594 | JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 4595 | 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] | 4596 | // [u4]: length of piece, in allocation units |
| 4597 | // 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] | 4598 | pieceLenField_ = p_; |
| 4599 | JDWP::Write4BE(&p_, 0x55555555); |
| 4600 | needHeader_ = false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4601 | } |
| 4602 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4603 | void Flush() REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4604 | if (pieceLenField_ == nullptr) { |
Ian Rogers | d636b06 | 2013-01-18 17:51:18 -0800 | [diff] [blame] | 4605 | // Flush immediately post Reset (maybe back-to-back Flush). Ignore. |
| 4606 | CHECK(needHeader_); |
| 4607 | return; |
| 4608 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4609 | // Patch the "length of piece" field. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4610 | CHECK_LE(&buf_[0], pieceLenField_); |
| 4611 | CHECK_LE(pieceLenField_, p_); |
| 4612 | JDWP::Set4BE(pieceLenField_, totalAllocationUnits_); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4613 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4614 | Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4615 | Reset(); |
| 4616 | } |
| 4617 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4618 | static void HeapChunkJavaCallback(void* start, void* end, size_t used_bytes, void* arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4619 | REQUIRES_SHARED(Locks::heap_bitmap_lock_, |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4620 | Locks::mutator_lock_) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4621 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkJavaCallback(start, end, used_bytes); |
| 4622 | } |
| 4623 | |
| 4624 | static void HeapChunkNativeCallback(void* start, void* end, size_t used_bytes, void* arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4625 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4626 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkNativeCallback(start, end, used_bytes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4627 | } |
| 4628 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4629 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4630 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 4631 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4632 | void Reset() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4633 | p_ = &buf_[0]; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4634 | ResetStartOfNextChunk(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4635 | totalAllocationUnits_ = 0; |
| 4636 | needHeader_ = true; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4637 | pieceLenField_ = nullptr; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4638 | } |
| 4639 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4640 | bool IsNative() const { |
| 4641 | return type_ == CHUNK_TYPE("NHSG"); |
| 4642 | } |
| 4643 | |
| 4644 | // Returns true if the object is not an empty chunk. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4645 | bool ProcessRecord(void* start, size_t used_bytes) REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4646 | // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken |
| 4647 | // 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] | 4648 | if (used_bytes == 0) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4649 | if (start == nullptr) { |
| 4650 | // Reset for start of new heap. |
| 4651 | startOfNextMemoryChunk_ = nullptr; |
| 4652 | Flush(); |
| 4653 | } |
| 4654 | // Only process in use memory so that free region information |
| 4655 | // also includes dlmalloc book keeping. |
| 4656 | return false; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4657 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4658 | if (startOfNextMemoryChunk_ != nullptr) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4659 | // Transmit any pending free memory. Native free memory of over kMaxFreeLen could be because |
| 4660 | // of the use of mmaps, so don't report. If not free memory then start a new segment. |
| 4661 | bool flush = true; |
| 4662 | if (start > startOfNextMemoryChunk_) { |
| 4663 | const size_t kMaxFreeLen = 2 * kPageSize; |
| 4664 | void* free_start = startOfNextMemoryChunk_; |
| 4665 | void* free_end = start; |
| 4666 | const size_t free_len = |
| 4667 | reinterpret_cast<uintptr_t>(free_end) - reinterpret_cast<uintptr_t>(free_start); |
| 4668 | if (!IsNative() || free_len < kMaxFreeLen) { |
| 4669 | AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), free_start, free_len, IsNative()); |
| 4670 | flush = false; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4671 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4672 | } |
| 4673 | if (flush) { |
| 4674 | startOfNextMemoryChunk_ = nullptr; |
| 4675 | Flush(); |
| 4676 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4677 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4678 | return true; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4679 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4680 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4681 | void HeapChunkNativeCallback(void* start, void* /*end*/, size_t used_bytes) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4682 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4683 | if (ProcessRecord(start, used_bytes)) { |
| 4684 | uint8_t state = ExamineNativeObject(start); |
| 4685 | AppendChunk(state, start, used_bytes + chunk_overhead_, true /*is_native*/); |
| 4686 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4687 | } |
| 4688 | } |
| 4689 | |
| 4690 | void HeapChunkJavaCallback(void* start, void* /*end*/, size_t used_bytes) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4691 | REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4692 | if (ProcessRecord(start, used_bytes)) { |
| 4693 | // Determine the type of this chunk. |
| 4694 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 4695 | // If it's the same, we should combine them. |
| 4696 | uint8_t state = ExamineJavaObject(reinterpret_cast<mirror::Object*>(start)); |
| 4697 | AppendChunk(state, start, used_bytes + chunk_overhead_, false /*is_native*/); |
| 4698 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4699 | } |
| 4700 | } |
| 4701 | |
| 4702 | void AppendChunk(uint8_t state, void* ptr, size_t length, bool is_native) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4703 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4704 | // Make sure there's enough room left in the buffer. |
| 4705 | // We need to use two bytes for every fractional 256 allocation units used by the chunk plus |
| 4706 | // 17 bytes for any header. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4707 | const size_t needed = ((RoundUp(length / ALLOCATION_UNIT_SIZE, 256) / 256) * 2) + 17; |
| 4708 | size_t byte_left = &buf_.back() - p_; |
| 4709 | if (byte_left < needed) { |
| 4710 | if (is_native) { |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4711 | // Cannot trigger memory allocation while walking native heap. |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4712 | return; |
| 4713 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4714 | Flush(); |
| 4715 | } |
| 4716 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4717 | byte_left = &buf_.back() - p_; |
| 4718 | if (byte_left < needed) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4719 | LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", " |
| 4720 | << needed << " bytes)"; |
| 4721 | return; |
| 4722 | } |
| 4723 | EnsureHeader(ptr); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4724 | // Write out the chunk description. |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4725 | length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units. |
| 4726 | totalAllocationUnits_ += length; |
| 4727 | while (length > 256) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4728 | *p_++ = state | HPSG_PARTIAL; |
| 4729 | *p_++ = 255; // length - 1 |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4730 | length -= 256; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4731 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4732 | *p_++ = state; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4733 | *p_++ = length - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4734 | } |
| 4735 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4736 | uint8_t ExamineNativeObject(const void* p) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4737 | return p == nullptr ? HPSG_STATE(SOLIDITY_FREE, 0) : HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4738 | } |
| 4739 | |
| 4740 | uint8_t ExamineJavaObject(mirror::Object* o) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4741 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4742 | if (o == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4743 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 4744 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4745 | // It's an allocated chunk. Figure out what it is. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4746 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 4747 | if (!heap->IsLiveObjectLocked(o)) { |
| 4748 | LOG(ERROR) << "Invalid object in managed heap: " << o; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4749 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4750 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4751 | mirror::Class* c = o->GetClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4752 | if (c == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4753 | // The object was probably just created but hasn't been initialized yet. |
| 4754 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4755 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4756 | if (!heap->IsValidObjectAddress(c)) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4757 | LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4758 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4759 | } |
Mathieu Chartier | f26e1b3 | 2015-01-29 10:47:10 -0800 | [diff] [blame] | 4760 | if (c->GetClass() == nullptr) { |
| 4761 | LOG(ERROR) << "Null class of class " << c << " for object " << o; |
| 4762 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4763 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4764 | if (c->IsClassClass()) { |
| 4765 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 4766 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4767 | if (c->IsArrayClass()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4768 | switch (c->GetComponentSize()) { |
| 4769 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 4770 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 4771 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 4772 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 4773 | } |
| 4774 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4775 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4776 | } |
| 4777 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4778 | std::vector<uint8_t> buf_; |
| 4779 | uint8_t* p_; |
| 4780 | uint8_t* pieceLenField_; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4781 | void* startOfNextMemoryChunk_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4782 | size_t totalAllocationUnits_; |
| 4783 | uint32_t type_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4784 | bool needHeader_; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4785 | size_t chunk_overhead_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4786 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4787 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 4788 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4789 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4790 | static void BumpPointerSpaceCallback(mirror::Object* obj, void* arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4791 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4792 | const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4793 | HeapChunkContext::HeapChunkJavaCallback( |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4794 | obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, arg); |
| 4795 | } |
| 4796 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4797 | void Dbg::DdmSendHeapSegments(bool native) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4798 | Dbg::HpsgWhen when = native ? gDdmNhsgWhen : gDdmHpsgWhen; |
| 4799 | Dbg::HpsgWhat what = native ? gDdmNhsgWhat : gDdmHpsgWhat; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4800 | if (when == HPSG_WHEN_NEVER) { |
| 4801 | return; |
| 4802 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4803 | // Figure out what kind of chunks we'll be sending. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4804 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) |
| 4805 | << static_cast<int>(what); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4806 | |
| 4807 | // First, send a heap start chunk. |
| 4808 | uint8_t heap_id[4]; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4809 | 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] | 4810 | 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] | 4811 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4812 | Locks::mutator_lock_->AssertSharedHeld(self); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4813 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4814 | // Send a series of heap segment chunks. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4815 | HeapChunkContext context(what == HPSG_WHAT_MERGED_OBJECTS, native); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4816 | if (native) { |
Dimitry Ivanov | e6465bc | 2015-12-14 18:55:02 -0800 | [diff] [blame] | 4817 | UNIMPLEMENTED(WARNING) << "Native heap inspection is not supported"; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4818 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4819 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4820 | for (const auto& space : heap->GetContinuousSpaces()) { |
| 4821 | if (space->IsDlMallocSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4822 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4823 | // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an |
| 4824 | // allocation then the first sizeof(size_t) may belong to it. |
| 4825 | context.SetChunkOverhead(sizeof(size_t)); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4826 | space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4827 | } else if (space->IsRosAllocSpace()) { |
| 4828 | context.SetChunkOverhead(0); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4829 | // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since |
| 4830 | // RosAlloc's internal logic doesn't know to release and reacquire the heap bitmap lock. |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 4831 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 4832 | ScopedSuspendAll ssa(__FUNCTION__); |
| 4833 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 4834 | space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4835 | } else if (space->IsBumpPointerSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4836 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4837 | context.SetChunkOverhead(0); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4838 | space->AsBumpPointerSpace()->Walk(BumpPointerSpaceCallback, &context); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4839 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4840 | } else if (space->IsRegionSpace()) { |
| 4841 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 4842 | { |
| 4843 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 4844 | ScopedSuspendAll ssa(__FUNCTION__); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 4845 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 4846 | context.SetChunkOverhead(0); |
| 4847 | space->AsRegionSpace()->Walk(BumpPointerSpaceCallback, &context); |
| 4848 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 4849 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4850 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4851 | } else { |
| 4852 | UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4853 | } |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4854 | context.ResetStartOfNextChunk(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4855 | } |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4856 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 4857 | // Walk the large objects, these are not in the AllocSpace. |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4858 | context.SetChunkOverhead(0); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4859 | heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4860 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4861 | |
| 4862 | // Finally, send a heap end chunk. |
| 4863 | 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] | 4864 | } |
| 4865 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4866 | void Dbg::SetAllocTrackingEnabled(bool enable) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4867 | gc::AllocRecordObjectMap::SetAllocTrackingEnabled(enable); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4868 | } |
| 4869 | |
| 4870 | void Dbg::DumpRecentAllocations() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4871 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4872 | MutexLock mu(soa.Self(), *Locks::alloc_tracker_lock_); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4873 | if (!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4874 | LOG(INFO) << "Not recording tracked allocations"; |
| 4875 | return; |
| 4876 | } |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4877 | gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords(); |
| 4878 | CHECK(records != nullptr); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4879 | |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 4880 | const uint16_t capped_count = CappedAllocRecordCount(records->GetRecentAllocationSize()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4881 | uint16_t count = capped_count; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4882 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4883 | LOG(INFO) << "Tracked allocations, (count=" << count << ")"; |
| 4884 | for (auto it = records->RBegin(), end = records->REnd(); |
| 4885 | count > 0 && it != end; count--, it++) { |
Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 4886 | const gc::AllocRecord* record = &it->second; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4887 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4888 | LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->GetTid(), record->ByteCount()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4889 | << mirror::Class::PrettyClass(record->GetClass()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4890 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4891 | for (size_t stack_frame = 0, depth = record->GetDepth(); stack_frame < depth; ++stack_frame) { |
| 4892 | const gc::AllocRecordStackTraceElement& stack_element = record->StackElement(stack_frame); |
| 4893 | ArtMethod* m = stack_element.GetMethod(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4894 | LOG(INFO) << " " << ArtMethod::PrettyMethod(m) << " line " |
| 4895 | << stack_element.ComputeLineNumber(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4896 | } |
| 4897 | |
| 4898 | // pause periodically to help logcat catch up |
| 4899 | if ((count % 5) == 0) { |
| 4900 | usleep(40000); |
| 4901 | } |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4902 | } |
| 4903 | } |
| 4904 | |
| 4905 | class StringTable { |
| 4906 | public: |
| 4907 | StringTable() { |
| 4908 | } |
| 4909 | |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 4910 | void Add(const std::string& str) { |
| 4911 | table_.insert(str); |
| 4912 | } |
| 4913 | |
| 4914 | void Add(const char* str) { |
| 4915 | table_.insert(str); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4916 | } |
| 4917 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4918 | size_t IndexOf(const char* s) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4919 | auto it = table_.find(s); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4920 | if (it == table_.end()) { |
| 4921 | LOG(FATAL) << "IndexOf(\"" << s << "\") failed"; |
| 4922 | } |
| 4923 | return std::distance(table_.begin(), it); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4924 | } |
| 4925 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4926 | size_t Size() const { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4927 | return table_.size(); |
| 4928 | } |
| 4929 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4930 | void WriteTo(std::vector<uint8_t>& bytes) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4931 | for (const std::string& str : table_) { |
| 4932 | const char* s = str.c_str(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4933 | size_t s_len = CountModifiedUtf8Chars(s); |
Christopher Ferris | 8a35405 | 2015-04-24 17:23:53 -0700 | [diff] [blame] | 4934 | std::unique_ptr<uint16_t[]> s_utf16(new uint16_t[s_len]); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4935 | ConvertModifiedUtf8ToUtf16(s_utf16.get(), s); |
| 4936 | JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | private: |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4941 | std::set<std::string> table_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4942 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 4943 | }; |
| 4944 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4945 | static const char* GetMethodSourceFile(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4946 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4947 | DCHECK(method != nullptr); |
| 4948 | const char* source_file = method->GetDeclaringClassSourceFile(); |
Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 4949 | return (source_file != nullptr) ? source_file : ""; |
| 4950 | } |
| 4951 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4952 | /* |
| 4953 | * The data we send to DDMS contains everything we have recorded. |
| 4954 | * |
| 4955 | * Message header (all values big-endian): |
| 4956 | * (1b) message header len (to allow future expansion); includes itself |
| 4957 | * (1b) entry header len |
| 4958 | * (1b) stack frame len |
| 4959 | * (2b) number of entries |
| 4960 | * (4b) offset to string table from start of message |
| 4961 | * (2b) number of class name strings |
| 4962 | * (2b) number of method name strings |
| 4963 | * (2b) number of source file name strings |
| 4964 | * For each entry: |
| 4965 | * (4b) total allocation size |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 4966 | * (2b) thread id |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4967 | * (2b) allocated object's class name index |
| 4968 | * (1b) stack depth |
| 4969 | * For each stack frame: |
| 4970 | * (2b) method's class name |
| 4971 | * (2b) method name |
| 4972 | * (2b) method source file |
| 4973 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 4974 | * (xb) class name strings |
| 4975 | * (xb) method name strings |
| 4976 | * (xb) source file strings |
| 4977 | * |
| 4978 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 4979 | * followed by UTF-16 data. |
| 4980 | * |
| 4981 | * 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] | 4982 | * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4983 | * each table, but in practice there should be far fewer. |
| 4984 | * |
| 4985 | * The chief reason for using a string table here is to keep the size of |
| 4986 | * the DDMS message to a minimum. This is partly to make the protocol |
| 4987 | * efficient, but also because we have to form the whole thing up all at |
| 4988 | * once in a memory buffer. |
| 4989 | * |
| 4990 | * We use separate string tables for class names, method names, and source |
| 4991 | * files to keep the indexes small. There will generally be no overlap |
| 4992 | * between the contents of these tables. |
| 4993 | */ |
| 4994 | jbyteArray Dbg::GetRecentAllocations() { |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 4995 | if ((false)) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4996 | DumpRecentAllocations(); |
| 4997 | } |
| 4998 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4999 | Thread* self = Thread::Current(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5000 | std::vector<uint8_t> bytes; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5001 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 5002 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5003 | gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords(); |
| 5004 | // In case this method is called when allocation tracker is disabled, |
| 5005 | // we should still send some data back. |
| 5006 | gc::AllocRecordObjectMap dummy; |
| 5007 | if (records == nullptr) { |
| 5008 | CHECK(!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()); |
| 5009 | records = &dummy; |
| 5010 | } |
Man Cao | 41656de | 2015-07-06 18:53:15 -0700 | [diff] [blame] | 5011 | // We don't need to wait on the condition variable records->new_record_condition_, because this |
| 5012 | // function only reads the class objects, which are already marked so it doesn't change their |
| 5013 | // reachability. |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5014 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5015 | // |
| 5016 | // Part 1: generate string tables. |
| 5017 | // |
| 5018 | StringTable class_names; |
| 5019 | StringTable method_names; |
| 5020 | StringTable filenames; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5021 | |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 5022 | const uint16_t capped_count = CappedAllocRecordCount(records->GetRecentAllocationSize()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 5023 | uint16_t count = capped_count; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5024 | for (auto it = records->RBegin(), end = records->REnd(); |
| 5025 | count > 0 && it != end; count--, it++) { |
Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 5026 | const gc::AllocRecord* record = &it->second; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5027 | std::string temp; |
Man Cao | 41656de | 2015-07-06 18:53:15 -0700 | [diff] [blame] | 5028 | class_names.Add(record->GetClassDescriptor(&temp)); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5029 | for (size_t i = 0, depth = record->GetDepth(); i < depth; i++) { |
| 5030 | ArtMethod* m = record->StackElement(i).GetMethod(); |
| 5031 | class_names.Add(m->GetDeclaringClassDescriptor()); |
| 5032 | method_names.Add(m->GetName()); |
| 5033 | filenames.Add(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5034 | } |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5035 | } |
| 5036 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5037 | LOG(INFO) << "recent allocation records: " << capped_count; |
| 5038 | LOG(INFO) << "allocation records all objects: " << records->Size(); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5039 | |
| 5040 | // |
| 5041 | // Part 2: Generate the output and store it in the buffer. |
| 5042 | // |
| 5043 | |
| 5044 | // (1b) message header len (to allow future expansion); includes itself |
| 5045 | // (1b) entry header len |
| 5046 | // (1b) stack frame len |
| 5047 | const int kMessageHeaderLen = 15; |
| 5048 | const int kEntryHeaderLen = 9; |
| 5049 | const int kStackFrameLen = 8; |
| 5050 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 5051 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 5052 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 5053 | |
| 5054 | // (2b) number of entries |
| 5055 | // (4b) offset to string table from start of message |
| 5056 | // (2b) number of class name strings |
| 5057 | // (2b) number of method name strings |
| 5058 | // (2b) number of source file name strings |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 5059 | JDWP::Append2BE(bytes, capped_count); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5060 | size_t string_table_offset = bytes.size(); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 5061 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5062 | JDWP::Append2BE(bytes, class_names.Size()); |
| 5063 | JDWP::Append2BE(bytes, method_names.Size()); |
| 5064 | JDWP::Append2BE(bytes, filenames.Size()); |
| 5065 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5066 | std::string temp; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5067 | count = capped_count; |
| 5068 | // The last "count" number of allocation records in "records" are the most recent "count" number |
| 5069 | // of allocations. Reverse iterate to get them. The most recent allocation is sent first. |
| 5070 | for (auto it = records->RBegin(), end = records->REnd(); |
| 5071 | count > 0 && it != end; count--, it++) { |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5072 | // For each entry: |
| 5073 | // (4b) total allocation size |
| 5074 | // (2b) thread id |
| 5075 | // (2b) allocated object's class name index |
| 5076 | // (1b) stack depth |
Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 5077 | const gc::AllocRecord* record = &it->second; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5078 | size_t stack_depth = record->GetDepth(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5079 | size_t allocated_object_class_name_index = |
Man Cao | 41656de | 2015-07-06 18:53:15 -0700 | [diff] [blame] | 5080 | class_names.IndexOf(record->GetClassDescriptor(&temp)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 5081 | JDWP::Append4BE(bytes, record->ByteCount()); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5082 | JDWP::Append2BE(bytes, static_cast<uint16_t>(record->GetTid())); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5083 | JDWP::Append2BE(bytes, allocated_object_class_name_index); |
| 5084 | JDWP::Append1BE(bytes, stack_depth); |
| 5085 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5086 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 5087 | // For each stack frame: |
| 5088 | // (2b) method's class name |
| 5089 | // (2b) method name |
| 5090 | // (2b) method source file |
| 5091 | // (2b) line number, clipped to 32767; -2 if native; -1 if no source |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5092 | ArtMethod* m = record->StackElement(stack_frame).GetMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5093 | size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor()); |
| 5094 | size_t method_name_index = method_names.IndexOf(m->GetName()); |
| 5095 | size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5096 | JDWP::Append2BE(bytes, class_name_index); |
| 5097 | JDWP::Append2BE(bytes, method_name_index); |
| 5098 | JDWP::Append2BE(bytes, file_name_index); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5099 | JDWP::Append2BE(bytes, record->StackElement(stack_frame).ComputeLineNumber()); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5100 | } |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | // (xb) class name strings |
| 5104 | // (xb) method name strings |
| 5105 | // (xb) source file strings |
| 5106 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 5107 | class_names.WriteTo(bytes); |
| 5108 | method_names.WriteTo(bytes); |
| 5109 | filenames.WriteTo(bytes); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5110 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 5111 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5112 | jbyteArray result = env->NewByteArray(bytes.size()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 5113 | if (result != nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5114 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 5115 | } |
| 5116 | return result; |
| 5117 | } |
| 5118 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5119 | ArtMethod* DeoptimizationRequest::Method() const { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 5120 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 5121 | return soa.DecodeMethod(method_); |
| 5122 | } |
| 5123 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5124 | void DeoptimizationRequest::SetMethod(ArtMethod* m) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 5125 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 5126 | method_ = soa.EncodeMethod(m); |
| 5127 | } |
| 5128 | |
Mathieu Chartier | a6b1ead | 2015-10-06 10:32:38 -0700 | [diff] [blame] | 5129 | void Dbg::VisitRoots(RootVisitor* visitor) { |
| 5130 | // Visit breakpoint roots, used to prevent unloading of methods with breakpoints. |
| 5131 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| 5132 | BufferedRootVisitor<128> root_visitor(visitor, RootInfo(kRootVMInternal)); |
| 5133 | for (Breakpoint& breakpoint : gBreakpoints) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5134 | breakpoint.Method()->VisitRoots(root_visitor, kRuntimePointerSize); |
Mathieu Chartier | a6b1ead | 2015-10-06 10:32:38 -0700 | [diff] [blame] | 5135 | } |
| 5136 | } |
| 5137 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 5138 | } // namespace art |