Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "debugger.h" |
| 18 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 19 | #include <sys/uio.h> |
| 20 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 21 | #include <set> |
| 22 | |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 23 | #include "arch/context.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 24 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "art_method-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 26 | #include "base/time_utils.h" |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 27 | #include "class_linker.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 29 | #include "dex_file-inl.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 30 | #include "dex_instruction.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 31 | #include "gc/accounting/card_table-inl.h" |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 32 | #include "gc/allocation_record.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 33 | #include "gc/space/large_object_space.h" |
| 34 | #include "gc/space/space-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 35 | #include "handle_scope.h" |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 36 | #include "jdwp/jdwp_priv.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 37 | #include "jdwp/object_registry.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | #include "mirror/class.h" |
| 39 | #include "mirror/class-inl.h" |
| 40 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 41 | #include "mirror/object-inl.h" |
| 42 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 43 | #include "mirror/string-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | #include "mirror/throwable.h" |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 45 | #include "quick/inline_method_analyser.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 46 | #include "reflection.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 47 | #include "safe_map.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 48 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 49 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 50 | #include "ScopedPrimitiveArray.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 51 | #include "handle_scope-inl.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 52 | #include "thread_list.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | #include "utf.h" |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 54 | #include "verifier/method_verifier-inl.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 55 | #include "well_known_classes.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 56 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 57 | namespace art { |
| 58 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 59 | // The key identifying the debugger to update instrumentation. |
| 60 | static constexpr const char* kDbgInstrumentationKey = "Debugger"; |
| 61 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 62 | // 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] | 63 | static uint16_t CappedAllocRecordCount(size_t alloc_record_count) { |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 64 | const size_t cap = 0xffff; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 65 | if (alloc_record_count > cap) { |
| 66 | return cap; |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 67 | } |
| 68 | return alloc_record_count; |
| 69 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 70 | |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 71 | class Breakpoint { |
| 72 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 73 | Breakpoint(ArtMethod* method, uint32_t dex_pc, |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 74 | DeoptimizationRequest::Kind deoptimization_kind) |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 75 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 76 | : method_(nullptr), dex_pc_(dex_pc), deoptimization_kind_(deoptimization_kind) { |
| 77 | CHECK(deoptimization_kind_ == DeoptimizationRequest::kNothing || |
| 78 | deoptimization_kind_ == DeoptimizationRequest::kSelectiveDeoptimization || |
| 79 | deoptimization_kind_ == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 80 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 81 | method_ = soa.EncodeMethod(method); |
| 82 | } |
| 83 | |
| 84 | Breakpoint(const Breakpoint& other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 85 | : method_(nullptr), dex_pc_(other.dex_pc_), |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 86 | deoptimization_kind_(other.deoptimization_kind_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 87 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 88 | method_ = soa.EncodeMethod(other.Method()); |
| 89 | } |
| 90 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 91 | ArtMethod* Method() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 92 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 93 | return soa.DecodeMethod(method_); |
| 94 | } |
| 95 | |
| 96 | uint32_t DexPc() const { |
| 97 | return dex_pc_; |
| 98 | } |
| 99 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 100 | DeoptimizationRequest::Kind GetDeoptimizationKind() const { |
| 101 | return deoptimization_kind_; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | private: |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 105 | // The location of this breakpoint. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 106 | jmethodID method_; |
| 107 | uint32_t dex_pc_; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 108 | |
| 109 | // Indicates whether breakpoint needs full deoptimization or selective deoptimization. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 110 | DeoptimizationRequest::Kind deoptimization_kind_; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 113 | static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 114 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 115 | os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.Method()).c_str(), rhs.DexPc()); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 116 | return os; |
| 117 | } |
| 118 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 119 | class DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 120 | public: |
| 121 | DebugInstrumentationListener() {} |
| 122 | virtual ~DebugInstrumentationListener() {} |
| 123 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 124 | void MethodEntered(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 125 | uint32_t dex_pc) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 126 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 127 | if (method->IsNative()) { |
| 128 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 129 | return; |
| 130 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 131 | if (IsListeningToDexPcMoved()) { |
| 132 | // We also listen to kDexPcMoved instrumentation event so we know the DexPcMoved method is |
| 133 | // going to be called right after us. To avoid sending JDWP events twice for this location, |
| 134 | // we report the event in DexPcMoved. However, we must remind this is method entry so we |
| 135 | // send the METHOD_ENTRY event. And we can also group it with other events for this location |
| 136 | // like BREAKPOINT or SINGLE_STEP (or even METHOD_EXIT if this is a RETURN instruction). |
| 137 | thread->SetDebugMethodEntry(); |
| 138 | } else if (IsListeningToMethodExit() && IsReturn(method, dex_pc)) { |
| 139 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 140 | // RETURN so we know the MethodExited method is going to be called right after us. To avoid |
| 141 | // sending JDWP events twice for this location, we report the event(s) in MethodExited. |
| 142 | // However, we must remind this is method entry so we send the METHOD_ENTRY event. And we can |
| 143 | // also group it with other events for this location like BREAKPOINT or SINGLE_STEP. |
| 144 | thread->SetDebugMethodEntry(); |
| 145 | } else { |
| 146 | Dbg::UpdateDebugger(thread, this_object, method, 0, Dbg::kMethodEntry, nullptr); |
| 147 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 150 | void MethodExited(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 151 | uint32_t dex_pc, const JValue& return_value) |
| 152 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 153 | if (method->IsNative()) { |
| 154 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 155 | return; |
| 156 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 157 | uint32_t events = Dbg::kMethodExit; |
| 158 | if (thread->IsDebugMethodEntry()) { |
| 159 | // It is also the method entry. |
| 160 | DCHECK(IsReturn(method, dex_pc)); |
| 161 | events |= Dbg::kMethodEntry; |
| 162 | thread->ClearDebugMethodEntry(); |
| 163 | } |
| 164 | Dbg::UpdateDebugger(thread, this_object, method, dex_pc, events, &return_value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 167 | void MethodUnwind(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 168 | ArtMethod* method, uint32_t dex_pc) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 169 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 170 | // We're not recorded to listen to this kind of event, so complain. |
| 171 | LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method) |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 172 | << " " << dex_pc; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 175 | void DexPcMoved(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 176 | uint32_t new_dex_pc) |
| 177 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 178 | if (IsListeningToMethodExit() && IsReturn(method, new_dex_pc)) { |
| 179 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 180 | // RETURN so we know the MethodExited method is going to be called right after us. Like in |
| 181 | // MethodEntered, we delegate event reporting to MethodExited. |
| 182 | // Besides, if this RETURN instruction is the only one in the method, we can send multiple |
| 183 | // JDWP events in the same packet: METHOD_ENTRY, METHOD_EXIT, BREAKPOINT and/or SINGLE_STEP. |
| 184 | // Therefore, we must not clear the debug method entry flag here. |
| 185 | } else { |
| 186 | uint32_t events = 0; |
| 187 | if (thread->IsDebugMethodEntry()) { |
| 188 | // It is also the method entry. |
| 189 | events = Dbg::kMethodEntry; |
| 190 | thread->ClearDebugMethodEntry(); |
| 191 | } |
| 192 | Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc, events, nullptr); |
| 193 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 196 | void FieldRead(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 197 | ArtMethod* method, uint32_t dex_pc, ArtField* field) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 198 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 199 | Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 200 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 201 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 202 | void FieldWritten(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 203 | ArtMethod* method, uint32_t dex_pc, ArtField* field, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 204 | const JValue& field_value) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 205 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 206 | Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value); |
| 207 | } |
| 208 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 209 | void ExceptionCaught(Thread* thread ATTRIBUTE_UNUSED, mirror::Throwable* exception_object) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 210 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 211 | Dbg::PostException(exception_object); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 214 | // We only care about how many backward branches were executed in the Jit. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 215 | void BackwardBranch(Thread* /*thread*/, ArtMethod* method, int32_t dex_pc_offset) |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 216 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 217 | LOG(ERROR) << "Unexpected backward branch event in debugger " << PrettyMethod(method) |
| 218 | << " " << dex_pc_offset; |
| 219 | } |
| 220 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 221 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 222 | static bool IsReturn(ArtMethod* method, uint32_t dex_pc) |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 223 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 224 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
| 225 | const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]); |
| 226 | return instruction->IsReturn(); |
| 227 | } |
| 228 | |
| 229 | static bool IsListeningToDexPcMoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 230 | return IsListeningTo(instrumentation::Instrumentation::kDexPcMoved); |
| 231 | } |
| 232 | |
| 233 | static bool IsListeningToMethodExit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 234 | return IsListeningTo(instrumentation::Instrumentation::kMethodExited); |
| 235 | } |
| 236 | |
| 237 | static bool IsListeningTo(instrumentation::Instrumentation::InstrumentationEvent event) |
| 238 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 239 | return (Dbg::GetInstrumentationEvents() & event) != 0; |
| 240 | } |
| 241 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 242 | DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 243 | } gDebugInstrumentationListener; |
| 244 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 245 | // JDWP is allowed unless the Zygote forbids it. |
| 246 | static bool gJdwpAllowed = true; |
| 247 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 248 | // Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line? |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 249 | static bool gJdwpConfigured = false; |
| 250 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 251 | // JDWP options for debugging. Only valid if IsJdwpConfigured() is true. |
| 252 | static JDWP::JdwpOptions gJdwpOptions; |
| 253 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 254 | // Runtime JDWP state. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 255 | static JDWP::JdwpState* gJdwpState = nullptr; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 256 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 257 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 258 | static bool gDdmThreadNotification = false; |
| 259 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 260 | // DDMS GC-related settings. |
| 261 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 262 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 263 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 264 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 265 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 266 | |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 267 | bool Dbg::gDebuggerActive = false; |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 268 | bool Dbg::gDisposed = false; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 269 | ObjectRegistry* Dbg::gRegistry = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 270 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 271 | // Deoptimization support. |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 272 | std::vector<DeoptimizationRequest> Dbg::deoptimization_requests_; |
| 273 | size_t Dbg::full_deoptimization_event_count_ = 0; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 274 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 275 | // Instrumentation event reference counters. |
| 276 | size_t Dbg::dex_pc_change_event_ref_count_ = 0; |
| 277 | size_t Dbg::method_enter_event_ref_count_ = 0; |
| 278 | size_t Dbg::method_exit_event_ref_count_ = 0; |
| 279 | size_t Dbg::field_read_event_ref_count_ = 0; |
| 280 | size_t Dbg::field_write_event_ref_count_ = 0; |
| 281 | size_t Dbg::exception_catch_event_ref_count_ = 0; |
| 282 | uint32_t Dbg::instrumentation_events_ = 0; |
| 283 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 284 | // Breakpoints. |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 285 | static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 286 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 287 | void DebugInvokeReq::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 288 | receiver.VisitRootIfNonNull(visitor, root_info); // null for static method call. |
| 289 | klass.VisitRoot(visitor, root_info); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 292 | void SingleStepControl::AddDexPc(uint32_t dex_pc) { |
| 293 | dex_pcs_.insert(dex_pc); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 296 | bool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const { |
| 297 | return dex_pcs_.find(dex_pc) == dex_pcs_.end(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 300 | static bool IsBreakpoint(const ArtMethod* m, uint32_t dex_pc) |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 301 | LOCKS_EXCLUDED(Locks::breakpoint_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 302 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 303 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 304 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 305 | if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].Method() == m) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 306 | VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i]; |
| 307 | return true; |
| 308 | } |
| 309 | } |
| 310 | return false; |
| 311 | } |
| 312 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 313 | static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) |
| 314 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) { |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 315 | MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); |
| 316 | // A thread may be suspended for GC; in this code, we really want to know whether |
| 317 | // there's a debugger suspension active. |
| 318 | return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0; |
| 319 | } |
| 320 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 321 | static mirror::Array* DecodeNonNullArray(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 322 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 323 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 324 | if (o == nullptr) { |
| 325 | *error = JDWP::ERR_INVALID_OBJECT; |
| 326 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 327 | } |
| 328 | if (!o->IsArrayInstance()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 329 | *error = JDWP::ERR_INVALID_ARRAY; |
| 330 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 331 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 332 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 333 | return o->AsArray(); |
| 334 | } |
| 335 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 336 | static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 337 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 338 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 339 | if (o == nullptr) { |
| 340 | *error = JDWP::ERR_INVALID_OBJECT; |
| 341 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 342 | } |
| 343 | if (!o->IsClass()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 344 | *error = JDWP::ERR_INVALID_CLASS; |
| 345 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 346 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 347 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 348 | return o->AsClass(); |
| 349 | } |
| 350 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 351 | static Thread* DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, |
| 352 | JDWP::JdwpError* error) |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 353 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 354 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::thread_suspend_count_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 355 | mirror::Object* thread_peer = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 356 | if (thread_peer == nullptr) { |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 357 | // This isn't even an object. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 358 | *error = JDWP::ERR_INVALID_OBJECT; |
| 359 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 360 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 361 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 362 | mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 363 | if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) { |
| 364 | // This isn't a thread. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 365 | *error = JDWP::ERR_INVALID_THREAD; |
| 366 | return nullptr; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 369 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 370 | Thread* thread = Thread::FromManagedThread(soa, thread_peer); |
| 371 | // If thread is null then this a java.lang.Thread without a Thread*. Must be a un-started or a |
| 372 | // zombie. |
| 373 | *error = (thread == nullptr) ? JDWP::ERR_THREAD_NOT_ALIVE : JDWP::ERR_NONE; |
| 374 | return thread; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 377 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 378 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 379 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 380 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 381 | } |
| 382 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 383 | static JDWP::JdwpTag BasicTagFromClass(mirror::Class* klass) |
| 384 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 385 | std::string temp; |
| 386 | const char* descriptor = klass->GetDescriptor(&temp); |
| 387 | return BasicTagFromDescriptor(descriptor); |
| 388 | } |
| 389 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 390 | static JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 391 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 392 | CHECK(c != nullptr); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 393 | if (c->IsArrayClass()) { |
| 394 | return JDWP::JT_ARRAY; |
| 395 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 396 | if (c->IsStringClass()) { |
| 397 | return JDWP::JT_STRING; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 398 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 399 | if (c->IsClassClass()) { |
| 400 | return JDWP::JT_CLASS_OBJECT; |
| 401 | } |
| 402 | { |
| 403 | mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
| 404 | if (thread_class->IsAssignableFrom(c)) { |
| 405 | return JDWP::JT_THREAD; |
| 406 | } |
| 407 | } |
| 408 | { |
| 409 | mirror::Class* thread_group_class = |
| 410 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup); |
| 411 | if (thread_group_class->IsAssignableFrom(c)) { |
| 412 | return JDWP::JT_THREAD_GROUP; |
| 413 | } |
| 414 | } |
| 415 | { |
| 416 | mirror::Class* class_loader_class = |
| 417 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader); |
| 418 | if (class_loader_class->IsAssignableFrom(c)) { |
| 419 | return JDWP::JT_CLASS_LOADER; |
| 420 | } |
| 421 | } |
| 422 | return JDWP::JT_OBJECT; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Objects declared to hold Object might actually hold a more specific |
| 427 | * type. The debugger may take a special interest in these (e.g. it |
| 428 | * wants to display the contents of Strings), so we want to return an |
| 429 | * appropriate tag. |
| 430 | * |
| 431 | * Null objects are tagged JT_OBJECT. |
| 432 | */ |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 433 | JDWP::JdwpTag Dbg::TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 434 | return (o == nullptr) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 438 | switch (tag) { |
| 439 | case JDWP::JT_BOOLEAN: |
| 440 | case JDWP::JT_BYTE: |
| 441 | case JDWP::JT_CHAR: |
| 442 | case JDWP::JT_FLOAT: |
| 443 | case JDWP::JT_DOUBLE: |
| 444 | case JDWP::JT_INT: |
| 445 | case JDWP::JT_LONG: |
| 446 | case JDWP::JT_SHORT: |
| 447 | case JDWP::JT_VOID: |
| 448 | return true; |
| 449 | default: |
| 450 | return false; |
| 451 | } |
| 452 | } |
| 453 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 454 | void Dbg::StartJdwp() { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 455 | if (!gJdwpAllowed || !IsJdwpConfigured()) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 456 | // No JDWP for you! |
| 457 | return; |
| 458 | } |
| 459 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 460 | CHECK(gRegistry == nullptr); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 461 | gRegistry = new ObjectRegistry; |
| 462 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 463 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 464 | // debugger, passively listen for a debugger, or block waiting for a |
| 465 | // debugger. |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 466 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 467 | if (gJdwpState == nullptr) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 468 | // We probably failed because some other process has the port already, which means that |
| 469 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 470 | // talking to that other process. |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 471 | LOG(FATAL) << "Debugger thread failed to initialize"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // If a debugger has already attached, send the "welcome" message. |
| 475 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 476 | if (gJdwpState->IsActive()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 477 | ScopedObjectAccess soa(Thread::Current()); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 478 | gJdwpState->PostVMStart(); |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 479 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 482 | void Dbg::StopJdwp() { |
Sebastien Hertz | c6345ef | 2014-08-18 19:26:39 +0200 | [diff] [blame] | 483 | // Post VM_DEATH event before the JDWP connection is closed (either by the JDWP thread or the |
| 484 | // destruction of gJdwpState). |
| 485 | if (gJdwpState != nullptr && gJdwpState->IsActive()) { |
| 486 | gJdwpState->PostVMDeath(); |
| 487 | } |
Sebastien Hertz | 0376e6b | 2014-02-06 18:12:59 +0100 | [diff] [blame] | 488 | // 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] | 489 | Dispose(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 490 | delete gJdwpState; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 491 | gJdwpState = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 492 | delete gRegistry; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 493 | gRegistry = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 496 | void Dbg::GcDidFinish() { |
| 497 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 498 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 499 | VLOG(jdwp) << "Sending heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 500 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 501 | } |
| 502 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 503 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 504 | VLOG(jdwp) << "Dumping heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 505 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 506 | } |
| 507 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 508 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 509 | VLOG(jdwp) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 510 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 514 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 515 | gJdwpAllowed = allowed; |
| 516 | } |
| 517 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 518 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 519 | return Thread::Current()->GetInvokeReq(); |
| 520 | } |
| 521 | |
| 522 | Thread* Dbg::GetDebugThread() { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 523 | return (gJdwpState != nullptr) ? gJdwpState->GetDebugThread() : nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | void Dbg::ClearWaitForEventThread() { |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 527 | gJdwpState->ReleaseJdwpTokenForEvent(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 531 | CHECK(!gDebuggerConnected); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 532 | VLOG(jdwp) << "JDWP has attached"; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 533 | gDebuggerConnected = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 534 | gDisposed = false; |
| 535 | } |
| 536 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 537 | bool Dbg::RequiresDeoptimization() { |
| 538 | // We don't need deoptimization if everything runs with interpreter after |
| 539 | // enabling -Xint mode. |
| 540 | return !Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly(); |
| 541 | } |
| 542 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 543 | void Dbg::GoActive() { |
| 544 | // Enable all debugging features, including scans for breakpoints. |
| 545 | // This is a no-op if we're already active. |
| 546 | // Only called from the JDWP handler thread. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 547 | if (IsDebuggerActive()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 548 | return; |
| 549 | } |
| 550 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 551 | { |
| 552 | // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected? |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 553 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 554 | CHECK_EQ(gBreakpoints.size(), 0U); |
| 555 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 556 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 557 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 558 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 559 | CHECK_EQ(deoptimization_requests_.size(), 0U); |
| 560 | CHECK_EQ(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 561 | CHECK_EQ(dex_pc_change_event_ref_count_, 0U); |
| 562 | CHECK_EQ(method_enter_event_ref_count_, 0U); |
| 563 | CHECK_EQ(method_exit_event_ref_count_, 0U); |
| 564 | CHECK_EQ(field_read_event_ref_count_, 0U); |
| 565 | CHECK_EQ(field_write_event_ref_count_, 0U); |
| 566 | CHECK_EQ(exception_catch_event_ref_count_, 0U); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 567 | } |
| 568 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 569 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 570 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 571 | Thread* self = Thread::Current(); |
| 572 | ThreadState old_state = self->SetStateUnsafe(kRunnable); |
| 573 | CHECK_NE(old_state, kRunnable); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 574 | if (RequiresDeoptimization()) { |
| 575 | runtime->GetInstrumentation()->EnableDeoptimization(); |
| 576 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 577 | instrumentation_events_ = 0; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 578 | gDebuggerActive = true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 579 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 580 | runtime->GetThreadList()->ResumeAll(); |
| 581 | |
| 582 | LOG(INFO) << "Debugger is active"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 586 | CHECK(gDebuggerConnected); |
| 587 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 588 | LOG(INFO) << "Debugger is no longer active"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 589 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 590 | // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread |
| 591 | // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener |
| 592 | // and clear the object registry. |
| 593 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 594 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 595 | Thread* self = Thread::Current(); |
| 596 | ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 597 | |
| 598 | // Debugger may not be active at this point. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 599 | if (IsDebuggerActive()) { |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 600 | { |
| 601 | // Since we're going to disable deoptimization, we clear the deoptimization requests queue. |
| 602 | // This prevents us from having any pending deoptimization request when the debugger attaches |
| 603 | // to us again while no event has been requested yet. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 604 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 605 | deoptimization_requests_.clear(); |
| 606 | full_deoptimization_event_count_ = 0U; |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 607 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 608 | if (instrumentation_events_ != 0) { |
| 609 | runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener, |
| 610 | instrumentation_events_); |
| 611 | instrumentation_events_ = 0; |
| 612 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 613 | if (RequiresDeoptimization()) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 614 | runtime->GetInstrumentation()->DisableDeoptimization(kDbgInstrumentationKey); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 615 | } |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 616 | gDebuggerActive = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 617 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 618 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 619 | runtime->GetThreadList()->ResumeAll(); |
Sebastien Hertz | 55f6534 | 2015-01-13 22:48:34 +0100 | [diff] [blame] | 620 | |
| 621 | { |
| 622 | ScopedObjectAccess soa(self); |
| 623 | gRegistry->Clear(); |
| 624 | } |
| 625 | |
| 626 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 629 | void Dbg::ConfigureJdwp(const JDWP::JdwpOptions& jdwp_options) { |
| 630 | CHECK_NE(jdwp_options.transport, JDWP::kJdwpTransportUnknown); |
| 631 | gJdwpOptions = jdwp_options; |
| 632 | gJdwpConfigured = true; |
| 633 | } |
| 634 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 635 | bool Dbg::IsJdwpConfigured() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 636 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | int64_t Dbg::LastDebuggerActivity() { |
Elliott Hughes | ca95152 | 2011-12-05 12:01:32 -0800 | [diff] [blame] | 640 | return gJdwpState->LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 643 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 644 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 647 | std::string Dbg::GetClassName(JDWP::RefTypeId class_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 648 | JDWP::JdwpError error; |
| 649 | mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id, &error); |
| 650 | if (o == nullptr) { |
| 651 | if (error == JDWP::ERR_NONE) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 652 | return "null"; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 653 | } else { |
| 654 | return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id)); |
| 655 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 656 | } |
| 657 | if (!o->IsClass()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 658 | 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] | 659 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 660 | return GetClassName(o->AsClass()); |
| 661 | } |
| 662 | |
| 663 | std::string Dbg::GetClassName(mirror::Class* klass) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 664 | if (klass == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 665 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 666 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 667 | std::string temp; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 668 | return DescriptorToName(klass->GetDescriptor(&temp)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 671 | JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId* class_object_id) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 672 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 673 | mirror::Class* c = DecodeClass(id, &status); |
| 674 | if (c == nullptr) { |
| 675 | *class_object_id = 0; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 676 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 677 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 678 | *class_object_id = gRegistry->Add(c); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 679 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 682 | JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId* superclass_id) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 683 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 684 | mirror::Class* c = DecodeClass(id, &status); |
| 685 | if (c == nullptr) { |
| 686 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 687 | return status; |
| 688 | } |
| 689 | if (c->IsInterface()) { |
| 690 | // http://code.google.com/p/android/issues/detail?id=20856 |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 691 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 692 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 693 | *superclass_id = gRegistry->Add(c->GetSuperClass()); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 694 | } |
| 695 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 698 | JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 699 | JDWP::JdwpError error; |
| 700 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 701 | if (o == nullptr) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 702 | return JDWP::ERR_INVALID_OBJECT; |
| 703 | } |
| 704 | expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader())); |
| 705 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 708 | JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 709 | JDWP::JdwpError error; |
| 710 | mirror::Class* c = DecodeClass(id, &error); |
| 711 | if (c == nullptr) { |
| 712 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 713 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 714 | |
| 715 | uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask; |
| 716 | |
Yevgeny Rouban | de34eea | 2014-02-15 01:06:03 +0700 | [diff] [blame] | 717 | // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set, |
| 718 | // not interfaces. |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 719 | // 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] | 720 | if ((access_flags & kAccInterface) == 0) { |
| 721 | access_flags |= kAccSuper; |
| 722 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 723 | |
| 724 | expandBufAdd4BE(pReply, access_flags); |
| 725 | |
| 726 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 729 | JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) { |
| 730 | JDWP::JdwpError error; |
| 731 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 732 | if (o == nullptr) { |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 733 | return JDWP::ERR_INVALID_OBJECT; |
| 734 | } |
| 735 | |
| 736 | // Ensure all threads are suspended while we read objects' lock words. |
| 737 | Thread* self = Thread::Current(); |
Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 738 | CHECK_EQ(self->GetState(), kRunnable); |
| 739 | self->TransitionFromRunnableToSuspended(kSuspended); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 740 | Runtime::Current()->GetThreadList()->SuspendAll(__FUNCTION__); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 741 | |
| 742 | MonitorInfo monitor_info(o); |
| 743 | |
Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 744 | Runtime::Current()->GetThreadList()->ResumeAll(); |
| 745 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 746 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 747 | if (monitor_info.owner_ != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 748 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 749 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 750 | expandBufAddObjectId(reply, gRegistry->Add(nullptr)); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 751 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 752 | expandBufAdd4BE(reply, monitor_info.entry_count_); |
| 753 | expandBufAdd4BE(reply, monitor_info.waiters_.size()); |
| 754 | for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) { |
| 755 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 756 | } |
| 757 | return JDWP::ERR_NONE; |
| 758 | } |
| 759 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 760 | JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 761 | std::vector<JDWP::ObjectId>* monitors, |
| 762 | std::vector<uint32_t>* stack_depths) { |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 763 | struct OwnedMonitorVisitor : public StackVisitor { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 764 | OwnedMonitorVisitor(Thread* thread, Context* context, |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 765 | std::vector<JDWP::ObjectId>* monitor_vector, |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 766 | std::vector<uint32_t>* stack_depth_vector) |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 767 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 768 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 769 | current_stack_depth(0), |
| 770 | monitors(monitor_vector), |
| 771 | stack_depths(stack_depth_vector) {} |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 772 | |
| 773 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 774 | // annotalysis. |
| 775 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 776 | if (!GetMethod()->IsRuntimeMethod()) { |
| 777 | Monitor::VisitLocks(this, AppendOwnedMonitors, this); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 778 | ++current_stack_depth; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 779 | } |
| 780 | return true; |
| 781 | } |
| 782 | |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 783 | static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) |
| 784 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 785 | OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 786 | visitor->monitors->push_back(gRegistry->Add(owned_monitor)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 787 | visitor->stack_depths->push_back(visitor->current_stack_depth); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 788 | } |
| 789 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 790 | size_t current_stack_depth; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 791 | std::vector<JDWP::ObjectId>* const monitors; |
| 792 | std::vector<uint32_t>* const stack_depths; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 793 | }; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 794 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 795 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 796 | JDWP::JdwpError error; |
| 797 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 798 | if (thread == nullptr) { |
| 799 | return error; |
| 800 | } |
| 801 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 802 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 803 | } |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 804 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 805 | OwnedMonitorVisitor visitor(thread, context.get(), monitors, stack_depths); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 806 | visitor.WalkStack(); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 807 | return JDWP::ERR_NONE; |
| 808 | } |
| 809 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 810 | JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 811 | JDWP::ObjectId* contended_monitor) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 812 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 813 | *contended_monitor = 0; |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 814 | JDWP::JdwpError error; |
| 815 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 816 | if (thread == nullptr) { |
| 817 | return error; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 818 | } |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 819 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 820 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 821 | } |
| 822 | mirror::Object* contended_monitor_obj = Monitor::GetContendedMonitor(thread); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 823 | // Add() requires the thread_list_lock_ not held to avoid the lock |
| 824 | // level violation. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 825 | *contended_monitor = gRegistry->Add(contended_monitor_obj); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 826 | return JDWP::ERR_NONE; |
| 827 | } |
| 828 | |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 829 | JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 830 | std::vector<uint64_t>* counts) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 831 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 832 | heap->CollectGarbage(false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 833 | std::vector<mirror::Class*> classes; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 834 | counts->clear(); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 835 | for (size_t i = 0; i < class_ids.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 836 | JDWP::JdwpError error; |
| 837 | mirror::Class* c = DecodeClass(class_ids[i], &error); |
| 838 | if (c == nullptr) { |
| 839 | return error; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 840 | } |
| 841 | classes.push_back(c); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 842 | counts->push_back(0); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 843 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 844 | heap->CountInstances(classes, false, &(*counts)[0]); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 845 | return JDWP::ERR_NONE; |
| 846 | } |
| 847 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 848 | JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, |
| 849 | std::vector<JDWP::ObjectId>* instances) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 850 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 851 | // We only want reachable instances, so do a GC. |
| 852 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 853 | JDWP::JdwpError error; |
| 854 | mirror::Class* c = DecodeClass(class_id, &error); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 855 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 856 | return error; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 857 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 858 | std::vector<mirror::Object*> raw_instances; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 859 | Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances); |
| 860 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 861 | instances->push_back(gRegistry->Add(raw_instances[i])); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 862 | } |
| 863 | return JDWP::ERR_NONE; |
| 864 | } |
| 865 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 866 | JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 867 | std::vector<JDWP::ObjectId>* referring_objects) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 868 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 869 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 870 | JDWP::JdwpError error; |
| 871 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 872 | if (o == nullptr) { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 873 | return JDWP::ERR_INVALID_OBJECT; |
| 874 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 875 | std::vector<mirror::Object*> raw_instances; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 876 | heap->GetReferringObjects(o, max_count, raw_instances); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 877 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 878 | referring_objects->push_back(gRegistry->Add(raw_instances[i])); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 879 | } |
| 880 | return JDWP::ERR_NONE; |
| 881 | } |
| 882 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 883 | JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id) { |
| 884 | JDWP::JdwpError error; |
| 885 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 886 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 887 | return JDWP::ERR_INVALID_OBJECT; |
| 888 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 889 | gRegistry->DisableCollection(object_id); |
| 890 | return JDWP::ERR_NONE; |
| 891 | } |
| 892 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 893 | JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id) { |
| 894 | JDWP::JdwpError error; |
| 895 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 896 | // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI |
| 897 | // also ignores these cases and never return an error. However it's not obvious why this command |
| 898 | // should behave differently from DisableCollection and IsCollected commands. So let's be more |
| 899 | // strict and return an error if this happens. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 900 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 901 | return JDWP::ERR_INVALID_OBJECT; |
| 902 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 903 | gRegistry->EnableCollection(object_id); |
| 904 | return JDWP::ERR_NONE; |
| 905 | } |
| 906 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 907 | JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool* is_collected) { |
| 908 | *is_collected = true; |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 909 | if (object_id == 0) { |
| 910 | // Null object id is invalid. |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 911 | return JDWP::ERR_INVALID_OBJECT; |
| 912 | } |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 913 | // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However |
| 914 | // the RI seems to ignore this and assume object has been collected. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 915 | JDWP::JdwpError error; |
| 916 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 917 | if (o != nullptr) { |
| 918 | *is_collected = gRegistry->IsCollected(object_id); |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 919 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 920 | return JDWP::ERR_NONE; |
| 921 | } |
| 922 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 923 | void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 924 | gRegistry->DisposeObject(object_id, reference_count); |
| 925 | } |
| 926 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 927 | JDWP::JdwpTypeTag Dbg::GetTypeTag(mirror::Class* klass) { |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 928 | DCHECK(klass != nullptr); |
| 929 | if (klass->IsArrayClass()) { |
| 930 | return JDWP::TT_ARRAY; |
| 931 | } else if (klass->IsInterface()) { |
| 932 | return JDWP::TT_INTERFACE; |
| 933 | } else { |
| 934 | return JDWP::TT_CLASS; |
| 935 | } |
| 936 | } |
| 937 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 938 | JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 939 | JDWP::JdwpError error; |
| 940 | mirror::Class* c = DecodeClass(class_id, &error); |
| 941 | if (c == nullptr) { |
| 942 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 943 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 944 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 945 | JDWP::JdwpTypeTag type_tag = GetTypeTag(c); |
| 946 | expandBufAdd1(pReply, type_tag); |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 947 | expandBufAddRefTypeId(pReply, class_id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 948 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 951 | void Dbg::GetClassList(std::vector<JDWP::RefTypeId>* classes) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 952 | // Get the complete list of reference classes (i.e. all classes except |
| 953 | // the primitive types). |
| 954 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 955 | struct ClassListCreator { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 956 | explicit ClassListCreator(std::vector<JDWP::RefTypeId>* classes_in) : classes(classes_in) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 957 | } |
| 958 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 959 | static bool Visit(mirror::Class* c, void* arg) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 960 | return reinterpret_cast<ClassListCreator*>(arg)->Visit(c); |
| 961 | } |
| 962 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 963 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 964 | // annotalysis. |
| 965 | bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 966 | if (!c->IsPrimitive()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 967 | classes->push_back(gRegistry->AddRefType(c)); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 968 | } |
| 969 | return true; |
| 970 | } |
| 971 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 972 | std::vector<JDWP::RefTypeId>* const classes; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 973 | }; |
| 974 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 975 | ClassListCreator clc(classes); |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 976 | Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(ClassListCreator::Visit, |
| 977 | &clc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 980 | JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, |
| 981 | uint32_t* pStatus, std::string* pDescriptor) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 982 | JDWP::JdwpError error; |
| 983 | mirror::Class* c = DecodeClass(class_id, &error); |
| 984 | if (c == nullptr) { |
| 985 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 986 | } |
| 987 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 988 | if (c->IsArrayClass()) { |
| 989 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 990 | *pTypeTag = JDWP::TT_ARRAY; |
| 991 | } else { |
| 992 | if (c->IsErroneous()) { |
| 993 | *pStatus = JDWP::CS_ERROR; |
| 994 | } else { |
| 995 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 996 | } |
| 997 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 998 | } |
| 999 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1000 | if (pDescriptor != nullptr) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1001 | std::string temp; |
| 1002 | *pDescriptor = c->GetDescriptor(&temp); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1003 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1004 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1007 | void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>* ids) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1008 | std::vector<mirror::Class*> classes; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1009 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1010 | ids->clear(); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1011 | for (size_t i = 0; i < classes.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1012 | ids->push_back(gRegistry->Add(classes[i])); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1013 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1016 | JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) { |
| 1017 | JDWP::JdwpError error; |
| 1018 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1019 | if (o == nullptr) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1020 | return JDWP::ERR_INVALID_OBJECT; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1021 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1022 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1023 | JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass()); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1024 | JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1025 | |
| 1026 | expandBufAdd1(pReply, type_tag); |
| 1027 | expandBufAddRefTypeId(pReply, type_id); |
| 1028 | |
| 1029 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 1032 | JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1033 | JDWP::JdwpError error; |
| 1034 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1035 | if (c == nullptr) { |
| 1036 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1037 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1038 | std::string temp; |
| 1039 | *signature = c->GetDescriptor(&temp); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 1040 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1043 | JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string* result) { |
| 1044 | JDWP::JdwpError error; |
| 1045 | mirror::Class* c = DecodeClass(class_id, &error); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1046 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1047 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1048 | } |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1049 | const char* source_file = c->GetSourceFile(); |
| 1050 | if (source_file == nullptr) { |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 1051 | return JDWP::ERR_ABSENT_INFORMATION; |
| 1052 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1053 | *result = source_file; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1054 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1057 | JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t* tag) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1058 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1059 | JDWP::JdwpError error; |
| 1060 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1061 | if (error != JDWP::ERR_NONE) { |
| 1062 | *tag = JDWP::JT_VOID; |
| 1063 | return error; |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1064 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1065 | *tag = TagFromObject(soa, o); |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1066 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1069 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1070 | switch (tag) { |
| 1071 | case JDWP::JT_VOID: |
| 1072 | return 0; |
| 1073 | case JDWP::JT_BYTE: |
| 1074 | case JDWP::JT_BOOLEAN: |
| 1075 | return 1; |
| 1076 | case JDWP::JT_CHAR: |
| 1077 | case JDWP::JT_SHORT: |
| 1078 | return 2; |
| 1079 | case JDWP::JT_FLOAT: |
| 1080 | case JDWP::JT_INT: |
| 1081 | return 4; |
| 1082 | case JDWP::JT_ARRAY: |
| 1083 | case JDWP::JT_OBJECT: |
| 1084 | case JDWP::JT_STRING: |
| 1085 | case JDWP::JT_THREAD: |
| 1086 | case JDWP::JT_THREAD_GROUP: |
| 1087 | case JDWP::JT_CLASS_LOADER: |
| 1088 | case JDWP::JT_CLASS_OBJECT: |
| 1089 | return sizeof(JDWP::ObjectId); |
| 1090 | case JDWP::JT_DOUBLE: |
| 1091 | case JDWP::JT_LONG: |
| 1092 | return 8; |
| 1093 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1094 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1095 | return -1; |
| 1096 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1099 | JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int32_t* length) { |
| 1100 | JDWP::JdwpError error; |
| 1101 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
| 1102 | if (a == nullptr) { |
| 1103 | return error; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1104 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1105 | *length = a->GetLength(); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1106 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1109 | 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] | 1110 | JDWP::JdwpError error; |
| 1111 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1112 | if (a == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1113 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1114 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1115 | |
| 1116 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 1117 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1118 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1119 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1120 | JDWP::JdwpTag element_tag = BasicTagFromClass(a->GetClass()->GetComponentType()); |
| 1121 | expandBufAdd1(pReply, element_tag); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1122 | expandBufAdd4BE(pReply, count); |
| 1123 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1124 | if (IsPrimitiveTag(element_tag)) { |
| 1125 | size_t width = GetTagWidth(element_tag); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1126 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 1127 | if (width == 8) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1128 | 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] | 1129 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 1130 | } else if (width == 4) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1131 | 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] | 1132 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 1133 | } else if (width == 2) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1134 | 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] | 1135 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 1136 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1137 | 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] | 1138 | memcpy(dst, &src[offset * width], count * width); |
| 1139 | } |
| 1140 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1141 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1142 | mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1143 | for (int i = 0; i < count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1144 | mirror::Object* element = oa->Get(offset + i); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1145 | JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element) |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1146 | : element_tag; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1147 | expandBufAdd1(pReply, specific_tag); |
| 1148 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 1149 | } |
| 1150 | } |
| 1151 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1152 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1155 | template <typename T> |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1156 | 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] | 1157 | NO_THREAD_SAFETY_ANALYSIS { |
| 1158 | // TODO: fix when annotalysis correctly handles non-member functions. |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1159 | DCHECK(a->GetClass()->IsPrimitiveArray()); |
| 1160 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1161 | T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1162 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1163 | *dst++ = src->ReadValue(sizeof(T)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1167 | JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1168 | JDWP::Request* request) { |
| 1169 | JDWP::JdwpError error; |
| 1170 | mirror::Array* dst = DecodeNonNullArray(array_id, &error); |
| 1171 | if (dst == nullptr) { |
| 1172 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1173 | } |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1174 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1175 | if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1176 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1177 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1178 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1179 | JDWP::JdwpTag element_tag = BasicTagFromClass(dst->GetClass()->GetComponentType()); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1180 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1181 | if (IsPrimitiveTag(element_tag)) { |
| 1182 | size_t width = GetTagWidth(element_tag); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1183 | if (width == 8) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1184 | CopyArrayData<uint64_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1185 | } else if (width == 4) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1186 | CopyArrayData<uint32_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1187 | } else if (width == 2) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1188 | CopyArrayData<uint16_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1189 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1190 | CopyArrayData<uint8_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1191 | } |
| 1192 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1193 | mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>(); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1194 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1195 | JDWP::ObjectId id = request->ReadObjectId(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1196 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 1197 | if (error != JDWP::ERR_NONE) { |
| 1198 | return error; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1199 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1200 | oa->Set<false>(offset + i, o); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1204 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1207 | JDWP::JdwpError Dbg::CreateString(const std::string& str, JDWP::ObjectId* new_string_id) { |
| 1208 | Thread* self = Thread::Current(); |
| 1209 | mirror::String* new_string = mirror::String::AllocFromModifiedUtf8(self, str.c_str()); |
| 1210 | if (new_string == nullptr) { |
| 1211 | DCHECK(self->IsExceptionPending()); |
| 1212 | self->ClearException(); |
| 1213 | LOG(ERROR) << "Could not allocate string"; |
| 1214 | *new_string_id = 0; |
| 1215 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1216 | } |
| 1217 | *new_string_id = gRegistry->Add(new_string); |
| 1218 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1221 | 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] | 1222 | JDWP::JdwpError error; |
| 1223 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1224 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1225 | *new_object_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1226 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1227 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1228 | Thread* self = Thread::Current(); |
| 1229 | mirror::Object* new_object = c->AllocObject(self); |
| 1230 | if (new_object == nullptr) { |
| 1231 | DCHECK(self->IsExceptionPending()); |
| 1232 | self->ClearException(); |
| 1233 | LOG(ERROR) << "Could not allocate object of type " << PrettyDescriptor(c); |
| 1234 | *new_object_id = 0; |
| 1235 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1236 | } |
| 1237 | *new_object_id = gRegistry->Add(new_object); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1238 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 1241 | /* |
| 1242 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 1243 | */ |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1244 | JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length, |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1245 | JDWP::ObjectId* new_array_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1246 | JDWP::JdwpError error; |
| 1247 | mirror::Class* c = DecodeClass(array_class_id, &error); |
| 1248 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1249 | *new_array_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1250 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1251 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1252 | Thread* self = Thread::Current(); |
| 1253 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1254 | mirror::Array* new_array = mirror::Array::Alloc<true>(self, c, length, |
| 1255 | c->GetComponentSizeShift(), |
| 1256 | heap->GetCurrentAllocator()); |
| 1257 | if (new_array == nullptr) { |
| 1258 | DCHECK(self->IsExceptionPending()); |
| 1259 | self->ClearException(); |
| 1260 | LOG(ERROR) << "Could not allocate array of type " << PrettyDescriptor(c); |
| 1261 | *new_array_id = 0; |
| 1262 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1263 | } |
| 1264 | *new_array_id = gRegistry->Add(new_array); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1265 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1268 | JDWP::FieldId Dbg::ToFieldId(const ArtField* f) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1269 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1270 | } |
| 1271 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1272 | static JDWP::MethodId ToMethodId(const ArtMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1273 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1274 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1277 | static ArtField* FromFieldId(JDWP::FieldId fid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1278 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1279 | return reinterpret_cast<ArtField*>(static_cast<uintptr_t>(fid)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1282 | static ArtMethod* FromMethodId(JDWP::MethodId mid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1283 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1284 | return reinterpret_cast<ArtMethod*>(static_cast<uintptr_t>(mid)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1287 | bool Dbg::MatchThread(JDWP::ObjectId expected_thread_id, Thread* event_thread) { |
| 1288 | CHECK(event_thread != nullptr); |
| 1289 | JDWP::JdwpError error; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1290 | mirror::Object* expected_thread_peer = gRegistry->Get<mirror::Object*>( |
| 1291 | expected_thread_id, &error); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1292 | return expected_thread_peer == event_thread->GetPeer(); |
| 1293 | } |
| 1294 | |
| 1295 | bool Dbg::MatchLocation(const JDWP::JdwpLocation& expected_location, |
| 1296 | const JDWP::EventLocation& event_location) { |
| 1297 | if (expected_location.dex_pc != event_location.dex_pc) { |
| 1298 | return false; |
| 1299 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1300 | ArtMethod* m = FromMethodId(expected_location.method_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1301 | return m == event_location.method; |
| 1302 | } |
| 1303 | |
| 1304 | bool Dbg::MatchType(mirror::Class* event_class, JDWP::RefTypeId class_id) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1305 | if (event_class == nullptr) { |
| 1306 | return false; |
| 1307 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1308 | JDWP::JdwpError error; |
| 1309 | mirror::Class* expected_class = DecodeClass(class_id, &error); |
| 1310 | CHECK(expected_class != nullptr); |
| 1311 | return expected_class->IsAssignableFrom(event_class); |
| 1312 | } |
| 1313 | |
| 1314 | 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] | 1315 | ArtField* event_field) { |
| 1316 | ArtField* expected_field = FromFieldId(expected_field_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1317 | if (expected_field != event_field) { |
| 1318 | return false; |
| 1319 | } |
| 1320 | return Dbg::MatchType(event_field->GetDeclaringClass(), expected_type_id); |
| 1321 | } |
| 1322 | |
| 1323 | bool Dbg::MatchInstance(JDWP::ObjectId expected_instance_id, mirror::Object* event_instance) { |
| 1324 | JDWP::JdwpError error; |
| 1325 | mirror::Object* modifier_instance = gRegistry->Get<mirror::Object*>(expected_instance_id, &error); |
| 1326 | return modifier_instance == event_instance; |
| 1327 | } |
| 1328 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1329 | void Dbg::SetJdwpLocation(JDWP::JdwpLocation* location, ArtMethod* m, uint32_t dex_pc) |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 1330 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 1331 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 1332 | Locks::thread_suspend_count_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1333 | if (m == nullptr) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1334 | memset(location, 0, sizeof(*location)); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1335 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1336 | mirror::Class* c = m->GetDeclaringClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1337 | location->type_tag = GetTypeTag(c); |
| 1338 | location->class_id = gRegistry->AddRefType(c); |
| 1339 | location->method_id = ToMethodId(m); |
| 1340 | 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] | 1341 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1342 | } |
| 1343 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1344 | std::string Dbg::GetMethodName(JDWP::MethodId method_id) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1345 | ArtMethod* m = FromMethodId(method_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1346 | if (m == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1347 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1348 | } |
Sebastien Hertz | 415fd08 | 2015-06-01 11:42:27 +0200 | [diff] [blame] | 1349 | return m->GetInterfaceMethodIfProxy(sizeof(void*))->GetName(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1352 | std::string Dbg::GetFieldName(JDWP::FieldId field_id) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1353 | ArtField* f = FromFieldId(field_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1354 | if (f == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1355 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1356 | } |
| 1357 | return f->GetName(); |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1358 | } |
| 1359 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1360 | /* |
| 1361 | * Augment the access flags for synthetic methods and fields by setting |
| 1362 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 1363 | * flags not specified by the Java programming language. |
| 1364 | */ |
| 1365 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 1366 | accessFlags &= kAccJavaFlagsMask; |
| 1367 | if ((accessFlags & kAccSynthetic) != 0) { |
| 1368 | accessFlags |= 0xf0000000; |
| 1369 | } |
| 1370 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1373 | /* |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1374 | * Circularly shifts registers so that arguments come first. Debuggers |
| 1375 | * expect slots to begin with arguments, but dex code places them at |
| 1376 | * the end. |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1377 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1378 | static uint16_t MangleSlot(uint16_t slot, ArtMethod* m) |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1379 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1380 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1381 | if (code_item == nullptr) { |
| 1382 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1383 | // return the slot as is since all registers are arguments. |
| 1384 | LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m); |
| 1385 | return slot; |
| 1386 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1387 | uint16_t ins_size = code_item->ins_size_; |
| 1388 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1389 | if (slot >= locals_size) { |
| 1390 | return slot - locals_size; |
| 1391 | } else { |
| 1392 | return slot + ins_size; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1393 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1394 | } |
| 1395 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1396 | /* |
| 1397 | * Circularly shifts registers so that arguments come last. Reverts |
| 1398 | * slots to dex style argument placement. |
| 1399 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1400 | static uint16_t DemangleSlot(uint16_t slot, ArtMethod* m, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1401 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1402 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1403 | if (code_item == nullptr) { |
| 1404 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1405 | // return the slot as is since all registers are arguments. |
| 1406 | LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1407 | uint16_t vreg_count = ArtMethod::NumArgRegisters(m->GetShorty()); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1408 | if (slot < vreg_count) { |
| 1409 | *error = JDWP::ERR_NONE; |
| 1410 | return slot; |
| 1411 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1412 | } else { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1413 | if (slot < code_item->registers_size_) { |
| 1414 | uint16_t ins_size = code_item->ins_size_; |
| 1415 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1416 | *error = JDWP::ERR_NONE; |
| 1417 | return (slot < ins_size) ? slot + locals_size : slot - ins_size; |
| 1418 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1419 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1420 | |
| 1421 | // Slot is invalid in the method. |
| 1422 | LOG(ERROR) << "Invalid local slot " << slot << " for method " << PrettyMethod(m); |
| 1423 | *error = JDWP::ERR_INVALID_SLOT; |
| 1424 | return DexFile::kDexNoIndex16; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1425 | } |
| 1426 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1427 | JDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1428 | JDWP::JdwpError error; |
| 1429 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1430 | if (c == nullptr) { |
| 1431 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1432 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1433 | |
| 1434 | size_t instance_field_count = c->NumInstanceFields(); |
| 1435 | size_t static_field_count = c->NumStaticFields(); |
| 1436 | |
| 1437 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 1438 | |
| 1439 | for (size_t i = 0; i < instance_field_count + static_field_count; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1440 | ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1441 | expandBufAddFieldId(pReply, ToFieldId(f)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1442 | expandBufAddUtf8String(pReply, f->GetName()); |
| 1443 | expandBufAddUtf8String(pReply, f->GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1444 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1445 | static const char genericSignature[1] = ""; |
| 1446 | expandBufAddUtf8String(pReply, genericSignature); |
| 1447 | } |
| 1448 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 1449 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1450 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1451 | } |
| 1452 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1453 | JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1454 | JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1455 | JDWP::JdwpError error; |
| 1456 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1457 | if (c == nullptr) { |
| 1458 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1459 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1460 | |
| 1461 | size_t direct_method_count = c->NumDirectMethods(); |
| 1462 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 1463 | |
| 1464 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 1465 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1466 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 1467 | auto ptr_size = cl->GetImagePointerSize(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1468 | for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1469 | ArtMethod* m = i < direct_method_count ? |
| 1470 | c->GetDirectMethod(i, ptr_size) : c->GetVirtualMethod(i - direct_method_count, ptr_size); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1471 | expandBufAddMethodId(pReply, ToMethodId(m)); |
Sebastien Hertz | 415fd08 | 2015-06-01 11:42:27 +0200 | [diff] [blame] | 1472 | expandBufAddUtf8String(pReply, m->GetInterfaceMethodIfProxy(sizeof(void*))->GetName()); |
| 1473 | expandBufAddUtf8String(pReply, |
| 1474 | m->GetInterfaceMethodIfProxy(sizeof(void*))->GetSignature().ToString()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1475 | if (with_generic) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1476 | const char* generic_signature = ""; |
| 1477 | expandBufAddUtf8String(pReply, generic_signature); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1478 | } |
| 1479 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 1480 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1481 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1482 | } |
| 1483 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1484 | JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1485 | JDWP::JdwpError error; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1486 | Thread* self = Thread::Current(); |
| 1487 | StackHandleScope<1> hs(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1488 | Handle<mirror::Class> c(hs.NewHandle(DecodeClass(class_id, &error))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1489 | if (c.Get() == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1490 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1491 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1492 | size_t interface_count = c->NumDirectInterfaces(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1493 | expandBufAdd4BE(pReply, interface_count); |
| 1494 | for (size_t i = 0; i < interface_count; ++i) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1495 | expandBufAddRefTypeId(pReply, |
| 1496 | gRegistry->AddRefType(mirror::Class::GetDirectInterface(self, c, i))); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1497 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1498 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1501 | void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1502 | struct DebugCallbackContext { |
| 1503 | int numItems; |
| 1504 | JDWP::ExpandBuf* pReply; |
| 1505 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1506 | static bool Callback(void* context, uint32_t address, uint32_t line_number) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1507 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1508 | expandBufAdd8BE(pContext->pReply, address); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1509 | expandBufAdd4BE(pContext->pReply, line_number); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1510 | pContext->numItems++; |
Sebastien Hertz | f2910ee | 2013-10-19 16:39:24 +0200 | [diff] [blame] | 1511 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1512 | } |
| 1513 | }; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1514 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1515 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1516 | uint64_t start, end; |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1517 | if (code_item == nullptr) { |
| 1518 | DCHECK(m->IsNative() || m->IsProxyMethod()); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1519 | start = -1; |
| 1520 | end = -1; |
| 1521 | } else { |
| 1522 | start = 0; |
jeffhao | 14f0db9 | 2012-12-14 17:50:42 -0800 | [diff] [blame] | 1523 | // Return the index of the last instruction |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1524 | end = code_item->insns_size_in_code_units_ - 1; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | expandBufAdd8BE(pReply, start); |
| 1528 | expandBufAdd8BE(pReply, end); |
| 1529 | |
| 1530 | // Add numLines later |
| 1531 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 1532 | expandBufAdd4BE(pReply, 0); |
| 1533 | |
| 1534 | DebugCallbackContext context; |
| 1535 | context.numItems = 0; |
| 1536 | context.pReply = pReply; |
| 1537 | |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1538 | if (code_item != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1539 | m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1540 | DebugCallbackContext::Callback, nullptr, &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1541 | } |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1542 | |
| 1543 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1546 | void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, |
| 1547 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1548 | struct DebugCallbackContext { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1549 | ArtMethod* method; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1550 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1551 | size_t variable_count; |
| 1552 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1553 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1554 | static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, |
| 1555 | const char* name, const char* descriptor, const char* signature) |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1556 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1557 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1558 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1559 | VLOG(jdwp) << StringPrintf(" %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d", |
| 1560 | pContext->variable_count, startAddress, endAddress - startAddress, |
| 1561 | name, descriptor, signature, slot, |
| 1562 | MangleSlot(slot, pContext->method)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1563 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1564 | slot = MangleSlot(slot, pContext->method); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1565 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1566 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 1567 | expandBufAddUtf8String(pContext->pReply, name); |
| 1568 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1569 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1570 | expandBufAddUtf8String(pContext->pReply, signature); |
| 1571 | } |
| 1572 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 1573 | expandBufAdd4BE(pContext->pReply, slot); |
| 1574 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1575 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1576 | } |
| 1577 | }; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1578 | ArtMethod* m = FromMethodId(method_id); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1579 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1580 | // arg_count considers doubles and longs to take 2 units. |
| 1581 | // variable_count considers everything to take 1 unit. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1582 | std::string shorty(m->GetShorty()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1583 | expandBufAdd4BE(pReply, ArtMethod::NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1584 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1585 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1586 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1587 | expandBufAdd4BE(pReply, 0); |
| 1588 | |
| 1589 | DebugCallbackContext context; |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1590 | context.method = m; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1591 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1592 | context.variable_count = 0; |
| 1593 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1594 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1595 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1596 | if (code_item != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1597 | m->GetDexFile()->DecodeDebugInfo( |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1598 | code_item, m->IsStatic(), m->GetDexMethodIndex(), nullptr, DebugCallbackContext::Callback, |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1599 | &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1600 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1601 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1602 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1605 | void Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value, |
| 1606 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1607 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1608 | JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1609 | OutputJValue(tag, return_value, pReply); |
| 1610 | } |
| 1611 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1612 | void Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value, |
| 1613 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1614 | ArtField* f = FromFieldId(field_id); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1615 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1616 | OutputJValue(tag, field_value, pReply); |
| 1617 | } |
| 1618 | |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1619 | JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1620 | std::vector<uint8_t>* bytecodes) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1621 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1622 | if (m == nullptr) { |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1623 | return JDWP::ERR_INVALID_METHODID; |
| 1624 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1625 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1626 | size_t byte_count = code_item->insns_size_in_code_units_ * 2; |
| 1627 | const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_); |
| 1628 | const uint8_t* end = begin + byte_count; |
| 1629 | for (const uint8_t* p = begin; p != end; ++p) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1630 | bytecodes->push_back(*p); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1631 | } |
| 1632 | return JDWP::ERR_NONE; |
| 1633 | } |
| 1634 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1635 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1636 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1639 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1640 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1643 | static JValue GetArtFieldValue(ArtField* f, mirror::Object* o) |
| 1644 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1645 | Primitive::Type fieldType = f->GetTypeAsPrimitiveType(); |
| 1646 | JValue field_value; |
| 1647 | switch (fieldType) { |
| 1648 | case Primitive::kPrimBoolean: |
| 1649 | field_value.SetZ(f->GetBoolean(o)); |
| 1650 | return field_value; |
| 1651 | |
| 1652 | case Primitive::kPrimByte: |
| 1653 | field_value.SetB(f->GetByte(o)); |
| 1654 | return field_value; |
| 1655 | |
| 1656 | case Primitive::kPrimChar: |
| 1657 | field_value.SetC(f->GetChar(o)); |
| 1658 | return field_value; |
| 1659 | |
| 1660 | case Primitive::kPrimShort: |
| 1661 | field_value.SetS(f->GetShort(o)); |
| 1662 | return field_value; |
| 1663 | |
| 1664 | case Primitive::kPrimInt: |
| 1665 | case Primitive::kPrimFloat: |
| 1666 | // Int and Float must be treated as 32-bit values in JDWP. |
| 1667 | field_value.SetI(f->GetInt(o)); |
| 1668 | return field_value; |
| 1669 | |
| 1670 | case Primitive::kPrimLong: |
| 1671 | case Primitive::kPrimDouble: |
| 1672 | // Long and Double must be treated as 64-bit values in JDWP. |
| 1673 | field_value.SetJ(f->GetLong(o)); |
| 1674 | return field_value; |
| 1675 | |
| 1676 | case Primitive::kPrimNot: |
| 1677 | field_value.SetL(f->GetObject(o)); |
| 1678 | return field_value; |
| 1679 | |
| 1680 | case Primitive::kPrimVoid: |
| 1681 | LOG(FATAL) << "Attempt to read from field of type 'void'"; |
| 1682 | UNREACHABLE(); |
| 1683 | } |
| 1684 | LOG(FATAL) << "Attempt to read from field of unknown type"; |
| 1685 | UNREACHABLE(); |
| 1686 | } |
| 1687 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1688 | static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id, |
| 1689 | JDWP::FieldId field_id, JDWP::ExpandBuf* pReply, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1690 | bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1691 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1692 | JDWP::JdwpError error; |
| 1693 | mirror::Class* c = DecodeClass(ref_type_id, &error); |
| 1694 | if (ref_type_id != 0 && c == nullptr) { |
| 1695 | return error; |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1696 | } |
| 1697 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1698 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1699 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1700 | return JDWP::ERR_INVALID_OBJECT; |
| 1701 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1702 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1703 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1704 | mirror::Class* receiver_class = c; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1705 | if (receiver_class == nullptr && o != nullptr) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1706 | receiver_class = o->GetClass(); |
| 1707 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1708 | // TODO: should we give up now if receiver_class is null? |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1709 | if (receiver_class != nullptr && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1710 | LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1711 | return JDWP::ERR_INVALID_FIELDID; |
| 1712 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1713 | |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1714 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1715 | // TODO: should we change the tests and check both? |
| 1716 | if (is_static) { |
| 1717 | if (!f->IsStatic()) { |
| 1718 | return JDWP::ERR_INVALID_FIELDID; |
| 1719 | } |
| 1720 | } else { |
| 1721 | if (f->IsStatic()) { |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1722 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.GetValues" |
| 1723 | << " on static field " << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1724 | } |
| 1725 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1726 | if (f->IsStatic()) { |
| 1727 | o = f->GetDeclaringClass(); |
| 1728 | } |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1729 | |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1730 | JValue field_value(GetArtFieldValue(f, o)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1731 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1732 | Dbg::OutputJValue(tag, &field_value, pReply); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1733 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1736 | JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1737 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1738 | return GetFieldValueImpl(0, object_id, field_id, pReply, false); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1739 | } |
| 1740 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1741 | JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, |
| 1742 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1743 | return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1746 | static JDWP::JdwpError SetArtFieldValue(ArtField* f, mirror::Object* o, uint64_t value, int width) |
| 1747 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1748 | Primitive::Type fieldType = f->GetTypeAsPrimitiveType(); |
| 1749 | // Debugging only happens at runtime so we know we are not running in a transaction. |
| 1750 | static constexpr bool kNoTransactionMode = false; |
| 1751 | switch (fieldType) { |
| 1752 | case Primitive::kPrimBoolean: |
| 1753 | CHECK_EQ(width, 1); |
| 1754 | f->SetBoolean<kNoTransactionMode>(o, static_cast<uint8_t>(value)); |
| 1755 | return JDWP::ERR_NONE; |
| 1756 | |
| 1757 | case Primitive::kPrimByte: |
| 1758 | CHECK_EQ(width, 1); |
| 1759 | f->SetByte<kNoTransactionMode>(o, static_cast<uint8_t>(value)); |
| 1760 | return JDWP::ERR_NONE; |
| 1761 | |
| 1762 | case Primitive::kPrimChar: |
| 1763 | CHECK_EQ(width, 2); |
| 1764 | f->SetChar<kNoTransactionMode>(o, static_cast<uint16_t>(value)); |
| 1765 | return JDWP::ERR_NONE; |
| 1766 | |
| 1767 | case Primitive::kPrimShort: |
| 1768 | CHECK_EQ(width, 2); |
| 1769 | f->SetShort<kNoTransactionMode>(o, static_cast<int16_t>(value)); |
| 1770 | return JDWP::ERR_NONE; |
| 1771 | |
| 1772 | case Primitive::kPrimInt: |
| 1773 | case Primitive::kPrimFloat: |
| 1774 | CHECK_EQ(width, 4); |
| 1775 | // Int and Float must be treated as 32-bit values in JDWP. |
| 1776 | f->SetInt<kNoTransactionMode>(o, static_cast<int32_t>(value)); |
| 1777 | return JDWP::ERR_NONE; |
| 1778 | |
| 1779 | case Primitive::kPrimLong: |
| 1780 | case Primitive::kPrimDouble: |
| 1781 | CHECK_EQ(width, 8); |
| 1782 | // Long and Double must be treated as 64-bit values in JDWP. |
| 1783 | f->SetLong<kNoTransactionMode>(o, value); |
| 1784 | return JDWP::ERR_NONE; |
| 1785 | |
| 1786 | case Primitive::kPrimNot: { |
| 1787 | JDWP::JdwpError error; |
| 1788 | mirror::Object* v = Dbg::GetObjectRegistry()->Get<mirror::Object*>(value, &error); |
| 1789 | if (error != JDWP::ERR_NONE) { |
| 1790 | return JDWP::ERR_INVALID_OBJECT; |
| 1791 | } |
| 1792 | if (v != nullptr) { |
| 1793 | mirror::Class* field_type; |
| 1794 | { |
| 1795 | StackHandleScope<2> hs(Thread::Current()); |
| 1796 | HandleWrapper<mirror::Object> h_v(hs.NewHandleWrapper(&v)); |
| 1797 | HandleWrapper<mirror::Object> h_o(hs.NewHandleWrapper(&o)); |
| 1798 | field_type = f->GetType<true>(); |
| 1799 | } |
| 1800 | if (!field_type->IsAssignableFrom(v->GetClass())) { |
| 1801 | return JDWP::ERR_INVALID_OBJECT; |
| 1802 | } |
| 1803 | } |
| 1804 | f->SetObject<kNoTransactionMode>(o, v); |
| 1805 | return JDWP::ERR_NONE; |
| 1806 | } |
| 1807 | |
| 1808 | case Primitive::kPrimVoid: |
| 1809 | LOG(FATAL) << "Attempt to write to field of type 'void'"; |
| 1810 | UNREACHABLE(); |
| 1811 | } |
| 1812 | LOG(FATAL) << "Attempt to write to field of unknown type"; |
| 1813 | UNREACHABLE(); |
| 1814 | } |
| 1815 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1816 | static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1817 | uint64_t value, int width, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1818 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1819 | JDWP::JdwpError error; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1820 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1821 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1822 | return JDWP::ERR_INVALID_OBJECT; |
| 1823 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1824 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1825 | |
| 1826 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1827 | // TODO: should we change the tests and check both? |
| 1828 | if (is_static) { |
| 1829 | if (!f->IsStatic()) { |
| 1830 | return JDWP::ERR_INVALID_FIELDID; |
| 1831 | } |
| 1832 | } else { |
| 1833 | if (f->IsStatic()) { |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1834 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues" |
| 1835 | << " on static field " << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1836 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1837 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1838 | if (f->IsStatic()) { |
| 1839 | o = f->GetDeclaringClass(); |
| 1840 | } |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1841 | return SetArtFieldValue(f, o, value, width); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1844 | 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] | 1845 | int width) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1846 | return SetFieldValueImpl(object_id, field_id, value, width, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1849 | JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) { |
| 1850 | return SetFieldValueImpl(0, field_id, value, width, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1853 | JDWP::JdwpError Dbg::StringToUtf8(JDWP::ObjectId string_id, std::string* str) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1854 | JDWP::JdwpError error; |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1855 | mirror::Object* obj = gRegistry->Get<mirror::Object*>(string_id, &error); |
| 1856 | if (error != JDWP::ERR_NONE) { |
| 1857 | return error; |
| 1858 | } |
| 1859 | if (obj == nullptr) { |
| 1860 | return JDWP::ERR_INVALID_OBJECT; |
| 1861 | } |
| 1862 | { |
| 1863 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1864 | mirror::Class* java_lang_String = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_String); |
| 1865 | if (!java_lang_String->IsAssignableFrom(obj->GetClass())) { |
| 1866 | // This isn't a string. |
| 1867 | return JDWP::ERR_INVALID_STRING; |
| 1868 | } |
| 1869 | } |
| 1870 | *str = obj->AsString()->ToModifiedUtf8(); |
| 1871 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1874 | void Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) { |
| 1875 | if (IsPrimitiveTag(tag)) { |
| 1876 | expandBufAdd1(pReply, tag); |
| 1877 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 1878 | expandBufAdd1(pReply, return_value->GetI()); |
| 1879 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 1880 | expandBufAdd2BE(pReply, return_value->GetI()); |
| 1881 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 1882 | expandBufAdd4BE(pReply, return_value->GetI()); |
| 1883 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1884 | expandBufAdd8BE(pReply, return_value->GetJ()); |
| 1885 | } else { |
| 1886 | CHECK_EQ(tag, JDWP::JT_VOID); |
| 1887 | } |
| 1888 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1889 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1890 | mirror::Object* value = return_value->GetL(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1891 | expandBufAdd1(pReply, TagFromObject(soa, value)); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1892 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 1893 | } |
| 1894 | } |
| 1895 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1896 | JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string* name) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1897 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1898 | JDWP::JdwpError error; |
| 1899 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 1900 | UNUSED(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1901 | if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1902 | return error; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1903 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1904 | |
| 1905 | // 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] | 1906 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 1907 | CHECK(thread_object != nullptr) << error; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1908 | ArtField* java_lang_Thread_name_field = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1909 | soa.DecodeField(WellKnownClasses::java_lang_Thread_name); |
| 1910 | mirror::String* s = |
| 1911 | reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1912 | if (s != nullptr) { |
| 1913 | *name = s->ToModifiedUtf8(); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1914 | } |
| 1915 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1918 | JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1919 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1920 | JDWP::JdwpError error; |
| 1921 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 1922 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1923 | return JDWP::ERR_INVALID_OBJECT; |
| 1924 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1925 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroup"); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1926 | // Okay, so it's an object, but is it actually a thread? |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 1927 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 1928 | UNUSED(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1929 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1930 | // Zombie threads are in the null group. |
| 1931 | expandBufAddObjectId(pReply, JDWP::ObjectId(0)); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1932 | error = JDWP::ERR_NONE; |
| 1933 | } else if (error == JDWP::ERR_NONE) { |
| 1934 | mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
| 1935 | CHECK(c != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1936 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 1937 | CHECK(f != nullptr); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1938 | mirror::Object* group = f->GetObject(thread_object); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 1939 | CHECK(group != nullptr); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1940 | JDWP::ObjectId thread_group_id = gRegistry->Add(group); |
| 1941 | expandBufAddObjectId(pReply, thread_group_id); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1942 | } |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1943 | return error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1946 | static mirror::Object* DecodeThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 1947 | JDWP::ObjectId thread_group_id, JDWP::JdwpError* error) |
| 1948 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1949 | mirror::Object* thread_group = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_group_id, |
| 1950 | error); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1951 | if (*error != JDWP::ERR_NONE) { |
| 1952 | return nullptr; |
| 1953 | } |
| 1954 | if (thread_group == nullptr) { |
| 1955 | *error = JDWP::ERR_INVALID_OBJECT; |
| 1956 | return nullptr; |
| 1957 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1958 | mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup); |
| 1959 | CHECK(c != nullptr); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1960 | if (!c->IsAssignableFrom(thread_group->GetClass())) { |
| 1961 | // This is not a java.lang.ThreadGroup. |
| 1962 | *error = JDWP::ERR_INVALID_THREAD_GROUP; |
| 1963 | return nullptr; |
| 1964 | } |
| 1965 | *error = JDWP::ERR_NONE; |
| 1966 | return thread_group; |
| 1967 | } |
| 1968 | |
| 1969 | JDWP::JdwpError Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
| 1970 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1971 | JDWP::JdwpError error; |
| 1972 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 1973 | if (error != JDWP::ERR_NONE) { |
| 1974 | return error; |
| 1975 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1976 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupName"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1977 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1978 | CHECK(f != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1979 | mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group)); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1980 | |
| 1981 | std::string thread_group_name(s->ToModifiedUtf8()); |
| 1982 | expandBufAddUtf8String(pReply, thread_group_name); |
| 1983 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1986 | JDWP::JdwpError Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1987 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1988 | JDWP::JdwpError error; |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1989 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 1990 | if (error != JDWP::ERR_NONE) { |
| 1991 | return error; |
| 1992 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1993 | mirror::Object* parent; |
| 1994 | { |
| 1995 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupParent"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1996 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_parent); |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1997 | CHECK(f != nullptr); |
| 1998 | parent = f->GetObject(thread_group); |
| 1999 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2000 | JDWP::ObjectId parent_group_id = gRegistry->Add(parent); |
| 2001 | expandBufAddObjectId(pReply, parent_group_id); |
| 2002 | return JDWP::ERR_NONE; |
| 2003 | } |
| 2004 | |
| 2005 | static void GetChildThreadGroups(ScopedObjectAccessUnchecked& soa, mirror::Object* thread_group, |
| 2006 | std::vector<JDWP::ObjectId>* child_thread_group_ids) |
| 2007 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2008 | CHECK(thread_group != nullptr); |
| 2009 | |
| 2010 | // Get the ArrayList<ThreadGroup> "groups" out of this thread group... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2011 | ArtField* groups_field = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_groups); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2012 | mirror::Object* groups_array_list = groups_field->GetObject(thread_group); |
Sebastien Hertz | e49e195 | 2014-10-13 11:27:13 +0200 | [diff] [blame] | 2013 | { |
| 2014 | // The "groups" field is declared as a java.util.List: check it really is |
| 2015 | // an instance of java.util.ArrayList. |
| 2016 | CHECK(groups_array_list != nullptr); |
| 2017 | mirror::Class* java_util_ArrayList_class = |
| 2018 | soa.Decode<mirror::Class*>(WellKnownClasses::java_util_ArrayList); |
| 2019 | CHECK(groups_array_list->InstanceOf(java_util_ArrayList_class)); |
| 2020 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2021 | |
| 2022 | // Get the array and size out of the ArrayList<ThreadGroup>... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2023 | ArtField* array_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_array); |
| 2024 | ArtField* size_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_size); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2025 | mirror::ObjectArray<mirror::Object>* groups_array = |
| 2026 | array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>(); |
| 2027 | const int32_t size = size_field->GetInt(groups_array_list); |
| 2028 | |
| 2029 | // Copy the first 'size' elements out of the array into the result. |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2030 | ObjectRegistry* registry = Dbg::GetObjectRegistry(); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2031 | for (int32_t i = 0; i < size; ++i) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2032 | child_thread_group_ids->push_back(registry->Add(groups_array->Get(i))); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | JDWP::JdwpError Dbg::GetThreadGroupChildren(JDWP::ObjectId thread_group_id, |
| 2037 | JDWP::ExpandBuf* pReply) { |
| 2038 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 2039 | JDWP::JdwpError error; |
| 2040 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2041 | if (error != JDWP::ERR_NONE) { |
| 2042 | return error; |
| 2043 | } |
| 2044 | |
| 2045 | // Add child threads. |
| 2046 | { |
| 2047 | std::vector<JDWP::ObjectId> child_thread_ids; |
| 2048 | GetThreads(thread_group, &child_thread_ids); |
| 2049 | expandBufAdd4BE(pReply, child_thread_ids.size()); |
| 2050 | for (JDWP::ObjectId child_thread_id : child_thread_ids) { |
| 2051 | expandBufAddObjectId(pReply, child_thread_id); |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | // Add child thread groups. |
| 2056 | { |
| 2057 | std::vector<JDWP::ObjectId> child_thread_groups_ids; |
| 2058 | GetChildThreadGroups(soa, thread_group, &child_thread_groups_ids); |
| 2059 | expandBufAdd4BE(pReply, child_thread_groups_ids.size()); |
| 2060 | for (JDWP::ObjectId child_thread_group_id : child_thread_groups_ids) { |
| 2061 | expandBufAddObjectId(pReply, child_thread_group_id); |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2069 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2070 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2071 | mirror::Object* group = f->GetObject(f->GetDeclaringClass()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2072 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2073 | } |
| 2074 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2075 | JDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) { |
| 2076 | switch (state) { |
| 2077 | case kBlocked: |
| 2078 | return JDWP::TS_MONITOR; |
| 2079 | case kNative: |
| 2080 | case kRunnable: |
| 2081 | case kSuspended: |
| 2082 | return JDWP::TS_RUNNING; |
| 2083 | case kSleeping: |
| 2084 | return JDWP::TS_SLEEPING; |
| 2085 | case kStarting: |
| 2086 | case kTerminated: |
| 2087 | return JDWP::TS_ZOMBIE; |
| 2088 | case kTimedWaiting: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2089 | case kWaitingForCheckPointsToRun: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2090 | case kWaitingForDebuggerSend: |
| 2091 | case kWaitingForDebuggerSuspension: |
| 2092 | case kWaitingForDebuggerToAttach: |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 2093 | case kWaitingForDeoptimization: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2094 | case kWaitingForGcToComplete: |
Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 2095 | case kWaitingForGetObjectsAllocated: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2096 | case kWaitingForJniOnLoad: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2097 | case kWaitingForMethodTracingStart: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2098 | case kWaitingForSignalCatcherOutput: |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 2099 | case kWaitingForVisitObjects: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2100 | case kWaitingInMainDebuggerLoop: |
| 2101 | case kWaitingInMainSignalCatcherLoop: |
| 2102 | case kWaitingPerformingGc: |
| 2103 | case kWaiting: |
| 2104 | return JDWP::TS_WAIT; |
| 2105 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 2106 | } |
| 2107 | LOG(FATAL) << "Unknown thread state: " << state; |
| 2108 | return JDWP::TS_ZOMBIE; |
| 2109 | } |
| 2110 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2111 | JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, |
| 2112 | JDWP::JdwpSuspendStatus* pSuspendStatus) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2113 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2114 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2115 | *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED; |
| 2116 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2117 | JDWP::JdwpError error; |
| 2118 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2119 | if (error != JDWP::ERR_NONE) { |
| 2120 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2121 | *pThreadStatus = JDWP::TS_ZOMBIE; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2122 | return JDWP::ERR_NONE; |
| 2123 | } |
| 2124 | return error; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2125 | } |
| 2126 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2127 | if (IsSuspendedForDebugger(soa, thread)) { |
| 2128 | *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2129 | } |
| 2130 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2131 | *pThreadStatus = ToJdwpThreadStatus(thread->GetState()); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2132 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2133 | } |
| 2134 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2135 | JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2136 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2137 | JDWP::JdwpError error; |
| 2138 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2139 | if (error != JDWP::ERR_NONE) { |
| 2140 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2141 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2142 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2143 | expandBufAdd4BE(pReply, thread->GetDebugSuspendCount()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2144 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2147 | JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) { |
| 2148 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2149 | JDWP::JdwpError error; |
| 2150 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2151 | if (error != JDWP::ERR_NONE) { |
| 2152 | return error; |
| 2153 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 2154 | thread->Interrupt(soa.Self()); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2155 | return JDWP::ERR_NONE; |
| 2156 | } |
| 2157 | |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2158 | static bool IsInDesiredThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 2159 | mirror::Object* desired_thread_group, mirror::Object* peer) |
| 2160 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2161 | // Do we want threads from all thread groups? |
| 2162 | if (desired_thread_group == nullptr) { |
| 2163 | return true; |
| 2164 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2165 | ArtField* thread_group_field = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2166 | DCHECK(thread_group_field != nullptr); |
| 2167 | mirror::Object* group = thread_group_field->GetObject(peer); |
| 2168 | return (group == desired_thread_group); |
| 2169 | } |
| 2170 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2171 | 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] | 2172 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2173 | std::list<Thread*> all_threads_list; |
| 2174 | { |
| 2175 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 2176 | all_threads_list = Runtime::Current()->GetThreadList()->GetList(); |
| 2177 | } |
| 2178 | for (Thread* t : all_threads_list) { |
| 2179 | if (t == Dbg::GetDebugThread()) { |
| 2180 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 2181 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 2182 | continue; |
| 2183 | } |
| 2184 | if (t->IsStillStarting()) { |
| 2185 | // This thread is being started (and has been registered in the thread list). However, it is |
| 2186 | // not completely started yet so we must ignore it. |
| 2187 | continue; |
| 2188 | } |
| 2189 | mirror::Object* peer = t->GetPeer(); |
| 2190 | if (peer == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2191 | // 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] | 2192 | // this thread yet. |
| 2193 | // TODO: if we identified threads to the debugger by their Thread* |
| 2194 | // rather than their peer's mirror::Object*, we could fix this. |
| 2195 | // Doing so might help us report ZOMBIE threads too. |
| 2196 | continue; |
| 2197 | } |
| 2198 | if (IsInDesiredThreadGroup(soa, thread_group, peer)) { |
| 2199 | thread_ids->push_back(gRegistry->Add(peer)); |
| 2200 | } |
| 2201 | } |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2202 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2203 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2204 | static int GetStackDepth(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2205 | struct CountStackDepthVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2206 | explicit CountStackDepthVisitor(Thread* thread_in) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2207 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2208 | depth(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2209 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2210 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2211 | // annotalysis. |
| 2212 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2213 | if (!GetMethod()->IsRuntimeMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 2214 | ++depth; |
| 2215 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2216 | return true; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2217 | } |
| 2218 | size_t depth; |
| 2219 | }; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2220 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2221 | CountStackDepthVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2222 | visitor.WalkStack(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2223 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2224 | } |
| 2225 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2226 | JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t* result) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2227 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2228 | JDWP::JdwpError error; |
| 2229 | *result = 0; |
| 2230 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2231 | if (error != JDWP::ERR_NONE) { |
| 2232 | return error; |
| 2233 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2234 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2235 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2236 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2237 | *result = GetStackDepth(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2238 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2239 | } |
| 2240 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 2241 | JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame, |
| 2242 | size_t frame_count, JDWP::ExpandBuf* buf) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2243 | class GetFrameVisitor : public StackVisitor { |
| 2244 | public: |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2245 | GetFrameVisitor(Thread* thread, size_t start_frame_in, size_t frame_count_in, |
| 2246 | JDWP::ExpandBuf* buf_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2247 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2248 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2249 | depth_(0), |
| 2250 | start_frame_(start_frame_in), |
| 2251 | frame_count_(frame_count_in), |
| 2252 | buf_(buf_in) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2253 | expandBufAdd4BE(buf_, frame_count_); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2254 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2255 | |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2256 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2257 | if (GetMethod()->IsRuntimeMethod()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2258 | 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] | 2259 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2260 | if (depth_ >= start_frame_ + frame_count_) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2261 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2262 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2263 | if (depth_ >= start_frame_) { |
| 2264 | JDWP::FrameId frame_id(GetFrameId()); |
| 2265 | JDWP::JdwpLocation location; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2266 | SetJdwpLocation(&location, GetMethod(), GetDexPc()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2267 | VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2268 | expandBufAdd8BE(buf_, frame_id); |
| 2269 | expandBufAddLocation(buf_, location); |
| 2270 | } |
| 2271 | ++depth_; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2272 | return true; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2273 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2274 | |
| 2275 | private: |
| 2276 | size_t depth_; |
| 2277 | const size_t start_frame_; |
| 2278 | const size_t frame_count_; |
| 2279 | JDWP::ExpandBuf* buf_; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2280 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2281 | |
| 2282 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2283 | JDWP::JdwpError error; |
| 2284 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2285 | if (error != JDWP::ERR_NONE) { |
| 2286 | return error; |
| 2287 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2288 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2289 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2290 | } |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2291 | GetFrameVisitor visitor(thread, start_frame, frame_count, buf); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2292 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2293 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2297 | return GetThreadId(Thread::Current()); |
| 2298 | } |
| 2299 | |
| 2300 | JDWP::ObjectId Dbg::GetThreadId(Thread* thread) { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 2301 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2302 | return gRegistry->Add(thread->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2303 | } |
| 2304 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2305 | void Dbg::SuspendVM() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2306 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | void Dbg::ResumeVM() { |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 2310 | Runtime::Current()->GetThreadList()->ResumeAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2311 | } |
| 2312 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2313 | JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2314 | Thread* self = Thread::Current(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2315 | ScopedLocalRef<jobject> peer(self->GetJniEnv(), nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2316 | { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2317 | ScopedObjectAccess soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2318 | JDWP::JdwpError error; |
| 2319 | peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id, &error))); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2320 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2321 | if (peer.get() == nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2322 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2323 | } |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 2324 | // Suspend thread to build stack trace. |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2325 | bool timed_out; |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 2326 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 2327 | Thread* thread = thread_list->SuspendThreadByPeer(peer.get(), request_suspension, true, |
| 2328 | &timed_out); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2329 | if (thread != nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2330 | return JDWP::ERR_NONE; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2331 | } else if (timed_out) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2332 | return JDWP::ERR_INTERNAL; |
| 2333 | } else { |
| 2334 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2335 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2336 | } |
| 2337 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2338 | void Dbg::ResumeThread(JDWP::ObjectId thread_id) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2339 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2340 | JDWP::JdwpError error; |
| 2341 | mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2342 | CHECK(peer != nullptr) << error; |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2343 | Thread* thread; |
| 2344 | { |
| 2345 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 2346 | thread = Thread::FromManagedThread(soa, peer); |
| 2347 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2348 | if (thread == nullptr) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2349 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 2350 | return; |
| 2351 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2352 | bool needs_resume; |
| 2353 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2354 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2355 | needs_resume = thread->GetSuspendCount() > 0; |
| 2356 | } |
| 2357 | if (needs_resume) { |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 2358 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
| 2359 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2363 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2364 | } |
| 2365 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2366 | struct GetThisVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2367 | GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2368 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2369 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2370 | this_object(nullptr), |
| 2371 | frame_id(frame_id_in) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2372 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2373 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2374 | // annotalysis. |
| 2375 | virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2376 | if (frame_id != GetFrameId()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2377 | return true; // continue |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2378 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2379 | this_object = GetThisObject(); |
| 2380 | return false; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2381 | } |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 2382 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2383 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2384 | mirror::Object* this_object; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2385 | JDWP::FrameId frame_id; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2386 | }; |
| 2387 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2388 | JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, |
| 2389 | JDWP::ObjectId* result) { |
| 2390 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2391 | JDWP::JdwpError error; |
| 2392 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2393 | if (error != JDWP::ERR_NONE) { |
| 2394 | return error; |
| 2395 | } |
| 2396 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2397 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2398 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2399 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2400 | GetThisVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2401 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2402 | *result = gRegistry->Add(visitor.this_object); |
| 2403 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2406 | // Walks the stack until we find the frame with the given FrameId. |
| 2407 | class FindFrameVisitor FINAL : public StackVisitor { |
| 2408 | public: |
| 2409 | FindFrameVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id) |
| 2410 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2411 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2412 | frame_id_(frame_id), |
| 2413 | error_(JDWP::ERR_INVALID_FRAMEID) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2414 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2415 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2416 | // annotalysis. |
| 2417 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 2418 | if (GetFrameId() != frame_id_) { |
| 2419 | return true; // Not our frame, carry on. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2420 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2421 | ArtMethod* m = GetMethod(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2422 | if (m->IsNative()) { |
| 2423 | // We can't read/write local value from/into native method. |
| 2424 | error_ = JDWP::ERR_OPAQUE_FRAME; |
| 2425 | } else { |
| 2426 | // We found our frame. |
| 2427 | error_ = JDWP::ERR_NONE; |
| 2428 | } |
| 2429 | return false; |
| 2430 | } |
| 2431 | |
| 2432 | JDWP::JdwpError GetError() const { |
| 2433 | return error_; |
| 2434 | } |
| 2435 | |
| 2436 | private: |
| 2437 | const JDWP::FrameId frame_id_; |
| 2438 | JDWP::JdwpError error_; |
| 2439 | }; |
| 2440 | |
| 2441 | JDWP::JdwpError Dbg::GetLocalValues(JDWP::Request* request, JDWP::ExpandBuf* pReply) { |
| 2442 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2443 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2444 | |
| 2445 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2446 | JDWP::JdwpError error; |
| 2447 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2448 | if (error != JDWP::ERR_NONE) { |
| 2449 | return error; |
| 2450 | } |
| 2451 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2452 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2453 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2454 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2455 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2456 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2457 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2458 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2459 | return visitor.GetError(); |
| 2460 | } |
| 2461 | |
| 2462 | // Read the values from visitor's context. |
| 2463 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2464 | expandBufAdd4BE(pReply, slot_count); /* "int values" */ |
| 2465 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2466 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2467 | JDWP::JdwpTag reqSigByte = request->ReadTag(); |
| 2468 | |
| 2469 | VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte; |
| 2470 | |
| 2471 | size_t width = Dbg::GetTagWidth(reqSigByte); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 2472 | uint8_t* ptr = expandBufAddSpace(pReply, width + 1); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2473 | error = Dbg::GetLocalValue(visitor, soa, slot, reqSigByte, ptr, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2474 | if (error != JDWP::ERR_NONE) { |
| 2475 | return error; |
| 2476 | } |
| 2477 | } |
| 2478 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2479 | } |
| 2480 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2481 | constexpr JDWP::JdwpError kStackFrameLocalAccessError = JDWP::ERR_ABSENT_INFORMATION; |
| 2482 | |
| 2483 | static std::string GetStackContextAsString(const StackVisitor& visitor) |
| 2484 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2485 | return StringPrintf(" at DEX pc 0x%08x in method %s", visitor.GetDexPc(false), |
| 2486 | PrettyMethod(visitor.GetMethod()).c_str()); |
| 2487 | } |
| 2488 | |
| 2489 | static JDWP::JdwpError FailGetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2490 | JDWP::JdwpTag tag) |
| 2491 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2492 | LOG(ERROR) << "Failed to read " << tag << " local from register v" << vreg |
| 2493 | << GetStackContextAsString(visitor); |
| 2494 | return kStackFrameLocalAccessError; |
| 2495 | } |
| 2496 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2497 | JDWP::JdwpError Dbg::GetLocalValue(const StackVisitor& visitor, ScopedObjectAccessUnchecked& soa, |
| 2498 | int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2499 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2500 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2501 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2502 | if (error != JDWP::ERR_NONE) { |
| 2503 | return error; |
| 2504 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2505 | // 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] | 2506 | switch (tag) { |
| 2507 | case JDWP::JT_BOOLEAN: { |
| 2508 | CHECK_EQ(width, 1U); |
| 2509 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2510 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2511 | return FailGetLocalValue(visitor, vreg, tag); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2512 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2513 | VLOG(jdwp) << "get boolean local " << vreg << " = " << intVal; |
| 2514 | JDWP::Set1(buf + 1, intVal != 0); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2515 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2516 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2517 | case JDWP::JT_BYTE: { |
| 2518 | CHECK_EQ(width, 1U); |
| 2519 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2520 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2521 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2522 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2523 | VLOG(jdwp) << "get byte local " << vreg << " = " << intVal; |
| 2524 | JDWP::Set1(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2525 | break; |
| 2526 | } |
| 2527 | case JDWP::JT_SHORT: |
| 2528 | case JDWP::JT_CHAR: { |
| 2529 | CHECK_EQ(width, 2U); |
| 2530 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2531 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2532 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2533 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2534 | VLOG(jdwp) << "get short/char local " << vreg << " = " << intVal; |
| 2535 | JDWP::Set2BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2536 | break; |
| 2537 | } |
| 2538 | case JDWP::JT_INT: { |
| 2539 | CHECK_EQ(width, 4U); |
| 2540 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2541 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2542 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2543 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2544 | VLOG(jdwp) << "get int local " << vreg << " = " << intVal; |
| 2545 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2546 | break; |
| 2547 | } |
| 2548 | case JDWP::JT_FLOAT: { |
| 2549 | CHECK_EQ(width, 4U); |
| 2550 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2551 | if (!visitor.GetVReg(m, vreg, kFloatVReg, &intVal)) { |
| 2552 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2553 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2554 | VLOG(jdwp) << "get float local " << vreg << " = " << intVal; |
| 2555 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2556 | break; |
| 2557 | } |
| 2558 | case JDWP::JT_ARRAY: |
| 2559 | case JDWP::JT_CLASS_LOADER: |
| 2560 | case JDWP::JT_CLASS_OBJECT: |
| 2561 | case JDWP::JT_OBJECT: |
| 2562 | case JDWP::JT_STRING: |
| 2563 | case JDWP::JT_THREAD: |
| 2564 | case JDWP::JT_THREAD_GROUP: { |
| 2565 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
| 2566 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2567 | if (!visitor.GetVReg(m, vreg, kReferenceVReg, &intVal)) { |
| 2568 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2569 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2570 | mirror::Object* o = reinterpret_cast<mirror::Object*>(intVal); |
| 2571 | VLOG(jdwp) << "get " << tag << " object local " << vreg << " = " << o; |
| 2572 | if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) { |
| 2573 | LOG(FATAL) << StringPrintf("Found invalid object %#" PRIxPTR " in register v%u", |
| 2574 | reinterpret_cast<uintptr_t>(o), vreg) |
| 2575 | << GetStackContextAsString(visitor); |
| 2576 | UNREACHABLE(); |
| 2577 | } |
| 2578 | tag = TagFromObject(soa, o); |
| 2579 | JDWP::SetObjectId(buf + 1, gRegistry->Add(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2580 | break; |
| 2581 | } |
| 2582 | case JDWP::JT_DOUBLE: { |
| 2583 | CHECK_EQ(width, 8U); |
| 2584 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2585 | if (!visitor.GetVRegPair(m, vreg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) { |
| 2586 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2587 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2588 | VLOG(jdwp) << "get double local " << vreg << " = " << longVal; |
| 2589 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2590 | break; |
| 2591 | } |
| 2592 | case JDWP::JT_LONG: { |
| 2593 | CHECK_EQ(width, 8U); |
| 2594 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2595 | if (!visitor.GetVRegPair(m, vreg, kLongLoVReg, kLongHiVReg, &longVal)) { |
| 2596 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2597 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2598 | VLOG(jdwp) << "get long local " << vreg << " = " << longVal; |
| 2599 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2600 | break; |
| 2601 | } |
| 2602 | default: |
| 2603 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2604 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2605 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2606 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2607 | // Prepend tag, which may have been updated. |
| 2608 | JDWP::Set1(buf, tag); |
| 2609 | return JDWP::ERR_NONE; |
| 2610 | } |
| 2611 | |
| 2612 | JDWP::JdwpError Dbg::SetLocalValues(JDWP::Request* request) { |
| 2613 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2614 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2615 | |
| 2616 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2617 | JDWP::JdwpError error; |
| 2618 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2619 | if (error != JDWP::ERR_NONE) { |
| 2620 | return error; |
| 2621 | } |
| 2622 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2623 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2624 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2625 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2626 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2627 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2628 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2629 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2630 | return visitor.GetError(); |
| 2631 | } |
| 2632 | |
| 2633 | // Writes the values into visitor's context. |
| 2634 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2635 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2636 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2637 | JDWP::JdwpTag sigByte = request->ReadTag(); |
| 2638 | size_t width = Dbg::GetTagWidth(sigByte); |
| 2639 | uint64_t value = request->ReadValue(width); |
| 2640 | |
| 2641 | VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value; |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2642 | error = Dbg::SetLocalValue(visitor, slot, sigByte, value, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2643 | if (error != JDWP::ERR_NONE) { |
| 2644 | return error; |
| 2645 | } |
| 2646 | } |
| 2647 | return JDWP::ERR_NONE; |
| 2648 | } |
| 2649 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2650 | template<typename T> |
| 2651 | static JDWP::JdwpError FailSetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2652 | JDWP::JdwpTag tag, T value) |
| 2653 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2654 | LOG(ERROR) << "Failed to write " << tag << " local " << value |
| 2655 | << " (0x" << std::hex << value << ") into register v" << vreg |
| 2656 | << GetStackContextAsString(visitor); |
| 2657 | return kStackFrameLocalAccessError; |
| 2658 | } |
| 2659 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2660 | JDWP::JdwpError Dbg::SetLocalValue(StackVisitor& visitor, int slot, JDWP::JdwpTag tag, |
| 2661 | uint64_t value, size_t width) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2662 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2663 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2664 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2665 | if (error != JDWP::ERR_NONE) { |
| 2666 | return error; |
| 2667 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2668 | // 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] | 2669 | switch (tag) { |
| 2670 | case JDWP::JT_BOOLEAN: |
| 2671 | case JDWP::JT_BYTE: |
| 2672 | CHECK_EQ(width, 1U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2673 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2674 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2675 | } |
| 2676 | break; |
| 2677 | case JDWP::JT_SHORT: |
| 2678 | case JDWP::JT_CHAR: |
| 2679 | CHECK_EQ(width, 2U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2680 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2681 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2682 | } |
| 2683 | break; |
| 2684 | case JDWP::JT_INT: |
| 2685 | CHECK_EQ(width, 4U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2686 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2687 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2688 | } |
| 2689 | break; |
| 2690 | case JDWP::JT_FLOAT: |
| 2691 | CHECK_EQ(width, 4U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2692 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kFloatVReg)) { |
| 2693 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2694 | } |
| 2695 | break; |
| 2696 | case JDWP::JT_ARRAY: |
| 2697 | case JDWP::JT_CLASS_LOADER: |
| 2698 | case JDWP::JT_CLASS_OBJECT: |
| 2699 | case JDWP::JT_OBJECT: |
| 2700 | case JDWP::JT_STRING: |
| 2701 | case JDWP::JT_THREAD: |
| 2702 | case JDWP::JT_THREAD_GROUP: { |
| 2703 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2704 | mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value), |
| 2705 | &error); |
| 2706 | if (error != JDWP::ERR_NONE) { |
| 2707 | VLOG(jdwp) << tag << " object " << o << " is an invalid object"; |
| 2708 | return JDWP::ERR_INVALID_OBJECT; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2709 | } |
| 2710 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), |
| 2711 | kReferenceVReg)) { |
| 2712 | return FailSetLocalValue(visitor, vreg, tag, reinterpret_cast<uintptr_t>(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2713 | } |
| 2714 | break; |
| 2715 | } |
| 2716 | case JDWP::JT_DOUBLE: { |
| 2717 | CHECK_EQ(width, 8U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2718 | if (!visitor.SetVRegPair(m, vreg, value, kDoubleLoVReg, kDoubleHiVReg)) { |
| 2719 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2720 | } |
| 2721 | break; |
| 2722 | } |
| 2723 | case JDWP::JT_LONG: { |
| 2724 | CHECK_EQ(width, 8U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2725 | if (!visitor.SetVRegPair(m, vreg, value, kLongLoVReg, kLongHiVReg)) { |
| 2726 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2727 | } |
| 2728 | break; |
| 2729 | } |
| 2730 | default: |
| 2731 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2732 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2733 | } |
| 2734 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2735 | } |
| 2736 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2737 | static void SetEventLocation(JDWP::EventLocation* location, ArtMethod* m, uint32_t dex_pc) |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2738 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2739 | DCHECK(location != nullptr); |
| 2740 | if (m == nullptr) { |
| 2741 | memset(location, 0, sizeof(*location)); |
| 2742 | } else { |
| 2743 | location->method = m; |
| 2744 | 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] | 2745 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2746 | } |
| 2747 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2748 | void Dbg::PostLocationEvent(ArtMethod* m, int dex_pc, mirror::Object* this_object, |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2749 | int event_flags, const JValue* return_value) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2750 | if (!IsDebuggerActive()) { |
| 2751 | return; |
| 2752 | } |
| 2753 | DCHECK(m != nullptr); |
| 2754 | DCHECK_EQ(m->IsStatic(), this_object == nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2755 | JDWP::EventLocation location; |
| 2756 | SetEventLocation(&location, m, dex_pc); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2757 | |
Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2758 | // We need to be sure no exception is pending when calling JdwpState::PostLocationEvent. |
| 2759 | // This is required to be able to call JNI functions to create JDWP ids. To achieve this, |
| 2760 | // we temporarily clear the current thread's exception (if any) and will restore it after |
| 2761 | // the call. |
| 2762 | // Note: the only way to get a pending exception here is to suspend on a move-exception |
| 2763 | // instruction. |
| 2764 | Thread* const self = Thread::Current(); |
| 2765 | StackHandleScope<1> hs(self); |
| 2766 | Handle<mirror::Throwable> pending_exception(hs.NewHandle(self->GetException())); |
| 2767 | self->ClearException(); |
| 2768 | if (kIsDebugBuild && pending_exception.Get() != nullptr) { |
| 2769 | const DexFile::CodeItem* code_item = location.method->GetCodeItem(); |
| 2770 | const Instruction* instr = Instruction::At(&code_item->insns_[location.dex_pc]); |
| 2771 | CHECK_EQ(Instruction::MOVE_EXCEPTION, instr->Opcode()); |
| 2772 | } |
| 2773 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2774 | gJdwpState->PostLocationEvent(&location, this_object, event_flags, return_value); |
Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2775 | |
| 2776 | if (pending_exception.Get() != nullptr) { |
| 2777 | self->SetException(pending_exception.Get()); |
| 2778 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2779 | } |
| 2780 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2781 | void Dbg::PostFieldAccessEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2782 | mirror::Object* this_object, ArtField* f) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2783 | if (!IsDebuggerActive()) { |
| 2784 | return; |
| 2785 | } |
| 2786 | DCHECK(m != nullptr); |
| 2787 | DCHECK(f != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2788 | JDWP::EventLocation location; |
| 2789 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2790 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2791 | gJdwpState->PostFieldEvent(&location, f, this_object, nullptr, false); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2792 | } |
| 2793 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2794 | void Dbg::PostFieldModificationEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2795 | mirror::Object* this_object, ArtField* f, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2796 | const JValue* field_value) { |
| 2797 | if (!IsDebuggerActive()) { |
| 2798 | return; |
| 2799 | } |
| 2800 | DCHECK(m != nullptr); |
| 2801 | DCHECK(f != nullptr); |
| 2802 | DCHECK(field_value != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2803 | JDWP::EventLocation location; |
| 2804 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2805 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2806 | gJdwpState->PostFieldEvent(&location, f, this_object, field_value, true); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2807 | } |
| 2808 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2809 | /** |
| 2810 | * Finds the location where this exception will be caught. We search until we reach the top |
| 2811 | * frame, in which case this exception is considered uncaught. |
| 2812 | */ |
| 2813 | class CatchLocationFinder : public StackVisitor { |
| 2814 | public: |
| 2815 | CatchLocationFinder(Thread* self, const Handle<mirror::Throwable>& exception, Context* context) |
| 2816 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2817 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2818 | self_(self), |
| 2819 | exception_(exception), |
| 2820 | handle_scope_(self), |
| 2821 | this_at_throw_(handle_scope_.NewHandle<mirror::Object>(nullptr)), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2822 | catch_method_(nullptr), |
| 2823 | throw_method_(nullptr), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2824 | catch_dex_pc_(DexFile::kDexNoIndex), |
| 2825 | throw_dex_pc_(DexFile::kDexNoIndex) { |
| 2826 | } |
| 2827 | |
| 2828 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2829 | ArtMethod* method = GetMethod(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2830 | DCHECK(method != nullptr); |
| 2831 | if (method->IsRuntimeMethod()) { |
| 2832 | // Ignore callee save method. |
| 2833 | DCHECK(method->IsCalleeSaveMethod()); |
| 2834 | return true; |
| 2835 | } |
| 2836 | |
| 2837 | uint32_t dex_pc = GetDexPc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2838 | if (throw_method_ == nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2839 | // First Java method found. It is either the method that threw the exception, |
| 2840 | // or the Java native method that is reporting an exception thrown by |
| 2841 | // native code. |
| 2842 | this_at_throw_.Assign(GetThisObject()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2843 | throw_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2844 | throw_dex_pc_ = dex_pc; |
| 2845 | } |
| 2846 | |
| 2847 | if (dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2848 | StackHandleScope<1> hs(self_); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2849 | uint32_t found_dex_pc; |
| 2850 | Handle<mirror::Class> exception_class(hs.NewHandle(exception_->GetClass())); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2851 | bool unused_clear_exception; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2852 | found_dex_pc = method->FindCatchBlock(exception_class, dex_pc, &unused_clear_exception); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2853 | if (found_dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2854 | catch_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2855 | catch_dex_pc_ = found_dex_pc; |
| 2856 | return false; // End stack walk. |
| 2857 | } |
| 2858 | } |
| 2859 | return true; // Continue stack walk. |
| 2860 | } |
| 2861 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2862 | ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2863 | return catch_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2866 | ArtMethod* GetThrowMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2867 | return throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | mirror::Object* GetThisAtThrow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2871 | return this_at_throw_.Get(); |
| 2872 | } |
| 2873 | |
| 2874 | uint32_t GetCatchDexPc() const { |
| 2875 | return catch_dex_pc_; |
| 2876 | } |
| 2877 | |
| 2878 | uint32_t GetThrowDexPc() const { |
| 2879 | return throw_dex_pc_; |
| 2880 | } |
| 2881 | |
| 2882 | private: |
| 2883 | Thread* const self_; |
| 2884 | const Handle<mirror::Throwable>& exception_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2885 | StackHandleScope<1> handle_scope_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2886 | MutableHandle<mirror::Object> this_at_throw_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2887 | ArtMethod* catch_method_; |
| 2888 | ArtMethod* throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2889 | uint32_t catch_dex_pc_; |
| 2890 | uint32_t throw_dex_pc_; |
| 2891 | |
| 2892 | DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder); |
| 2893 | }; |
| 2894 | |
| 2895 | void Dbg::PostException(mirror::Throwable* exception_object) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2896 | if (!IsDebuggerActive()) { |
Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 2897 | return; |
| 2898 | } |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 2899 | Thread* const self = Thread::Current(); |
| 2900 | StackHandleScope<1> handle_scope(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2901 | Handle<mirror::Throwable> h_exception(handle_scope.NewHandle(exception_object)); |
| 2902 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 2903 | CatchLocationFinder clf(self, h_exception, context.get()); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2904 | clf.WalkStack(/* include_transitions */ false); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2905 | JDWP::EventLocation exception_throw_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2906 | SetEventLocation(&exception_throw_location, clf.GetThrowMethod(), clf.GetThrowDexPc()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2907 | JDWP::EventLocation exception_catch_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2908 | SetEventLocation(&exception_catch_location, clf.GetCatchMethod(), clf.GetCatchDexPc()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2909 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2910 | gJdwpState->PostException(&exception_throw_location, h_exception.Get(), &exception_catch_location, |
| 2911 | clf.GetThisAtThrow()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2912 | } |
| 2913 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2914 | void Dbg::PostClassPrepare(mirror::Class* c) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2915 | if (!IsDebuggerActive()) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2916 | return; |
| 2917 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2918 | gJdwpState->PostClassPrepare(c); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2919 | } |
| 2920 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2921 | void Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2922 | ArtMethod* m, uint32_t dex_pc, |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 2923 | int event_flags, const JValue* return_value) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2924 | if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 2925 | return; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2926 | } |
| 2927 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2928 | if (IsBreakpoint(m, dex_pc)) { |
| 2929 | event_flags |= kBreakpoint; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2930 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2931 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2932 | // If the debugger is single-stepping one of our threads, check to |
| 2933 | // see if we're that thread and we've reached a step point. |
| 2934 | const SingleStepControl* single_step_control = thread->GetSingleStepControl(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2935 | if (single_step_control != nullptr) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2936 | CHECK(!m->IsNative()); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2937 | if (single_step_control->GetStepDepth() == JDWP::SD_INTO) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2938 | // Step into method calls. We break when the line number |
| 2939 | // or method pointer changes. If we're in SS_MIN mode, we |
| 2940 | // always stop. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2941 | if (single_step_control->GetMethod() != m) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2942 | event_flags |= kSingleStep; |
| 2943 | VLOG(jdwp) << "SS new method"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2944 | } else if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2945 | event_flags |= kSingleStep; |
| 2946 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 2947 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2948 | event_flags |= kSingleStep; |
| 2949 | VLOG(jdwp) << "SS new line"; |
| 2950 | } |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2951 | } else if (single_step_control->GetStepDepth() == JDWP::SD_OVER) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2952 | // Step over method calls. We break when the line number is |
| 2953 | // different and the frame depth is <= the original frame |
| 2954 | // depth. (We can't just compare on the method, because we |
| 2955 | // might get unrolled past it by an exception, and it's tricky |
| 2956 | // to identify recursion.) |
| 2957 | |
| 2958 | int stack_depth = GetStackDepth(thread); |
| 2959 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2960 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2961 | // Popped up one or more frames, always trigger. |
| 2962 | event_flags |= kSingleStep; |
| 2963 | VLOG(jdwp) << "SS method pop"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2964 | } else if (stack_depth == single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2965 | // Same depth, see if we moved. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2966 | if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2967 | event_flags |= kSingleStep; |
| 2968 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 2969 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2970 | event_flags |= kSingleStep; |
| 2971 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2972 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2973 | } |
| 2974 | } else { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2975 | CHECK_EQ(single_step_control->GetStepDepth(), JDWP::SD_OUT); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2976 | // Return from the current method. We break when the frame |
| 2977 | // depth pops up. |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2978 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2979 | // This differs from the "method exit" break in that it stops |
| 2980 | // with the PC at the next instruction in the returned-to |
| 2981 | // function, rather than the end of the returning function. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2982 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2983 | int stack_depth = GetStackDepth(thread); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2984 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2985 | event_flags |= kSingleStep; |
| 2986 | VLOG(jdwp) << "SS method pop"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2987 | } |
| 2988 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2989 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2990 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2991 | // If there's something interesting going on, see if it matches one |
| 2992 | // of the debugger filters. |
| 2993 | if (event_flags != 0) { |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 2994 | Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2995 | } |
| 2996 | } |
| 2997 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 2998 | size_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) { |
| 2999 | switch (instrumentation_event) { |
| 3000 | case instrumentation::Instrumentation::kMethodEntered: |
| 3001 | return &method_enter_event_ref_count_; |
| 3002 | case instrumentation::Instrumentation::kMethodExited: |
| 3003 | return &method_exit_event_ref_count_; |
| 3004 | case instrumentation::Instrumentation::kDexPcMoved: |
| 3005 | return &dex_pc_change_event_ref_count_; |
| 3006 | case instrumentation::Instrumentation::kFieldRead: |
| 3007 | return &field_read_event_ref_count_; |
| 3008 | case instrumentation::Instrumentation::kFieldWritten: |
| 3009 | return &field_write_event_ref_count_; |
| 3010 | case instrumentation::Instrumentation::kExceptionCaught: |
| 3011 | return &exception_catch_event_ref_count_; |
| 3012 | default: |
| 3013 | return nullptr; |
| 3014 | } |
| 3015 | } |
| 3016 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3017 | // Process request while all mutator threads are suspended. |
| 3018 | void Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3019 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3020 | switch (request.GetKind()) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3021 | case DeoptimizationRequest::kNothing: |
| 3022 | LOG(WARNING) << "Ignoring empty deoptimization request."; |
| 3023 | break; |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3024 | case DeoptimizationRequest::kRegisterForEvent: |
| 3025 | VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3026 | request.InstrumentationEvent()); |
| 3027 | instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent()); |
| 3028 | instrumentation_events_ |= request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3029 | break; |
| 3030 | case DeoptimizationRequest::kUnregisterForEvent: |
| 3031 | VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3032 | request.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3033 | instrumentation->RemoveListener(&gDebugInstrumentationListener, |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3034 | request.InstrumentationEvent()); |
| 3035 | instrumentation_events_ &= ~request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3036 | break; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3037 | case DeoptimizationRequest::kFullDeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3038 | VLOG(jdwp) << "Deoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3039 | instrumentation->DeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3040 | VLOG(jdwp) << "Deoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3041 | break; |
| 3042 | case DeoptimizationRequest::kFullUndeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3043 | VLOG(jdwp) << "Undeoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3044 | instrumentation->UndeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3045 | VLOG(jdwp) << "Undeoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3046 | break; |
| 3047 | case DeoptimizationRequest::kSelectiveDeoptimization: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3048 | VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " ..."; |
| 3049 | instrumentation->Deoptimize(request.Method()); |
| 3050 | VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3051 | break; |
| 3052 | case DeoptimizationRequest::kSelectiveUndeoptimization: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3053 | VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " ..."; |
| 3054 | instrumentation->Undeoptimize(request.Method()); |
| 3055 | VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3056 | break; |
| 3057 | default: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3058 | LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3059 | break; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3060 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3061 | } |
| 3062 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3063 | void Dbg::RequestDeoptimization(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3064 | if (req.GetKind() == DeoptimizationRequest::kNothing) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3065 | // Nothing to do. |
| 3066 | return; |
| 3067 | } |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3068 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3069 | RequestDeoptimizationLocked(req); |
| 3070 | } |
| 3071 | |
| 3072 | void Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3073 | switch (req.GetKind()) { |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3074 | case DeoptimizationRequest::kRegisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3075 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3076 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3077 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3078 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3079 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3080 | 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] | 3081 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3082 | deoptimization_requests_.push_back(req); |
| 3083 | } |
| 3084 | *counter = *counter + 1; |
| 3085 | break; |
| 3086 | } |
| 3087 | case DeoptimizationRequest::kUnregisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3088 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3089 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3090 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3091 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3092 | *counter = *counter - 1; |
| 3093 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3094 | 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] | 3095 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3096 | deoptimization_requests_.push_back(req); |
| 3097 | } |
| 3098 | break; |
| 3099 | } |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3100 | case DeoptimizationRequest::kFullDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3101 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3102 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3103 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3104 | << " for full deoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3105 | deoptimization_requests_.push_back(req); |
| 3106 | } |
| 3107 | ++full_deoptimization_event_count_; |
| 3108 | break; |
| 3109 | } |
| 3110 | case DeoptimizationRequest::kFullUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3111 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | e713d93 | 2014-05-15 10:48:53 +0200 | [diff] [blame] | 3112 | DCHECK_GT(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3113 | --full_deoptimization_event_count_; |
| 3114 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3115 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3116 | << " for full undeoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3117 | deoptimization_requests_.push_back(req); |
| 3118 | } |
| 3119 | break; |
| 3120 | } |
| 3121 | case DeoptimizationRequest::kSelectiveDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3122 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3123 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3124 | << " for deoptimization of " << PrettyMethod(req.Method()); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3125 | deoptimization_requests_.push_back(req); |
| 3126 | break; |
| 3127 | } |
| 3128 | case DeoptimizationRequest::kSelectiveUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3129 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3130 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3131 | << " for undeoptimization of " << PrettyMethod(req.Method()); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3132 | deoptimization_requests_.push_back(req); |
| 3133 | break; |
| 3134 | } |
| 3135 | default: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3136 | LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3137 | break; |
| 3138 | } |
| 3139 | } |
| 3140 | } |
| 3141 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3142 | void Dbg::ManageDeoptimization() { |
| 3143 | Thread* const self = Thread::Current(); |
| 3144 | { |
| 3145 | // Avoid suspend/resume if there is no pending request. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3146 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3147 | if (deoptimization_requests_.empty()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3148 | return; |
| 3149 | } |
| 3150 | } |
| 3151 | CHECK_EQ(self->GetState(), kRunnable); |
| 3152 | self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization); |
| 3153 | // We need to suspend mutator threads first. |
| 3154 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 3155 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3156 | const ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3157 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3158 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3159 | size_t req_index = 0; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3160 | for (DeoptimizationRequest& request : deoptimization_requests_) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3161 | VLOG(jdwp) << "Process deoptimization request #" << req_index++; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3162 | ProcessDeoptimizationRequest(request); |
| 3163 | } |
| 3164 | deoptimization_requests_.clear(); |
| 3165 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3166 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 3167 | runtime->GetThreadList()->ResumeAll(); |
| 3168 | self->TransitionFromSuspendedToRunnable(); |
| 3169 | } |
| 3170 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3171 | static bool IsMethodPossiblyInlined(Thread* self, ArtMethod* m) |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3172 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3173 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3174 | if (code_item == nullptr) { |
| 3175 | // TODO We should not be asked to watch location in a native or abstract method so the code item |
| 3176 | // should never be null. We could just check we never encounter this case. |
| 3177 | return false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3178 | } |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3179 | // Note: method verifier may cause thread suspension. |
| 3180 | self->AssertThreadSuspensionIsAllowable(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3181 | StackHandleScope<2> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3182 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 3183 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 3184 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3185 | verifier::MethodVerifier verifier(self, dex_cache->GetDexFile(), dex_cache, class_loader, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3186 | &m->GetClassDef(), code_item, m->GetDexMethodIndex(), m, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 3187 | m->GetAccessFlags(), false, true, false, true); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3188 | // Note: we don't need to verify the method. |
| 3189 | return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr); |
| 3190 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3191 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3192 | static const Breakpoint* FindFirstBreakpointForMethod(ArtMethod* m) |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3193 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3194 | for (Breakpoint& breakpoint : gBreakpoints) { |
| 3195 | if (breakpoint.Method() == m) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3196 | return &breakpoint; |
| 3197 | } |
| 3198 | } |
| 3199 | return nullptr; |
| 3200 | } |
| 3201 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3202 | bool Dbg::MethodHasAnyBreakpoints(ArtMethod* method) { |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 3203 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| 3204 | return FindFirstBreakpointForMethod(method) != nullptr; |
| 3205 | } |
| 3206 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3207 | // Sanity checks all existing breakpoints on the same method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3208 | static void SanityCheckExistingBreakpoints(ArtMethod* m, |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3209 | DeoptimizationRequest::Kind deoptimization_kind) |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3210 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3211 | for (const Breakpoint& breakpoint : gBreakpoints) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3212 | if (breakpoint.Method() == m) { |
| 3213 | CHECK_EQ(deoptimization_kind, breakpoint.GetDeoptimizationKind()); |
| 3214 | } |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3215 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3216 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 3217 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3218 | // We should have deoptimized everything but not "selectively" deoptimized this method. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3219 | CHECK(instrumentation->AreAllMethodsDeoptimized()); |
| 3220 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3221 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3222 | // We should have "selectively" deoptimized this method. |
| 3223 | // Note: while we have not deoptimized everything for this method, we may have done it for |
| 3224 | // another event. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3225 | CHECK(instrumentation->IsDeoptimized(m)); |
| 3226 | } else { |
| 3227 | // This method does not require deoptimization. |
| 3228 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3229 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3230 | } |
| 3231 | } |
| 3232 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3233 | // Returns the deoptimization kind required to set a breakpoint in a method. |
| 3234 | // If a breakpoint has already been set, we also return the first breakpoint |
| 3235 | // through the given 'existing_brkpt' pointer. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3236 | static DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3237 | ArtMethod* m, |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3238 | const Breakpoint** existing_brkpt) |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3239 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3240 | if (!Dbg::RequiresDeoptimization()) { |
| 3241 | // We already run in interpreter-only mode so we don't need to deoptimize anything. |
| 3242 | VLOG(jdwp) << "No need for deoptimization when fully running with interpreter for method " |
| 3243 | << PrettyMethod(m); |
| 3244 | return DeoptimizationRequest::kNothing; |
| 3245 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3246 | const Breakpoint* first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3247 | { |
| 3248 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3249 | first_breakpoint = FindFirstBreakpointForMethod(m); |
| 3250 | *existing_brkpt = first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3251 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3252 | |
| 3253 | if (first_breakpoint == nullptr) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3254 | // There is no breakpoint on this method yet: we need to deoptimize. If this method may be |
| 3255 | // inlined, we deoptimize everything; otherwise we deoptimize only this method. |
| 3256 | // Note: IsMethodPossiblyInlined goes into the method verifier and may cause thread suspension. |
| 3257 | // Therefore we must not hold any lock when we call it. |
| 3258 | bool need_full_deoptimization = IsMethodPossiblyInlined(self, m); |
| 3259 | if (need_full_deoptimization) { |
| 3260 | VLOG(jdwp) << "Need full deoptimization because of possible inlining of method " |
| 3261 | << PrettyMethod(m); |
| 3262 | return DeoptimizationRequest::kFullDeoptimization; |
| 3263 | } else { |
| 3264 | // We don't need to deoptimize if the method has not been compiled. |
| 3265 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 3266 | const bool is_compiled = class_linker->GetOatMethodQuickCodeFor(m) != nullptr; |
| 3267 | if (is_compiled) { |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 3268 | // If the method may be called through its direct code pointer (without loading |
| 3269 | // its updated entrypoint), we need full deoptimization to not miss the breakpoint. |
| 3270 | if (class_linker->MayBeCalledWithDirectCodePointer(m)) { |
| 3271 | VLOG(jdwp) << "Need full deoptimization because of possible direct code call " |
| 3272 | << "into image for compiled method " << PrettyMethod(m); |
| 3273 | return DeoptimizationRequest::kFullDeoptimization; |
| 3274 | } else { |
| 3275 | VLOG(jdwp) << "Need selective deoptimization for compiled method " << PrettyMethod(m); |
| 3276 | return DeoptimizationRequest::kSelectiveDeoptimization; |
| 3277 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3278 | } else { |
| 3279 | // Method is not compiled: we don't need to deoptimize. |
| 3280 | VLOG(jdwp) << "No need for deoptimization for non-compiled method " << PrettyMethod(m); |
| 3281 | return DeoptimizationRequest::kNothing; |
| 3282 | } |
| 3283 | } |
| 3284 | } else { |
| 3285 | // There is at least one breakpoint for this method: we don't need to deoptimize. |
| 3286 | // Let's check that all breakpoints are configured the same way for deoptimization. |
| 3287 | VLOG(jdwp) << "Breakpoint already set: no deoptimization is required"; |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3288 | DeoptimizationRequest::Kind deoptimization_kind = first_breakpoint->GetDeoptimizationKind(); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3289 | if (kIsDebugBuild) { |
| 3290 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
| 3291 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
| 3292 | } |
| 3293 | return DeoptimizationRequest::kNothing; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3294 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3295 | } |
| 3296 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3297 | // Installs a breakpoint at the specified location. Also indicates through the deoptimization |
| 3298 | // request if we need to deoptimize. |
| 3299 | void Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
| 3300 | Thread* const self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3301 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3302 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3303 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3304 | const Breakpoint* existing_breakpoint = nullptr; |
| 3305 | const DeoptimizationRequest::Kind deoptimization_kind = |
| 3306 | GetRequiredDeoptimizationKind(self, m, &existing_breakpoint); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3307 | req->SetKind(deoptimization_kind); |
| 3308 | if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
| 3309 | req->SetMethod(m); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3310 | } else { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3311 | CHECK(deoptimization_kind == DeoptimizationRequest::kNothing || |
| 3312 | deoptimization_kind == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3313 | req->SetMethod(nullptr); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3314 | } |
| 3315 | |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3316 | { |
| 3317 | WriterMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3318 | // If there is at least one existing breakpoint on the same method, the new breakpoint |
| 3319 | // must have the same deoptimization kind than the existing breakpoint(s). |
| 3320 | DeoptimizationRequest::Kind breakpoint_deoptimization_kind; |
| 3321 | if (existing_breakpoint != nullptr) { |
| 3322 | breakpoint_deoptimization_kind = existing_breakpoint->GetDeoptimizationKind(); |
| 3323 | } else { |
| 3324 | breakpoint_deoptimization_kind = deoptimization_kind; |
| 3325 | } |
| 3326 | gBreakpoints.push_back(Breakpoint(m, location->dex_pc, breakpoint_deoptimization_kind)); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3327 | VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " |
| 3328 | << gBreakpoints[gBreakpoints.size() - 1]; |
| 3329 | } |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | // Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization |
| 3333 | // request if we need to undeoptimize. |
| 3334 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3335 | WriterMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3336 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3337 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3338 | DeoptimizationRequest::Kind deoptimization_kind = DeoptimizationRequest::kNothing; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3339 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3340 | if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].Method() == m) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3341 | VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i]; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3342 | deoptimization_kind = gBreakpoints[i].GetDeoptimizationKind(); |
| 3343 | DCHECK_EQ(deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization, |
| 3344 | Runtime::Current()->GetInstrumentation()->IsDeoptimized(m)); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3345 | gBreakpoints.erase(gBreakpoints.begin() + i); |
| 3346 | break; |
| 3347 | } |
| 3348 | } |
| 3349 | const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m); |
| 3350 | if (existing_breakpoint == nullptr) { |
| 3351 | // There is no more breakpoint on this method: we need to undeoptimize. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3352 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3353 | // This method required full deoptimization: we need to undeoptimize everything. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3354 | req->SetKind(DeoptimizationRequest::kFullUndeoptimization); |
| 3355 | req->SetMethod(nullptr); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3356 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3357 | // This method required selective deoptimization: we need to undeoptimize only that method. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3358 | req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization); |
| 3359 | req->SetMethod(m); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3360 | } else { |
| 3361 | // This method had no need for deoptimization: do nothing. |
| 3362 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3363 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3364 | req->SetMethod(nullptr); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3365 | } |
| 3366 | } else { |
| 3367 | // 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] | 3368 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3369 | req->SetMethod(nullptr); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3370 | if (kIsDebugBuild) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3371 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3372 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3373 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3374 | } |
| 3375 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3376 | bool Dbg::IsForcedInterpreterNeededForCallingImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3377 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3378 | if (ssc == nullptr) { |
| 3379 | // If we are not single-stepping, then we don't have to force interpreter. |
| 3380 | return false; |
| 3381 | } |
| 3382 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
| 3383 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3384 | return false; |
| 3385 | } |
| 3386 | |
| 3387 | if (!m->IsNative() && !m->IsProxyMethod()) { |
| 3388 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3389 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3390 | return true; |
| 3391 | } |
| 3392 | } |
| 3393 | return false; |
| 3394 | } |
| 3395 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3396 | bool Dbg::IsForcedInterpreterNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3397 | instrumentation::Instrumentation* const instrumentation = |
| 3398 | Runtime::Current()->GetInstrumentation(); |
| 3399 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3400 | if (instrumentation->InterpretOnly()) { |
| 3401 | return false; |
| 3402 | } |
| 3403 | // We can only interpret pure Java method. |
| 3404 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3405 | return false; |
| 3406 | } |
| 3407 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3408 | if (ssc != nullptr) { |
| 3409 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3410 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3411 | return true; |
| 3412 | } |
| 3413 | // If we are stepping out from a static initializer, by issuing a step |
| 3414 | // in or step over, that was implicitly invoked by calling a static method, |
| 3415 | // then we need to step into that method. Having a lower stack depth than |
| 3416 | // the one the single step control has indicates that the step originates |
| 3417 | // from the static initializer. |
| 3418 | if (ssc->GetStepDepth() != JDWP::SD_OUT && |
| 3419 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3420 | return true; |
| 3421 | } |
| 3422 | } |
| 3423 | // There are cases where we have to force interpreter on deoptimized methods, |
| 3424 | // because in some cases the call will not be performed by invoking an entry |
| 3425 | // point that has been replaced by the deoptimization, but instead by directly |
| 3426 | // invoking the compiled code of the method, for example. |
| 3427 | return instrumentation->IsDeoptimized(m); |
| 3428 | } |
| 3429 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3430 | bool Dbg::IsForcedInstrumentationNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3431 | // 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] | 3432 | if (m == nullptr) { |
| 3433 | return false; |
| 3434 | } |
| 3435 | instrumentation::Instrumentation* const instrumentation = |
| 3436 | Runtime::Current()->GetInstrumentation(); |
| 3437 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3438 | if (instrumentation->InterpretOnly()) { |
| 3439 | return false; |
| 3440 | } |
| 3441 | // We can only interpret pure Java method. |
| 3442 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3443 | return false; |
| 3444 | } |
| 3445 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3446 | if (ssc != nullptr) { |
| 3447 | // If we are stepping out from a static initializer, by issuing a step |
| 3448 | // out, that was implicitly invoked by calling a static method, then we |
| 3449 | // need to step into the caller of that method. Having a lower stack |
| 3450 | // depth than the one the single step control has indicates that the |
| 3451 | // step originates from the static initializer. |
| 3452 | if (ssc->GetStepDepth() == JDWP::SD_OUT && |
| 3453 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3454 | return true; |
| 3455 | } |
| 3456 | } |
| 3457 | // If we are returning from a static intializer, that was implicitly |
| 3458 | // invoked by calling a static method and the caller is deoptimized, |
| 3459 | // then we have to deoptimize the stack without forcing interpreter |
| 3460 | // on the static method that was called originally. This problem can |
| 3461 | // be solved easily by forcing instrumentation on the called method, |
| 3462 | // because the instrumentation exit hook will recognise the need of |
| 3463 | // stack deoptimization by calling IsForcedInterpreterNeededForUpcall. |
| 3464 | return instrumentation->IsDeoptimized(m); |
| 3465 | } |
| 3466 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3467 | bool Dbg::IsForcedInterpreterNeededForUpcallImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3468 | // 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] | 3469 | if (m == nullptr) { |
| 3470 | return false; |
| 3471 | } |
| 3472 | instrumentation::Instrumentation* const instrumentation = |
| 3473 | Runtime::Current()->GetInstrumentation(); |
| 3474 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3475 | if (instrumentation->InterpretOnly()) { |
| 3476 | return false; |
| 3477 | } |
| 3478 | // We can only interpret pure Java method. |
| 3479 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3480 | return false; |
| 3481 | } |
| 3482 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3483 | if (ssc != nullptr) { |
| 3484 | // The debugger is not interested in what is happening under the level |
| 3485 | // of the step, thus we only force interpreter when we are not below of |
| 3486 | // the step. |
| 3487 | if (ssc->GetStackDepth() >= GetStackDepth(thread)) { |
| 3488 | return true; |
| 3489 | } |
| 3490 | } |
| 3491 | // We have to require stack deoptimization if the upcall is deoptimized. |
| 3492 | return instrumentation->IsDeoptimized(m); |
| 3493 | } |
| 3494 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3495 | // Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't |
| 3496 | // cause suspension if the thread is the current thread. |
| 3497 | class ScopedThreadSuspension { |
| 3498 | public: |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3499 | ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id) |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 3500 | LOCKS_EXCLUDED(Locks::thread_list_lock_) |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3501 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3502 | thread_(nullptr), |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3503 | error_(JDWP::ERR_NONE), |
| 3504 | self_suspend_(false), |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3505 | other_suspend_(false) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3506 | ScopedObjectAccessUnchecked soa(self); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 3507 | thread_ = DecodeThread(soa, thread_id, &error_); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3508 | if (error_ == JDWP::ERR_NONE) { |
| 3509 | if (thread_ == soa.Self()) { |
| 3510 | self_suspend_ = true; |
| 3511 | } else { |
| 3512 | soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3513 | jobject thread_peer = Dbg::GetObjectRegistry()->GetJObject(thread_id); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3514 | bool timed_out; |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 3515 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 3516 | Thread* suspended_thread = thread_list->SuspendThreadByPeer(thread_peer, true, true, |
| 3517 | &timed_out); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3518 | CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension); |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3519 | if (suspended_thread == nullptr) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3520 | // Thread terminated from under us while suspending. |
| 3521 | error_ = JDWP::ERR_INVALID_THREAD; |
| 3522 | } else { |
| 3523 | CHECK_EQ(suspended_thread, thread_); |
| 3524 | other_suspend_ = true; |
| 3525 | } |
| 3526 | } |
| 3527 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3528 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3529 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3530 | Thread* GetThread() const { |
| 3531 | return thread_; |
| 3532 | } |
| 3533 | |
| 3534 | JDWP::JdwpError GetError() const { |
| 3535 | return error_; |
| 3536 | } |
| 3537 | |
| 3538 | ~ScopedThreadSuspension() { |
| 3539 | if (other_suspend_) { |
| 3540 | Runtime::Current()->GetThreadList()->Resume(thread_, true); |
| 3541 | } |
| 3542 | } |
| 3543 | |
| 3544 | private: |
| 3545 | Thread* thread_; |
| 3546 | JDWP::JdwpError error_; |
| 3547 | bool self_suspend_; |
| 3548 | bool other_suspend_; |
| 3549 | }; |
| 3550 | |
| 3551 | JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size, |
| 3552 | JDWP::JdwpStepDepth step_depth) { |
| 3553 | Thread* self = Thread::Current(); |
| 3554 | ScopedThreadSuspension sts(self, thread_id); |
| 3555 | if (sts.GetError() != JDWP::ERR_NONE) { |
| 3556 | return sts.GetError(); |
| 3557 | } |
| 3558 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3559 | // 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] | 3560 | // is for step-out. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3561 | struct SingleStepStackVisitor : public StackVisitor { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3562 | explicit SingleStepStackVisitor(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 3563 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 3564 | stack_depth(0), |
| 3565 | method(nullptr), |
| 3566 | line_number(-1) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 3567 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3568 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 3569 | // annotalysis. |
| 3570 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3571 | ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3572 | if (!m->IsRuntimeMethod()) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3573 | ++stack_depth; |
| 3574 | if (method == nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3575 | mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3576 | method = m; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3577 | if (dex_cache != nullptr) { |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 3578 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3579 | line_number = dex_file.GetLineNumFromPC(m, GetDexPc()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3580 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3581 | } |
| 3582 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 3583 | return true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3584 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3585 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3586 | int stack_depth; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3587 | ArtMethod* method; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3588 | int32_t line_number; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3589 | }; |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3590 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3591 | Thread* const thread = sts.GetThread(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3592 | SingleStepStackVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3593 | visitor.WalkStack(); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3594 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3595 | // Find the dex_pc values that correspond to the current line, for line-based single-stepping. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3596 | struct DebugCallbackContext { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3597 | explicit DebugCallbackContext(SingleStepControl* single_step_control_cb, |
| 3598 | int32_t line_number_cb, const DexFile::CodeItem* code_item) |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3599 | : single_step_control_(single_step_control_cb), line_number_(line_number_cb), |
| 3600 | code_item_(code_item), last_pc_valid(false), last_pc(0) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3601 | } |
| 3602 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3603 | static bool Callback(void* raw_context, uint32_t address, uint32_t line_number_cb) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3604 | DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3605 | if (static_cast<int32_t>(line_number_cb) == context->line_number_) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3606 | if (!context->last_pc_valid) { |
| 3607 | // Everything from this address until the next line change is ours. |
| 3608 | context->last_pc = address; |
| 3609 | context->last_pc_valid = true; |
| 3610 | } |
| 3611 | // Otherwise, if we're already in a valid range for this line, |
| 3612 | // just keep going (shouldn't really happen)... |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3613 | } else if (context->last_pc_valid) { // and the line number is new |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3614 | // Add everything from the last entry up until here to the set |
| 3615 | for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3616 | context->single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3617 | } |
| 3618 | context->last_pc_valid = false; |
| 3619 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3620 | return false; // There may be multiple entries for any given line. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3621 | } |
| 3622 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3623 | ~DebugCallbackContext() { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3624 | // If the line number was the last in the position table... |
| 3625 | if (last_pc_valid) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3626 | size_t end = code_item_->insns_size_in_code_units_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3627 | for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3628 | single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3629 | } |
| 3630 | } |
| 3631 | } |
| 3632 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3633 | SingleStepControl* const single_step_control_; |
| 3634 | const int32_t line_number_; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3635 | const DexFile::CodeItem* const code_item_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3636 | bool last_pc_valid; |
| 3637 | uint32_t last_pc; |
| 3638 | }; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3639 | |
| 3640 | // Allocate single step. |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3641 | SingleStepControl* single_step_control = |
| 3642 | new (std::nothrow) SingleStepControl(step_size, step_depth, |
| 3643 | visitor.stack_depth, visitor.method); |
| 3644 | if (single_step_control == nullptr) { |
| 3645 | LOG(ERROR) << "Failed to allocate SingleStepControl"; |
| 3646 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3647 | } |
| 3648 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3649 | ArtMethod* m = single_step_control->GetMethod(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3650 | const int32_t line_number = visitor.line_number; |
Sebastien Hertz | 52f5f93 | 2015-05-28 11:00:57 +0200 | [diff] [blame] | 3651 | // Note: if the thread is not running Java code (pure native thread), there is no "current" |
| 3652 | // method on the stack (and no line number either). |
| 3653 | if (m != nullptr && !m->IsNative()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3654 | const DexFile::CodeItem* const code_item = m->GetCodeItem(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3655 | DebugCallbackContext context(single_step_control, line_number, code_item); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3656 | m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3657 | DebugCallbackContext::Callback, nullptr, &context); |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 3658 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3659 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3660 | // Activate single-step in the thread. |
| 3661 | thread->ActivateSingleStepControl(single_step_control); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3662 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3663 | if (VLOG_IS_ON(jdwp)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3664 | VLOG(jdwp) << "Single-step thread: " << *thread; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3665 | VLOG(jdwp) << "Single-step step size: " << single_step_control->GetStepSize(); |
| 3666 | VLOG(jdwp) << "Single-step step depth: " << single_step_control->GetStepDepth(); |
| 3667 | VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->GetMethod()); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3668 | VLOG(jdwp) << "Single-step current line: " << line_number; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3669 | VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->GetStackDepth(); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3670 | VLOG(jdwp) << "Single-step dex_pc values:"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3671 | for (uint32_t dex_pc : single_step_control->GetDexPcs()) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3672 | VLOG(jdwp) << StringPrintf(" %#x", dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3673 | } |
| 3674 | } |
| 3675 | |
| 3676 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3677 | } |
| 3678 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3679 | void Dbg::UnconfigureStep(JDWP::ObjectId thread_id) { |
| 3680 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3681 | JDWP::JdwpError error; |
| 3682 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Sebastien Hertz | 87118ed | 2013-11-26 17:57:18 +0100 | [diff] [blame] | 3683 | if (error == JDWP::ERR_NONE) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3684 | thread->DeactivateSingleStepControl(); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3685 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3686 | } |
| 3687 | |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3688 | static char JdwpTagToShortyChar(JDWP::JdwpTag tag) { |
| 3689 | switch (tag) { |
| 3690 | default: |
| 3691 | LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag); |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 3692 | UNREACHABLE(); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3693 | |
| 3694 | // Primitives. |
| 3695 | case JDWP::JT_BYTE: return 'B'; |
| 3696 | case JDWP::JT_CHAR: return 'C'; |
| 3697 | case JDWP::JT_FLOAT: return 'F'; |
| 3698 | case JDWP::JT_DOUBLE: return 'D'; |
| 3699 | case JDWP::JT_INT: return 'I'; |
| 3700 | case JDWP::JT_LONG: return 'J'; |
| 3701 | case JDWP::JT_SHORT: return 'S'; |
| 3702 | case JDWP::JT_VOID: return 'V'; |
| 3703 | case JDWP::JT_BOOLEAN: return 'Z'; |
| 3704 | |
| 3705 | // Reference types. |
| 3706 | case JDWP::JT_ARRAY: |
| 3707 | case JDWP::JT_OBJECT: |
| 3708 | case JDWP::JT_STRING: |
| 3709 | case JDWP::JT_THREAD: |
| 3710 | case JDWP::JT_THREAD_GROUP: |
| 3711 | case JDWP::JT_CLASS_LOADER: |
| 3712 | case JDWP::JT_CLASS_OBJECT: |
| 3713 | return 'L'; |
| 3714 | } |
| 3715 | } |
| 3716 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3717 | JDWP::JdwpError Dbg::PrepareInvokeMethod(uint32_t request_id, JDWP::ObjectId thread_id, |
| 3718 | JDWP::ObjectId object_id, JDWP::RefTypeId class_id, |
| 3719 | JDWP::MethodId method_id, uint32_t arg_count, |
| 3720 | uint64_t arg_values[], JDWP::JdwpTag* arg_types, |
| 3721 | uint32_t options) { |
| 3722 | Thread* const self = Thread::Current(); |
| 3723 | CHECK_EQ(self, GetDebugThread()) << "This must be called by the JDWP thread"; |
Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame^] | 3724 | const bool resume_all_threads = ((options & JDWP::INVOKE_SINGLE_THREADED) == 0); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3725 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3726 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3727 | Thread* targetThread = nullptr; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3728 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3729 | ScopedObjectAccessUnchecked soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3730 | JDWP::JdwpError error; |
| 3731 | targetThread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 3732 | if (error != JDWP::ERR_NONE) { |
| 3733 | LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id; |
| 3734 | return error; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3735 | } |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3736 | if (targetThread->GetInvokeReq() != nullptr) { |
| 3737 | // Thread is already invoking a method on behalf of the debugger. |
| 3738 | LOG(ERROR) << "InvokeMethod request for thread already invoking a method: " << *targetThread; |
| 3739 | return JDWP::ERR_ALREADY_INVOKING; |
| 3740 | } |
| 3741 | if (!targetThread->IsReadyForDebugInvoke()) { |
| 3742 | // 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] | 3743 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 3744 | return JDWP::ERR_INVALID_THREAD; |
| 3745 | } |
| 3746 | |
| 3747 | /* |
Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame^] | 3748 | * According to the JDWP specs, we are expected to resume all threads (or only the |
| 3749 | * target thread) once. So if a thread has been suspended more than once (either by |
| 3750 | * the debugger for an event or by the runtime for GC), it will remain suspended before |
| 3751 | * the invoke is executed. This means the debugger is responsible to properly resume all |
| 3752 | * 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] | 3753 | * |
Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame^] | 3754 | * However, for compatibility reason with older versions of debuggers (like Eclipse), we |
| 3755 | * fully resume all threads (by canceling *all* debugger suspensions) when the debugger |
| 3756 | * wants us to resume all threads. This is to avoid ending up in deadlock situation. |
| 3757 | * |
| 3758 | * On the other hand, if we are asked to only resume the target thread, then we follow the |
| 3759 | * JDWP specs by resuming that thread only once. This means the thread will remain suspended |
| 3760 | * if it has been suspended more than once before the invoke (and again, this is the |
| 3761 | * 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] | 3762 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3763 | int suspend_count; |
| 3764 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3765 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3766 | suspend_count = targetThread->GetSuspendCount(); |
| 3767 | } |
Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame^] | 3768 | if (suspend_count > 1 && resume_all_threads) { |
| 3769 | // The target thread will remain suspended even after we resume it. Let's emit a warning |
| 3770 | // to indicate the invoke won't be executed until the thread is resumed. |
| 3771 | LOG(WARNING) << *targetThread << " suspended more than once (suspend count == " |
| 3772 | << suspend_count << "). This thread will invoke the method only once " |
| 3773 | << "it is fully resumed."; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3774 | } |
| 3775 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3776 | mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 3777 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3778 | return JDWP::ERR_INVALID_OBJECT; |
| 3779 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3780 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3781 | gRegistry->Get<mirror::Object*>(thread_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3782 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3783 | return JDWP::ERR_INVALID_OBJECT; |
| 3784 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3785 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3786 | mirror::Class* c = DecodeClass(class_id, &error); |
| 3787 | if (c == nullptr) { |
| 3788 | return error; |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3789 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3790 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3791 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3792 | if (m->IsStatic() != (receiver == nullptr)) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3793 | return JDWP::ERR_INVALID_METHODID; |
| 3794 | } |
| 3795 | if (m->IsStatic()) { |
| 3796 | if (m->GetDeclaringClass() != c) { |
| 3797 | return JDWP::ERR_INVALID_METHODID; |
| 3798 | } |
| 3799 | } else { |
| 3800 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
| 3801 | return JDWP::ERR_INVALID_METHODID; |
| 3802 | } |
| 3803 | } |
| 3804 | |
| 3805 | // Check the argument list matches the method. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3806 | uint32_t shorty_len = 0; |
| 3807 | const char* shorty = m->GetShorty(&shorty_len); |
| 3808 | if (shorty_len - 1 != arg_count) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3809 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3810 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3811 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3812 | { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3813 | StackHandleScope<2> hs(soa.Self()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3814 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver)); |
| 3815 | HandleWrapper<mirror::Class> h_klass(hs.NewHandleWrapper(&c)); |
| 3816 | const DexFile::TypeList* types = m->GetParameterTypeList(); |
| 3817 | for (size_t i = 0; i < arg_count; ++i) { |
| 3818 | if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) { |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3819 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3820 | } |
| 3821 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3822 | if (shorty[i + 1] == 'L') { |
| 3823 | // Did we really get an argument of an appropriate reference type? |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 3824 | mirror::Class* parameter_type = |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3825 | m->GetClassFromTypeIndex(types->GetTypeItem(i).type_idx_, true); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3826 | mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i], &error); |
| 3827 | if (error != JDWP::ERR_NONE) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3828 | return JDWP::ERR_INVALID_OBJECT; |
| 3829 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3830 | if (argument != nullptr && !argument->InstanceOf(parameter_type)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3831 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3832 | } |
| 3833 | |
| 3834 | // Turn the on-the-wire ObjectId into a jobject. |
| 3835 | jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]); |
| 3836 | v.l = gRegistry->GetJObject(arg_values[i]); |
| 3837 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3838 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3839 | } |
| 3840 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3841 | // Allocates a DebugInvokeReq. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3842 | DebugInvokeReq* req = new (std::nothrow) DebugInvokeReq(request_id, thread_id, receiver, c, m, |
| 3843 | options, arg_values, arg_count); |
| 3844 | if (req == nullptr) { |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3845 | LOG(ERROR) << "Failed to allocate DebugInvokeReq"; |
| 3846 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3847 | } |
| 3848 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3849 | // Attaches the DebugInvokeReq to the target thread so it executes the method when |
| 3850 | // it is resumed. Once the invocation completes, the target thread will delete it before |
| 3851 | // suspending itself (see ThreadList::SuspendSelfForDebugger). |
| 3852 | targetThread->SetDebugInvokeReq(req); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3853 | } |
| 3854 | |
| 3855 | // 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] | 3856 | // away we're sitting high and dry -- but we must release this before the UndoDebuggerSuspensions |
| 3857 | // call. |
Sebastien Hertz | d4032e4 | 2015-06-12 15:47:34 +0200 | [diff] [blame^] | 3858 | if (resume_all_threads) { |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3859 | VLOG(jdwp) << " Resuming all threads"; |
| 3860 | thread_list->UndoDebuggerSuspensions(); |
| 3861 | } else { |
| 3862 | VLOG(jdwp) << " Resuming event thread only"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3863 | thread_list->Resume(targetThread, true); |
| 3864 | } |
| 3865 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3866 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3870 | Thread* const self = Thread::Current(); |
| 3871 | CHECK_NE(self, GetDebugThread()) << "This must be called by the event thread"; |
| 3872 | |
| 3873 | ScopedObjectAccess soa(self); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3874 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 3875 | // We can be called while an exception is pending. We need |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3876 | // to preserve that across the method invocation. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3877 | StackHandleScope<1> hs(soa.Self()); |
| 3878 | Handle<mirror::Throwable> old_exception = hs.NewHandle(soa.Self()->GetException()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3879 | soa.Self()->ClearException(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3880 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3881 | // Execute the method then sends reply to the debugger. |
| 3882 | ExecuteMethodWithoutPendingException(soa, pReq); |
| 3883 | |
| 3884 | // If an exception was pending before the invoke, restore it now. |
| 3885 | if (old_exception.Get() != nullptr) { |
| 3886 | soa.Self()->SetException(old_exception.Get()); |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | // Helper function: write a variable-width value into the output input buffer. |
| 3891 | static void WriteValue(JDWP::ExpandBuf* pReply, int width, uint64_t value) { |
| 3892 | switch (width) { |
| 3893 | case 1: |
| 3894 | expandBufAdd1(pReply, value); |
| 3895 | break; |
| 3896 | case 2: |
| 3897 | expandBufAdd2BE(pReply, value); |
| 3898 | break; |
| 3899 | case 4: |
| 3900 | expandBufAdd4BE(pReply, value); |
| 3901 | break; |
| 3902 | case 8: |
| 3903 | expandBufAdd8BE(pReply, value); |
| 3904 | break; |
| 3905 | default: |
| 3906 | LOG(FATAL) << width; |
| 3907 | UNREACHABLE(); |
| 3908 | } |
| 3909 | } |
| 3910 | |
| 3911 | void Dbg::ExecuteMethodWithoutPendingException(ScopedObjectAccess& soa, DebugInvokeReq* pReq) { |
| 3912 | soa.Self()->AssertNoPendingException(); |
| 3913 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3914 | // 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] | 3915 | ArtMethod* m = pReq->method; |
| 3916 | size_t image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3917 | if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver.Read() != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3918 | ArtMethod* actual_method = |
| 3919 | pReq->klass.Read()->FindVirtualMethodForVirtualOrInterface(m, image_pointer_size); |
| 3920 | if (actual_method != m) { |
| 3921 | VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3922 | << " to " << PrettyMethod(actual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3923 | m = actual_method; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3924 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3925 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3926 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m) |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3927 | << " receiver=" << pReq->receiver.Read() |
Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 3928 | << " arg_count=" << pReq->arg_count; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3929 | CHECK(m != nullptr); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3930 | |
Roland Levillain | 33d6903 | 2015-06-18 18:20:59 +0100 | [diff] [blame] | 3931 | 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] | 3932 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3933 | // Invoke the method. |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 3934 | ScopedLocalRef<jobject> ref(soa.Env(), soa.AddLocalReference<jobject>(pReq->receiver.Read())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3935 | JValue result = InvokeWithJValues(soa, ref.get(), soa.EncodeMethod(m), |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3936 | reinterpret_cast<jvalue*>(pReq->arg_values.get())); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3937 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3938 | // Prepare JDWP ids for the reply. |
| 3939 | JDWP::JdwpTag result_tag = BasicTagFromDescriptor(m->GetShorty()); |
| 3940 | const bool is_object_result = (result_tag == JDWP::JT_OBJECT); |
| 3941 | StackHandleScope<2> hs(soa.Self()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3942 | Handle<mirror::Object> object_result = hs.NewHandle(is_object_result ? result.GetL() : nullptr); |
| 3943 | Handle<mirror::Throwable> exception = hs.NewHandle(soa.Self()->GetException()); |
| 3944 | soa.Self()->ClearException(); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3945 | |
| 3946 | if (!IsDebuggerActive()) { |
| 3947 | // The debugger detached: we must not re-suspend threads. We also don't need to fill the reply |
| 3948 | // because it won't be sent either. |
| 3949 | return; |
| 3950 | } |
| 3951 | |
| 3952 | JDWP::ObjectId exceptionObjectId = gRegistry->Add(exception); |
| 3953 | uint64_t result_value = 0; |
| 3954 | if (exceptionObjectId != 0) { |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3955 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exception.Get() |
| 3956 | << " " << exception->Dump(); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3957 | result_value = 0; |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3958 | } else if (is_object_result) { |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3959 | /* if no exception was thrown, examine object result more closely */ |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3960 | JDWP::JdwpTag new_tag = TagFromObject(soa, object_result.Get()); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3961 | if (new_tag != result_tag) { |
| 3962 | VLOG(jdwp) << " JDWP promoted result from " << result_tag << " to " << new_tag; |
| 3963 | result_tag = new_tag; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3964 | } |
| 3965 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3966 | // Register the object in the registry and reference its ObjectId. This ensures |
| 3967 | // 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] | 3968 | result_value = gRegistry->Add(object_result.Get()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3969 | } else { |
| 3970 | // Primitive result. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3971 | DCHECK(IsPrimitiveTag(result_tag)); |
| 3972 | result_value = result.GetJ(); |
| 3973 | } |
| 3974 | const bool is_constructor = m->IsConstructor() && !m->IsStatic(); |
| 3975 | if (is_constructor) { |
| 3976 | // If we invoked a constructor (which actually returns void), return the receiver, |
| 3977 | // unless we threw, in which case we return null. |
| 3978 | result_tag = JDWP::JT_OBJECT; |
| 3979 | if (exceptionObjectId == 0) { |
| 3980 | // TODO we could keep the receiver ObjectId in the DebugInvokeReq to avoid looking into the |
| 3981 | // object registry. |
| 3982 | result_value = GetObjectRegistry()->Add(pReq->receiver.Read()); |
| 3983 | } else { |
| 3984 | result_value = 0; |
| 3985 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3986 | } |
| 3987 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3988 | // Suspend other threads if the invoke is not single-threaded. |
| 3989 | if ((pReq->options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
| 3990 | soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
| 3991 | VLOG(jdwp) << " Suspending all threads"; |
| 3992 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
| 3993 | soa.Self()->TransitionFromSuspendedToRunnable(); |
| 3994 | } |
| 3995 | |
| 3996 | VLOG(jdwp) << " --> returned " << result_tag |
| 3997 | << StringPrintf(" %#" PRIx64 " (except=%#" PRIx64 ")", result_value, |
| 3998 | exceptionObjectId); |
| 3999 | |
| 4000 | // Show detailed debug output. |
| 4001 | if (result_tag == JDWP::JT_STRING && exceptionObjectId == 0) { |
| 4002 | if (result_value != 0) { |
| 4003 | if (VLOG_IS_ON(jdwp)) { |
| 4004 | std::string result_string; |
| 4005 | JDWP::JdwpError error = Dbg::StringToUtf8(result_value, &result_string); |
| 4006 | CHECK_EQ(error, JDWP::ERR_NONE); |
| 4007 | VLOG(jdwp) << " string '" << result_string << "'"; |
| 4008 | } |
| 4009 | } else { |
| 4010 | VLOG(jdwp) << " string (null)"; |
| 4011 | } |
| 4012 | } |
| 4013 | |
| 4014 | // Attach the reply to DebugInvokeReq so it can be sent to the debugger when the event thread |
| 4015 | // is ready to suspend. |
| 4016 | BuildInvokeReply(pReq->reply, pReq->request_id, result_tag, result_value, exceptionObjectId); |
| 4017 | } |
| 4018 | |
| 4019 | void Dbg::BuildInvokeReply(JDWP::ExpandBuf* pReply, uint32_t request_id, JDWP::JdwpTag result_tag, |
| 4020 | uint64_t result_value, JDWP::ObjectId exception) { |
| 4021 | // Make room for the JDWP header since we do not know the size of the reply yet. |
| 4022 | JDWP::expandBufAddSpace(pReply, kJDWPHeaderLen); |
| 4023 | |
| 4024 | size_t width = GetTagWidth(result_tag); |
| 4025 | JDWP::expandBufAdd1(pReply, result_tag); |
| 4026 | if (width != 0) { |
| 4027 | WriteValue(pReply, width, result_value); |
| 4028 | } |
| 4029 | JDWP::expandBufAdd1(pReply, JDWP::JT_OBJECT); |
| 4030 | JDWP::expandBufAddObjectId(pReply, exception); |
| 4031 | |
| 4032 | // Now we know the size, we can complete the JDWP header. |
| 4033 | uint8_t* buf = expandBufGetBuffer(pReply); |
| 4034 | JDWP::Set4BE(buf + kJDWPHeaderSizeOffset, expandBufGetLength(pReply)); |
| 4035 | JDWP::Set4BE(buf + kJDWPHeaderIdOffset, request_id); |
| 4036 | JDWP::Set1(buf + kJDWPHeaderFlagsOffset, kJDWPFlagReply); // flags |
| 4037 | JDWP::Set2BE(buf + kJDWPHeaderErrorCodeOffset, JDWP::ERR_NONE); |
| 4038 | } |
| 4039 | |
| 4040 | void Dbg::FinishInvokeMethod(DebugInvokeReq* pReq) { |
| 4041 | CHECK_NE(Thread::Current(), GetDebugThread()) << "This must be called by the event thread"; |
| 4042 | |
| 4043 | JDWP::ExpandBuf* const pReply = pReq->reply; |
| 4044 | CHECK(pReply != nullptr) << "No reply attached to DebugInvokeReq"; |
| 4045 | |
| 4046 | // We need to prevent other threads (including JDWP thread) from interacting with the debugger |
| 4047 | // while we send the reply but are not yet suspended. The JDWP token will be released just before |
| 4048 | // we suspend ourself again (see ThreadList::SuspendSelfForDebugger). |
| 4049 | gJdwpState->AcquireJdwpTokenForEvent(pReq->thread_id); |
| 4050 | |
| 4051 | // Send the reply unless the debugger detached before the completion of the method. |
| 4052 | if (IsDebuggerActive()) { |
| 4053 | const size_t replyDataLength = expandBufGetLength(pReply) - kJDWPHeaderLen; |
| 4054 | VLOG(jdwp) << StringPrintf("REPLY INVOKE id=0x%06x (length=%zu)", |
| 4055 | pReq->request_id, replyDataLength); |
| 4056 | |
| 4057 | gJdwpState->SendRequest(pReply); |
| 4058 | } else { |
| 4059 | VLOG(jdwp) << "Not sending invoke reply because debugger detached"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4060 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4063 | /* |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4064 | * "request" contains a full JDWP packet, possibly with multiple chunks. We |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4065 | * need to process each, accumulate the replies, and ship the whole thing |
| 4066 | * back. |
| 4067 | * |
| 4068 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 4069 | * and includes the chunk type/length, followed by the data. |
| 4070 | * |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 4071 | * OLD-TODO: we currently assume that the request and reply include a single |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4072 | * chunk. If this becomes inconvenient we will need to adapt. |
| 4073 | */ |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4074 | bool Dbg::DdmHandlePacket(JDWP::Request* request, uint8_t** pReplyBuf, int* pReplyLen) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4075 | Thread* self = Thread::Current(); |
| 4076 | JNIEnv* env = self->GetJniEnv(); |
| 4077 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4078 | uint32_t type = request->ReadUnsigned32("type"); |
| 4079 | uint32_t length = request->ReadUnsigned32("length"); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4080 | |
| 4081 | // Create a byte[] corresponding to 'request'. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4082 | size_t request_length = request->size(); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4083 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4084 | if (dataArray.get() == nullptr) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4085 | LOG(WARNING) << "byte[] allocation failed: " << request_length; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4086 | env->ExceptionClear(); |
| 4087 | return false; |
| 4088 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4089 | env->SetByteArrayRegion(dataArray.get(), 0, request_length, |
| 4090 | reinterpret_cast<const jbyte*>(request->data())); |
| 4091 | request->Skip(request_length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4092 | |
| 4093 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4094 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4095 | if (length != request_length) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4096 | 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] | 4097 | return false; |
| 4098 | } |
| 4099 | |
| 4100 | // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)". |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4101 | ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4102 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch, |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4103 | type, dataArray.get(), 0, length)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4104 | if (env->ExceptionCheck()) { |
| 4105 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 4106 | env->ExceptionDescribe(); |
| 4107 | env->ExceptionClear(); |
| 4108 | return false; |
| 4109 | } |
| 4110 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4111 | if (chunk.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4112 | return false; |
| 4113 | } |
| 4114 | |
| 4115 | /* |
| 4116 | * Pull the pieces out of the chunk. We copy the results into a |
| 4117 | * newly-allocated buffer that the caller can free. We don't want to |
| 4118 | * continue using the Chunk object because nothing has a reference to it. |
| 4119 | * |
| 4120 | * We could avoid this by returning type/data/offset/length and having |
| 4121 | * the caller be aware of the object lifetime issues, but that |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 4122 | * 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] | 4123 | * if we have responses for multiple chunks. |
| 4124 | * |
| 4125 | * So we're pretty much stuck with copying data around multiple times. |
| 4126 | */ |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4127 | ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data))); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4128 | jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4129 | length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4130 | type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4131 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4132 | VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4133 | if (length == 0 || replyData.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4134 | return false; |
| 4135 | } |
| 4136 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4137 | const int kChunkHdrLen = 8; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4138 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4139 | if (reply == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4140 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 4141 | return false; |
| 4142 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 4143 | JDWP::Set4BE(reply + 0, type); |
| 4144 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4145 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4146 | |
| 4147 | *pReplyBuf = reply; |
| 4148 | *pReplyLen = length + kChunkHdrLen; |
| 4149 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4150 | VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4151 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4152 | } |
| 4153 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4154 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4155 | VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4156 | |
| 4157 | Thread* self = Thread::Current(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4158 | if (self->GetState() != kRunnable) { |
| 4159 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 4160 | /* try anyway? */ |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4164 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4165 | env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4166 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast, |
| 4167 | event); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4168 | if (env->ExceptionCheck()) { |
| 4169 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 4170 | env->ExceptionDescribe(); |
| 4171 | env->ExceptionClear(); |
| 4172 | } |
| 4173 | } |
| 4174 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4175 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4176 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4180 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4181 | gDdmThreadNotification = false; |
| 4182 | } |
| 4183 | |
| 4184 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4185 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4186 | * |
| 4187 | * Because we broadcast the full set of threads when the notifications are |
| 4188 | * first enabled, it's possible for "thread" to be actively executing. |
| 4189 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4190 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4191 | if (!gDdmThreadNotification) { |
| 4192 | return; |
| 4193 | } |
| 4194 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4195 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4196 | uint8_t buf[4]; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4197 | JDWP::Set4BE(&buf[0], t->GetThreadId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4198 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4199 | } else { |
| 4200 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4201 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4202 | StackHandleScope<1> hs(soa.Self()); |
| 4203 | Handle<mirror::String> name(hs.NewHandle(t->GetThreadName(soa))); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4204 | size_t char_count = (name.Get() != nullptr) ? name->GetLength() : 0; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 4205 | const jchar* chars = (name.Get() != nullptr) ? name->GetValue() : nullptr; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4206 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4207 | std::vector<uint8_t> bytes; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4208 | JDWP::Append4BE(bytes, t->GetThreadId()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4209 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4210 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 4211 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4212 | } |
| 4213 | } |
| 4214 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4215 | void Dbg::DdmSetThreadNotification(bool enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4216 | // Enable/disable thread notifications. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4217 | gDdmThreadNotification = enable; |
| 4218 | if (enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4219 | // Suspend the VM then post thread start notifications for all threads. Threads attaching will |
| 4220 | // see a suspension in progress and block until that ends. They then post their own start |
| 4221 | // notification. |
| 4222 | SuspendVM(); |
| 4223 | std::list<Thread*> threads; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4224 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4225 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4226 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4227 | threads = Runtime::Current()->GetThreadList()->GetList(); |
| 4228 | } |
| 4229 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4230 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4231 | for (Thread* thread : threads) { |
| 4232 | Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR")); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4233 | } |
| 4234 | } |
| 4235 | ResumeVM(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4236 | } |
| 4237 | } |
| 4238 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4239 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 4240 | if (IsDebuggerActive()) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 4241 | gJdwpState->PostThreadChange(t, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4242 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4243 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4247 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4248 | } |
| 4249 | |
| 4250 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4251 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4252 | } |
| 4253 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4254 | void Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4255 | CHECK(buf != nullptr); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4256 | iovec vec[1]; |
| 4257 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 4258 | vec[0].iov_len = byte_count; |
| 4259 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4260 | } |
| 4261 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4262 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 4263 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 4264 | } |
| 4265 | |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 4266 | void Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4267 | if (gJdwpState == nullptr) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4268 | VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4269 | } else { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 4270 | gJdwpState->DdmSendChunkV(type, iov, iov_count); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4271 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4272 | } |
| 4273 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 4274 | JDWP::JdwpState* Dbg::GetJdwpState() { |
| 4275 | return gJdwpState; |
| 4276 | } |
| 4277 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4278 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 4279 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4280 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4281 | return true; |
| 4282 | } |
| 4283 | |
| 4284 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 4285 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 4286 | return false; |
| 4287 | } |
| 4288 | |
| 4289 | gDdmHpifWhen = when; |
| 4290 | return true; |
| 4291 | } |
| 4292 | |
| 4293 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 4294 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 4295 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 4296 | return false; |
| 4297 | } |
| 4298 | |
| 4299 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 4300 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 4301 | return false; |
| 4302 | } |
| 4303 | |
| 4304 | if (native) { |
| 4305 | gDdmNhsgWhen = when; |
| 4306 | gDdmNhsgWhat = what; |
| 4307 | } else { |
| 4308 | gDdmHpsgWhen = when; |
| 4309 | gDdmHpsgWhat = what; |
| 4310 | } |
| 4311 | return true; |
| 4312 | } |
| 4313 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4314 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 4315 | // If there's a one-shot 'when', reset it. |
| 4316 | if (reason == gDdmHpifWhen) { |
| 4317 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 4318 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 4319 | } |
| 4320 | } |
| 4321 | |
| 4322 | /* |
| 4323 | * Chunk HPIF (client --> server) |
| 4324 | * |
| 4325 | * Heap Info. General information about the heap, |
| 4326 | * suitable for a summary display. |
| 4327 | * |
| 4328 | * [u4]: number of heaps |
| 4329 | * |
| 4330 | * For each heap: |
| 4331 | * [u4]: heap ID |
| 4332 | * [u8]: timestamp in ms since Unix epoch |
| 4333 | * [u1]: capture reason (same as 'when' value from server) |
| 4334 | * [u4]: max heap size in bytes (-Xmx) |
| 4335 | * [u4]: current heap size in bytes |
| 4336 | * [u4]: current number of bytes allocated |
| 4337 | * [u4]: current number of objects allocated |
| 4338 | */ |
| 4339 | uint8_t heap_count = 1; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4340 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4341 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4342 | JDWP::Append4BE(bytes, heap_count); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4343 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4344 | JDWP::Append8BE(bytes, MilliTime()); |
| 4345 | JDWP::Append1BE(bytes, reason); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4346 | JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes. |
| 4347 | JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4348 | JDWP::Append4BE(bytes, heap->GetBytesAllocated()); |
| 4349 | JDWP::Append4BE(bytes, heap->GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4350 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 4351 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4352 | } |
| 4353 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4354 | enum HpsgSolidity { |
| 4355 | SOLIDITY_FREE = 0, |
| 4356 | SOLIDITY_HARD = 1, |
| 4357 | SOLIDITY_SOFT = 2, |
| 4358 | SOLIDITY_WEAK = 3, |
| 4359 | SOLIDITY_PHANTOM = 4, |
| 4360 | SOLIDITY_FINALIZABLE = 5, |
| 4361 | SOLIDITY_SWEEP = 6, |
| 4362 | }; |
| 4363 | |
| 4364 | enum HpsgKind { |
| 4365 | KIND_OBJECT = 0, |
| 4366 | KIND_CLASS_OBJECT = 1, |
| 4367 | KIND_ARRAY_1 = 2, |
| 4368 | KIND_ARRAY_2 = 3, |
| 4369 | KIND_ARRAY_4 = 4, |
| 4370 | KIND_ARRAY_8 = 5, |
| 4371 | KIND_UNKNOWN = 6, |
| 4372 | KIND_NATIVE = 7, |
| 4373 | }; |
| 4374 | |
| 4375 | #define HPSG_PARTIAL (1<<7) |
| 4376 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 4377 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4378 | class HeapChunkContext { |
| 4379 | public: |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4380 | // Maximum chunk size. Obtain this from the formula: |
| 4381 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 4382 | HeapChunkContext(bool merge, bool native) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4383 | : buf_(16384 - 16), |
| 4384 | type_(0), |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4385 | chunk_overhead_(0) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4386 | Reset(); |
| 4387 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4388 | type_ = CHUNK_TYPE("NHSG"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4389 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4390 | type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | ~HeapChunkContext() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4395 | if (p_ > &buf_[0]) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4396 | Flush(); |
| 4397 | } |
| 4398 | } |
| 4399 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4400 | void SetChunkOverhead(size_t chunk_overhead) { |
| 4401 | chunk_overhead_ = chunk_overhead; |
| 4402 | } |
| 4403 | |
| 4404 | void ResetStartOfNextChunk() { |
| 4405 | startOfNextMemoryChunk_ = nullptr; |
| 4406 | } |
| 4407 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4408 | void EnsureHeader(const void* chunk_ptr) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4409 | if (!needHeader_) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4410 | return; |
| 4411 | } |
| 4412 | |
| 4413 | // Start a new HPSx chunk. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4414 | JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap). |
| 4415 | JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4416 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4417 | JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 4418 | 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] | 4419 | // [u4]: length of piece, in allocation units |
| 4420 | // 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] | 4421 | pieceLenField_ = p_; |
| 4422 | JDWP::Write4BE(&p_, 0x55555555); |
| 4423 | needHeader_ = false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4424 | } |
| 4425 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4426 | void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4427 | if (pieceLenField_ == nullptr) { |
Ian Rogers | d636b06 | 2013-01-18 17:51:18 -0800 | [diff] [blame] | 4428 | // Flush immediately post Reset (maybe back-to-back Flush). Ignore. |
| 4429 | CHECK(needHeader_); |
| 4430 | return; |
| 4431 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4432 | // Patch the "length of piece" field. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4433 | CHECK_LE(&buf_[0], pieceLenField_); |
| 4434 | CHECK_LE(pieceLenField_, p_); |
| 4435 | JDWP::Set4BE(pieceLenField_, totalAllocationUnits_); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4436 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4437 | Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4438 | Reset(); |
| 4439 | } |
| 4440 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4441 | static void HeapChunkJavaCallback(void* start, void* end, size_t used_bytes, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4442 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 4443 | Locks::mutator_lock_) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4444 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkJavaCallback(start, end, used_bytes); |
| 4445 | } |
| 4446 | |
| 4447 | static void HeapChunkNativeCallback(void* start, void* end, size_t used_bytes, void* arg) |
| 4448 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4449 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkNativeCallback(start, end, used_bytes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4450 | } |
| 4451 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4452 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4453 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 4454 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4455 | void Reset() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4456 | p_ = &buf_[0]; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4457 | ResetStartOfNextChunk(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4458 | totalAllocationUnits_ = 0; |
| 4459 | needHeader_ = true; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4460 | pieceLenField_ = nullptr; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4461 | } |
| 4462 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4463 | bool IsNative() const { |
| 4464 | return type_ == CHUNK_TYPE("NHSG"); |
| 4465 | } |
| 4466 | |
| 4467 | // Returns true if the object is not an empty chunk. |
| 4468 | bool ProcessRecord(void* start, size_t used_bytes) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4469 | // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken |
| 4470 | // 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] | 4471 | if (used_bytes == 0) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4472 | if (start == nullptr) { |
| 4473 | // Reset for start of new heap. |
| 4474 | startOfNextMemoryChunk_ = nullptr; |
| 4475 | Flush(); |
| 4476 | } |
| 4477 | // Only process in use memory so that free region information |
| 4478 | // also includes dlmalloc book keeping. |
| 4479 | return false; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4480 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4481 | if (startOfNextMemoryChunk_ != nullptr) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4482 | // Transmit any pending free memory. Native free memory of over kMaxFreeLen could be because |
| 4483 | // of the use of mmaps, so don't report. If not free memory then start a new segment. |
| 4484 | bool flush = true; |
| 4485 | if (start > startOfNextMemoryChunk_) { |
| 4486 | const size_t kMaxFreeLen = 2 * kPageSize; |
| 4487 | void* free_start = startOfNextMemoryChunk_; |
| 4488 | void* free_end = start; |
| 4489 | const size_t free_len = |
| 4490 | reinterpret_cast<uintptr_t>(free_end) - reinterpret_cast<uintptr_t>(free_start); |
| 4491 | if (!IsNative() || free_len < kMaxFreeLen) { |
| 4492 | AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), free_start, free_len, IsNative()); |
| 4493 | flush = false; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4494 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4495 | } |
| 4496 | if (flush) { |
| 4497 | startOfNextMemoryChunk_ = nullptr; |
| 4498 | Flush(); |
| 4499 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4500 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4501 | return true; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4502 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4503 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4504 | void HeapChunkNativeCallback(void* start, void* /*end*/, size_t used_bytes) |
| 4505 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4506 | if (ProcessRecord(start, used_bytes)) { |
| 4507 | uint8_t state = ExamineNativeObject(start); |
| 4508 | AppendChunk(state, start, used_bytes + chunk_overhead_, true /*is_native*/); |
| 4509 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4510 | } |
| 4511 | } |
| 4512 | |
| 4513 | void HeapChunkJavaCallback(void* start, void* /*end*/, size_t used_bytes) |
| 4514 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 4515 | if (ProcessRecord(start, used_bytes)) { |
| 4516 | // Determine the type of this chunk. |
| 4517 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 4518 | // If it's the same, we should combine them. |
| 4519 | uint8_t state = ExamineJavaObject(reinterpret_cast<mirror::Object*>(start)); |
| 4520 | AppendChunk(state, start, used_bytes + chunk_overhead_, false /*is_native*/); |
| 4521 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4522 | } |
| 4523 | } |
| 4524 | |
| 4525 | void AppendChunk(uint8_t state, void* ptr, size_t length, bool is_native) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4526 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4527 | // Make sure there's enough room left in the buffer. |
| 4528 | // We need to use two bytes for every fractional 256 allocation units used by the chunk plus |
| 4529 | // 17 bytes for any header. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4530 | const size_t needed = ((RoundUp(length / ALLOCATION_UNIT_SIZE, 256) / 256) * 2) + 17; |
| 4531 | size_t byte_left = &buf_.back() - p_; |
| 4532 | if (byte_left < needed) { |
| 4533 | if (is_native) { |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4534 | // Cannot trigger memory allocation while walking native heap. |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4535 | return; |
| 4536 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4537 | Flush(); |
| 4538 | } |
| 4539 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4540 | byte_left = &buf_.back() - p_; |
| 4541 | if (byte_left < needed) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4542 | LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", " |
| 4543 | << needed << " bytes)"; |
| 4544 | return; |
| 4545 | } |
| 4546 | EnsureHeader(ptr); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4547 | // Write out the chunk description. |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4548 | length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units. |
| 4549 | totalAllocationUnits_ += length; |
| 4550 | while (length > 256) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4551 | *p_++ = state | HPSG_PARTIAL; |
| 4552 | *p_++ = 255; // length - 1 |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4553 | length -= 256; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4554 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4555 | *p_++ = state; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4556 | *p_++ = length - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4557 | } |
| 4558 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4559 | uint8_t ExamineNativeObject(const void* p) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4560 | return p == nullptr ? HPSG_STATE(SOLIDITY_FREE, 0) : HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4561 | } |
| 4562 | |
| 4563 | uint8_t ExamineJavaObject(mirror::Object* o) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4564 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4565 | if (o == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4566 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 4567 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4568 | // It's an allocated chunk. Figure out what it is. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4569 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 4570 | if (!heap->IsLiveObjectLocked(o)) { |
| 4571 | LOG(ERROR) << "Invalid object in managed heap: " << o; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4572 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4573 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4574 | mirror::Class* c = o->GetClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4575 | if (c == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4576 | // The object was probably just created but hasn't been initialized yet. |
| 4577 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4578 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4579 | if (!heap->IsValidObjectAddress(c)) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4580 | LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4581 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4582 | } |
Mathieu Chartier | f26e1b3 | 2015-01-29 10:47:10 -0800 | [diff] [blame] | 4583 | if (c->GetClass() == nullptr) { |
| 4584 | LOG(ERROR) << "Null class of class " << c << " for object " << o; |
| 4585 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4586 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4587 | if (c->IsClassClass()) { |
| 4588 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 4589 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4590 | if (c->IsArrayClass()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4591 | switch (c->GetComponentSize()) { |
| 4592 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 4593 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 4594 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 4595 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 4596 | } |
| 4597 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4598 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4599 | } |
| 4600 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4601 | std::vector<uint8_t> buf_; |
| 4602 | uint8_t* p_; |
| 4603 | uint8_t* pieceLenField_; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4604 | void* startOfNextMemoryChunk_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4605 | size_t totalAllocationUnits_; |
| 4606 | uint32_t type_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4607 | bool needHeader_; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4608 | size_t chunk_overhead_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4609 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4610 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 4611 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4612 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4613 | static void BumpPointerSpaceCallback(mirror::Object* obj, void* arg) |
| 4614 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 4615 | const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4616 | HeapChunkContext::HeapChunkJavaCallback( |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4617 | obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, arg); |
| 4618 | } |
| 4619 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4620 | void Dbg::DdmSendHeapSegments(bool native) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4621 | Dbg::HpsgWhen when = native ? gDdmNhsgWhen : gDdmHpsgWhen; |
| 4622 | Dbg::HpsgWhat what = native ? gDdmNhsgWhat : gDdmHpsgWhat; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4623 | if (when == HPSG_WHEN_NEVER) { |
| 4624 | return; |
| 4625 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4626 | // Figure out what kind of chunks we'll be sending. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4627 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) |
| 4628 | << static_cast<int>(what); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4629 | |
| 4630 | // First, send a heap start chunk. |
| 4631 | uint8_t heap_id[4]; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4632 | JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap). |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4633 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4634 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4635 | Locks::mutator_lock_->AssertSharedHeld(self); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4636 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4637 | // Send a series of heap segment chunks. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4638 | HeapChunkContext context(what == HPSG_WHAT_MERGED_OBJECTS, native); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4639 | if (native) { |
Ian Rogers | 872dd82 | 2014-10-30 11:19:14 -0700 | [diff] [blame] | 4640 | #if defined(HAVE_ANDROID_OS) && defined(USE_DLMALLOC) |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4641 | dlmalloc_inspect_all(HeapChunkContext::HeapChunkNativeCallback, &context); |
| 4642 | HeapChunkContext::HeapChunkNativeCallback(nullptr, nullptr, 0, &context); // Indicate end of a space. |
Christopher Ferris | c4ddc04 | 2014-05-13 14:47:50 -0700 | [diff] [blame] | 4643 | #else |
| 4644 | UNIMPLEMENTED(WARNING) << "Native heap inspection is only supported with dlmalloc"; |
| 4645 | #endif |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4646 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4647 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4648 | for (const auto& space : heap->GetContinuousSpaces()) { |
| 4649 | if (space->IsDlMallocSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4650 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4651 | // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an |
| 4652 | // allocation then the first sizeof(size_t) may belong to it. |
| 4653 | context.SetChunkOverhead(sizeof(size_t)); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4654 | space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4655 | } else if (space->IsRosAllocSpace()) { |
| 4656 | context.SetChunkOverhead(0); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4657 | // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since |
| 4658 | // RosAlloc's internal logic doesn't know to release and reacquire the heap bitmap lock. |
| 4659 | self->TransitionFromRunnableToSuspended(kSuspended); |
| 4660 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 4661 | tl->SuspendAll(__FUNCTION__); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4662 | { |
| 4663 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4664 | space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4665 | } |
| 4666 | tl->ResumeAll(); |
| 4667 | self->TransitionFromSuspendedToRunnable(); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4668 | } else if (space->IsBumpPointerSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4669 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4670 | context.SetChunkOverhead(0); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4671 | space->AsBumpPointerSpace()->Walk(BumpPointerSpaceCallback, &context); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4672 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4673 | } else if (space->IsRegionSpace()) { |
| 4674 | heap->IncrementDisableMovingGC(self); |
| 4675 | self->TransitionFromRunnableToSuspended(kSuspended); |
| 4676 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 4677 | tl->SuspendAll(__FUNCTION__); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4678 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 4679 | context.SetChunkOverhead(0); |
| 4680 | space->AsRegionSpace()->Walk(BumpPointerSpaceCallback, &context); |
| 4681 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
| 4682 | tl->ResumeAll(); |
| 4683 | self->TransitionFromSuspendedToRunnable(); |
| 4684 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4685 | } else { |
| 4686 | UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4687 | } |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4688 | context.ResetStartOfNextChunk(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4689 | } |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4690 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 4691 | // Walk the large objects, these are not in the AllocSpace. |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4692 | context.SetChunkOverhead(0); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4693 | heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4694 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4695 | |
| 4696 | // Finally, send a heap end chunk. |
| 4697 | Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4698 | } |
| 4699 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4700 | void Dbg::SetAllocTrackingEnabled(bool enable) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4701 | gc::AllocRecordObjectMap::SetAllocTrackingEnabled(enable); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | void Dbg::DumpRecentAllocations() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4705 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4706 | MutexLock mu(soa.Self(), *Locks::alloc_tracker_lock_); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4707 | if (!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4708 | LOG(INFO) << "Not recording tracked allocations"; |
| 4709 | return; |
| 4710 | } |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4711 | gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords(); |
| 4712 | CHECK(records != nullptr); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4713 | |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 4714 | const uint16_t capped_count = CappedAllocRecordCount(records->GetRecentAllocationSize()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4715 | uint16_t count = capped_count; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4716 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4717 | LOG(INFO) << "Tracked allocations, (count=" << count << ")"; |
| 4718 | for (auto it = records->RBegin(), end = records->REnd(); |
| 4719 | count > 0 && it != end; count--, it++) { |
| 4720 | const gc::AllocRecord* record = it->second; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4721 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4722 | LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->GetTid(), record->ByteCount()) |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 4723 | << PrettyClass(record->GetClass()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4724 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4725 | for (size_t stack_frame = 0, depth = record->GetDepth(); stack_frame < depth; ++stack_frame) { |
| 4726 | const gc::AllocRecordStackTraceElement& stack_element = record->StackElement(stack_frame); |
| 4727 | ArtMethod* m = stack_element.GetMethod(); |
| 4728 | LOG(INFO) << " " << PrettyMethod(m) << " line " << stack_element.ComputeLineNumber(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4729 | } |
| 4730 | |
| 4731 | // pause periodically to help logcat catch up |
| 4732 | if ((count % 5) == 0) { |
| 4733 | usleep(40000); |
| 4734 | } |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4735 | } |
| 4736 | } |
| 4737 | |
| 4738 | class StringTable { |
| 4739 | public: |
| 4740 | StringTable() { |
| 4741 | } |
| 4742 | |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 4743 | void Add(const std::string& str) { |
| 4744 | table_.insert(str); |
| 4745 | } |
| 4746 | |
| 4747 | void Add(const char* str) { |
| 4748 | table_.insert(str); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4749 | } |
| 4750 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4751 | size_t IndexOf(const char* s) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4752 | auto it = table_.find(s); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4753 | if (it == table_.end()) { |
| 4754 | LOG(FATAL) << "IndexOf(\"" << s << "\") failed"; |
| 4755 | } |
| 4756 | return std::distance(table_.begin(), it); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4757 | } |
| 4758 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4759 | size_t Size() const { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4760 | return table_.size(); |
| 4761 | } |
| 4762 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4763 | void WriteTo(std::vector<uint8_t>& bytes) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4764 | for (const std::string& str : table_) { |
| 4765 | const char* s = str.c_str(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4766 | size_t s_len = CountModifiedUtf8Chars(s); |
Christopher Ferris | 8a35405 | 2015-04-24 17:23:53 -0700 | [diff] [blame] | 4767 | std::unique_ptr<uint16_t[]> s_utf16(new uint16_t[s_len]); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4768 | ConvertModifiedUtf8ToUtf16(s_utf16.get(), s); |
| 4769 | JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4770 | } |
| 4771 | } |
| 4772 | |
| 4773 | private: |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4774 | std::set<std::string> table_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4775 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 4776 | }; |
| 4777 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4778 | static const char* GetMethodSourceFile(ArtMethod* method) |
Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 4779 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4780 | DCHECK(method != nullptr); |
| 4781 | const char* source_file = method->GetDeclaringClassSourceFile(); |
Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 4782 | return (source_file != nullptr) ? source_file : ""; |
| 4783 | } |
| 4784 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4785 | /* |
| 4786 | * The data we send to DDMS contains everything we have recorded. |
| 4787 | * |
| 4788 | * Message header (all values big-endian): |
| 4789 | * (1b) message header len (to allow future expansion); includes itself |
| 4790 | * (1b) entry header len |
| 4791 | * (1b) stack frame len |
| 4792 | * (2b) number of entries |
| 4793 | * (4b) offset to string table from start of message |
| 4794 | * (2b) number of class name strings |
| 4795 | * (2b) number of method name strings |
| 4796 | * (2b) number of source file name strings |
| 4797 | * For each entry: |
| 4798 | * (4b) total allocation size |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 4799 | * (2b) thread id |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4800 | * (2b) allocated object's class name index |
| 4801 | * (1b) stack depth |
| 4802 | * For each stack frame: |
| 4803 | * (2b) method's class name |
| 4804 | * (2b) method name |
| 4805 | * (2b) method source file |
| 4806 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 4807 | * (xb) class name strings |
| 4808 | * (xb) method name strings |
| 4809 | * (xb) source file strings |
| 4810 | * |
| 4811 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 4812 | * followed by UTF-16 data. |
| 4813 | * |
| 4814 | * 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] | 4815 | * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4816 | * each table, but in practice there should be far fewer. |
| 4817 | * |
| 4818 | * The chief reason for using a string table here is to keep the size of |
| 4819 | * the DDMS message to a minimum. This is partly to make the protocol |
| 4820 | * efficient, but also because we have to form the whole thing up all at |
| 4821 | * once in a memory buffer. |
| 4822 | * |
| 4823 | * We use separate string tables for class names, method names, and source |
| 4824 | * files to keep the indexes small. There will generally be no overlap |
| 4825 | * between the contents of these tables. |
| 4826 | */ |
| 4827 | jbyteArray Dbg::GetRecentAllocations() { |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 4828 | if ((false)) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4829 | DumpRecentAllocations(); |
| 4830 | } |
| 4831 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4832 | Thread* self = Thread::Current(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4833 | std::vector<uint8_t> bytes; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4834 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4835 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4836 | gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords(); |
| 4837 | // In case this method is called when allocation tracker is disabled, |
| 4838 | // we should still send some data back. |
| 4839 | gc::AllocRecordObjectMap dummy; |
| 4840 | if (records == nullptr) { |
| 4841 | CHECK(!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()); |
| 4842 | records = &dummy; |
| 4843 | } |
| 4844 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4845 | // |
| 4846 | // Part 1: generate string tables. |
| 4847 | // |
| 4848 | StringTable class_names; |
| 4849 | StringTable method_names; |
| 4850 | StringTable filenames; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4851 | |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 4852 | const uint16_t capped_count = CappedAllocRecordCount(records->GetRecentAllocationSize()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4853 | uint16_t count = capped_count; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4854 | for (auto it = records->RBegin(), end = records->REnd(); |
| 4855 | count > 0 && it != end; count--, it++) { |
| 4856 | const gc::AllocRecord* record = it->second; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4857 | std::string temp; |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 4858 | class_names.Add(record->GetClass()->GetDescriptor(&temp)); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4859 | for (size_t i = 0, depth = record->GetDepth(); i < depth; i++) { |
| 4860 | ArtMethod* m = record->StackElement(i).GetMethod(); |
| 4861 | class_names.Add(m->GetDeclaringClassDescriptor()); |
| 4862 | method_names.Add(m->GetName()); |
| 4863 | filenames.Add(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4864 | } |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4865 | } |
| 4866 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4867 | LOG(INFO) << "recent allocation records: " << capped_count; |
| 4868 | LOG(INFO) << "allocation records all objects: " << records->Size(); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4869 | |
| 4870 | // |
| 4871 | // Part 2: Generate the output and store it in the buffer. |
| 4872 | // |
| 4873 | |
| 4874 | // (1b) message header len (to allow future expansion); includes itself |
| 4875 | // (1b) entry header len |
| 4876 | // (1b) stack frame len |
| 4877 | const int kMessageHeaderLen = 15; |
| 4878 | const int kEntryHeaderLen = 9; |
| 4879 | const int kStackFrameLen = 8; |
| 4880 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 4881 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 4882 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 4883 | |
| 4884 | // (2b) number of entries |
| 4885 | // (4b) offset to string table from start of message |
| 4886 | // (2b) number of class name strings |
| 4887 | // (2b) number of method name strings |
| 4888 | // (2b) number of source file name strings |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4889 | JDWP::Append2BE(bytes, capped_count); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4890 | size_t string_table_offset = bytes.size(); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4891 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4892 | JDWP::Append2BE(bytes, class_names.Size()); |
| 4893 | JDWP::Append2BE(bytes, method_names.Size()); |
| 4894 | JDWP::Append2BE(bytes, filenames.Size()); |
| 4895 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4896 | std::string temp; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4897 | count = capped_count; |
| 4898 | // The last "count" number of allocation records in "records" are the most recent "count" number |
| 4899 | // of allocations. Reverse iterate to get them. The most recent allocation is sent first. |
| 4900 | for (auto it = records->RBegin(), end = records->REnd(); |
| 4901 | count > 0 && it != end; count--, it++) { |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4902 | // For each entry: |
| 4903 | // (4b) total allocation size |
| 4904 | // (2b) thread id |
| 4905 | // (2b) allocated object's class name index |
| 4906 | // (1b) stack depth |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4907 | const gc::AllocRecord* record = it->second; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4908 | size_t stack_depth = record->GetDepth(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4909 | size_t allocated_object_class_name_index = |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 4910 | class_names.IndexOf(record->GetClass()->GetDescriptor(&temp)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4911 | JDWP::Append4BE(bytes, record->ByteCount()); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4912 | JDWP::Append2BE(bytes, static_cast<uint16_t>(record->GetTid())); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4913 | JDWP::Append2BE(bytes, allocated_object_class_name_index); |
| 4914 | JDWP::Append1BE(bytes, stack_depth); |
| 4915 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4916 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 4917 | // For each stack frame: |
| 4918 | // (2b) method's class name |
| 4919 | // (2b) method name |
| 4920 | // (2b) method source file |
| 4921 | // (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] | 4922 | ArtMethod* m = record->StackElement(stack_frame).GetMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4923 | size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor()); |
| 4924 | size_t method_name_index = method_names.IndexOf(m->GetName()); |
| 4925 | size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4926 | JDWP::Append2BE(bytes, class_name_index); |
| 4927 | JDWP::Append2BE(bytes, method_name_index); |
| 4928 | JDWP::Append2BE(bytes, file_name_index); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4929 | JDWP::Append2BE(bytes, record->StackElement(stack_frame).ComputeLineNumber()); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4930 | } |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | // (xb) class name strings |
| 4934 | // (xb) method name strings |
| 4935 | // (xb) source file strings |
| 4936 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 4937 | class_names.WriteTo(bytes); |
| 4938 | method_names.WriteTo(bytes); |
| 4939 | filenames.WriteTo(bytes); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4940 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4941 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4942 | jbyteArray result = env->NewByteArray(bytes.size()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4943 | if (result != nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4944 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 4945 | } |
| 4946 | return result; |
| 4947 | } |
| 4948 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4949 | ArtMethod* DeoptimizationRequest::Method() const { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 4950 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 4951 | return soa.DecodeMethod(method_); |
| 4952 | } |
| 4953 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4954 | void DeoptimizationRequest::SetMethod(ArtMethod* m) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 4955 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 4956 | method_ = soa.EncodeMethod(m); |
| 4957 | } |
| 4958 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4959 | } // namespace art |