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" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 36 | #include "jdwp/object_registry.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "mirror/class.h" |
| 38 | #include "mirror/class-inl.h" |
| 39 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | #include "mirror/object-inl.h" |
| 41 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 42 | #include "mirror/string-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | #include "mirror/throwable.h" |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 44 | #include "quick/inline_method_analyser.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 45 | #include "reflection.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 46 | #include "safe_map.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 47 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 48 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 49 | #include "ScopedPrimitiveArray.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 50 | #include "handle_scope-inl.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 51 | #include "thread_list.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | #include "utf.h" |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 53 | #include "verifier/method_verifier-inl.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 54 | #include "well_known_classes.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 55 | |
Brian Carlstrom | 3d92d52 | 2013-07-12 09:03:08 -0700 | [diff] [blame] | 56 | #ifdef HAVE_ANDROID_OS |
| 57 | #include "cutils/properties.h" |
| 58 | #endif |
| 59 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 60 | namespace art { |
| 61 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 62 | // The key identifying the debugger to update instrumentation. |
| 63 | static constexpr const char* kDbgInstrumentationKey = "Debugger"; |
| 64 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 65 | // 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] | 66 | static uint16_t CappedAllocRecordCount(size_t alloc_record_count) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 67 | size_t cap = 0xffff; |
| 68 | #ifdef HAVE_ANDROID_OS |
| 69 | // Check whether there's a system property overriding the number of recent records. |
| 70 | const char* propertyName = "dalvik.vm.recentAllocMax"; |
| 71 | char recentAllocMaxString[PROPERTY_VALUE_MAX]; |
| 72 | if (property_get(propertyName, recentAllocMaxString, "") > 0) { |
| 73 | char* end; |
| 74 | size_t value = strtoul(recentAllocMaxString, &end, 10); |
| 75 | if (*end != '\0') { |
| 76 | LOG(ERROR) << "Ignoring " << propertyName << " '" << recentAllocMaxString |
| 77 | << "' --- invalid"; |
| 78 | } else { |
| 79 | cap = value; |
| 80 | } |
| 81 | } |
| 82 | #endif |
| 83 | if (alloc_record_count > cap) { |
| 84 | return cap; |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 85 | } |
| 86 | return alloc_record_count; |
| 87 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 88 | |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 89 | class Breakpoint { |
| 90 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 91 | Breakpoint(ArtMethod* method, uint32_t dex_pc, |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 92 | DeoptimizationRequest::Kind deoptimization_kind) |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 93 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 94 | : method_(nullptr), dex_pc_(dex_pc), deoptimization_kind_(deoptimization_kind) { |
| 95 | CHECK(deoptimization_kind_ == DeoptimizationRequest::kNothing || |
| 96 | deoptimization_kind_ == DeoptimizationRequest::kSelectiveDeoptimization || |
| 97 | deoptimization_kind_ == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 98 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 99 | method_ = soa.EncodeMethod(method); |
| 100 | } |
| 101 | |
| 102 | Breakpoint(const Breakpoint& other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 103 | : method_(nullptr), dex_pc_(other.dex_pc_), |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 104 | deoptimization_kind_(other.deoptimization_kind_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 105 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 106 | method_ = soa.EncodeMethod(other.Method()); |
| 107 | } |
| 108 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 109 | ArtMethod* Method() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 110 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 111 | return soa.DecodeMethod(method_); |
| 112 | } |
| 113 | |
| 114 | uint32_t DexPc() const { |
| 115 | return dex_pc_; |
| 116 | } |
| 117 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 118 | DeoptimizationRequest::Kind GetDeoptimizationKind() const { |
| 119 | return deoptimization_kind_; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | private: |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 123 | // The location of this breakpoint. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 124 | jmethodID method_; |
| 125 | uint32_t dex_pc_; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 126 | |
| 127 | // Indicates whether breakpoint needs full deoptimization or selective deoptimization. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 128 | DeoptimizationRequest::Kind deoptimization_kind_; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 131 | static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 132 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 133 | os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.Method()).c_str(), rhs.DexPc()); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 134 | return os; |
| 135 | } |
| 136 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 137 | class DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 138 | public: |
| 139 | DebugInstrumentationListener() {} |
| 140 | virtual ~DebugInstrumentationListener() {} |
| 141 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 142 | void MethodEntered(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 143 | uint32_t dex_pc) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 144 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 145 | if (method->IsNative()) { |
| 146 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 147 | return; |
| 148 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 149 | if (IsListeningToDexPcMoved()) { |
| 150 | // We also listen to kDexPcMoved instrumentation event so we know the DexPcMoved method is |
| 151 | // going to be called right after us. To avoid sending JDWP events twice for this location, |
| 152 | // we report the event in DexPcMoved. However, we must remind this is method entry so we |
| 153 | // send the METHOD_ENTRY event. And we can also group it with other events for this location |
| 154 | // like BREAKPOINT or SINGLE_STEP (or even METHOD_EXIT if this is a RETURN instruction). |
| 155 | thread->SetDebugMethodEntry(); |
| 156 | } else if (IsListeningToMethodExit() && IsReturn(method, dex_pc)) { |
| 157 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 158 | // RETURN so we know the MethodExited method is going to be called right after us. To avoid |
| 159 | // sending JDWP events twice for this location, we report the event(s) in MethodExited. |
| 160 | // However, we must remind this is method entry so we send the METHOD_ENTRY event. And we can |
| 161 | // also group it with other events for this location like BREAKPOINT or SINGLE_STEP. |
| 162 | thread->SetDebugMethodEntry(); |
| 163 | } else { |
| 164 | Dbg::UpdateDebugger(thread, this_object, method, 0, Dbg::kMethodEntry, nullptr); |
| 165 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 168 | void MethodExited(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 169 | uint32_t dex_pc, const JValue& return_value) |
| 170 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 171 | if (method->IsNative()) { |
| 172 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 173 | return; |
| 174 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 175 | uint32_t events = Dbg::kMethodExit; |
| 176 | if (thread->IsDebugMethodEntry()) { |
| 177 | // It is also the method entry. |
| 178 | DCHECK(IsReturn(method, dex_pc)); |
| 179 | events |= Dbg::kMethodEntry; |
| 180 | thread->ClearDebugMethodEntry(); |
| 181 | } |
| 182 | Dbg::UpdateDebugger(thread, this_object, method, dex_pc, events, &return_value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 185 | void MethodUnwind(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 186 | ArtMethod* method, uint32_t dex_pc) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 187 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 188 | // We're not recorded to listen to this kind of event, so complain. |
| 189 | LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method) |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 190 | << " " << dex_pc; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 193 | void DexPcMoved(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 194 | uint32_t new_dex_pc) |
| 195 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 196 | if (IsListeningToMethodExit() && IsReturn(method, new_dex_pc)) { |
| 197 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 198 | // RETURN so we know the MethodExited method is going to be called right after us. Like in |
| 199 | // MethodEntered, we delegate event reporting to MethodExited. |
| 200 | // Besides, if this RETURN instruction is the only one in the method, we can send multiple |
| 201 | // JDWP events in the same packet: METHOD_ENTRY, METHOD_EXIT, BREAKPOINT and/or SINGLE_STEP. |
| 202 | // Therefore, we must not clear the debug method entry flag here. |
| 203 | } else { |
| 204 | uint32_t events = 0; |
| 205 | if (thread->IsDebugMethodEntry()) { |
| 206 | // It is also the method entry. |
| 207 | events = Dbg::kMethodEntry; |
| 208 | thread->ClearDebugMethodEntry(); |
| 209 | } |
| 210 | Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc, events, nullptr); |
| 211 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 214 | void FieldRead(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 215 | ArtMethod* method, uint32_t dex_pc, ArtField* field) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 216 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 217 | Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 218 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 219 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 220 | void FieldWritten(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 221 | ArtMethod* method, uint32_t dex_pc, ArtField* field, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 222 | const JValue& field_value) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 223 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 224 | Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value); |
| 225 | } |
| 226 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 227 | void ExceptionCaught(Thread* thread ATTRIBUTE_UNUSED, mirror::Throwable* exception_object) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 228 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 229 | Dbg::PostException(exception_object); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 232 | // 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] | 233 | void BackwardBranch(Thread* /*thread*/, ArtMethod* method, int32_t dex_pc_offset) |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 234 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 235 | LOG(ERROR) << "Unexpected backward branch event in debugger " << PrettyMethod(method) |
| 236 | << " " << dex_pc_offset; |
| 237 | } |
| 238 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 239 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 240 | static bool IsReturn(ArtMethod* method, uint32_t dex_pc) |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 241 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 242 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
| 243 | const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]); |
| 244 | return instruction->IsReturn(); |
| 245 | } |
| 246 | |
| 247 | static bool IsListeningToDexPcMoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 248 | return IsListeningTo(instrumentation::Instrumentation::kDexPcMoved); |
| 249 | } |
| 250 | |
| 251 | static bool IsListeningToMethodExit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 252 | return IsListeningTo(instrumentation::Instrumentation::kMethodExited); |
| 253 | } |
| 254 | |
| 255 | static bool IsListeningTo(instrumentation::Instrumentation::InstrumentationEvent event) |
| 256 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 257 | return (Dbg::GetInstrumentationEvents() & event) != 0; |
| 258 | } |
| 259 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 260 | DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 261 | } gDebugInstrumentationListener; |
| 262 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 263 | // JDWP is allowed unless the Zygote forbids it. |
| 264 | static bool gJdwpAllowed = true; |
| 265 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 266 | // Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line? |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 267 | static bool gJdwpConfigured = false; |
| 268 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 269 | // JDWP options for debugging. Only valid if IsJdwpConfigured() is true. |
| 270 | static JDWP::JdwpOptions gJdwpOptions; |
| 271 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 272 | // Runtime JDWP state. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 273 | static JDWP::JdwpState* gJdwpState = nullptr; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 274 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 275 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 276 | static bool gDdmThreadNotification = false; |
| 277 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 278 | // DDMS GC-related settings. |
| 279 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 280 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 281 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 282 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 283 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 284 | |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 285 | bool Dbg::gDebuggerActive = false; |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 286 | bool Dbg::gDisposed = false; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 287 | ObjectRegistry* Dbg::gRegistry = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 288 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 289 | // Deoptimization support. |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 290 | std::vector<DeoptimizationRequest> Dbg::deoptimization_requests_; |
| 291 | size_t Dbg::full_deoptimization_event_count_ = 0; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 292 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 293 | // Instrumentation event reference counters. |
| 294 | size_t Dbg::dex_pc_change_event_ref_count_ = 0; |
| 295 | size_t Dbg::method_enter_event_ref_count_ = 0; |
| 296 | size_t Dbg::method_exit_event_ref_count_ = 0; |
| 297 | size_t Dbg::field_read_event_ref_count_ = 0; |
| 298 | size_t Dbg::field_write_event_ref_count_ = 0; |
| 299 | size_t Dbg::exception_catch_event_ref_count_ = 0; |
| 300 | uint32_t Dbg::instrumentation_events_ = 0; |
| 301 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 302 | // Breakpoints. |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 303 | static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 304 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 305 | void DebugInvokeReq::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 306 | receiver.VisitRootIfNonNull(visitor, root_info); // null for static method call. |
| 307 | klass.VisitRoot(visitor, root_info); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 310 | void SingleStepControl::AddDexPc(uint32_t dex_pc) { |
| 311 | dex_pcs_.insert(dex_pc); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 314 | bool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const { |
| 315 | return dex_pcs_.find(dex_pc) == dex_pcs_.end(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 316 | } |
| 317 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 318 | static bool IsBreakpoint(const ArtMethod* m, uint32_t dex_pc) |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 319 | LOCKS_EXCLUDED(Locks::breakpoint_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 320 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 321 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 322 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 323 | if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].Method() == m) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 324 | VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i]; |
| 325 | return true; |
| 326 | } |
| 327 | } |
| 328 | return false; |
| 329 | } |
| 330 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 331 | static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) |
| 332 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) { |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 333 | MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); |
| 334 | // A thread may be suspended for GC; in this code, we really want to know whether |
| 335 | // there's a debugger suspension active. |
| 336 | return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0; |
| 337 | } |
| 338 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 339 | static mirror::Array* DecodeNonNullArray(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 340 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 341 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 342 | if (o == nullptr) { |
| 343 | *error = JDWP::ERR_INVALID_OBJECT; |
| 344 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 345 | } |
| 346 | if (!o->IsArrayInstance()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 347 | *error = JDWP::ERR_INVALID_ARRAY; |
| 348 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 349 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 350 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 351 | return o->AsArray(); |
| 352 | } |
| 353 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 354 | static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 355 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 356 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 357 | if (o == nullptr) { |
| 358 | *error = JDWP::ERR_INVALID_OBJECT; |
| 359 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 360 | } |
| 361 | if (!o->IsClass()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 362 | *error = JDWP::ERR_INVALID_CLASS; |
| 363 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 364 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 365 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 366 | return o->AsClass(); |
| 367 | } |
| 368 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 369 | static Thread* DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, |
| 370 | JDWP::JdwpError* error) |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 371 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 372 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::thread_suspend_count_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 373 | mirror::Object* thread_peer = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 374 | if (thread_peer == nullptr) { |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 375 | // This isn't even an object. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 376 | *error = JDWP::ERR_INVALID_OBJECT; |
| 377 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 378 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 379 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 380 | 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] | 381 | if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) { |
| 382 | // This isn't a thread. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 383 | *error = JDWP::ERR_INVALID_THREAD; |
| 384 | return nullptr; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 387 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 388 | Thread* thread = Thread::FromManagedThread(soa, thread_peer); |
| 389 | // If thread is null then this a java.lang.Thread without a Thread*. Must be a un-started or a |
| 390 | // zombie. |
| 391 | *error = (thread == nullptr) ? JDWP::ERR_THREAD_NOT_ALIVE : JDWP::ERR_NONE; |
| 392 | return thread; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 395 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 396 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 397 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 398 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 399 | } |
| 400 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 401 | static JDWP::JdwpTag BasicTagFromClass(mirror::Class* klass) |
| 402 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 403 | std::string temp; |
| 404 | const char* descriptor = klass->GetDescriptor(&temp); |
| 405 | return BasicTagFromDescriptor(descriptor); |
| 406 | } |
| 407 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 408 | static JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 409 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 410 | CHECK(c != nullptr); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 411 | if (c->IsArrayClass()) { |
| 412 | return JDWP::JT_ARRAY; |
| 413 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 414 | if (c->IsStringClass()) { |
| 415 | return JDWP::JT_STRING; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 416 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 417 | if (c->IsClassClass()) { |
| 418 | return JDWP::JT_CLASS_OBJECT; |
| 419 | } |
| 420 | { |
| 421 | mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
| 422 | if (thread_class->IsAssignableFrom(c)) { |
| 423 | return JDWP::JT_THREAD; |
| 424 | } |
| 425 | } |
| 426 | { |
| 427 | mirror::Class* thread_group_class = |
| 428 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup); |
| 429 | if (thread_group_class->IsAssignableFrom(c)) { |
| 430 | return JDWP::JT_THREAD_GROUP; |
| 431 | } |
| 432 | } |
| 433 | { |
| 434 | mirror::Class* class_loader_class = |
| 435 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader); |
| 436 | if (class_loader_class->IsAssignableFrom(c)) { |
| 437 | return JDWP::JT_CLASS_LOADER; |
| 438 | } |
| 439 | } |
| 440 | return JDWP::JT_OBJECT; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Objects declared to hold Object might actually hold a more specific |
| 445 | * type. The debugger may take a special interest in these (e.g. it |
| 446 | * wants to display the contents of Strings), so we want to return an |
| 447 | * appropriate tag. |
| 448 | * |
| 449 | * Null objects are tagged JT_OBJECT. |
| 450 | */ |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 451 | JDWP::JdwpTag Dbg::TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 452 | return (o == nullptr) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 456 | switch (tag) { |
| 457 | case JDWP::JT_BOOLEAN: |
| 458 | case JDWP::JT_BYTE: |
| 459 | case JDWP::JT_CHAR: |
| 460 | case JDWP::JT_FLOAT: |
| 461 | case JDWP::JT_DOUBLE: |
| 462 | case JDWP::JT_INT: |
| 463 | case JDWP::JT_LONG: |
| 464 | case JDWP::JT_SHORT: |
| 465 | case JDWP::JT_VOID: |
| 466 | return true; |
| 467 | default: |
| 468 | return false; |
| 469 | } |
| 470 | } |
| 471 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 472 | void Dbg::StartJdwp() { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 473 | if (!gJdwpAllowed || !IsJdwpConfigured()) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 474 | // No JDWP for you! |
| 475 | return; |
| 476 | } |
| 477 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 478 | CHECK(gRegistry == nullptr); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 479 | gRegistry = new ObjectRegistry; |
| 480 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 481 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 482 | // debugger, passively listen for a debugger, or block waiting for a |
| 483 | // debugger. |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 484 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 485 | if (gJdwpState == nullptr) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 486 | // We probably failed because some other process has the port already, which means that |
| 487 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 488 | // talking to that other process. |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 489 | LOG(FATAL) << "Debugger thread failed to initialize"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // If a debugger has already attached, send the "welcome" message. |
| 493 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 494 | if (gJdwpState->IsActive()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 495 | ScopedObjectAccess soa(Thread::Current()); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 496 | gJdwpState->PostVMStart(); |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 497 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 500 | void Dbg::StopJdwp() { |
Sebastien Hertz | c6345ef | 2014-08-18 19:26:39 +0200 | [diff] [blame] | 501 | // Post VM_DEATH event before the JDWP connection is closed (either by the JDWP thread or the |
| 502 | // destruction of gJdwpState). |
| 503 | if (gJdwpState != nullptr && gJdwpState->IsActive()) { |
| 504 | gJdwpState->PostVMDeath(); |
| 505 | } |
Sebastien Hertz | 0376e6b | 2014-02-06 18:12:59 +0100 | [diff] [blame] | 506 | // 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] | 507 | Dispose(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 508 | delete gJdwpState; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 509 | gJdwpState = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 510 | delete gRegistry; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 511 | gRegistry = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 514 | void Dbg::GcDidFinish() { |
| 515 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 516 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 517 | VLOG(jdwp) << "Sending heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 518 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 519 | } |
| 520 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 521 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 522 | VLOG(jdwp) << "Dumping heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 523 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 524 | } |
| 525 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 526 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 527 | VLOG(jdwp) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 528 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 532 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 533 | gJdwpAllowed = allowed; |
| 534 | } |
| 535 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 536 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 537 | return Thread::Current()->GetInvokeReq(); |
| 538 | } |
| 539 | |
| 540 | Thread* Dbg::GetDebugThread() { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 541 | return (gJdwpState != nullptr) ? gJdwpState->GetDebugThread() : nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | void Dbg::ClearWaitForEventThread() { |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 545 | gJdwpState->ReleaseJdwpTokenForEvent(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 549 | CHECK(!gDebuggerConnected); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 550 | VLOG(jdwp) << "JDWP has attached"; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 551 | gDebuggerConnected = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 552 | gDisposed = false; |
| 553 | } |
| 554 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 555 | bool Dbg::RequiresDeoptimization() { |
| 556 | // We don't need deoptimization if everything runs with interpreter after |
| 557 | // enabling -Xint mode. |
| 558 | return !Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly(); |
| 559 | } |
| 560 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 561 | void Dbg::GoActive() { |
| 562 | // Enable all debugging features, including scans for breakpoints. |
| 563 | // This is a no-op if we're already active. |
| 564 | // Only called from the JDWP handler thread. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 565 | if (IsDebuggerActive()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 566 | return; |
| 567 | } |
| 568 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 569 | { |
| 570 | // 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] | 571 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 572 | CHECK_EQ(gBreakpoints.size(), 0U); |
| 573 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 574 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 575 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 576 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 577 | CHECK_EQ(deoptimization_requests_.size(), 0U); |
| 578 | CHECK_EQ(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 579 | CHECK_EQ(dex_pc_change_event_ref_count_, 0U); |
| 580 | CHECK_EQ(method_enter_event_ref_count_, 0U); |
| 581 | CHECK_EQ(method_exit_event_ref_count_, 0U); |
| 582 | CHECK_EQ(field_read_event_ref_count_, 0U); |
| 583 | CHECK_EQ(field_write_event_ref_count_, 0U); |
| 584 | CHECK_EQ(exception_catch_event_ref_count_, 0U); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 587 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 588 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 589 | Thread* self = Thread::Current(); |
| 590 | ThreadState old_state = self->SetStateUnsafe(kRunnable); |
| 591 | CHECK_NE(old_state, kRunnable); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 592 | if (RequiresDeoptimization()) { |
| 593 | runtime->GetInstrumentation()->EnableDeoptimization(); |
| 594 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 595 | instrumentation_events_ = 0; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 596 | gDebuggerActive = true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 597 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 598 | runtime->GetThreadList()->ResumeAll(); |
| 599 | |
| 600 | LOG(INFO) << "Debugger is active"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 604 | CHECK(gDebuggerConnected); |
| 605 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 606 | LOG(INFO) << "Debugger is no longer active"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 607 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 608 | // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread |
| 609 | // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener |
| 610 | // and clear the object registry. |
| 611 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 612 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 613 | Thread* self = Thread::Current(); |
| 614 | ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 615 | |
| 616 | // Debugger may not be active at this point. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 617 | if (IsDebuggerActive()) { |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 618 | { |
| 619 | // Since we're going to disable deoptimization, we clear the deoptimization requests queue. |
| 620 | // This prevents us from having any pending deoptimization request when the debugger attaches |
| 621 | // to us again while no event has been requested yet. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 622 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 623 | deoptimization_requests_.clear(); |
| 624 | full_deoptimization_event_count_ = 0U; |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 625 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 626 | if (instrumentation_events_ != 0) { |
| 627 | runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener, |
| 628 | instrumentation_events_); |
| 629 | instrumentation_events_ = 0; |
| 630 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 631 | if (RequiresDeoptimization()) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 632 | runtime->GetInstrumentation()->DisableDeoptimization(kDbgInstrumentationKey); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 633 | } |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 634 | gDebuggerActive = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 635 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 636 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 637 | runtime->GetThreadList()->ResumeAll(); |
Sebastien Hertz | 55f6534 | 2015-01-13 22:48:34 +0100 | [diff] [blame] | 638 | |
| 639 | { |
| 640 | ScopedObjectAccess soa(self); |
| 641 | gRegistry->Clear(); |
| 642 | } |
| 643 | |
| 644 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 647 | void Dbg::ConfigureJdwp(const JDWP::JdwpOptions& jdwp_options) { |
| 648 | CHECK_NE(jdwp_options.transport, JDWP::kJdwpTransportUnknown); |
| 649 | gJdwpOptions = jdwp_options; |
| 650 | gJdwpConfigured = true; |
| 651 | } |
| 652 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 653 | bool Dbg::IsJdwpConfigured() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 654 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | int64_t Dbg::LastDebuggerActivity() { |
Elliott Hughes | ca95152 | 2011-12-05 12:01:32 -0800 | [diff] [blame] | 658 | return gJdwpState->LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 661 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 662 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 665 | std::string Dbg::GetClassName(JDWP::RefTypeId class_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 666 | JDWP::JdwpError error; |
| 667 | mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id, &error); |
| 668 | if (o == nullptr) { |
| 669 | if (error == JDWP::ERR_NONE) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 670 | return "null"; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 671 | } else { |
| 672 | return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id)); |
| 673 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 674 | } |
| 675 | if (!o->IsClass()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 676 | 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] | 677 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 678 | return GetClassName(o->AsClass()); |
| 679 | } |
| 680 | |
| 681 | std::string Dbg::GetClassName(mirror::Class* klass) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 682 | if (klass == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 683 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 684 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 685 | std::string temp; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 686 | return DescriptorToName(klass->GetDescriptor(&temp)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 689 | JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId* class_object_id) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 690 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 691 | mirror::Class* c = DecodeClass(id, &status); |
| 692 | if (c == nullptr) { |
| 693 | *class_object_id = 0; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 694 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 695 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 696 | *class_object_id = gRegistry->Add(c); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 697 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 698 | } |
| 699 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 700 | JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId* superclass_id) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 701 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 702 | mirror::Class* c = DecodeClass(id, &status); |
| 703 | if (c == nullptr) { |
| 704 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 705 | return status; |
| 706 | } |
| 707 | if (c->IsInterface()) { |
| 708 | // http://code.google.com/p/android/issues/detail?id=20856 |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 709 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 710 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 711 | *superclass_id = gRegistry->Add(c->GetSuperClass()); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 712 | } |
| 713 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 716 | JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 717 | JDWP::JdwpError error; |
| 718 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 719 | if (o == nullptr) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 720 | return JDWP::ERR_INVALID_OBJECT; |
| 721 | } |
| 722 | expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader())); |
| 723 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 726 | JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 727 | JDWP::JdwpError error; |
| 728 | mirror::Class* c = DecodeClass(id, &error); |
| 729 | if (c == nullptr) { |
| 730 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 731 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 732 | |
| 733 | uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask; |
| 734 | |
Yevgeny Rouban | de34eea | 2014-02-15 01:06:03 +0700 | [diff] [blame] | 735 | // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set, |
| 736 | // not interfaces. |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 737 | // 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] | 738 | if ((access_flags & kAccInterface) == 0) { |
| 739 | access_flags |= kAccSuper; |
| 740 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 741 | |
| 742 | expandBufAdd4BE(pReply, access_flags); |
| 743 | |
| 744 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 747 | JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) { |
| 748 | JDWP::JdwpError error; |
| 749 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 750 | if (o == nullptr) { |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 751 | return JDWP::ERR_INVALID_OBJECT; |
| 752 | } |
| 753 | |
| 754 | // Ensure all threads are suspended while we read objects' lock words. |
| 755 | Thread* self = Thread::Current(); |
Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 756 | CHECK_EQ(self->GetState(), kRunnable); |
| 757 | self->TransitionFromRunnableToSuspended(kSuspended); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 758 | Runtime::Current()->GetThreadList()->SuspendAll(__FUNCTION__); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 759 | |
| 760 | MonitorInfo monitor_info(o); |
| 761 | |
Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 762 | Runtime::Current()->GetThreadList()->ResumeAll(); |
| 763 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 764 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 765 | if (monitor_info.owner_ != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 766 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 767 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 768 | expandBufAddObjectId(reply, gRegistry->Add(nullptr)); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 769 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 770 | expandBufAdd4BE(reply, monitor_info.entry_count_); |
| 771 | expandBufAdd4BE(reply, monitor_info.waiters_.size()); |
| 772 | for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) { |
| 773 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 774 | } |
| 775 | return JDWP::ERR_NONE; |
| 776 | } |
| 777 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 778 | JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 779 | std::vector<JDWP::ObjectId>* monitors, |
| 780 | std::vector<uint32_t>* stack_depths) { |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 781 | struct OwnedMonitorVisitor : public StackVisitor { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 782 | OwnedMonitorVisitor(Thread* thread, Context* context, |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 783 | std::vector<JDWP::ObjectId>* monitor_vector, |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 784 | std::vector<uint32_t>* stack_depth_vector) |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 785 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 786 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 787 | current_stack_depth(0), |
| 788 | monitors(monitor_vector), |
| 789 | stack_depths(stack_depth_vector) {} |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 790 | |
| 791 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 792 | // annotalysis. |
| 793 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 794 | if (!GetMethod()->IsRuntimeMethod()) { |
| 795 | Monitor::VisitLocks(this, AppendOwnedMonitors, this); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 796 | ++current_stack_depth; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 797 | } |
| 798 | return true; |
| 799 | } |
| 800 | |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 801 | static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) |
| 802 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 803 | OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 804 | visitor->monitors->push_back(gRegistry->Add(owned_monitor)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 805 | visitor->stack_depths->push_back(visitor->current_stack_depth); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 808 | size_t current_stack_depth; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 809 | std::vector<JDWP::ObjectId>* const monitors; |
| 810 | std::vector<uint32_t>* const stack_depths; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 811 | }; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 812 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 813 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
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; |
| 818 | } |
| 819 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 820 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 821 | } |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 822 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 823 | OwnedMonitorVisitor visitor(thread, context.get(), monitors, stack_depths); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 824 | visitor.WalkStack(); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 825 | return JDWP::ERR_NONE; |
| 826 | } |
| 827 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 828 | JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 829 | JDWP::ObjectId* contended_monitor) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 830 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 831 | *contended_monitor = 0; |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 832 | JDWP::JdwpError error; |
| 833 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 834 | if (thread == nullptr) { |
| 835 | return error; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 836 | } |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 837 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 838 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 839 | } |
| 840 | mirror::Object* contended_monitor_obj = Monitor::GetContendedMonitor(thread); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 841 | // Add() requires the thread_list_lock_ not held to avoid the lock |
| 842 | // level violation. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 843 | *contended_monitor = gRegistry->Add(contended_monitor_obj); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 844 | return JDWP::ERR_NONE; |
| 845 | } |
| 846 | |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 847 | JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 848 | std::vector<uint64_t>* counts) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 849 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 850 | heap->CollectGarbage(false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 851 | std::vector<mirror::Class*> classes; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 852 | counts->clear(); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 853 | for (size_t i = 0; i < class_ids.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 854 | JDWP::JdwpError error; |
| 855 | mirror::Class* c = DecodeClass(class_ids[i], &error); |
| 856 | if (c == nullptr) { |
| 857 | return error; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 858 | } |
| 859 | classes.push_back(c); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 860 | counts->push_back(0); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 861 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 862 | heap->CountInstances(classes, false, &(*counts)[0]); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 863 | return JDWP::ERR_NONE; |
| 864 | } |
| 865 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 866 | JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, |
| 867 | std::vector<JDWP::ObjectId>* instances) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 868 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 869 | // We only want reachable instances, so do a GC. |
| 870 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 871 | JDWP::JdwpError error; |
| 872 | mirror::Class* c = DecodeClass(class_id, &error); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 873 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 874 | return error; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 875 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 876 | std::vector<mirror::Object*> raw_instances; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 877 | Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances); |
| 878 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 879 | instances->push_back(gRegistry->Add(raw_instances[i])); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 880 | } |
| 881 | return JDWP::ERR_NONE; |
| 882 | } |
| 883 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 884 | JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 885 | std::vector<JDWP::ObjectId>* referring_objects) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 886 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 887 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 888 | JDWP::JdwpError error; |
| 889 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 890 | if (o == nullptr) { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 891 | return JDWP::ERR_INVALID_OBJECT; |
| 892 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 893 | std::vector<mirror::Object*> raw_instances; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 894 | heap->GetReferringObjects(o, max_count, raw_instances); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 895 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 896 | referring_objects->push_back(gRegistry->Add(raw_instances[i])); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 897 | } |
| 898 | return JDWP::ERR_NONE; |
| 899 | } |
| 900 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 901 | JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id) { |
| 902 | JDWP::JdwpError error; |
| 903 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 904 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 905 | return JDWP::ERR_INVALID_OBJECT; |
| 906 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 907 | gRegistry->DisableCollection(object_id); |
| 908 | return JDWP::ERR_NONE; |
| 909 | } |
| 910 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 911 | JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id) { |
| 912 | JDWP::JdwpError error; |
| 913 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 914 | // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI |
| 915 | // also ignores these cases and never return an error. However it's not obvious why this command |
| 916 | // should behave differently from DisableCollection and IsCollected commands. So let's be more |
| 917 | // strict and return an error if this happens. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 918 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 919 | return JDWP::ERR_INVALID_OBJECT; |
| 920 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 921 | gRegistry->EnableCollection(object_id); |
| 922 | return JDWP::ERR_NONE; |
| 923 | } |
| 924 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 925 | JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool* is_collected) { |
| 926 | *is_collected = true; |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 927 | if (object_id == 0) { |
| 928 | // Null object id is invalid. |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 929 | return JDWP::ERR_INVALID_OBJECT; |
| 930 | } |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 931 | // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However |
| 932 | // the RI seems to ignore this and assume object has been collected. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 933 | JDWP::JdwpError error; |
| 934 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 935 | if (o != nullptr) { |
| 936 | *is_collected = gRegistry->IsCollected(object_id); |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 937 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 938 | return JDWP::ERR_NONE; |
| 939 | } |
| 940 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 941 | void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 942 | gRegistry->DisposeObject(object_id, reference_count); |
| 943 | } |
| 944 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 945 | JDWP::JdwpTypeTag Dbg::GetTypeTag(mirror::Class* klass) { |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 946 | DCHECK(klass != nullptr); |
| 947 | if (klass->IsArrayClass()) { |
| 948 | return JDWP::TT_ARRAY; |
| 949 | } else if (klass->IsInterface()) { |
| 950 | return JDWP::TT_INTERFACE; |
| 951 | } else { |
| 952 | return JDWP::TT_CLASS; |
| 953 | } |
| 954 | } |
| 955 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 956 | JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 957 | JDWP::JdwpError error; |
| 958 | mirror::Class* c = DecodeClass(class_id, &error); |
| 959 | if (c == nullptr) { |
| 960 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 961 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 962 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 963 | JDWP::JdwpTypeTag type_tag = GetTypeTag(c); |
| 964 | expandBufAdd1(pReply, type_tag); |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 965 | expandBufAddRefTypeId(pReply, class_id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 966 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 969 | void Dbg::GetClassList(std::vector<JDWP::RefTypeId>* classes) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 970 | // Get the complete list of reference classes (i.e. all classes except |
| 971 | // the primitive types). |
| 972 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 973 | struct ClassListCreator { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 974 | explicit ClassListCreator(std::vector<JDWP::RefTypeId>* classes_in) : classes(classes_in) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 975 | } |
| 976 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 977 | static bool Visit(mirror::Class* c, void* arg) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 978 | return reinterpret_cast<ClassListCreator*>(arg)->Visit(c); |
| 979 | } |
| 980 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 981 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 982 | // annotalysis. |
| 983 | bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 984 | if (!c->IsPrimitive()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 985 | classes->push_back(gRegistry->AddRefType(c)); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 986 | } |
| 987 | return true; |
| 988 | } |
| 989 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 990 | std::vector<JDWP::RefTypeId>* const classes; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 991 | }; |
| 992 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 993 | ClassListCreator clc(classes); |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 994 | Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(ClassListCreator::Visit, |
| 995 | &clc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 998 | JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, |
| 999 | uint32_t* pStatus, std::string* pDescriptor) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1000 | JDWP::JdwpError error; |
| 1001 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1002 | if (c == nullptr) { |
| 1003 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1006 | if (c->IsArrayClass()) { |
| 1007 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 1008 | *pTypeTag = JDWP::TT_ARRAY; |
| 1009 | } else { |
| 1010 | if (c->IsErroneous()) { |
| 1011 | *pStatus = JDWP::CS_ERROR; |
| 1012 | } else { |
| 1013 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 1014 | } |
| 1015 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1016 | } |
| 1017 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1018 | if (pDescriptor != nullptr) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1019 | std::string temp; |
| 1020 | *pDescriptor = c->GetDescriptor(&temp); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1021 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1022 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1025 | void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>* ids) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1026 | std::vector<mirror::Class*> classes; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1027 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1028 | ids->clear(); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1029 | for (size_t i = 0; i < classes.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1030 | ids->push_back(gRegistry->Add(classes[i])); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1031 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1034 | JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) { |
| 1035 | JDWP::JdwpError error; |
| 1036 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1037 | if (o == nullptr) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1038 | return JDWP::ERR_INVALID_OBJECT; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1039 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1040 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1041 | JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass()); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1042 | JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1043 | |
| 1044 | expandBufAdd1(pReply, type_tag); |
| 1045 | expandBufAddRefTypeId(pReply, type_id); |
| 1046 | |
| 1047 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 1050 | JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1051 | JDWP::JdwpError error; |
| 1052 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1053 | if (c == nullptr) { |
| 1054 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1055 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1056 | std::string temp; |
| 1057 | *signature = c->GetDescriptor(&temp); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 1058 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1061 | JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string* result) { |
| 1062 | JDWP::JdwpError error; |
| 1063 | mirror::Class* c = DecodeClass(class_id, &error); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1064 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1065 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1066 | } |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1067 | const char* source_file = c->GetSourceFile(); |
| 1068 | if (source_file == nullptr) { |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 1069 | return JDWP::ERR_ABSENT_INFORMATION; |
| 1070 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1071 | *result = source_file; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1072 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1075 | JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t* tag) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1076 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1077 | JDWP::JdwpError error; |
| 1078 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1079 | if (error != JDWP::ERR_NONE) { |
| 1080 | *tag = JDWP::JT_VOID; |
| 1081 | return error; |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1082 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1083 | *tag = TagFromObject(soa, o); |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1084 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1087 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1088 | switch (tag) { |
| 1089 | case JDWP::JT_VOID: |
| 1090 | return 0; |
| 1091 | case JDWP::JT_BYTE: |
| 1092 | case JDWP::JT_BOOLEAN: |
| 1093 | return 1; |
| 1094 | case JDWP::JT_CHAR: |
| 1095 | case JDWP::JT_SHORT: |
| 1096 | return 2; |
| 1097 | case JDWP::JT_FLOAT: |
| 1098 | case JDWP::JT_INT: |
| 1099 | return 4; |
| 1100 | case JDWP::JT_ARRAY: |
| 1101 | case JDWP::JT_OBJECT: |
| 1102 | case JDWP::JT_STRING: |
| 1103 | case JDWP::JT_THREAD: |
| 1104 | case JDWP::JT_THREAD_GROUP: |
| 1105 | case JDWP::JT_CLASS_LOADER: |
| 1106 | case JDWP::JT_CLASS_OBJECT: |
| 1107 | return sizeof(JDWP::ObjectId); |
| 1108 | case JDWP::JT_DOUBLE: |
| 1109 | case JDWP::JT_LONG: |
| 1110 | return 8; |
| 1111 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1112 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1113 | return -1; |
| 1114 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1117 | JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int32_t* length) { |
| 1118 | JDWP::JdwpError error; |
| 1119 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
| 1120 | if (a == nullptr) { |
| 1121 | return error; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1122 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1123 | *length = a->GetLength(); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1124 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1127 | 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] | 1128 | JDWP::JdwpError error; |
| 1129 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1130 | if (a == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1131 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1132 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1133 | |
| 1134 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 1135 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1136 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1137 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1138 | JDWP::JdwpTag element_tag = BasicTagFromClass(a->GetClass()->GetComponentType()); |
| 1139 | expandBufAdd1(pReply, element_tag); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1140 | expandBufAdd4BE(pReply, count); |
| 1141 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1142 | if (IsPrimitiveTag(element_tag)) { |
| 1143 | size_t width = GetTagWidth(element_tag); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1144 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 1145 | if (width == 8) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1146 | 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] | 1147 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 1148 | } else if (width == 4) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1149 | 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] | 1150 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 1151 | } else if (width == 2) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1152 | 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] | 1153 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 1154 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1155 | 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] | 1156 | memcpy(dst, &src[offset * width], count * width); |
| 1157 | } |
| 1158 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1159 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1160 | mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1161 | for (int i = 0; i < count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1162 | mirror::Object* element = oa->Get(offset + i); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1163 | JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element) |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1164 | : element_tag; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1165 | expandBufAdd1(pReply, specific_tag); |
| 1166 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 1167 | } |
| 1168 | } |
| 1169 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1170 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1173 | template <typename T> |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1174 | 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] | 1175 | NO_THREAD_SAFETY_ANALYSIS { |
| 1176 | // TODO: fix when annotalysis correctly handles non-member functions. |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1177 | DCHECK(a->GetClass()->IsPrimitiveArray()); |
| 1178 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1179 | T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1180 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1181 | *dst++ = src->ReadValue(sizeof(T)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1185 | JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1186 | JDWP::Request* request) { |
| 1187 | JDWP::JdwpError error; |
| 1188 | mirror::Array* dst = DecodeNonNullArray(array_id, &error); |
| 1189 | if (dst == nullptr) { |
| 1190 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1191 | } |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1192 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1193 | if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1194 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1195 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1196 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1197 | JDWP::JdwpTag element_tag = BasicTagFromClass(dst->GetClass()->GetComponentType()); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1198 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1199 | if (IsPrimitiveTag(element_tag)) { |
| 1200 | size_t width = GetTagWidth(element_tag); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1201 | if (width == 8) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1202 | CopyArrayData<uint64_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1203 | } else if (width == 4) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1204 | CopyArrayData<uint32_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1205 | } else if (width == 2) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1206 | CopyArrayData<uint16_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1207 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1208 | CopyArrayData<uint8_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1209 | } |
| 1210 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1211 | mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>(); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1212 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1213 | JDWP::ObjectId id = request->ReadObjectId(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1214 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 1215 | if (error != JDWP::ERR_NONE) { |
| 1216 | return error; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1217 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1218 | oa->Set<false>(offset + i, o); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1222 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1225 | JDWP::JdwpError Dbg::CreateString(const std::string& str, JDWP::ObjectId* new_string_id) { |
| 1226 | Thread* self = Thread::Current(); |
| 1227 | mirror::String* new_string = mirror::String::AllocFromModifiedUtf8(self, str.c_str()); |
| 1228 | if (new_string == nullptr) { |
| 1229 | DCHECK(self->IsExceptionPending()); |
| 1230 | self->ClearException(); |
| 1231 | LOG(ERROR) << "Could not allocate string"; |
| 1232 | *new_string_id = 0; |
| 1233 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1234 | } |
| 1235 | *new_string_id = gRegistry->Add(new_string); |
| 1236 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1239 | 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] | 1240 | JDWP::JdwpError error; |
| 1241 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1242 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1243 | *new_object_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1244 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1245 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1246 | Thread* self = Thread::Current(); |
| 1247 | mirror::Object* new_object = c->AllocObject(self); |
| 1248 | if (new_object == nullptr) { |
| 1249 | DCHECK(self->IsExceptionPending()); |
| 1250 | self->ClearException(); |
| 1251 | LOG(ERROR) << "Could not allocate object of type " << PrettyDescriptor(c); |
| 1252 | *new_object_id = 0; |
| 1253 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1254 | } |
| 1255 | *new_object_id = gRegistry->Add(new_object); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1256 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 1259 | /* |
| 1260 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 1261 | */ |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1262 | JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length, |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1263 | JDWP::ObjectId* new_array_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1264 | JDWP::JdwpError error; |
| 1265 | mirror::Class* c = DecodeClass(array_class_id, &error); |
| 1266 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1267 | *new_array_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1268 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1269 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1270 | Thread* self = Thread::Current(); |
| 1271 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1272 | mirror::Array* new_array = mirror::Array::Alloc<true>(self, c, length, |
| 1273 | c->GetComponentSizeShift(), |
| 1274 | heap->GetCurrentAllocator()); |
| 1275 | if (new_array == nullptr) { |
| 1276 | DCHECK(self->IsExceptionPending()); |
| 1277 | self->ClearException(); |
| 1278 | LOG(ERROR) << "Could not allocate array of type " << PrettyDescriptor(c); |
| 1279 | *new_array_id = 0; |
| 1280 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1281 | } |
| 1282 | *new_array_id = gRegistry->Add(new_array); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1283 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1286 | JDWP::FieldId Dbg::ToFieldId(const ArtField* f) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1287 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1290 | static JDWP::MethodId ToMethodId(const ArtMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1291 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1292 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1295 | static ArtField* FromFieldId(JDWP::FieldId fid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1296 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1297 | return reinterpret_cast<ArtField*>(static_cast<uintptr_t>(fid)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1300 | static ArtMethod* FromMethodId(JDWP::MethodId mid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1301 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1302 | return reinterpret_cast<ArtMethod*>(static_cast<uintptr_t>(mid)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1305 | bool Dbg::MatchThread(JDWP::ObjectId expected_thread_id, Thread* event_thread) { |
| 1306 | CHECK(event_thread != nullptr); |
| 1307 | JDWP::JdwpError error; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1308 | mirror::Object* expected_thread_peer = gRegistry->Get<mirror::Object*>( |
| 1309 | expected_thread_id, &error); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1310 | return expected_thread_peer == event_thread->GetPeer(); |
| 1311 | } |
| 1312 | |
| 1313 | bool Dbg::MatchLocation(const JDWP::JdwpLocation& expected_location, |
| 1314 | const JDWP::EventLocation& event_location) { |
| 1315 | if (expected_location.dex_pc != event_location.dex_pc) { |
| 1316 | return false; |
| 1317 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1318 | ArtMethod* m = FromMethodId(expected_location.method_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1319 | return m == event_location.method; |
| 1320 | } |
| 1321 | |
| 1322 | bool Dbg::MatchType(mirror::Class* event_class, JDWP::RefTypeId class_id) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1323 | if (event_class == nullptr) { |
| 1324 | return false; |
| 1325 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1326 | JDWP::JdwpError error; |
| 1327 | mirror::Class* expected_class = DecodeClass(class_id, &error); |
| 1328 | CHECK(expected_class != nullptr); |
| 1329 | return expected_class->IsAssignableFrom(event_class); |
| 1330 | } |
| 1331 | |
| 1332 | 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] | 1333 | ArtField* event_field) { |
| 1334 | ArtField* expected_field = FromFieldId(expected_field_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1335 | if (expected_field != event_field) { |
| 1336 | return false; |
| 1337 | } |
| 1338 | return Dbg::MatchType(event_field->GetDeclaringClass(), expected_type_id); |
| 1339 | } |
| 1340 | |
| 1341 | bool Dbg::MatchInstance(JDWP::ObjectId expected_instance_id, mirror::Object* event_instance) { |
| 1342 | JDWP::JdwpError error; |
| 1343 | mirror::Object* modifier_instance = gRegistry->Get<mirror::Object*>(expected_instance_id, &error); |
| 1344 | return modifier_instance == event_instance; |
| 1345 | } |
| 1346 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1347 | void Dbg::SetJdwpLocation(JDWP::JdwpLocation* location, ArtMethod* m, uint32_t dex_pc) |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 1348 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 1349 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 1350 | Locks::thread_suspend_count_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1351 | if (m == nullptr) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1352 | memset(location, 0, sizeof(*location)); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1353 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1354 | mirror::Class* c = m->GetDeclaringClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1355 | location->type_tag = GetTypeTag(c); |
| 1356 | location->class_id = gRegistry->AddRefType(c); |
| 1357 | location->method_id = ToMethodId(m); |
| 1358 | 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] | 1359 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1362 | std::string Dbg::GetMethodName(JDWP::MethodId method_id) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1363 | ArtMethod* m = FromMethodId(method_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1364 | if (m == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1365 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1366 | } |
Sebastien Hertz | 415fd08 | 2015-06-01 11:42:27 +0200 | [diff] [blame] | 1367 | return m->GetInterfaceMethodIfProxy(sizeof(void*))->GetName(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1370 | std::string Dbg::GetFieldName(JDWP::FieldId field_id) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1371 | ArtField* f = FromFieldId(field_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1372 | if (f == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1373 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1374 | } |
| 1375 | return f->GetName(); |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1376 | } |
| 1377 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1378 | /* |
| 1379 | * Augment the access flags for synthetic methods and fields by setting |
| 1380 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 1381 | * flags not specified by the Java programming language. |
| 1382 | */ |
| 1383 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 1384 | accessFlags &= kAccJavaFlagsMask; |
| 1385 | if ((accessFlags & kAccSynthetic) != 0) { |
| 1386 | accessFlags |= 0xf0000000; |
| 1387 | } |
| 1388 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1391 | /* |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1392 | * Circularly shifts registers so that arguments come first. Debuggers |
| 1393 | * expect slots to begin with arguments, but dex code places them at |
| 1394 | * the end. |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1395 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1396 | static uint16_t MangleSlot(uint16_t slot, ArtMethod* m) |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1397 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1398 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1399 | if (code_item == nullptr) { |
| 1400 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1401 | // return the slot as is since all registers are arguments. |
| 1402 | LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m); |
| 1403 | return slot; |
| 1404 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1405 | uint16_t ins_size = code_item->ins_size_; |
| 1406 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1407 | if (slot >= locals_size) { |
| 1408 | return slot - locals_size; |
| 1409 | } else { |
| 1410 | return slot + ins_size; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1411 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1412 | } |
| 1413 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1414 | /* |
| 1415 | * Circularly shifts registers so that arguments come last. Reverts |
| 1416 | * slots to dex style argument placement. |
| 1417 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1418 | static uint16_t DemangleSlot(uint16_t slot, ArtMethod* m, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1419 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1420 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1421 | if (code_item == nullptr) { |
| 1422 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1423 | // return the slot as is since all registers are arguments. |
| 1424 | LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1425 | uint16_t vreg_count = ArtMethod::NumArgRegisters(m->GetShorty()); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1426 | if (slot < vreg_count) { |
| 1427 | *error = JDWP::ERR_NONE; |
| 1428 | return slot; |
| 1429 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1430 | } else { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1431 | if (slot < code_item->registers_size_) { |
| 1432 | uint16_t ins_size = code_item->ins_size_; |
| 1433 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1434 | *error = JDWP::ERR_NONE; |
| 1435 | return (slot < ins_size) ? slot + locals_size : slot - ins_size; |
| 1436 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1437 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1438 | |
| 1439 | // Slot is invalid in the method. |
| 1440 | LOG(ERROR) << "Invalid local slot " << slot << " for method " << PrettyMethod(m); |
| 1441 | *error = JDWP::ERR_INVALID_SLOT; |
| 1442 | return DexFile::kDexNoIndex16; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1443 | } |
| 1444 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1445 | 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] | 1446 | JDWP::JdwpError error; |
| 1447 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1448 | if (c == nullptr) { |
| 1449 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1450 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1451 | |
| 1452 | size_t instance_field_count = c->NumInstanceFields(); |
| 1453 | size_t static_field_count = c->NumStaticFields(); |
| 1454 | |
| 1455 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 1456 | |
| 1457 | 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] | 1458 | 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] | 1459 | expandBufAddFieldId(pReply, ToFieldId(f)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1460 | expandBufAddUtf8String(pReply, f->GetName()); |
| 1461 | expandBufAddUtf8String(pReply, f->GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1462 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1463 | static const char genericSignature[1] = ""; |
| 1464 | expandBufAddUtf8String(pReply, genericSignature); |
| 1465 | } |
| 1466 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 1467 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1468 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1469 | } |
| 1470 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1471 | JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1472 | JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1473 | JDWP::JdwpError error; |
| 1474 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1475 | if (c == nullptr) { |
| 1476 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1477 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1478 | |
| 1479 | size_t direct_method_count = c->NumDirectMethods(); |
| 1480 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 1481 | |
| 1482 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 1483 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1484 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 1485 | auto ptr_size = cl->GetImagePointerSize(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1486 | 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] | 1487 | ArtMethod* m = i < direct_method_count ? |
| 1488 | 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] | 1489 | expandBufAddMethodId(pReply, ToMethodId(m)); |
Sebastien Hertz | 415fd08 | 2015-06-01 11:42:27 +0200 | [diff] [blame] | 1490 | expandBufAddUtf8String(pReply, m->GetInterfaceMethodIfProxy(sizeof(void*))->GetName()); |
| 1491 | expandBufAddUtf8String(pReply, |
| 1492 | m->GetInterfaceMethodIfProxy(sizeof(void*))->GetSignature().ToString()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1493 | if (with_generic) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1494 | const char* generic_signature = ""; |
| 1495 | expandBufAddUtf8String(pReply, generic_signature); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1496 | } |
| 1497 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 1498 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1499 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1500 | } |
| 1501 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1502 | JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1503 | JDWP::JdwpError error; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1504 | Thread* self = Thread::Current(); |
| 1505 | StackHandleScope<1> hs(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1506 | Handle<mirror::Class> c(hs.NewHandle(DecodeClass(class_id, &error))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1507 | if (c.Get() == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1508 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1509 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1510 | size_t interface_count = c->NumDirectInterfaces(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1511 | expandBufAdd4BE(pReply, interface_count); |
| 1512 | for (size_t i = 0; i < interface_count; ++i) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1513 | expandBufAddRefTypeId(pReply, |
| 1514 | gRegistry->AddRefType(mirror::Class::GetDirectInterface(self, c, i))); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1515 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1516 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1519 | void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1520 | struct DebugCallbackContext { |
| 1521 | int numItems; |
| 1522 | JDWP::ExpandBuf* pReply; |
| 1523 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1524 | static bool Callback(void* context, uint32_t address, uint32_t line_number) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1525 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1526 | expandBufAdd8BE(pContext->pReply, address); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1527 | expandBufAdd4BE(pContext->pReply, line_number); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1528 | pContext->numItems++; |
Sebastien Hertz | f2910ee | 2013-10-19 16:39:24 +0200 | [diff] [blame] | 1529 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1530 | } |
| 1531 | }; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1532 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1533 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1534 | uint64_t start, end; |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1535 | if (code_item == nullptr) { |
| 1536 | DCHECK(m->IsNative() || m->IsProxyMethod()); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1537 | start = -1; |
| 1538 | end = -1; |
| 1539 | } else { |
| 1540 | start = 0; |
jeffhao | 14f0db9 | 2012-12-14 17:50:42 -0800 | [diff] [blame] | 1541 | // Return the index of the last instruction |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1542 | end = code_item->insns_size_in_code_units_ - 1; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | expandBufAdd8BE(pReply, start); |
| 1546 | expandBufAdd8BE(pReply, end); |
| 1547 | |
| 1548 | // Add numLines later |
| 1549 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 1550 | expandBufAdd4BE(pReply, 0); |
| 1551 | |
| 1552 | DebugCallbackContext context; |
| 1553 | context.numItems = 0; |
| 1554 | context.pReply = pReply; |
| 1555 | |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1556 | if (code_item != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1557 | m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1558 | DebugCallbackContext::Callback, nullptr, &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1559 | } |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1560 | |
| 1561 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1564 | void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, |
| 1565 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1566 | struct DebugCallbackContext { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1567 | ArtMethod* method; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1568 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1569 | size_t variable_count; |
| 1570 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1571 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1572 | static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, |
| 1573 | const char* name, const char* descriptor, const char* signature) |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1574 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1575 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1576 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1577 | VLOG(jdwp) << StringPrintf(" %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d", |
| 1578 | pContext->variable_count, startAddress, endAddress - startAddress, |
| 1579 | name, descriptor, signature, slot, |
| 1580 | MangleSlot(slot, pContext->method)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1581 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1582 | slot = MangleSlot(slot, pContext->method); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1583 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1584 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 1585 | expandBufAddUtf8String(pContext->pReply, name); |
| 1586 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1587 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1588 | expandBufAddUtf8String(pContext->pReply, signature); |
| 1589 | } |
| 1590 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 1591 | expandBufAdd4BE(pContext->pReply, slot); |
| 1592 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1593 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1594 | } |
| 1595 | }; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1596 | ArtMethod* m = FromMethodId(method_id); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1597 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1598 | // arg_count considers doubles and longs to take 2 units. |
| 1599 | // variable_count considers everything to take 1 unit. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1600 | std::string shorty(m->GetShorty()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1601 | expandBufAdd4BE(pReply, ArtMethod::NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1602 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1603 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1604 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1605 | expandBufAdd4BE(pReply, 0); |
| 1606 | |
| 1607 | DebugCallbackContext context; |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1608 | context.method = m; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1609 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1610 | context.variable_count = 0; |
| 1611 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1612 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1613 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1614 | if (code_item != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1615 | m->GetDexFile()->DecodeDebugInfo( |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1616 | code_item, m->IsStatic(), m->GetDexMethodIndex(), nullptr, DebugCallbackContext::Callback, |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1617 | &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1618 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1619 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1620 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1623 | void Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value, |
| 1624 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1625 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1626 | JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1627 | OutputJValue(tag, return_value, pReply); |
| 1628 | } |
| 1629 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1630 | void Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value, |
| 1631 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1632 | ArtField* f = FromFieldId(field_id); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1633 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1634 | OutputJValue(tag, field_value, pReply); |
| 1635 | } |
| 1636 | |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1637 | JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1638 | std::vector<uint8_t>* bytecodes) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1639 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1640 | if (m == nullptr) { |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1641 | return JDWP::ERR_INVALID_METHODID; |
| 1642 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1643 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1644 | size_t byte_count = code_item->insns_size_in_code_units_ * 2; |
| 1645 | const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_); |
| 1646 | const uint8_t* end = begin + byte_count; |
| 1647 | for (const uint8_t* p = begin; p != end; ++p) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1648 | bytecodes->push_back(*p); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1649 | } |
| 1650 | return JDWP::ERR_NONE; |
| 1651 | } |
| 1652 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1653 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1654 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1657 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1658 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1661 | static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id, |
| 1662 | JDWP::FieldId field_id, JDWP::ExpandBuf* pReply, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1663 | bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1664 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1665 | JDWP::JdwpError error; |
| 1666 | mirror::Class* c = DecodeClass(ref_type_id, &error); |
| 1667 | if (ref_type_id != 0 && c == nullptr) { |
| 1668 | return error; |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1671 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1672 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1673 | return JDWP::ERR_INVALID_OBJECT; |
| 1674 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1675 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1676 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1677 | mirror::Class* receiver_class = c; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1678 | if (receiver_class == nullptr && o != nullptr) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1679 | receiver_class = o->GetClass(); |
| 1680 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1681 | // TODO: should we give up now if receiver_class is null? |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1682 | if (receiver_class != nullptr && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1683 | LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1684 | return JDWP::ERR_INVALID_FIELDID; |
| 1685 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1686 | |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1687 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1688 | // TODO: should we change the tests and check both? |
| 1689 | if (is_static) { |
| 1690 | if (!f->IsStatic()) { |
| 1691 | return JDWP::ERR_INVALID_FIELDID; |
| 1692 | } |
| 1693 | } else { |
| 1694 | if (f->IsStatic()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1695 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues on static field " |
| 1696 | << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1697 | } |
| 1698 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1699 | if (f->IsStatic()) { |
| 1700 | o = f->GetDeclaringClass(); |
| 1701 | } |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1702 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1703 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1704 | JValue field_value; |
| 1705 | if (tag == JDWP::JT_VOID) { |
| 1706 | LOG(FATAL) << "Unknown tag: " << tag; |
| 1707 | } else if (!IsPrimitiveTag(tag)) { |
| 1708 | field_value.SetL(f->GetObject(o)); |
| 1709 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1710 | field_value.SetJ(f->Get64(o)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1711 | } else { |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1712 | field_value.SetI(f->Get32(o)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1713 | } |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1714 | Dbg::OutputJValue(tag, &field_value, pReply); |
| 1715 | |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1716 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1719 | JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1720 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1721 | return GetFieldValueImpl(0, object_id, field_id, pReply, false); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1722 | } |
| 1723 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1724 | JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, |
| 1725 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1726 | return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1727 | } |
| 1728 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1729 | static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1730 | uint64_t value, int width, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1731 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1732 | JDWP::JdwpError error; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1733 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1734 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1735 | return JDWP::ERR_INVALID_OBJECT; |
| 1736 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1737 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1738 | |
| 1739 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1740 | // TODO: should we change the tests and check both? |
| 1741 | if (is_static) { |
| 1742 | if (!f->IsStatic()) { |
| 1743 | return JDWP::ERR_INVALID_FIELDID; |
| 1744 | } |
| 1745 | } else { |
| 1746 | if (f->IsStatic()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1747 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues on static field " << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1748 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1749 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1750 | if (f->IsStatic()) { |
| 1751 | o = f->GetDeclaringClass(); |
| 1752 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1753 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1754 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1755 | |
| 1756 | if (IsPrimitiveTag(tag)) { |
| 1757 | if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1758 | CHECK_EQ(width, 8); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1759 | // Debugging can't use transactional mode (runtime only). |
| 1760 | f->Set64<false>(o, value); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1761 | } else { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1762 | CHECK_LE(width, 4); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1763 | // Debugging can't use transactional mode (runtime only). |
| 1764 | f->Set32<false>(o, value); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1765 | } |
| 1766 | } else { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1767 | mirror::Object* v = Dbg::GetObjectRegistry()->Get<mirror::Object*>(value, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1768 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1769 | return JDWP::ERR_INVALID_OBJECT; |
| 1770 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1771 | if (v != nullptr) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1772 | mirror::Class* field_type; |
| 1773 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1774 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1775 | HandleWrapper<mirror::Object> h_v(hs.NewHandleWrapper(&v)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1776 | HandleWrapper<mirror::Object> h_o(hs.NewHandleWrapper(&o)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1777 | field_type = f->GetType<true>(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1778 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1779 | if (!field_type->IsAssignableFrom(v->GetClass())) { |
| 1780 | return JDWP::ERR_INVALID_OBJECT; |
| 1781 | } |
| 1782 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1783 | // Debugging can't use transactional mode (runtime only). |
| 1784 | f->SetObject<false>(o, v); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1785 | } |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1786 | |
| 1787 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1788 | } |
| 1789 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1790 | 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] | 1791 | int width) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1792 | return SetFieldValueImpl(object_id, field_id, value, width, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1795 | JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) { |
| 1796 | return SetFieldValueImpl(0, field_id, value, width, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1797 | } |
| 1798 | |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1799 | JDWP::JdwpError Dbg::StringToUtf8(JDWP::ObjectId string_id, std::string* str) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1800 | JDWP::JdwpError error; |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1801 | mirror::Object* obj = gRegistry->Get<mirror::Object*>(string_id, &error); |
| 1802 | if (error != JDWP::ERR_NONE) { |
| 1803 | return error; |
| 1804 | } |
| 1805 | if (obj == nullptr) { |
| 1806 | return JDWP::ERR_INVALID_OBJECT; |
| 1807 | } |
| 1808 | { |
| 1809 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1810 | mirror::Class* java_lang_String = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_String); |
| 1811 | if (!java_lang_String->IsAssignableFrom(obj->GetClass())) { |
| 1812 | // This isn't a string. |
| 1813 | return JDWP::ERR_INVALID_STRING; |
| 1814 | } |
| 1815 | } |
| 1816 | *str = obj->AsString()->ToModifiedUtf8(); |
| 1817 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1820 | void Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) { |
| 1821 | if (IsPrimitiveTag(tag)) { |
| 1822 | expandBufAdd1(pReply, tag); |
| 1823 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 1824 | expandBufAdd1(pReply, return_value->GetI()); |
| 1825 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 1826 | expandBufAdd2BE(pReply, return_value->GetI()); |
| 1827 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 1828 | expandBufAdd4BE(pReply, return_value->GetI()); |
| 1829 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1830 | expandBufAdd8BE(pReply, return_value->GetJ()); |
| 1831 | } else { |
| 1832 | CHECK_EQ(tag, JDWP::JT_VOID); |
| 1833 | } |
| 1834 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1835 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1836 | mirror::Object* value = return_value->GetL(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1837 | expandBufAdd1(pReply, TagFromObject(soa, value)); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1838 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 1839 | } |
| 1840 | } |
| 1841 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1842 | JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string* name) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1843 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1844 | JDWP::JdwpError error; |
| 1845 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 1846 | UNUSED(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1847 | if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1848 | return error; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1849 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1850 | |
| 1851 | // 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] | 1852 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 1853 | CHECK(thread_object != nullptr) << error; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1854 | ArtField* java_lang_Thread_name_field = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1855 | soa.DecodeField(WellKnownClasses::java_lang_Thread_name); |
| 1856 | mirror::String* s = |
| 1857 | reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1858 | if (s != nullptr) { |
| 1859 | *name = s->ToModifiedUtf8(); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1860 | } |
| 1861 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1864 | JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1865 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1866 | JDWP::JdwpError error; |
| 1867 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 1868 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1869 | return JDWP::ERR_INVALID_OBJECT; |
| 1870 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1871 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroup"); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1872 | // Okay, so it's an object, but is it actually a thread? |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 1873 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 1874 | UNUSED(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1875 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1876 | // Zombie threads are in the null group. |
| 1877 | expandBufAddObjectId(pReply, JDWP::ObjectId(0)); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1878 | error = JDWP::ERR_NONE; |
| 1879 | } else if (error == JDWP::ERR_NONE) { |
| 1880 | mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
| 1881 | CHECK(c != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1882 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 1883 | CHECK(f != nullptr); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1884 | mirror::Object* group = f->GetObject(thread_object); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 1885 | CHECK(group != nullptr); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1886 | JDWP::ObjectId thread_group_id = gRegistry->Add(group); |
| 1887 | expandBufAddObjectId(pReply, thread_group_id); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1888 | } |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1889 | return error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1892 | static mirror::Object* DecodeThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 1893 | JDWP::ObjectId thread_group_id, JDWP::JdwpError* error) |
| 1894 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1895 | mirror::Object* thread_group = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_group_id, |
| 1896 | error); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1897 | if (*error != JDWP::ERR_NONE) { |
| 1898 | return nullptr; |
| 1899 | } |
| 1900 | if (thread_group == nullptr) { |
| 1901 | *error = JDWP::ERR_INVALID_OBJECT; |
| 1902 | return nullptr; |
| 1903 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1904 | mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup); |
| 1905 | CHECK(c != nullptr); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1906 | if (!c->IsAssignableFrom(thread_group->GetClass())) { |
| 1907 | // This is not a java.lang.ThreadGroup. |
| 1908 | *error = JDWP::ERR_INVALID_THREAD_GROUP; |
| 1909 | return nullptr; |
| 1910 | } |
| 1911 | *error = JDWP::ERR_NONE; |
| 1912 | return thread_group; |
| 1913 | } |
| 1914 | |
| 1915 | JDWP::JdwpError Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
| 1916 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1917 | JDWP::JdwpError error; |
| 1918 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 1919 | if (error != JDWP::ERR_NONE) { |
| 1920 | return error; |
| 1921 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1922 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupName"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1923 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1924 | CHECK(f != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1925 | mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group)); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1926 | |
| 1927 | std::string thread_group_name(s->ToModifiedUtf8()); |
| 1928 | expandBufAddUtf8String(pReply, thread_group_name); |
| 1929 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1930 | } |
| 1931 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1932 | JDWP::JdwpError Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1933 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1934 | JDWP::JdwpError error; |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1935 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 1936 | if (error != JDWP::ERR_NONE) { |
| 1937 | return error; |
| 1938 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1939 | mirror::Object* parent; |
| 1940 | { |
| 1941 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupParent"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1942 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_parent); |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1943 | CHECK(f != nullptr); |
| 1944 | parent = f->GetObject(thread_group); |
| 1945 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1946 | JDWP::ObjectId parent_group_id = gRegistry->Add(parent); |
| 1947 | expandBufAddObjectId(pReply, parent_group_id); |
| 1948 | return JDWP::ERR_NONE; |
| 1949 | } |
| 1950 | |
| 1951 | static void GetChildThreadGroups(ScopedObjectAccessUnchecked& soa, mirror::Object* thread_group, |
| 1952 | std::vector<JDWP::ObjectId>* child_thread_group_ids) |
| 1953 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1954 | CHECK(thread_group != nullptr); |
| 1955 | |
| 1956 | // Get the ArrayList<ThreadGroup> "groups" out of this thread group... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1957 | ArtField* groups_field = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_groups); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1958 | mirror::Object* groups_array_list = groups_field->GetObject(thread_group); |
Sebastien Hertz | e49e195 | 2014-10-13 11:27:13 +0200 | [diff] [blame] | 1959 | { |
| 1960 | // The "groups" field is declared as a java.util.List: check it really is |
| 1961 | // an instance of java.util.ArrayList. |
| 1962 | CHECK(groups_array_list != nullptr); |
| 1963 | mirror::Class* java_util_ArrayList_class = |
| 1964 | soa.Decode<mirror::Class*>(WellKnownClasses::java_util_ArrayList); |
| 1965 | CHECK(groups_array_list->InstanceOf(java_util_ArrayList_class)); |
| 1966 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1967 | |
| 1968 | // Get the array and size out of the ArrayList<ThreadGroup>... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1969 | ArtField* array_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_array); |
| 1970 | ArtField* size_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_size); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1971 | mirror::ObjectArray<mirror::Object>* groups_array = |
| 1972 | array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>(); |
| 1973 | const int32_t size = size_field->GetInt(groups_array_list); |
| 1974 | |
| 1975 | // Copy the first 'size' elements out of the array into the result. |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1976 | ObjectRegistry* registry = Dbg::GetObjectRegistry(); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1977 | for (int32_t i = 0; i < size; ++i) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1978 | child_thread_group_ids->push_back(registry->Add(groups_array->Get(i))); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | JDWP::JdwpError Dbg::GetThreadGroupChildren(JDWP::ObjectId thread_group_id, |
| 1983 | JDWP::ExpandBuf* pReply) { |
| 1984 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1985 | JDWP::JdwpError error; |
| 1986 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 1987 | if (error != JDWP::ERR_NONE) { |
| 1988 | return error; |
| 1989 | } |
| 1990 | |
| 1991 | // Add child threads. |
| 1992 | { |
| 1993 | std::vector<JDWP::ObjectId> child_thread_ids; |
| 1994 | GetThreads(thread_group, &child_thread_ids); |
| 1995 | expandBufAdd4BE(pReply, child_thread_ids.size()); |
| 1996 | for (JDWP::ObjectId child_thread_id : child_thread_ids) { |
| 1997 | expandBufAddObjectId(pReply, child_thread_id); |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | // Add child thread groups. |
| 2002 | { |
| 2003 | std::vector<JDWP::ObjectId> child_thread_groups_ids; |
| 2004 | GetChildThreadGroups(soa, thread_group, &child_thread_groups_ids); |
| 2005 | expandBufAdd4BE(pReply, child_thread_groups_ids.size()); |
| 2006 | for (JDWP::ObjectId child_thread_group_id : child_thread_groups_ids) { |
| 2007 | expandBufAddObjectId(pReply, child_thread_group_id); |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2015 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2016 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2017 | mirror::Object* group = f->GetObject(f->GetDeclaringClass()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2018 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2021 | JDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) { |
| 2022 | switch (state) { |
| 2023 | case kBlocked: |
| 2024 | return JDWP::TS_MONITOR; |
| 2025 | case kNative: |
| 2026 | case kRunnable: |
| 2027 | case kSuspended: |
| 2028 | return JDWP::TS_RUNNING; |
| 2029 | case kSleeping: |
| 2030 | return JDWP::TS_SLEEPING; |
| 2031 | case kStarting: |
| 2032 | case kTerminated: |
| 2033 | return JDWP::TS_ZOMBIE; |
| 2034 | case kTimedWaiting: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2035 | case kWaitingForCheckPointsToRun: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2036 | case kWaitingForDebuggerSend: |
| 2037 | case kWaitingForDebuggerSuspension: |
| 2038 | case kWaitingForDebuggerToAttach: |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 2039 | case kWaitingForDeoptimization: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2040 | case kWaitingForGcToComplete: |
Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 2041 | case kWaitingForGetObjectsAllocated: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2042 | case kWaitingForJniOnLoad: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2043 | case kWaitingForMethodTracingStart: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2044 | case kWaitingForSignalCatcherOutput: |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 2045 | case kWaitingForVisitObjects: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2046 | case kWaitingInMainDebuggerLoop: |
| 2047 | case kWaitingInMainSignalCatcherLoop: |
| 2048 | case kWaitingPerformingGc: |
| 2049 | case kWaiting: |
| 2050 | return JDWP::TS_WAIT; |
| 2051 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 2052 | } |
| 2053 | LOG(FATAL) << "Unknown thread state: " << state; |
| 2054 | return JDWP::TS_ZOMBIE; |
| 2055 | } |
| 2056 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2057 | JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, |
| 2058 | JDWP::JdwpSuspendStatus* pSuspendStatus) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2059 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2060 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2061 | *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED; |
| 2062 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2063 | JDWP::JdwpError error; |
| 2064 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2065 | if (error != JDWP::ERR_NONE) { |
| 2066 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2067 | *pThreadStatus = JDWP::TS_ZOMBIE; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2068 | return JDWP::ERR_NONE; |
| 2069 | } |
| 2070 | return error; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2071 | } |
| 2072 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2073 | if (IsSuspendedForDebugger(soa, thread)) { |
| 2074 | *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2075 | } |
| 2076 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2077 | *pThreadStatus = ToJdwpThreadStatus(thread->GetState()); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2078 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2081 | JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2082 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2083 | JDWP::JdwpError error; |
| 2084 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2085 | if (error != JDWP::ERR_NONE) { |
| 2086 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2087 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2088 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2089 | expandBufAdd4BE(pReply, thread->GetDebugSuspendCount()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2090 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2093 | JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) { |
| 2094 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2095 | JDWP::JdwpError error; |
| 2096 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2097 | if (error != JDWP::ERR_NONE) { |
| 2098 | return error; |
| 2099 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 2100 | thread->Interrupt(soa.Self()); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2101 | return JDWP::ERR_NONE; |
| 2102 | } |
| 2103 | |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2104 | static bool IsInDesiredThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 2105 | mirror::Object* desired_thread_group, mirror::Object* peer) |
| 2106 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2107 | // Do we want threads from all thread groups? |
| 2108 | if (desired_thread_group == nullptr) { |
| 2109 | return true; |
| 2110 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2111 | ArtField* thread_group_field = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2112 | DCHECK(thread_group_field != nullptr); |
| 2113 | mirror::Object* group = thread_group_field->GetObject(peer); |
| 2114 | return (group == desired_thread_group); |
| 2115 | } |
| 2116 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2117 | 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] | 2118 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2119 | std::list<Thread*> all_threads_list; |
| 2120 | { |
| 2121 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 2122 | all_threads_list = Runtime::Current()->GetThreadList()->GetList(); |
| 2123 | } |
| 2124 | for (Thread* t : all_threads_list) { |
| 2125 | if (t == Dbg::GetDebugThread()) { |
| 2126 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 2127 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 2128 | continue; |
| 2129 | } |
| 2130 | if (t->IsStillStarting()) { |
| 2131 | // This thread is being started (and has been registered in the thread list). However, it is |
| 2132 | // not completely started yet so we must ignore it. |
| 2133 | continue; |
| 2134 | } |
| 2135 | mirror::Object* peer = t->GetPeer(); |
| 2136 | if (peer == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2137 | // 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] | 2138 | // this thread yet. |
| 2139 | // TODO: if we identified threads to the debugger by their Thread* |
| 2140 | // rather than their peer's mirror::Object*, we could fix this. |
| 2141 | // Doing so might help us report ZOMBIE threads too. |
| 2142 | continue; |
| 2143 | } |
| 2144 | if (IsInDesiredThreadGroup(soa, thread_group, peer)) { |
| 2145 | thread_ids->push_back(gRegistry->Add(peer)); |
| 2146 | } |
| 2147 | } |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2148 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2149 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2150 | static int GetStackDepth(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2151 | struct CountStackDepthVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2152 | explicit CountStackDepthVisitor(Thread* thread_in) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2153 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2154 | depth(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2155 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2156 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2157 | // annotalysis. |
| 2158 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2159 | if (!GetMethod()->IsRuntimeMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 2160 | ++depth; |
| 2161 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2162 | return true; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2163 | } |
| 2164 | size_t depth; |
| 2165 | }; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2166 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2167 | CountStackDepthVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2168 | visitor.WalkStack(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2169 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2170 | } |
| 2171 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2172 | JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t* result) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2173 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2174 | JDWP::JdwpError error; |
| 2175 | *result = 0; |
| 2176 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2177 | if (error != JDWP::ERR_NONE) { |
| 2178 | return error; |
| 2179 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2180 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2181 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2182 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2183 | *result = GetStackDepth(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2184 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2185 | } |
| 2186 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 2187 | JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame, |
| 2188 | size_t frame_count, JDWP::ExpandBuf* buf) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2189 | class GetFrameVisitor : public StackVisitor { |
| 2190 | public: |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2191 | GetFrameVisitor(Thread* thread, size_t start_frame_in, size_t frame_count_in, |
| 2192 | JDWP::ExpandBuf* buf_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2193 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2194 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2195 | depth_(0), |
| 2196 | start_frame_(start_frame_in), |
| 2197 | frame_count_(frame_count_in), |
| 2198 | buf_(buf_in) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2199 | expandBufAdd4BE(buf_, frame_count_); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2200 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2201 | |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2202 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2203 | if (GetMethod()->IsRuntimeMethod()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2204 | 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] | 2205 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2206 | if (depth_ >= start_frame_ + frame_count_) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2207 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2208 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2209 | if (depth_ >= start_frame_) { |
| 2210 | JDWP::FrameId frame_id(GetFrameId()); |
| 2211 | JDWP::JdwpLocation location; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2212 | SetJdwpLocation(&location, GetMethod(), GetDexPc()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2213 | VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2214 | expandBufAdd8BE(buf_, frame_id); |
| 2215 | expandBufAddLocation(buf_, location); |
| 2216 | } |
| 2217 | ++depth_; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2218 | return true; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2219 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2220 | |
| 2221 | private: |
| 2222 | size_t depth_; |
| 2223 | const size_t start_frame_; |
| 2224 | const size_t frame_count_; |
| 2225 | JDWP::ExpandBuf* buf_; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2226 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2227 | |
| 2228 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2229 | JDWP::JdwpError error; |
| 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 | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2237 | GetFrameVisitor visitor(thread, start_frame, frame_count, buf); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2238 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2239 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2243 | return GetThreadId(Thread::Current()); |
| 2244 | } |
| 2245 | |
| 2246 | JDWP::ObjectId Dbg::GetThreadId(Thread* thread) { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 2247 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2248 | return gRegistry->Add(thread->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2249 | } |
| 2250 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2251 | void Dbg::SuspendVM() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2252 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | void Dbg::ResumeVM() { |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 2256 | Runtime::Current()->GetThreadList()->ResumeAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2257 | } |
| 2258 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2259 | JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2260 | Thread* self = Thread::Current(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2261 | ScopedLocalRef<jobject> peer(self->GetJniEnv(), nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2262 | { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2263 | ScopedObjectAccess soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2264 | JDWP::JdwpError error; |
| 2265 | peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id, &error))); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2266 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2267 | if (peer.get() == nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2268 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2269 | } |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 2270 | // Suspend thread to build stack trace. |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2271 | bool timed_out; |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 2272 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 2273 | Thread* thread = thread_list->SuspendThreadByPeer(peer.get(), request_suspension, true, |
| 2274 | &timed_out); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2275 | if (thread != nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2276 | return JDWP::ERR_NONE; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2277 | } else if (timed_out) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2278 | return JDWP::ERR_INTERNAL; |
| 2279 | } else { |
| 2280 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2281 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2284 | void Dbg::ResumeThread(JDWP::ObjectId thread_id) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2285 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2286 | JDWP::JdwpError error; |
| 2287 | mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2288 | CHECK(peer != nullptr) << error; |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2289 | Thread* thread; |
| 2290 | { |
| 2291 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 2292 | thread = Thread::FromManagedThread(soa, peer); |
| 2293 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2294 | if (thread == nullptr) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2295 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 2296 | return; |
| 2297 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2298 | bool needs_resume; |
| 2299 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2300 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2301 | needs_resume = thread->GetSuspendCount() > 0; |
| 2302 | } |
| 2303 | if (needs_resume) { |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 2304 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
| 2305 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2309 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2310 | } |
| 2311 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2312 | struct GetThisVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2313 | GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2314 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2315 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2316 | this_object(nullptr), |
| 2317 | frame_id(frame_id_in) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2318 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2319 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2320 | // annotalysis. |
| 2321 | virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2322 | if (frame_id != GetFrameId()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2323 | return true; // continue |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2324 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2325 | this_object = GetThisObject(); |
| 2326 | return false; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2327 | } |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 2328 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2329 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2330 | mirror::Object* this_object; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2331 | JDWP::FrameId frame_id; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2332 | }; |
| 2333 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2334 | JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, |
| 2335 | JDWP::ObjectId* result) { |
| 2336 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2337 | JDWP::JdwpError error; |
| 2338 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2339 | if (error != JDWP::ERR_NONE) { |
| 2340 | return error; |
| 2341 | } |
| 2342 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2343 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2344 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2345 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2346 | GetThisVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2347 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2348 | *result = gRegistry->Add(visitor.this_object); |
| 2349 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2350 | } |
| 2351 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2352 | // Walks the stack until we find the frame with the given FrameId. |
| 2353 | class FindFrameVisitor FINAL : public StackVisitor { |
| 2354 | public: |
| 2355 | FindFrameVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id) |
| 2356 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2357 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2358 | frame_id_(frame_id), |
| 2359 | error_(JDWP::ERR_INVALID_FRAMEID) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2360 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2361 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2362 | // annotalysis. |
| 2363 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 2364 | if (GetFrameId() != frame_id_) { |
| 2365 | return true; // Not our frame, carry on. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2366 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2367 | ArtMethod* m = GetMethod(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2368 | if (m->IsNative()) { |
| 2369 | // We can't read/write local value from/into native method. |
| 2370 | error_ = JDWP::ERR_OPAQUE_FRAME; |
| 2371 | } else { |
| 2372 | // We found our frame. |
| 2373 | error_ = JDWP::ERR_NONE; |
| 2374 | } |
| 2375 | return false; |
| 2376 | } |
| 2377 | |
| 2378 | JDWP::JdwpError GetError() const { |
| 2379 | return error_; |
| 2380 | } |
| 2381 | |
| 2382 | private: |
| 2383 | const JDWP::FrameId frame_id_; |
| 2384 | JDWP::JdwpError error_; |
| 2385 | }; |
| 2386 | |
| 2387 | JDWP::JdwpError Dbg::GetLocalValues(JDWP::Request* request, JDWP::ExpandBuf* pReply) { |
| 2388 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2389 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2390 | |
| 2391 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2392 | JDWP::JdwpError error; |
| 2393 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2394 | if (error != JDWP::ERR_NONE) { |
| 2395 | return error; |
| 2396 | } |
| 2397 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2398 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2399 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2400 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2401 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2402 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2403 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2404 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2405 | return visitor.GetError(); |
| 2406 | } |
| 2407 | |
| 2408 | // Read the values from visitor's context. |
| 2409 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2410 | expandBufAdd4BE(pReply, slot_count); /* "int values" */ |
| 2411 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2412 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2413 | JDWP::JdwpTag reqSigByte = request->ReadTag(); |
| 2414 | |
| 2415 | VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte; |
| 2416 | |
| 2417 | size_t width = Dbg::GetTagWidth(reqSigByte); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 2418 | uint8_t* ptr = expandBufAddSpace(pReply, width + 1); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2419 | error = Dbg::GetLocalValue(visitor, soa, slot, reqSigByte, ptr, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2420 | if (error != JDWP::ERR_NONE) { |
| 2421 | return error; |
| 2422 | } |
| 2423 | } |
| 2424 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2425 | } |
| 2426 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2427 | constexpr JDWP::JdwpError kStackFrameLocalAccessError = JDWP::ERR_ABSENT_INFORMATION; |
| 2428 | |
| 2429 | static std::string GetStackContextAsString(const StackVisitor& visitor) |
| 2430 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2431 | return StringPrintf(" at DEX pc 0x%08x in method %s", visitor.GetDexPc(false), |
| 2432 | PrettyMethod(visitor.GetMethod()).c_str()); |
| 2433 | } |
| 2434 | |
| 2435 | static JDWP::JdwpError FailGetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2436 | JDWP::JdwpTag tag) |
| 2437 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2438 | LOG(ERROR) << "Failed to read " << tag << " local from register v" << vreg |
| 2439 | << GetStackContextAsString(visitor); |
| 2440 | return kStackFrameLocalAccessError; |
| 2441 | } |
| 2442 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2443 | JDWP::JdwpError Dbg::GetLocalValue(const StackVisitor& visitor, ScopedObjectAccessUnchecked& soa, |
| 2444 | int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2445 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2446 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2447 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2448 | if (error != JDWP::ERR_NONE) { |
| 2449 | return error; |
| 2450 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2451 | // 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] | 2452 | switch (tag) { |
| 2453 | case JDWP::JT_BOOLEAN: { |
| 2454 | CHECK_EQ(width, 1U); |
| 2455 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2456 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2457 | return FailGetLocalValue(visitor, vreg, tag); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2458 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2459 | VLOG(jdwp) << "get boolean local " << vreg << " = " << intVal; |
| 2460 | JDWP::Set1(buf + 1, intVal != 0); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2461 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2462 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2463 | case JDWP::JT_BYTE: { |
| 2464 | CHECK_EQ(width, 1U); |
| 2465 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2466 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2467 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2468 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2469 | VLOG(jdwp) << "get byte local " << vreg << " = " << intVal; |
| 2470 | JDWP::Set1(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2471 | break; |
| 2472 | } |
| 2473 | case JDWP::JT_SHORT: |
| 2474 | case JDWP::JT_CHAR: { |
| 2475 | CHECK_EQ(width, 2U); |
| 2476 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2477 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2478 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2479 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2480 | VLOG(jdwp) << "get short/char local " << vreg << " = " << intVal; |
| 2481 | JDWP::Set2BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2482 | break; |
| 2483 | } |
| 2484 | case JDWP::JT_INT: { |
| 2485 | CHECK_EQ(width, 4U); |
| 2486 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2487 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2488 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2489 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2490 | VLOG(jdwp) << "get int local " << vreg << " = " << intVal; |
| 2491 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2492 | break; |
| 2493 | } |
| 2494 | case JDWP::JT_FLOAT: { |
| 2495 | CHECK_EQ(width, 4U); |
| 2496 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2497 | if (!visitor.GetVReg(m, vreg, kFloatVReg, &intVal)) { |
| 2498 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2499 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2500 | VLOG(jdwp) << "get float local " << vreg << " = " << intVal; |
| 2501 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2502 | break; |
| 2503 | } |
| 2504 | case JDWP::JT_ARRAY: |
| 2505 | case JDWP::JT_CLASS_LOADER: |
| 2506 | case JDWP::JT_CLASS_OBJECT: |
| 2507 | case JDWP::JT_OBJECT: |
| 2508 | case JDWP::JT_STRING: |
| 2509 | case JDWP::JT_THREAD: |
| 2510 | case JDWP::JT_THREAD_GROUP: { |
| 2511 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
| 2512 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2513 | if (!visitor.GetVReg(m, vreg, kReferenceVReg, &intVal)) { |
| 2514 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2515 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2516 | mirror::Object* o = reinterpret_cast<mirror::Object*>(intVal); |
| 2517 | VLOG(jdwp) << "get " << tag << " object local " << vreg << " = " << o; |
| 2518 | if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) { |
| 2519 | LOG(FATAL) << StringPrintf("Found invalid object %#" PRIxPTR " in register v%u", |
| 2520 | reinterpret_cast<uintptr_t>(o), vreg) |
| 2521 | << GetStackContextAsString(visitor); |
| 2522 | UNREACHABLE(); |
| 2523 | } |
| 2524 | tag = TagFromObject(soa, o); |
| 2525 | JDWP::SetObjectId(buf + 1, gRegistry->Add(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2526 | break; |
| 2527 | } |
| 2528 | case JDWP::JT_DOUBLE: { |
| 2529 | CHECK_EQ(width, 8U); |
| 2530 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2531 | if (!visitor.GetVRegPair(m, vreg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) { |
| 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 double local " << vreg << " = " << longVal; |
| 2535 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2536 | break; |
| 2537 | } |
| 2538 | case JDWP::JT_LONG: { |
| 2539 | CHECK_EQ(width, 8U); |
| 2540 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2541 | if (!visitor.GetVRegPair(m, vreg, kLongLoVReg, kLongHiVReg, &longVal)) { |
| 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 long local " << vreg << " = " << longVal; |
| 2545 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2546 | break; |
| 2547 | } |
| 2548 | default: |
| 2549 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2550 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2551 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2552 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2553 | // Prepend tag, which may have been updated. |
| 2554 | JDWP::Set1(buf, tag); |
| 2555 | return JDWP::ERR_NONE; |
| 2556 | } |
| 2557 | |
| 2558 | JDWP::JdwpError Dbg::SetLocalValues(JDWP::Request* request) { |
| 2559 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2560 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2561 | |
| 2562 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2563 | JDWP::JdwpError error; |
| 2564 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2565 | if (error != JDWP::ERR_NONE) { |
| 2566 | return error; |
| 2567 | } |
| 2568 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2569 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2570 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2571 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2572 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2573 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2574 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2575 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2576 | return visitor.GetError(); |
| 2577 | } |
| 2578 | |
| 2579 | // Writes the values into visitor's context. |
| 2580 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2581 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2582 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2583 | JDWP::JdwpTag sigByte = request->ReadTag(); |
| 2584 | size_t width = Dbg::GetTagWidth(sigByte); |
| 2585 | uint64_t value = request->ReadValue(width); |
| 2586 | |
| 2587 | VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value; |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2588 | error = Dbg::SetLocalValue(visitor, slot, sigByte, value, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2589 | if (error != JDWP::ERR_NONE) { |
| 2590 | return error; |
| 2591 | } |
| 2592 | } |
| 2593 | return JDWP::ERR_NONE; |
| 2594 | } |
| 2595 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2596 | template<typename T> |
| 2597 | static JDWP::JdwpError FailSetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2598 | JDWP::JdwpTag tag, T value) |
| 2599 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2600 | LOG(ERROR) << "Failed to write " << tag << " local " << value |
| 2601 | << " (0x" << std::hex << value << ") into register v" << vreg |
| 2602 | << GetStackContextAsString(visitor); |
| 2603 | return kStackFrameLocalAccessError; |
| 2604 | } |
| 2605 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2606 | JDWP::JdwpError Dbg::SetLocalValue(StackVisitor& visitor, int slot, JDWP::JdwpTag tag, |
| 2607 | uint64_t value, size_t width) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2608 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2609 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2610 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2611 | if (error != JDWP::ERR_NONE) { |
| 2612 | return error; |
| 2613 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2614 | // 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] | 2615 | switch (tag) { |
| 2616 | case JDWP::JT_BOOLEAN: |
| 2617 | case JDWP::JT_BYTE: |
| 2618 | CHECK_EQ(width, 1U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2619 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2620 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2621 | } |
| 2622 | break; |
| 2623 | case JDWP::JT_SHORT: |
| 2624 | case JDWP::JT_CHAR: |
| 2625 | CHECK_EQ(width, 2U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2626 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2627 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2628 | } |
| 2629 | break; |
| 2630 | case JDWP::JT_INT: |
| 2631 | CHECK_EQ(width, 4U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2632 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2633 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2634 | } |
| 2635 | break; |
| 2636 | case JDWP::JT_FLOAT: |
| 2637 | CHECK_EQ(width, 4U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2638 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kFloatVReg)) { |
| 2639 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2640 | } |
| 2641 | break; |
| 2642 | case JDWP::JT_ARRAY: |
| 2643 | case JDWP::JT_CLASS_LOADER: |
| 2644 | case JDWP::JT_CLASS_OBJECT: |
| 2645 | case JDWP::JT_OBJECT: |
| 2646 | case JDWP::JT_STRING: |
| 2647 | case JDWP::JT_THREAD: |
| 2648 | case JDWP::JT_THREAD_GROUP: { |
| 2649 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2650 | mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value), |
| 2651 | &error); |
| 2652 | if (error != JDWP::ERR_NONE) { |
| 2653 | VLOG(jdwp) << tag << " object " << o << " is an invalid object"; |
| 2654 | return JDWP::ERR_INVALID_OBJECT; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2655 | } |
| 2656 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), |
| 2657 | kReferenceVReg)) { |
| 2658 | return FailSetLocalValue(visitor, vreg, tag, reinterpret_cast<uintptr_t>(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2659 | } |
| 2660 | break; |
| 2661 | } |
| 2662 | case JDWP::JT_DOUBLE: { |
| 2663 | CHECK_EQ(width, 8U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2664 | if (!visitor.SetVRegPair(m, vreg, value, kDoubleLoVReg, kDoubleHiVReg)) { |
| 2665 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2666 | } |
| 2667 | break; |
| 2668 | } |
| 2669 | case JDWP::JT_LONG: { |
| 2670 | CHECK_EQ(width, 8U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2671 | if (!visitor.SetVRegPair(m, vreg, value, kLongLoVReg, kLongHiVReg)) { |
| 2672 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2673 | } |
| 2674 | break; |
| 2675 | } |
| 2676 | default: |
| 2677 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2678 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2679 | } |
| 2680 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2681 | } |
| 2682 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2683 | static void SetEventLocation(JDWP::EventLocation* location, ArtMethod* m, uint32_t dex_pc) |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2684 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2685 | DCHECK(location != nullptr); |
| 2686 | if (m == nullptr) { |
| 2687 | memset(location, 0, sizeof(*location)); |
| 2688 | } else { |
| 2689 | location->method = m; |
| 2690 | 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] | 2691 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2692 | } |
| 2693 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2694 | void Dbg::PostLocationEvent(ArtMethod* m, int dex_pc, mirror::Object* this_object, |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2695 | int event_flags, const JValue* return_value) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2696 | if (!IsDebuggerActive()) { |
| 2697 | return; |
| 2698 | } |
| 2699 | DCHECK(m != nullptr); |
| 2700 | DCHECK_EQ(m->IsStatic(), this_object == nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2701 | JDWP::EventLocation location; |
| 2702 | SetEventLocation(&location, m, dex_pc); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2703 | |
Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2704 | // We need to be sure no exception is pending when calling JdwpState::PostLocationEvent. |
| 2705 | // This is required to be able to call JNI functions to create JDWP ids. To achieve this, |
| 2706 | // we temporarily clear the current thread's exception (if any) and will restore it after |
| 2707 | // the call. |
| 2708 | // Note: the only way to get a pending exception here is to suspend on a move-exception |
| 2709 | // instruction. |
| 2710 | Thread* const self = Thread::Current(); |
| 2711 | StackHandleScope<1> hs(self); |
| 2712 | Handle<mirror::Throwable> pending_exception(hs.NewHandle(self->GetException())); |
| 2713 | self->ClearException(); |
| 2714 | if (kIsDebugBuild && pending_exception.Get() != nullptr) { |
| 2715 | const DexFile::CodeItem* code_item = location.method->GetCodeItem(); |
| 2716 | const Instruction* instr = Instruction::At(&code_item->insns_[location.dex_pc]); |
| 2717 | CHECK_EQ(Instruction::MOVE_EXCEPTION, instr->Opcode()); |
| 2718 | } |
| 2719 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2720 | gJdwpState->PostLocationEvent(&location, this_object, event_flags, return_value); |
Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2721 | |
| 2722 | if (pending_exception.Get() != nullptr) { |
| 2723 | self->SetException(pending_exception.Get()); |
| 2724 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2725 | } |
| 2726 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2727 | void Dbg::PostFieldAccessEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2728 | mirror::Object* this_object, ArtField* f) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2729 | if (!IsDebuggerActive()) { |
| 2730 | return; |
| 2731 | } |
| 2732 | DCHECK(m != nullptr); |
| 2733 | DCHECK(f != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2734 | JDWP::EventLocation location; |
| 2735 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2736 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2737 | gJdwpState->PostFieldEvent(&location, f, this_object, nullptr, false); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2738 | } |
| 2739 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2740 | void Dbg::PostFieldModificationEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2741 | mirror::Object* this_object, ArtField* f, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2742 | const JValue* field_value) { |
| 2743 | if (!IsDebuggerActive()) { |
| 2744 | return; |
| 2745 | } |
| 2746 | DCHECK(m != nullptr); |
| 2747 | DCHECK(f != nullptr); |
| 2748 | DCHECK(field_value != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2749 | JDWP::EventLocation location; |
| 2750 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2751 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2752 | gJdwpState->PostFieldEvent(&location, f, this_object, field_value, true); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2753 | } |
| 2754 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2755 | /** |
| 2756 | * Finds the location where this exception will be caught. We search until we reach the top |
| 2757 | * frame, in which case this exception is considered uncaught. |
| 2758 | */ |
| 2759 | class CatchLocationFinder : public StackVisitor { |
| 2760 | public: |
| 2761 | CatchLocationFinder(Thread* self, const Handle<mirror::Throwable>& exception, Context* context) |
| 2762 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2763 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2764 | self_(self), |
| 2765 | exception_(exception), |
| 2766 | handle_scope_(self), |
| 2767 | this_at_throw_(handle_scope_.NewHandle<mirror::Object>(nullptr)), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2768 | catch_method_(nullptr), |
| 2769 | throw_method_(nullptr), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2770 | catch_dex_pc_(DexFile::kDexNoIndex), |
| 2771 | throw_dex_pc_(DexFile::kDexNoIndex) { |
| 2772 | } |
| 2773 | |
| 2774 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2775 | ArtMethod* method = GetMethod(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2776 | DCHECK(method != nullptr); |
| 2777 | if (method->IsRuntimeMethod()) { |
| 2778 | // Ignore callee save method. |
| 2779 | DCHECK(method->IsCalleeSaveMethod()); |
| 2780 | return true; |
| 2781 | } |
| 2782 | |
| 2783 | uint32_t dex_pc = GetDexPc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2784 | if (throw_method_ == nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2785 | // First Java method found. It is either the method that threw the exception, |
| 2786 | // or the Java native method that is reporting an exception thrown by |
| 2787 | // native code. |
| 2788 | this_at_throw_.Assign(GetThisObject()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2789 | throw_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2790 | throw_dex_pc_ = dex_pc; |
| 2791 | } |
| 2792 | |
| 2793 | if (dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2794 | StackHandleScope<1> hs(self_); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2795 | uint32_t found_dex_pc; |
| 2796 | Handle<mirror::Class> exception_class(hs.NewHandle(exception_->GetClass())); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2797 | bool unused_clear_exception; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2798 | found_dex_pc = method->FindCatchBlock(exception_class, dex_pc, &unused_clear_exception); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2799 | if (found_dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2800 | catch_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2801 | catch_dex_pc_ = found_dex_pc; |
| 2802 | return false; // End stack walk. |
| 2803 | } |
| 2804 | } |
| 2805 | return true; // Continue stack walk. |
| 2806 | } |
| 2807 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2808 | ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2809 | return catch_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2812 | ArtMethod* GetThrowMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2813 | return throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | mirror::Object* GetThisAtThrow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2817 | return this_at_throw_.Get(); |
| 2818 | } |
| 2819 | |
| 2820 | uint32_t GetCatchDexPc() const { |
| 2821 | return catch_dex_pc_; |
| 2822 | } |
| 2823 | |
| 2824 | uint32_t GetThrowDexPc() const { |
| 2825 | return throw_dex_pc_; |
| 2826 | } |
| 2827 | |
| 2828 | private: |
| 2829 | Thread* const self_; |
| 2830 | const Handle<mirror::Throwable>& exception_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2831 | StackHandleScope<1> handle_scope_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2832 | MutableHandle<mirror::Object> this_at_throw_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2833 | ArtMethod* catch_method_; |
| 2834 | ArtMethod* throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2835 | uint32_t catch_dex_pc_; |
| 2836 | uint32_t throw_dex_pc_; |
| 2837 | |
| 2838 | DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder); |
| 2839 | }; |
| 2840 | |
| 2841 | void Dbg::PostException(mirror::Throwable* exception_object) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2842 | if (!IsDebuggerActive()) { |
Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 2843 | return; |
| 2844 | } |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 2845 | Thread* const self = Thread::Current(); |
| 2846 | StackHandleScope<1> handle_scope(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2847 | Handle<mirror::Throwable> h_exception(handle_scope.NewHandle(exception_object)); |
| 2848 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 2849 | CatchLocationFinder clf(self, h_exception, context.get()); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2850 | clf.WalkStack(/* include_transitions */ false); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2851 | JDWP::EventLocation exception_throw_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2852 | SetEventLocation(&exception_throw_location, clf.GetThrowMethod(), clf.GetThrowDexPc()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2853 | JDWP::EventLocation exception_catch_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2854 | SetEventLocation(&exception_catch_location, clf.GetCatchMethod(), clf.GetCatchDexPc()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2855 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2856 | gJdwpState->PostException(&exception_throw_location, h_exception.Get(), &exception_catch_location, |
| 2857 | clf.GetThisAtThrow()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2858 | } |
| 2859 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2860 | void Dbg::PostClassPrepare(mirror::Class* c) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2861 | if (!IsDebuggerActive()) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2862 | return; |
| 2863 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2864 | gJdwpState->PostClassPrepare(c); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2865 | } |
| 2866 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2867 | void Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2868 | ArtMethod* m, uint32_t dex_pc, |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 2869 | int event_flags, const JValue* return_value) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2870 | if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 2871 | return; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2872 | } |
| 2873 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2874 | if (IsBreakpoint(m, dex_pc)) { |
| 2875 | event_flags |= kBreakpoint; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2876 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2877 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2878 | // If the debugger is single-stepping one of our threads, check to |
| 2879 | // see if we're that thread and we've reached a step point. |
| 2880 | const SingleStepControl* single_step_control = thread->GetSingleStepControl(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2881 | if (single_step_control != nullptr) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2882 | CHECK(!m->IsNative()); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2883 | if (single_step_control->GetStepDepth() == JDWP::SD_INTO) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2884 | // Step into method calls. We break when the line number |
| 2885 | // or method pointer changes. If we're in SS_MIN mode, we |
| 2886 | // always stop. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2887 | if (single_step_control->GetMethod() != m) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2888 | event_flags |= kSingleStep; |
| 2889 | VLOG(jdwp) << "SS new method"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2890 | } else if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2891 | event_flags |= kSingleStep; |
| 2892 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 2893 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2894 | event_flags |= kSingleStep; |
| 2895 | VLOG(jdwp) << "SS new line"; |
| 2896 | } |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2897 | } else if (single_step_control->GetStepDepth() == JDWP::SD_OVER) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2898 | // Step over method calls. We break when the line number is |
| 2899 | // different and the frame depth is <= the original frame |
| 2900 | // depth. (We can't just compare on the method, because we |
| 2901 | // might get unrolled past it by an exception, and it's tricky |
| 2902 | // to identify recursion.) |
| 2903 | |
| 2904 | int stack_depth = GetStackDepth(thread); |
| 2905 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2906 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2907 | // Popped up one or more frames, always trigger. |
| 2908 | event_flags |= kSingleStep; |
| 2909 | VLOG(jdwp) << "SS method pop"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2910 | } else if (stack_depth == single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2911 | // Same depth, see if we moved. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2912 | if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2913 | event_flags |= kSingleStep; |
| 2914 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 2915 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2916 | event_flags |= kSingleStep; |
| 2917 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2918 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2919 | } |
| 2920 | } else { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2921 | CHECK_EQ(single_step_control->GetStepDepth(), JDWP::SD_OUT); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2922 | // Return from the current method. We break when the frame |
| 2923 | // depth pops up. |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2924 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2925 | // This differs from the "method exit" break in that it stops |
| 2926 | // with the PC at the next instruction in the returned-to |
| 2927 | // function, rather than the end of the returning function. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2928 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2929 | int stack_depth = GetStackDepth(thread); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2930 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2931 | event_flags |= kSingleStep; |
| 2932 | VLOG(jdwp) << "SS method pop"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2933 | } |
| 2934 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2935 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2936 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2937 | // If there's something interesting going on, see if it matches one |
| 2938 | // of the debugger filters. |
| 2939 | if (event_flags != 0) { |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 2940 | Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 2944 | size_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) { |
| 2945 | switch (instrumentation_event) { |
| 2946 | case instrumentation::Instrumentation::kMethodEntered: |
| 2947 | return &method_enter_event_ref_count_; |
| 2948 | case instrumentation::Instrumentation::kMethodExited: |
| 2949 | return &method_exit_event_ref_count_; |
| 2950 | case instrumentation::Instrumentation::kDexPcMoved: |
| 2951 | return &dex_pc_change_event_ref_count_; |
| 2952 | case instrumentation::Instrumentation::kFieldRead: |
| 2953 | return &field_read_event_ref_count_; |
| 2954 | case instrumentation::Instrumentation::kFieldWritten: |
| 2955 | return &field_write_event_ref_count_; |
| 2956 | case instrumentation::Instrumentation::kExceptionCaught: |
| 2957 | return &exception_catch_event_ref_count_; |
| 2958 | default: |
| 2959 | return nullptr; |
| 2960 | } |
| 2961 | } |
| 2962 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 2963 | // Process request while all mutator threads are suspended. |
| 2964 | void Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 2965 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 2966 | switch (request.GetKind()) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 2967 | case DeoptimizationRequest::kNothing: |
| 2968 | LOG(WARNING) << "Ignoring empty deoptimization request."; |
| 2969 | break; |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 2970 | case DeoptimizationRequest::kRegisterForEvent: |
| 2971 | VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 2972 | request.InstrumentationEvent()); |
| 2973 | instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent()); |
| 2974 | instrumentation_events_ |= request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 2975 | break; |
| 2976 | case DeoptimizationRequest::kUnregisterForEvent: |
| 2977 | VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 2978 | request.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 2979 | instrumentation->RemoveListener(&gDebugInstrumentationListener, |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 2980 | request.InstrumentationEvent()); |
| 2981 | instrumentation_events_ &= ~request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 2982 | break; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 2983 | case DeoptimizationRequest::kFullDeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 2984 | VLOG(jdwp) << "Deoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 2985 | instrumentation->DeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 2986 | VLOG(jdwp) << "Deoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 2987 | break; |
| 2988 | case DeoptimizationRequest::kFullUndeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 2989 | VLOG(jdwp) << "Undeoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 2990 | instrumentation->UndeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 2991 | VLOG(jdwp) << "Undeoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 2992 | break; |
| 2993 | case DeoptimizationRequest::kSelectiveDeoptimization: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 2994 | VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " ..."; |
| 2995 | instrumentation->Deoptimize(request.Method()); |
| 2996 | VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 2997 | break; |
| 2998 | case DeoptimizationRequest::kSelectiveUndeoptimization: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 2999 | VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " ..."; |
| 3000 | instrumentation->Undeoptimize(request.Method()); |
| 3001 | VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3002 | break; |
| 3003 | default: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3004 | LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3005 | break; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3006 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3007 | } |
| 3008 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3009 | void Dbg::RequestDeoptimization(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3010 | if (req.GetKind() == DeoptimizationRequest::kNothing) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3011 | // Nothing to do. |
| 3012 | return; |
| 3013 | } |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3014 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3015 | RequestDeoptimizationLocked(req); |
| 3016 | } |
| 3017 | |
| 3018 | void Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3019 | switch (req.GetKind()) { |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3020 | case DeoptimizationRequest::kRegisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3021 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3022 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3023 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3024 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3025 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3026 | 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] | 3027 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3028 | deoptimization_requests_.push_back(req); |
| 3029 | } |
| 3030 | *counter = *counter + 1; |
| 3031 | break; |
| 3032 | } |
| 3033 | case DeoptimizationRequest::kUnregisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3034 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3035 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3036 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3037 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3038 | *counter = *counter - 1; |
| 3039 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3040 | 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] | 3041 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3042 | deoptimization_requests_.push_back(req); |
| 3043 | } |
| 3044 | break; |
| 3045 | } |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3046 | case DeoptimizationRequest::kFullDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3047 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3048 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3049 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3050 | << " for full deoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3051 | deoptimization_requests_.push_back(req); |
| 3052 | } |
| 3053 | ++full_deoptimization_event_count_; |
| 3054 | break; |
| 3055 | } |
| 3056 | case DeoptimizationRequest::kFullUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3057 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | e713d93 | 2014-05-15 10:48:53 +0200 | [diff] [blame] | 3058 | DCHECK_GT(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3059 | --full_deoptimization_event_count_; |
| 3060 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3061 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3062 | << " for full undeoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3063 | deoptimization_requests_.push_back(req); |
| 3064 | } |
| 3065 | break; |
| 3066 | } |
| 3067 | case DeoptimizationRequest::kSelectiveDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3068 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3069 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3070 | << " for deoptimization of " << PrettyMethod(req.Method()); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3071 | deoptimization_requests_.push_back(req); |
| 3072 | break; |
| 3073 | } |
| 3074 | case DeoptimizationRequest::kSelectiveUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3075 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3076 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3077 | << " for undeoptimization of " << PrettyMethod(req.Method()); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3078 | deoptimization_requests_.push_back(req); |
| 3079 | break; |
| 3080 | } |
| 3081 | default: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3082 | LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3083 | break; |
| 3084 | } |
| 3085 | } |
| 3086 | } |
| 3087 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3088 | void Dbg::ManageDeoptimization() { |
| 3089 | Thread* const self = Thread::Current(); |
| 3090 | { |
| 3091 | // Avoid suspend/resume if there is no pending request. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3092 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3093 | if (deoptimization_requests_.empty()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3094 | return; |
| 3095 | } |
| 3096 | } |
| 3097 | CHECK_EQ(self->GetState(), kRunnable); |
| 3098 | self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization); |
| 3099 | // We need to suspend mutator threads first. |
| 3100 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 3101 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3102 | const ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3103 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3104 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3105 | size_t req_index = 0; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3106 | for (DeoptimizationRequest& request : deoptimization_requests_) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3107 | VLOG(jdwp) << "Process deoptimization request #" << req_index++; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3108 | ProcessDeoptimizationRequest(request); |
| 3109 | } |
| 3110 | deoptimization_requests_.clear(); |
| 3111 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3112 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 3113 | runtime->GetThreadList()->ResumeAll(); |
| 3114 | self->TransitionFromSuspendedToRunnable(); |
| 3115 | } |
| 3116 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3117 | static bool IsMethodPossiblyInlined(Thread* self, ArtMethod* m) |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3118 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3119 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3120 | if (code_item == nullptr) { |
| 3121 | // TODO We should not be asked to watch location in a native or abstract method so the code item |
| 3122 | // should never be null. We could just check we never encounter this case. |
| 3123 | return false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3124 | } |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3125 | // Note: method verifier may cause thread suspension. |
| 3126 | self->AssertThreadSuspensionIsAllowable(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3127 | StackHandleScope<2> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3128 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 3129 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 3130 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3131 | verifier::MethodVerifier verifier(self, dex_cache->GetDexFile(), dex_cache, class_loader, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3132 | &m->GetClassDef(), code_item, m->GetDexMethodIndex(), m, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 3133 | m->GetAccessFlags(), false, true, false, true); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3134 | // Note: we don't need to verify the method. |
| 3135 | return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr); |
| 3136 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3137 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3138 | static const Breakpoint* FindFirstBreakpointForMethod(ArtMethod* m) |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3139 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3140 | for (Breakpoint& breakpoint : gBreakpoints) { |
| 3141 | if (breakpoint.Method() == m) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3142 | return &breakpoint; |
| 3143 | } |
| 3144 | } |
| 3145 | return nullptr; |
| 3146 | } |
| 3147 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3148 | bool Dbg::MethodHasAnyBreakpoints(ArtMethod* method) { |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 3149 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| 3150 | return FindFirstBreakpointForMethod(method) != nullptr; |
| 3151 | } |
| 3152 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3153 | // Sanity checks all existing breakpoints on the same method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3154 | static void SanityCheckExistingBreakpoints(ArtMethod* m, |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3155 | DeoptimizationRequest::Kind deoptimization_kind) |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3156 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3157 | for (const Breakpoint& breakpoint : gBreakpoints) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3158 | if (breakpoint.Method() == m) { |
| 3159 | CHECK_EQ(deoptimization_kind, breakpoint.GetDeoptimizationKind()); |
| 3160 | } |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3161 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3162 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 3163 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3164 | // We should have deoptimized everything but not "selectively" deoptimized this method. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3165 | CHECK(instrumentation->AreAllMethodsDeoptimized()); |
| 3166 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3167 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3168 | // We should have "selectively" deoptimized this method. |
| 3169 | // Note: while we have not deoptimized everything for this method, we may have done it for |
| 3170 | // another event. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3171 | CHECK(instrumentation->IsDeoptimized(m)); |
| 3172 | } else { |
| 3173 | // This method does not require deoptimization. |
| 3174 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3175 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3176 | } |
| 3177 | } |
| 3178 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3179 | // Returns the deoptimization kind required to set a breakpoint in a method. |
| 3180 | // If a breakpoint has already been set, we also return the first breakpoint |
| 3181 | // through the given 'existing_brkpt' pointer. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3182 | static DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3183 | ArtMethod* m, |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3184 | const Breakpoint** existing_brkpt) |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3185 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3186 | if (!Dbg::RequiresDeoptimization()) { |
| 3187 | // We already run in interpreter-only mode so we don't need to deoptimize anything. |
| 3188 | VLOG(jdwp) << "No need for deoptimization when fully running with interpreter for method " |
| 3189 | << PrettyMethod(m); |
| 3190 | return DeoptimizationRequest::kNothing; |
| 3191 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3192 | const Breakpoint* first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3193 | { |
| 3194 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3195 | first_breakpoint = FindFirstBreakpointForMethod(m); |
| 3196 | *existing_brkpt = first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3197 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3198 | |
| 3199 | if (first_breakpoint == nullptr) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3200 | // There is no breakpoint on this method yet: we need to deoptimize. If this method may be |
| 3201 | // inlined, we deoptimize everything; otherwise we deoptimize only this method. |
| 3202 | // Note: IsMethodPossiblyInlined goes into the method verifier and may cause thread suspension. |
| 3203 | // Therefore we must not hold any lock when we call it. |
| 3204 | bool need_full_deoptimization = IsMethodPossiblyInlined(self, m); |
| 3205 | if (need_full_deoptimization) { |
| 3206 | VLOG(jdwp) << "Need full deoptimization because of possible inlining of method " |
| 3207 | << PrettyMethod(m); |
| 3208 | return DeoptimizationRequest::kFullDeoptimization; |
| 3209 | } else { |
| 3210 | // We don't need to deoptimize if the method has not been compiled. |
| 3211 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 3212 | const bool is_compiled = class_linker->GetOatMethodQuickCodeFor(m) != nullptr; |
| 3213 | if (is_compiled) { |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 3214 | // If the method may be called through its direct code pointer (without loading |
| 3215 | // its updated entrypoint), we need full deoptimization to not miss the breakpoint. |
| 3216 | if (class_linker->MayBeCalledWithDirectCodePointer(m)) { |
| 3217 | VLOG(jdwp) << "Need full deoptimization because of possible direct code call " |
| 3218 | << "into image for compiled method " << PrettyMethod(m); |
| 3219 | return DeoptimizationRequest::kFullDeoptimization; |
| 3220 | } else { |
| 3221 | VLOG(jdwp) << "Need selective deoptimization for compiled method " << PrettyMethod(m); |
| 3222 | return DeoptimizationRequest::kSelectiveDeoptimization; |
| 3223 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3224 | } else { |
| 3225 | // Method is not compiled: we don't need to deoptimize. |
| 3226 | VLOG(jdwp) << "No need for deoptimization for non-compiled method " << PrettyMethod(m); |
| 3227 | return DeoptimizationRequest::kNothing; |
| 3228 | } |
| 3229 | } |
| 3230 | } else { |
| 3231 | // There is at least one breakpoint for this method: we don't need to deoptimize. |
| 3232 | // Let's check that all breakpoints are configured the same way for deoptimization. |
| 3233 | VLOG(jdwp) << "Breakpoint already set: no deoptimization is required"; |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3234 | DeoptimizationRequest::Kind deoptimization_kind = first_breakpoint->GetDeoptimizationKind(); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3235 | if (kIsDebugBuild) { |
| 3236 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
| 3237 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
| 3238 | } |
| 3239 | return DeoptimizationRequest::kNothing; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3240 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3241 | } |
| 3242 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3243 | // Installs a breakpoint at the specified location. Also indicates through the deoptimization |
| 3244 | // request if we need to deoptimize. |
| 3245 | void Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
| 3246 | Thread* const self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3247 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3248 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3249 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3250 | const Breakpoint* existing_breakpoint = nullptr; |
| 3251 | const DeoptimizationRequest::Kind deoptimization_kind = |
| 3252 | GetRequiredDeoptimizationKind(self, m, &existing_breakpoint); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3253 | req->SetKind(deoptimization_kind); |
| 3254 | if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
| 3255 | req->SetMethod(m); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3256 | } else { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3257 | CHECK(deoptimization_kind == DeoptimizationRequest::kNothing || |
| 3258 | deoptimization_kind == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3259 | req->SetMethod(nullptr); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3260 | } |
| 3261 | |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3262 | { |
| 3263 | WriterMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3264 | // If there is at least one existing breakpoint on the same method, the new breakpoint |
| 3265 | // must have the same deoptimization kind than the existing breakpoint(s). |
| 3266 | DeoptimizationRequest::Kind breakpoint_deoptimization_kind; |
| 3267 | if (existing_breakpoint != nullptr) { |
| 3268 | breakpoint_deoptimization_kind = existing_breakpoint->GetDeoptimizationKind(); |
| 3269 | } else { |
| 3270 | breakpoint_deoptimization_kind = deoptimization_kind; |
| 3271 | } |
| 3272 | gBreakpoints.push_back(Breakpoint(m, location->dex_pc, breakpoint_deoptimization_kind)); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3273 | VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " |
| 3274 | << gBreakpoints[gBreakpoints.size() - 1]; |
| 3275 | } |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | // Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization |
| 3279 | // request if we need to undeoptimize. |
| 3280 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3281 | WriterMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3282 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3283 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3284 | DeoptimizationRequest::Kind deoptimization_kind = DeoptimizationRequest::kNothing; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3285 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3286 | if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].Method() == m) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3287 | VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i]; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3288 | deoptimization_kind = gBreakpoints[i].GetDeoptimizationKind(); |
| 3289 | DCHECK_EQ(deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization, |
| 3290 | Runtime::Current()->GetInstrumentation()->IsDeoptimized(m)); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3291 | gBreakpoints.erase(gBreakpoints.begin() + i); |
| 3292 | break; |
| 3293 | } |
| 3294 | } |
| 3295 | const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m); |
| 3296 | if (existing_breakpoint == nullptr) { |
| 3297 | // There is no more breakpoint on this method: we need to undeoptimize. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3298 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3299 | // This method required full deoptimization: we need to undeoptimize everything. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3300 | req->SetKind(DeoptimizationRequest::kFullUndeoptimization); |
| 3301 | req->SetMethod(nullptr); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3302 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3303 | // This method required selective deoptimization: we need to undeoptimize only that method. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3304 | req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization); |
| 3305 | req->SetMethod(m); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3306 | } else { |
| 3307 | // This method had no need for deoptimization: do nothing. |
| 3308 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3309 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3310 | req->SetMethod(nullptr); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3311 | } |
| 3312 | } else { |
| 3313 | // 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] | 3314 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3315 | req->SetMethod(nullptr); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3316 | if (kIsDebugBuild) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3317 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3318 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3319 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3320 | } |
| 3321 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3322 | bool Dbg::IsForcedInterpreterNeededForCallingImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3323 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3324 | if (ssc == nullptr) { |
| 3325 | // If we are not single-stepping, then we don't have to force interpreter. |
| 3326 | return false; |
| 3327 | } |
| 3328 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
| 3329 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3330 | return false; |
| 3331 | } |
| 3332 | |
| 3333 | if (!m->IsNative() && !m->IsProxyMethod()) { |
| 3334 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3335 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3336 | return true; |
| 3337 | } |
| 3338 | } |
| 3339 | return false; |
| 3340 | } |
| 3341 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3342 | bool Dbg::IsForcedInterpreterNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3343 | instrumentation::Instrumentation* const instrumentation = |
| 3344 | Runtime::Current()->GetInstrumentation(); |
| 3345 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3346 | if (instrumentation->InterpretOnly()) { |
| 3347 | return false; |
| 3348 | } |
| 3349 | // We can only interpret pure Java method. |
| 3350 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3351 | return false; |
| 3352 | } |
| 3353 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3354 | if (ssc != nullptr) { |
| 3355 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3356 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3357 | return true; |
| 3358 | } |
| 3359 | // If we are stepping out from a static initializer, by issuing a step |
| 3360 | // in or step over, that was implicitly invoked by calling a static method, |
| 3361 | // then we need to step into that method. Having a lower stack depth than |
| 3362 | // the one the single step control has indicates that the step originates |
| 3363 | // from the static initializer. |
| 3364 | if (ssc->GetStepDepth() != JDWP::SD_OUT && |
| 3365 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3366 | return true; |
| 3367 | } |
| 3368 | } |
| 3369 | // There are cases where we have to force interpreter on deoptimized methods, |
| 3370 | // because in some cases the call will not be performed by invoking an entry |
| 3371 | // point that has been replaced by the deoptimization, but instead by directly |
| 3372 | // invoking the compiled code of the method, for example. |
| 3373 | return instrumentation->IsDeoptimized(m); |
| 3374 | } |
| 3375 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3376 | bool Dbg::IsForcedInstrumentationNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3377 | // 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] | 3378 | if (m == nullptr) { |
| 3379 | return false; |
| 3380 | } |
| 3381 | instrumentation::Instrumentation* const instrumentation = |
| 3382 | Runtime::Current()->GetInstrumentation(); |
| 3383 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3384 | if (instrumentation->InterpretOnly()) { |
| 3385 | return false; |
| 3386 | } |
| 3387 | // We can only interpret pure Java method. |
| 3388 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3389 | return false; |
| 3390 | } |
| 3391 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3392 | if (ssc != nullptr) { |
| 3393 | // If we are stepping out from a static initializer, by issuing a step |
| 3394 | // out, that was implicitly invoked by calling a static method, then we |
| 3395 | // need to step into the caller of that method. Having a lower stack |
| 3396 | // depth than the one the single step control has indicates that the |
| 3397 | // step originates from the static initializer. |
| 3398 | if (ssc->GetStepDepth() == JDWP::SD_OUT && |
| 3399 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3400 | return true; |
| 3401 | } |
| 3402 | } |
| 3403 | // If we are returning from a static intializer, that was implicitly |
| 3404 | // invoked by calling a static method and the caller is deoptimized, |
| 3405 | // then we have to deoptimize the stack without forcing interpreter |
| 3406 | // on the static method that was called originally. This problem can |
| 3407 | // be solved easily by forcing instrumentation on the called method, |
| 3408 | // because the instrumentation exit hook will recognise the need of |
| 3409 | // stack deoptimization by calling IsForcedInterpreterNeededForUpcall. |
| 3410 | return instrumentation->IsDeoptimized(m); |
| 3411 | } |
| 3412 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3413 | bool Dbg::IsForcedInterpreterNeededForUpcallImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3414 | // 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] | 3415 | if (m == nullptr) { |
| 3416 | return false; |
| 3417 | } |
| 3418 | instrumentation::Instrumentation* const instrumentation = |
| 3419 | Runtime::Current()->GetInstrumentation(); |
| 3420 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3421 | if (instrumentation->InterpretOnly()) { |
| 3422 | return false; |
| 3423 | } |
| 3424 | // We can only interpret pure Java method. |
| 3425 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3426 | return false; |
| 3427 | } |
| 3428 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3429 | if (ssc != nullptr) { |
| 3430 | // The debugger is not interested in what is happening under the level |
| 3431 | // of the step, thus we only force interpreter when we are not below of |
| 3432 | // the step. |
| 3433 | if (ssc->GetStackDepth() >= GetStackDepth(thread)) { |
| 3434 | return true; |
| 3435 | } |
| 3436 | } |
| 3437 | // We have to require stack deoptimization if the upcall is deoptimized. |
| 3438 | return instrumentation->IsDeoptimized(m); |
| 3439 | } |
| 3440 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3441 | // Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't |
| 3442 | // cause suspension if the thread is the current thread. |
| 3443 | class ScopedThreadSuspension { |
| 3444 | public: |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3445 | ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id) |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 3446 | LOCKS_EXCLUDED(Locks::thread_list_lock_) |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3447 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3448 | thread_(nullptr), |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3449 | error_(JDWP::ERR_NONE), |
| 3450 | self_suspend_(false), |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3451 | other_suspend_(false) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3452 | ScopedObjectAccessUnchecked soa(self); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 3453 | thread_ = DecodeThread(soa, thread_id, &error_); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3454 | if (error_ == JDWP::ERR_NONE) { |
| 3455 | if (thread_ == soa.Self()) { |
| 3456 | self_suspend_ = true; |
| 3457 | } else { |
| 3458 | soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3459 | jobject thread_peer = Dbg::GetObjectRegistry()->GetJObject(thread_id); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3460 | bool timed_out; |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 3461 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 3462 | Thread* suspended_thread = thread_list->SuspendThreadByPeer(thread_peer, true, true, |
| 3463 | &timed_out); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3464 | CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension); |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3465 | if (suspended_thread == nullptr) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3466 | // Thread terminated from under us while suspending. |
| 3467 | error_ = JDWP::ERR_INVALID_THREAD; |
| 3468 | } else { |
| 3469 | CHECK_EQ(suspended_thread, thread_); |
| 3470 | other_suspend_ = true; |
| 3471 | } |
| 3472 | } |
| 3473 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3474 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3475 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3476 | Thread* GetThread() const { |
| 3477 | return thread_; |
| 3478 | } |
| 3479 | |
| 3480 | JDWP::JdwpError GetError() const { |
| 3481 | return error_; |
| 3482 | } |
| 3483 | |
| 3484 | ~ScopedThreadSuspension() { |
| 3485 | if (other_suspend_) { |
| 3486 | Runtime::Current()->GetThreadList()->Resume(thread_, true); |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | private: |
| 3491 | Thread* thread_; |
| 3492 | JDWP::JdwpError error_; |
| 3493 | bool self_suspend_; |
| 3494 | bool other_suspend_; |
| 3495 | }; |
| 3496 | |
| 3497 | JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size, |
| 3498 | JDWP::JdwpStepDepth step_depth) { |
| 3499 | Thread* self = Thread::Current(); |
| 3500 | ScopedThreadSuspension sts(self, thread_id); |
| 3501 | if (sts.GetError() != JDWP::ERR_NONE) { |
| 3502 | return sts.GetError(); |
| 3503 | } |
| 3504 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3505 | // 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] | 3506 | // is for step-out. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3507 | struct SingleStepStackVisitor : public StackVisitor { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3508 | explicit SingleStepStackVisitor(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 3509 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 3510 | stack_depth(0), |
| 3511 | method(nullptr), |
| 3512 | line_number(-1) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 3513 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3514 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 3515 | // annotalysis. |
| 3516 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3517 | ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3518 | if (!m->IsRuntimeMethod()) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3519 | ++stack_depth; |
| 3520 | if (method == nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3521 | mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3522 | method = m; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3523 | if (dex_cache != nullptr) { |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 3524 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3525 | line_number = dex_file.GetLineNumFromPC(m, GetDexPc()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3526 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3527 | } |
| 3528 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 3529 | return true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3530 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3531 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3532 | int stack_depth; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3533 | ArtMethod* method; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3534 | int32_t line_number; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3535 | }; |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3536 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3537 | Thread* const thread = sts.GetThread(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3538 | SingleStepStackVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3539 | visitor.WalkStack(); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3540 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3541 | // 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] | 3542 | struct DebugCallbackContext { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3543 | explicit DebugCallbackContext(SingleStepControl* single_step_control_cb, |
| 3544 | int32_t line_number_cb, const DexFile::CodeItem* code_item) |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3545 | : single_step_control_(single_step_control_cb), line_number_(line_number_cb), |
| 3546 | code_item_(code_item), last_pc_valid(false), last_pc(0) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3547 | } |
| 3548 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3549 | 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] | 3550 | DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3551 | if (static_cast<int32_t>(line_number_cb) == context->line_number_) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3552 | if (!context->last_pc_valid) { |
| 3553 | // Everything from this address until the next line change is ours. |
| 3554 | context->last_pc = address; |
| 3555 | context->last_pc_valid = true; |
| 3556 | } |
| 3557 | // Otherwise, if we're already in a valid range for this line, |
| 3558 | // just keep going (shouldn't really happen)... |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3559 | } else if (context->last_pc_valid) { // and the line number is new |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3560 | // Add everything from the last entry up until here to the set |
| 3561 | 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] | 3562 | context->single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3563 | } |
| 3564 | context->last_pc_valid = false; |
| 3565 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3566 | return false; // There may be multiple entries for any given line. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3567 | } |
| 3568 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3569 | ~DebugCallbackContext() { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3570 | // If the line number was the last in the position table... |
| 3571 | if (last_pc_valid) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3572 | size_t end = code_item_->insns_size_in_code_units_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3573 | for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3574 | single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3575 | } |
| 3576 | } |
| 3577 | } |
| 3578 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3579 | SingleStepControl* const single_step_control_; |
| 3580 | const int32_t line_number_; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3581 | const DexFile::CodeItem* const code_item_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3582 | bool last_pc_valid; |
| 3583 | uint32_t last_pc; |
| 3584 | }; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3585 | |
| 3586 | // Allocate single step. |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3587 | SingleStepControl* single_step_control = |
| 3588 | new (std::nothrow) SingleStepControl(step_size, step_depth, |
| 3589 | visitor.stack_depth, visitor.method); |
| 3590 | if (single_step_control == nullptr) { |
| 3591 | LOG(ERROR) << "Failed to allocate SingleStepControl"; |
| 3592 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3593 | } |
| 3594 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3595 | ArtMethod* m = single_step_control->GetMethod(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3596 | const int32_t line_number = visitor.line_number; |
Sebastien Hertz | 52f5f93 | 2015-05-28 11:00:57 +0200 | [diff] [blame] | 3597 | // Note: if the thread is not running Java code (pure native thread), there is no "current" |
| 3598 | // method on the stack (and no line number either). |
| 3599 | if (m != nullptr && !m->IsNative()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3600 | const DexFile::CodeItem* const code_item = m->GetCodeItem(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3601 | DebugCallbackContext context(single_step_control, line_number, code_item); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3602 | m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3603 | DebugCallbackContext::Callback, nullptr, &context); |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 3604 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3605 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3606 | // Activate single-step in the thread. |
| 3607 | thread->ActivateSingleStepControl(single_step_control); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3608 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3609 | if (VLOG_IS_ON(jdwp)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3610 | VLOG(jdwp) << "Single-step thread: " << *thread; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3611 | VLOG(jdwp) << "Single-step step size: " << single_step_control->GetStepSize(); |
| 3612 | VLOG(jdwp) << "Single-step step depth: " << single_step_control->GetStepDepth(); |
| 3613 | VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->GetMethod()); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3614 | VLOG(jdwp) << "Single-step current line: " << line_number; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3615 | VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->GetStackDepth(); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3616 | VLOG(jdwp) << "Single-step dex_pc values:"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3617 | for (uint32_t dex_pc : single_step_control->GetDexPcs()) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3618 | VLOG(jdwp) << StringPrintf(" %#x", dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3619 | } |
| 3620 | } |
| 3621 | |
| 3622 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3623 | } |
| 3624 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3625 | void Dbg::UnconfigureStep(JDWP::ObjectId thread_id) { |
| 3626 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3627 | JDWP::JdwpError error; |
| 3628 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Sebastien Hertz | 87118ed | 2013-11-26 17:57:18 +0100 | [diff] [blame] | 3629 | if (error == JDWP::ERR_NONE) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3630 | thread->DeactivateSingleStepControl(); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3631 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3634 | static char JdwpTagToShortyChar(JDWP::JdwpTag tag) { |
| 3635 | switch (tag) { |
| 3636 | default: |
| 3637 | LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag); |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 3638 | UNREACHABLE(); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3639 | |
| 3640 | // Primitives. |
| 3641 | case JDWP::JT_BYTE: return 'B'; |
| 3642 | case JDWP::JT_CHAR: return 'C'; |
| 3643 | case JDWP::JT_FLOAT: return 'F'; |
| 3644 | case JDWP::JT_DOUBLE: return 'D'; |
| 3645 | case JDWP::JT_INT: return 'I'; |
| 3646 | case JDWP::JT_LONG: return 'J'; |
| 3647 | case JDWP::JT_SHORT: return 'S'; |
| 3648 | case JDWP::JT_VOID: return 'V'; |
| 3649 | case JDWP::JT_BOOLEAN: return 'Z'; |
| 3650 | |
| 3651 | // Reference types. |
| 3652 | case JDWP::JT_ARRAY: |
| 3653 | case JDWP::JT_OBJECT: |
| 3654 | case JDWP::JT_STRING: |
| 3655 | case JDWP::JT_THREAD: |
| 3656 | case JDWP::JT_THREAD_GROUP: |
| 3657 | case JDWP::JT_CLASS_LOADER: |
| 3658 | case JDWP::JT_CLASS_OBJECT: |
| 3659 | return 'L'; |
| 3660 | } |
| 3661 | } |
| 3662 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 3663 | JDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id, |
| 3664 | JDWP::RefTypeId class_id, JDWP::MethodId method_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3665 | uint32_t arg_count, uint64_t* arg_values, |
| 3666 | JDWP::JdwpTag* arg_types, uint32_t options, |
| 3667 | JDWP::JdwpTag* pResultTag, uint64_t* pResultValue, |
| 3668 | JDWP::ObjectId* pExceptionId) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3669 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 3670 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3671 | Thread* targetThread = nullptr; |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3672 | std::unique_ptr<DebugInvokeReq> req; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3673 | Thread* self = Thread::Current(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3674 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3675 | ScopedObjectAccessUnchecked soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3676 | JDWP::JdwpError error; |
| 3677 | targetThread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 3678 | if (error != JDWP::ERR_NONE) { |
| 3679 | LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id; |
| 3680 | return error; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3681 | } |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3682 | if (targetThread->GetInvokeReq() != nullptr) { |
| 3683 | // Thread is already invoking a method on behalf of the debugger. |
| 3684 | LOG(ERROR) << "InvokeMethod request for thread already invoking a method: " << *targetThread; |
| 3685 | return JDWP::ERR_ALREADY_INVOKING; |
| 3686 | } |
| 3687 | if (!targetThread->IsReadyForDebugInvoke()) { |
| 3688 | // 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] | 3689 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 3690 | return JDWP::ERR_INVALID_THREAD; |
| 3691 | } |
| 3692 | |
| 3693 | /* |
| 3694 | * We currently have a bug where we don't successfully resume the |
| 3695 | * target thread if the suspend count is too deep. We're expected to |
| 3696 | * require one "resume" for each "suspend", but when asked to execute |
| 3697 | * a method we have to resume fully and then re-suspend it back to the |
| 3698 | * same level. (The easiest way to cause this is to type "suspend" |
| 3699 | * multiple times in jdb.) |
| 3700 | * |
| 3701 | * It's unclear what this means when the event specifies "resume all" |
| 3702 | * and some threads are suspended more deeply than others. This is |
| 3703 | * a rare problem, so for now we just prevent it from hanging forever |
| 3704 | * by rejecting the method invocation request. Without this, we will |
| 3705 | * be stuck waiting on a suspended thread. |
| 3706 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3707 | int suspend_count; |
| 3708 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3709 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3710 | suspend_count = targetThread->GetSuspendCount(); |
| 3711 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3712 | if (suspend_count > 1) { |
| 3713 | LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3714 | return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here. |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3715 | } |
| 3716 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3717 | mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 3718 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3719 | return JDWP::ERR_INVALID_OBJECT; |
| 3720 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3721 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3722 | gRegistry->Get<mirror::Object*>(thread_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3723 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3724 | return JDWP::ERR_INVALID_OBJECT; |
| 3725 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3726 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3727 | mirror::Class* c = DecodeClass(class_id, &error); |
| 3728 | if (c == nullptr) { |
| 3729 | return error; |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3730 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3731 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3732 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3733 | if (m->IsStatic() != (receiver == nullptr)) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3734 | return JDWP::ERR_INVALID_METHODID; |
| 3735 | } |
| 3736 | if (m->IsStatic()) { |
| 3737 | if (m->GetDeclaringClass() != c) { |
| 3738 | return JDWP::ERR_INVALID_METHODID; |
| 3739 | } |
| 3740 | } else { |
| 3741 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
| 3742 | return JDWP::ERR_INVALID_METHODID; |
| 3743 | } |
| 3744 | } |
| 3745 | |
| 3746 | // Check the argument list matches the method. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3747 | uint32_t shorty_len = 0; |
| 3748 | const char* shorty = m->GetShorty(&shorty_len); |
| 3749 | if (shorty_len - 1 != arg_count) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3750 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3751 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3752 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3753 | { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3754 | StackHandleScope<2> hs(soa.Self()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3755 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver)); |
| 3756 | HandleWrapper<mirror::Class> h_klass(hs.NewHandleWrapper(&c)); |
| 3757 | const DexFile::TypeList* types = m->GetParameterTypeList(); |
| 3758 | for (size_t i = 0; i < arg_count; ++i) { |
| 3759 | if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) { |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3760 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3761 | } |
| 3762 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3763 | if (shorty[i + 1] == 'L') { |
| 3764 | // Did we really get an argument of an appropriate reference type? |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 3765 | mirror::Class* parameter_type = |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3766 | m->GetClassFromTypeIndex(types->GetTypeItem(i).type_idx_, true); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3767 | mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i], &error); |
| 3768 | if (error != JDWP::ERR_NONE) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3769 | return JDWP::ERR_INVALID_OBJECT; |
| 3770 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3771 | if (argument != nullptr && !argument->InstanceOf(parameter_type)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3772 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3773 | } |
| 3774 | |
| 3775 | // Turn the on-the-wire ObjectId into a jobject. |
| 3776 | jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]); |
| 3777 | v.l = gRegistry->GetJObject(arg_values[i]); |
| 3778 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3779 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3780 | } |
| 3781 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3782 | // Allocates a DebugInvokeReq. |
| 3783 | req.reset(new (std::nothrow) DebugInvokeReq(receiver, c, m, options, arg_values, arg_count)); |
| 3784 | if (req.get() == nullptr) { |
| 3785 | LOG(ERROR) << "Failed to allocate DebugInvokeReq"; |
| 3786 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3787 | } |
| 3788 | |
| 3789 | // Attach the DebugInvokeReq to the target thread so it executes the method when |
| 3790 | // it is resumed. Once the invocation completes, it will detach it and signal us |
| 3791 | // before suspending itself. |
| 3792 | targetThread->SetDebugInvokeReq(req.get()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | // The fact that we've released the thread list lock is a bit risky --- if the thread goes |
| 3796 | // away we're sitting high and dry -- but we must release this before the ResumeAllThreads |
| 3797 | // call, and it's unwise to hold it during WaitForSuspend. |
| 3798 | |
| 3799 | { |
| 3800 | /* |
| 3801 | * We change our (JDWP thread) status, which should be THREAD_RUNNING, |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 3802 | * so we can suspend for a GC if the invoke request causes us to |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3803 | * run out of memory. It's also a good idea to change it before locking |
| 3804 | * the invokeReq mutex, although that should never be held for long. |
| 3805 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3806 | self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3807 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3808 | VLOG(jdwp) << " Transferring control to event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3809 | { |
Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 3810 | MutexLock mu(self, req->lock); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3811 | |
| 3812 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3813 | VLOG(jdwp) << " Resuming all threads"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3814 | thread_list->UndoDebuggerSuspensions(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3815 | } else { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3816 | VLOG(jdwp) << " Resuming event thread only"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3817 | thread_list->Resume(targetThread, true); |
| 3818 | } |
| 3819 | |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 3820 | // The target thread is resumed but needs the JDWP token we're holding. |
| 3821 | // We release it now and will acquire it again when the invocation is |
| 3822 | // complete and the target thread suspends itself. |
| 3823 | gJdwpState->ReleaseJdwpTokenForCommand(); |
| 3824 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3825 | // Wait for the request to finish executing. |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3826 | while (targetThread->GetInvokeReq() != nullptr) { |
Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 3827 | req->cond.Wait(self); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3828 | } |
| 3829 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3830 | VLOG(jdwp) << " Control has returned from event thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3831 | |
| 3832 | /* wait for thread to re-suspend itself */ |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 3833 | SuspendThread(thread_id, false /* request_suspension */); |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 3834 | |
| 3835 | // Now the thread is suspended again, we can re-acquire the JDWP token. |
| 3836 | gJdwpState->AcquireJdwpTokenForCommand(); |
| 3837 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3838 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | /* |
| 3842 | * Suspend the threads. We waited for the target thread to suspend |
| 3843 | * itself, so all we need to do is suspend the others. |
| 3844 | * |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 3845 | * The SuspendAllForDebugger() call will double-suspend the event thread, |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3846 | * so we want to resume the target thread once to keep the books straight. |
| 3847 | */ |
| 3848 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3849 | self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3850 | VLOG(jdwp) << " Suspending all threads"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3851 | thread_list->SuspendAllForDebugger(); |
| 3852 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3853 | VLOG(jdwp) << " Resuming event thread to balance the count"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3854 | thread_list->Resume(targetThread, true); |
| 3855 | } |
| 3856 | |
| 3857 | // Copy the result. |
| 3858 | *pResultTag = req->result_tag; |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3859 | *pResultValue = req->result_value; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3860 | *pExceptionId = req->exception; |
| 3861 | return req->error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3865 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3866 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 3867 | // We can be called while an exception is pending. We need |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3868 | // to preserve that across the method invocation. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3869 | StackHandleScope<3> hs(soa.Self()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3870 | auto old_exception = hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()); |
| 3871 | soa.Self()->ClearException(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3872 | |
| 3873 | // Translate the method through the vtable, unless the debugger wants to suppress it. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3874 | auto* m = pReq->method; |
| 3875 | auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3876 | if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver.Read() != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3877 | ArtMethod* actual_method = |
| 3878 | pReq->klass.Read()->FindVirtualMethodForVirtualOrInterface(m, image_pointer_size); |
| 3879 | if (actual_method != m) { |
| 3880 | VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3881 | << " to " << PrettyMethod(actual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3882 | m = actual_method; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3883 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3884 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3885 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m) |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3886 | << " receiver=" << pReq->receiver.Read() |
Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 3887 | << " arg_count=" << pReq->arg_count; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3888 | CHECK(m != nullptr); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3889 | |
| 3890 | CHECK_EQ(sizeof(jvalue), sizeof(uint64_t)); |
| 3891 | |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 3892 | ScopedLocalRef<jobject> ref(soa.Env(), soa.AddLocalReference<jobject>(pReq->receiver.Read())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3893 | JValue result = InvokeWithJValues(soa, ref.get(), soa.EncodeMethod(m), |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3894 | reinterpret_cast<jvalue*>(pReq->arg_values)); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3895 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3896 | pReq->result_tag = BasicTagFromDescriptor(m->GetShorty()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3897 | const bool is_object_result = (pReq->result_tag == JDWP::JT_OBJECT); |
| 3898 | Handle<mirror::Object> object_result = hs.NewHandle(is_object_result ? result.GetL() : nullptr); |
| 3899 | Handle<mirror::Throwable> exception = hs.NewHandle(soa.Self()->GetException()); |
| 3900 | soa.Self()->ClearException(); |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 3901 | pReq->exception = gRegistry->Add(exception); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3902 | if (pReq->exception != 0) { |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3903 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exception.Get() |
| 3904 | << " " << exception->Dump(); |
| 3905 | pReq->result_value = 0; |
| 3906 | } else if (is_object_result) { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3907 | /* if no exception thrown, examine object result more closely */ |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3908 | JDWP::JdwpTag new_tag = TagFromObject(soa, object_result.Get()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3909 | if (new_tag != pReq->result_tag) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3910 | VLOG(jdwp) << " JDWP promoted result from " << pReq->result_tag << " to " << new_tag; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3911 | pReq->result_tag = new_tag; |
| 3912 | } |
| 3913 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3914 | // Register the object in the registry and reference its ObjectId. This ensures |
| 3915 | // GC safety and prevents from accessing stale reference if the object is moved. |
| 3916 | pReq->result_value = gRegistry->Add(object_result.Get()); |
| 3917 | } else { |
| 3918 | // Primitive result. |
| 3919 | DCHECK(IsPrimitiveTag(pReq->result_tag)); |
| 3920 | pReq->result_value = result.GetJ(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3921 | } |
| 3922 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3923 | if (old_exception.Get() != nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3924 | soa.Self()->SetException(old_exception.Get()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3925 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3926 | } |
| 3927 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3928 | /* |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 3929 | * "request" contains a full JDWP packet, possibly with multiple chunks. We |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 3930 | * need to process each, accumulate the replies, and ship the whole thing |
| 3931 | * back. |
| 3932 | * |
| 3933 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 3934 | * and includes the chunk type/length, followed by the data. |
| 3935 | * |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 3936 | * 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] | 3937 | * chunk. If this becomes inconvenient we will need to adapt. |
| 3938 | */ |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3939 | bool Dbg::DdmHandlePacket(JDWP::Request* request, uint8_t** pReplyBuf, int* pReplyLen) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 3940 | Thread* self = Thread::Current(); |
| 3941 | JNIEnv* env = self->GetJniEnv(); |
| 3942 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3943 | uint32_t type = request->ReadUnsigned32("type"); |
| 3944 | uint32_t length = request->ReadUnsigned32("length"); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 3945 | |
| 3946 | // Create a byte[] corresponding to 'request'. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3947 | size_t request_length = request->size(); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 3948 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3949 | if (dataArray.get() == nullptr) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 3950 | LOG(WARNING) << "byte[] allocation failed: " << request_length; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 3951 | env->ExceptionClear(); |
| 3952 | return false; |
| 3953 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3954 | env->SetByteArrayRegion(dataArray.get(), 0, request_length, |
| 3955 | reinterpret_cast<const jbyte*>(request->data())); |
| 3956 | request->Skip(request_length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 3957 | |
| 3958 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 3959 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 3960 | if (length != request_length) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3961 | 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] | 3962 | return false; |
| 3963 | } |
| 3964 | |
| 3965 | // 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] | 3966 | ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 3967 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch, |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 3968 | type, dataArray.get(), 0, length)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 3969 | if (env->ExceptionCheck()) { |
| 3970 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 3971 | env->ExceptionDescribe(); |
| 3972 | env->ExceptionClear(); |
| 3973 | return false; |
| 3974 | } |
| 3975 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3976 | if (chunk.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 3977 | return false; |
| 3978 | } |
| 3979 | |
| 3980 | /* |
| 3981 | * Pull the pieces out of the chunk. We copy the results into a |
| 3982 | * newly-allocated buffer that the caller can free. We don't want to |
| 3983 | * continue using the Chunk object because nothing has a reference to it. |
| 3984 | * |
| 3985 | * We could avoid this by returning type/data/offset/length and having |
| 3986 | * the caller be aware of the object lifetime issues, but that |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 3987 | * 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] | 3988 | * if we have responses for multiple chunks. |
| 3989 | * |
| 3990 | * So we're pretty much stuck with copying data around multiple times. |
| 3991 | */ |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 3992 | 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] | 3993 | 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] | 3994 | 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] | 3995 | 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] | 3996 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3997 | 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] | 3998 | if (length == 0 || replyData.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 3999 | return false; |
| 4000 | } |
| 4001 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4002 | const int kChunkHdrLen = 8; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4003 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4004 | if (reply == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4005 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 4006 | return false; |
| 4007 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 4008 | JDWP::Set4BE(reply + 0, type); |
| 4009 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4010 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4011 | |
| 4012 | *pReplyBuf = reply; |
| 4013 | *pReplyLen = length + kChunkHdrLen; |
| 4014 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4015 | 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] | 4016 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4017 | } |
| 4018 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4019 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4020 | VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4021 | |
| 4022 | Thread* self = Thread::Current(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4023 | if (self->GetState() != kRunnable) { |
| 4024 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 4025 | /* try anyway? */ |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4029 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4030 | env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4031 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast, |
| 4032 | event); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4033 | if (env->ExceptionCheck()) { |
| 4034 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 4035 | env->ExceptionDescribe(); |
| 4036 | env->ExceptionClear(); |
| 4037 | } |
| 4038 | } |
| 4039 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4040 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4041 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4045 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4046 | gDdmThreadNotification = false; |
| 4047 | } |
| 4048 | |
| 4049 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4050 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4051 | * |
| 4052 | * Because we broadcast the full set of threads when the notifications are |
| 4053 | * first enabled, it's possible for "thread" to be actively executing. |
| 4054 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4055 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4056 | if (!gDdmThreadNotification) { |
| 4057 | return; |
| 4058 | } |
| 4059 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4060 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4061 | uint8_t buf[4]; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4062 | JDWP::Set4BE(&buf[0], t->GetThreadId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4063 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4064 | } else { |
| 4065 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4066 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4067 | StackHandleScope<1> hs(soa.Self()); |
| 4068 | Handle<mirror::String> name(hs.NewHandle(t->GetThreadName(soa))); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4069 | size_t char_count = (name.Get() != nullptr) ? name->GetLength() : 0; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 4070 | const jchar* chars = (name.Get() != nullptr) ? name->GetValue() : nullptr; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4071 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4072 | std::vector<uint8_t> bytes; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4073 | JDWP::Append4BE(bytes, t->GetThreadId()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4074 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4075 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 4076 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4077 | } |
| 4078 | } |
| 4079 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4080 | void Dbg::DdmSetThreadNotification(bool enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4081 | // Enable/disable thread notifications. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4082 | gDdmThreadNotification = enable; |
| 4083 | if (enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4084 | // Suspend the VM then post thread start notifications for all threads. Threads attaching will |
| 4085 | // see a suspension in progress and block until that ends. They then post their own start |
| 4086 | // notification. |
| 4087 | SuspendVM(); |
| 4088 | std::list<Thread*> threads; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4089 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4090 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4091 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4092 | threads = Runtime::Current()->GetThreadList()->GetList(); |
| 4093 | } |
| 4094 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4095 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4096 | for (Thread* thread : threads) { |
| 4097 | Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR")); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4098 | } |
| 4099 | } |
| 4100 | ResumeVM(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4101 | } |
| 4102 | } |
| 4103 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4104 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 4105 | if (IsDebuggerActive()) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 4106 | gJdwpState->PostThreadChange(t, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4107 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4108 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4112 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4116 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4117 | } |
| 4118 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4119 | 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] | 4120 | CHECK(buf != nullptr); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4121 | iovec vec[1]; |
| 4122 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 4123 | vec[0].iov_len = byte_count; |
| 4124 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4125 | } |
| 4126 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4127 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 4128 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 4129 | } |
| 4130 | |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 4131 | void Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4132 | if (gJdwpState == nullptr) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4133 | VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4134 | } else { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 4135 | gJdwpState->DdmSendChunkV(type, iov, iov_count); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4136 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4137 | } |
| 4138 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 4139 | JDWP::JdwpState* Dbg::GetJdwpState() { |
| 4140 | return gJdwpState; |
| 4141 | } |
| 4142 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4143 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 4144 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4145 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4146 | return true; |
| 4147 | } |
| 4148 | |
| 4149 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 4150 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 4151 | return false; |
| 4152 | } |
| 4153 | |
| 4154 | gDdmHpifWhen = when; |
| 4155 | return true; |
| 4156 | } |
| 4157 | |
| 4158 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 4159 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 4160 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 4161 | return false; |
| 4162 | } |
| 4163 | |
| 4164 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 4165 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 4166 | return false; |
| 4167 | } |
| 4168 | |
| 4169 | if (native) { |
| 4170 | gDdmNhsgWhen = when; |
| 4171 | gDdmNhsgWhat = what; |
| 4172 | } else { |
| 4173 | gDdmHpsgWhen = when; |
| 4174 | gDdmHpsgWhat = what; |
| 4175 | } |
| 4176 | return true; |
| 4177 | } |
| 4178 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4179 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 4180 | // If there's a one-shot 'when', reset it. |
| 4181 | if (reason == gDdmHpifWhen) { |
| 4182 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 4183 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 4184 | } |
| 4185 | } |
| 4186 | |
| 4187 | /* |
| 4188 | * Chunk HPIF (client --> server) |
| 4189 | * |
| 4190 | * Heap Info. General information about the heap, |
| 4191 | * suitable for a summary display. |
| 4192 | * |
| 4193 | * [u4]: number of heaps |
| 4194 | * |
| 4195 | * For each heap: |
| 4196 | * [u4]: heap ID |
| 4197 | * [u8]: timestamp in ms since Unix epoch |
| 4198 | * [u1]: capture reason (same as 'when' value from server) |
| 4199 | * [u4]: max heap size in bytes (-Xmx) |
| 4200 | * [u4]: current heap size in bytes |
| 4201 | * [u4]: current number of bytes allocated |
| 4202 | * [u4]: current number of objects allocated |
| 4203 | */ |
| 4204 | uint8_t heap_count = 1; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4205 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4206 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4207 | JDWP::Append4BE(bytes, heap_count); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4208 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4209 | JDWP::Append8BE(bytes, MilliTime()); |
| 4210 | JDWP::Append1BE(bytes, reason); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4211 | JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes. |
| 4212 | JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4213 | JDWP::Append4BE(bytes, heap->GetBytesAllocated()); |
| 4214 | JDWP::Append4BE(bytes, heap->GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4215 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 4216 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4217 | } |
| 4218 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4219 | enum HpsgSolidity { |
| 4220 | SOLIDITY_FREE = 0, |
| 4221 | SOLIDITY_HARD = 1, |
| 4222 | SOLIDITY_SOFT = 2, |
| 4223 | SOLIDITY_WEAK = 3, |
| 4224 | SOLIDITY_PHANTOM = 4, |
| 4225 | SOLIDITY_FINALIZABLE = 5, |
| 4226 | SOLIDITY_SWEEP = 6, |
| 4227 | }; |
| 4228 | |
| 4229 | enum HpsgKind { |
| 4230 | KIND_OBJECT = 0, |
| 4231 | KIND_CLASS_OBJECT = 1, |
| 4232 | KIND_ARRAY_1 = 2, |
| 4233 | KIND_ARRAY_2 = 3, |
| 4234 | KIND_ARRAY_4 = 4, |
| 4235 | KIND_ARRAY_8 = 5, |
| 4236 | KIND_UNKNOWN = 6, |
| 4237 | KIND_NATIVE = 7, |
| 4238 | }; |
| 4239 | |
| 4240 | #define HPSG_PARTIAL (1<<7) |
| 4241 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 4242 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4243 | class HeapChunkContext { |
| 4244 | public: |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4245 | // Maximum chunk size. Obtain this from the formula: |
| 4246 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 4247 | HeapChunkContext(bool merge, bool native) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4248 | : buf_(16384 - 16), |
| 4249 | type_(0), |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4250 | chunk_overhead_(0) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4251 | Reset(); |
| 4252 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4253 | type_ = CHUNK_TYPE("NHSG"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4254 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4255 | type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4256 | } |
| 4257 | } |
| 4258 | |
| 4259 | ~HeapChunkContext() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4260 | if (p_ > &buf_[0]) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4261 | Flush(); |
| 4262 | } |
| 4263 | } |
| 4264 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4265 | void SetChunkOverhead(size_t chunk_overhead) { |
| 4266 | chunk_overhead_ = chunk_overhead; |
| 4267 | } |
| 4268 | |
| 4269 | void ResetStartOfNextChunk() { |
| 4270 | startOfNextMemoryChunk_ = nullptr; |
| 4271 | } |
| 4272 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4273 | void EnsureHeader(const void* chunk_ptr) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4274 | if (!needHeader_) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4275 | return; |
| 4276 | } |
| 4277 | |
| 4278 | // Start a new HPSx chunk. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4279 | JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap). |
| 4280 | JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4281 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4282 | JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 4283 | 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] | 4284 | // [u4]: length of piece, in allocation units |
| 4285 | // 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] | 4286 | pieceLenField_ = p_; |
| 4287 | JDWP::Write4BE(&p_, 0x55555555); |
| 4288 | needHeader_ = false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4289 | } |
| 4290 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4291 | void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4292 | if (pieceLenField_ == nullptr) { |
Ian Rogers | d636b06 | 2013-01-18 17:51:18 -0800 | [diff] [blame] | 4293 | // Flush immediately post Reset (maybe back-to-back Flush). Ignore. |
| 4294 | CHECK(needHeader_); |
| 4295 | return; |
| 4296 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4297 | // Patch the "length of piece" field. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4298 | CHECK_LE(&buf_[0], pieceLenField_); |
| 4299 | CHECK_LE(pieceLenField_, p_); |
| 4300 | JDWP::Set4BE(pieceLenField_, totalAllocationUnits_); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4301 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4302 | Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4303 | Reset(); |
| 4304 | } |
| 4305 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4306 | 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] | 4307 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 4308 | Locks::mutator_lock_) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4309 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkJavaCallback(start, end, used_bytes); |
| 4310 | } |
| 4311 | |
| 4312 | static void HeapChunkNativeCallback(void* start, void* end, size_t used_bytes, void* arg) |
| 4313 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4314 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkNativeCallback(start, end, used_bytes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4315 | } |
| 4316 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4317 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4318 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 4319 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4320 | void Reset() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4321 | p_ = &buf_[0]; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4322 | ResetStartOfNextChunk(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4323 | totalAllocationUnits_ = 0; |
| 4324 | needHeader_ = true; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4325 | pieceLenField_ = nullptr; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4326 | } |
| 4327 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4328 | bool IsNative() const { |
| 4329 | return type_ == CHUNK_TYPE("NHSG"); |
| 4330 | } |
| 4331 | |
| 4332 | // Returns true if the object is not an empty chunk. |
| 4333 | 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] | 4334 | // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken |
| 4335 | // 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] | 4336 | if (used_bytes == 0) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4337 | if (start == nullptr) { |
| 4338 | // Reset for start of new heap. |
| 4339 | startOfNextMemoryChunk_ = nullptr; |
| 4340 | Flush(); |
| 4341 | } |
| 4342 | // Only process in use memory so that free region information |
| 4343 | // also includes dlmalloc book keeping. |
| 4344 | return false; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4345 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4346 | if (startOfNextMemoryChunk_ != nullptr) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4347 | // Transmit any pending free memory. Native free memory of over kMaxFreeLen could be because |
| 4348 | // of the use of mmaps, so don't report. If not free memory then start a new segment. |
| 4349 | bool flush = true; |
| 4350 | if (start > startOfNextMemoryChunk_) { |
| 4351 | const size_t kMaxFreeLen = 2 * kPageSize; |
| 4352 | void* free_start = startOfNextMemoryChunk_; |
| 4353 | void* free_end = start; |
| 4354 | const size_t free_len = |
| 4355 | reinterpret_cast<uintptr_t>(free_end) - reinterpret_cast<uintptr_t>(free_start); |
| 4356 | if (!IsNative() || free_len < kMaxFreeLen) { |
| 4357 | AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), free_start, free_len, IsNative()); |
| 4358 | flush = false; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4359 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4360 | } |
| 4361 | if (flush) { |
| 4362 | startOfNextMemoryChunk_ = nullptr; |
| 4363 | Flush(); |
| 4364 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4365 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4366 | return true; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4367 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4368 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4369 | void HeapChunkNativeCallback(void* start, void* /*end*/, size_t used_bytes) |
| 4370 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4371 | if (ProcessRecord(start, used_bytes)) { |
| 4372 | uint8_t state = ExamineNativeObject(start); |
| 4373 | AppendChunk(state, start, used_bytes + chunk_overhead_, true /*is_native*/); |
| 4374 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4375 | } |
| 4376 | } |
| 4377 | |
| 4378 | void HeapChunkJavaCallback(void* start, void* /*end*/, size_t used_bytes) |
| 4379 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 4380 | if (ProcessRecord(start, used_bytes)) { |
| 4381 | // Determine the type of this chunk. |
| 4382 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 4383 | // If it's the same, we should combine them. |
| 4384 | uint8_t state = ExamineJavaObject(reinterpret_cast<mirror::Object*>(start)); |
| 4385 | AppendChunk(state, start, used_bytes + chunk_overhead_, false /*is_native*/); |
| 4386 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4387 | } |
| 4388 | } |
| 4389 | |
| 4390 | 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] | 4391 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4392 | // Make sure there's enough room left in the buffer. |
| 4393 | // We need to use two bytes for every fractional 256 allocation units used by the chunk plus |
| 4394 | // 17 bytes for any header. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4395 | const size_t needed = ((RoundUp(length / ALLOCATION_UNIT_SIZE, 256) / 256) * 2) + 17; |
| 4396 | size_t byte_left = &buf_.back() - p_; |
| 4397 | if (byte_left < needed) { |
| 4398 | if (is_native) { |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4399 | // Cannot trigger memory allocation while walking native heap. |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4400 | return; |
| 4401 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4402 | Flush(); |
| 4403 | } |
| 4404 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4405 | byte_left = &buf_.back() - p_; |
| 4406 | if (byte_left < needed) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4407 | LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", " |
| 4408 | << needed << " bytes)"; |
| 4409 | return; |
| 4410 | } |
| 4411 | EnsureHeader(ptr); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4412 | // Write out the chunk description. |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4413 | length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units. |
| 4414 | totalAllocationUnits_ += length; |
| 4415 | while (length > 256) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4416 | *p_++ = state | HPSG_PARTIAL; |
| 4417 | *p_++ = 255; // length - 1 |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4418 | length -= 256; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4419 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4420 | *p_++ = state; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4421 | *p_++ = length - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4422 | } |
| 4423 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4424 | uint8_t ExamineNativeObject(const void* p) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4425 | return p == nullptr ? HPSG_STATE(SOLIDITY_FREE, 0) : HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4426 | } |
| 4427 | |
| 4428 | uint8_t ExamineJavaObject(mirror::Object* o) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4429 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4430 | if (o == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4431 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 4432 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4433 | // It's an allocated chunk. Figure out what it is. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4434 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 4435 | if (!heap->IsLiveObjectLocked(o)) { |
| 4436 | LOG(ERROR) << "Invalid object in managed heap: " << o; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4437 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4438 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4439 | mirror::Class* c = o->GetClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4440 | if (c == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4441 | // The object was probably just created but hasn't been initialized yet. |
| 4442 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4443 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4444 | if (!heap->IsValidObjectAddress(c)) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4445 | LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4446 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4447 | } |
Mathieu Chartier | f26e1b3 | 2015-01-29 10:47:10 -0800 | [diff] [blame] | 4448 | if (c->GetClass() == nullptr) { |
| 4449 | LOG(ERROR) << "Null class of class " << c << " for object " << o; |
| 4450 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4451 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4452 | if (c->IsClassClass()) { |
| 4453 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 4454 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4455 | if (c->IsArrayClass()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4456 | switch (c->GetComponentSize()) { |
| 4457 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 4458 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 4459 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 4460 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 4461 | } |
| 4462 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4463 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4464 | } |
| 4465 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4466 | std::vector<uint8_t> buf_; |
| 4467 | uint8_t* p_; |
| 4468 | uint8_t* pieceLenField_; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4469 | void* startOfNextMemoryChunk_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4470 | size_t totalAllocationUnits_; |
| 4471 | uint32_t type_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4472 | bool needHeader_; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4473 | size_t chunk_overhead_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4474 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4475 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 4476 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4477 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4478 | static void BumpPointerSpaceCallback(mirror::Object* obj, void* arg) |
| 4479 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 4480 | const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4481 | HeapChunkContext::HeapChunkJavaCallback( |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4482 | obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, arg); |
| 4483 | } |
| 4484 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4485 | void Dbg::DdmSendHeapSegments(bool native) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4486 | Dbg::HpsgWhen when = native ? gDdmNhsgWhen : gDdmHpsgWhen; |
| 4487 | Dbg::HpsgWhat what = native ? gDdmNhsgWhat : gDdmHpsgWhat; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4488 | if (when == HPSG_WHEN_NEVER) { |
| 4489 | return; |
| 4490 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4491 | // Figure out what kind of chunks we'll be sending. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4492 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) |
| 4493 | << static_cast<int>(what); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4494 | |
| 4495 | // First, send a heap start chunk. |
| 4496 | uint8_t heap_id[4]; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4497 | 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] | 4498 | 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] | 4499 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4500 | Locks::mutator_lock_->AssertSharedHeld(self); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4501 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4502 | // Send a series of heap segment chunks. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4503 | HeapChunkContext context(what == HPSG_WHAT_MERGED_OBJECTS, native); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4504 | if (native) { |
Ian Rogers | 872dd82 | 2014-10-30 11:19:14 -0700 | [diff] [blame] | 4505 | #if defined(HAVE_ANDROID_OS) && defined(USE_DLMALLOC) |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4506 | dlmalloc_inspect_all(HeapChunkContext::HeapChunkNativeCallback, &context); |
| 4507 | HeapChunkContext::HeapChunkNativeCallback(nullptr, nullptr, 0, &context); // Indicate end of a space. |
Christopher Ferris | c4ddc04 | 2014-05-13 14:47:50 -0700 | [diff] [blame] | 4508 | #else |
| 4509 | UNIMPLEMENTED(WARNING) << "Native heap inspection is only supported with dlmalloc"; |
| 4510 | #endif |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4511 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4512 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4513 | for (const auto& space : heap->GetContinuousSpaces()) { |
| 4514 | if (space->IsDlMallocSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4515 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4516 | // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an |
| 4517 | // allocation then the first sizeof(size_t) may belong to it. |
| 4518 | context.SetChunkOverhead(sizeof(size_t)); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4519 | space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4520 | } else if (space->IsRosAllocSpace()) { |
| 4521 | context.SetChunkOverhead(0); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4522 | // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since |
| 4523 | // RosAlloc's internal logic doesn't know to release and reacquire the heap bitmap lock. |
| 4524 | self->TransitionFromRunnableToSuspended(kSuspended); |
| 4525 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 4526 | tl->SuspendAll(__FUNCTION__); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4527 | { |
| 4528 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4529 | space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4530 | } |
| 4531 | tl->ResumeAll(); |
| 4532 | self->TransitionFromSuspendedToRunnable(); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4533 | } else if (space->IsBumpPointerSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4534 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4535 | context.SetChunkOverhead(0); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4536 | space->AsBumpPointerSpace()->Walk(BumpPointerSpaceCallback, &context); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4537 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4538 | } else if (space->IsRegionSpace()) { |
| 4539 | heap->IncrementDisableMovingGC(self); |
| 4540 | self->TransitionFromRunnableToSuspended(kSuspended); |
| 4541 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 4542 | tl->SuspendAll(__FUNCTION__); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4543 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 4544 | context.SetChunkOverhead(0); |
| 4545 | space->AsRegionSpace()->Walk(BumpPointerSpaceCallback, &context); |
| 4546 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
| 4547 | tl->ResumeAll(); |
| 4548 | self->TransitionFromSuspendedToRunnable(); |
| 4549 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4550 | } else { |
| 4551 | UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4552 | } |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4553 | context.ResetStartOfNextChunk(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4554 | } |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4555 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 4556 | // Walk the large objects, these are not in the AllocSpace. |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4557 | context.SetChunkOverhead(0); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4558 | heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4559 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4560 | |
| 4561 | // Finally, send a heap end chunk. |
| 4562 | 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] | 4563 | } |
| 4564 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4565 | void Dbg::SetAllocTrackingEnabled(bool enable) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4566 | gc::AllocRecordObjectMap::SetAllocTrackingEnabled(enable); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4567 | } |
| 4568 | |
| 4569 | void Dbg::DumpRecentAllocations() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4570 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4571 | MutexLock mu(soa.Self(), *Locks::alloc_tracker_lock_); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4572 | if (!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4573 | LOG(INFO) << "Not recording tracked allocations"; |
| 4574 | return; |
| 4575 | } |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4576 | gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords(); |
| 4577 | CHECK(records != nullptr); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4578 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4579 | const uint16_t capped_count = CappedAllocRecordCount(records->Size()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4580 | uint16_t count = capped_count; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4581 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4582 | LOG(INFO) << "Tracked allocations, (count=" << count << ")"; |
| 4583 | for (auto it = records->RBegin(), end = records->REnd(); |
| 4584 | count > 0 && it != end; count--, it++) { |
| 4585 | const gc::AllocRecord* record = it->second; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4586 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4587 | LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->GetTid(), record->ByteCount()) |
| 4588 | << PrettyClass(it->first.Read()->GetClass()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4589 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4590 | for (size_t stack_frame = 0, depth = record->GetDepth(); stack_frame < depth; ++stack_frame) { |
| 4591 | const gc::AllocRecordStackTraceElement& stack_element = record->StackElement(stack_frame); |
| 4592 | ArtMethod* m = stack_element.GetMethod(); |
| 4593 | LOG(INFO) << " " << PrettyMethod(m) << " line " << stack_element.ComputeLineNumber(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | // pause periodically to help logcat catch up |
| 4597 | if ((count % 5) == 0) { |
| 4598 | usleep(40000); |
| 4599 | } |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4600 | } |
| 4601 | } |
| 4602 | |
| 4603 | class StringTable { |
| 4604 | public: |
| 4605 | StringTable() { |
| 4606 | } |
| 4607 | |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 4608 | void Add(const std::string& str) { |
| 4609 | table_.insert(str); |
| 4610 | } |
| 4611 | |
| 4612 | void Add(const char* str) { |
| 4613 | table_.insert(str); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4614 | } |
| 4615 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4616 | size_t IndexOf(const char* s) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4617 | auto it = table_.find(s); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4618 | if (it == table_.end()) { |
| 4619 | LOG(FATAL) << "IndexOf(\"" << s << "\") failed"; |
| 4620 | } |
| 4621 | return std::distance(table_.begin(), it); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4622 | } |
| 4623 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4624 | size_t Size() const { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4625 | return table_.size(); |
| 4626 | } |
| 4627 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4628 | void WriteTo(std::vector<uint8_t>& bytes) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4629 | for (const std::string& str : table_) { |
| 4630 | const char* s = str.c_str(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4631 | size_t s_len = CountModifiedUtf8Chars(s); |
Christopher Ferris | 8a35405 | 2015-04-24 17:23:53 -0700 | [diff] [blame] | 4632 | std::unique_ptr<uint16_t[]> s_utf16(new uint16_t[s_len]); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4633 | ConvertModifiedUtf8ToUtf16(s_utf16.get(), s); |
| 4634 | JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4635 | } |
| 4636 | } |
| 4637 | |
| 4638 | private: |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4639 | std::set<std::string> table_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4640 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 4641 | }; |
| 4642 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4643 | static const char* GetMethodSourceFile(ArtMethod* method) |
Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 4644 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4645 | DCHECK(method != nullptr); |
| 4646 | const char* source_file = method->GetDeclaringClassSourceFile(); |
Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 4647 | return (source_file != nullptr) ? source_file : ""; |
| 4648 | } |
| 4649 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4650 | /* |
| 4651 | * The data we send to DDMS contains everything we have recorded. |
| 4652 | * |
| 4653 | * Message header (all values big-endian): |
| 4654 | * (1b) message header len (to allow future expansion); includes itself |
| 4655 | * (1b) entry header len |
| 4656 | * (1b) stack frame len |
| 4657 | * (2b) number of entries |
| 4658 | * (4b) offset to string table from start of message |
| 4659 | * (2b) number of class name strings |
| 4660 | * (2b) number of method name strings |
| 4661 | * (2b) number of source file name strings |
| 4662 | * For each entry: |
| 4663 | * (4b) total allocation size |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 4664 | * (2b) thread id |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4665 | * (2b) allocated object's class name index |
| 4666 | * (1b) stack depth |
| 4667 | * For each stack frame: |
| 4668 | * (2b) method's class name |
| 4669 | * (2b) method name |
| 4670 | * (2b) method source file |
| 4671 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 4672 | * (xb) class name strings |
| 4673 | * (xb) method name strings |
| 4674 | * (xb) source file strings |
| 4675 | * |
| 4676 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 4677 | * followed by UTF-16 data. |
| 4678 | * |
| 4679 | * 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] | 4680 | * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4681 | * each table, but in practice there should be far fewer. |
| 4682 | * |
| 4683 | * The chief reason for using a string table here is to keep the size of |
| 4684 | * the DDMS message to a minimum. This is partly to make the protocol |
| 4685 | * efficient, but also because we have to form the whole thing up all at |
| 4686 | * once in a memory buffer. |
| 4687 | * |
| 4688 | * We use separate string tables for class names, method names, and source |
| 4689 | * files to keep the indexes small. There will generally be no overlap |
| 4690 | * between the contents of these tables. |
| 4691 | */ |
| 4692 | jbyteArray Dbg::GetRecentAllocations() { |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 4693 | if ((false)) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4694 | DumpRecentAllocations(); |
| 4695 | } |
| 4696 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4697 | Thread* self = Thread::Current(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4698 | std::vector<uint8_t> bytes; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4699 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4700 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4701 | gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords(); |
| 4702 | // In case this method is called when allocation tracker is disabled, |
| 4703 | // we should still send some data back. |
| 4704 | gc::AllocRecordObjectMap dummy; |
| 4705 | if (records == nullptr) { |
| 4706 | CHECK(!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()); |
| 4707 | records = &dummy; |
| 4708 | } |
| 4709 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4710 | // |
| 4711 | // Part 1: generate string tables. |
| 4712 | // |
| 4713 | StringTable class_names; |
| 4714 | StringTable method_names; |
| 4715 | StringTable filenames; |
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 | const uint16_t capped_count = CappedAllocRecordCount(records->Size()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4718 | uint16_t count = capped_count; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4719 | for (auto it = records->RBegin(), end = records->REnd(); |
| 4720 | count > 0 && it != end; count--, it++) { |
| 4721 | const gc::AllocRecord* record = it->second; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4722 | std::string temp; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4723 | class_names.Add(it->first.Read()->GetClass()->GetDescriptor(&temp)); |
| 4724 | for (size_t i = 0, depth = record->GetDepth(); i < depth; i++) { |
| 4725 | ArtMethod* m = record->StackElement(i).GetMethod(); |
| 4726 | class_names.Add(m->GetDeclaringClassDescriptor()); |
| 4727 | method_names.Add(m->GetName()); |
| 4728 | filenames.Add(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4729 | } |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4730 | } |
| 4731 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4732 | LOG(INFO) << "recent allocation records: " << capped_count; |
| 4733 | LOG(INFO) << "allocation records all objects: " << records->Size(); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4734 | |
| 4735 | // |
| 4736 | // Part 2: Generate the output and store it in the buffer. |
| 4737 | // |
| 4738 | |
| 4739 | // (1b) message header len (to allow future expansion); includes itself |
| 4740 | // (1b) entry header len |
| 4741 | // (1b) stack frame len |
| 4742 | const int kMessageHeaderLen = 15; |
| 4743 | const int kEntryHeaderLen = 9; |
| 4744 | const int kStackFrameLen = 8; |
| 4745 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 4746 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 4747 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 4748 | |
| 4749 | // (2b) number of entries |
| 4750 | // (4b) offset to string table from start of message |
| 4751 | // (2b) number of class name strings |
| 4752 | // (2b) number of method name strings |
| 4753 | // (2b) number of source file name strings |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4754 | JDWP::Append2BE(bytes, capped_count); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4755 | size_t string_table_offset = bytes.size(); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4756 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4757 | JDWP::Append2BE(bytes, class_names.Size()); |
| 4758 | JDWP::Append2BE(bytes, method_names.Size()); |
| 4759 | JDWP::Append2BE(bytes, filenames.Size()); |
| 4760 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4761 | std::string temp; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4762 | count = capped_count; |
| 4763 | // The last "count" number of allocation records in "records" are the most recent "count" number |
| 4764 | // of allocations. Reverse iterate to get them. The most recent allocation is sent first. |
| 4765 | for (auto it = records->RBegin(), end = records->REnd(); |
| 4766 | count > 0 && it != end; count--, it++) { |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4767 | // For each entry: |
| 4768 | // (4b) total allocation size |
| 4769 | // (2b) thread id |
| 4770 | // (2b) allocated object's class name index |
| 4771 | // (1b) stack depth |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4772 | const gc::AllocRecord* record = it->second; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4773 | size_t stack_depth = record->GetDepth(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4774 | size_t allocated_object_class_name_index = |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4775 | class_names.IndexOf(it->first.Read()->GetClass()->GetDescriptor(&temp)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4776 | JDWP::Append4BE(bytes, record->ByteCount()); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4777 | JDWP::Append2BE(bytes, static_cast<uint16_t>(record->GetTid())); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4778 | JDWP::Append2BE(bytes, allocated_object_class_name_index); |
| 4779 | JDWP::Append1BE(bytes, stack_depth); |
| 4780 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4781 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 4782 | // For each stack frame: |
| 4783 | // (2b) method's class name |
| 4784 | // (2b) method name |
| 4785 | // (2b) method source file |
| 4786 | // (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^] | 4787 | ArtMethod* m = record->StackElement(stack_frame).GetMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4788 | size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor()); |
| 4789 | size_t method_name_index = method_names.IndexOf(m->GetName()); |
| 4790 | size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4791 | JDWP::Append2BE(bytes, class_name_index); |
| 4792 | JDWP::Append2BE(bytes, method_name_index); |
| 4793 | JDWP::Append2BE(bytes, file_name_index); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame^] | 4794 | JDWP::Append2BE(bytes, record->StackElement(stack_frame).ComputeLineNumber()); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4795 | } |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | // (xb) class name strings |
| 4799 | // (xb) method name strings |
| 4800 | // (xb) source file strings |
| 4801 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 4802 | class_names.WriteTo(bytes); |
| 4803 | method_names.WriteTo(bytes); |
| 4804 | filenames.WriteTo(bytes); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4805 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4806 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4807 | jbyteArray result = env->NewByteArray(bytes.size()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4808 | if (result != nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4809 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 4810 | } |
| 4811 | return result; |
| 4812 | } |
| 4813 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4814 | ArtMethod* DeoptimizationRequest::Method() const { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 4815 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 4816 | return soa.DecodeMethod(method_); |
| 4817 | } |
| 4818 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4819 | void DeoptimizationRequest::SetMethod(ArtMethod* m) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 4820 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 4821 | method_ = soa.EncodeMethod(m); |
| 4822 | } |
| 4823 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4824 | } // namespace art |