| 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 | |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 21 | #include <functional> |
| Andreas Gampe | f774a4e | 2017-07-06 22:15:18 -0700 | [diff] [blame] | 22 | #include <memory> |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 23 | #include <set> |
| Andreas Gampe | f774a4e | 2017-07-06 22:15:18 -0700 | [diff] [blame] | 24 | #include <vector> |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 25 | |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 26 | #include "android-base/stringprintf.h" |
| 27 | |
| Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 28 | #include "arch/context.h" |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 29 | #include "art_field-inl.h" |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 30 | #include "art_method-inl.h" |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 31 | #include "base/enums.h" |
| David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 32 | #include "base/safe_map.h" |
| Andreas Gampe | f774a4e | 2017-07-06 22:15:18 -0700 | [diff] [blame] | 33 | #include "base/strlcpy.h" |
| Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 34 | #include "base/time_utils.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "class_linker-inl.h" |
| Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 36 | #include "class_linker.h" |
| David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 37 | #include "dex/descriptors_names.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 38 | #include "dex/dex_file-inl.h" |
| 39 | #include "dex/dex_file_annotations.h" |
| 40 | #include "dex/dex_file_types.h" |
| 41 | #include "dex/dex_instruction.h" |
| David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 42 | #include "dex/utf.h" |
| Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 43 | #include "entrypoints/runtime_asm_entrypoints.h" |
| Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 44 | #include "gc/accounting/card_table-inl.h" |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 45 | #include "gc/allocation_record.h" |
| Andreas Gampe | 94c589d | 2017-12-27 12:43:01 -0800 | [diff] [blame] | 46 | #include "gc/gc_cause.h" |
| Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 47 | #include "gc/scoped_gc_critical_section.h" |
| Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 48 | #include "gc/space/bump_pointer_space-walk-inl.h" |
| Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 49 | #include "gc/space/large_object_space.h" |
| 50 | #include "gc/space/space-inl.h" |
| Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 51 | #include "handle_scope-inl.h" |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 52 | #include "jdwp/jdwp_priv.h" |
| Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 53 | #include "jdwp/object_registry.h" |
| Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 54 | #include "jni/jni_internal.h" |
| Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 55 | #include "jvalue-inl.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | #include "mirror/class-inl.h" |
| Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 57 | #include "mirror/class.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | #include "mirror/class_loader.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | #include "mirror/object-inl.h" |
| 60 | #include "mirror/object_array-inl.h" |
| Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 61 | #include "mirror/string-inl.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 62 | #include "mirror/throwable.h" |
| Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 63 | #include "nativehelper/scoped_local_ref.h" |
| 64 | #include "nativehelper/scoped_primitive_array.h" |
| Nicolas Geoffray | 58cc1cb | 2017-11-20 13:27:29 +0000 | [diff] [blame] | 65 | #include "oat_file.h" |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 66 | #include "obj_ptr-inl.h" |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 67 | #include "reflection.h" |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 68 | #include "scoped_thread_state_change-inl.h" |
| Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 69 | #include "stack.h" |
| Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 70 | #include "thread_list.h" |
| Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 71 | #include "well_known_classes.h" |
| Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 72 | |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 73 | namespace art { |
| 74 | |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 75 | using android::base::StringPrintf; |
| 76 | |
| Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 77 | // The key identifying the debugger to update instrumentation. |
| 78 | static constexpr const char* kDbgInstrumentationKey = "Debugger"; |
| 79 | |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 80 | // 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] | 81 | static uint16_t CappedAllocRecordCount(size_t alloc_record_count) { |
| Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 82 | const size_t cap = 0xffff; |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 83 | if (alloc_record_count > cap) { |
| 84 | return cap; |
| Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 85 | } |
| 86 | return alloc_record_count; |
| 87 | } |
| Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 88 | |
| Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 89 | class Breakpoint : public ValueObject { |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 90 | public: |
| Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 91 | Breakpoint(ArtMethod* method, uint32_t dex_pc, DeoptimizationRequest::Kind deoptimization_kind) |
| Alex Light | 97e7803 | 2017-06-27 17:51:55 -0700 | [diff] [blame] | 92 | : method_(method->GetCanonicalMethod(kRuntimePointerSize)), |
| Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 93 | dex_pc_(dex_pc), |
| 94 | deoptimization_kind_(deoptimization_kind) { |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 95 | CHECK(deoptimization_kind_ == DeoptimizationRequest::kNothing || |
| 96 | deoptimization_kind_ == DeoptimizationRequest::kSelectiveDeoptimization || |
| 97 | deoptimization_kind_ == DeoptimizationRequest::kFullDeoptimization); |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 100 | Breakpoint(const Breakpoint& other) REQUIRES_SHARED(Locks::mutator_lock_) |
| Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 101 | : method_(other.method_), |
| 102 | dex_pc_(other.dex_pc_), |
| 103 | deoptimization_kind_(other.deoptimization_kind_) {} |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 104 | |
| Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 105 | // Method() is called from root visiting, do not use ScopedObjectAccess here or it can cause |
| 106 | // GC to deadlock if another thread tries to call SuspendAll while the GC is in a runnable state. |
| 107 | ArtMethod* Method() const { |
| 108 | return method_; |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | uint32_t DexPc() const { |
| 112 | return dex_pc_; |
| 113 | } |
| 114 | |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 115 | DeoptimizationRequest::Kind GetDeoptimizationKind() const { |
| 116 | return deoptimization_kind_; |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 119 | // Returns true if the method of this breakpoint and the passed in method should be considered the |
| 120 | // 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] | 121 | bool IsInMethod(ArtMethod* m) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| Alex Light | 97e7803 | 2017-06-27 17:51:55 -0700 | [diff] [blame] | 122 | return method_ == m->GetCanonicalMethod(kRuntimePointerSize); |
| Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 125 | private: |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 126 | // The location of this breakpoint. |
| Mathieu Chartier | 41af5e5 | 2015-10-28 11:10:46 -0700 | [diff] [blame] | 127 | ArtMethod* method_; |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 128 | uint32_t dex_pc_; |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 129 | |
| 130 | // Indicates whether breakpoint needs full deoptimization or selective deoptimization. |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 131 | DeoptimizationRequest::Kind deoptimization_kind_; |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
| Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 134 | static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 135 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 136 | os << StringPrintf("Breakpoint[%s @%#x]", ArtMethod::PrettyMethod(rhs.Method()).c_str(), |
| 137 | rhs.DexPc()); |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 138 | return os; |
| 139 | } |
| 140 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 141 | class DebugInstrumentationListener final : public instrumentation::InstrumentationListener { |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 142 | public: |
| 143 | DebugInstrumentationListener() {} |
| 144 | virtual ~DebugInstrumentationListener() {} |
| 145 | |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 146 | void MethodEntered(Thread* thread, |
| 147 | Handle<mirror::Object> this_object, |
| 148 | ArtMethod* method, |
| Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 149 | uint32_t dex_pc) |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 150 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 151 | if (method->IsNative()) { |
| 152 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 153 | return; |
| 154 | } |
| Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 155 | if (IsListeningToDexPcMoved()) { |
| 156 | // We also listen to kDexPcMoved instrumentation event so we know the DexPcMoved method is |
| 157 | // going to be called right after us. To avoid sending JDWP events twice for this location, |
| 158 | // we report the event in DexPcMoved. However, we must remind this is method entry so we |
| 159 | // send the METHOD_ENTRY event. And we can also group it with other events for this location |
| 160 | // like BREAKPOINT or SINGLE_STEP (or even METHOD_EXIT if this is a RETURN instruction). |
| 161 | thread->SetDebugMethodEntry(); |
| 162 | } else if (IsListeningToMethodExit() && IsReturn(method, dex_pc)) { |
| 163 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 164 | // RETURN so we know the MethodExited method is going to be called right after us. To avoid |
| 165 | // sending JDWP events twice for this location, we report the event(s) in MethodExited. |
| 166 | // However, we must remind this is method entry so we send the METHOD_ENTRY event. And we can |
| 167 | // also group it with other events for this location like BREAKPOINT or SINGLE_STEP. |
| 168 | thread->SetDebugMethodEntry(); |
| 169 | } else { |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 170 | Dbg::UpdateDebugger(thread, this_object.Get(), method, 0, Dbg::kMethodEntry, nullptr); |
| Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 171 | } |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 174 | void MethodExited(Thread* thread, |
| 175 | Handle<mirror::Object> this_object, |
| 176 | ArtMethod* method, |
| 177 | uint32_t dex_pc, |
| 178 | const JValue& return_value) |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 179 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 180 | if (method->IsNative()) { |
| 181 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 182 | return; |
| 183 | } |
| Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 184 | uint32_t events = Dbg::kMethodExit; |
| 185 | if (thread->IsDebugMethodEntry()) { |
| 186 | // It is also the method entry. |
| 187 | DCHECK(IsReturn(method, dex_pc)); |
| 188 | events |= Dbg::kMethodEntry; |
| 189 | thread->ClearDebugMethodEntry(); |
| 190 | } |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 191 | Dbg::UpdateDebugger(thread, this_object.Get(), method, dex_pc, events, &return_value); |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 194 | void MethodUnwind(Thread* thread ATTRIBUTE_UNUSED, |
| 195 | Handle<mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 196 | ArtMethod* method, |
| 197 | uint32_t dex_pc) |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 198 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 199 | // 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] | 200 | LOG(ERROR) << "Unexpected method unwind event in debugger " << ArtMethod::PrettyMethod(method) |
| Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 201 | << " " << dex_pc; |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 204 | void DexPcMoved(Thread* thread, |
| 205 | Handle<mirror::Object> this_object, |
| 206 | ArtMethod* method, |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 207 | uint32_t new_dex_pc) |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 208 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 209 | if (IsListeningToMethodExit() && IsReturn(method, new_dex_pc)) { |
| 210 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 211 | // RETURN so we know the MethodExited method is going to be called right after us. Like in |
| 212 | // MethodEntered, we delegate event reporting to MethodExited. |
| 213 | // Besides, if this RETURN instruction is the only one in the method, we can send multiple |
| 214 | // JDWP events in the same packet: METHOD_ENTRY, METHOD_EXIT, BREAKPOINT and/or SINGLE_STEP. |
| 215 | // Therefore, we must not clear the debug method entry flag here. |
| 216 | } else { |
| 217 | uint32_t events = 0; |
| 218 | if (thread->IsDebugMethodEntry()) { |
| 219 | // It is also the method entry. |
| 220 | events = Dbg::kMethodEntry; |
| 221 | thread->ClearDebugMethodEntry(); |
| 222 | } |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 223 | Dbg::UpdateDebugger(thread, this_object.Get(), method, new_dex_pc, events, nullptr); |
| Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 224 | } |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 227 | void FieldRead(Thread* thread ATTRIBUTE_UNUSED, |
| 228 | Handle<mirror::Object> this_object, |
| 229 | ArtMethod* method, |
| 230 | uint32_t dex_pc, |
| 231 | ArtField* field) |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 232 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 233 | Dbg::PostFieldAccessEvent(method, dex_pc, this_object.Get(), field); |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 234 | } |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 235 | |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 236 | void FieldWritten(Thread* thread ATTRIBUTE_UNUSED, |
| 237 | Handle<mirror::Object> this_object, |
| 238 | ArtMethod* method, |
| 239 | uint32_t dex_pc, |
| 240 | ArtField* field, |
| Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 241 | const JValue& field_value) |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 242 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 243 | Dbg::PostFieldModificationEvent(method, dex_pc, this_object.Get(), field, &field_value); |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 246 | void ExceptionThrown(Thread* thread ATTRIBUTE_UNUSED, |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 247 | Handle<mirror::Throwable> exception_object) |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 248 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 249 | Dbg::PostException(exception_object.Get()); |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 252 | // We only care about branches in the Jit. |
| 253 | void Branch(Thread* /*thread*/, ArtMethod* method, uint32_t dex_pc, int32_t dex_pc_offset) |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 254 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 255 | LOG(ERROR) << "Unexpected branch event in debugger " << ArtMethod::PrettyMethod(method) |
| Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 256 | << " " << dex_pc << ", " << dex_pc_offset; |
| Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| Alex Light | 798eab0 | 2017-08-23 12:54:53 -0700 | [diff] [blame] | 259 | // TODO Might be worth it to post ExceptionCatch event. |
| 260 | void ExceptionHandled(Thread* thread ATTRIBUTE_UNUSED, |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 261 | Handle<mirror::Throwable> throwable ATTRIBUTE_UNUSED) override { |
| Alex Light | 798eab0 | 2017-08-23 12:54:53 -0700 | [diff] [blame] | 262 | LOG(ERROR) << "Unexpected exception handled event in debugger"; |
| 263 | } |
| 264 | |
| Alex Light | 05f4774 | 2017-09-14 00:34:44 +0000 | [diff] [blame] | 265 | // TODO Might be worth it to implement this. |
| 266 | void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED, |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 267 | const ShadowFrame& frame ATTRIBUTE_UNUSED) override { |
| Alex Light | 05f4774 | 2017-09-14 00:34:44 +0000 | [diff] [blame] | 268 | LOG(ERROR) << "Unexpected WatchedFramePop event in debugger"; |
| 269 | } |
| Alex Light | 798eab0 | 2017-08-23 12:54:53 -0700 | [diff] [blame] | 270 | |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 271 | private: |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 272 | static bool IsReturn(ArtMethod* method, uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 273 | return method->DexInstructions().InstructionAt(dex_pc).IsReturn(); |
| Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 276 | static bool IsListeningToDexPcMoved() REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 277 | return IsListeningTo(instrumentation::Instrumentation::kDexPcMoved); |
| 278 | } |
| 279 | |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 280 | static bool IsListeningToMethodExit() REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 281 | return IsListeningTo(instrumentation::Instrumentation::kMethodExited); |
| 282 | } |
| 283 | |
| 284 | static bool IsListeningTo(instrumentation::Instrumentation::InstrumentationEvent event) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 285 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 286 | return (Dbg::GetInstrumentationEvents() & event) != 0; |
| 287 | } |
| 288 | |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 289 | DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener); |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 290 | } gDebugInstrumentationListener; |
| 291 | |
| Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 292 | // JDWP is allowed unless the Zygote forbids it. |
| 293 | static bool gJdwpAllowed = true; |
| 294 | |
| Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 295 | // Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line? |
| Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 296 | static bool gJdwpConfigured = false; |
| 297 | |
| Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 298 | // JDWP options for debugging. Only valid if IsJdwpConfigured() is true. |
| 299 | static JDWP::JdwpOptions gJdwpOptions; |
| 300 | |
| Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 301 | // Runtime JDWP state. |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 302 | static JDWP::JdwpState* gJdwpState = nullptr; |
| Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 303 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
| Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 304 | |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 305 | static bool gDdmThreadNotification = false; |
| 306 | |
| Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 307 | // DDMS GC-related settings. |
| 308 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 309 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 310 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 311 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 312 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 313 | |
| Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 314 | bool Dbg::gDebuggerActive = false; |
| Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 315 | bool Dbg::gDisposed = false; |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 316 | ObjectRegistry* Dbg::gRegistry = nullptr; |
| Alex Light | 2161193 | 2017-09-26 13:07:39 -0700 | [diff] [blame] | 317 | DebuggerActiveMethodInspectionCallback Dbg::gDebugActiveCallback; |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 318 | DebuggerDdmCallback Dbg::gDebugDdmCallback; |
| Alex Light | 4032071 | 2017-12-14 11:52:04 -0800 | [diff] [blame] | 319 | InternalDebuggerControlCallback Dbg::gDebuggerControlCallback; |
| Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 320 | |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 321 | // Deoptimization support. |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 322 | std::vector<DeoptimizationRequest> Dbg::deoptimization_requests_; |
| 323 | size_t Dbg::full_deoptimization_event_count_ = 0; |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 324 | |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 325 | // Instrumentation event reference counters. |
| 326 | size_t Dbg::dex_pc_change_event_ref_count_ = 0; |
| 327 | size_t Dbg::method_enter_event_ref_count_ = 0; |
| 328 | size_t Dbg::method_exit_event_ref_count_ = 0; |
| 329 | size_t Dbg::field_read_event_ref_count_ = 0; |
| 330 | size_t Dbg::field_write_event_ref_count_ = 0; |
| 331 | size_t Dbg::exception_catch_event_ref_count_ = 0; |
| 332 | uint32_t Dbg::instrumentation_events_ = 0; |
| 333 | |
| Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 334 | Dbg::DbgThreadLifecycleCallback Dbg::thread_lifecycle_callback_; |
| Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 335 | Dbg::DbgClassLoadCallback Dbg::class_load_callback_; |
| Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 336 | |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 337 | void DebuggerDdmCallback::DdmPublishChunk(uint32_t type, const ArrayRef<const uint8_t>& data) { |
| Alex Light | 772099a | 2017-11-21 14:05:04 -0800 | [diff] [blame] | 338 | if (gJdwpState == nullptr) { |
| 339 | VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type; |
| 340 | } else { |
| 341 | iovec vec[1]; |
| 342 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(data.data())); |
| 343 | vec[0].iov_len = data.size(); |
| 344 | gJdwpState->DdmSendChunkV(type, vec, 1); |
| 345 | } |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| Alex Light | 2161193 | 2017-09-26 13:07:39 -0700 | [diff] [blame] | 348 | bool DebuggerActiveMethodInspectionCallback::IsMethodBeingInspected(ArtMethod* m ATTRIBUTE_UNUSED) { |
| 349 | return Dbg::IsDebuggerActive(); |
| 350 | } |
| 351 | |
| Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 352 | bool DebuggerActiveMethodInspectionCallback::IsMethodSafeToJit(ArtMethod* m) { |
| 353 | return !Dbg::MethodHasAnyBreakpoints(m); |
| 354 | } |
| 355 | |
| Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 356 | bool DebuggerActiveMethodInspectionCallback::MethodNeedsDebugVersion( |
| 357 | ArtMethod* m ATTRIBUTE_UNUSED) { |
| 358 | return Dbg::IsDebuggerActive(); |
| 359 | } |
| 360 | |
| Alex Light | 4032071 | 2017-12-14 11:52:04 -0800 | [diff] [blame] | 361 | void InternalDebuggerControlCallback::StartDebugger() { |
| 362 | // Release the mutator lock. |
| 363 | ScopedThreadStateChange stsc(art::Thread::Current(), kNative); |
| 364 | Dbg::StartJdwp(); |
| 365 | } |
| 366 | |
| 367 | void InternalDebuggerControlCallback::StopDebugger() { |
| 368 | Dbg::StopJdwp(); |
| 369 | } |
| 370 | |
| 371 | bool InternalDebuggerControlCallback::IsDebuggerConfigured() { |
| 372 | return Dbg::IsJdwpConfigured(); |
| 373 | } |
| 374 | |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 375 | // Breakpoints. |
| jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 376 | static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_); |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 377 | |
| Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 378 | void DebugInvokeReq::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 379 | receiver.VisitRootIfNonNull(visitor, root_info); // null for static method call. |
| 380 | klass.VisitRoot(visitor, root_info); |
| Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 383 | void SingleStepControl::AddDexPc(uint32_t dex_pc) { |
| 384 | dex_pcs_.insert(dex_pc); |
| Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 385 | } |
| 386 | |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 387 | bool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const { |
| 388 | return dex_pcs_.find(dex_pc) == dex_pcs_.end(); |
| Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 389 | } |
| 390 | |
| Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 391 | static bool IsBreakpoint(ArtMethod* m, uint32_t dex_pc) |
| Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 392 | REQUIRES(!Locks::breakpoint_lock_) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 393 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 394 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 395 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
| Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 396 | if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].IsInMethod(m)) { |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 397 | VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i]; |
| 398 | return true; |
| 399 | } |
| 400 | } |
| 401 | return false; |
| 402 | } |
| 403 | |
| Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 404 | static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) |
| Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 405 | REQUIRES(!Locks::thread_suspend_count_lock_) { |
| Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 406 | MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); |
| 407 | // A thread may be suspended for GC; in this code, we really want to know whether |
| 408 | // there's a debugger suspension active. |
| 409 | return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0; |
| 410 | } |
| 411 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 412 | static mirror::Array* DecodeNonNullArray(JDWP::RefTypeId id, JDWP::JdwpError* error) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 413 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 414 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 415 | if (o == nullptr) { |
| 416 | *error = JDWP::ERR_INVALID_OBJECT; |
| 417 | return nullptr; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 418 | } |
| 419 | if (!o->IsArrayInstance()) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 420 | *error = JDWP::ERR_INVALID_ARRAY; |
| 421 | return nullptr; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 422 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 423 | *error = JDWP::ERR_NONE; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 424 | return o->AsArray(); |
| 425 | } |
| 426 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 427 | static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError* error) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 428 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 429 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 430 | if (o == nullptr) { |
| 431 | *error = JDWP::ERR_INVALID_OBJECT; |
| 432 | return nullptr; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 433 | } |
| 434 | if (!o->IsClass()) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 435 | *error = JDWP::ERR_INVALID_CLASS; |
| 436 | return nullptr; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 437 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 438 | *error = JDWP::ERR_NONE; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 439 | return o->AsClass(); |
| 440 | } |
| 441 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 442 | static Thread* DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, |
| 443 | JDWP::JdwpError* error) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 444 | REQUIRES_SHARED(Locks::mutator_lock_) |
| Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 445 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) { |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 446 | mirror::Object* thread_peer = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_id, error); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 447 | if (thread_peer == nullptr) { |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 448 | // This isn't even an object. |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 449 | *error = JDWP::ERR_INVALID_OBJECT; |
| 450 | return nullptr; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 451 | } |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 452 | |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 453 | ObjPtr<mirror::Class> java_lang_Thread = |
| 454 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_Thread); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 455 | if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) { |
| 456 | // This isn't a thread. |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 457 | *error = JDWP::ERR_INVALID_THREAD; |
| 458 | return nullptr; |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 461 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 462 | Thread* thread = Thread::FromManagedThread(soa, thread_peer); |
| 463 | // If thread is null then this a java.lang.Thread without a Thread*. Must be a un-started or a |
| 464 | // zombie. |
| 465 | *error = (thread == nullptr) ? JDWP::ERR_THREAD_NOT_ALIVE : JDWP::ERR_NONE; |
| 466 | return thread; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 469 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 470 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 471 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 472 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 473 | } |
| 474 | |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 475 | static JDWP::JdwpTag BasicTagFromClass(mirror::Class* klass) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 476 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 477 | std::string temp; |
| 478 | const char* descriptor = klass->GetDescriptor(&temp); |
| 479 | return BasicTagFromDescriptor(descriptor); |
| 480 | } |
| 481 | |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 482 | static JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 483 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 484 | CHECK(c != nullptr); |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 485 | if (c->IsArrayClass()) { |
| 486 | return JDWP::JT_ARRAY; |
| 487 | } |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 488 | if (c->IsStringClass()) { |
| 489 | return JDWP::JT_STRING; |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 490 | } |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 491 | if (c->IsClassClass()) { |
| 492 | return JDWP::JT_CLASS_OBJECT; |
| 493 | } |
| 494 | { |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 495 | ObjPtr<mirror::Class> thread_class = |
| 496 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_Thread); |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 497 | if (thread_class->IsAssignableFrom(c)) { |
| 498 | return JDWP::JT_THREAD; |
| 499 | } |
| 500 | } |
| 501 | { |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 502 | ObjPtr<mirror::Class> thread_group_class = |
| 503 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_ThreadGroup); |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 504 | if (thread_group_class->IsAssignableFrom(c)) { |
| 505 | return JDWP::JT_THREAD_GROUP; |
| 506 | } |
| 507 | } |
| 508 | { |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 509 | ObjPtr<mirror::Class> class_loader_class = |
| 510 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_ClassLoader); |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 511 | if (class_loader_class->IsAssignableFrom(c)) { |
| 512 | return JDWP::JT_CLASS_LOADER; |
| 513 | } |
| 514 | } |
| 515 | return JDWP::JT_OBJECT; |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Objects declared to hold Object might actually hold a more specific |
| 520 | * type. The debugger may take a special interest in these (e.g. it |
| 521 | * wants to display the contents of Strings), so we want to return an |
| 522 | * appropriate tag. |
| 523 | * |
| 524 | * Null objects are tagged JT_OBJECT. |
| 525 | */ |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 526 | JDWP::JdwpTag Dbg::TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 527 | return (o == nullptr) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass()); |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 531 | switch (tag) { |
| 532 | case JDWP::JT_BOOLEAN: |
| 533 | case JDWP::JT_BYTE: |
| 534 | case JDWP::JT_CHAR: |
| 535 | case JDWP::JT_FLOAT: |
| 536 | case JDWP::JT_DOUBLE: |
| 537 | case JDWP::JT_INT: |
| 538 | case JDWP::JT_LONG: |
| 539 | case JDWP::JT_SHORT: |
| 540 | case JDWP::JT_VOID: |
| 541 | return true; |
| 542 | default: |
| 543 | return false; |
| 544 | } |
| 545 | } |
| 546 | |
| Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 547 | void Dbg::StartJdwp() { |
| Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 548 | if (!gJdwpAllowed || !IsJdwpConfigured()) { |
| Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 549 | // No JDWP for you! |
| 550 | return; |
| 551 | } |
| 552 | |
| Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 553 | CHECK(gRegistry == nullptr); |
| Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 554 | gRegistry = new ObjectRegistry; |
| 555 | |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 556 | { |
| 557 | // Setup the Ddm listener |
| 558 | ScopedObjectAccess soa(Thread::Current()); |
| 559 | Runtime::Current()->GetRuntimeCallbacks()->AddDdmCallback(&gDebugDdmCallback); |
| 560 | } |
| 561 | |
| Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 562 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 563 | // debugger, passively listen for a debugger, or block waiting for a |
| 564 | // debugger. |
| Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 565 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 566 | if (gJdwpState == nullptr) { |
| Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 567 | // We probably failed because some other process has the port already, which means that |
| 568 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 569 | // talking to that other process. |
| Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 570 | LOG(FATAL) << "Debugger thread failed to initialize"; |
| Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | // If a debugger has already attached, send the "welcome" message. |
| 574 | // This may cause us to suspend all threads. |
| Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 575 | if (gJdwpState->IsActive()) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 576 | ScopedObjectAccess soa(Thread::Current()); |
| Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 577 | gJdwpState->PostVMStart(); |
| Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 578 | } |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 581 | void Dbg::StopJdwp() { |
| Sebastien Hertz | c6345ef | 2014-08-18 19:26:39 +0200 | [diff] [blame] | 582 | // Post VM_DEATH event before the JDWP connection is closed (either by the JDWP thread or the |
| 583 | // destruction of gJdwpState). |
| 584 | if (gJdwpState != nullptr && gJdwpState->IsActive()) { |
| 585 | gJdwpState->PostVMDeath(); |
| 586 | } |
| Sebastien Hertz | 0376e6b | 2014-02-06 18:12:59 +0100 | [diff] [blame] | 587 | // 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] | 588 | Dispose(); |
| Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 589 | delete gJdwpState; |
| Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 590 | gJdwpState = nullptr; |
| Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 591 | delete gRegistry; |
| Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 592 | gRegistry = nullptr; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 595 | void Dbg::GcDidFinish() { |
| 596 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 597 | ScopedObjectAccess soa(Thread::Current()); |
| Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 598 | VLOG(jdwp) << "Sending heap info to DDM"; |
| Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 599 | DdmSendHeapInfo(gDdmHpifWhen); |
| Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 600 | } |
| 601 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 602 | ScopedObjectAccess soa(Thread::Current()); |
| Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 603 | VLOG(jdwp) << "Dumping heap to DDM"; |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 604 | DdmSendHeapSegments(false); |
| Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 605 | } |
| 606 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 607 | ScopedObjectAccess soa(Thread::Current()); |
| Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 608 | VLOG(jdwp) << "Dumping native heap to DDM"; |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 609 | DdmSendHeapSegments(true); |
| Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
| Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 613 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 614 | gJdwpAllowed = allowed; |
| 615 | } |
| 616 | |
| Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 617 | bool Dbg::IsJdwpAllowed() { |
| 618 | return gJdwpAllowed; |
| 619 | } |
| 620 | |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 621 | DebugInvokeReq* Dbg::GetInvokeReq() { |
| Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 622 | return Thread::Current()->GetInvokeReq(); |
| 623 | } |
| 624 | |
| 625 | Thread* Dbg::GetDebugThread() { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 626 | return (gJdwpState != nullptr) ? gJdwpState->GetDebugThread() : nullptr; |
| Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void Dbg::ClearWaitForEventThread() { |
| Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 630 | gJdwpState->ReleaseJdwpTokenForEvent(); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | void Dbg::Connected() { |
| Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 634 | CHECK(!gDebuggerConnected); |
| Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 635 | VLOG(jdwp) << "JDWP has attached"; |
| Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 636 | gDebuggerConnected = true; |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 637 | gDisposed = false; |
| 638 | } |
| 639 | |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 640 | bool Dbg::RequiresDeoptimization() { |
| 641 | // We don't need deoptimization if everything runs with interpreter after |
| 642 | // enabling -Xint mode. |
| 643 | return !Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly(); |
| 644 | } |
| 645 | |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 646 | void Dbg::GoActive() { |
| 647 | // Enable all debugging features, including scans for breakpoints. |
| 648 | // This is a no-op if we're already active. |
| 649 | // Only called from the JDWP handler thread. |
| Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 650 | if (IsDebuggerActive()) { |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | |
| Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 654 | Thread* const self = Thread::Current(); |
| Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 655 | { |
| 656 | // 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] | 657 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
| Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 658 | CHECK_EQ(gBreakpoints.size(), 0U); |
| 659 | } |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 660 | |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 661 | { |
| Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 662 | MutexLock mu(self, *Locks::deoptimization_lock_); |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 663 | CHECK_EQ(deoptimization_requests_.size(), 0U); |
| 664 | CHECK_EQ(full_deoptimization_event_count_, 0U); |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 665 | CHECK_EQ(dex_pc_change_event_ref_count_, 0U); |
| 666 | CHECK_EQ(method_enter_event_ref_count_, 0U); |
| 667 | CHECK_EQ(method_exit_event_ref_count_, 0U); |
| 668 | CHECK_EQ(field_read_event_ref_count_, 0U); |
| 669 | CHECK_EQ(field_write_event_ref_count_, 0U); |
| 670 | CHECK_EQ(exception_catch_event_ref_count_, 0U); |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 671 | } |
| 672 | |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 673 | Runtime* runtime = Runtime::Current(); |
| Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 674 | // Best effort deoptimization if the runtime is non-Java debuggable. This happens when |
| 675 | // ro.debuggable is set, but the application is not debuggable, or when a standalone |
| 676 | // dalvikvm invocation is not passed the debuggable option (-Xcompiler-option --debuggable). |
| 677 | // |
| 678 | // The performance cost of this is non-negligible during native-debugging due to the |
| David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 679 | // forced JIT, so we keep the AOT code in that case in exchange for limited native debugging. |
| Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 680 | if (!runtime->IsJavaDebuggable() && |
| 681 | !runtime->GetInstrumentation()->IsForcedInterpretOnly() && |
| 682 | !runtime->IsNativeDebuggable()) { |
| 683 | runtime->DeoptimizeBootImage(); |
| Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 684 | } |
| 685 | |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 686 | ScopedSuspendAll ssa(__FUNCTION__); |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 687 | if (RequiresDeoptimization()) { |
| 688 | runtime->GetInstrumentation()->EnableDeoptimization(); |
| 689 | } |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 690 | instrumentation_events_ = 0; |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 691 | gDebuggerActive = true; |
| Alex Light | 2161193 | 2017-09-26 13:07:39 -0700 | [diff] [blame] | 692 | Runtime::Current()->GetRuntimeCallbacks()->AddMethodInspectionCallback(&gDebugActiveCallback); |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 693 | LOG(INFO) << "Debugger is active"; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void Dbg::Disconnected() { |
| Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 697 | CHECK(gDebuggerConnected); |
| 698 | |
| Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 699 | LOG(INFO) << "Debugger is no longer active"; |
| Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 700 | |
| Hiroshi Yamauchi | 98810e3 | 2016-05-24 14:55:40 -0700 | [diff] [blame] | 701 | // 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] | 702 | // and clear the object registry. |
| 703 | Runtime* runtime = Runtime::Current(); |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 704 | Thread* self = Thread::Current(); |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 705 | { |
| Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 706 | // Required for DisableDeoptimization. |
| 707 | gc::ScopedGCCriticalSection gcs(self, |
| 708 | gc::kGcCauseInstrumentation, |
| 709 | gc::kCollectorTypeInstrumentation); |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 710 | ScopedSuspendAll ssa(__FUNCTION__); |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 711 | // Debugger may not be active at this point. |
| 712 | if (IsDebuggerActive()) { |
| 713 | { |
| 714 | // Since we're going to disable deoptimization, we clear the deoptimization requests queue. |
| 715 | // This prevents us from having any pending deoptimization request when the debugger attaches |
| 716 | // to us again while no event has been requested yet. |
| Mathieu Chartier | a6b1ead | 2015-10-06 10:32:38 -0700 | [diff] [blame] | 717 | MutexLock mu(self, *Locks::deoptimization_lock_); |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 718 | deoptimization_requests_.clear(); |
| 719 | full_deoptimization_event_count_ = 0U; |
| 720 | } |
| 721 | if (instrumentation_events_ != 0) { |
| 722 | runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener, |
| 723 | instrumentation_events_); |
| 724 | instrumentation_events_ = 0; |
| 725 | } |
| 726 | if (RequiresDeoptimization()) { |
| 727 | runtime->GetInstrumentation()->DisableDeoptimization(kDbgInstrumentationKey); |
| 728 | } |
| 729 | gDebuggerActive = false; |
| Alex Light | 2161193 | 2017-09-26 13:07:39 -0700 | [diff] [blame] | 730 | Runtime::Current()->GetRuntimeCallbacks()->RemoveMethodInspectionCallback( |
| 731 | &gDebugActiveCallback); |
| Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 732 | } |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 733 | } |
| Sebastien Hertz | 55f6534 | 2015-01-13 22:48:34 +0100 | [diff] [blame] | 734 | |
| 735 | { |
| 736 | ScopedObjectAccess soa(self); |
| 737 | gRegistry->Clear(); |
| 738 | } |
| 739 | |
| 740 | gDebuggerConnected = false; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 743 | void Dbg::ConfigureJdwp(const JDWP::JdwpOptions& jdwp_options) { |
| 744 | CHECK_NE(jdwp_options.transport, JDWP::kJdwpTransportUnknown); |
| 745 | gJdwpOptions = jdwp_options; |
| 746 | gJdwpConfigured = true; |
| Alex Light | 4032071 | 2017-12-14 11:52:04 -0800 | [diff] [blame] | 747 | Runtime::Current()->GetRuntimeCallbacks()->AddDebuggerControlCallback(&gDebuggerControlCallback); |
| Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 748 | } |
| 749 | |
| Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 750 | bool Dbg::IsJdwpConfigured() { |
| Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 751 | return gJdwpConfigured; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | int64_t Dbg::LastDebuggerActivity() { |
| Elliott Hughes | ca95152 | 2011-12-05 12:01:32 -0800 | [diff] [blame] | 755 | return gJdwpState->LastDebuggerActivity(); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 758 | void Dbg::UndoDebuggerSuspensions() { |
| Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 759 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 762 | std::string Dbg::GetClassName(JDWP::RefTypeId class_id) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 763 | JDWP::JdwpError error; |
| 764 | mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id, &error); |
| 765 | if (o == nullptr) { |
| 766 | if (error == JDWP::ERR_NONE) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 767 | return "null"; |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 768 | } else { |
| 769 | return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id)); |
| 770 | } |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 771 | } |
| 772 | if (!o->IsClass()) { |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 773 | 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] | 774 | } |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 775 | return GetClassName(o->AsClass()); |
| 776 | } |
| 777 | |
| 778 | std::string Dbg::GetClassName(mirror::Class* klass) { |
| Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 779 | if (klass == nullptr) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 780 | return "null"; |
| Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 781 | } |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 782 | std::string temp; |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 783 | return DescriptorToName(klass->GetDescriptor(&temp)); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 786 | JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId* class_object_id) { |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 787 | JDWP::JdwpError status; |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 788 | mirror::Class* c = DecodeClass(id, &status); |
| 789 | if (c == nullptr) { |
| 790 | *class_object_id = 0; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 791 | return status; |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 792 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 793 | *class_object_id = gRegistry->Add(c); |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 794 | return JDWP::ERR_NONE; |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 795 | } |
| 796 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 797 | JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId* superclass_id) { |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 798 | JDWP::JdwpError status; |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 799 | mirror::Class* c = DecodeClass(id, &status); |
| 800 | if (c == nullptr) { |
| 801 | *superclass_id = 0; |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 802 | return status; |
| 803 | } |
| 804 | if (c->IsInterface()) { |
| 805 | // http://code.google.com/p/android/issues/detail?id=20856 |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 806 | *superclass_id = 0; |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 807 | } else { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 808 | *superclass_id = gRegistry->Add(c->GetSuperClass()); |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 809 | } |
| 810 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 813 | JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 814 | JDWP::JdwpError error; |
| Andreas Gampe | 7929a48 | 2015-12-30 19:33:49 -0800 | [diff] [blame] | 815 | mirror::Class* c = DecodeClass(id, &error); |
| 816 | if (c == nullptr) { |
| 817 | return error; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 818 | } |
| Andreas Gampe | 7929a48 | 2015-12-30 19:33:49 -0800 | [diff] [blame] | 819 | expandBufAddObjectId(pReply, gRegistry->Add(c->GetClassLoader())); |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 820 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 823 | JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 824 | JDWP::JdwpError error; |
| 825 | mirror::Class* c = DecodeClass(id, &error); |
| 826 | if (c == nullptr) { |
| 827 | return error; |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 828 | } |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 829 | |
| 830 | uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask; |
| 831 | |
| Yevgeny Rouban | de34eea | 2014-02-15 01:06:03 +0700 | [diff] [blame] | 832 | // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set, |
| 833 | // not interfaces. |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 834 | // 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] | 835 | if ((access_flags & kAccInterface) == 0) { |
| 836 | access_flags |= kAccSuper; |
| 837 | } |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 838 | |
| 839 | expandBufAdd4BE(pReply, access_flags); |
| 840 | |
| 841 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 844 | JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) { |
| 845 | JDWP::JdwpError error; |
| 846 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 847 | if (o == nullptr) { |
| Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 848 | return JDWP::ERR_INVALID_OBJECT; |
| 849 | } |
| 850 | |
| 851 | // Ensure all threads are suspended while we read objects' lock words. |
| 852 | Thread* self = Thread::Current(); |
| Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 853 | CHECK_EQ(self->GetState(), kRunnable); |
| Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 854 | |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 855 | MonitorInfo monitor_info; |
| 856 | { |
| 857 | ScopedThreadSuspension sts(self, kSuspended); |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 858 | ScopedSuspendAll ssa(__FUNCTION__); |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 859 | monitor_info = MonitorInfo(o); |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 860 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 861 | if (monitor_info.owner_ != nullptr) { |
| Nicolas Geoffray | cafa081 | 2017-02-15 18:27:34 +0000 | [diff] [blame] | 862 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeerFromOtherThread())); |
| Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 863 | } else { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 864 | expandBufAddObjectId(reply, gRegistry->Add(nullptr)); |
| Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 865 | } |
| Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 866 | expandBufAdd4BE(reply, monitor_info.entry_count_); |
| 867 | expandBufAdd4BE(reply, monitor_info.waiters_.size()); |
| 868 | for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) { |
| Nicolas Geoffray | cafa081 | 2017-02-15 18:27:34 +0000 | [diff] [blame] | 869 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeerFromOtherThread())); |
| Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 870 | } |
| 871 | return JDWP::ERR_NONE; |
| 872 | } |
| 873 | |
| Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 874 | JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id, |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 875 | std::vector<JDWP::ObjectId>* monitors, |
| 876 | std::vector<uint32_t>* stack_depths) { |
| Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 877 | struct OwnedMonitorVisitor : public StackVisitor { |
| Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 878 | OwnedMonitorVisitor(Thread* thread, Context* context, |
| Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 879 | std::vector<JDWP::ObjectId>* monitor_vector, |
| Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 880 | std::vector<uint32_t>* stack_depth_vector) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 881 | REQUIRES_SHARED(Locks::mutator_lock_) |
| Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 882 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 883 | current_stack_depth(0), |
| 884 | monitors(monitor_vector), |
| 885 | stack_depths(stack_depth_vector) {} |
| Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 886 | |
| 887 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 888 | // annotalysis. |
| Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 889 | bool VisitFrame() override NO_THREAD_SAFETY_ANALYSIS { |
| Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 890 | if (!GetMethod()->IsRuntimeMethod()) { |
| 891 | Monitor::VisitLocks(this, AppendOwnedMonitors, this); |
| Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 892 | ++current_stack_depth; |
| Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 893 | } |
| 894 | return true; |
| 895 | } |
| 896 | |
| Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 897 | static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 898 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 899 | OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg); |
| Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 900 | visitor->monitors->push_back(gRegistry->Add(owned_monitor)); |
| Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 901 | visitor->stack_depths->push_back(visitor->current_stack_depth); |
| Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 902 | } |
| 903 | |
| Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 904 | size_t current_stack_depth; |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 905 | std::vector<JDWP::ObjectId>* const monitors; |
| 906 | std::vector<uint32_t>* const stack_depths; |
| Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 907 | }; |
| Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 908 | |
| Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 909 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 910 | JDWP::JdwpError error; |
| 911 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 912 | if (thread == nullptr) { |
| 913 | return error; |
| 914 | } |
| 915 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 916 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 917 | } |
| Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 918 | std::unique_ptr<Context> context(Context::Create()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 919 | OwnedMonitorVisitor visitor(thread, context.get(), monitors, stack_depths); |
| Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 920 | visitor.WalkStack(); |
| Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 921 | return JDWP::ERR_NONE; |
| 922 | } |
| 923 | |
| Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 924 | JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 925 | JDWP::ObjectId* contended_monitor) { |
| Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 926 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 927 | *contended_monitor = 0; |
| Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 928 | JDWP::JdwpError error; |
| 929 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 930 | if (thread == nullptr) { |
| 931 | return error; |
| Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 932 | } |
| Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 933 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 934 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 935 | } |
| 936 | mirror::Object* contended_monitor_obj = Monitor::GetContendedMonitor(thread); |
| Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 937 | // Add() requires the thread_list_lock_ not held to avoid the lock |
| 938 | // level violation. |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 939 | *contended_monitor = gRegistry->Add(contended_monitor_obj); |
| Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 940 | return JDWP::ERR_NONE; |
| 941 | } |
| 942 | |
| Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 943 | JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids, |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 944 | std::vector<uint64_t>* counts) { |
| Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 945 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| Roland Levillain | af29031 | 2018-02-27 20:02:17 +0000 | [diff] [blame] | 946 | heap->CollectGarbage(/* clear_soft_references */ false, gc::GcCause::kGcCauseDebugger); |
| Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 947 | VariableSizedHandleScope hs(Thread::Current()); |
| Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 948 | std::vector<Handle<mirror::Class>> classes; |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 949 | counts->clear(); |
| Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 950 | for (size_t i = 0; i < class_ids.size(); ++i) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 951 | JDWP::JdwpError error; |
| Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 952 | ObjPtr<mirror::Class> c = DecodeClass(class_ids[i], &error); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 953 | if (c == nullptr) { |
| 954 | return error; |
| Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 955 | } |
| Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 956 | classes.push_back(hs.NewHandle(c)); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 957 | counts->push_back(0); |
| Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 958 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 959 | heap->CountInstances(classes, false, &(*counts)[0]); |
| Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 960 | return JDWP::ERR_NONE; |
| 961 | } |
| 962 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 963 | JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, |
| 964 | std::vector<JDWP::ObjectId>* instances) { |
| Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 965 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 966 | // We only want reachable instances, so do a GC. |
| Roland Levillain | af29031 | 2018-02-27 20:02:17 +0000 | [diff] [blame] | 967 | heap->CollectGarbage(/* clear_soft_references */ false, gc::GcCause::kGcCauseDebugger); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 968 | JDWP::JdwpError error; |
| Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 969 | ObjPtr<mirror::Class> c = DecodeClass(class_id, &error); |
| Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 970 | if (c == nullptr) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 971 | return error; |
| Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 972 | } |
| Mathieu Chartier | 2d85595 | 2016-10-12 19:37:59 -0700 | [diff] [blame] | 973 | VariableSizedHandleScope hs(Thread::Current()); |
| 974 | std::vector<Handle<mirror::Object>> raw_instances; |
| Richard Uhler | 660be6f | 2017-11-22 16:12:29 +0000 | [diff] [blame] | 975 | Runtime::Current()->GetHeap()->GetInstances(hs, |
| 976 | hs.NewHandle(c), |
| 977 | /* use_is_assignable_from */ false, |
| 978 | max_count, |
| 979 | raw_instances); |
| Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 980 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
| Mathieu Chartier | 2d85595 | 2016-10-12 19:37:59 -0700 | [diff] [blame] | 981 | instances->push_back(gRegistry->Add(raw_instances[i].Get())); |
| Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 982 | } |
| 983 | return JDWP::ERR_NONE; |
| 984 | } |
| 985 | |
| Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 986 | JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count, |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 987 | std::vector<JDWP::ObjectId>* referring_objects) { |
| Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 988 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| Roland Levillain | af29031 | 2018-02-27 20:02:17 +0000 | [diff] [blame] | 989 | heap->CollectGarbage(/* clear_soft_references */ false, gc::GcCause::kGcCauseDebugger); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 990 | JDWP::JdwpError error; |
| Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 991 | ObjPtr<mirror::Object> o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 992 | if (o == nullptr) { |
| Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 993 | return JDWP::ERR_INVALID_OBJECT; |
| 994 | } |
| Mathieu Chartier | aea9bfb | 2016-10-12 19:19:56 -0700 | [diff] [blame] | 995 | VariableSizedHandleScope hs(Thread::Current()); |
| 996 | std::vector<Handle<mirror::Object>> raw_instances; |
| 997 | heap->GetReferringObjects(hs, hs.NewHandle(o), max_count, raw_instances); |
| Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 998 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
| Mathieu Chartier | aea9bfb | 2016-10-12 19:19:56 -0700 | [diff] [blame] | 999 | referring_objects->push_back(gRegistry->Add(raw_instances[i].Get())); |
| Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 1000 | } |
| 1001 | return JDWP::ERR_NONE; |
| 1002 | } |
| 1003 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1004 | JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id) { |
| 1005 | JDWP::JdwpError error; |
| 1006 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1007 | if (o == nullptr) { |
| Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1008 | return JDWP::ERR_INVALID_OBJECT; |
| 1009 | } |
| Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1010 | gRegistry->DisableCollection(object_id); |
| 1011 | return JDWP::ERR_NONE; |
| 1012 | } |
| 1013 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1014 | JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id) { |
| 1015 | JDWP::JdwpError error; |
| 1016 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1017 | // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI |
| 1018 | // also ignores these cases and never return an error. However it's not obvious why this command |
| 1019 | // should behave differently from DisableCollection and IsCollected commands. So let's be more |
| 1020 | // strict and return an error if this happens. |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1021 | if (o == nullptr) { |
| Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1022 | return JDWP::ERR_INVALID_OBJECT; |
| 1023 | } |
| Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1024 | gRegistry->EnableCollection(object_id); |
| 1025 | return JDWP::ERR_NONE; |
| 1026 | } |
| 1027 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1028 | JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool* is_collected) { |
| 1029 | *is_collected = true; |
| Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 1030 | if (object_id == 0) { |
| 1031 | // Null object id is invalid. |
| Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 1032 | return JDWP::ERR_INVALID_OBJECT; |
| 1033 | } |
| Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 1034 | // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However |
| 1035 | // the RI seems to ignore this and assume object has been collected. |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1036 | JDWP::JdwpError error; |
| 1037 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1038 | if (o != nullptr) { |
| 1039 | *is_collected = gRegistry->IsCollected(object_id); |
| Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 1040 | } |
| Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1041 | return JDWP::ERR_NONE; |
| 1042 | } |
| 1043 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1044 | void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) { |
| Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1045 | gRegistry->DisposeObject(object_id, reference_count); |
| 1046 | } |
| 1047 | |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1048 | JDWP::JdwpTypeTag Dbg::GetTypeTag(ObjPtr<mirror::Class> klass) { |
| Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1049 | DCHECK(klass != nullptr); |
| 1050 | if (klass->IsArrayClass()) { |
| 1051 | return JDWP::TT_ARRAY; |
| 1052 | } else if (klass->IsInterface()) { |
| 1053 | return JDWP::TT_INTERFACE; |
| 1054 | } else { |
| 1055 | return JDWP::TT_CLASS; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1059 | JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1060 | JDWP::JdwpError error; |
| 1061 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1062 | if (c == nullptr) { |
| 1063 | return error; |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1064 | } |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1065 | |
| Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1066 | JDWP::JdwpTypeTag type_tag = GetTypeTag(c); |
| 1067 | expandBufAdd1(pReply, type_tag); |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1068 | expandBufAddRefTypeId(pReply, class_id); |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1069 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
| Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1072 | // Get the complete list of reference classes (i.e. all classes except |
| 1073 | // the primitive types). |
| 1074 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 1075 | class ClassListCreator : public ClassVisitor { |
| 1076 | public: |
| 1077 | explicit ClassListCreator(std::vector<JDWP::RefTypeId>* classes) : classes_(classes) {} |
| 1078 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1079 | bool operator()(ObjPtr<mirror::Class> c) override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1080 | if (!c->IsPrimitive()) { |
| 1081 | classes_->push_back(Dbg::GetObjectRegistry()->AddRefType(c)); |
| 1082 | } |
| 1083 | return true; |
| 1084 | } |
| 1085 | |
| 1086 | private: |
| 1087 | std::vector<JDWP::RefTypeId>* const classes_; |
| 1088 | }; |
| 1089 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1090 | void Dbg::GetClassList(std::vector<JDWP::RefTypeId>* classes) { |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1091 | ClassListCreator clc(classes); |
| Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1092 | Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(&clc); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1095 | JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, |
| 1096 | uint32_t* pStatus, std::string* pDescriptor) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1097 | JDWP::JdwpError error; |
| 1098 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1099 | if (c == nullptr) { |
| 1100 | return error; |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1103 | if (c->IsArrayClass()) { |
| 1104 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 1105 | *pTypeTag = JDWP::TT_ARRAY; |
| 1106 | } else { |
| 1107 | if (c->IsErroneous()) { |
| 1108 | *pStatus = JDWP::CS_ERROR; |
| 1109 | } else { |
| 1110 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 1111 | } |
| 1112 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1113 | } |
| 1114 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1115 | if (pDescriptor != nullptr) { |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1116 | std::string temp; |
| 1117 | *pDescriptor = c->GetDescriptor(&temp); |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1118 | } |
| 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 | void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>* ids) { |
| Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1123 | std::vector<ObjPtr<mirror::Class>> classes; |
| Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1124 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1125 | ids->clear(); |
| Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1126 | for (ObjPtr<mirror::Class> c : classes) { |
| 1127 | ids->push_back(gRegistry->Add(c)); |
| Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1128 | } |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1131 | JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) { |
| 1132 | JDWP::JdwpError error; |
| 1133 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1134 | if (o == nullptr) { |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1135 | return JDWP::ERR_INVALID_OBJECT; |
| Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1136 | } |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1137 | |
| Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1138 | JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass()); |
| Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1139 | JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass()); |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1140 | |
| 1141 | expandBufAdd1(pReply, type_tag); |
| 1142 | expandBufAddRefTypeId(pReply, type_id); |
| 1143 | |
| 1144 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 1147 | JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1148 | JDWP::JdwpError error; |
| 1149 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1150 | if (c == nullptr) { |
| 1151 | return error; |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1152 | } |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1153 | std::string temp; |
| 1154 | *signature = c->GetDescriptor(&temp); |
| Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 1155 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| Orion Hodson | 77d8a1c | 2017-04-24 14:53:19 +0100 | [diff] [blame] | 1158 | JDWP::JdwpError Dbg::GetSourceDebugExtension(JDWP::RefTypeId class_id, |
| 1159 | std::string* extension_data) { |
| 1160 | JDWP::JdwpError error; |
| 1161 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1162 | if (c == nullptr) { |
| 1163 | return error; |
| 1164 | } |
| 1165 | StackHandleScope<1> hs(Thread::Current()); |
| 1166 | Handle<mirror::Class> klass(hs.NewHandle(c)); |
| 1167 | const char* data = annotations::GetSourceDebugExtension(klass); |
| 1168 | if (data == nullptr) { |
| 1169 | return JDWP::ERR_ABSENT_INFORMATION; |
| 1170 | } |
| 1171 | *extension_data = data; |
| 1172 | return JDWP::ERR_NONE; |
| 1173 | } |
| 1174 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1175 | JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string* result) { |
| 1176 | JDWP::JdwpError error; |
| 1177 | mirror::Class* c = DecodeClass(class_id, &error); |
| Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1178 | if (c == nullptr) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1179 | return error; |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1180 | } |
| Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1181 | const char* source_file = c->GetSourceFile(); |
| 1182 | if (source_file == nullptr) { |
| Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 1183 | return JDWP::ERR_ABSENT_INFORMATION; |
| 1184 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1185 | *result = source_file; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1186 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1189 | JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t* tag) { |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1190 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1191 | JDWP::JdwpError error; |
| 1192 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1193 | if (error != JDWP::ERR_NONE) { |
| 1194 | *tag = JDWP::JT_VOID; |
| 1195 | return error; |
| Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1196 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1197 | *tag = TagFromObject(soa, o); |
| Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1198 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1201 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1202 | switch (tag) { |
| 1203 | case JDWP::JT_VOID: |
| 1204 | return 0; |
| 1205 | case JDWP::JT_BYTE: |
| 1206 | case JDWP::JT_BOOLEAN: |
| 1207 | return 1; |
| 1208 | case JDWP::JT_CHAR: |
| 1209 | case JDWP::JT_SHORT: |
| 1210 | return 2; |
| 1211 | case JDWP::JT_FLOAT: |
| 1212 | case JDWP::JT_INT: |
| 1213 | return 4; |
| 1214 | case JDWP::JT_ARRAY: |
| 1215 | case JDWP::JT_OBJECT: |
| 1216 | case JDWP::JT_STRING: |
| 1217 | case JDWP::JT_THREAD: |
| 1218 | case JDWP::JT_THREAD_GROUP: |
| 1219 | case JDWP::JT_CLASS_LOADER: |
| 1220 | case JDWP::JT_CLASS_OBJECT: |
| 1221 | return sizeof(JDWP::ObjectId); |
| 1222 | case JDWP::JT_DOUBLE: |
| 1223 | case JDWP::JT_LONG: |
| 1224 | return 8; |
| 1225 | default: |
| Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1226 | LOG(FATAL) << "Unknown tag " << tag; |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1227 | return -1; |
| 1228 | } |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1231 | JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int32_t* length) { |
| 1232 | JDWP::JdwpError error; |
| 1233 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
| 1234 | if (a == nullptr) { |
| 1235 | return error; |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1236 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1237 | *length = a->GetLength(); |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1238 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1241 | 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] | 1242 | JDWP::JdwpError error; |
| 1243 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1244 | if (a == nullptr) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1245 | return error; |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1246 | } |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1247 | |
| 1248 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 1249 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1250 | return JDWP::ERR_INVALID_LENGTH; |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1251 | } |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1252 | JDWP::JdwpTag element_tag = BasicTagFromClass(a->GetClass()->GetComponentType()); |
| 1253 | expandBufAdd1(pReply, element_tag); |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1254 | expandBufAdd4BE(pReply, count); |
| 1255 | |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1256 | if (IsPrimitiveTag(element_tag)) { |
| 1257 | size_t width = GetTagWidth(element_tag); |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1258 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 1259 | if (width == 8) { |
| Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1260 | 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] | 1261 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 1262 | } else if (width == 4) { |
| Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1263 | 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] | 1264 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 1265 | } else if (width == 2) { |
| Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1266 | 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] | 1267 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 1268 | } else { |
| Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1269 | 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] | 1270 | memcpy(dst, &src[offset * width], count * width); |
| 1271 | } |
| 1272 | } else { |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1273 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1274 | mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>(); |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1275 | for (int i = 0; i < count; ++i) { |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1276 | mirror::Object* element = oa->Get(offset + i); |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1277 | JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element) |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1278 | : element_tag; |
| Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1279 | expandBufAdd1(pReply, specific_tag); |
| 1280 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 1281 | } |
| 1282 | } |
| 1283 | |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1284 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
| Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1287 | template <typename T> |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1288 | 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] | 1289 | NO_THREAD_SAFETY_ANALYSIS { |
| 1290 | // TODO: fix when annotalysis correctly handles non-member functions. |
| Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1291 | DCHECK(a->GetClass()->IsPrimitiveArray()); |
| 1292 | |
| Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1293 | T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset)); |
| Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1294 | for (int i = 0; i < count; ++i) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1295 | *dst++ = src->ReadValue(sizeof(T)); |
| Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1299 | JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count, |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1300 | JDWP::Request* request) { |
| 1301 | JDWP::JdwpError error; |
| 1302 | mirror::Array* dst = DecodeNonNullArray(array_id, &error); |
| 1303 | if (dst == nullptr) { |
| 1304 | return error; |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1305 | } |
| Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1306 | |
| Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1307 | if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) { |
| Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1308 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1309 | return JDWP::ERR_INVALID_LENGTH; |
| Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1310 | } |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1311 | JDWP::JdwpTag element_tag = BasicTagFromClass(dst->GetClass()->GetComponentType()); |
| Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1312 | |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1313 | if (IsPrimitiveTag(element_tag)) { |
| 1314 | size_t width = GetTagWidth(element_tag); |
| Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1315 | if (width == 8) { |
| Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1316 | CopyArrayData<uint64_t>(dst, request, offset, count); |
| Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1317 | } else if (width == 4) { |
| Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1318 | CopyArrayData<uint32_t>(dst, request, offset, count); |
| Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1319 | } else if (width == 2) { |
| Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1320 | CopyArrayData<uint16_t>(dst, request, offset, count); |
| Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1321 | } else { |
| Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1322 | CopyArrayData<uint8_t>(dst, request, offset, count); |
| Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1323 | } |
| 1324 | } else { |
| Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1325 | mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>(); |
| Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1326 | for (int i = 0; i < count; ++i) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1327 | JDWP::ObjectId id = request->ReadObjectId(); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1328 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 1329 | if (error != JDWP::ERR_NONE) { |
| 1330 | return error; |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1331 | } |
| Sebastien Hertz | 2e1c16d | 2015-08-28 11:57:49 +0200 | [diff] [blame] | 1332 | // Check if the object's type is compatible with the array's type. |
| 1333 | if (o != nullptr && !o->InstanceOf(oa->GetClass()->GetComponentType())) { |
| 1334 | return JDWP::ERR_TYPE_MISMATCH; |
| 1335 | } |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1336 | oa->Set<false>(offset + i, o); |
| Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1337 | } |
| 1338 | } |
| 1339 | |
| Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1340 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
| Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1343 | JDWP::JdwpError Dbg::CreateString(const std::string& str, JDWP::ObjectId* new_string_id) { |
| 1344 | Thread* self = Thread::Current(); |
| 1345 | mirror::String* new_string = mirror::String::AllocFromModifiedUtf8(self, str.c_str()); |
| 1346 | if (new_string == nullptr) { |
| 1347 | DCHECK(self->IsExceptionPending()); |
| 1348 | self->ClearException(); |
| 1349 | LOG(ERROR) << "Could not allocate string"; |
| 1350 | *new_string_id = 0; |
| 1351 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1352 | } |
| 1353 | *new_string_id = gRegistry->Add(new_string); |
| 1354 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1357 | 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] | 1358 | JDWP::JdwpError error; |
| 1359 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1360 | if (c == nullptr) { |
| Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1361 | *new_object_id = 0; |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1362 | return error; |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1363 | } |
| Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1364 | Thread* self = Thread::Current(); |
| Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1365 | ObjPtr<mirror::Object> new_object; |
| Sebastien Hertz | 56d5e50 | 2015-11-03 17:38:35 +0100 | [diff] [blame] | 1366 | if (c->IsStringClass()) { |
| 1367 | // Special case for java.lang.String. |
| 1368 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
| jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 1369 | new_object = mirror::String::AllocEmptyString<true>(self, allocator_type); |
| Sebastien Hertz | 56d5e50 | 2015-11-03 17:38:35 +0100 | [diff] [blame] | 1370 | } else { |
| 1371 | new_object = c->AllocObject(self); |
| 1372 | } |
| Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1373 | if (new_object == nullptr) { |
| 1374 | DCHECK(self->IsExceptionPending()); |
| 1375 | self->ClearException(); |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1376 | LOG(ERROR) << "Could not allocate object of type " << mirror::Class::PrettyDescriptor(c); |
| Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1377 | *new_object_id = 0; |
| 1378 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1379 | } |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 1380 | *new_object_id = gRegistry->Add(new_object); |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1381 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
| Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 1384 | /* |
| 1385 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 1386 | */ |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1387 | JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length, |
| Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1388 | JDWP::ObjectId* new_array_id) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1389 | JDWP::JdwpError error; |
| 1390 | mirror::Class* c = DecodeClass(array_class_id, &error); |
| 1391 | if (c == nullptr) { |
| Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1392 | *new_array_id = 0; |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1393 | return error; |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1394 | } |
| Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1395 | Thread* self = Thread::Current(); |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 1396 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
| 1397 | ObjPtr<mirror::Array> new_array = |
| 1398 | mirror::Array::Alloc<true>(self, c, length, c->GetComponentSizeShift(), allocator_type); |
| Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1399 | if (new_array == nullptr) { |
| 1400 | DCHECK(self->IsExceptionPending()); |
| 1401 | self->ClearException(); |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1402 | LOG(ERROR) << "Could not allocate array of type " << mirror::Class::PrettyDescriptor(c); |
| Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1403 | *new_array_id = 0; |
| 1404 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1405 | } |
| 1406 | *new_array_id = gRegistry->Add(new_array); |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1407 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1410 | JDWP::FieldId Dbg::ToFieldId(const ArtField* f) { |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1411 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1412 | } |
| 1413 | |
| Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 1414 | static JDWP::MethodId ToMethodId(ArtMethod* m) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1415 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Alex Light | 97e7803 | 2017-06-27 17:51:55 -0700 | [diff] [blame] | 1416 | return static_cast<JDWP::MethodId>( |
| 1417 | reinterpret_cast<uintptr_t>(m->GetCanonicalMethod(kRuntimePointerSize))); |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1420 | static ArtField* FromFieldId(JDWP::FieldId fid) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1421 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1422 | return reinterpret_cast<ArtField*>(static_cast<uintptr_t>(fid)); |
| Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1423 | } |
| 1424 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1425 | static ArtMethod* FromMethodId(JDWP::MethodId mid) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1426 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1427 | return reinterpret_cast<ArtMethod*>(static_cast<uintptr_t>(mid)); |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1428 | } |
| 1429 | |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1430 | bool Dbg::MatchThread(JDWP::ObjectId expected_thread_id, Thread* event_thread) { |
| 1431 | CHECK(event_thread != nullptr); |
| 1432 | JDWP::JdwpError error; |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1433 | mirror::Object* expected_thread_peer = gRegistry->Get<mirror::Object*>( |
| 1434 | expected_thread_id, &error); |
| Nicolas Geoffray | cafa081 | 2017-02-15 18:27:34 +0000 | [diff] [blame] | 1435 | return expected_thread_peer == event_thread->GetPeerFromOtherThread(); |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | bool Dbg::MatchLocation(const JDWP::JdwpLocation& expected_location, |
| 1439 | const JDWP::EventLocation& event_location) { |
| 1440 | if (expected_location.dex_pc != event_location.dex_pc) { |
| 1441 | return false; |
| 1442 | } |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1443 | ArtMethod* m = FromMethodId(expected_location.method_id); |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1444 | return m == event_location.method; |
| 1445 | } |
| 1446 | |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1447 | bool Dbg::MatchType(ObjPtr<mirror::Class> event_class, JDWP::RefTypeId class_id) { |
| Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1448 | if (event_class == nullptr) { |
| 1449 | return false; |
| 1450 | } |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1451 | JDWP::JdwpError error; |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1452 | ObjPtr<mirror::Class> expected_class = DecodeClass(class_id, &error); |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1453 | CHECK(expected_class != nullptr); |
| 1454 | return expected_class->IsAssignableFrom(event_class); |
| 1455 | } |
| 1456 | |
| 1457 | 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] | 1458 | ArtField* event_field) { |
| 1459 | ArtField* expected_field = FromFieldId(expected_field_id); |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1460 | if (expected_field != event_field) { |
| 1461 | return false; |
| 1462 | } |
| 1463 | return Dbg::MatchType(event_field->GetDeclaringClass(), expected_type_id); |
| 1464 | } |
| 1465 | |
| 1466 | bool Dbg::MatchInstance(JDWP::ObjectId expected_instance_id, mirror::Object* event_instance) { |
| 1467 | JDWP::JdwpError error; |
| 1468 | mirror::Object* modifier_instance = gRegistry->Get<mirror::Object*>(expected_instance_id, &error); |
| 1469 | return modifier_instance == event_instance; |
| 1470 | } |
| 1471 | |
| Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1472 | void Dbg::SetJdwpLocation(JDWP::JdwpLocation* location, ArtMethod* m, uint32_t dex_pc) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1473 | if (m == nullptr) { |
| Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1474 | memset(location, 0, sizeof(*location)); |
| Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1475 | } else { |
| Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1476 | ObjPtr<mirror::Class> c = m->GetDeclaringClass(); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1477 | location->type_tag = GetTypeTag(c); |
| 1478 | location->class_id = gRegistry->AddRefType(c); |
| Alex Light | 7337631 | 2017-04-06 10:10:51 -0700 | [diff] [blame] | 1479 | // The RI Seems to return 0 for all obsolete methods. For compatibility we shall do the same. |
| 1480 | location->method_id = m->IsObsolete() ? 0 : ToMethodId(m); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1481 | 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] | 1482 | } |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1483 | } |
| 1484 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1485 | std::string Dbg::GetMethodName(JDWP::MethodId method_id) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1486 | ArtMethod* m = FromMethodId(method_id); |
| Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1487 | if (m == nullptr) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1488 | return "null"; |
| Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1489 | } |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1490 | return m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetName(); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
| Alex Light | 7337631 | 2017-04-06 10:10:51 -0700 | [diff] [blame] | 1493 | bool Dbg::IsMethodObsolete(JDWP::MethodId method_id) { |
| 1494 | ArtMethod* m = FromMethodId(method_id); |
| 1495 | if (m == nullptr) { |
| 1496 | // NB Since we return 0 as MID for obsolete methods we want to default to true here. |
| 1497 | return true; |
| 1498 | } |
| 1499 | return m->IsObsolete(); |
| 1500 | } |
| 1501 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1502 | std::string Dbg::GetFieldName(JDWP::FieldId field_id) { |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1503 | ArtField* f = FromFieldId(field_id); |
| Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1504 | if (f == nullptr) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1505 | return "null"; |
| Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1506 | } |
| 1507 | return f->GetName(); |
| Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1508 | } |
| 1509 | |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1510 | /* |
| 1511 | * Augment the access flags for synthetic methods and fields by setting |
| 1512 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 1513 | * flags not specified by the Java programming language. |
| 1514 | */ |
| 1515 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 1516 | accessFlags &= kAccJavaFlagsMask; |
| 1517 | if ((accessFlags & kAccSynthetic) != 0) { |
| 1518 | accessFlags |= 0xf0000000; |
| 1519 | } |
| 1520 | return accessFlags; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1523 | /* |
| Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1524 | * Circularly shifts registers so that arguments come first. Debuggers |
| 1525 | * expect slots to begin with arguments, but dex code places them at |
| 1526 | * the end. |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1527 | */ |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1528 | static uint16_t MangleSlot(uint16_t slot, ArtMethod* m) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1529 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 1530 | CodeItemDataAccessor accessor(m->DexInstructionData()); |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1531 | if (!accessor.HasCodeItem()) { |
| Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1532 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1533 | // return the slot as is since all registers are arguments. |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1534 | LOG(WARNING) << "Trying to mangle slot for method without code " << m->PrettyMethod(); |
| Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1535 | return slot; |
| 1536 | } |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1537 | uint16_t ins_size = accessor.InsSize(); |
| 1538 | uint16_t locals_size = accessor.RegistersSize() - ins_size; |
| Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1539 | if (slot >= locals_size) { |
| 1540 | return slot - locals_size; |
| 1541 | } else { |
| 1542 | return slot + ins_size; |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1543 | } |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
| Sebastien Hertz | aef0c91 | 2016-08-08 10:20:28 +0200 | [diff] [blame] | 1546 | static size_t GetMethodNumArgRegistersIncludingThis(ArtMethod* method) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1547 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | aef0c91 | 2016-08-08 10:20:28 +0200 | [diff] [blame] | 1548 | uint32_t num_registers = ArtMethod::NumArgRegisters(method->GetShorty()); |
| 1549 | if (!method->IsStatic()) { |
| 1550 | ++num_registers; |
| 1551 | } |
| 1552 | return num_registers; |
| 1553 | } |
| 1554 | |
| Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1555 | /* |
| 1556 | * Circularly shifts registers so that arguments come last. Reverts |
| 1557 | * slots to dex style argument placement. |
| 1558 | */ |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1559 | static uint16_t DemangleSlot(uint16_t slot, ArtMethod* m, JDWP::JdwpError* error) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1560 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 1561 | CodeItemDataAccessor accessor(m->DexInstructionData()); |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1562 | if (!accessor.HasCodeItem()) { |
| Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1563 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1564 | // return the slot as is since all registers are arguments. |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1565 | LOG(WARNING) << "Trying to demangle slot for method without code " |
| 1566 | << m->PrettyMethod(); |
| Sebastien Hertz | aef0c91 | 2016-08-08 10:20:28 +0200 | [diff] [blame] | 1567 | uint16_t vreg_count = GetMethodNumArgRegistersIncludingThis(m); |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1568 | if (slot < vreg_count) { |
| 1569 | *error = JDWP::ERR_NONE; |
| 1570 | return slot; |
| 1571 | } |
| Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1572 | } else { |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1573 | if (slot < accessor.RegistersSize()) { |
| 1574 | uint16_t ins_size = accessor.InsSize(); |
| 1575 | uint16_t locals_size = accessor.RegistersSize() - ins_size; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1576 | *error = JDWP::ERR_NONE; |
| 1577 | return (slot < ins_size) ? slot + locals_size : slot - ins_size; |
| 1578 | } |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1579 | } |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1580 | |
| 1581 | // Slot is invalid in the method. |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1582 | LOG(ERROR) << "Invalid local slot " << slot << " for method " << m->PrettyMethod(); |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1583 | *error = JDWP::ERR_INVALID_SLOT; |
| 1584 | return DexFile::kDexNoIndex16; |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
| Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1587 | JDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, |
| 1588 | JDWP::ExpandBuf* pReply) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1589 | JDWP::JdwpError error; |
| 1590 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1591 | if (c == nullptr) { |
| 1592 | return error; |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1593 | } |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1594 | |
| 1595 | size_t instance_field_count = c->NumInstanceFields(); |
| 1596 | size_t static_field_count = c->NumStaticFields(); |
| 1597 | |
| 1598 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 1599 | |
| 1600 | 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] | 1601 | ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : |
| 1602 | c->GetStaticField(i - instance_field_count); |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1603 | expandBufAddFieldId(pReply, ToFieldId(f)); |
| Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1604 | expandBufAddUtf8String(pReply, f->GetName()); |
| 1605 | expandBufAddUtf8String(pReply, f->GetTypeDescriptor()); |
| Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1606 | if (with_generic) { |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1607 | static const char genericSignature[1] = ""; |
| 1608 | expandBufAddUtf8String(pReply, genericSignature); |
| 1609 | } |
| 1610 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 1611 | } |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1612 | return JDWP::ERR_NONE; |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1613 | } |
| 1614 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1615 | JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic, |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1616 | JDWP::ExpandBuf* pReply) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1617 | JDWP::JdwpError error; |
| 1618 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1619 | if (c == nullptr) { |
| 1620 | return error; |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1621 | } |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1622 | |
| Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 1623 | expandBufAdd4BE(pReply, c->NumMethods()); |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1624 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1625 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 1626 | auto ptr_size = cl->GetImagePointerSize(); |
| Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 1627 | for (ArtMethod& m : c->GetMethods(ptr_size)) { |
| 1628 | expandBufAddMethodId(pReply, ToMethodId(&m)); |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1629 | expandBufAddUtf8String(pReply, m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetName()); |
| 1630 | expandBufAddUtf8String( |
| 1631 | pReply, m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetSignature().ToString()); |
| Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1632 | if (with_generic) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1633 | const char* generic_signature = ""; |
| 1634 | expandBufAddUtf8String(pReply, generic_signature); |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1635 | } |
| Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 1636 | expandBufAdd4BE(pReply, MangleAccessFlags(m.GetAccessFlags())); |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1637 | } |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1638 | return JDWP::ERR_NONE; |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1639 | } |
| 1640 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1641 | JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1642 | JDWP::JdwpError error; |
| Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1643 | Thread* self = Thread::Current(); |
| Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1644 | ObjPtr<mirror::Class> c = DecodeClass(class_id, &error); |
| 1645 | if (c == nullptr) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1646 | return error; |
| Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1647 | } |
| Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1648 | size_t interface_count = c->NumDirectInterfaces(); |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1649 | expandBufAdd4BE(pReply, interface_count); |
| 1650 | for (size_t i = 0; i < interface_count; ++i) { |
| Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1651 | ObjPtr<mirror::Class> interface = mirror::Class::GetDirectInterface(self, c, i); |
| 1652 | DCHECK(interface != nullptr); |
| 1653 | expandBufAddRefTypeId(pReply, gRegistry->AddRefType(interface)); |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1654 | } |
| Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1655 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1658 | void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1659 | ArtMethod* m = FromMethodId(method_id); |
| David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 1660 | CodeItemDebugInfoAccessor accessor(m->DexInstructionDebugInfo()); |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1661 | uint64_t start, end; |
| Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 1662 | if (!accessor.HasCodeItem()) { |
| Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1663 | DCHECK(m->IsNative() || m->IsProxyMethod()); |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1664 | start = -1; |
| 1665 | end = -1; |
| 1666 | } else { |
| 1667 | start = 0; |
| jeffhao | 14f0db9 | 2012-12-14 17:50:42 -0800 | [diff] [blame] | 1668 | // Return the index of the last instruction |
| Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 1669 | end = accessor.InsnsSizeInCodeUnits() - 1; |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | expandBufAdd8BE(pReply, start); |
| 1673 | expandBufAdd8BE(pReply, end); |
| 1674 | |
| 1675 | // Add numLines later |
| 1676 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 1677 | expandBufAdd4BE(pReply, 0); |
| 1678 | |
| Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 1679 | int numItems = 0; |
| 1680 | accessor.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) { |
| 1681 | expandBufAdd8BE(pReply, entry.address_); |
| 1682 | expandBufAdd4BE(pReply, entry.line_); |
| 1683 | numItems++; |
| 1684 | return false; |
| 1685 | }); |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1686 | |
| Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 1687 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, numItems); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1690 | void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, |
| 1691 | JDWP::ExpandBuf* pReply) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1692 | ArtMethod* m = FromMethodId(method_id); |
| David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 1693 | CodeItemDebugInfoAccessor accessor(m->DexInstructionDebugInfo()); |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1694 | |
| Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1695 | // arg_count considers doubles and longs to take 2 units. |
| 1696 | // variable_count considers everything to take 1 unit. |
| Sebastien Hertz | aef0c91 | 2016-08-08 10:20:28 +0200 | [diff] [blame] | 1697 | expandBufAdd4BE(pReply, GetMethodNumArgRegistersIncludingThis(m)); |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1698 | |
| Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1699 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1700 | size_t variable_count_offset = expandBufGetLength(pReply); |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1701 | expandBufAdd4BE(pReply, 0); |
| 1702 | |
| Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 1703 | size_t variable_count = 0; |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1704 | |
| Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 1705 | if (accessor.HasCodeItem()) { |
| Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 1706 | accessor.DecodeDebugLocalInfo(m->IsStatic(), |
| 1707 | m->GetDexMethodIndex(), |
| 1708 | [&](const DexFile::LocalInfo& entry) |
| 1709 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1710 | uint16_t slot = entry.reg_; |
| 1711 | VLOG(jdwp) << StringPrintf(" %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d", |
| 1712 | variable_count, |
| 1713 | entry.start_address_, |
| 1714 | entry.end_address_ - entry.start_address_, |
| 1715 | entry.name_, |
| 1716 | entry.descriptor_, entry.signature_, |
| 1717 | slot, |
| 1718 | MangleSlot(slot, m)); |
| 1719 | |
| 1720 | slot = MangleSlot(slot, m); |
| 1721 | |
| 1722 | expandBufAdd8BE(pReply, entry.start_address_); |
| 1723 | expandBufAddUtf8String(pReply, entry.name_); |
| 1724 | expandBufAddUtf8String(pReply, entry.descriptor_); |
| 1725 | if (with_generic) { |
| 1726 | expandBufAddUtf8String(pReply, entry.signature_); |
| 1727 | } |
| 1728 | expandBufAdd4BE(pReply, entry.end_address_- entry.start_address_); |
| 1729 | expandBufAdd4BE(pReply, slot); |
| 1730 | |
| 1731 | ++variable_count; |
| 1732 | }); |
| Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1733 | } |
| Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1734 | |
| Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 1735 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, variable_count); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
| Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1738 | void Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value, |
| 1739 | JDWP::ExpandBuf* pReply) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1740 | ArtMethod* m = FromMethodId(method_id); |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1741 | JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty()); |
| Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1742 | OutputJValue(tag, return_value, pReply); |
| 1743 | } |
| 1744 | |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1745 | void Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value, |
| 1746 | JDWP::ExpandBuf* pReply) { |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1747 | ArtField* f = FromFieldId(field_id); |
| Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1748 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1749 | OutputJValue(tag, field_value, pReply); |
| 1750 | } |
| 1751 | |
| Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1752 | JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id, |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1753 | std::vector<uint8_t>* bytecodes) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1754 | ArtMethod* m = FromMethodId(method_id); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1755 | if (m == nullptr) { |
| Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1756 | return JDWP::ERR_INVALID_METHODID; |
| 1757 | } |
| David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 1758 | CodeItemDataAccessor accessor(m->DexInstructionData()); |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1759 | size_t byte_count = accessor.InsnsSizeInCodeUnits() * 2; |
| 1760 | const uint8_t* begin = reinterpret_cast<const uint8_t*>(accessor.Insns()); |
| Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1761 | const uint8_t* end = begin + byte_count; |
| 1762 | for (const uint8_t* p = begin; p != end; ++p) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1763 | bytecodes->push_back(*p); |
| Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1764 | } |
| 1765 | return JDWP::ERR_NONE; |
| 1766 | } |
| 1767 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1768 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) { |
| Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1769 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1772 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) { |
| Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1773 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
| Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1776 | static JValue GetArtFieldValue(ArtField* f, mirror::Object* o) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1777 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1778 | Primitive::Type fieldType = f->GetTypeAsPrimitiveType(); |
| 1779 | JValue field_value; |
| 1780 | switch (fieldType) { |
| 1781 | case Primitive::kPrimBoolean: |
| 1782 | field_value.SetZ(f->GetBoolean(o)); |
| 1783 | return field_value; |
| 1784 | |
| 1785 | case Primitive::kPrimByte: |
| 1786 | field_value.SetB(f->GetByte(o)); |
| 1787 | return field_value; |
| 1788 | |
| 1789 | case Primitive::kPrimChar: |
| 1790 | field_value.SetC(f->GetChar(o)); |
| 1791 | return field_value; |
| 1792 | |
| 1793 | case Primitive::kPrimShort: |
| 1794 | field_value.SetS(f->GetShort(o)); |
| 1795 | return field_value; |
| 1796 | |
| 1797 | case Primitive::kPrimInt: |
| 1798 | case Primitive::kPrimFloat: |
| 1799 | // Int and Float must be treated as 32-bit values in JDWP. |
| 1800 | field_value.SetI(f->GetInt(o)); |
| 1801 | return field_value; |
| 1802 | |
| 1803 | case Primitive::kPrimLong: |
| 1804 | case Primitive::kPrimDouble: |
| 1805 | // Long and Double must be treated as 64-bit values in JDWP. |
| 1806 | field_value.SetJ(f->GetLong(o)); |
| 1807 | return field_value; |
| 1808 | |
| 1809 | case Primitive::kPrimNot: |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 1810 | field_value.SetL(f->GetObject(o)); |
| Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1811 | return field_value; |
| 1812 | |
| 1813 | case Primitive::kPrimVoid: |
| 1814 | LOG(FATAL) << "Attempt to read from field of type 'void'"; |
| 1815 | UNREACHABLE(); |
| 1816 | } |
| 1817 | LOG(FATAL) << "Attempt to read from field of unknown type"; |
| 1818 | UNREACHABLE(); |
| 1819 | } |
| 1820 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1821 | static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id, |
| 1822 | JDWP::FieldId field_id, JDWP::ExpandBuf* pReply, |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1823 | bool is_static) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1824 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1825 | JDWP::JdwpError error; |
| 1826 | mirror::Class* c = DecodeClass(ref_type_id, &error); |
| 1827 | if (ref_type_id != 0 && c == nullptr) { |
| 1828 | return error; |
| Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1829 | } |
| 1830 | |
| Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1831 | Thread* self = Thread::Current(); |
| 1832 | StackHandleScope<2> hs(self); |
| 1833 | MutableHandle<mirror::Object> |
| 1834 | o(hs.NewHandle(Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error))); |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1835 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
| Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1836 | return JDWP::ERR_INVALID_OBJECT; |
| 1837 | } |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1838 | ArtField* f = FromFieldId(field_id); |
| Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1839 | |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1840 | mirror::Class* receiver_class = c; |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1841 | if (receiver_class == nullptr && o != nullptr) { |
| Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1842 | receiver_class = o->GetClass(); |
| 1843 | } |
| Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1844 | |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1845 | // TODO: should we give up now if receiver_class is null? |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1846 | if (receiver_class != nullptr && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1847 | LOG(INFO) << "ERR_INVALID_FIELDID: " << f->PrettyField() << " " |
| 1848 | << receiver_class->PrettyClass(); |
| Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1849 | return JDWP::ERR_INVALID_FIELDID; |
| 1850 | } |
| Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1851 | |
| Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1852 | // Ensure the field's class is initialized. |
| 1853 | Handle<mirror::Class> klass(hs.NewHandle(f->GetDeclaringClass())); |
| 1854 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, klass, true, false)) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1855 | LOG(WARNING) << "Not able to initialize class for SetValues: " |
| 1856 | << mirror::Class::PrettyClass(klass.Get()); |
| Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
| Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1859 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1860 | // TODO: should we change the tests and check both? |
| 1861 | if (is_static) { |
| 1862 | if (!f->IsStatic()) { |
| 1863 | return JDWP::ERR_INVALID_FIELDID; |
| 1864 | } |
| 1865 | } else { |
| 1866 | if (f->IsStatic()) { |
| Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1867 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.GetValues" |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1868 | << " on static field " << f->PrettyField(); |
| Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1869 | } |
| 1870 | } |
| jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1871 | if (f->IsStatic()) { |
| Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1872 | o.Assign(f->GetDeclaringClass()); |
| jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1873 | } |
| Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1874 | |
| Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1875 | JValue field_value(GetArtFieldValue(f, o.Get())); |
| Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1876 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
| Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1877 | Dbg::OutputJValue(tag, &field_value, pReply); |
| Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1878 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1881 | JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1882 | JDWP::ExpandBuf* pReply) { |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1883 | return GetFieldValueImpl(0, object_id, field_id, pReply, false); |
| Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1884 | } |
| 1885 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1886 | JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, |
| 1887 | JDWP::ExpandBuf* pReply) { |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1888 | return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true); |
| Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1889 | } |
| 1890 | |
| Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1891 | 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] | 1892 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1893 | Primitive::Type fieldType = f->GetTypeAsPrimitiveType(); |
| 1894 | // Debugging only happens at runtime so we know we are not running in a transaction. |
| 1895 | static constexpr bool kNoTransactionMode = false; |
| 1896 | switch (fieldType) { |
| 1897 | case Primitive::kPrimBoolean: |
| 1898 | CHECK_EQ(width, 1); |
| 1899 | f->SetBoolean<kNoTransactionMode>(o, static_cast<uint8_t>(value)); |
| 1900 | return JDWP::ERR_NONE; |
| 1901 | |
| 1902 | case Primitive::kPrimByte: |
| 1903 | CHECK_EQ(width, 1); |
| 1904 | f->SetByte<kNoTransactionMode>(o, static_cast<uint8_t>(value)); |
| 1905 | return JDWP::ERR_NONE; |
| 1906 | |
| 1907 | case Primitive::kPrimChar: |
| 1908 | CHECK_EQ(width, 2); |
| 1909 | f->SetChar<kNoTransactionMode>(o, static_cast<uint16_t>(value)); |
| 1910 | return JDWP::ERR_NONE; |
| 1911 | |
| 1912 | case Primitive::kPrimShort: |
| 1913 | CHECK_EQ(width, 2); |
| 1914 | f->SetShort<kNoTransactionMode>(o, static_cast<int16_t>(value)); |
| 1915 | return JDWP::ERR_NONE; |
| 1916 | |
| 1917 | case Primitive::kPrimInt: |
| 1918 | case Primitive::kPrimFloat: |
| 1919 | CHECK_EQ(width, 4); |
| 1920 | // Int and Float must be treated as 32-bit values in JDWP. |
| 1921 | f->SetInt<kNoTransactionMode>(o, static_cast<int32_t>(value)); |
| 1922 | return JDWP::ERR_NONE; |
| 1923 | |
| 1924 | case Primitive::kPrimLong: |
| 1925 | case Primitive::kPrimDouble: |
| 1926 | CHECK_EQ(width, 8); |
| 1927 | // Long and Double must be treated as 64-bit values in JDWP. |
| 1928 | f->SetLong<kNoTransactionMode>(o, value); |
| 1929 | return JDWP::ERR_NONE; |
| 1930 | |
| 1931 | case Primitive::kPrimNot: { |
| 1932 | JDWP::JdwpError error; |
| 1933 | mirror::Object* v = Dbg::GetObjectRegistry()->Get<mirror::Object*>(value, &error); |
| 1934 | if (error != JDWP::ERR_NONE) { |
| 1935 | return JDWP::ERR_INVALID_OBJECT; |
| 1936 | } |
| 1937 | if (v != nullptr) { |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1938 | ObjPtr<mirror::Class> field_type; |
| Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1939 | { |
| 1940 | StackHandleScope<2> hs(Thread::Current()); |
| 1941 | HandleWrapper<mirror::Object> h_v(hs.NewHandleWrapper(&v)); |
| 1942 | HandleWrapper<mirror::Object> h_o(hs.NewHandleWrapper(&o)); |
| Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 1943 | field_type = f->ResolveType(); |
| Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1944 | } |
| 1945 | if (!field_type->IsAssignableFrom(v->GetClass())) { |
| 1946 | return JDWP::ERR_INVALID_OBJECT; |
| 1947 | } |
| 1948 | } |
| 1949 | f->SetObject<kNoTransactionMode>(o, v); |
| 1950 | return JDWP::ERR_NONE; |
| 1951 | } |
| 1952 | |
| 1953 | case Primitive::kPrimVoid: |
| 1954 | LOG(FATAL) << "Attempt to write to field of type 'void'"; |
| 1955 | UNREACHABLE(); |
| 1956 | } |
| 1957 | LOG(FATAL) << "Attempt to write to field of unknown type"; |
| 1958 | UNREACHABLE(); |
| 1959 | } |
| 1960 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1961 | static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1962 | uint64_t value, int width, bool is_static) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1963 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1964 | JDWP::JdwpError error; |
| Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1965 | Thread* self = Thread::Current(); |
| 1966 | StackHandleScope<2> hs(self); |
| 1967 | MutableHandle<mirror::Object> |
| 1968 | o(hs.NewHandle(Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error))); |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1969 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
| Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1970 | return JDWP::ERR_INVALID_OBJECT; |
| 1971 | } |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1972 | ArtField* f = FromFieldId(field_id); |
| Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1973 | |
| Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1974 | // Ensure the field's class is initialized. |
| 1975 | Handle<mirror::Class> klass(hs.NewHandle(f->GetDeclaringClass())); |
| 1976 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, klass, true, false)) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1977 | LOG(WARNING) << "Not able to initialize class for SetValues: " |
| 1978 | << mirror::Class::PrettyClass(klass.Get()); |
| Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
| Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1981 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1982 | // TODO: should we change the tests and check both? |
| 1983 | if (is_static) { |
| 1984 | if (!f->IsStatic()) { |
| 1985 | return JDWP::ERR_INVALID_FIELDID; |
| 1986 | } |
| 1987 | } else { |
| 1988 | if (f->IsStatic()) { |
| Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1989 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues" |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1990 | << " on static field " << f->PrettyField(); |
| Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1991 | } |
| Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1992 | } |
| jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1993 | if (f->IsStatic()) { |
| Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1994 | o.Assign(f->GetDeclaringClass()); |
| jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1995 | } |
| Jeff Hao | de19a25 | 2016-09-14 15:56:35 -0700 | [diff] [blame] | 1996 | return SetArtFieldValue(f, o.Get(), value, width); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1997 | } |
| 1998 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1999 | 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] | 2000 | int width) { |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 2001 | return SetFieldValueImpl(object_id, field_id, value, width, false); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
| Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 2004 | JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) { |
| 2005 | return SetFieldValueImpl(0, field_id, value, width, true); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
| Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 2008 | JDWP::JdwpError Dbg::StringToUtf8(JDWP::ObjectId string_id, std::string* str) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2009 | JDWP::JdwpError error; |
| Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 2010 | mirror::Object* obj = gRegistry->Get<mirror::Object*>(string_id, &error); |
| 2011 | if (error != JDWP::ERR_NONE) { |
| 2012 | return error; |
| 2013 | } |
| 2014 | if (obj == nullptr) { |
| 2015 | return JDWP::ERR_INVALID_OBJECT; |
| 2016 | } |
| 2017 | { |
| 2018 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 2019 | ObjPtr<mirror::Class> java_lang_String = |
| 2020 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_String); |
| Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 2021 | if (!java_lang_String->IsAssignableFrom(obj->GetClass())) { |
| 2022 | // This isn't a string. |
| 2023 | return JDWP::ERR_INVALID_STRING; |
| 2024 | } |
| 2025 | } |
| 2026 | *str = obj->AsString()->ToModifiedUtf8(); |
| 2027 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
| Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2030 | void Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) { |
| 2031 | if (IsPrimitiveTag(tag)) { |
| 2032 | expandBufAdd1(pReply, tag); |
| 2033 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 2034 | expandBufAdd1(pReply, return_value->GetI()); |
| 2035 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 2036 | expandBufAdd2BE(pReply, return_value->GetI()); |
| 2037 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 2038 | expandBufAdd4BE(pReply, return_value->GetI()); |
| 2039 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 2040 | expandBufAdd8BE(pReply, return_value->GetJ()); |
| 2041 | } else { |
| 2042 | CHECK_EQ(tag, JDWP::JT_VOID); |
| 2043 | } |
| 2044 | } else { |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2045 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2046 | mirror::Object* value = return_value->GetL(); |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2047 | expandBufAdd1(pReply, TagFromObject(soa, value)); |
| Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2048 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 2049 | } |
| 2050 | } |
| 2051 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2052 | JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string* name) { |
| jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2053 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2054 | JDWP::JdwpError error; |
| Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2055 | DecodeThread(soa, thread_id, &error); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2056 | if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2057 | return error; |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2058 | } |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2059 | |
| 2060 | // 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] | 2061 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2062 | CHECK(thread_object != nullptr) << error; |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2063 | ArtField* java_lang_Thread_name_field = |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2064 | jni::DecodeArtField(WellKnownClasses::java_lang_Thread_name); |
| Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 2065 | 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] | 2066 | if (s != nullptr) { |
| 2067 | *name = s->ToModifiedUtf8(); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2068 | } |
| 2069 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2072 | JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2073 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2074 | JDWP::JdwpError error; |
| 2075 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2076 | if (error != JDWP::ERR_NONE) { |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2077 | return JDWP::ERR_INVALID_OBJECT; |
| 2078 | } |
| Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2079 | ScopedAssertNoThreadSuspension ants("Debugger: GetThreadGroup"); |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2080 | // Okay, so it's an object, but is it actually a thread? |
| Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2081 | DecodeThread(soa, thread_id, &error); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2082 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2083 | // Zombie threads are in the null group. |
| 2084 | expandBufAddObjectId(pReply, JDWP::ObjectId(0)); |
| Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2085 | error = JDWP::ERR_NONE; |
| 2086 | } else if (error == JDWP::ERR_NONE) { |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 2087 | ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(WellKnownClasses::java_lang_Thread); |
| Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2088 | CHECK(c != nullptr); |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2089 | ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_group); |
| Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 2090 | CHECK(f != nullptr); |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2091 | ObjPtr<mirror::Object> group = f->GetObject(thread_object); |
| Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 2092 | CHECK(group != nullptr); |
| Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2093 | JDWP::ObjectId thread_group_id = gRegistry->Add(group); |
| 2094 | expandBufAddObjectId(pReply, thread_group_id); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2095 | } |
| Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2096 | return error; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2099 | static mirror::Object* DecodeThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 2100 | JDWP::ObjectId thread_group_id, JDWP::JdwpError* error) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2101 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2102 | mirror::Object* thread_group = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_group_id, |
| 2103 | error); |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2104 | if (*error != JDWP::ERR_NONE) { |
| 2105 | return nullptr; |
| 2106 | } |
| 2107 | if (thread_group == nullptr) { |
| 2108 | *error = JDWP::ERR_INVALID_OBJECT; |
| 2109 | return nullptr; |
| 2110 | } |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 2111 | ObjPtr<mirror::Class> c = |
| 2112 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_ThreadGroup); |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2113 | CHECK(c != nullptr); |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2114 | if (!c->IsAssignableFrom(thread_group->GetClass())) { |
| 2115 | // This is not a java.lang.ThreadGroup. |
| 2116 | *error = JDWP::ERR_INVALID_THREAD_GROUP; |
| 2117 | return nullptr; |
| 2118 | } |
| 2119 | *error = JDWP::ERR_NONE; |
| 2120 | return thread_group; |
| 2121 | } |
| 2122 | |
| 2123 | JDWP::JdwpError Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
| 2124 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 2125 | JDWP::JdwpError error; |
| 2126 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2127 | if (error != JDWP::ERR_NONE) { |
| 2128 | return error; |
| 2129 | } |
| Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2130 | ScopedAssertNoThreadSuspension ants("Debugger: GetThreadGroupName"); |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2131 | ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_name); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2132 | CHECK(f != nullptr); |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2133 | ObjPtr<mirror::String> s = f->GetObject(thread_group)->AsString(); |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2134 | |
| 2135 | std::string thread_group_name(s->ToModifiedUtf8()); |
| 2136 | expandBufAddUtf8String(pReply, thread_group_name); |
| 2137 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2138 | } |
| 2139 | |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2140 | JDWP::JdwpError Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2141 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2142 | JDWP::JdwpError error; |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2143 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2144 | if (error != JDWP::ERR_NONE) { |
| 2145 | return error; |
| 2146 | } |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2147 | ObjPtr<mirror::Object> parent; |
| Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2148 | { |
| Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2149 | ScopedAssertNoThreadSuspension ants("Debugger: GetThreadGroupParent"); |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2150 | ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_parent); |
| Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2151 | CHECK(f != nullptr); |
| 2152 | parent = f->GetObject(thread_group); |
| 2153 | } |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2154 | JDWP::ObjectId parent_group_id = gRegistry->Add(parent); |
| 2155 | expandBufAddObjectId(pReply, parent_group_id); |
| 2156 | return JDWP::ERR_NONE; |
| 2157 | } |
| 2158 | |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2159 | static void GetChildThreadGroups(mirror::Object* thread_group, |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2160 | std::vector<JDWP::ObjectId>* child_thread_group_ids) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2161 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2162 | CHECK(thread_group != nullptr); |
| 2163 | |
| Przemyslaw Szczepaniak | 464595f | 2015-11-24 11:59:59 +0000 | [diff] [blame] | 2164 | // Get the int "ngroups" count of this thread group... |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2165 | ArtField* ngroups_field = jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_ngroups); |
| Przemyslaw Szczepaniak | 464595f | 2015-11-24 11:59:59 +0000 | [diff] [blame] | 2166 | CHECK(ngroups_field != nullptr); |
| 2167 | const int32_t size = ngroups_field->GetInt(thread_group); |
| 2168 | if (size == 0) { |
| 2169 | return; |
| Sebastien Hertz | e49e195 | 2014-10-13 11:27:13 +0200 | [diff] [blame] | 2170 | } |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2171 | |
| Przemyslaw Szczepaniak | 464595f | 2015-11-24 11:59:59 +0000 | [diff] [blame] | 2172 | // Get the ThreadGroup[] "groups" out of this thread group... |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2173 | ArtField* groups_field = jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_groups); |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2174 | ObjPtr<mirror::Object> groups_array = groups_field->GetObject(thread_group); |
| Przemyslaw Szczepaniak | 464595f | 2015-11-24 11:59:59 +0000 | [diff] [blame] | 2175 | |
| 2176 | CHECK(groups_array != nullptr); |
| 2177 | CHECK(groups_array->IsObjectArray()); |
| 2178 | |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2179 | ObjPtr<mirror::ObjectArray<mirror::Object>> groups_array_as_array = |
| Przemyslaw Szczepaniak | 464595f | 2015-11-24 11:59:59 +0000 | [diff] [blame] | 2180 | groups_array->AsObjectArray<mirror::Object>(); |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2181 | |
| 2182 | // Copy the first 'size' elements out of the array into the result. |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2183 | ObjectRegistry* registry = Dbg::GetObjectRegistry(); |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2184 | for (int32_t i = 0; i < size; ++i) { |
| Przemyslaw Szczepaniak | 464595f | 2015-11-24 11:59:59 +0000 | [diff] [blame] | 2185 | 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] | 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | JDWP::JdwpError Dbg::GetThreadGroupChildren(JDWP::ObjectId thread_group_id, |
| 2190 | JDWP::ExpandBuf* pReply) { |
| 2191 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 2192 | JDWP::JdwpError error; |
| 2193 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2194 | if (error != JDWP::ERR_NONE) { |
| 2195 | return error; |
| 2196 | } |
| 2197 | |
| 2198 | // Add child threads. |
| 2199 | { |
| 2200 | std::vector<JDWP::ObjectId> child_thread_ids; |
| 2201 | GetThreads(thread_group, &child_thread_ids); |
| 2202 | expandBufAdd4BE(pReply, child_thread_ids.size()); |
| 2203 | for (JDWP::ObjectId child_thread_id : child_thread_ids) { |
| 2204 | expandBufAddObjectId(pReply, child_thread_id); |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | // Add child thread groups. |
| 2209 | { |
| 2210 | std::vector<JDWP::ObjectId> child_thread_groups_ids; |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2211 | GetChildThreadGroups(thread_group, &child_thread_groups_ids); |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2212 | expandBufAdd4BE(pReply, child_thread_groups_ids.size()); |
| 2213 | for (JDWP::ObjectId child_thread_group_id : child_thread_groups_ids) { |
| 2214 | expandBufAddObjectId(pReply, child_thread_group_id); |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2222 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2223 | ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup); |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2224 | ObjPtr<mirror::Object> group = f->GetObject(f->GetDeclaringClass()); |
| Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2225 | return gRegistry->Add(group); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
| Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2228 | JDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) { |
| 2229 | switch (state) { |
| 2230 | case kBlocked: |
| 2231 | return JDWP::TS_MONITOR; |
| 2232 | case kNative: |
| 2233 | case kRunnable: |
| 2234 | case kSuspended: |
| 2235 | return JDWP::TS_RUNNING; |
| 2236 | case kSleeping: |
| 2237 | return JDWP::TS_SLEEPING; |
| 2238 | case kStarting: |
| 2239 | case kTerminated: |
| 2240 | return JDWP::TS_ZOMBIE; |
| 2241 | case kTimedWaiting: |
| Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 2242 | case kWaitingForTaskProcessor: |
| 2243 | case kWaitingForLockInflation: |
| Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2244 | case kWaitingForCheckPointsToRun: |
| Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2245 | case kWaitingForDebuggerSend: |
| 2246 | case kWaitingForDebuggerSuspension: |
| 2247 | case kWaitingForDebuggerToAttach: |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 2248 | case kWaitingForDeoptimization: |
| Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2249 | case kWaitingForGcToComplete: |
| Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 2250 | case kWaitingForGetObjectsAllocated: |
| Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2251 | case kWaitingForJniOnLoad: |
| Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2252 | case kWaitingForMethodTracingStart: |
| Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2253 | case kWaitingForSignalCatcherOutput: |
| Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 2254 | case kWaitingForVisitObjects: |
| Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2255 | case kWaitingInMainDebuggerLoop: |
| 2256 | case kWaitingInMainSignalCatcherLoop: |
| 2257 | case kWaitingPerformingGc: |
| Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 2258 | case kWaitingWeakGcRootRead: |
| Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 2259 | case kWaitingForGcThreadFlip: |
| Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2260 | case kWaiting: |
| 2261 | return JDWP::TS_WAIT; |
| 2262 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 2263 | } |
| 2264 | LOG(FATAL) << "Unknown thread state: " << state; |
| 2265 | return JDWP::TS_ZOMBIE; |
| 2266 | } |
| 2267 | |
| Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2268 | JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, |
| 2269 | JDWP::JdwpSuspendStatus* pSuspendStatus) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2270 | ScopedObjectAccess soa(Thread::Current()); |
| Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2271 | |
| Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2272 | *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED; |
| 2273 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2274 | JDWP::JdwpError error; |
| 2275 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2276 | if (error != JDWP::ERR_NONE) { |
| 2277 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2278 | *pThreadStatus = JDWP::TS_ZOMBIE; |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2279 | return JDWP::ERR_NONE; |
| 2280 | } |
| 2281 | return error; |
| Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2282 | } |
| 2283 | |
| Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2284 | if (IsSuspendedForDebugger(soa, thread)) { |
| 2285 | *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED; |
| Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2286 | } |
| 2287 | |
| Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2288 | *pThreadStatus = ToJdwpThreadStatus(thread->GetState()); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2289 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2292 | JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2293 | ScopedObjectAccess soa(Thread::Current()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2294 | JDWP::JdwpError error; |
| 2295 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2296 | if (error != JDWP::ERR_NONE) { |
| 2297 | return error; |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2298 | } |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2299 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2300 | expandBufAdd4BE(pReply, thread->GetDebugSuspendCount()); |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2301 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2302 | } |
| 2303 | |
| Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2304 | JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) { |
| 2305 | ScopedObjectAccess soa(Thread::Current()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2306 | JDWP::JdwpError error; |
| 2307 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2308 | if (error != JDWP::ERR_NONE) { |
| 2309 | return error; |
| 2310 | } |
| Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 2311 | thread->Interrupt(soa.Self()); |
| Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2312 | return JDWP::ERR_NONE; |
| 2313 | } |
| 2314 | |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2315 | static bool IsInDesiredThreadGroup(mirror::Object* desired_thread_group, mirror::Object* peer) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2316 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2317 | // Do we want threads from all thread groups? |
| 2318 | if (desired_thread_group == nullptr) { |
| 2319 | return true; |
| 2320 | } |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2321 | ArtField* thread_group_field = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_group); |
| Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2322 | DCHECK(thread_group_field != nullptr); |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2323 | ObjPtr<mirror::Object> group = thread_group_field->GetObject(peer); |
| Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2324 | return (group == desired_thread_group); |
| 2325 | } |
| 2326 | |
| Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2327 | 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] | 2328 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2329 | std::list<Thread*> all_threads_list; |
| 2330 | { |
| 2331 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 2332 | all_threads_list = Runtime::Current()->GetThreadList()->GetList(); |
| 2333 | } |
| 2334 | for (Thread* t : all_threads_list) { |
| 2335 | if (t == Dbg::GetDebugThread()) { |
| 2336 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 2337 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 2338 | continue; |
| 2339 | } |
| 2340 | if (t->IsStillStarting()) { |
| 2341 | // This thread is being started (and has been registered in the thread list). However, it is |
| 2342 | // not completely started yet so we must ignore it. |
| 2343 | continue; |
| 2344 | } |
| Nicolas Geoffray | cafa081 | 2017-02-15 18:27:34 +0000 | [diff] [blame] | 2345 | mirror::Object* peer = t->GetPeerFromOtherThread(); |
| Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2346 | if (peer == nullptr) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2347 | // 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] | 2348 | // this thread yet. |
| 2349 | // TODO: if we identified threads to the debugger by their Thread* |
| 2350 | // rather than their peer's mirror::Object*, we could fix this. |
| 2351 | // Doing so might help us report ZOMBIE threads too. |
| 2352 | continue; |
| 2353 | } |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2354 | if (IsInDesiredThreadGroup(thread_group, peer)) { |
| Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2355 | thread_ids->push_back(gRegistry->Add(peer)); |
| 2356 | } |
| 2357 | } |
| Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2358 | } |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2359 | |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2360 | static int GetStackDepth(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2361 | struct CountStackDepthVisitor : public StackVisitor { |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2362 | explicit CountStackDepthVisitor(Thread* thread_in) |
| Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2363 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2364 | depth(0) {} |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2365 | |
| Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2366 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2367 | // annotalysis. |
| Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 2368 | bool VisitFrame() override NO_THREAD_SAFETY_ANALYSIS { |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2369 | if (!GetMethod()->IsRuntimeMethod()) { |
| Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 2370 | ++depth; |
| 2371 | } |
| Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2372 | return true; |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2373 | } |
| 2374 | size_t depth; |
| 2375 | }; |
| Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2376 | |
| Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2377 | CountStackDepthVisitor visitor(thread); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2378 | visitor.WalkStack(); |
| Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2379 | return visitor.depth; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2380 | } |
| 2381 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2382 | JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t* result) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2383 | ScopedObjectAccess soa(Thread::Current()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2384 | JDWP::JdwpError error; |
| 2385 | *result = 0; |
| 2386 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2387 | if (error != JDWP::ERR_NONE) { |
| 2388 | return error; |
| 2389 | } |
| Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2390 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2391 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2392 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2393 | *result = GetStackDepth(thread); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2394 | return JDWP::ERR_NONE; |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2395 | } |
| 2396 | |
| Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 2397 | JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame, |
| 2398 | size_t frame_count, JDWP::ExpandBuf* buf) { |
| Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2399 | class GetFrameVisitor : public StackVisitor { |
| 2400 | public: |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2401 | GetFrameVisitor(Thread* thread, size_t start_frame_in, size_t frame_count_in, |
| 2402 | JDWP::ExpandBuf* buf_in) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2403 | REQUIRES_SHARED(Locks::mutator_lock_) |
| Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2404 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2405 | depth_(0), |
| 2406 | start_frame_(start_frame_in), |
| 2407 | frame_count_(frame_count_in), |
| 2408 | buf_(buf_in) { |
| Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2409 | expandBufAdd4BE(buf_, frame_count_); |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2410 | } |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2411 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2412 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2413 | if (GetMethod()->IsRuntimeMethod()) { |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2414 | 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] | 2415 | } |
| Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2416 | if (depth_ >= start_frame_ + frame_count_) { |
| Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2417 | return false; |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2418 | } |
| Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2419 | if (depth_ >= start_frame_) { |
| 2420 | JDWP::FrameId frame_id(GetFrameId()); |
| 2421 | JDWP::JdwpLocation location; |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2422 | SetJdwpLocation(&location, GetMethod(), GetDexPc()); |
| Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2423 | VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location; |
| Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2424 | expandBufAdd8BE(buf_, frame_id); |
| 2425 | expandBufAddLocation(buf_, location); |
| 2426 | } |
| 2427 | ++depth_; |
| Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2428 | return true; |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2429 | } |
| Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2430 | |
| 2431 | private: |
| 2432 | size_t depth_; |
| 2433 | const size_t start_frame_; |
| 2434 | const size_t frame_count_; |
| 2435 | JDWP::ExpandBuf* buf_; |
| Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2436 | }; |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2437 | |
| 2438 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2439 | JDWP::JdwpError error; |
| 2440 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2441 | if (error != JDWP::ERR_NONE) { |
| 2442 | return error; |
| 2443 | } |
| Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2444 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2445 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2446 | } |
| Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2447 | GetFrameVisitor visitor(thread, start_frame, frame_count, buf); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2448 | visitor.WalkStack(); |
| Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2449 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2453 | return GetThreadId(Thread::Current()); |
| 2454 | } |
| 2455 | |
| 2456 | JDWP::ObjectId Dbg::GetThreadId(Thread* thread) { |
| Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 2457 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Nicolas Geoffray | cafa081 | 2017-02-15 18:27:34 +0000 | [diff] [blame] | 2458 | return gRegistry->Add(thread->GetPeerFromOtherThread()); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2459 | } |
| 2460 | |
| Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2461 | void Dbg::SuspendVM() { |
| Hiroshi Yamauchi | 8f95cf3 | 2016-04-19 11:14:06 -0700 | [diff] [blame] | 2462 | // Avoid a deadlock between GC and debugger where GC gets suspended during GC. b/25800335. |
| 2463 | gc::ScopedGCCriticalSection gcs(Thread::Current(), |
| 2464 | gc::kGcCauseDebugger, |
| 2465 | gc::kCollectorTypeDebugger); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2466 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | void Dbg::ResumeVM() { |
| Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 2470 | Runtime::Current()->GetThreadList()->ResumeAllForDebugger(); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2473 | JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) { |
| Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2474 | Thread* self = Thread::Current(); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2475 | ScopedLocalRef<jobject> peer(self->GetJniEnv(), nullptr); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2476 | { |
| Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2477 | ScopedObjectAccess soa(self); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2478 | JDWP::JdwpError error; |
| 2479 | peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id, &error))); |
| Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2480 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2481 | if (peer.get() == nullptr) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2482 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2483 | } |
| Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 2484 | // Suspend thread to build stack trace. |
| Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2485 | bool timed_out; |
| Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 2486 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| Sebastien Hertz | cdd798d | 2017-04-18 18:28:51 +0200 | [diff] [blame] | 2487 | Thread* thread = thread_list->SuspendThreadByPeer(peer.get(), |
| 2488 | request_suspension, |
| Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 2489 | SuspendReason::kForDebugger, |
| Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 2490 | &timed_out); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2491 | if (thread != nullptr) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2492 | return JDWP::ERR_NONE; |
| Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2493 | } else if (timed_out) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2494 | return JDWP::ERR_INTERNAL; |
| 2495 | } else { |
| 2496 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2497 | } |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2498 | } |
| 2499 | |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2500 | void Dbg::ResumeThread(JDWP::ObjectId thread_id) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2501 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2502 | JDWP::JdwpError error; |
| 2503 | mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2504 | CHECK(peer != nullptr) << error; |
| jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2505 | Thread* thread; |
| 2506 | { |
| 2507 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 2508 | thread = Thread::FromManagedThread(soa, peer); |
| 2509 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2510 | if (thread == nullptr) { |
| Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2511 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 2512 | return; |
| 2513 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2514 | bool needs_resume; |
| 2515 | { |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2516 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
| Sebastien Hertz | 70d6027 | 2017-04-14 14:18:36 +0200 | [diff] [blame] | 2517 | needs_resume = thread->GetDebugSuspendCount() > 0; |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2518 | } |
| 2519 | if (needs_resume) { |
| Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 2520 | bool resumed = Runtime::Current()->GetThreadList()->Resume(thread, SuspendReason::kForDebugger); |
| 2521 | DCHECK(resumed); |
| Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 2522 | } |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | void Dbg::SuspendSelf() { |
| Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2526 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2527 | } |
| 2528 | |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2529 | struct GetThisVisitor : public StackVisitor { |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2530 | GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id_in) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2531 | REQUIRES_SHARED(Locks::mutator_lock_) |
| Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2532 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2533 | this_object(nullptr), |
| 2534 | frame_id(frame_id_in) {} |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2535 | |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2536 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2537 | // annotalysis. |
| Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 2538 | bool VisitFrame() override NO_THREAD_SAFETY_ANALYSIS { |
| Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2539 | if (frame_id != GetFrameId()) { |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2540 | return true; // continue |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2541 | } else { |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2542 | this_object = GetThisObject(); |
| 2543 | return false; |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2544 | } |
| Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 2545 | } |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2546 | |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2547 | mirror::Object* this_object; |
| Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2548 | JDWP::FrameId frame_id; |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2549 | }; |
| 2550 | |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2551 | JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, |
| 2552 | JDWP::ObjectId* result) { |
| 2553 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2554 | JDWP::JdwpError error; |
| 2555 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2556 | if (error != JDWP::ERR_NONE) { |
| 2557 | return error; |
| 2558 | } |
| 2559 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2560 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2561 | } |
| Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2562 | std::unique_ptr<Context> context(Context::Create()); |
| Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2563 | GetThisVisitor visitor(thread, context.get(), frame_id); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2564 | visitor.WalkStack(); |
| Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2565 | *result = gRegistry->Add(visitor.this_object); |
| 2566 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2567 | } |
| 2568 | |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2569 | // Walks the stack until we find the frame with the given FrameId. |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2570 | class FindFrameVisitor final : public StackVisitor { |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2571 | public: |
| 2572 | FindFrameVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2573 | REQUIRES_SHARED(Locks::mutator_lock_) |
| Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2574 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2575 | frame_id_(frame_id), |
| 2576 | error_(JDWP::ERR_INVALID_FRAMEID) {} |
| Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2577 | |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2578 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2579 | // annotalysis. |
| Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 2580 | bool VisitFrame() override NO_THREAD_SAFETY_ANALYSIS { |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2581 | if (GetFrameId() != frame_id_) { |
| 2582 | return true; // Not our frame, carry on. |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2583 | } |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2584 | ArtMethod* m = GetMethod(); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2585 | if (m->IsNative()) { |
| 2586 | // We can't read/write local value from/into native method. |
| 2587 | error_ = JDWP::ERR_OPAQUE_FRAME; |
| 2588 | } else { |
| 2589 | // We found our frame. |
| 2590 | error_ = JDWP::ERR_NONE; |
| 2591 | } |
| 2592 | return false; |
| 2593 | } |
| 2594 | |
| 2595 | JDWP::JdwpError GetError() const { |
| 2596 | return error_; |
| 2597 | } |
| 2598 | |
| 2599 | private: |
| 2600 | const JDWP::FrameId frame_id_; |
| 2601 | JDWP::JdwpError error_; |
| Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame] | 2602 | |
| 2603 | DISALLOW_COPY_AND_ASSIGN(FindFrameVisitor); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2604 | }; |
| 2605 | |
| 2606 | JDWP::JdwpError Dbg::GetLocalValues(JDWP::Request* request, JDWP::ExpandBuf* pReply) { |
| 2607 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2608 | JDWP::FrameId frame_id = request->ReadFrameId(); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2609 | |
| 2610 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2611 | JDWP::JdwpError error; |
| 2612 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2613 | if (error != JDWP::ERR_NONE) { |
| 2614 | return error; |
| 2615 | } |
| 2616 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2617 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2618 | } |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2619 | // Find the frame with the given frame_id. |
| Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2620 | std::unique_ptr<Context> context(Context::Create()); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2621 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2622 | visitor.WalkStack(); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2623 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2624 | return visitor.GetError(); |
| 2625 | } |
| 2626 | |
| 2627 | // Read the values from visitor's context. |
| 2628 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2629 | expandBufAdd4BE(pReply, slot_count); /* "int values" */ |
| 2630 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2631 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2632 | JDWP::JdwpTag reqSigByte = request->ReadTag(); |
| 2633 | |
| 2634 | VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte; |
| 2635 | |
| 2636 | size_t width = Dbg::GetTagWidth(reqSigByte); |
| Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 2637 | uint8_t* ptr = expandBufAddSpace(pReply, width + 1); |
| Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2638 | error = Dbg::GetLocalValue(visitor, soa, slot, reqSigByte, ptr, width); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2639 | if (error != JDWP::ERR_NONE) { |
| 2640 | return error; |
| 2641 | } |
| 2642 | } |
| 2643 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2644 | } |
| 2645 | |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2646 | constexpr JDWP::JdwpError kStackFrameLocalAccessError = JDWP::ERR_ABSENT_INFORMATION; |
| 2647 | |
| 2648 | static std::string GetStackContextAsString(const StackVisitor& visitor) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2649 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2650 | 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] | 2651 | ArtMethod::PrettyMethod(visitor.GetMethod()).c_str()); |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | static JDWP::JdwpError FailGetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2655 | JDWP::JdwpTag tag) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2656 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2657 | LOG(ERROR) << "Failed to read " << tag << " local from register v" << vreg |
| 2658 | << GetStackContextAsString(visitor); |
| 2659 | return kStackFrameLocalAccessError; |
| 2660 | } |
| 2661 | |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2662 | JDWP::JdwpError Dbg::GetLocalValue(const StackVisitor& visitor, ScopedObjectAccessUnchecked& soa, |
| 2663 | int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2664 | ArtMethod* m = visitor.GetMethod(); |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2665 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2666 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2667 | if (error != JDWP::ERR_NONE) { |
| 2668 | return error; |
| 2669 | } |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2670 | // 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] | 2671 | switch (tag) { |
| 2672 | case JDWP::JT_BOOLEAN: { |
| 2673 | CHECK_EQ(width, 1U); |
| 2674 | uint32_t intVal; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2675 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2676 | return FailGetLocalValue(visitor, vreg, tag); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2677 | } |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2678 | VLOG(jdwp) << "get boolean local " << vreg << " = " << intVal; |
| 2679 | JDWP::Set1(buf + 1, intVal != 0); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2680 | break; |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2681 | } |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2682 | case JDWP::JT_BYTE: { |
| 2683 | CHECK_EQ(width, 1U); |
| 2684 | uint32_t intVal; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2685 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2686 | return FailGetLocalValue(visitor, vreg, tag); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2687 | } |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2688 | VLOG(jdwp) << "get byte local " << vreg << " = " << intVal; |
| 2689 | JDWP::Set1(buf + 1, intVal); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2690 | break; |
| 2691 | } |
| 2692 | case JDWP::JT_SHORT: |
| 2693 | case JDWP::JT_CHAR: { |
| 2694 | CHECK_EQ(width, 2U); |
| 2695 | uint32_t intVal; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2696 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2697 | return FailGetLocalValue(visitor, vreg, tag); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2698 | } |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2699 | VLOG(jdwp) << "get short/char local " << vreg << " = " << intVal; |
| 2700 | JDWP::Set2BE(buf + 1, intVal); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2701 | break; |
| 2702 | } |
| 2703 | case JDWP::JT_INT: { |
| 2704 | CHECK_EQ(width, 4U); |
| 2705 | uint32_t intVal; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2706 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2707 | return FailGetLocalValue(visitor, vreg, tag); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2708 | } |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2709 | VLOG(jdwp) << "get int local " << vreg << " = " << intVal; |
| 2710 | JDWP::Set4BE(buf + 1, intVal); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2711 | break; |
| 2712 | } |
| 2713 | case JDWP::JT_FLOAT: { |
| 2714 | CHECK_EQ(width, 4U); |
| 2715 | uint32_t intVal; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2716 | if (!visitor.GetVReg(m, vreg, kFloatVReg, &intVal)) { |
| 2717 | return FailGetLocalValue(visitor, vreg, tag); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2718 | } |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2719 | VLOG(jdwp) << "get float local " << vreg << " = " << intVal; |
| 2720 | JDWP::Set4BE(buf + 1, intVal); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2721 | break; |
| 2722 | } |
| 2723 | case JDWP::JT_ARRAY: |
| 2724 | case JDWP::JT_CLASS_LOADER: |
| 2725 | case JDWP::JT_CLASS_OBJECT: |
| 2726 | case JDWP::JT_OBJECT: |
| 2727 | case JDWP::JT_STRING: |
| 2728 | case JDWP::JT_THREAD: |
| 2729 | case JDWP::JT_THREAD_GROUP: { |
| 2730 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
| 2731 | uint32_t intVal; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2732 | if (!visitor.GetVReg(m, vreg, kReferenceVReg, &intVal)) { |
| 2733 | return FailGetLocalValue(visitor, vreg, tag); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2734 | } |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2735 | mirror::Object* o = reinterpret_cast<mirror::Object*>(intVal); |
| 2736 | VLOG(jdwp) << "get " << tag << " object local " << vreg << " = " << o; |
| 2737 | if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) { |
| 2738 | LOG(FATAL) << StringPrintf("Found invalid object %#" PRIxPTR " in register v%u", |
| 2739 | reinterpret_cast<uintptr_t>(o), vreg) |
| 2740 | << GetStackContextAsString(visitor); |
| 2741 | UNREACHABLE(); |
| 2742 | } |
| 2743 | tag = TagFromObject(soa, o); |
| 2744 | JDWP::SetObjectId(buf + 1, gRegistry->Add(o)); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2745 | break; |
| 2746 | } |
| 2747 | case JDWP::JT_DOUBLE: { |
| 2748 | CHECK_EQ(width, 8U); |
| 2749 | uint64_t longVal; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2750 | if (!visitor.GetVRegPair(m, vreg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) { |
| 2751 | return FailGetLocalValue(visitor, vreg, tag); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2752 | } |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2753 | VLOG(jdwp) << "get double local " << vreg << " = " << longVal; |
| 2754 | JDWP::Set8BE(buf + 1, longVal); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2755 | break; |
| 2756 | } |
| 2757 | case JDWP::JT_LONG: { |
| 2758 | CHECK_EQ(width, 8U); |
| 2759 | uint64_t longVal; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2760 | if (!visitor.GetVRegPair(m, vreg, kLongLoVReg, kLongHiVReg, &longVal)) { |
| 2761 | return FailGetLocalValue(visitor, vreg, tag); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2762 | } |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2763 | VLOG(jdwp) << "get long local " << vreg << " = " << longVal; |
| 2764 | JDWP::Set8BE(buf + 1, longVal); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2765 | break; |
| 2766 | } |
| 2767 | default: |
| 2768 | LOG(FATAL) << "Unknown tag " << tag; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2769 | UNREACHABLE(); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2770 | } |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2771 | |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2772 | // Prepend tag, which may have been updated. |
| 2773 | JDWP::Set1(buf, tag); |
| 2774 | return JDWP::ERR_NONE; |
| 2775 | } |
| 2776 | |
| 2777 | JDWP::JdwpError Dbg::SetLocalValues(JDWP::Request* request) { |
| 2778 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2779 | JDWP::FrameId frame_id = request->ReadFrameId(); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2780 | |
| 2781 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2782 | JDWP::JdwpError error; |
| 2783 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2784 | if (error != JDWP::ERR_NONE) { |
| 2785 | return error; |
| 2786 | } |
| 2787 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2788 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2789 | } |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2790 | // Find the frame with the given frame_id. |
| Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2791 | std::unique_ptr<Context> context(Context::Create()); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2792 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2793 | visitor.WalkStack(); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2794 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2795 | return visitor.GetError(); |
| 2796 | } |
| 2797 | |
| 2798 | // Writes the values into visitor's context. |
| 2799 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2800 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2801 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2802 | JDWP::JdwpTag sigByte = request->ReadTag(); |
| 2803 | size_t width = Dbg::GetTagWidth(sigByte); |
| 2804 | uint64_t value = request->ReadValue(width); |
| 2805 | |
| 2806 | VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value; |
| Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 2807 | error = Dbg::SetLocalValue(thread, visitor, slot, sigByte, value, width); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2808 | if (error != JDWP::ERR_NONE) { |
| 2809 | return error; |
| 2810 | } |
| 2811 | } |
| 2812 | return JDWP::ERR_NONE; |
| 2813 | } |
| 2814 | |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2815 | template<typename T> |
| 2816 | static JDWP::JdwpError FailSetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2817 | JDWP::JdwpTag tag, T value) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2818 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2819 | LOG(ERROR) << "Failed to write " << tag << " local " << value |
| 2820 | << " (0x" << std::hex << value << ") into register v" << vreg |
| 2821 | << GetStackContextAsString(visitor); |
| 2822 | return kStackFrameLocalAccessError; |
| 2823 | } |
| 2824 | |
| Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 2825 | JDWP::JdwpError Dbg::SetLocalValue(Thread* thread, StackVisitor& visitor, int slot, |
| 2826 | JDWP::JdwpTag tag, uint64_t value, size_t width) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2827 | ArtMethod* m = visitor.GetMethod(); |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2828 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2829 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2830 | if (error != JDWP::ERR_NONE) { |
| 2831 | return error; |
| 2832 | } |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2833 | // 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] | 2834 | switch (tag) { |
| 2835 | case JDWP::JT_BOOLEAN: |
| 2836 | case JDWP::JT_BYTE: |
| 2837 | CHECK_EQ(width, 1U); |
| Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2838 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2839 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2840 | } |
| 2841 | break; |
| 2842 | case JDWP::JT_SHORT: |
| 2843 | case JDWP::JT_CHAR: |
| 2844 | CHECK_EQ(width, 2U); |
| Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2845 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2846 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2847 | } |
| 2848 | break; |
| 2849 | case JDWP::JT_INT: |
| 2850 | CHECK_EQ(width, 4U); |
| Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2851 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2852 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2853 | } |
| 2854 | break; |
| 2855 | case JDWP::JT_FLOAT: |
| 2856 | CHECK_EQ(width, 4U); |
| Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2857 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kFloatVReg)) { |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2858 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2859 | } |
| 2860 | break; |
| 2861 | case JDWP::JT_ARRAY: |
| 2862 | case JDWP::JT_CLASS_LOADER: |
| 2863 | case JDWP::JT_CLASS_OBJECT: |
| 2864 | case JDWP::JT_OBJECT: |
| 2865 | case JDWP::JT_STRING: |
| 2866 | case JDWP::JT_THREAD: |
| 2867 | case JDWP::JT_THREAD_GROUP: { |
| 2868 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2869 | mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value), |
| 2870 | &error); |
| 2871 | if (error != JDWP::ERR_NONE) { |
| 2872 | VLOG(jdwp) << tag << " object " << o << " is an invalid object"; |
| 2873 | return JDWP::ERR_INVALID_OBJECT; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2874 | } |
| Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2875 | 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] | 2876 | kReferenceVReg)) { |
| 2877 | return FailSetLocalValue(visitor, vreg, tag, reinterpret_cast<uintptr_t>(o)); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2878 | } |
| 2879 | break; |
| 2880 | } |
| 2881 | case JDWP::JT_DOUBLE: { |
| 2882 | CHECK_EQ(width, 8U); |
| Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2883 | if (!visitor.SetVRegPair(m, vreg, value, kDoubleLoVReg, kDoubleHiVReg)) { |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2884 | return FailSetLocalValue(visitor, vreg, tag, value); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2885 | } |
| 2886 | break; |
| 2887 | } |
| 2888 | case JDWP::JT_LONG: { |
| 2889 | CHECK_EQ(width, 8U); |
| Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 2890 | if (!visitor.SetVRegPair(m, vreg, value, kLongLoVReg, kLongHiVReg)) { |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2891 | return FailSetLocalValue(visitor, vreg, tag, value); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2892 | } |
| 2893 | break; |
| 2894 | } |
| 2895 | default: |
| 2896 | LOG(FATAL) << "Unknown tag " << tag; |
| Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2897 | UNREACHABLE(); |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2898 | } |
| Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 2899 | |
| 2900 | // If we set the local variable in a compiled frame, we need to trigger a deoptimization of |
| 2901 | // the stack so we continue execution with the interpreter using the new value(s) of the updated |
| 2902 | // local variable(s). To achieve this, we install instrumentation exit stub on each method of the |
| 2903 | // thread's stack. The stub will cause the deoptimization to happen. |
| 2904 | if (!visitor.IsShadowFrame() && thread->HasDebuggerShadowFrames()) { |
| 2905 | Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(thread); |
| 2906 | } |
| 2907 | |
| Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2908 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2911 | static void SetEventLocation(JDWP::EventLocation* location, ArtMethod* m, uint32_t dex_pc) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2912 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2913 | DCHECK(location != nullptr); |
| 2914 | if (m == nullptr) { |
| 2915 | memset(location, 0, sizeof(*location)); |
| 2916 | } else { |
| Alex Light | 97e7803 | 2017-06-27 17:51:55 -0700 | [diff] [blame] | 2917 | location->method = m->GetCanonicalMethod(kRuntimePointerSize); |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2918 | 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] | 2919 | } |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2920 | } |
| 2921 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2922 | void Dbg::PostLocationEvent(ArtMethod* m, int dex_pc, mirror::Object* this_object, |
| Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2923 | int event_flags, const JValue* return_value) { |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2924 | if (!IsDebuggerActive()) { |
| 2925 | return; |
| 2926 | } |
| 2927 | DCHECK(m != nullptr); |
| 2928 | DCHECK_EQ(m->IsStatic(), this_object == nullptr); |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2929 | JDWP::EventLocation location; |
| 2930 | SetEventLocation(&location, m, dex_pc); |
| Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2931 | |
| Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2932 | // We need to be sure no exception is pending when calling JdwpState::PostLocationEvent. |
| 2933 | // This is required to be able to call JNI functions to create JDWP ids. To achieve this, |
| 2934 | // we temporarily clear the current thread's exception (if any) and will restore it after |
| 2935 | // the call. |
| 2936 | // Note: the only way to get a pending exception here is to suspend on a move-exception |
| 2937 | // instruction. |
| 2938 | Thread* const self = Thread::Current(); |
| 2939 | StackHandleScope<1> hs(self); |
| 2940 | Handle<mirror::Throwable> pending_exception(hs.NewHandle(self->GetException())); |
| 2941 | self->ClearException(); |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2942 | if (kIsDebugBuild && pending_exception != nullptr) { |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 2943 | const Instruction& instr = location.method->DexInstructions().InstructionAt(location.dex_pc); |
| 2944 | CHECK_EQ(Instruction::MOVE_EXCEPTION, instr.Opcode()); |
| Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2945 | } |
| 2946 | |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2947 | gJdwpState->PostLocationEvent(&location, this_object, event_flags, return_value); |
| Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2948 | |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2949 | if (pending_exception != nullptr) { |
| Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2950 | self->SetException(pending_exception.Get()); |
| 2951 | } |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2954 | void Dbg::PostFieldAccessEvent(ArtMethod* m, int dex_pc, |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2955 | mirror::Object* this_object, ArtField* f) { |
| Alex Light | e00ec30 | 2017-06-16 08:56:43 -0700 | [diff] [blame] | 2956 | // TODO We should send events for native methods. |
| 2957 | if (!IsDebuggerActive() || m->IsNative()) { |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2958 | return; |
| 2959 | } |
| 2960 | DCHECK(m != nullptr); |
| 2961 | DCHECK(f != nullptr); |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2962 | JDWP::EventLocation location; |
| 2963 | SetEventLocation(&location, m, dex_pc); |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2964 | |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2965 | gJdwpState->PostFieldEvent(&location, f, this_object, nullptr, false); |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2966 | } |
| 2967 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2968 | void Dbg::PostFieldModificationEvent(ArtMethod* m, int dex_pc, |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2969 | mirror::Object* this_object, ArtField* f, |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2970 | const JValue* field_value) { |
| Alex Light | e00ec30 | 2017-06-16 08:56:43 -0700 | [diff] [blame] | 2971 | // TODO We should send events for native methods. |
| 2972 | if (!IsDebuggerActive() || m->IsNative()) { |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2973 | return; |
| 2974 | } |
| 2975 | DCHECK(m != nullptr); |
| 2976 | DCHECK(f != nullptr); |
| 2977 | DCHECK(field_value != nullptr); |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2978 | JDWP::EventLocation location; |
| 2979 | SetEventLocation(&location, m, dex_pc); |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2980 | |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2981 | gJdwpState->PostFieldEvent(&location, f, this_object, field_value, true); |
| Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2982 | } |
| 2983 | |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2984 | /** |
| 2985 | * Finds the location where this exception will be caught. We search until we reach the top |
| 2986 | * frame, in which case this exception is considered uncaught. |
| 2987 | */ |
| 2988 | class CatchLocationFinder : public StackVisitor { |
| 2989 | public: |
| 2990 | CatchLocationFinder(Thread* self, const Handle<mirror::Throwable>& exception, Context* context) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2991 | REQUIRES_SHARED(Locks::mutator_lock_) |
| Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2992 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2993 | exception_(exception), |
| 2994 | handle_scope_(self), |
| 2995 | this_at_throw_(handle_scope_.NewHandle<mirror::Object>(nullptr)), |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2996 | catch_method_(nullptr), |
| 2997 | throw_method_(nullptr), |
| Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 2998 | catch_dex_pc_(dex::kDexNoIndex), |
| 2999 | throw_dex_pc_(dex::kDexNoIndex) { |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 3002 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3003 | ArtMethod* method = GetMethod(); |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3004 | DCHECK(method != nullptr); |
| 3005 | if (method->IsRuntimeMethod()) { |
| 3006 | // Ignore callee save method. |
| 3007 | DCHECK(method->IsCalleeSaveMethod()); |
| 3008 | return true; |
| 3009 | } |
| 3010 | |
| 3011 | uint32_t dex_pc = GetDexPc(); |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3012 | if (throw_method_ == nullptr) { |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3013 | // First Java method found. It is either the method that threw the exception, |
| 3014 | // or the Java native method that is reporting an exception thrown by |
| 3015 | // native code. |
| 3016 | this_at_throw_.Assign(GetThisObject()); |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3017 | throw_method_ = method; |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3018 | throw_dex_pc_ = dex_pc; |
| 3019 | } |
| 3020 | |
| Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 3021 | if (dex_pc != dex::kDexNoIndex) { |
| Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame] | 3022 | StackHandleScope<1> hs(GetThread()); |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3023 | uint32_t found_dex_pc; |
| 3024 | Handle<mirror::Class> exception_class(hs.NewHandle(exception_->GetClass())); |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3025 | bool unused_clear_exception; |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3026 | found_dex_pc = method->FindCatchBlock(exception_class, dex_pc, &unused_clear_exception); |
| Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 3027 | if (found_dex_pc != dex::kDexNoIndex) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3028 | catch_method_ = method; |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3029 | catch_dex_pc_ = found_dex_pc; |
| 3030 | return false; // End stack walk. |
| 3031 | } |
| 3032 | } |
| 3033 | return true; // Continue stack walk. |
| 3034 | } |
| 3035 | |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3036 | ArtMethod* GetCatchMethod() REQUIRES_SHARED(Locks::mutator_lock_) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3037 | return catch_method_; |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3040 | ArtMethod* GetThrowMethod() REQUIRES_SHARED(Locks::mutator_lock_) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3041 | return throw_method_; |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3044 | mirror::Object* GetThisAtThrow() REQUIRES_SHARED(Locks::mutator_lock_) { |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3045 | return this_at_throw_.Get(); |
| 3046 | } |
| 3047 | |
| 3048 | uint32_t GetCatchDexPc() const { |
| 3049 | return catch_dex_pc_; |
| 3050 | } |
| 3051 | |
| 3052 | uint32_t GetThrowDexPc() const { |
| 3053 | return throw_dex_pc_; |
| 3054 | } |
| 3055 | |
| 3056 | private: |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3057 | const Handle<mirror::Throwable>& exception_; |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3058 | StackHandleScope<1> handle_scope_; |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3059 | MutableHandle<mirror::Object> this_at_throw_; |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3060 | ArtMethod* catch_method_; |
| 3061 | ArtMethod* throw_method_; |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3062 | uint32_t catch_dex_pc_; |
| 3063 | uint32_t throw_dex_pc_; |
| 3064 | |
| 3065 | DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder); |
| 3066 | }; |
| 3067 | |
| 3068 | void Dbg::PostException(mirror::Throwable* exception_object) { |
| Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 3069 | if (!IsDebuggerActive()) { |
| Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 3070 | return; |
| 3071 | } |
| Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 3072 | Thread* const self = Thread::Current(); |
| 3073 | StackHandleScope<1> handle_scope(self); |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3074 | Handle<mirror::Throwable> h_exception(handle_scope.NewHandle(exception_object)); |
| 3075 | std::unique_ptr<Context> context(Context::Create()); |
| Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 3076 | CatchLocationFinder clf(self, h_exception, context.get()); |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3077 | clf.WalkStack(/* include_transitions */ false); |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3078 | JDWP::EventLocation exception_throw_location; |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3079 | SetEventLocation(&exception_throw_location, clf.GetThrowMethod(), clf.GetThrowDexPc()); |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3080 | JDWP::EventLocation exception_catch_location; |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3081 | SetEventLocation(&exception_catch_location, clf.GetCatchMethod(), clf.GetCatchDexPc()); |
| Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3082 | |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3083 | gJdwpState->PostException(&exception_throw_location, h_exception.Get(), &exception_catch_location, |
| 3084 | clf.GetThisAtThrow()); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3085 | } |
| 3086 | |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3087 | void Dbg::PostClassPrepare(mirror::Class* c) { |
| Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 3088 | if (!IsDebuggerActive()) { |
| Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3089 | return; |
| 3090 | } |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3091 | gJdwpState->PostClassPrepare(c); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3092 | } |
| 3093 | |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3094 | void Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object, |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3095 | ArtMethod* m, uint32_t dex_pc, |
| Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 3096 | int event_flags, const JValue* return_value) { |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3097 | if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) { |
| Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 3098 | return; |
| Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3099 | } |
| 3100 | |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3101 | if (IsBreakpoint(m, dex_pc)) { |
| 3102 | event_flags |= kBreakpoint; |
| 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 | |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3105 | // If the debugger is single-stepping one of our threads, check to |
| 3106 | // see if we're that thread and we've reached a step point. |
| 3107 | const SingleStepControl* single_step_control = thread->GetSingleStepControl(); |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3108 | if (single_step_control != nullptr) { |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3109 | CHECK(!m->IsNative()); |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3110 | if (single_step_control->GetStepDepth() == JDWP::SD_INTO) { |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3111 | // Step into method calls. We break when the line number |
| 3112 | // or method pointer changes. If we're in SS_MIN mode, we |
| 3113 | // always stop. |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3114 | if (single_step_control->GetMethod() != m) { |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3115 | event_flags |= kSingleStep; |
| 3116 | VLOG(jdwp) << "SS new method"; |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3117 | } else if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3118 | event_flags |= kSingleStep; |
| 3119 | VLOG(jdwp) << "SS new instruction"; |
| Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3120 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3121 | event_flags |= kSingleStep; |
| 3122 | VLOG(jdwp) << "SS new line"; |
| 3123 | } |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3124 | } else if (single_step_control->GetStepDepth() == JDWP::SD_OVER) { |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3125 | // Step over method calls. We break when the line number is |
| 3126 | // different and the frame depth is <= the original frame |
| 3127 | // depth. (We can't just compare on the method, because we |
| 3128 | // might get unrolled past it by an exception, and it's tricky |
| 3129 | // to identify recursion.) |
| 3130 | |
| 3131 | int stack_depth = GetStackDepth(thread); |
| 3132 | |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3133 | if (stack_depth < single_step_control->GetStackDepth()) { |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3134 | // Popped up one or more frames, always trigger. |
| 3135 | event_flags |= kSingleStep; |
| 3136 | VLOG(jdwp) << "SS method pop"; |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3137 | } else if (stack_depth == single_step_control->GetStackDepth()) { |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3138 | // Same depth, see if we moved. |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3139 | if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3140 | event_flags |= kSingleStep; |
| 3141 | VLOG(jdwp) << "SS new instruction"; |
| Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3142 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3143 | event_flags |= kSingleStep; |
| 3144 | VLOG(jdwp) << "SS new line"; |
| Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3145 | } |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3146 | } |
| 3147 | } else { |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3148 | CHECK_EQ(single_step_control->GetStepDepth(), JDWP::SD_OUT); |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3149 | // Return from the current method. We break when the frame |
| 3150 | // depth pops up. |
| Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3151 | |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3152 | // This differs from the "method exit" break in that it stops |
| 3153 | // with the PC at the next instruction in the returned-to |
| 3154 | // function, rather than the end of the returning function. |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3155 | |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3156 | int stack_depth = GetStackDepth(thread); |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3157 | if (stack_depth < single_step_control->GetStackDepth()) { |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3158 | event_flags |= kSingleStep; |
| 3159 | VLOG(jdwp) << "SS method pop"; |
| Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3160 | } |
| 3161 | } |
| Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3162 | } |
| Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3163 | |
| Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3164 | // If there's something interesting going on, see if it matches one |
| 3165 | // of the debugger filters. |
| 3166 | if (event_flags != 0) { |
| Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 3167 | Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value); |
| Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3168 | } |
| 3169 | } |
| 3170 | |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3171 | size_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) { |
| 3172 | switch (instrumentation_event) { |
| 3173 | case instrumentation::Instrumentation::kMethodEntered: |
| 3174 | return &method_enter_event_ref_count_; |
| 3175 | case instrumentation::Instrumentation::kMethodExited: |
| 3176 | return &method_exit_event_ref_count_; |
| 3177 | case instrumentation::Instrumentation::kDexPcMoved: |
| 3178 | return &dex_pc_change_event_ref_count_; |
| 3179 | case instrumentation::Instrumentation::kFieldRead: |
| 3180 | return &field_read_event_ref_count_; |
| 3181 | case instrumentation::Instrumentation::kFieldWritten: |
| 3182 | return &field_write_event_ref_count_; |
| Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 3183 | case instrumentation::Instrumentation::kExceptionThrown: |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3184 | return &exception_catch_event_ref_count_; |
| 3185 | default: |
| 3186 | return nullptr; |
| 3187 | } |
| 3188 | } |
| 3189 | |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3190 | // Process request while all mutator threads are suspended. |
| 3191 | void Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) { |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3192 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3193 | switch (request.GetKind()) { |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3194 | case DeoptimizationRequest::kNothing: |
| 3195 | LOG(WARNING) << "Ignoring empty deoptimization request."; |
| 3196 | break; |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3197 | case DeoptimizationRequest::kRegisterForEvent: |
| 3198 | VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x", |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3199 | request.InstrumentationEvent()); |
| 3200 | instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent()); |
| 3201 | instrumentation_events_ |= request.InstrumentationEvent(); |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3202 | break; |
| 3203 | case DeoptimizationRequest::kUnregisterForEvent: |
| 3204 | VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x", |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3205 | request.InstrumentationEvent()); |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3206 | instrumentation->RemoveListener(&gDebugInstrumentationListener, |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3207 | request.InstrumentationEvent()); |
| 3208 | instrumentation_events_ &= ~request.InstrumentationEvent(); |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3209 | break; |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3210 | case DeoptimizationRequest::kFullDeoptimization: |
| Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3211 | VLOG(jdwp) << "Deoptimize the world ..."; |
| Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3212 | instrumentation->DeoptimizeEverything(kDbgInstrumentationKey); |
| Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3213 | VLOG(jdwp) << "Deoptimize the world DONE"; |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3214 | break; |
| 3215 | case DeoptimizationRequest::kFullUndeoptimization: |
| Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3216 | VLOG(jdwp) << "Undeoptimize the world ..."; |
| Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3217 | instrumentation->UndeoptimizeEverything(kDbgInstrumentationKey); |
| Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3218 | VLOG(jdwp) << "Undeoptimize the world DONE"; |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3219 | break; |
| 3220 | case DeoptimizationRequest::kSelectiveDeoptimization: |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3221 | VLOG(jdwp) << "Deoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " ..."; |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3222 | instrumentation->Deoptimize(request.Method()); |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3223 | VLOG(jdwp) << "Deoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " DONE"; |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3224 | break; |
| 3225 | case DeoptimizationRequest::kSelectiveUndeoptimization: |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3226 | VLOG(jdwp) << "Undeoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " ..."; |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3227 | instrumentation->Undeoptimize(request.Method()); |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3228 | VLOG(jdwp) << "Undeoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " DONE"; |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3229 | break; |
| 3230 | default: |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3231 | LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind(); |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3232 | break; |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3233 | } |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3234 | } |
| 3235 | |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3236 | void Dbg::RequestDeoptimization(const DeoptimizationRequest& req) { |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3237 | if (req.GetKind() == DeoptimizationRequest::kNothing) { |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3238 | // Nothing to do. |
| 3239 | return; |
| 3240 | } |
| Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3241 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
| Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3242 | RequestDeoptimizationLocked(req); |
| 3243 | } |
| 3244 | |
| 3245 | void Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) { |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3246 | switch (req.GetKind()) { |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3247 | case DeoptimizationRequest::kRegisterForEvent: { |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3248 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3249 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3250 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3251 | req.InstrumentationEvent()); |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3252 | if (*counter == 0) { |
| Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3253 | 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] | 3254 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3255 | deoptimization_requests_.push_back(req); |
| 3256 | } |
| 3257 | *counter = *counter + 1; |
| 3258 | break; |
| 3259 | } |
| 3260 | case DeoptimizationRequest::kUnregisterForEvent: { |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3261 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3262 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3263 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3264 | req.InstrumentationEvent()); |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3265 | *counter = *counter - 1; |
| 3266 | if (*counter == 0) { |
| Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3267 | 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] | 3268 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
| Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3269 | deoptimization_requests_.push_back(req); |
| 3270 | } |
| 3271 | break; |
| 3272 | } |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3273 | case DeoptimizationRequest::kFullDeoptimization: { |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3274 | DCHECK(req.Method() == nullptr); |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3275 | if (full_deoptimization_event_count_ == 0) { |
| Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3276 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3277 | << " for full deoptimization"; |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3278 | deoptimization_requests_.push_back(req); |
| 3279 | } |
| 3280 | ++full_deoptimization_event_count_; |
| 3281 | break; |
| 3282 | } |
| 3283 | case DeoptimizationRequest::kFullUndeoptimization: { |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3284 | DCHECK(req.Method() == nullptr); |
| Sebastien Hertz | e713d93 | 2014-05-15 10:48:53 +0200 | [diff] [blame] | 3285 | DCHECK_GT(full_deoptimization_event_count_, 0U); |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3286 | --full_deoptimization_event_count_; |
| 3287 | if (full_deoptimization_event_count_ == 0) { |
| Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3288 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3289 | << " for full undeoptimization"; |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3290 | deoptimization_requests_.push_back(req); |
| 3291 | } |
| 3292 | break; |
| 3293 | } |
| 3294 | case DeoptimizationRequest::kSelectiveDeoptimization: { |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3295 | DCHECK(req.Method() != nullptr); |
| Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3296 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3297 | << " for deoptimization of " << req.Method()->PrettyMethod(); |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3298 | deoptimization_requests_.push_back(req); |
| 3299 | break; |
| 3300 | } |
| 3301 | case DeoptimizationRequest::kSelectiveUndeoptimization: { |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3302 | DCHECK(req.Method() != nullptr); |
| Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3303 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3304 | << " for undeoptimization of " << req.Method()->PrettyMethod(); |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3305 | deoptimization_requests_.push_back(req); |
| 3306 | break; |
| 3307 | } |
| 3308 | default: { |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3309 | LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind(); |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3310 | break; |
| 3311 | } |
| 3312 | } |
| 3313 | } |
| 3314 | |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3315 | void Dbg::ManageDeoptimization() { |
| 3316 | Thread* const self = Thread::Current(); |
| 3317 | { |
| 3318 | // Avoid suspend/resume if there is no pending request. |
| Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3319 | MutexLock mu(self, *Locks::deoptimization_lock_); |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3320 | if (deoptimization_requests_.empty()) { |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3321 | return; |
| 3322 | } |
| 3323 | } |
| 3324 | CHECK_EQ(self->GetState(), kRunnable); |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3325 | ScopedThreadSuspension sts(self, kWaitingForDeoptimization); |
| Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 3326 | // Required for ProcessDeoptimizationRequest. |
| 3327 | gc::ScopedGCCriticalSection gcs(self, |
| 3328 | gc::kGcCauseInstrumentation, |
| 3329 | gc::kCollectorTypeInstrumentation); |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3330 | // We need to suspend mutator threads first. |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 3331 | ScopedSuspendAll ssa(__FUNCTION__); |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3332 | const ThreadState old_state = self->SetStateUnsafe(kRunnable); |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3333 | { |
| Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3334 | MutexLock mu(self, *Locks::deoptimization_lock_); |
| Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3335 | size_t req_index = 0; |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3336 | for (DeoptimizationRequest& request : deoptimization_requests_) { |
| Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3337 | VLOG(jdwp) << "Process deoptimization request #" << req_index++; |
| Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3338 | ProcessDeoptimizationRequest(request); |
| 3339 | } |
| 3340 | deoptimization_requests_.clear(); |
| 3341 | } |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3342 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3343 | } |
| 3344 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3345 | static const Breakpoint* FindFirstBreakpointForMethod(ArtMethod* m) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3346 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3347 | for (Breakpoint& breakpoint : gBreakpoints) { |
| Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 3348 | if (breakpoint.IsInMethod(m)) { |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3349 | return &breakpoint; |
| 3350 | } |
| 3351 | } |
| 3352 | return nullptr; |
| 3353 | } |
| 3354 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3355 | bool Dbg::MethodHasAnyBreakpoints(ArtMethod* method) { |
| Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 3356 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| 3357 | return FindFirstBreakpointForMethod(method) != nullptr; |
| 3358 | } |
| 3359 | |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3360 | // Sanity checks all existing breakpoints on the same method. |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3361 | static void SanityCheckExistingBreakpoints(ArtMethod* m, |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3362 | DeoptimizationRequest::Kind deoptimization_kind) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3363 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
| Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3364 | for (const Breakpoint& breakpoint : gBreakpoints) { |
| Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 3365 | if (breakpoint.IsInMethod(m)) { |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3366 | CHECK_EQ(deoptimization_kind, breakpoint.GetDeoptimizationKind()); |
| 3367 | } |
| Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3368 | } |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3369 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 3370 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
| Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3371 | // We should have deoptimized everything but not "selectively" deoptimized this method. |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3372 | CHECK(instrumentation->AreAllMethodsDeoptimized()); |
| 3373 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3374 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
| Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3375 | // We should have "selectively" deoptimized this method. |
| 3376 | // Note: while we have not deoptimized everything for this method, we may have done it for |
| 3377 | // another event. |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3378 | CHECK(instrumentation->IsDeoptimized(m)); |
| 3379 | } else { |
| 3380 | // This method does not require deoptimization. |
| 3381 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3382 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3383 | } |
| 3384 | } |
| 3385 | |
| Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3386 | // Returns the deoptimization kind required to set a breakpoint in a method. |
| 3387 | // If a breakpoint has already been set, we also return the first breakpoint |
| 3388 | // through the given 'existing_brkpt' pointer. |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3389 | static DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self, |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3390 | ArtMethod* m, |
| Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3391 | const Breakpoint** existing_brkpt) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3392 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3393 | if (!Dbg::RequiresDeoptimization()) { |
| 3394 | // We already run in interpreter-only mode so we don't need to deoptimize anything. |
| 3395 | 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] | 3396 | << ArtMethod::PrettyMethod(m); |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3397 | return DeoptimizationRequest::kNothing; |
| 3398 | } |
| Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3399 | const Breakpoint* first_breakpoint; |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3400 | { |
| 3401 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
| Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3402 | first_breakpoint = FindFirstBreakpointForMethod(m); |
| 3403 | *existing_brkpt = first_breakpoint; |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3404 | } |
| Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3405 | |
| 3406 | if (first_breakpoint == nullptr) { |
| Nicolas Geoffray | 6300fd7 | 2016-03-18 09:40:17 +0000 | [diff] [blame] | 3407 | // There is no breakpoint on this method yet: we need to deoptimize. If this method is default, |
| 3408 | // we deoptimize everything; otherwise we deoptimize only this method. We |
| Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 3409 | // 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] | 3410 | // of the copies could be missed. |
| Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 3411 | // TODO Deoptimizing on default methods might not be necessary in all cases. |
| Nicolas Geoffray | 6300fd7 | 2016-03-18 09:40:17 +0000 | [diff] [blame] | 3412 | bool need_full_deoptimization = m->IsDefault(); |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3413 | if (need_full_deoptimization) { |
| Nicolas Geoffray | 6300fd7 | 2016-03-18 09:40:17 +0000 | [diff] [blame] | 3414 | VLOG(jdwp) << "Need full deoptimization because of copying of method " |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3415 | << ArtMethod::PrettyMethod(m); |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3416 | return DeoptimizationRequest::kFullDeoptimization; |
| 3417 | } else { |
| 3418 | // 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] | 3419 | const bool is_compiled = m->HasAnyCompiledCode(); |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3420 | if (is_compiled) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3421 | VLOG(jdwp) << "Need selective deoptimization for compiled method " |
| 3422 | << ArtMethod::PrettyMethod(m); |
| Nicolas Geoffray | 6300fd7 | 2016-03-18 09:40:17 +0000 | [diff] [blame] | 3423 | return DeoptimizationRequest::kSelectiveDeoptimization; |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3424 | } else { |
| 3425 | // Method is not compiled: we don't need to deoptimize. |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3426 | VLOG(jdwp) << "No need for deoptimization for non-compiled method " |
| 3427 | << ArtMethod::PrettyMethod(m); |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3428 | return DeoptimizationRequest::kNothing; |
| 3429 | } |
| 3430 | } |
| 3431 | } else { |
| 3432 | // There is at least one breakpoint for this method: we don't need to deoptimize. |
| 3433 | // Let's check that all breakpoints are configured the same way for deoptimization. |
| 3434 | VLOG(jdwp) << "Breakpoint already set: no deoptimization is required"; |
| Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3435 | DeoptimizationRequest::Kind deoptimization_kind = first_breakpoint->GetDeoptimizationKind(); |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3436 | if (kIsDebugBuild) { |
| 3437 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
| 3438 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
| 3439 | } |
| 3440 | return DeoptimizationRequest::kNothing; |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3441 | } |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3442 | } |
| 3443 | |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3444 | // Installs a breakpoint at the specified location. Also indicates through the deoptimization |
| 3445 | // request if we need to deoptimize. |
| 3446 | void Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
| 3447 | Thread* const self = Thread::Current(); |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3448 | ArtMethod* m = FromMethodId(location->method_id); |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3449 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3450 | |
| Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3451 | const Breakpoint* existing_breakpoint = nullptr; |
| 3452 | const DeoptimizationRequest::Kind deoptimization_kind = |
| 3453 | GetRequiredDeoptimizationKind(self, m, &existing_breakpoint); |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3454 | req->SetKind(deoptimization_kind); |
| 3455 | if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
| 3456 | req->SetMethod(m); |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3457 | } else { |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3458 | CHECK(deoptimization_kind == DeoptimizationRequest::kNothing || |
| 3459 | deoptimization_kind == DeoptimizationRequest::kFullDeoptimization); |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3460 | req->SetMethod(nullptr); |
| Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3461 | } |
| 3462 | |
| Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3463 | { |
| 3464 | WriterMutexLock mu(self, *Locks::breakpoint_lock_); |
| Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3465 | // If there is at least one existing breakpoint on the same method, the new breakpoint |
| 3466 | // must have the same deoptimization kind than the existing breakpoint(s). |
| 3467 | DeoptimizationRequest::Kind breakpoint_deoptimization_kind; |
| 3468 | if (existing_breakpoint != nullptr) { |
| 3469 | breakpoint_deoptimization_kind = existing_breakpoint->GetDeoptimizationKind(); |
| 3470 | } else { |
| 3471 | breakpoint_deoptimization_kind = deoptimization_kind; |
| 3472 | } |
| 3473 | gBreakpoints.push_back(Breakpoint(m, location->dex_pc, breakpoint_deoptimization_kind)); |
| Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3474 | VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " |
| 3475 | << gBreakpoints[gBreakpoints.size() - 1]; |
| 3476 | } |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | // Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization |
| 3480 | // request if we need to undeoptimize. |
| 3481 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
| Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3482 | WriterMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3483 | ArtMethod* m = FromMethodId(location->method_id); |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3484 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3485 | DeoptimizationRequest::Kind deoptimization_kind = DeoptimizationRequest::kNothing; |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3486 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
| Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 3487 | if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].IsInMethod(m)) { |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3488 | VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i]; |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3489 | deoptimization_kind = gBreakpoints[i].GetDeoptimizationKind(); |
| 3490 | DCHECK_EQ(deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization, |
| 3491 | Runtime::Current()->GetInstrumentation()->IsDeoptimized(m)); |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3492 | gBreakpoints.erase(gBreakpoints.begin() + i); |
| 3493 | break; |
| 3494 | } |
| 3495 | } |
| 3496 | const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m); |
| 3497 | if (existing_breakpoint == nullptr) { |
| 3498 | // There is no more breakpoint on this method: we need to undeoptimize. |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3499 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3500 | // This method required full deoptimization: we need to undeoptimize everything. |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3501 | req->SetKind(DeoptimizationRequest::kFullUndeoptimization); |
| 3502 | req->SetMethod(nullptr); |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3503 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3504 | // This method required selective deoptimization: we need to undeoptimize only that method. |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3505 | req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization); |
| 3506 | req->SetMethod(m); |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3507 | } else { |
| 3508 | // This method had no need for deoptimization: do nothing. |
| 3509 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3510 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3511 | req->SetMethod(nullptr); |
| Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3512 | } |
| 3513 | } else { |
| 3514 | // 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] | 3515 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3516 | req->SetMethod(nullptr); |
| Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3517 | if (kIsDebugBuild) { |
| Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3518 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
| Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3519 | } |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3520 | } |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3521 | } |
| 3522 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3523 | bool Dbg::IsForcedInterpreterNeededForCallingImpl(Thread* thread, ArtMethod* m) { |
| Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3524 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3525 | if (ssc == nullptr) { |
| 3526 | // If we are not single-stepping, then we don't have to force interpreter. |
| 3527 | return false; |
| 3528 | } |
| 3529 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
| 3530 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3531 | return false; |
| 3532 | } |
| 3533 | |
| 3534 | if (!m->IsNative() && !m->IsProxyMethod()) { |
| 3535 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3536 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3537 | return true; |
| 3538 | } |
| 3539 | } |
| 3540 | return false; |
| 3541 | } |
| 3542 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3543 | bool Dbg::IsForcedInterpreterNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
| Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3544 | instrumentation::Instrumentation* const instrumentation = |
| 3545 | Runtime::Current()->GetInstrumentation(); |
| 3546 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3547 | if (instrumentation->InterpretOnly()) { |
| 3548 | return false; |
| 3549 | } |
| 3550 | // We can only interpret pure Java method. |
| 3551 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3552 | return false; |
| 3553 | } |
| 3554 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3555 | if (ssc != nullptr) { |
| 3556 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3557 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3558 | return true; |
| 3559 | } |
| 3560 | // If we are stepping out from a static initializer, by issuing a step |
| 3561 | // in or step over, that was implicitly invoked by calling a static method, |
| 3562 | // then we need to step into that method. Having a lower stack depth than |
| 3563 | // the one the single step control has indicates that the step originates |
| 3564 | // from the static initializer. |
| 3565 | if (ssc->GetStepDepth() != JDWP::SD_OUT && |
| 3566 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3567 | return true; |
| 3568 | } |
| 3569 | } |
| 3570 | // There are cases where we have to force interpreter on deoptimized methods, |
| 3571 | // because in some cases the call will not be performed by invoking an entry |
| 3572 | // point that has been replaced by the deoptimization, but instead by directly |
| 3573 | // invoking the compiled code of the method, for example. |
| 3574 | return instrumentation->IsDeoptimized(m); |
| 3575 | } |
| 3576 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3577 | bool Dbg::IsForcedInstrumentationNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3578 | // 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] | 3579 | if (m == nullptr) { |
| 3580 | return false; |
| 3581 | } |
| 3582 | instrumentation::Instrumentation* const instrumentation = |
| 3583 | Runtime::Current()->GetInstrumentation(); |
| 3584 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3585 | if (instrumentation->InterpretOnly()) { |
| 3586 | return false; |
| 3587 | } |
| 3588 | // We can only interpret pure Java method. |
| 3589 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3590 | return false; |
| 3591 | } |
| 3592 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3593 | if (ssc != nullptr) { |
| 3594 | // If we are stepping out from a static initializer, by issuing a step |
| 3595 | // out, that was implicitly invoked by calling a static method, then we |
| 3596 | // need to step into the caller of that method. Having a lower stack |
| 3597 | // depth than the one the single step control has indicates that the |
| 3598 | // step originates from the static initializer. |
| 3599 | if (ssc->GetStepDepth() == JDWP::SD_OUT && |
| 3600 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3601 | return true; |
| 3602 | } |
| 3603 | } |
| 3604 | // If we are returning from a static intializer, that was implicitly |
| 3605 | // invoked by calling a static method and the caller is deoptimized, |
| 3606 | // then we have to deoptimize the stack without forcing interpreter |
| 3607 | // on the static method that was called originally. This problem can |
| 3608 | // be solved easily by forcing instrumentation on the called method, |
| 3609 | // because the instrumentation exit hook will recognise the need of |
| 3610 | // stack deoptimization by calling IsForcedInterpreterNeededForUpcall. |
| 3611 | return instrumentation->IsDeoptimized(m); |
| 3612 | } |
| 3613 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3614 | bool Dbg::IsForcedInterpreterNeededForUpcallImpl(Thread* thread, ArtMethod* m) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3615 | // 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] | 3616 | if (m == nullptr) { |
| 3617 | return false; |
| 3618 | } |
| 3619 | instrumentation::Instrumentation* const instrumentation = |
| 3620 | Runtime::Current()->GetInstrumentation(); |
| 3621 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3622 | if (instrumentation->InterpretOnly()) { |
| 3623 | return false; |
| 3624 | } |
| 3625 | // We can only interpret pure Java method. |
| 3626 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3627 | return false; |
| 3628 | } |
| 3629 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3630 | if (ssc != nullptr) { |
| 3631 | // The debugger is not interested in what is happening under the level |
| 3632 | // of the step, thus we only force interpreter when we are not below of |
| 3633 | // the step. |
| 3634 | if (ssc->GetStackDepth() >= GetStackDepth(thread)) { |
| 3635 | return true; |
| 3636 | } |
| 3637 | } |
| Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 3638 | if (thread->HasDebuggerShadowFrames()) { |
| 3639 | // We need to deoptimize the stack for the exception handling flow so that |
| 3640 | // we don't miss any deoptimization that should be done when there are |
| 3641 | // debugger shadow frames. |
| 3642 | return true; |
| 3643 | } |
| Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3644 | // We have to require stack deoptimization if the upcall is deoptimized. |
| 3645 | return instrumentation->IsDeoptimized(m); |
| 3646 | } |
| 3647 | |
| Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 3648 | class NeedsDeoptimizationVisitor : public StackVisitor { |
| Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 3649 | public: |
| 3650 | explicit NeedsDeoptimizationVisitor(Thread* self) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3651 | REQUIRES_SHARED(Locks::mutator_lock_) |
| Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 3652 | : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 3653 | needs_deoptimization_(false) {} |
| 3654 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 3655 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 3656 | // 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] | 3657 | CHECK(!IsShadowFrame()) << "We only expect to visit compiled frame: " |
| 3658 | << ArtMethod::PrettyMethod(GetMethod()); |
| Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 3659 | ArtMethod* method = GetMethod(); |
| 3660 | if (method == nullptr) { |
| 3661 | // We reach an upcall and don't need to deoptimize this part of the stack (ManagedFragment) |
| 3662 | // so we can stop the visit. |
| 3663 | DCHECK(!needs_deoptimization_); |
| 3664 | return false; |
| 3665 | } |
| 3666 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
| 3667 | // We found a compiled frame in the stack but instrumentation is set to interpret |
| 3668 | // everything: we need to deoptimize. |
| 3669 | needs_deoptimization_ = true; |
| 3670 | return false; |
| 3671 | } |
| 3672 | if (Runtime::Current()->GetInstrumentation()->IsDeoptimized(method)) { |
| 3673 | // We found a deoptimized method in the stack. |
| 3674 | needs_deoptimization_ = true; |
| 3675 | return false; |
| 3676 | } |
| Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 3677 | ShadowFrame* frame = GetThread()->FindDebuggerShadowFrame(GetFrameId()); |
| 3678 | if (frame != nullptr) { |
| 3679 | // The debugger allocated a ShadowFrame to update a variable in the stack: we need to |
| 3680 | // deoptimize the stack to execute (and deallocate) this frame. |
| 3681 | needs_deoptimization_ = true; |
| 3682 | return false; |
| 3683 | } |
| Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 3684 | return true; |
| 3685 | } |
| 3686 | |
| 3687 | bool NeedsDeoptimization() const { |
| 3688 | return needs_deoptimization_; |
| 3689 | } |
| 3690 | |
| 3691 | private: |
| 3692 | // Do we need to deoptimize the stack? |
| 3693 | bool needs_deoptimization_; |
| 3694 | |
| 3695 | DISALLOW_COPY_AND_ASSIGN(NeedsDeoptimizationVisitor); |
| 3696 | }; |
| 3697 | |
| 3698 | // Do we need to deoptimize the stack to handle an exception? |
| 3699 | bool Dbg::IsForcedInterpreterNeededForExceptionImpl(Thread* thread) { |
| 3700 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3701 | if (ssc != nullptr) { |
| 3702 | // We deopt to step into the catch handler. |
| 3703 | return true; |
| 3704 | } |
| 3705 | // Deoptimization is required if at least one method in the stack needs it. However we |
| 3706 | // skip frames that will be unwound (thus not executed). |
| 3707 | NeedsDeoptimizationVisitor visitor(thread); |
| 3708 | visitor.WalkStack(true); // includes upcall. |
| 3709 | return visitor.NeedsDeoptimization(); |
| 3710 | } |
| 3711 | |
| Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3712 | // Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't |
| 3713 | // cause suspension if the thread is the current thread. |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3714 | class ScopedDebuggerThreadSuspension { |
| Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3715 | public: |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3716 | ScopedDebuggerThreadSuspension(Thread* self, JDWP::ObjectId thread_id) |
| Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3717 | REQUIRES(!Locks::thread_list_lock_) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3718 | REQUIRES_SHARED(Locks::mutator_lock_) : |
| Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3719 | thread_(nullptr), |
| Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3720 | error_(JDWP::ERR_NONE), |
| 3721 | self_suspend_(false), |
| Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3722 | other_suspend_(false) { |
| Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3723 | ScopedObjectAccessUnchecked soa(self); |
| Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 3724 | thread_ = DecodeThread(soa, thread_id, &error_); |
| Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3725 | if (error_ == JDWP::ERR_NONE) { |
| 3726 | if (thread_ == soa.Self()) { |
| 3727 | self_suspend_ = true; |
| 3728 | } else { |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3729 | Thread* suspended_thread; |
| 3730 | { |
| 3731 | ScopedThreadSuspension sts(self, kWaitingForDebuggerSuspension); |
| 3732 | jobject thread_peer = Dbg::GetObjectRegistry()->GetJObject(thread_id); |
| 3733 | bool timed_out; |
| 3734 | ThreadList* const thread_list = Runtime::Current()->GetThreadList(); |
| Sebastien Hertz | cdd798d | 2017-04-18 18:28:51 +0200 | [diff] [blame] | 3735 | suspended_thread = thread_list->SuspendThreadByPeer(thread_peer, |
| 3736 | /* request_suspension */ true, |
| Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 3737 | SuspendReason::kForDebugger, |
| Sebastien Hertz | cdd798d | 2017-04-18 18:28:51 +0200 | [diff] [blame] | 3738 | &timed_out); |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3739 | } |
| Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3740 | if (suspended_thread == nullptr) { |
| Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3741 | // Thread terminated from under us while suspending. |
| 3742 | error_ = JDWP::ERR_INVALID_THREAD; |
| 3743 | } else { |
| 3744 | CHECK_EQ(suspended_thread, thread_); |
| 3745 | other_suspend_ = true; |
| 3746 | } |
| 3747 | } |
| 3748 | } |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3749 | } |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3750 | |
| Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3751 | Thread* GetThread() const { |
| 3752 | return thread_; |
| 3753 | } |
| 3754 | |
| 3755 | JDWP::JdwpError GetError() const { |
| 3756 | return error_; |
| 3757 | } |
| 3758 | |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3759 | ~ScopedDebuggerThreadSuspension() { |
| Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3760 | if (other_suspend_) { |
| Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 3761 | bool resumed = Runtime::Current()->GetThreadList()->Resume(thread_, |
| 3762 | SuspendReason::kForDebugger); |
| 3763 | DCHECK(resumed); |
| Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3764 | } |
| 3765 | } |
| 3766 | |
| 3767 | private: |
| 3768 | Thread* thread_; |
| 3769 | JDWP::JdwpError error_; |
| 3770 | bool self_suspend_; |
| 3771 | bool other_suspend_; |
| 3772 | }; |
| 3773 | |
| 3774 | JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size, |
| 3775 | JDWP::JdwpStepDepth step_depth) { |
| 3776 | Thread* self = Thread::Current(); |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 3777 | ScopedDebuggerThreadSuspension sts(self, thread_id); |
| Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3778 | if (sts.GetError() != JDWP::ERR_NONE) { |
| 3779 | return sts.GetError(); |
| 3780 | } |
| 3781 | |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3782 | // 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] | 3783 | // is for step-out. |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3784 | struct SingleStepStackVisitor : public StackVisitor { |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3785 | explicit SingleStepStackVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) |
| Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 3786 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 3787 | stack_depth(0), |
| 3788 | method(nullptr), |
| 3789 | line_number(-1) {} |
| Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 3790 | |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3791 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 3792 | // annotalysis. |
| Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 3793 | bool VisitFrame() override NO_THREAD_SAFETY_ANALYSIS { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3794 | ArtMethod* m = GetMethod(); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3795 | if (!m->IsRuntimeMethod()) { |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3796 | ++stack_depth; |
| 3797 | if (method == nullptr) { |
| Alex Light | 7337631 | 2017-04-06 10:10:51 -0700 | [diff] [blame] | 3798 | const DexFile* dex_file = m->GetDexFile(); |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3799 | method = m; |
| Alex Light | 7337631 | 2017-04-06 10:10:51 -0700 | [diff] [blame] | 3800 | if (dex_file != nullptr) { |
| David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 3801 | line_number = annotations::GetLineNumFromPC(dex_file, m, GetDexPc()); |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3802 | } |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3803 | } |
| 3804 | } |
| Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 3805 | return true; |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3806 | } |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3807 | |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3808 | int stack_depth; |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3809 | ArtMethod* method; |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3810 | int32_t line_number; |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3811 | }; |
| Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3812 | |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3813 | Thread* const thread = sts.GetThread(); |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3814 | SingleStepStackVisitor visitor(thread); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3815 | visitor.WalkStack(); |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3816 | |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3817 | // Allocate single step. |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3818 | SingleStepControl* single_step_control = |
| 3819 | new (std::nothrow) SingleStepControl(step_size, step_depth, |
| 3820 | visitor.stack_depth, visitor.method); |
| 3821 | if (single_step_control == nullptr) { |
| 3822 | LOG(ERROR) << "Failed to allocate SingleStepControl"; |
| 3823 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3824 | } |
| 3825 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3826 | ArtMethod* m = single_step_control->GetMethod(); |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3827 | const int32_t line_number = visitor.line_number; |
| Sebastien Hertz | 52f5f93 | 2015-05-28 11:00:57 +0200 | [diff] [blame] | 3828 | // Note: if the thread is not running Java code (pure native thread), there is no "current" |
| 3829 | // method on the stack (and no line number either). |
| 3830 | if (m != nullptr && !m->IsNative()) { |
| David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 3831 | CodeItemDebugInfoAccessor accessor(m->DexInstructionDebugInfo()); |
| Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 3832 | bool last_pc_valid = false; |
| 3833 | uint32_t last_pc = 0u; |
| 3834 | // Find the dex_pc values that correspond to the current line, for line-based single-stepping. |
| 3835 | accessor.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) { |
| 3836 | if (static_cast<int32_t>(entry.line_) == line_number) { |
| 3837 | if (!last_pc_valid) { |
| 3838 | // Everything from this address until the next line change is ours. |
| 3839 | last_pc = entry.address_; |
| 3840 | last_pc_valid = true; |
| 3841 | } |
| 3842 | // Otherwise, if we're already in a valid range for this line, |
| 3843 | // just keep going (shouldn't really happen)... |
| 3844 | } else if (last_pc_valid) { // and the line number is new |
| 3845 | // Add everything from the last entry up until here to the set |
| 3846 | for (uint32_t dex_pc = last_pc; dex_pc < entry.address_; ++dex_pc) { |
| 3847 | single_step_control->AddDexPc(dex_pc); |
| 3848 | } |
| 3849 | last_pc_valid = false; |
| 3850 | } |
| 3851 | return false; // There may be multiple entries for any given line. |
| 3852 | }); |
| 3853 | // If the line number was the last in the position table... |
| 3854 | if (last_pc_valid) { |
| 3855 | for (uint32_t dex_pc = last_pc; dex_pc < accessor.InsnsSizeInCodeUnits(); ++dex_pc) { |
| 3856 | single_step_control->AddDexPc(dex_pc); |
| 3857 | } |
| 3858 | } |
| Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 3859 | } |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3860 | |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3861 | // Activate single-step in the thread. |
| 3862 | thread->ActivateSingleStepControl(single_step_control); |
| Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3863 | |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3864 | if (VLOG_IS_ON(jdwp)) { |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3865 | VLOG(jdwp) << "Single-step thread: " << *thread; |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3866 | VLOG(jdwp) << "Single-step step size: " << single_step_control->GetStepSize(); |
| 3867 | VLOG(jdwp) << "Single-step step depth: " << single_step_control->GetStepDepth(); |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3868 | VLOG(jdwp) << "Single-step current method: " |
| 3869 | << ArtMethod::PrettyMethod(single_step_control->GetMethod()); |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3870 | VLOG(jdwp) << "Single-step current line: " << line_number; |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3871 | VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->GetStackDepth(); |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3872 | VLOG(jdwp) << "Single-step dex_pc values:"; |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3873 | for (uint32_t dex_pc : single_step_control->GetDexPcs()) { |
| Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3874 | VLOG(jdwp) << StringPrintf(" %#x", dex_pc); |
| Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3875 | } |
| 3876 | } |
| 3877 | |
| 3878 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3879 | } |
| 3880 | |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3881 | void Dbg::UnconfigureStep(JDWP::ObjectId thread_id) { |
| 3882 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3883 | JDWP::JdwpError error; |
| 3884 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| Sebastien Hertz | 87118ed | 2013-11-26 17:57:18 +0100 | [diff] [blame] | 3885 | if (error == JDWP::ERR_NONE) { |
| Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3886 | thread->DeactivateSingleStepControl(); |
| Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3887 | } |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3888 | } |
| 3889 | |
| Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3890 | static char JdwpTagToShortyChar(JDWP::JdwpTag tag) { |
| 3891 | switch (tag) { |
| 3892 | default: |
| 3893 | LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag); |
| Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 3894 | UNREACHABLE(); |
| Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3895 | |
| 3896 | // Primitives. |
| 3897 | case JDWP::JT_BYTE: return 'B'; |
| 3898 | case JDWP::JT_CHAR: return 'C'; |
| 3899 | case JDWP::JT_FLOAT: return 'F'; |
| 3900 | case JDWP::JT_DOUBLE: return 'D'; |
| 3901 | case JDWP::JT_INT: return 'I'; |
| 3902 | case JDWP::JT_LONG: return 'J'; |
| 3903 | case JDWP::JT_SHORT: return 'S'; |
| 3904 | case JDWP::JT_VOID: return 'V'; |
| 3905 | case JDWP::JT_BOOLEAN: return 'Z'; |
| 3906 | |
| 3907 | // Reference types. |
| 3908 | case JDWP::JT_ARRAY: |
| 3909 | case JDWP::JT_OBJECT: |
| 3910 | case JDWP::JT_STRING: |
| 3911 | case JDWP::JT_THREAD: |
| 3912 | case JDWP::JT_THREAD_GROUP: |
| 3913 | case JDWP::JT_CLASS_LOADER: |
| 3914 | case JDWP::JT_CLASS_OBJECT: |
| 3915 | return 'L'; |
| 3916 | } |
| 3917 | } |
| 3918 | |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3919 | JDWP::JdwpError Dbg::PrepareInvokeMethod(uint32_t request_id, JDWP::ObjectId thread_id, |
| 3920 | JDWP::ObjectId object_id, JDWP::RefTypeId class_id, |
| 3921 | JDWP::MethodId method_id, uint32_t arg_count, |
| 3922 | uint64_t arg_values[], JDWP::JdwpTag* arg_types, |
| 3923 | uint32_t options) { |
| 3924 | Thread* const self = Thread::Current(); |
| 3925 | CHECK_EQ(self, GetDebugThread()) << "This must be called by the JDWP thread"; |
| Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame] | 3926 | const bool resume_all_threads = ((options & JDWP::INVOKE_SINGLE_THREADED) == 0); |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3927 | |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3928 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3929 | Thread* targetThread = nullptr; |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3930 | { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3931 | ScopedObjectAccessUnchecked soa(self); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3932 | JDWP::JdwpError error; |
| 3933 | targetThread = DecodeThread(soa, thread_id, &error); |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 3934 | if (error != JDWP::ERR_NONE) { |
| 3935 | LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id; |
| 3936 | return error; |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3937 | } |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3938 | if (targetThread->GetInvokeReq() != nullptr) { |
| 3939 | // Thread is already invoking a method on behalf of the debugger. |
| 3940 | LOG(ERROR) << "InvokeMethod request for thread already invoking a method: " << *targetThread; |
| 3941 | return JDWP::ERR_ALREADY_INVOKING; |
| 3942 | } |
| 3943 | if (!targetThread->IsReadyForDebugInvoke()) { |
| 3944 | // 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] | 3945 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 3946 | return JDWP::ERR_INVALID_THREAD; |
| 3947 | } |
| 3948 | |
| 3949 | /* |
| Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame] | 3950 | * According to the JDWP specs, we are expected to resume all threads (or only the |
| 3951 | * target thread) once. So if a thread has been suspended more than once (either by |
| 3952 | * the debugger for an event or by the runtime for GC), it will remain suspended before |
| 3953 | * the invoke is executed. This means the debugger is responsible to properly resume all |
| 3954 | * 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] | 3955 | * |
| Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame] | 3956 | * However, for compatibility reason with older versions of debuggers (like Eclipse), we |
| 3957 | * fully resume all threads (by canceling *all* debugger suspensions) when the debugger |
| 3958 | * wants us to resume all threads. This is to avoid ending up in deadlock situation. |
| 3959 | * |
| 3960 | * On the other hand, if we are asked to only resume the target thread, then we follow the |
| 3961 | * JDWP specs by resuming that thread only once. This means the thread will remain suspended |
| 3962 | * if it has been suspended more than once before the invoke (and again, this is the |
| 3963 | * 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] | 3964 | */ |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3965 | int suspend_count; |
| 3966 | { |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3967 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3968 | suspend_count = targetThread->GetSuspendCount(); |
| 3969 | } |
| Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame] | 3970 | if (suspend_count > 1 && resume_all_threads) { |
| 3971 | // The target thread will remain suspended even after we resume it. Let's emit a warning |
| 3972 | // to indicate the invoke won't be executed until the thread is resumed. |
| 3973 | LOG(WARNING) << *targetThread << " suspended more than once (suspend count == " |
| 3974 | << suspend_count << "). This thread will invoke the method only once " |
| 3975 | << "it is fully resumed."; |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3976 | } |
| 3977 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3978 | mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 3979 | if (error != JDWP::ERR_NONE) { |
| Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3980 | return JDWP::ERR_INVALID_OBJECT; |
| 3981 | } |
| Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3982 | |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3983 | gRegistry->Get<mirror::Object*>(thread_id, &error); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3984 | if (error != JDWP::ERR_NONE) { |
| Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3985 | return JDWP::ERR_INVALID_OBJECT; |
| 3986 | } |
| Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3987 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3988 | mirror::Class* c = DecodeClass(class_id, &error); |
| 3989 | if (c == nullptr) { |
| 3990 | return error; |
| Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3991 | } |
| Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3992 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3993 | ArtMethod* m = FromMethodId(method_id); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3994 | if (m->IsStatic() != (receiver == nullptr)) { |
| Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3995 | return JDWP::ERR_INVALID_METHODID; |
| 3996 | } |
| 3997 | if (m->IsStatic()) { |
| 3998 | if (m->GetDeclaringClass() != c) { |
| 3999 | return JDWP::ERR_INVALID_METHODID; |
| 4000 | } |
| 4001 | } else { |
| 4002 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
| 4003 | return JDWP::ERR_INVALID_METHODID; |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | // Check the argument list matches the method. |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4008 | uint32_t shorty_len = 0; |
| 4009 | const char* shorty = m->GetShorty(&shorty_len); |
| 4010 | if (shorty_len - 1 != arg_count) { |
| Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 4011 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 4012 | } |
| Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 4013 | |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4014 | { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4015 | StackHandleScope<2> hs(soa.Self()); |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4016 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver)); |
| 4017 | HandleWrapper<mirror::Class> h_klass(hs.NewHandleWrapper(&c)); |
| 4018 | const DexFile::TypeList* types = m->GetParameterTypeList(); |
| 4019 | for (size_t i = 0; i < arg_count; ++i) { |
| 4020 | if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) { |
| Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 4021 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 4022 | } |
| 4023 | |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4024 | if (shorty[i + 1] == 'L') { |
| 4025 | // Did we really get an argument of an appropriate reference type? |
| Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 4026 | ObjPtr<mirror::Class> parameter_type = |
| 4027 | m->ResolveClassFromTypeIndex(types->GetTypeItem(i).type_idx_); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4028 | mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i], &error); |
| 4029 | if (error != JDWP::ERR_NONE) { |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4030 | return JDWP::ERR_INVALID_OBJECT; |
| 4031 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4032 | if (argument != nullptr && !argument->InstanceOf(parameter_type)) { |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4033 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 4034 | } |
| 4035 | |
| 4036 | // Turn the on-the-wire ObjectId into a jobject. |
| 4037 | jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]); |
| 4038 | v.l = gRegistry->GetJObject(arg_values[i]); |
| 4039 | } |
| Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 4040 | } |
| Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 4041 | } |
| 4042 | |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4043 | // Allocates a DebugInvokeReq. |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4044 | DebugInvokeReq* req = new (std::nothrow) DebugInvokeReq(request_id, thread_id, receiver, c, m, |
| 4045 | options, arg_values, arg_count); |
| 4046 | if (req == nullptr) { |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4047 | LOG(ERROR) << "Failed to allocate DebugInvokeReq"; |
| 4048 | return JDWP::ERR_OUT_OF_MEMORY; |
| 4049 | } |
| 4050 | |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4051 | // Attaches the DebugInvokeReq to the target thread so it executes the method when |
| 4052 | // it is resumed. Once the invocation completes, the target thread will delete it before |
| 4053 | // suspending itself (see ThreadList::SuspendSelfForDebugger). |
| 4054 | targetThread->SetDebugInvokeReq(req); |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | // 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] | 4058 | // away we're sitting high and dry -- but we must release this before the UndoDebuggerSuspensions |
| 4059 | // call. |
| Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame] | 4060 | if (resume_all_threads) { |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4061 | VLOG(jdwp) << " Resuming all threads"; |
| 4062 | thread_list->UndoDebuggerSuspensions(); |
| 4063 | } else { |
| 4064 | VLOG(jdwp) << " Resuming event thread only"; |
| Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 4065 | bool resumed = thread_list->Resume(targetThread, SuspendReason::kForDebugger); |
| 4066 | DCHECK(resumed); |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4067 | } |
| 4068 | |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4069 | return JDWP::ERR_NONE; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4073 | Thread* const self = Thread::Current(); |
| 4074 | CHECK_NE(self, GetDebugThread()) << "This must be called by the event thread"; |
| 4075 | |
| 4076 | ScopedObjectAccess soa(self); |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4077 | |
| Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 4078 | // We can be called while an exception is pending. We need |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4079 | // to preserve that across the method invocation. |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4080 | StackHandleScope<1> hs(soa.Self()); |
| 4081 | Handle<mirror::Throwable> old_exception = hs.NewHandle(soa.Self()->GetException()); |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4082 | soa.Self()->ClearException(); |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4083 | |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4084 | // Execute the method then sends reply to the debugger. |
| 4085 | ExecuteMethodWithoutPendingException(soa, pReq); |
| 4086 | |
| 4087 | // If an exception was pending before the invoke, restore it now. |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4088 | if (old_exception != nullptr) { |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4089 | soa.Self()->SetException(old_exception.Get()); |
| 4090 | } |
| 4091 | } |
| 4092 | |
| 4093 | // Helper function: write a variable-width value into the output input buffer. |
| 4094 | static void WriteValue(JDWP::ExpandBuf* pReply, int width, uint64_t value) { |
| 4095 | switch (width) { |
| 4096 | case 1: |
| 4097 | expandBufAdd1(pReply, value); |
| 4098 | break; |
| 4099 | case 2: |
| 4100 | expandBufAdd2BE(pReply, value); |
| 4101 | break; |
| 4102 | case 4: |
| 4103 | expandBufAdd4BE(pReply, value); |
| 4104 | break; |
| 4105 | case 8: |
| 4106 | expandBufAdd8BE(pReply, value); |
| 4107 | break; |
| 4108 | default: |
| 4109 | LOG(FATAL) << width; |
| 4110 | UNREACHABLE(); |
| 4111 | } |
| 4112 | } |
| 4113 | |
| 4114 | void Dbg::ExecuteMethodWithoutPendingException(ScopedObjectAccess& soa, DebugInvokeReq* pReq) { |
| 4115 | soa.Self()->AssertNoPendingException(); |
| 4116 | |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4117 | // 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] | 4118 | ArtMethod* m = pReq->method; |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 4119 | PointerSize image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4120 | if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver.Read() != nullptr) { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4121 | ArtMethod* actual_method = |
| 4122 | pReq->klass.Read()->FindVirtualMethodForVirtualOrInterface(m, image_pointer_size); |
| 4123 | if (actual_method != m) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4124 | VLOG(jdwp) << "ExecuteMethod translated " << ArtMethod::PrettyMethod(m) |
| 4125 | << " to " << ArtMethod::PrettyMethod(actual_method); |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4126 | m = actual_method; |
| Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 4127 | } |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4128 | } |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4129 | VLOG(jdwp) << "ExecuteMethod " << ArtMethod::PrettyMethod(m) |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4130 | << " receiver=" << pReq->receiver.Read() |
| Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 4131 | << " arg_count=" << pReq->arg_count; |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4132 | CHECK(m != nullptr); |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4133 | |
| Roland Levillain | 33d6903 | 2015-06-18 18:20:59 +0100 | [diff] [blame] | 4134 | 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] | 4135 | |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4136 | // Invoke the method. |
| Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 4137 | ScopedLocalRef<jobject> ref(soa.Env(), soa.AddLocalReference<jobject>(pReq->receiver.Read())); |
| Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 4138 | JValue result = InvokeWithJValues(soa, ref.get(), jni::EncodeArtMethod(m), |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4139 | reinterpret_cast<jvalue*>(pReq->arg_values.get())); |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4140 | |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4141 | // Prepare JDWP ids for the reply. |
| 4142 | JDWP::JdwpTag result_tag = BasicTagFromDescriptor(m->GetShorty()); |
| 4143 | const bool is_object_result = (result_tag == JDWP::JT_OBJECT); |
| Jeff Hao | 064d24e | 2016-08-25 03:52:40 +0000 | [diff] [blame] | 4144 | StackHandleScope<3> hs(soa.Self()); |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4145 | Handle<mirror::Object> object_result = hs.NewHandle(is_object_result ? result.GetL() : nullptr); |
| 4146 | Handle<mirror::Throwable> exception = hs.NewHandle(soa.Self()->GetException()); |
| 4147 | soa.Self()->ClearException(); |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4148 | |
| 4149 | if (!IsDebuggerActive()) { |
| 4150 | // The debugger detached: we must not re-suspend threads. We also don't need to fill the reply |
| 4151 | // because it won't be sent either. |
| 4152 | return; |
| 4153 | } |
| 4154 | |
| 4155 | JDWP::ObjectId exceptionObjectId = gRegistry->Add(exception); |
| 4156 | uint64_t result_value = 0; |
| 4157 | if (exceptionObjectId != 0) { |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4158 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exception.Get() |
| 4159 | << " " << exception->Dump(); |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4160 | result_value = 0; |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4161 | } else if (is_object_result) { |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4162 | /* if no exception was thrown, examine object result more closely */ |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4163 | JDWP::JdwpTag new_tag = TagFromObject(soa, object_result.Get()); |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4164 | if (new_tag != result_tag) { |
| 4165 | VLOG(jdwp) << " JDWP promoted result from " << result_tag << " to " << new_tag; |
| 4166 | result_tag = new_tag; |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4167 | } |
| 4168 | |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4169 | // Register the object in the registry and reference its ObjectId. This ensures |
| 4170 | // 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] | 4171 | result_value = gRegistry->Add(object_result.Get()); |
| Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 4172 | } else { |
| 4173 | // Primitive result. |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4174 | DCHECK(IsPrimitiveTag(result_tag)); |
| 4175 | result_value = result.GetJ(); |
| 4176 | } |
| 4177 | const bool is_constructor = m->IsConstructor() && !m->IsStatic(); |
| 4178 | if (is_constructor) { |
| 4179 | // If we invoked a constructor (which actually returns void), return the receiver, |
| 4180 | // unless we threw, in which case we return null. |
| Sebastien Hertz | a3e1377 | 2015-11-05 12:09:44 +0100 | [diff] [blame] | 4181 | DCHECK_EQ(JDWP::JT_VOID, result_tag); |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4182 | if (exceptionObjectId == 0) { |
| Jeff Hao | 064d24e | 2016-08-25 03:52:40 +0000 | [diff] [blame] | 4183 | if (m->GetDeclaringClass()->IsStringClass()) { |
| 4184 | // For string constructors, the new string is remapped to the receiver (stored in ref). |
| 4185 | Handle<mirror::Object> decoded_ref = hs.NewHandle(soa.Self()->DecodeJObject(ref.get())); |
| 4186 | result_value = gRegistry->Add(decoded_ref); |
| 4187 | result_tag = TagFromObject(soa, decoded_ref.Get()); |
| 4188 | } else { |
| 4189 | // TODO we could keep the receiver ObjectId in the DebugInvokeReq to avoid looking into the |
| 4190 | // object registry. |
| 4191 | result_value = GetObjectRegistry()->Add(pReq->receiver.Read()); |
| 4192 | result_tag = TagFromObject(soa, pReq->receiver.Read()); |
| 4193 | } |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4194 | } else { |
| 4195 | result_value = 0; |
| Sebastien Hertz | a3e1377 | 2015-11-05 12:09:44 +0100 | [diff] [blame] | 4196 | result_tag = JDWP::JT_OBJECT; |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4197 | } |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4198 | } |
| 4199 | |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4200 | // Suspend other threads if the invoke is not single-threaded. |
| 4201 | if ((pReq->options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 4202 | ScopedThreadSuspension sts(soa.Self(), kWaitingForDebuggerSuspension); |
| Hiroshi Yamauchi | 8f95cf3 | 2016-04-19 11:14:06 -0700 | [diff] [blame] | 4203 | // Avoid a deadlock between GC and debugger where GC gets suspended during GC. b/25800335. |
| 4204 | gc::ScopedGCCriticalSection gcs(soa.Self(), gc::kGcCauseDebugger, gc::kCollectorTypeDebugger); |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4205 | VLOG(jdwp) << " Suspending all threads"; |
| 4206 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
| Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4207 | } |
| 4208 | |
| 4209 | VLOG(jdwp) << " --> returned " << result_tag |
| 4210 | << StringPrintf(" %#" PRIx64 " (except=%#" PRIx64 ")", result_value, |
| 4211 | exceptionObjectId); |
| 4212 | |
| 4213 | // Show detailed debug output. |
| 4214 | if (result_tag == JDWP::JT_STRING && exceptionObjectId == 0) { |
| 4215 | if (result_value != 0) { |
| 4216 | if (VLOG_IS_ON(jdwp)) { |
| 4217 | std::string result_string; |
| 4218 | JDWP::JdwpError error = Dbg::StringToUtf8(result_value, &result_string); |
| 4219 | CHECK_EQ(error, JDWP::ERR_NONE); |
| 4220 | VLOG(jdwp) << " string '" << result_string << "'"; |
| 4221 | } |
| 4222 | } else { |
| 4223 | VLOG(jdwp) << " string (null)"; |
| 4224 | } |
| 4225 | } |
| 4226 | |
| 4227 | // Attach the reply to DebugInvokeReq so it can be sent to the debugger when the event thread |
| 4228 | // is ready to suspend. |
| 4229 | BuildInvokeReply(pReq->reply, pReq->request_id, result_tag, result_value, exceptionObjectId); |
| 4230 | } |
| 4231 | |
| 4232 | void Dbg::BuildInvokeReply(JDWP::ExpandBuf* pReply, uint32_t request_id, JDWP::JdwpTag result_tag, |
| 4233 | uint64_t result_value, JDWP::ObjectId exception) { |
| 4234 | // Make room for the JDWP header since we do not know the size of the reply yet. |
| 4235 | JDWP::expandBufAddSpace(pReply, kJDWPHeaderLen); |
| 4236 | |
| 4237 | size_t width = GetTagWidth(result_tag); |
| 4238 | JDWP::expandBufAdd1(pReply, result_tag); |
| 4239 | if (width != 0) { |
| 4240 | WriteValue(pReply, width, result_value); |
| 4241 | } |
| 4242 | JDWP::expandBufAdd1(pReply, JDWP::JT_OBJECT); |
| 4243 | JDWP::expandBufAddObjectId(pReply, exception); |
| 4244 | |
| 4245 | // Now we know the size, we can complete the JDWP header. |
| 4246 | uint8_t* buf = expandBufGetBuffer(pReply); |
| 4247 | JDWP::Set4BE(buf + kJDWPHeaderSizeOffset, expandBufGetLength(pReply)); |
| 4248 | JDWP::Set4BE(buf + kJDWPHeaderIdOffset, request_id); |
| 4249 | JDWP::Set1(buf + kJDWPHeaderFlagsOffset, kJDWPFlagReply); // flags |
| 4250 | JDWP::Set2BE(buf + kJDWPHeaderErrorCodeOffset, JDWP::ERR_NONE); |
| 4251 | } |
| 4252 | |
| 4253 | void Dbg::FinishInvokeMethod(DebugInvokeReq* pReq) { |
| 4254 | CHECK_NE(Thread::Current(), GetDebugThread()) << "This must be called by the event thread"; |
| 4255 | |
| 4256 | JDWP::ExpandBuf* const pReply = pReq->reply; |
| 4257 | CHECK(pReply != nullptr) << "No reply attached to DebugInvokeReq"; |
| 4258 | |
| 4259 | // We need to prevent other threads (including JDWP thread) from interacting with the debugger |
| 4260 | // while we send the reply but are not yet suspended. The JDWP token will be released just before |
| 4261 | // we suspend ourself again (see ThreadList::SuspendSelfForDebugger). |
| 4262 | gJdwpState->AcquireJdwpTokenForEvent(pReq->thread_id); |
| 4263 | |
| 4264 | // Send the reply unless the debugger detached before the completion of the method. |
| 4265 | if (IsDebuggerActive()) { |
| 4266 | const size_t replyDataLength = expandBufGetLength(pReply) - kJDWPHeaderLen; |
| 4267 | VLOG(jdwp) << StringPrintf("REPLY INVOKE id=0x%06x (length=%zu)", |
| 4268 | pReq->request_id, replyDataLength); |
| 4269 | |
| 4270 | gJdwpState->SendRequest(pReply); |
| 4271 | } else { |
| 4272 | VLOG(jdwp) << "Not sending invoke reply because debugger detached"; |
| Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4273 | } |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4274 | } |
| 4275 | |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 4276 | bool Dbg::DdmHandleChunk(JNIEnv* env, |
| 4277 | uint32_t type, |
| 4278 | const ArrayRef<const jbyte>& data, |
| 4279 | /*out*/uint32_t* out_type, |
| 4280 | /*out*/std::vector<uint8_t>* out_data) { |
| 4281 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(data.size())); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4282 | if (dataArray.get() == nullptr) { |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 4283 | LOG(WARNING) << "byte[] allocation failed: " << data.size(); |
| Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4284 | env->ExceptionClear(); |
| 4285 | return false; |
| 4286 | } |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 4287 | env->SetByteArrayRegion(dataArray.get(), |
| 4288 | 0, |
| 4289 | data.size(), |
| 4290 | reinterpret_cast<const jbyte*>(data.data())); |
| Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4291 | // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)". |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 4292 | ScopedLocalRef<jobject> chunk( |
| 4293 | env, |
| 4294 | env->CallStaticObjectMethod( |
| 4295 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4296 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch, |
| 4297 | type, dataArray.get(), 0, data.size())); |
| Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4298 | if (env->ExceptionCheck()) { |
| Alex Light | 1eeefa6 | 2018-03-19 13:47:56 -0700 | [diff] [blame] | 4299 | Thread* self = Thread::Current(); |
| 4300 | ScopedObjectAccess soa(self); |
| 4301 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type) << std::endl |
| 4302 | << self->GetException()->Dump(); |
| 4303 | self->ClearException(); |
| Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4304 | return false; |
| 4305 | } |
| 4306 | |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4307 | if (chunk.get() == nullptr) { |
| Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4308 | return false; |
| 4309 | } |
| 4310 | |
| 4311 | /* |
| 4312 | * Pull the pieces out of the chunk. We copy the results into a |
| 4313 | * newly-allocated buffer that the caller can free. We don't want to |
| 4314 | * continue using the Chunk object because nothing has a reference to it. |
| 4315 | * |
| 4316 | * We could avoid this by returning type/data/offset/length and having |
| 4317 | * the caller be aware of the object lifetime issues, but that |
| Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 4318 | * 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] | 4319 | * if we have responses for multiple chunks. |
| 4320 | * |
| 4321 | * So we're pretty much stuck with copying data around multiple times. |
| 4322 | */ |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 4323 | ScopedLocalRef<jbyteArray> replyData( |
| 4324 | env, |
| 4325 | reinterpret_cast<jbyteArray>( |
| 4326 | env->GetObjectField( |
| 4327 | chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data))); |
| 4328 | jint offset = env->GetIntField(chunk.get(), |
| 4329 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset); |
| 4330 | jint length = env->GetIntField(chunk.get(), |
| 4331 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length); |
| 4332 | *out_type = env->GetIntField(chunk.get(), |
| 4333 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type); |
| Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4334 | |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 4335 | VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", |
| 4336 | type, |
| 4337 | replyData.get(), |
| 4338 | offset, |
| 4339 | length); |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 4340 | out_data->resize(length); |
| 4341 | env->GetByteArrayRegion(replyData.get(), |
| 4342 | offset, |
| 4343 | length, |
| 4344 | reinterpret_cast<jbyte*>(out_data->data())); |
| Alex Light | 6f2a634 | 2017-12-12 09:55:05 -0800 | [diff] [blame] | 4345 | |
| Alex Light | e5463a8 | 2017-12-12 13:33:28 -0800 | [diff] [blame] | 4346 | if (env->ExceptionCheck()) { |
| Alex Light | 1eeefa6 | 2018-03-19 13:47:56 -0700 | [diff] [blame] | 4347 | Thread* self = Thread::Current(); |
| 4348 | ScopedObjectAccess soa(self); |
| Alex Light | e5463a8 | 2017-12-12 13:33:28 -0800 | [diff] [blame] | 4349 | LOG(INFO) << StringPrintf("Exception thrown when reading response data from dispatcher 0x%08x", |
| Alex Light | 1eeefa6 | 2018-03-19 13:47:56 -0700 | [diff] [blame] | 4350 | type) << std::endl << self->GetException()->Dump(); |
| 4351 | self->ClearException(); |
| Alex Light | e5463a8 | 2017-12-12 13:33:28 -0800 | [diff] [blame] | 4352 | return false; |
| 4353 | } |
| 4354 | |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 4355 | return true; |
| 4356 | } |
| 4357 | |
| 4358 | /* |
| 4359 | * "request" contains a full JDWP packet, possibly with multiple chunks. We |
| 4360 | * need to process each, accumulate the replies, and ship the whole thing |
| 4361 | * back. |
| 4362 | * |
| 4363 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 4364 | * and includes the chunk type/length, followed by the data. |
| 4365 | * |
| 4366 | * OLD-TODO: we currently assume that the request and reply include a single |
| 4367 | * chunk. If this becomes inconvenient we will need to adapt. |
| 4368 | */ |
| 4369 | bool Dbg::DdmHandlePacket(JDWP::Request* request, uint8_t** pReplyBuf, int* pReplyLen) { |
| 4370 | Thread* self = Thread::Current(); |
| 4371 | JNIEnv* env = self->GetJniEnv(); |
| 4372 | |
| 4373 | uint32_t type = request->ReadUnsigned32("type"); |
| 4374 | uint32_t length = request->ReadUnsigned32("length"); |
| 4375 | |
| 4376 | // Create a byte[] corresponding to 'request'. |
| 4377 | size_t request_length = request->size(); |
| 4378 | // Run through and find all chunks. [Currently just find the first.] |
| 4379 | if (length != request_length) { |
| 4380 | 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] | 4381 | return false; |
| 4382 | } |
| Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4383 | |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 4384 | ArrayRef<const jbyte> data(reinterpret_cast<const jbyte*>(request->data()), request_length); |
| 4385 | std::vector<uint8_t> out_data; |
| 4386 | uint32_t out_type = 0; |
| 4387 | request->Skip(request_length); |
| Alex Light | 6f2a634 | 2017-12-12 09:55:05 -0800 | [diff] [blame] | 4388 | if (!DdmHandleChunk(env, type, data, &out_type, &out_data) || out_data.empty()) { |
| Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 4389 | return false; |
| 4390 | } |
| 4391 | const uint32_t kDdmHeaderSize = 8; |
| 4392 | *pReplyLen = out_data.size() + kDdmHeaderSize; |
| 4393 | *pReplyBuf = new uint8_t[out_data.size() + kDdmHeaderSize]; |
| 4394 | memcpy((*pReplyBuf) + kDdmHeaderSize, out_data.data(), out_data.size()); |
| 4395 | JDWP::Set4BE(*pReplyBuf, out_type); |
| 4396 | JDWP::Set4BE((*pReplyBuf) + 4, static_cast<uint32_t>(out_data.size())); |
| 4397 | VLOG(jdwp) |
| 4398 | << StringPrintf("dvmHandleDdm returning type=%.4s", reinterpret_cast<char*>(*pReplyBuf)) |
| 4399 | << "0x" << std::hex << reinterpret_cast<uintptr_t>(*pReplyBuf) << std::dec |
| 4400 | << " len= " << out_data.size(); |
| Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4401 | return true; |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4402 | } |
| 4403 | |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4404 | void Dbg::DdmBroadcast(bool connect) { |
| Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4405 | VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4406 | |
| 4407 | Thread* self = Thread::Current(); |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4408 | if (self->GetState() != kRunnable) { |
| 4409 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 4410 | /* try anyway? */ |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4411 | } |
| 4412 | |
| 4413 | JNIEnv* env = self->GetJniEnv(); |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4414 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
| Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4415 | env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4416 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast, |
| 4417 | event); |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4418 | if (env->ExceptionCheck()) { |
| 4419 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 4420 | env->ExceptionDescribe(); |
| 4421 | env->ExceptionClear(); |
| 4422 | } |
| 4423 | } |
| 4424 | |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4425 | void Dbg::DdmConnected() { |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4426 | Dbg::DdmBroadcast(true); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | void Dbg::DdmDisconnected() { |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4430 | Dbg::DdmBroadcast(false); |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4431 | gDdmThreadNotification = false; |
| 4432 | } |
| 4433 | |
| 4434 | /* |
| Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4435 | * Send a notification when a thread starts, stops, or changes its name. |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4436 | * |
| 4437 | * Because we broadcast the full set of threads when the notifications are |
| 4438 | * first enabled, it's possible for "thread" to be actively executing. |
| 4439 | */ |
| Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4440 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4441 | if (!gDdmThreadNotification) { |
| 4442 | return; |
| 4443 | } |
| 4444 | |
| Alex Light | 772099a | 2017-11-21 14:05:04 -0800 | [diff] [blame] | 4445 | RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks(); |
| Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4446 | if (type == CHUNK_TYPE("THDE")) { |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4447 | uint8_t buf[4]; |
| Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4448 | JDWP::Set4BE(&buf[0], t->GetThreadId()); |
| Alex Light | 772099a | 2017-11-21 14:05:04 -0800 | [diff] [blame] | 4449 | cb->DdmPublishChunk(CHUNK_TYPE("THDE"), ArrayRef<const uint8_t>(buf)); |
| Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4450 | } else { |
| 4451 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4452 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4453 | StackHandleScope<1> hs(soa.Self()); |
| Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 4454 | Handle<mirror::String> name(hs.NewHandle(t->GetThreadName())); |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4455 | size_t char_count = (name != nullptr) ? name->GetLength() : 0; |
| 4456 | const jchar* chars = (name != nullptr) ? name->GetValue() : nullptr; |
| 4457 | bool is_compressed = (name != nullptr) ? name->IsCompressed() : false; |
| Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4458 | |
| Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4459 | std::vector<uint8_t> bytes; |
| Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4460 | JDWP::Append4BE(bytes, t->GetThreadId()); |
| jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 4461 | if (is_compressed) { |
| 4462 | const uint8_t* chars_compressed = name->GetValueCompressed(); |
| 4463 | JDWP::AppendUtf16CompressedBE(bytes, chars_compressed, char_count); |
| 4464 | } else { |
| 4465 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
| 4466 | } |
| Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4467 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| Alex Light | 772099a | 2017-11-21 14:05:04 -0800 | [diff] [blame] | 4468 | cb->DdmPublishChunk(type, ArrayRef<const uint8_t>(bytes)); |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4469 | } |
| 4470 | } |
| 4471 | |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4472 | void Dbg::DdmSetThreadNotification(bool enable) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4473 | // Enable/disable thread notifications. |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4474 | gDdmThreadNotification = enable; |
| 4475 | if (enable) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4476 | // Suspend the VM then post thread start notifications for all threads. Threads attaching will |
| 4477 | // see a suspension in progress and block until that ends. They then post their own start |
| 4478 | // notification. |
| 4479 | SuspendVM(); |
| 4480 | std::list<Thread*> threads; |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4481 | Thread* self = Thread::Current(); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4482 | { |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4483 | MutexLock mu(self, *Locks::thread_list_lock_); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4484 | threads = Runtime::Current()->GetThreadList()->GetList(); |
| 4485 | } |
| 4486 | { |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4487 | ScopedObjectAccess soa(self); |
| Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4488 | for (Thread* thread : threads) { |
| 4489 | Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR")); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4490 | } |
| 4491 | } |
| 4492 | ResumeVM(); |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4493 | } |
| 4494 | } |
| 4495 | |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4496 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
| Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 4497 | if (IsDebuggerActive()) { |
| Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 4498 | gJdwpState->PostThreadChange(t, type == CHUNK_TYPE("THCR")); |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4499 | } |
| Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4500 | Dbg::DdmSendThreadNotification(t, type); |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | void Dbg::PostThreadStart(Thread* t) { |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4504 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
| Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | void Dbg::PostThreadDeath(Thread* t) { |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4508 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4509 | } |
| 4510 | |
| Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 4511 | JDWP::JdwpState* Dbg::GetJdwpState() { |
| 4512 | return gJdwpState; |
| 4513 | } |
| 4514 | |
| Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4515 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 4516 | if (when == HPIF_WHEN_NOW) { |
| Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4517 | DdmSendHeapInfo(when); |
| Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4518 | return true; |
| 4519 | } |
| 4520 | |
| 4521 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 4522 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 4523 | return false; |
| 4524 | } |
| 4525 | |
| 4526 | gDdmHpifWhen = when; |
| 4527 | return true; |
| 4528 | } |
| 4529 | |
| 4530 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 4531 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 4532 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 4533 | return false; |
| 4534 | } |
| 4535 | |
| 4536 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 4537 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 4538 | return false; |
| 4539 | } |
| 4540 | |
| 4541 | if (native) { |
| 4542 | gDdmNhsgWhen = when; |
| 4543 | gDdmNhsgWhat = what; |
| 4544 | } else { |
| 4545 | gDdmHpsgWhen = when; |
| 4546 | gDdmHpsgWhat = what; |
| 4547 | } |
| 4548 | return true; |
| 4549 | } |
| 4550 | |
| Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4551 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 4552 | // If there's a one-shot 'when', reset it. |
| 4553 | if (reason == gDdmHpifWhen) { |
| 4554 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 4555 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 4556 | } |
| 4557 | } |
| 4558 | |
| 4559 | /* |
| 4560 | * Chunk HPIF (client --> server) |
| 4561 | * |
| 4562 | * Heap Info. General information about the heap, |
| 4563 | * suitable for a summary display. |
| 4564 | * |
| 4565 | * [u4]: number of heaps |
| 4566 | * |
| 4567 | * For each heap: |
| 4568 | * [u4]: heap ID |
| 4569 | * [u8]: timestamp in ms since Unix epoch |
| 4570 | * [u1]: capture reason (same as 'when' value from server) |
| 4571 | * [u4]: max heap size in bytes (-Xmx) |
| 4572 | * [u4]: current heap size in bytes |
| 4573 | * [u4]: current number of bytes allocated |
| 4574 | * [u4]: current number of objects allocated |
| 4575 | */ |
| 4576 | uint8_t heap_count = 1; |
| Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4577 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4578 | std::vector<uint8_t> bytes; |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4579 | JDWP::Append4BE(bytes, heap_count); |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4580 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4581 | JDWP::Append8BE(bytes, MilliTime()); |
| 4582 | JDWP::Append1BE(bytes, reason); |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4583 | JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes. |
| 4584 | JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes. |
| Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4585 | JDWP::Append4BE(bytes, heap->GetBytesAllocated()); |
| 4586 | JDWP::Append4BE(bytes, heap->GetObjectsAllocated()); |
| Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4587 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| Alex Light | 772099a | 2017-11-21 14:05:04 -0800 | [diff] [blame] | 4588 | Runtime::Current()->GetRuntimeCallbacks()->DdmPublishChunk(CHUNK_TYPE("HPIF"), |
| 4589 | ArrayRef<const uint8_t>(bytes)); |
| Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4590 | } |
| 4591 | |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4592 | enum HpsgSolidity { |
| 4593 | SOLIDITY_FREE = 0, |
| 4594 | SOLIDITY_HARD = 1, |
| 4595 | SOLIDITY_SOFT = 2, |
| 4596 | SOLIDITY_WEAK = 3, |
| 4597 | SOLIDITY_PHANTOM = 4, |
| 4598 | SOLIDITY_FINALIZABLE = 5, |
| 4599 | SOLIDITY_SWEEP = 6, |
| 4600 | }; |
| 4601 | |
| 4602 | enum HpsgKind { |
| 4603 | KIND_OBJECT = 0, |
| 4604 | KIND_CLASS_OBJECT = 1, |
| 4605 | KIND_ARRAY_1 = 2, |
| 4606 | KIND_ARRAY_2 = 3, |
| 4607 | KIND_ARRAY_4 = 4, |
| 4608 | KIND_ARRAY_8 = 5, |
| 4609 | KIND_UNKNOWN = 6, |
| 4610 | KIND_NATIVE = 7, |
| 4611 | }; |
| 4612 | |
| 4613 | #define HPSG_PARTIAL (1<<7) |
| 4614 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 4615 | |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4616 | class HeapChunkContext { |
| 4617 | public: |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4618 | // Maximum chunk size. Obtain this from the formula: |
| 4619 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 4620 | HeapChunkContext(bool merge, bool native) |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4621 | : buf_(16384 - 16), |
| 4622 | type_(0), |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4623 | chunk_overhead_(0) { |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4624 | Reset(); |
| 4625 | if (native) { |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4626 | type_ = CHUNK_TYPE("NHSG"); |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4627 | } else { |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4628 | type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4629 | } |
| 4630 | } |
| 4631 | |
| 4632 | ~HeapChunkContext() { |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4633 | if (p_ > &buf_[0]) { |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4634 | Flush(); |
| 4635 | } |
| 4636 | } |
| 4637 | |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4638 | void SetChunkOverhead(size_t chunk_overhead) { |
| 4639 | chunk_overhead_ = chunk_overhead; |
| 4640 | } |
| 4641 | |
| 4642 | void ResetStartOfNextChunk() { |
| 4643 | startOfNextMemoryChunk_ = nullptr; |
| 4644 | } |
| 4645 | |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4646 | void EnsureHeader(const void* chunk_ptr) { |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4647 | if (!needHeader_) { |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4648 | return; |
| 4649 | } |
| 4650 | |
| 4651 | // Start a new HPSx chunk. |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4652 | JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap). |
| 4653 | JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes. |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4654 | |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4655 | JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 4656 | 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] | 4657 | // [u4]: length of piece, in allocation units |
| 4658 | // 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] | 4659 | pieceLenField_ = p_; |
| 4660 | JDWP::Write4BE(&p_, 0x55555555); |
| 4661 | needHeader_ = false; |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4662 | } |
| 4663 | |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4664 | void Flush() REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4665 | if (pieceLenField_ == nullptr) { |
| Ian Rogers | d636b06 | 2013-01-18 17:51:18 -0800 | [diff] [blame] | 4666 | // Flush immediately post Reset (maybe back-to-back Flush). Ignore. |
| 4667 | CHECK(needHeader_); |
| 4668 | return; |
| 4669 | } |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4670 | // Patch the "length of piece" field. |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4671 | CHECK_LE(&buf_[0], pieceLenField_); |
| 4672 | CHECK_LE(pieceLenField_, p_); |
| 4673 | JDWP::Set4BE(pieceLenField_, totalAllocationUnits_); |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4674 | |
| Alex Light | 772099a | 2017-11-21 14:05:04 -0800 | [diff] [blame] | 4675 | ArrayRef<const uint8_t> out(&buf_[0], p_ - &buf_[0]); |
| 4676 | Runtime::Current()->GetRuntimeCallbacks()->DdmPublishChunk(type_, out); |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4677 | Reset(); |
| 4678 | } |
| 4679 | |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4680 | 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] | 4681 | REQUIRES_SHARED(Locks::heap_bitmap_lock_, |
| Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4682 | Locks::mutator_lock_) { |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4683 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkJavaCallback(start, end, used_bytes); |
| 4684 | } |
| 4685 | |
| 4686 | 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] | 4687 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4688 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkNativeCallback(start, end, used_bytes); |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4689 | } |
| 4690 | |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4691 | private: |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4692 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 4693 | |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4694 | void Reset() { |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4695 | p_ = &buf_[0]; |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4696 | ResetStartOfNextChunk(); |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4697 | totalAllocationUnits_ = 0; |
| 4698 | needHeader_ = true; |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4699 | pieceLenField_ = nullptr; |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4700 | } |
| 4701 | |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4702 | bool IsNative() const { |
| 4703 | return type_ == CHUNK_TYPE("NHSG"); |
| 4704 | } |
| 4705 | |
| 4706 | // Returns true if the object is not an empty chunk. |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4707 | 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] | 4708 | // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken |
| 4709 | // 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] | 4710 | if (used_bytes == 0) { |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4711 | if (start == nullptr) { |
| 4712 | // Reset for start of new heap. |
| 4713 | startOfNextMemoryChunk_ = nullptr; |
| 4714 | Flush(); |
| 4715 | } |
| 4716 | // Only process in use memory so that free region information |
| 4717 | // also includes dlmalloc book keeping. |
| 4718 | return false; |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4719 | } |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4720 | if (startOfNextMemoryChunk_ != nullptr) { |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4721 | // Transmit any pending free memory. Native free memory of over kMaxFreeLen could be because |
| 4722 | // of the use of mmaps, so don't report. If not free memory then start a new segment. |
| 4723 | bool flush = true; |
| 4724 | if (start > startOfNextMemoryChunk_) { |
| 4725 | const size_t kMaxFreeLen = 2 * kPageSize; |
| 4726 | void* free_start = startOfNextMemoryChunk_; |
| 4727 | void* free_end = start; |
| 4728 | const size_t free_len = |
| 4729 | reinterpret_cast<uintptr_t>(free_end) - reinterpret_cast<uintptr_t>(free_start); |
| 4730 | if (!IsNative() || free_len < kMaxFreeLen) { |
| 4731 | AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), free_start, free_len, IsNative()); |
| 4732 | flush = false; |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4733 | } |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4734 | } |
| 4735 | if (flush) { |
| 4736 | startOfNextMemoryChunk_ = nullptr; |
| 4737 | Flush(); |
| 4738 | } |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4739 | } |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4740 | return true; |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4741 | } |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4742 | |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4743 | void HeapChunkNativeCallback(void* start, void* /*end*/, size_t used_bytes) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4744 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4745 | if (ProcessRecord(start, used_bytes)) { |
| 4746 | uint8_t state = ExamineNativeObject(start); |
| 4747 | AppendChunk(state, start, used_bytes + chunk_overhead_, true /*is_native*/); |
| 4748 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4749 | } |
| 4750 | } |
| 4751 | |
| 4752 | void HeapChunkJavaCallback(void* start, void* /*end*/, size_t used_bytes) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4753 | REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4754 | if (ProcessRecord(start, used_bytes)) { |
| 4755 | // Determine the type of this chunk. |
| 4756 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 4757 | // If it's the same, we should combine them. |
| 4758 | uint8_t state = ExamineJavaObject(reinterpret_cast<mirror::Object*>(start)); |
| 4759 | AppendChunk(state, start, used_bytes + chunk_overhead_, false /*is_native*/); |
| 4760 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4761 | } |
| 4762 | } |
| 4763 | |
| 4764 | 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] | 4765 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4766 | // Make sure there's enough room left in the buffer. |
| 4767 | // We need to use two bytes for every fractional 256 allocation units used by the chunk plus |
| 4768 | // 17 bytes for any header. |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4769 | const size_t needed = ((RoundUp(length / ALLOCATION_UNIT_SIZE, 256) / 256) * 2) + 17; |
| 4770 | size_t byte_left = &buf_.back() - p_; |
| 4771 | if (byte_left < needed) { |
| 4772 | if (is_native) { |
| Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4773 | // Cannot trigger memory allocation while walking native heap. |
| Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4774 | return; |
| 4775 | } |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4776 | Flush(); |
| 4777 | } |
| 4778 | |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4779 | byte_left = &buf_.back() - p_; |
| 4780 | if (byte_left < needed) { |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4781 | LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", " |
| 4782 | << needed << " bytes)"; |
| 4783 | return; |
| 4784 | } |
| 4785 | EnsureHeader(ptr); |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4786 | // Write out the chunk description. |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4787 | length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units. |
| 4788 | totalAllocationUnits_ += length; |
| 4789 | while (length > 256) { |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4790 | *p_++ = state | HPSG_PARTIAL; |
| 4791 | *p_++ = 255; // length - 1 |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4792 | length -= 256; |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4793 | } |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4794 | *p_++ = state; |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4795 | *p_++ = length - 1; |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4796 | } |
| 4797 | |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4798 | uint8_t ExamineNativeObject(const void* p) REQUIRES_SHARED(Locks::mutator_lock_) { |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4799 | return p == nullptr ? HPSG_STATE(SOLIDITY_FREE, 0) : HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4800 | } |
| 4801 | |
| 4802 | uint8_t ExamineJavaObject(mirror::Object* o) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4803 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4804 | if (o == nullptr) { |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4805 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 4806 | } |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4807 | // It's an allocated chunk. Figure out what it is. |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4808 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 4809 | if (!heap->IsLiveObjectLocked(o)) { |
| 4810 | LOG(ERROR) << "Invalid object in managed heap: " << o; |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4811 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4812 | } |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4813 | mirror::Class* c = o->GetClass(); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4814 | if (c == nullptr) { |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4815 | // The object was probably just created but hasn't been initialized yet. |
| 4816 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4817 | } |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4818 | if (!heap->IsValidObjectAddress(c)) { |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4819 | LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c; |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4820 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4821 | } |
| Mathieu Chartier | f26e1b3 | 2015-01-29 10:47:10 -0800 | [diff] [blame] | 4822 | if (c->GetClass() == nullptr) { |
| 4823 | LOG(ERROR) << "Null class of class " << c << " for object " << o; |
| 4824 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4825 | } |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4826 | if (c->IsClassClass()) { |
| 4827 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 4828 | } |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4829 | if (c->IsArrayClass()) { |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4830 | switch (c->GetComponentSize()) { |
| 4831 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 4832 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 4833 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 4834 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 4835 | } |
| 4836 | } |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4837 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4838 | } |
| 4839 | |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4840 | std::vector<uint8_t> buf_; |
| 4841 | uint8_t* p_; |
| 4842 | uint8_t* pieceLenField_; |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4843 | void* startOfNextMemoryChunk_; |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4844 | size_t totalAllocationUnits_; |
| 4845 | uint32_t type_; |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4846 | bool needHeader_; |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4847 | size_t chunk_overhead_; |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4848 | |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4849 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 4850 | }; |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4851 | |
| 4852 | void Dbg::DdmSendHeapSegments(bool native) { |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4853 | Dbg::HpsgWhen when = native ? gDdmNhsgWhen : gDdmHpsgWhen; |
| 4854 | Dbg::HpsgWhat what = native ? gDdmNhsgWhat : gDdmHpsgWhat; |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4855 | if (when == HPSG_WHEN_NEVER) { |
| 4856 | return; |
| 4857 | } |
| Alex Light | 772099a | 2017-11-21 14:05:04 -0800 | [diff] [blame] | 4858 | RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks(); |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4859 | // Figure out what kind of chunks we'll be sending. |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4860 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) |
| 4861 | << static_cast<int>(what); |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4862 | |
| 4863 | // First, send a heap start chunk. |
| 4864 | uint8_t heap_id[4]; |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4865 | JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap). |
| Alex Light | 772099a | 2017-11-21 14:05:04 -0800 | [diff] [blame] | 4866 | cb->DdmPublishChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), |
| 4867 | ArrayRef<const uint8_t>(heap_id)); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4868 | Thread* self = Thread::Current(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4869 | Locks::mutator_lock_->AssertSharedHeld(self); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4870 | |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4871 | // Send a series of heap segment chunks. |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4872 | HeapChunkContext context(what == HPSG_WHAT_MERGED_OBJECTS, native); |
| Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 4873 | auto bump_pointer_space_visitor = [&](mirror::Object* obj) |
| 4874 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
| 4875 | const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment); |
| 4876 | HeapChunkContext::HeapChunkJavaCallback( |
| 4877 | obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, &context); |
| 4878 | }; |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4879 | if (native) { |
| Dimitry Ivanov | e6465bc | 2015-12-14 18:55:02 -0800 | [diff] [blame] | 4880 | UNIMPLEMENTED(WARNING) << "Native heap inspection is not supported"; |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4881 | } else { |
| Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4882 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4883 | for (const auto& space : heap->GetContinuousSpaces()) { |
| 4884 | if (space->IsDlMallocSpace()) { |
| Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4885 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4886 | // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an |
| 4887 | // allocation then the first sizeof(size_t) may belong to it. |
| 4888 | context.SetChunkOverhead(sizeof(size_t)); |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4889 | space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4890 | } else if (space->IsRosAllocSpace()) { |
| 4891 | context.SetChunkOverhead(0); |
| Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4892 | // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since |
| 4893 | // 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] | 4894 | ScopedThreadSuspension sts(self, kSuspended); |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 4895 | ScopedSuspendAll ssa(__FUNCTION__); |
| 4896 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 4897 | space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4898 | } else if (space->IsBumpPointerSpace()) { |
| Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4899 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4900 | context.SetChunkOverhead(0); |
| Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 4901 | space->AsBumpPointerSpace()->Walk(bump_pointer_space_visitor); |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4902 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
| Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4903 | } else if (space->IsRegionSpace()) { |
| 4904 | heap->IncrementDisableMovingGC(self); |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 4905 | { |
| 4906 | ScopedThreadSuspension sts(self, kSuspended); |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 4907 | ScopedSuspendAll ssa(__FUNCTION__); |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 4908 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 4909 | context.SetChunkOverhead(0); |
| Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 4910 | space->AsRegionSpace()->Walk(bump_pointer_space_visitor); |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 4911 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 4912 | } |
| Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4913 | heap->DecrementDisableMovingGC(self); |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4914 | } else { |
| 4915 | UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space; |
| Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4916 | } |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4917 | context.ResetStartOfNextChunk(); |
| Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4918 | } |
| Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4919 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 4920 | // Walk the large objects, these are not in the AllocSpace. |
| Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4921 | context.SetChunkOverhead(0); |
| Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4922 | heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
| Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4923 | } |
| Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4924 | |
| 4925 | // Finally, send a heap end chunk. |
| Alex Light | 772099a | 2017-11-21 14:05:04 -0800 | [diff] [blame] | 4926 | cb->DdmPublishChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), |
| 4927 | ArrayRef<const uint8_t>(heap_id)); |
| Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4928 | } |
| 4929 | |
| Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4930 | void Dbg::SetAllocTrackingEnabled(bool enable) { |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4931 | gc::AllocRecordObjectMap::SetAllocTrackingEnabled(enable); |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | void Dbg::DumpRecentAllocations() { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4935 | ScopedObjectAccess soa(Thread::Current()); |
| Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4936 | MutexLock mu(soa.Self(), *Locks::alloc_tracker_lock_); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4937 | if (!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()) { |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4938 | LOG(INFO) << "Not recording tracked allocations"; |
| 4939 | return; |
| 4940 | } |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4941 | gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords(); |
| 4942 | CHECK(records != nullptr); |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4943 | |
| Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 4944 | const uint16_t capped_count = CappedAllocRecordCount(records->GetRecentAllocationSize()); |
| Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4945 | uint16_t count = capped_count; |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4946 | |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4947 | LOG(INFO) << "Tracked allocations, (count=" << count << ")"; |
| 4948 | for (auto it = records->RBegin(), end = records->REnd(); |
| 4949 | count > 0 && it != end; count--, it++) { |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 4950 | const gc::AllocRecord* record = &it->second; |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4951 | |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4952 | LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->GetTid(), record->ByteCount()) |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4953 | << mirror::Class::PrettyClass(record->GetClass()); |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4954 | |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4955 | for (size_t stack_frame = 0, depth = record->GetDepth(); stack_frame < depth; ++stack_frame) { |
| 4956 | const gc::AllocRecordStackTraceElement& stack_element = record->StackElement(stack_frame); |
| 4957 | ArtMethod* m = stack_element.GetMethod(); |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4958 | LOG(INFO) << " " << ArtMethod::PrettyMethod(m) << " line " |
| 4959 | << stack_element.ComputeLineNumber(); |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4960 | } |
| 4961 | |
| 4962 | // pause periodically to help logcat catch up |
| 4963 | if ((count % 5) == 0) { |
| 4964 | usleep(40000); |
| 4965 | } |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | class StringTable { |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 4970 | private: |
| 4971 | struct Entry { |
| Andreas Gampe | 84eadb2 | 2017-07-07 15:08:01 -0700 | [diff] [blame] | 4972 | explicit Entry(const char* data_in) |
| 4973 | : data(data_in), hash(ComputeModifiedUtf8Hash(data_in)), index(0) { |
| 4974 | } |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 4975 | Entry(const Entry& entry) = default; |
| 4976 | Entry(Entry&& entry) = default; |
| 4977 | |
| 4978 | // Pointer to the actual string data. |
| 4979 | const char* data; |
| Andreas Gampe | 84eadb2 | 2017-07-07 15:08:01 -0700 | [diff] [blame] | 4980 | |
| 4981 | // The hash of the data. |
| 4982 | const uint32_t hash; |
| 4983 | |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 4984 | // The index. This will be filled in on Finish and is not part of the ordering, so mark it |
| 4985 | // mutable. |
| 4986 | mutable uint32_t index; |
| 4987 | |
| Andreas Gampe | 84eadb2 | 2017-07-07 15:08:01 -0700 | [diff] [blame] | 4988 | bool operator==(const Entry& other) const { |
| 4989 | return strcmp(data, other.data) == 0; |
| 4990 | } |
| 4991 | }; |
| 4992 | struct EntryHash { |
| 4993 | size_t operator()(const Entry& entry) const { |
| 4994 | return entry.hash; |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 4995 | } |
| 4996 | }; |
| 4997 | |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4998 | public: |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 4999 | StringTable() : finished_(false) { |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5000 | } |
| 5001 | |
| Andreas Gampe | f774a4e | 2017-07-06 22:15:18 -0700 | [diff] [blame] | 5002 | void Add(const char* str, bool copy_string) { |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 5003 | DCHECK(!finished_); |
| Andreas Gampe | f774a4e | 2017-07-06 22:15:18 -0700 | [diff] [blame] | 5004 | if (UNLIKELY(copy_string)) { |
| 5005 | // Check whether it's already there. |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 5006 | Entry entry(str); |
| 5007 | if (table_.find(entry) != table_.end()) { |
| Andreas Gampe | f774a4e | 2017-07-06 22:15:18 -0700 | [diff] [blame] | 5008 | return; |
| 5009 | } |
| Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 5010 | |
| Andreas Gampe | f774a4e | 2017-07-06 22:15:18 -0700 | [diff] [blame] | 5011 | // Make a copy. |
| 5012 | size_t str_len = strlen(str); |
| 5013 | char* copy = new char[str_len + 1]; |
| 5014 | strlcpy(copy, str, str_len + 1); |
| 5015 | string_backup_.emplace_back(copy); |
| 5016 | str = copy; |
| 5017 | } |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 5018 | Entry entry(str); |
| 5019 | table_.insert(entry); |
| 5020 | } |
| 5021 | |
| 5022 | // Update all entries and give them an index. Note that this is likely not the insertion order, |
| 5023 | // as the set will with high likelihood reorder elements. Thus, Add must not be called after |
| 5024 | // Finish, and Finish must be called before IndexOf. In that case, WriteTo will walk in |
| 5025 | // the same order as Finish, and indices will agree. The order invariant, as well as indices, |
| 5026 | // are enforced through debug checks. |
| 5027 | void Finish() { |
| 5028 | DCHECK(!finished_); |
| 5029 | finished_ = true; |
| 5030 | uint32_t index = 0; |
| 5031 | for (auto& entry : table_) { |
| 5032 | entry.index = index; |
| 5033 | ++index; |
| 5034 | } |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5035 | } |
| 5036 | |
| Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 5037 | size_t IndexOf(const char* s) const { |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 5038 | DCHECK(finished_); |
| 5039 | Entry entry(s); |
| 5040 | auto it = table_.find(entry); |
| Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 5041 | if (it == table_.end()) { |
| 5042 | LOG(FATAL) << "IndexOf(\"" << s << "\") failed"; |
| 5043 | } |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 5044 | return it->index; |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5045 | } |
| 5046 | |
| Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 5047 | size_t Size() const { |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5048 | return table_.size(); |
| 5049 | } |
| 5050 | |
| Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 5051 | void WriteTo(std::vector<uint8_t>& bytes) const { |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 5052 | DCHECK(finished_); |
| 5053 | uint32_t cur_index = 0; |
| 5054 | for (const auto& entry : table_) { |
| 5055 | DCHECK_EQ(cur_index++, entry.index); |
| 5056 | |
| 5057 | size_t s_len = CountModifiedUtf8Chars(entry.data); |
| Christopher Ferris | 8a35405 | 2015-04-24 17:23:53 -0700 | [diff] [blame] | 5058 | std::unique_ptr<uint16_t[]> s_utf16(new uint16_t[s_len]); |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 5059 | ConvertModifiedUtf8ToUtf16(s_utf16.get(), entry.data); |
| Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5060 | JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len); |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5061 | } |
| 5062 | } |
| 5063 | |
| 5064 | private: |
| Andreas Gampe | 84eadb2 | 2017-07-07 15:08:01 -0700 | [diff] [blame] | 5065 | std::unordered_set<Entry, EntryHash> table_; |
| Andreas Gampe | f774a4e | 2017-07-06 22:15:18 -0700 | [diff] [blame] | 5066 | std::vector<std::unique_ptr<char[]>> string_backup_; |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 5067 | |
| 5068 | bool finished_; |
| 5069 | |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5070 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 5071 | }; |
| 5072 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5073 | static const char* GetMethodSourceFile(ArtMethod* method) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5074 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5075 | DCHECK(method != nullptr); |
| 5076 | const char* source_file = method->GetDeclaringClassSourceFile(); |
| Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 5077 | return (source_file != nullptr) ? source_file : ""; |
| 5078 | } |
| 5079 | |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5080 | /* |
| 5081 | * The data we send to DDMS contains everything we have recorded. |
| 5082 | * |
| 5083 | * Message header (all values big-endian): |
| 5084 | * (1b) message header len (to allow future expansion); includes itself |
| 5085 | * (1b) entry header len |
| 5086 | * (1b) stack frame len |
| 5087 | * (2b) number of entries |
| 5088 | * (4b) offset to string table from start of message |
| 5089 | * (2b) number of class name strings |
| 5090 | * (2b) number of method name strings |
| 5091 | * (2b) number of source file name strings |
| 5092 | * For each entry: |
| 5093 | * (4b) total allocation size |
| Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 5094 | * (2b) thread id |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5095 | * (2b) allocated object's class name index |
| 5096 | * (1b) stack depth |
| 5097 | * For each stack frame: |
| 5098 | * (2b) method's class name |
| 5099 | * (2b) method name |
| 5100 | * (2b) method source file |
| 5101 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 5102 | * (xb) class name strings |
| 5103 | * (xb) method name strings |
| 5104 | * (xb) source file strings |
| 5105 | * |
| 5106 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 5107 | * followed by UTF-16 data. |
| 5108 | * |
| 5109 | * 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] | 5110 | * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5111 | * each table, but in practice there should be far fewer. |
| 5112 | * |
| 5113 | * The chief reason for using a string table here is to keep the size of |
| 5114 | * the DDMS message to a minimum. This is partly to make the protocol |
| 5115 | * efficient, but also because we have to form the whole thing up all at |
| 5116 | * once in a memory buffer. |
| 5117 | * |
| 5118 | * We use separate string tables for class names, method names, and source |
| 5119 | * files to keep the indexes small. There will generally be no overlap |
| 5120 | * between the contents of these tables. |
| 5121 | */ |
| 5122 | jbyteArray Dbg::GetRecentAllocations() { |
| Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 5123 | if ((false)) { |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5124 | DumpRecentAllocations(); |
| 5125 | } |
| 5126 | |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 5127 | Thread* self = Thread::Current(); |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5128 | std::vector<uint8_t> bytes; |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5129 | { |
| Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 5130 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5131 | gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords(); |
| 5132 | // In case this method is called when allocation tracker is disabled, |
| 5133 | // we should still send some data back. |
| 5134 | gc::AllocRecordObjectMap dummy; |
| 5135 | if (records == nullptr) { |
| 5136 | CHECK(!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()); |
| 5137 | records = &dummy; |
| 5138 | } |
| Man Cao | 41656de | 2015-07-06 18:53:15 -0700 | [diff] [blame] | 5139 | // We don't need to wait on the condition variable records->new_record_condition_, because this |
| 5140 | // function only reads the class objects, which are already marked so it doesn't change their |
| 5141 | // reachability. |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5142 | |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5143 | // |
| 5144 | // Part 1: generate string tables. |
| 5145 | // |
| 5146 | StringTable class_names; |
| 5147 | StringTable method_names; |
| 5148 | StringTable filenames; |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5149 | |
| Andreas Gampe | ff29cee | 2017-07-07 11:11:15 -0700 | [diff] [blame] | 5150 | VLOG(jdwp) << "Collecting StringTables."; |
| 5151 | |
| Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 5152 | const uint16_t capped_count = CappedAllocRecordCount(records->GetRecentAllocationSize()); |
| Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 5153 | uint16_t count = capped_count; |
| Andreas Gampe | d0fc768 | 2017-07-07 14:03:08 -0700 | [diff] [blame] | 5154 | size_t alloc_byte_count = 0; |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5155 | for (auto it = records->RBegin(), end = records->REnd(); |
| 5156 | count > 0 && it != end; count--, it++) { |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 5157 | const gc::AllocRecord* record = &it->second; |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5158 | std::string temp; |
| Andreas Gampe | f774a4e | 2017-07-06 22:15:18 -0700 | [diff] [blame] | 5159 | const char* class_descr = record->GetClassDescriptor(&temp); |
| 5160 | class_names.Add(class_descr, !temp.empty()); |
| Andreas Gampe | d0fc768 | 2017-07-07 14:03:08 -0700 | [diff] [blame] | 5161 | |
| 5162 | // Size + tid + class name index + stack depth. |
| 5163 | alloc_byte_count += 4u + 2u + 2u + 1u; |
| 5164 | |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5165 | for (size_t i = 0, depth = record->GetDepth(); i < depth; i++) { |
| 5166 | ArtMethod* m = record->StackElement(i).GetMethod(); |
| Andreas Gampe | f774a4e | 2017-07-06 22:15:18 -0700 | [diff] [blame] | 5167 | class_names.Add(m->GetDeclaringClassDescriptor(), false); |
| 5168 | method_names.Add(m->GetName(), false); |
| 5169 | filenames.Add(GetMethodSourceFile(m), false); |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5170 | } |
| Andreas Gampe | d0fc768 | 2017-07-07 14:03:08 -0700 | [diff] [blame] | 5171 | |
| 5172 | // Depth * (class index + method name index + file name index + line number). |
| 5173 | alloc_byte_count += record->GetDepth() * (2u + 2u + 2u + 2u); |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5174 | } |
| 5175 | |
| Andreas Gampe | 2eeb01a | 2017-07-07 14:09:46 -0700 | [diff] [blame] | 5176 | class_names.Finish(); |
| 5177 | method_names.Finish(); |
| 5178 | filenames.Finish(); |
| Andreas Gampe | ff29cee | 2017-07-07 11:11:15 -0700 | [diff] [blame] | 5179 | VLOG(jdwp) << "Done collecting StringTables:" << std::endl |
| 5180 | << " ClassNames: " << class_names.Size() << std::endl |
| 5181 | << " MethodNames: " << method_names.Size() << std::endl |
| 5182 | << " Filenames: " << filenames.Size(); |
| 5183 | |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5184 | LOG(INFO) << "recent allocation records: " << capped_count; |
| 5185 | LOG(INFO) << "allocation records all objects: " << records->Size(); |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5186 | |
| 5187 | // |
| 5188 | // Part 2: Generate the output and store it in the buffer. |
| 5189 | // |
| 5190 | |
| 5191 | // (1b) message header len (to allow future expansion); includes itself |
| 5192 | // (1b) entry header len |
| 5193 | // (1b) stack frame len |
| 5194 | const int kMessageHeaderLen = 15; |
| 5195 | const int kEntryHeaderLen = 9; |
| 5196 | const int kStackFrameLen = 8; |
| 5197 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 5198 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 5199 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 5200 | |
| 5201 | // (2b) number of entries |
| 5202 | // (4b) offset to string table from start of message |
| 5203 | // (2b) number of class name strings |
| 5204 | // (2b) number of method name strings |
| 5205 | // (2b) number of source file name strings |
| Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 5206 | JDWP::Append2BE(bytes, capped_count); |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5207 | size_t string_table_offset = bytes.size(); |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 5208 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5209 | JDWP::Append2BE(bytes, class_names.Size()); |
| 5210 | JDWP::Append2BE(bytes, method_names.Size()); |
| 5211 | JDWP::Append2BE(bytes, filenames.Size()); |
| 5212 | |
| Andreas Gampe | ff29cee | 2017-07-07 11:11:15 -0700 | [diff] [blame] | 5213 | VLOG(jdwp) << "Dumping allocations with stacks"; |
| 5214 | |
| Andreas Gampe | d0fc768 | 2017-07-07 14:03:08 -0700 | [diff] [blame] | 5215 | // Enlarge the vector for the allocation data. |
| 5216 | size_t reserve_size = bytes.size() + alloc_byte_count; |
| 5217 | bytes.reserve(reserve_size); |
| 5218 | |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5219 | std::string temp; |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5220 | count = capped_count; |
| 5221 | // The last "count" number of allocation records in "records" are the most recent "count" number |
| 5222 | // of allocations. Reverse iterate to get them. The most recent allocation is sent first. |
| 5223 | for (auto it = records->RBegin(), end = records->REnd(); |
| 5224 | count > 0 && it != end; count--, it++) { |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5225 | // For each entry: |
| 5226 | // (4b) total allocation size |
| 5227 | // (2b) thread id |
| 5228 | // (2b) allocated object's class name index |
| 5229 | // (1b) stack depth |
| Mathieu Chartier | 458b105 | 2016-03-29 14:02:55 -0700 | [diff] [blame] | 5230 | const gc::AllocRecord* record = &it->second; |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5231 | size_t stack_depth = record->GetDepth(); |
| Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5232 | size_t allocated_object_class_name_index = |
| Man Cao | 41656de | 2015-07-06 18:53:15 -0700 | [diff] [blame] | 5233 | class_names.IndexOf(record->GetClassDescriptor(&temp)); |
| Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 5234 | JDWP::Append4BE(bytes, record->ByteCount()); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5235 | JDWP::Append2BE(bytes, static_cast<uint16_t>(record->GetTid())); |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5236 | JDWP::Append2BE(bytes, allocated_object_class_name_index); |
| 5237 | JDWP::Append1BE(bytes, stack_depth); |
| 5238 | |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5239 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 5240 | // For each stack frame: |
| 5241 | // (2b) method's class name |
| 5242 | // (2b) method name |
| 5243 | // (2b) method source file |
| 5244 | // (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] | 5245 | ArtMethod* m = record->StackElement(stack_frame).GetMethod(); |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5246 | size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor()); |
| 5247 | size_t method_name_index = method_names.IndexOf(m->GetName()); |
| 5248 | size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m)); |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5249 | JDWP::Append2BE(bytes, class_name_index); |
| 5250 | JDWP::Append2BE(bytes, method_name_index); |
| 5251 | JDWP::Append2BE(bytes, file_name_index); |
| Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 5252 | JDWP::Append2BE(bytes, record->StackElement(stack_frame).ComputeLineNumber()); |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5253 | } |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5254 | } |
| 5255 | |
| Andreas Gampe | d0fc768 | 2017-07-07 14:03:08 -0700 | [diff] [blame] | 5256 | CHECK_EQ(bytes.size(), reserve_size); |
| Andreas Gampe | ff29cee | 2017-07-07 11:11:15 -0700 | [diff] [blame] | 5257 | VLOG(jdwp) << "Dumping tables."; |
| 5258 | |
| Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 5259 | // (xb) class name strings |
| 5260 | // (xb) method name strings |
| 5261 | // (xb) source file strings |
| 5262 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 5263 | class_names.WriteTo(bytes); |
| 5264 | method_names.WriteTo(bytes); |
| 5265 | filenames.WriteTo(bytes); |
| Andreas Gampe | ff29cee | 2017-07-07 11:11:15 -0700 | [diff] [blame] | 5266 | |
| 5267 | VLOG(jdwp) << "GetRecentAllocations: data created. " << bytes.size(); |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5268 | } |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 5269 | JNIEnv* env = self->GetJniEnv(); |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5270 | jbyteArray result = env->NewByteArray(bytes.size()); |
| Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 5271 | if (result != nullptr) { |
| Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 5272 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 5273 | } |
| 5274 | return result; |
| 5275 | } |
| 5276 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5277 | ArtMethod* DeoptimizationRequest::Method() const { |
| Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 5278 | return jni::DecodeArtMethod(method_); |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 5279 | } |
| 5280 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5281 | void DeoptimizationRequest::SetMethod(ArtMethod* m) { |
| Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 5282 | method_ = jni::EncodeArtMethod(m); |
| Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 5283 | } |
| 5284 | |
| Mathieu Chartier | a6b1ead | 2015-10-06 10:32:38 -0700 | [diff] [blame] | 5285 | void Dbg::VisitRoots(RootVisitor* visitor) { |
| 5286 | // Visit breakpoint roots, used to prevent unloading of methods with breakpoints. |
| 5287 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| 5288 | BufferedRootVisitor<128> root_visitor(visitor, RootInfo(kRootVMInternal)); |
| 5289 | for (Breakpoint& breakpoint : gBreakpoints) { |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5290 | breakpoint.Method()->VisitRoots(root_visitor, kRuntimePointerSize); |
| Mathieu Chartier | a6b1ead | 2015-10-06 10:32:38 -0700 | [diff] [blame] | 5291 | } |
| 5292 | } |
| 5293 | |
| Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 5294 | void Dbg::DbgThreadLifecycleCallback::ThreadStart(Thread* self) { |
| 5295 | Dbg::PostThreadStart(self); |
| 5296 | } |
| 5297 | |
| 5298 | void Dbg::DbgThreadLifecycleCallback::ThreadDeath(Thread* self) { |
| 5299 | Dbg::PostThreadDeath(self); |
| 5300 | } |
| 5301 | |
| Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 5302 | void Dbg::DbgClassLoadCallback::ClassLoad(Handle<mirror::Class> klass ATTRIBUTE_UNUSED) { |
| 5303 | // Ignore ClassLoad; |
| 5304 | } |
| 5305 | void Dbg::DbgClassLoadCallback::ClassPrepare(Handle<mirror::Class> temp_klass ATTRIBUTE_UNUSED, |
| 5306 | Handle<mirror::Class> klass) { |
| 5307 | Dbg::PostClassPrepare(klass.Get()); |
| 5308 | } |
| 5309 | |
| Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 5310 | } // namespace art |