Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "debugger.h" |
| 18 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 19 | #include <sys/uio.h> |
| 20 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 21 | #include <set> |
| 22 | |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 23 | #include "arch/context.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 24 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "art_method-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 26 | #include "base/time_utils.h" |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 27 | #include "class_linker.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 29 | #include "dex_file-inl.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 30 | #include "dex_instruction.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 31 | #include "gc/accounting/card_table-inl.h" |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 32 | #include "gc/allocation_record.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 33 | #include "gc/space/large_object_space.h" |
| 34 | #include "gc/space/space-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 35 | #include "handle_scope.h" |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 36 | #include "jdwp/jdwp_priv.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 37 | #include "jdwp/object_registry.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | #include "mirror/class.h" |
| 39 | #include "mirror/class-inl.h" |
| 40 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 41 | #include "mirror/object-inl.h" |
| 42 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 43 | #include "mirror/string-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | #include "mirror/throwable.h" |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 45 | #include "quick/inline_method_analyser.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 46 | #include "reflection.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 47 | #include "safe_map.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 48 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 49 | #include "ScopedLocalRef.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 50 | #include "ScopedPrimitiveArray.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 51 | #include "handle_scope-inl.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 52 | #include "thread_list.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | #include "utf.h" |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 54 | #include "verifier/method_verifier-inl.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 55 | #include "well_known_classes.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 56 | |
Brian Carlstrom | 3d92d52 | 2013-07-12 09:03:08 -0700 | [diff] [blame] | 57 | #ifdef HAVE_ANDROID_OS |
| 58 | #include "cutils/properties.h" |
| 59 | #endif |
| 60 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 61 | namespace art { |
| 62 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 63 | // The key identifying the debugger to update instrumentation. |
| 64 | static constexpr const char* kDbgInstrumentationKey = "Debugger"; |
| 65 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 66 | // Limit alloc_record_count to the 2BE value (64k-1) that is the limit of the current protocol. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 67 | static uint16_t CappedAllocRecordCount(size_t alloc_record_count) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 68 | size_t cap = 0xffff; |
| 69 | #ifdef HAVE_ANDROID_OS |
| 70 | // Check whether there's a system property overriding the number of recent records. |
| 71 | const char* propertyName = "dalvik.vm.recentAllocMax"; |
| 72 | char recentAllocMaxString[PROPERTY_VALUE_MAX]; |
| 73 | if (property_get(propertyName, recentAllocMaxString, "") > 0) { |
| 74 | char* end; |
| 75 | size_t value = strtoul(recentAllocMaxString, &end, 10); |
| 76 | if (*end != '\0') { |
| 77 | LOG(ERROR) << "Ignoring " << propertyName << " '" << recentAllocMaxString |
| 78 | << "' --- invalid"; |
| 79 | } else { |
| 80 | cap = value; |
| 81 | } |
| 82 | } |
| 83 | #endif |
| 84 | if (alloc_record_count > cap) { |
| 85 | return cap; |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 86 | } |
| 87 | return alloc_record_count; |
| 88 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 89 | |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 90 | class Breakpoint { |
| 91 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 92 | Breakpoint(ArtMethod* method, uint32_t dex_pc, |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 93 | DeoptimizationRequest::Kind deoptimization_kind) |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 94 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 95 | : method_(nullptr), dex_pc_(dex_pc), deoptimization_kind_(deoptimization_kind) { |
| 96 | CHECK(deoptimization_kind_ == DeoptimizationRequest::kNothing || |
| 97 | deoptimization_kind_ == DeoptimizationRequest::kSelectiveDeoptimization || |
| 98 | deoptimization_kind_ == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 99 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 100 | method_ = soa.EncodeMethod(method); |
| 101 | } |
| 102 | |
| 103 | Breakpoint(const Breakpoint& other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 104 | : method_(nullptr), dex_pc_(other.dex_pc_), |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 105 | deoptimization_kind_(other.deoptimization_kind_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 106 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 107 | method_ = soa.EncodeMethod(other.Method()); |
| 108 | } |
| 109 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 110 | ArtMethod* Method() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 111 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 112 | return soa.DecodeMethod(method_); |
| 113 | } |
| 114 | |
| 115 | uint32_t DexPc() const { |
| 116 | return dex_pc_; |
| 117 | } |
| 118 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 119 | DeoptimizationRequest::Kind GetDeoptimizationKind() const { |
| 120 | return deoptimization_kind_; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | private: |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 124 | // The location of this breakpoint. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 125 | jmethodID method_; |
| 126 | uint32_t dex_pc_; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 127 | |
| 128 | // Indicates whether breakpoint needs full deoptimization or selective deoptimization. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 129 | DeoptimizationRequest::Kind deoptimization_kind_; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 132 | static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 133 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 134 | os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.Method()).c_str(), rhs.DexPc()); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 135 | return os; |
| 136 | } |
| 137 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 138 | class DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 139 | public: |
| 140 | DebugInstrumentationListener() {} |
| 141 | virtual ~DebugInstrumentationListener() {} |
| 142 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 143 | void MethodEntered(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 144 | uint32_t dex_pc) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 145 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 146 | if (method->IsNative()) { |
| 147 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 148 | return; |
| 149 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 150 | if (IsListeningToDexPcMoved()) { |
| 151 | // We also listen to kDexPcMoved instrumentation event so we know the DexPcMoved method is |
| 152 | // going to be called right after us. To avoid sending JDWP events twice for this location, |
| 153 | // we report the event in DexPcMoved. However, we must remind this is method entry so we |
| 154 | // send the METHOD_ENTRY event. And we can also group it with other events for this location |
| 155 | // like BREAKPOINT or SINGLE_STEP (or even METHOD_EXIT if this is a RETURN instruction). |
| 156 | thread->SetDebugMethodEntry(); |
| 157 | } else if (IsListeningToMethodExit() && IsReturn(method, dex_pc)) { |
| 158 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 159 | // RETURN so we know the MethodExited method is going to be called right after us. To avoid |
| 160 | // sending JDWP events twice for this location, we report the event(s) in MethodExited. |
| 161 | // However, we must remind this is method entry so we send the METHOD_ENTRY event. And we can |
| 162 | // also group it with other events for this location like BREAKPOINT or SINGLE_STEP. |
| 163 | thread->SetDebugMethodEntry(); |
| 164 | } else { |
| 165 | Dbg::UpdateDebugger(thread, this_object, method, 0, Dbg::kMethodEntry, nullptr); |
| 166 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 169 | void MethodExited(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 170 | uint32_t dex_pc, const JValue& return_value) |
| 171 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 172 | if (method->IsNative()) { |
| 173 | // TODO: post location events is a suspension point and native method entry stubs aren't. |
| 174 | return; |
| 175 | } |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 176 | uint32_t events = Dbg::kMethodExit; |
| 177 | if (thread->IsDebugMethodEntry()) { |
| 178 | // It is also the method entry. |
| 179 | DCHECK(IsReturn(method, dex_pc)); |
| 180 | events |= Dbg::kMethodEntry; |
| 181 | thread->ClearDebugMethodEntry(); |
| 182 | } |
| 183 | Dbg::UpdateDebugger(thread, this_object, method, dex_pc, events, &return_value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 186 | void MethodUnwind(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 187 | ArtMethod* method, uint32_t dex_pc) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 188 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 189 | // We're not recorded to listen to this kind of event, so complain. |
| 190 | LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method) |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 191 | << " " << dex_pc; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 194 | void DexPcMoved(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 195 | uint32_t new_dex_pc) |
| 196 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 197 | if (IsListeningToMethodExit() && IsReturn(method, new_dex_pc)) { |
| 198 | // We also listen to kMethodExited instrumentation event and the current instruction is a |
| 199 | // RETURN so we know the MethodExited method is going to be called right after us. Like in |
| 200 | // MethodEntered, we delegate event reporting to MethodExited. |
| 201 | // Besides, if this RETURN instruction is the only one in the method, we can send multiple |
| 202 | // JDWP events in the same packet: METHOD_ENTRY, METHOD_EXIT, BREAKPOINT and/or SINGLE_STEP. |
| 203 | // Therefore, we must not clear the debug method entry flag here. |
| 204 | } else { |
| 205 | uint32_t events = 0; |
| 206 | if (thread->IsDebugMethodEntry()) { |
| 207 | // It is also the method entry. |
| 208 | events = Dbg::kMethodEntry; |
| 209 | thread->ClearDebugMethodEntry(); |
| 210 | } |
| 211 | Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc, events, nullptr); |
| 212 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 215 | void FieldRead(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 216 | ArtMethod* method, uint32_t dex_pc, ArtField* field) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 217 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 218 | Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 219 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 220 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 221 | void FieldWritten(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 222 | ArtMethod* method, uint32_t dex_pc, ArtField* field, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 223 | const JValue& field_value) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 224 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 225 | Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value); |
| 226 | } |
| 227 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 228 | void ExceptionCaught(Thread* thread ATTRIBUTE_UNUSED, mirror::Throwable* exception_object) |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 229 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 230 | Dbg::PostException(exception_object); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 233 | // 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] | 234 | void BackwardBranch(Thread* /*thread*/, ArtMethod* method, int32_t dex_pc_offset) |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 235 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 236 | LOG(ERROR) << "Unexpected backward branch event in debugger " << PrettyMethod(method) |
| 237 | << " " << dex_pc_offset; |
| 238 | } |
| 239 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 240 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 241 | static bool IsReturn(ArtMethod* method, uint32_t dex_pc) |
Sebastien Hertz | 9d6bf69 | 2015-04-10 12:12:33 +0200 | [diff] [blame] | 242 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 243 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
| 244 | const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]); |
| 245 | return instruction->IsReturn(); |
| 246 | } |
| 247 | |
| 248 | static bool IsListeningToDexPcMoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 249 | return IsListeningTo(instrumentation::Instrumentation::kDexPcMoved); |
| 250 | } |
| 251 | |
| 252 | static bool IsListeningToMethodExit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 253 | return IsListeningTo(instrumentation::Instrumentation::kMethodExited); |
| 254 | } |
| 255 | |
| 256 | static bool IsListeningTo(instrumentation::Instrumentation::InstrumentationEvent event) |
| 257 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 258 | return (Dbg::GetInstrumentationEvents() & event) != 0; |
| 259 | } |
| 260 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 261 | DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 262 | } gDebugInstrumentationListener; |
| 263 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 264 | // JDWP is allowed unless the Zygote forbids it. |
| 265 | static bool gJdwpAllowed = true; |
| 266 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 267 | // Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line? |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 268 | static bool gJdwpConfigured = false; |
| 269 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 270 | // JDWP options for debugging. Only valid if IsJdwpConfigured() is true. |
| 271 | static JDWP::JdwpOptions gJdwpOptions; |
| 272 | |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 273 | // Runtime JDWP state. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 274 | static JDWP::JdwpState* gJdwpState = nullptr; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 275 | static bool gDebuggerConnected; // debugger or DDMS is connected. |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 276 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 277 | static bool gDdmThreadNotification = false; |
| 278 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 279 | // DDMS GC-related settings. |
| 280 | static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER; |
| 281 | static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 282 | static Dbg::HpsgWhat gDdmHpsgWhat; |
| 283 | static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER; |
| 284 | static Dbg::HpsgWhat gDdmNhsgWhat; |
| 285 | |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 286 | bool Dbg::gDebuggerActive = false; |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 287 | bool Dbg::gDisposed = false; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 288 | ObjectRegistry* Dbg::gRegistry = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 289 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 290 | // Deoptimization support. |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 291 | std::vector<DeoptimizationRequest> Dbg::deoptimization_requests_; |
| 292 | size_t Dbg::full_deoptimization_event_count_ = 0; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 293 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 294 | // Instrumentation event reference counters. |
| 295 | size_t Dbg::dex_pc_change_event_ref_count_ = 0; |
| 296 | size_t Dbg::method_enter_event_ref_count_ = 0; |
| 297 | size_t Dbg::method_exit_event_ref_count_ = 0; |
| 298 | size_t Dbg::field_read_event_ref_count_ = 0; |
| 299 | size_t Dbg::field_write_event_ref_count_ = 0; |
| 300 | size_t Dbg::exception_catch_event_ref_count_ = 0; |
| 301 | uint32_t Dbg::instrumentation_events_ = 0; |
| 302 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 303 | // Breakpoints. |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 304 | static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 305 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 306 | void DebugInvokeReq::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 307 | receiver.VisitRootIfNonNull(visitor, root_info); // null for static method call. |
| 308 | klass.VisitRoot(visitor, root_info); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 311 | void SingleStepControl::AddDexPc(uint32_t dex_pc) { |
| 312 | dex_pcs_.insert(dex_pc); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 315 | bool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const { |
| 316 | return dex_pcs_.find(dex_pc) == dex_pcs_.end(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 319 | static bool IsBreakpoint(const ArtMethod* m, uint32_t dex_pc) |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 320 | LOCKS_EXCLUDED(Locks::breakpoint_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 321 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 322 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 323 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 324 | if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].Method() == m) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 325 | VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i]; |
| 326 | return true; |
| 327 | } |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 332 | static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) |
| 333 | LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) { |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 334 | MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); |
| 335 | // A thread may be suspended for GC; in this code, we really want to know whether |
| 336 | // there's a debugger suspension active. |
| 337 | return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0; |
| 338 | } |
| 339 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 340 | static mirror::Array* DecodeNonNullArray(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 341 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 342 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 343 | if (o == nullptr) { |
| 344 | *error = JDWP::ERR_INVALID_OBJECT; |
| 345 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 346 | } |
| 347 | if (!o->IsArrayInstance()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 348 | *error = JDWP::ERR_INVALID_ARRAY; |
| 349 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 350 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 351 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 352 | return o->AsArray(); |
| 353 | } |
| 354 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 355 | static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 356 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 357 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 358 | if (o == nullptr) { |
| 359 | *error = JDWP::ERR_INVALID_OBJECT; |
| 360 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 361 | } |
| 362 | if (!o->IsClass()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 363 | *error = JDWP::ERR_INVALID_CLASS; |
| 364 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 365 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 366 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 367 | return o->AsClass(); |
| 368 | } |
| 369 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 370 | static Thread* DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, |
| 371 | JDWP::JdwpError* error) |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 372 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 373 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::thread_suspend_count_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 374 | mirror::Object* thread_peer = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_id, error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 375 | if (thread_peer == nullptr) { |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 376 | // This isn't even an object. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 377 | *error = JDWP::ERR_INVALID_OBJECT; |
| 378 | return nullptr; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 379 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 380 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 381 | 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] | 382 | if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) { |
| 383 | // This isn't a thread. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 384 | *error = JDWP::ERR_INVALID_THREAD; |
| 385 | return nullptr; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 388 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 389 | Thread* thread = Thread::FromManagedThread(soa, thread_peer); |
| 390 | // If thread is null then this a java.lang.Thread without a Thread*. Must be a un-started or a |
| 391 | // zombie. |
| 392 | *error = (thread == nullptr) ? JDWP::ERR_THREAD_NOT_ALIVE : JDWP::ERR_NONE; |
| 393 | return thread; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 396 | static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) { |
| 397 | // JDWP deliberately uses the descriptor characters' ASCII values for its enum. |
| 398 | // Note that by "basic" we mean that we don't get more specific than JT_OBJECT. |
| 399 | return static_cast<JDWP::JdwpTag>(descriptor[0]); |
| 400 | } |
| 401 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 402 | static JDWP::JdwpTag BasicTagFromClass(mirror::Class* klass) |
| 403 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 404 | std::string temp; |
| 405 | const char* descriptor = klass->GetDescriptor(&temp); |
| 406 | return BasicTagFromDescriptor(descriptor); |
| 407 | } |
| 408 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 409 | static JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 410 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 411 | CHECK(c != nullptr); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 412 | if (c->IsArrayClass()) { |
| 413 | return JDWP::JT_ARRAY; |
| 414 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 415 | if (c->IsStringClass()) { |
| 416 | return JDWP::JT_STRING; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 417 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 418 | if (c->IsClassClass()) { |
| 419 | return JDWP::JT_CLASS_OBJECT; |
| 420 | } |
| 421 | { |
| 422 | mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
| 423 | if (thread_class->IsAssignableFrom(c)) { |
| 424 | return JDWP::JT_THREAD; |
| 425 | } |
| 426 | } |
| 427 | { |
| 428 | mirror::Class* thread_group_class = |
| 429 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup); |
| 430 | if (thread_group_class->IsAssignableFrom(c)) { |
| 431 | return JDWP::JT_THREAD_GROUP; |
| 432 | } |
| 433 | } |
| 434 | { |
| 435 | mirror::Class* class_loader_class = |
| 436 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader); |
| 437 | if (class_loader_class->IsAssignableFrom(c)) { |
| 438 | return JDWP::JT_CLASS_LOADER; |
| 439 | } |
| 440 | } |
| 441 | return JDWP::JT_OBJECT; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Objects declared to hold Object might actually hold a more specific |
| 446 | * type. The debugger may take a special interest in these (e.g. it |
| 447 | * wants to display the contents of Strings), so we want to return an |
| 448 | * appropriate tag. |
| 449 | * |
| 450 | * Null objects are tagged JT_OBJECT. |
| 451 | */ |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 452 | JDWP::JdwpTag Dbg::TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 453 | return (o == nullptr) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass()); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static bool IsPrimitiveTag(JDWP::JdwpTag tag) { |
| 457 | switch (tag) { |
| 458 | case JDWP::JT_BOOLEAN: |
| 459 | case JDWP::JT_BYTE: |
| 460 | case JDWP::JT_CHAR: |
| 461 | case JDWP::JT_FLOAT: |
| 462 | case JDWP::JT_DOUBLE: |
| 463 | case JDWP::JT_INT: |
| 464 | case JDWP::JT_LONG: |
| 465 | case JDWP::JT_SHORT: |
| 466 | case JDWP::JT_VOID: |
| 467 | return true; |
| 468 | default: |
| 469 | return false; |
| 470 | } |
| 471 | } |
| 472 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 473 | void Dbg::StartJdwp() { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 474 | if (!gJdwpAllowed || !IsJdwpConfigured()) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 475 | // No JDWP for you! |
| 476 | return; |
| 477 | } |
| 478 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 479 | CHECK(gRegistry == nullptr); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 480 | gRegistry = new ObjectRegistry; |
| 481 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 482 | // Init JDWP if the debugger is enabled. This may connect out to a |
| 483 | // debugger, passively listen for a debugger, or block waiting for a |
| 484 | // debugger. |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 485 | gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 486 | if (gJdwpState == nullptr) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 487 | // We probably failed because some other process has the port already, which means that |
| 488 | // if we don't abort the user is likely to think they're talking to us when they're actually |
| 489 | // talking to that other process. |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 490 | LOG(FATAL) << "Debugger thread failed to initialize"; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // If a debugger has already attached, send the "welcome" message. |
| 494 | // This may cause us to suspend all threads. |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 495 | if (gJdwpState->IsActive()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 496 | ScopedObjectAccess soa(Thread::Current()); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 497 | gJdwpState->PostVMStart(); |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 498 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 501 | void Dbg::StopJdwp() { |
Sebastien Hertz | c6345ef | 2014-08-18 19:26:39 +0200 | [diff] [blame] | 502 | // Post VM_DEATH event before the JDWP connection is closed (either by the JDWP thread or the |
| 503 | // destruction of gJdwpState). |
| 504 | if (gJdwpState != nullptr && gJdwpState->IsActive()) { |
| 505 | gJdwpState->PostVMDeath(); |
| 506 | } |
Sebastien Hertz | 0376e6b | 2014-02-06 18:12:59 +0100 | [diff] [blame] | 507 | // 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] | 508 | Dispose(); |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 509 | delete gJdwpState; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 510 | gJdwpState = nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 511 | delete gRegistry; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 512 | gRegistry = nullptr; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 515 | void Dbg::GcDidFinish() { |
| 516 | if (gDdmHpifWhen != HPIF_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 517 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 518 | VLOG(jdwp) << "Sending heap info to DDM"; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 519 | DdmSendHeapInfo(gDdmHpifWhen); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 520 | } |
| 521 | if (gDdmHpsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 522 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 523 | VLOG(jdwp) << "Dumping heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 524 | DdmSendHeapSegments(false); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 525 | } |
| 526 | if (gDdmNhsgWhen != HPSG_WHEN_NEVER) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 527 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 528 | VLOG(jdwp) << "Dumping native heap to DDM"; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 529 | DdmSendHeapSegments(true); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 533 | void Dbg::SetJdwpAllowed(bool allowed) { |
| 534 | gJdwpAllowed = allowed; |
| 535 | } |
| 536 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 537 | DebugInvokeReq* Dbg::GetInvokeReq() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 538 | return Thread::Current()->GetInvokeReq(); |
| 539 | } |
| 540 | |
| 541 | Thread* Dbg::GetDebugThread() { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 542 | return (gJdwpState != nullptr) ? gJdwpState->GetDebugThread() : nullptr; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | void Dbg::ClearWaitForEventThread() { |
Sebastien Hertz | 2bf93f4 | 2015-01-09 18:44:05 +0100 | [diff] [blame] | 546 | gJdwpState->ReleaseJdwpTokenForEvent(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | void Dbg::Connected() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 550 | CHECK(!gDebuggerConnected); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 551 | VLOG(jdwp) << "JDWP has attached"; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 552 | gDebuggerConnected = true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 553 | gDisposed = false; |
| 554 | } |
| 555 | |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 556 | bool Dbg::RequiresDeoptimization() { |
| 557 | // We don't need deoptimization if everything runs with interpreter after |
| 558 | // enabling -Xint mode. |
| 559 | return !Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly(); |
| 560 | } |
| 561 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 562 | void Dbg::GoActive() { |
| 563 | // Enable all debugging features, including scans for breakpoints. |
| 564 | // This is a no-op if we're already active. |
| 565 | // Only called from the JDWP handler thread. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 566 | if (IsDebuggerActive()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 567 | return; |
| 568 | } |
| 569 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 570 | { |
| 571 | // 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] | 572 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 573 | CHECK_EQ(gBreakpoints.size(), 0U); |
| 574 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 575 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 576 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 577 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 578 | CHECK_EQ(deoptimization_requests_.size(), 0U); |
| 579 | CHECK_EQ(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 580 | CHECK_EQ(dex_pc_change_event_ref_count_, 0U); |
| 581 | CHECK_EQ(method_enter_event_ref_count_, 0U); |
| 582 | CHECK_EQ(method_exit_event_ref_count_, 0U); |
| 583 | CHECK_EQ(field_read_event_ref_count_, 0U); |
| 584 | CHECK_EQ(field_write_event_ref_count_, 0U); |
| 585 | CHECK_EQ(exception_catch_event_ref_count_, 0U); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 588 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 589 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 590 | Thread* self = Thread::Current(); |
| 591 | ThreadState old_state = self->SetStateUnsafe(kRunnable); |
| 592 | CHECK_NE(old_state, kRunnable); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 593 | if (RequiresDeoptimization()) { |
| 594 | runtime->GetInstrumentation()->EnableDeoptimization(); |
| 595 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 596 | instrumentation_events_ = 0; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 597 | gDebuggerActive = true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 598 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 599 | runtime->GetThreadList()->ResumeAll(); |
| 600 | |
| 601 | LOG(INFO) << "Debugger is active"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | void Dbg::Disconnected() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 605 | CHECK(gDebuggerConnected); |
| 606 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 607 | LOG(INFO) << "Debugger is no longer active"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 608 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 609 | // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread |
| 610 | // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener |
| 611 | // and clear the object registry. |
| 612 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 613 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 614 | Thread* self = Thread::Current(); |
| 615 | ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 616 | |
| 617 | // Debugger may not be active at this point. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 618 | if (IsDebuggerActive()) { |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 619 | { |
| 620 | // Since we're going to disable deoptimization, we clear the deoptimization requests queue. |
| 621 | // This prevents us from having any pending deoptimization request when the debugger attaches |
| 622 | // to us again while no event has been requested yet. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 623 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 624 | deoptimization_requests_.clear(); |
| 625 | full_deoptimization_event_count_ = 0U; |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 626 | } |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 627 | if (instrumentation_events_ != 0) { |
| 628 | runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener, |
| 629 | instrumentation_events_); |
| 630 | instrumentation_events_ = 0; |
| 631 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 632 | if (RequiresDeoptimization()) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 633 | runtime->GetInstrumentation()->DisableDeoptimization(kDbgInstrumentationKey); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 634 | } |
Sebastien Hertz | aaea734 | 2014-02-25 15:10:04 +0100 | [diff] [blame] | 635 | gDebuggerActive = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 636 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 637 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 638 | runtime->GetThreadList()->ResumeAll(); |
Sebastien Hertz | 55f6534 | 2015-01-13 22:48:34 +0100 | [diff] [blame] | 639 | |
| 640 | { |
| 641 | ScopedObjectAccess soa(self); |
| 642 | gRegistry->Clear(); |
| 643 | } |
| 644 | |
| 645 | gDebuggerConnected = false; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Sebastien Hertz | b3b173b | 2015-02-06 09:16:32 +0100 | [diff] [blame] | 648 | void Dbg::ConfigureJdwp(const JDWP::JdwpOptions& jdwp_options) { |
| 649 | CHECK_NE(jdwp_options.transport, JDWP::kJdwpTransportUnknown); |
| 650 | gJdwpOptions = jdwp_options; |
| 651 | gJdwpConfigured = true; |
| 652 | } |
| 653 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 654 | bool Dbg::IsJdwpConfigured() { |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 655 | return gJdwpConfigured; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | int64_t Dbg::LastDebuggerActivity() { |
Elliott Hughes | ca95152 | 2011-12-05 12:01:32 -0800 | [diff] [blame] | 659 | return gJdwpState->LastDebuggerActivity(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 662 | void Dbg::UndoDebuggerSuspensions() { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 663 | Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 666 | std::string Dbg::GetClassName(JDWP::RefTypeId class_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 667 | JDWP::JdwpError error; |
| 668 | mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id, &error); |
| 669 | if (o == nullptr) { |
| 670 | if (error == JDWP::ERR_NONE) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 671 | return "null"; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 672 | } else { |
| 673 | return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id)); |
| 674 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 675 | } |
| 676 | if (!o->IsClass()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 677 | 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] | 678 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 679 | return GetClassName(o->AsClass()); |
| 680 | } |
| 681 | |
| 682 | std::string Dbg::GetClassName(mirror::Class* klass) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 683 | if (klass == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 684 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 685 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 686 | std::string temp; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 687 | return DescriptorToName(klass->GetDescriptor(&temp)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 690 | JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId* class_object_id) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 691 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 692 | mirror::Class* c = DecodeClass(id, &status); |
| 693 | if (c == nullptr) { |
| 694 | *class_object_id = 0; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 695 | return status; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 696 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 697 | *class_object_id = gRegistry->Add(c); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 698 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 701 | JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId* superclass_id) { |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 702 | JDWP::JdwpError status; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 703 | mirror::Class* c = DecodeClass(id, &status); |
| 704 | if (c == nullptr) { |
| 705 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 706 | return status; |
| 707 | } |
| 708 | if (c->IsInterface()) { |
| 709 | // http://code.google.com/p/android/issues/detail?id=20856 |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 710 | *superclass_id = 0; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 711 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 712 | *superclass_id = gRegistry->Add(c->GetSuperClass()); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 713 | } |
| 714 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 717 | JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 718 | JDWP::JdwpError error; |
| 719 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 720 | if (o == nullptr) { |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 721 | return JDWP::ERR_INVALID_OBJECT; |
| 722 | } |
| 723 | expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader())); |
| 724 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 727 | JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 728 | JDWP::JdwpError error; |
| 729 | mirror::Class* c = DecodeClass(id, &error); |
| 730 | if (c == nullptr) { |
| 731 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 732 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 733 | |
| 734 | uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask; |
| 735 | |
Yevgeny Rouban | de34eea | 2014-02-15 01:06:03 +0700 | [diff] [blame] | 736 | // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set, |
| 737 | // not interfaces. |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 738 | // 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] | 739 | if ((access_flags & kAccInterface) == 0) { |
| 740 | access_flags |= kAccSuper; |
| 741 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 742 | |
| 743 | expandBufAdd4BE(pReply, access_flags); |
| 744 | |
| 745 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 748 | JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) { |
| 749 | JDWP::JdwpError error; |
| 750 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 751 | if (o == nullptr) { |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 752 | return JDWP::ERR_INVALID_OBJECT; |
| 753 | } |
| 754 | |
| 755 | // Ensure all threads are suspended while we read objects' lock words. |
| 756 | Thread* self = Thread::Current(); |
Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 757 | CHECK_EQ(self->GetState(), kRunnable); |
| 758 | self->TransitionFromRunnableToSuspended(kSuspended); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 759 | Runtime::Current()->GetThreadList()->SuspendAll(__FUNCTION__); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 760 | |
| 761 | MonitorInfo monitor_info(o); |
| 762 | |
Sebastien Hertz | 5426324 | 2014-03-19 18:16:50 +0100 | [diff] [blame] | 763 | Runtime::Current()->GetThreadList()->ResumeAll(); |
| 764 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 765 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 766 | if (monitor_info.owner_ != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 767 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 768 | } else { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 769 | expandBufAddObjectId(reply, gRegistry->Add(nullptr)); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 770 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 771 | expandBufAdd4BE(reply, monitor_info.entry_count_); |
| 772 | expandBufAdd4BE(reply, monitor_info.waiters_.size()); |
| 773 | for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) { |
| 774 | expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer())); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 775 | } |
| 776 | return JDWP::ERR_NONE; |
| 777 | } |
| 778 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 779 | JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 780 | std::vector<JDWP::ObjectId>* monitors, |
| 781 | std::vector<uint32_t>* stack_depths) { |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 782 | struct OwnedMonitorVisitor : public StackVisitor { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 783 | OwnedMonitorVisitor(Thread* thread, Context* context, |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 784 | std::vector<JDWP::ObjectId>* monitor_vector, |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 785 | std::vector<uint32_t>* stack_depth_vector) |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 786 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 787 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 788 | current_stack_depth(0), |
| 789 | monitors(monitor_vector), |
| 790 | stack_depths(stack_depth_vector) {} |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 791 | |
| 792 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 793 | // annotalysis. |
| 794 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 795 | if (!GetMethod()->IsRuntimeMethod()) { |
| 796 | Monitor::VisitLocks(this, AppendOwnedMonitors, this); |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 797 | ++current_stack_depth; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 798 | } |
| 799 | return true; |
| 800 | } |
| 801 | |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 802 | static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) |
| 803 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 804 | OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 805 | visitor->monitors->push_back(gRegistry->Add(owned_monitor)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 806 | visitor->stack_depths->push_back(visitor->current_stack_depth); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 807 | } |
| 808 | |
Elliott Hughes | 734b8c6 | 2013-01-11 15:32:45 -0800 | [diff] [blame] | 809 | size_t current_stack_depth; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 810 | std::vector<JDWP::ObjectId>* const monitors; |
| 811 | std::vector<uint32_t>* const stack_depths; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 812 | }; |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 813 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 814 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 815 | JDWP::JdwpError error; |
| 816 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 817 | if (thread == nullptr) { |
| 818 | return error; |
| 819 | } |
| 820 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 821 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 822 | } |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 823 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 824 | OwnedMonitorVisitor visitor(thread, context.get(), monitors, stack_depths); |
Hiroshi Yamauchi | cc8c5c5 | 2014-06-13 15:08:05 -0700 | [diff] [blame] | 825 | visitor.WalkStack(); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 826 | return JDWP::ERR_NONE; |
| 827 | } |
| 828 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 829 | JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 830 | JDWP::ObjectId* contended_monitor) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 831 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 832 | *contended_monitor = 0; |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 833 | JDWP::JdwpError error; |
| 834 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 835 | if (thread == nullptr) { |
| 836 | return error; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 837 | } |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 838 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 839 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 840 | } |
| 841 | mirror::Object* contended_monitor_obj = Monitor::GetContendedMonitor(thread); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 842 | // Add() requires the thread_list_lock_ not held to avoid the lock |
| 843 | // level violation. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 844 | *contended_monitor = gRegistry->Add(contended_monitor_obj); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 845 | return JDWP::ERR_NONE; |
| 846 | } |
| 847 | |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 848 | JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 849 | std::vector<uint64_t>* counts) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 850 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 851 | heap->CollectGarbage(false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 852 | std::vector<mirror::Class*> classes; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 853 | counts->clear(); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 854 | for (size_t i = 0; i < class_ids.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 855 | JDWP::JdwpError error; |
| 856 | mirror::Class* c = DecodeClass(class_ids[i], &error); |
| 857 | if (c == nullptr) { |
| 858 | return error; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 859 | } |
| 860 | classes.push_back(c); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 861 | counts->push_back(0); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 862 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 863 | heap->CountInstances(classes, false, &(*counts)[0]); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 864 | return JDWP::ERR_NONE; |
| 865 | } |
| 866 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 867 | JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, |
| 868 | std::vector<JDWP::ObjectId>* instances) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 869 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 870 | // We only want reachable instances, so do a GC. |
| 871 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 872 | JDWP::JdwpError error; |
| 873 | mirror::Class* c = DecodeClass(class_id, &error); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 874 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 875 | return error; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 876 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 877 | std::vector<mirror::Object*> raw_instances; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 878 | Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances); |
| 879 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 880 | instances->push_back(gRegistry->Add(raw_instances[i])); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 881 | } |
| 882 | return JDWP::ERR_NONE; |
| 883 | } |
| 884 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 885 | JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 886 | std::vector<JDWP::ObjectId>* referring_objects) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 887 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 888 | heap->CollectGarbage(false); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 889 | JDWP::JdwpError error; |
| 890 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 891 | if (o == nullptr) { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 892 | return JDWP::ERR_INVALID_OBJECT; |
| 893 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 894 | std::vector<mirror::Object*> raw_instances; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 895 | heap->GetReferringObjects(o, max_count, raw_instances); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 896 | for (size_t i = 0; i < raw_instances.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 897 | referring_objects->push_back(gRegistry->Add(raw_instances[i])); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 898 | } |
| 899 | return JDWP::ERR_NONE; |
| 900 | } |
| 901 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 902 | JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id) { |
| 903 | JDWP::JdwpError error; |
| 904 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 905 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 906 | return JDWP::ERR_INVALID_OBJECT; |
| 907 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 908 | gRegistry->DisableCollection(object_id); |
| 909 | return JDWP::ERR_NONE; |
| 910 | } |
| 911 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 912 | JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id) { |
| 913 | JDWP::JdwpError error; |
| 914 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 915 | // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI |
| 916 | // also ignores these cases and never return an error. However it's not obvious why this command |
| 917 | // should behave differently from DisableCollection and IsCollected commands. So let's be more |
| 918 | // strict and return an error if this happens. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 919 | if (o == nullptr) { |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 920 | return JDWP::ERR_INVALID_OBJECT; |
| 921 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 922 | gRegistry->EnableCollection(object_id); |
| 923 | return JDWP::ERR_NONE; |
| 924 | } |
| 925 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 926 | JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool* is_collected) { |
| 927 | *is_collected = true; |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 928 | if (object_id == 0) { |
| 929 | // Null object id is invalid. |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 930 | return JDWP::ERR_INVALID_OBJECT; |
| 931 | } |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 932 | // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However |
| 933 | // the RI seems to ignore this and assume object has been collected. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 934 | JDWP::JdwpError error; |
| 935 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 936 | if (o != nullptr) { |
| 937 | *is_collected = gRegistry->IsCollected(object_id); |
Sebastien Hertz | 65637eb | 2014-01-10 17:40:02 +0100 | [diff] [blame] | 938 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 939 | return JDWP::ERR_NONE; |
| 940 | } |
| 941 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 942 | void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 943 | gRegistry->DisposeObject(object_id, reference_count); |
| 944 | } |
| 945 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 946 | JDWP::JdwpTypeTag Dbg::GetTypeTag(mirror::Class* klass) { |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 947 | DCHECK(klass != nullptr); |
| 948 | if (klass->IsArrayClass()) { |
| 949 | return JDWP::TT_ARRAY; |
| 950 | } else if (klass->IsInterface()) { |
| 951 | return JDWP::TT_INTERFACE; |
| 952 | } else { |
| 953 | return JDWP::TT_CLASS; |
| 954 | } |
| 955 | } |
| 956 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 957 | JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 958 | JDWP::JdwpError error; |
| 959 | mirror::Class* c = DecodeClass(class_id, &error); |
| 960 | if (c == nullptr) { |
| 961 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 962 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 963 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 964 | JDWP::JdwpTypeTag type_tag = GetTypeTag(c); |
| 965 | expandBufAdd1(pReply, type_tag); |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 966 | expandBufAddRefTypeId(pReply, class_id); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 967 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 970 | void Dbg::GetClassList(std::vector<JDWP::RefTypeId>* classes) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 971 | // Get the complete list of reference classes (i.e. all classes except |
| 972 | // the primitive types). |
| 973 | // Returns a newly-allocated buffer full of RefTypeId values. |
| 974 | struct ClassListCreator { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 975 | explicit ClassListCreator(std::vector<JDWP::RefTypeId>* classes_in) : classes(classes_in) { |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 976 | } |
| 977 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 978 | static bool Visit(mirror::Class* c, void* arg) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 979 | return reinterpret_cast<ClassListCreator*>(arg)->Visit(c); |
| 980 | } |
| 981 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 982 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 983 | // annotalysis. |
| 984 | bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 985 | if (!c->IsPrimitive()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 986 | classes->push_back(gRegistry->AddRefType(c)); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 987 | } |
| 988 | return true; |
| 989 | } |
| 990 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 991 | std::vector<JDWP::RefTypeId>* const classes; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 992 | }; |
| 993 | |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 994 | ClassListCreator clc(classes); |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 995 | Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(ClassListCreator::Visit, |
| 996 | &clc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 999 | JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, |
| 1000 | uint32_t* pStatus, std::string* pDescriptor) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1001 | JDWP::JdwpError error; |
| 1002 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1003 | if (c == nullptr) { |
| 1004 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1007 | if (c->IsArrayClass()) { |
| 1008 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED; |
| 1009 | *pTypeTag = JDWP::TT_ARRAY; |
| 1010 | } else { |
| 1011 | if (c->IsErroneous()) { |
| 1012 | *pStatus = JDWP::CS_ERROR; |
| 1013 | } else { |
| 1014 | *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED; |
| 1015 | } |
| 1016 | *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS; |
| 1017 | } |
| 1018 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1019 | if (pDescriptor != nullptr) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1020 | std::string temp; |
| 1021 | *pDescriptor = c->GetDescriptor(&temp); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1022 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1023 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1026 | void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>* ids) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1027 | std::vector<mirror::Class*> classes; |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1028 | Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1029 | ids->clear(); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1030 | for (size_t i = 0; i < classes.size(); ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1031 | ids->push_back(gRegistry->Add(classes[i])); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1032 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1035 | JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) { |
| 1036 | JDWP::JdwpError error; |
| 1037 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1038 | if (o == nullptr) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1039 | return JDWP::ERR_INVALID_OBJECT; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 1040 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1041 | |
Sebastien Hertz | 4d8fd49 | 2014-03-28 16:29:41 +0100 | [diff] [blame] | 1042 | JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass()); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1043 | JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1044 | |
| 1045 | expandBufAdd1(pReply, type_tag); |
| 1046 | expandBufAddRefTypeId(pReply, type_id); |
| 1047 | |
| 1048 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 1051 | JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1052 | JDWP::JdwpError error; |
| 1053 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1054 | if (c == nullptr) { |
| 1055 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1056 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1057 | std::string temp; |
| 1058 | *signature = c->GetDescriptor(&temp); |
Elliott Hughes | 1fe7afb | 2012-02-13 17:23:03 -0800 | [diff] [blame] | 1059 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1062 | JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string* result) { |
| 1063 | JDWP::JdwpError error; |
| 1064 | mirror::Class* c = DecodeClass(class_id, &error); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1065 | if (c == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1066 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1067 | } |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1068 | const char* source_file = c->GetSourceFile(); |
| 1069 | if (source_file == nullptr) { |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 1070 | return JDWP::ERR_ABSENT_INFORMATION; |
| 1071 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1072 | *result = source_file; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1073 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1076 | JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t* tag) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1077 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1078 | JDWP::JdwpError error; |
| 1079 | mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 1080 | if (error != JDWP::ERR_NONE) { |
| 1081 | *tag = JDWP::JT_VOID; |
| 1082 | return error; |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1083 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1084 | *tag = TagFromObject(soa, o); |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 1085 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1088 | size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1089 | switch (tag) { |
| 1090 | case JDWP::JT_VOID: |
| 1091 | return 0; |
| 1092 | case JDWP::JT_BYTE: |
| 1093 | case JDWP::JT_BOOLEAN: |
| 1094 | return 1; |
| 1095 | case JDWP::JT_CHAR: |
| 1096 | case JDWP::JT_SHORT: |
| 1097 | return 2; |
| 1098 | case JDWP::JT_FLOAT: |
| 1099 | case JDWP::JT_INT: |
| 1100 | return 4; |
| 1101 | case JDWP::JT_ARRAY: |
| 1102 | case JDWP::JT_OBJECT: |
| 1103 | case JDWP::JT_STRING: |
| 1104 | case JDWP::JT_THREAD: |
| 1105 | case JDWP::JT_THREAD_GROUP: |
| 1106 | case JDWP::JT_CLASS_LOADER: |
| 1107 | case JDWP::JT_CLASS_OBJECT: |
| 1108 | return sizeof(JDWP::ObjectId); |
| 1109 | case JDWP::JT_DOUBLE: |
| 1110 | case JDWP::JT_LONG: |
| 1111 | return 8; |
| 1112 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 1113 | LOG(FATAL) << "Unknown tag " << tag; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1114 | return -1; |
| 1115 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1118 | JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int32_t* length) { |
| 1119 | JDWP::JdwpError error; |
| 1120 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
| 1121 | if (a == nullptr) { |
| 1122 | return error; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1123 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1124 | *length = a->GetLength(); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1125 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1128 | 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] | 1129 | JDWP::JdwpError error; |
| 1130 | mirror::Array* a = DecodeNonNullArray(array_id, &error); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1131 | if (a == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1132 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1133 | } |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1134 | |
| 1135 | if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) { |
| 1136 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1137 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1138 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1139 | JDWP::JdwpTag element_tag = BasicTagFromClass(a->GetClass()->GetComponentType()); |
| 1140 | expandBufAdd1(pReply, element_tag); |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1141 | expandBufAdd4BE(pReply, count); |
| 1142 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1143 | if (IsPrimitiveTag(element_tag)) { |
| 1144 | size_t width = GetTagWidth(element_tag); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1145 | uint8_t* dst = expandBufAddSpace(pReply, count * width); |
| 1146 | if (width == 8) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1147 | 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] | 1148 | for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]); |
| 1149 | } else if (width == 4) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1150 | 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] | 1151 | for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]); |
| 1152 | } else if (width == 2) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1153 | 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] | 1154 | for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]); |
| 1155 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1156 | 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] | 1157 | memcpy(dst, &src[offset * width], count * width); |
| 1158 | } |
| 1159 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1160 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1161 | mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>(); |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1162 | for (int i = 0; i < count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1163 | mirror::Object* element = oa->Get(offset + i); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1164 | JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element) |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1165 | : element_tag; |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 1166 | expandBufAdd1(pReply, specific_tag); |
| 1167 | expandBufAddObjectId(pReply, gRegistry->Add(element)); |
| 1168 | } |
| 1169 | } |
| 1170 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1171 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1174 | template <typename T> |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1175 | 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] | 1176 | NO_THREAD_SAFETY_ANALYSIS { |
| 1177 | // TODO: fix when annotalysis correctly handles non-member functions. |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1178 | DCHECK(a->GetClass()->IsPrimitiveArray()); |
| 1179 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1180 | T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1181 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1182 | *dst++ = src->ReadValue(sizeof(T)); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1186 | JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1187 | JDWP::Request* request) { |
| 1188 | JDWP::JdwpError error; |
| 1189 | mirror::Array* dst = DecodeNonNullArray(array_id, &error); |
| 1190 | if (dst == nullptr) { |
| 1191 | return error; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1192 | } |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1193 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1194 | if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) { |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1195 | LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count; |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1196 | return JDWP::ERR_INVALID_LENGTH; |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1197 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1198 | JDWP::JdwpTag element_tag = BasicTagFromClass(dst->GetClass()->GetComponentType()); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1199 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1200 | if (IsPrimitiveTag(element_tag)) { |
| 1201 | size_t width = GetTagWidth(element_tag); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1202 | if (width == 8) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1203 | CopyArrayData<uint64_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1204 | } else if (width == 4) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1205 | CopyArrayData<uint32_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1206 | } else if (width == 2) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1207 | CopyArrayData<uint16_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1208 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1209 | CopyArrayData<uint8_t>(dst, request, offset, count); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1210 | } |
| 1211 | } else { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 1212 | mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>(); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1213 | for (int i = 0; i < count; ++i) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1214 | JDWP::ObjectId id = request->ReadObjectId(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1215 | mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error); |
| 1216 | if (error != JDWP::ERR_NONE) { |
| 1217 | return error; |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1218 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1219 | oa->Set<false>(offset + i, o); |
Elliott Hughes | f03b8f6 | 2011-12-02 14:26:25 -0800 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | |
Elliott Hughes | 3d1ca6d | 2012-02-13 15:43:19 -0800 | [diff] [blame] | 1223 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1226 | JDWP::JdwpError Dbg::CreateString(const std::string& str, JDWP::ObjectId* new_string_id) { |
| 1227 | Thread* self = Thread::Current(); |
| 1228 | mirror::String* new_string = mirror::String::AllocFromModifiedUtf8(self, str.c_str()); |
| 1229 | if (new_string == nullptr) { |
| 1230 | DCHECK(self->IsExceptionPending()); |
| 1231 | self->ClearException(); |
| 1232 | LOG(ERROR) << "Could not allocate string"; |
| 1233 | *new_string_id = 0; |
| 1234 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1235 | } |
| 1236 | *new_string_id = gRegistry->Add(new_string); |
| 1237 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1240 | 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] | 1241 | JDWP::JdwpError error; |
| 1242 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1243 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1244 | *new_object_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1245 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1246 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1247 | Thread* self = Thread::Current(); |
| 1248 | mirror::Object* new_object = c->AllocObject(self); |
| 1249 | if (new_object == nullptr) { |
| 1250 | DCHECK(self->IsExceptionPending()); |
| 1251 | self->ClearException(); |
| 1252 | LOG(ERROR) << "Could not allocate object of type " << PrettyDescriptor(c); |
| 1253 | *new_object_id = 0; |
| 1254 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1255 | } |
| 1256 | *new_object_id = gRegistry->Add(new_object); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1257 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Elliott Hughes | bf13d36 | 2011-12-08 15:51:37 -0800 | [diff] [blame] | 1260 | /* |
| 1261 | * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]". |
| 1262 | */ |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1263 | JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length, |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1264 | JDWP::ObjectId* new_array_id) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1265 | JDWP::JdwpError error; |
| 1266 | mirror::Class* c = DecodeClass(array_class_id, &error); |
| 1267 | if (c == nullptr) { |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1268 | *new_array_id = 0; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1269 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1270 | } |
Sebastien Hertz | 2c3e77a | 2015-04-02 16:26:48 +0200 | [diff] [blame] | 1271 | Thread* self = Thread::Current(); |
| 1272 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1273 | mirror::Array* new_array = mirror::Array::Alloc<true>(self, c, length, |
| 1274 | c->GetComponentSizeShift(), |
| 1275 | heap->GetCurrentAllocator()); |
| 1276 | if (new_array == nullptr) { |
| 1277 | DCHECK(self->IsExceptionPending()); |
| 1278 | self->ClearException(); |
| 1279 | LOG(ERROR) << "Could not allocate array of type " << PrettyDescriptor(c); |
| 1280 | *new_array_id = 0; |
| 1281 | return JDWP::ERR_OUT_OF_MEMORY; |
| 1282 | } |
| 1283 | *new_array_id = gRegistry->Add(new_array); |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1284 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1287 | JDWP::FieldId Dbg::ToFieldId(const ArtField* f) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1288 | return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1291 | static JDWP::MethodId ToMethodId(const ArtMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1292 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1293 | return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1294 | } |
| 1295 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1296 | static ArtField* FromFieldId(JDWP::FieldId fid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1297 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1298 | return reinterpret_cast<ArtField*>(static_cast<uintptr_t>(fid)); |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1301 | static ArtMethod* FromMethodId(JDWP::MethodId mid) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1302 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1303 | return reinterpret_cast<ArtMethod*>(static_cast<uintptr_t>(mid)); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1304 | } |
| 1305 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1306 | bool Dbg::MatchThread(JDWP::ObjectId expected_thread_id, Thread* event_thread) { |
| 1307 | CHECK(event_thread != nullptr); |
| 1308 | JDWP::JdwpError error; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1309 | mirror::Object* expected_thread_peer = gRegistry->Get<mirror::Object*>( |
| 1310 | expected_thread_id, &error); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1311 | return expected_thread_peer == event_thread->GetPeer(); |
| 1312 | } |
| 1313 | |
| 1314 | bool Dbg::MatchLocation(const JDWP::JdwpLocation& expected_location, |
| 1315 | const JDWP::EventLocation& event_location) { |
| 1316 | if (expected_location.dex_pc != event_location.dex_pc) { |
| 1317 | return false; |
| 1318 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1319 | ArtMethod* m = FromMethodId(expected_location.method_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1320 | return m == event_location.method; |
| 1321 | } |
| 1322 | |
| 1323 | bool Dbg::MatchType(mirror::Class* event_class, JDWP::RefTypeId class_id) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1324 | if (event_class == nullptr) { |
| 1325 | return false; |
| 1326 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1327 | JDWP::JdwpError error; |
| 1328 | mirror::Class* expected_class = DecodeClass(class_id, &error); |
| 1329 | CHECK(expected_class != nullptr); |
| 1330 | return expected_class->IsAssignableFrom(event_class); |
| 1331 | } |
| 1332 | |
| 1333 | 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] | 1334 | ArtField* event_field) { |
| 1335 | ArtField* expected_field = FromFieldId(expected_field_id); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1336 | if (expected_field != event_field) { |
| 1337 | return false; |
| 1338 | } |
| 1339 | return Dbg::MatchType(event_field->GetDeclaringClass(), expected_type_id); |
| 1340 | } |
| 1341 | |
| 1342 | bool Dbg::MatchInstance(JDWP::ObjectId expected_instance_id, mirror::Object* event_instance) { |
| 1343 | JDWP::JdwpError error; |
| 1344 | mirror::Object* modifier_instance = gRegistry->Get<mirror::Object*>(expected_instance_id, &error); |
| 1345 | return modifier_instance == event_instance; |
| 1346 | } |
| 1347 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1348 | void Dbg::SetJdwpLocation(JDWP::JdwpLocation* location, ArtMethod* m, uint32_t dex_pc) |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 1349 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 1350 | LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 1351 | Locks::thread_suspend_count_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1352 | if (m == nullptr) { |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1353 | memset(location, 0, sizeof(*location)); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 1354 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1355 | mirror::Class* c = m->GetDeclaringClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1356 | location->type_tag = GetTypeTag(c); |
| 1357 | location->class_id = gRegistry->AddRefType(c); |
| 1358 | location->method_id = ToMethodId(m); |
| 1359 | 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] | 1360 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1361 | } |
| 1362 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1363 | std::string Dbg::GetMethodName(JDWP::MethodId method_id) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1364 | ArtMethod* m = FromMethodId(method_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1365 | if (m == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1366 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1367 | } |
Sebastien Hertz | 415fd08 | 2015-06-01 11:42:27 +0200 | [diff] [blame] | 1368 | return m->GetInterfaceMethodIfProxy(sizeof(void*))->GetName(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1371 | std::string Dbg::GetFieldName(JDWP::FieldId field_id) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1372 | ArtField* f = FromFieldId(field_id); |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1373 | if (f == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1374 | return "null"; |
Sebastien Hertz | a9aa0ff | 2014-09-19 12:07:51 +0200 | [diff] [blame] | 1375 | } |
| 1376 | return f->GetName(); |
Elliott Hughes | a96836a | 2013-01-17 12:27:49 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1379 | /* |
| 1380 | * Augment the access flags for synthetic methods and fields by setting |
| 1381 | * the (as described by the spec) "0xf0000000 bit". Also, strip out any |
| 1382 | * flags not specified by the Java programming language. |
| 1383 | */ |
| 1384 | static uint32_t MangleAccessFlags(uint32_t accessFlags) { |
| 1385 | accessFlags &= kAccJavaFlagsMask; |
| 1386 | if ((accessFlags & kAccSynthetic) != 0) { |
| 1387 | accessFlags |= 0xf0000000; |
| 1388 | } |
| 1389 | return accessFlags; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1392 | /* |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1393 | * Circularly shifts registers so that arguments come first. Debuggers |
| 1394 | * expect slots to begin with arguments, but dex code places them at |
| 1395 | * the end. |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1396 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1397 | static uint16_t MangleSlot(uint16_t slot, ArtMethod* m) |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1398 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1399 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1400 | if (code_item == nullptr) { |
| 1401 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1402 | // return the slot as is since all registers are arguments. |
| 1403 | LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m); |
| 1404 | return slot; |
| 1405 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1406 | uint16_t ins_size = code_item->ins_size_; |
| 1407 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1408 | if (slot >= locals_size) { |
| 1409 | return slot - locals_size; |
| 1410 | } else { |
| 1411 | return slot + ins_size; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1412 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1413 | } |
| 1414 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1415 | /* |
| 1416 | * Circularly shifts registers so that arguments come last. Reverts |
| 1417 | * slots to dex style argument placement. |
| 1418 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1419 | static uint16_t DemangleSlot(uint16_t slot, ArtMethod* m, JDWP::JdwpError* error) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1420 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1421 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1422 | if (code_item == nullptr) { |
| 1423 | // We should not get here for a method without code (native, proxy or abstract). Log it and |
| 1424 | // return the slot as is since all registers are arguments. |
| 1425 | LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1426 | uint16_t vreg_count = ArtMethod::NumArgRegisters(m->GetShorty()); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1427 | if (slot < vreg_count) { |
| 1428 | *error = JDWP::ERR_NONE; |
| 1429 | return slot; |
| 1430 | } |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1431 | } else { |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1432 | if (slot < code_item->registers_size_) { |
| 1433 | uint16_t ins_size = code_item->ins_size_; |
| 1434 | uint16_t locals_size = code_item->registers_size_ - ins_size; |
| 1435 | *error = JDWP::ERR_NONE; |
| 1436 | return (slot < ins_size) ? slot + locals_size : slot - ins_size; |
| 1437 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1438 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 1439 | |
| 1440 | // Slot is invalid in the method. |
| 1441 | LOG(ERROR) << "Invalid local slot " << slot << " for method " << PrettyMethod(m); |
| 1442 | *error = JDWP::ERR_INVALID_SLOT; |
| 1443 | return DexFile::kDexNoIndex16; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1444 | } |
| 1445 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1446 | 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] | 1447 | JDWP::JdwpError error; |
| 1448 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1449 | if (c == nullptr) { |
| 1450 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1451 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1452 | |
| 1453 | size_t instance_field_count = c->NumInstanceFields(); |
| 1454 | size_t static_field_count = c->NumStaticFields(); |
| 1455 | |
| 1456 | expandBufAdd4BE(pReply, instance_field_count + static_field_count); |
| 1457 | |
| 1458 | 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] | 1459 | 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] | 1460 | expandBufAddFieldId(pReply, ToFieldId(f)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1461 | expandBufAddUtf8String(pReply, f->GetName()); |
| 1462 | expandBufAddUtf8String(pReply, f->GetTypeDescriptor()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1463 | if (with_generic) { |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1464 | static const char genericSignature[1] = ""; |
| 1465 | expandBufAddUtf8String(pReply, genericSignature); |
| 1466 | } |
| 1467 | expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags())); |
| 1468 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1469 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1472 | JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1473 | JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1474 | JDWP::JdwpError error; |
| 1475 | mirror::Class* c = DecodeClass(class_id, &error); |
| 1476 | if (c == nullptr) { |
| 1477 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1478 | } |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1479 | |
| 1480 | size_t direct_method_count = c->NumDirectMethods(); |
| 1481 | size_t virtual_method_count = c->NumVirtualMethods(); |
| 1482 | |
| 1483 | expandBufAdd4BE(pReply, direct_method_count + virtual_method_count); |
| 1484 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1485 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 1486 | auto ptr_size = cl->GetImagePointerSize(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1487 | 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] | 1488 | ArtMethod* m = i < direct_method_count ? |
| 1489 | 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] | 1490 | expandBufAddMethodId(pReply, ToMethodId(m)); |
Sebastien Hertz | 415fd08 | 2015-06-01 11:42:27 +0200 | [diff] [blame] | 1491 | expandBufAddUtf8String(pReply, m->GetInterfaceMethodIfProxy(sizeof(void*))->GetName()); |
| 1492 | expandBufAddUtf8String(pReply, |
| 1493 | m->GetInterfaceMethodIfProxy(sizeof(void*))->GetSignature().ToString()); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1494 | if (with_generic) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1495 | const char* generic_signature = ""; |
| 1496 | expandBufAddUtf8String(pReply, generic_signature); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1497 | } |
| 1498 | expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags())); |
| 1499 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1500 | return JDWP::ERR_NONE; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1501 | } |
| 1502 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1503 | JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1504 | JDWP::JdwpError error; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1505 | Thread* self = Thread::Current(); |
| 1506 | StackHandleScope<1> hs(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1507 | Handle<mirror::Class> c(hs.NewHandle(DecodeClass(class_id, &error))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1508 | if (c.Get() == nullptr) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1509 | return error; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 1510 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1511 | size_t interface_count = c->NumDirectInterfaces(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1512 | expandBufAdd4BE(pReply, interface_count); |
| 1513 | for (size_t i = 0; i < interface_count; ++i) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1514 | expandBufAddRefTypeId(pReply, |
| 1515 | gRegistry->AddRefType(mirror::Class::GetDirectInterface(self, c, i))); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1516 | } |
Elliott Hughes | 436e372 | 2012-02-17 20:01:47 -0800 | [diff] [blame] | 1517 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1520 | void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1521 | struct DebugCallbackContext { |
| 1522 | int numItems; |
| 1523 | JDWP::ExpandBuf* pReply; |
| 1524 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1525 | static bool Callback(void* context, uint32_t address, uint32_t line_number) { |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1526 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1527 | expandBufAdd8BE(pContext->pReply, address); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1528 | expandBufAdd4BE(pContext->pReply, line_number); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1529 | pContext->numItems++; |
Sebastien Hertz | f2910ee | 2013-10-19 16:39:24 +0200 | [diff] [blame] | 1530 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1531 | } |
| 1532 | }; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1533 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1534 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1535 | uint64_t start, end; |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1536 | if (code_item == nullptr) { |
| 1537 | DCHECK(m->IsNative() || m->IsProxyMethod()); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1538 | start = -1; |
| 1539 | end = -1; |
| 1540 | } else { |
| 1541 | start = 0; |
jeffhao | 14f0db9 | 2012-12-14 17:50:42 -0800 | [diff] [blame] | 1542 | // Return the index of the last instruction |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1543 | end = code_item->insns_size_in_code_units_ - 1; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | expandBufAdd8BE(pReply, start); |
| 1547 | expandBufAdd8BE(pReply, end); |
| 1548 | |
| 1549 | // Add numLines later |
| 1550 | size_t numLinesOffset = expandBufGetLength(pReply); |
| 1551 | expandBufAdd4BE(pReply, 0); |
| 1552 | |
| 1553 | DebugCallbackContext context; |
| 1554 | context.numItems = 0; |
| 1555 | context.pReply = pReply; |
| 1556 | |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1557 | if (code_item != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1558 | m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1559 | DebugCallbackContext::Callback, nullptr, &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1560 | } |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 1561 | |
| 1562 | JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1565 | void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, |
| 1566 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1567 | struct DebugCallbackContext { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1568 | ArtMethod* method; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1569 | JDWP::ExpandBuf* pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1570 | size_t variable_count; |
| 1571 | bool with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1572 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1573 | static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, |
| 1574 | const char* name, const char* descriptor, const char* signature) |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1575 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1576 | DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context); |
| 1577 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1578 | VLOG(jdwp) << StringPrintf(" %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d", |
| 1579 | pContext->variable_count, startAddress, endAddress - startAddress, |
| 1580 | name, descriptor, signature, slot, |
| 1581 | MangleSlot(slot, pContext->method)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1582 | |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1583 | slot = MangleSlot(slot, pContext->method); |
Elliott Hughes | 68fdbd0 | 2011-11-29 19:22:47 -0800 | [diff] [blame] | 1584 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1585 | expandBufAdd8BE(pContext->pReply, startAddress); |
| 1586 | expandBufAddUtf8String(pContext->pReply, name); |
| 1587 | expandBufAddUtf8String(pContext->pReply, descriptor); |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1588 | if (pContext->with_generic) { |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1589 | expandBufAddUtf8String(pContext->pReply, signature); |
| 1590 | } |
| 1591 | expandBufAdd4BE(pContext->pReply, endAddress - startAddress); |
| 1592 | expandBufAdd4BE(pContext->pReply, slot); |
| 1593 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1594 | ++pContext->variable_count; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1595 | } |
| 1596 | }; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1597 | ArtMethod* m = FromMethodId(method_id); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1598 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1599 | // arg_count considers doubles and longs to take 2 units. |
| 1600 | // variable_count considers everything to take 1 unit. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1601 | std::string shorty(m->GetShorty()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1602 | expandBufAdd4BE(pReply, ArtMethod::NumArgRegisters(shorty)); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1603 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1604 | // We don't know the total number of variables yet, so leave a blank and update it later. |
| 1605 | size_t variable_count_offset = expandBufGetLength(pReply); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1606 | expandBufAdd4BE(pReply, 0); |
| 1607 | |
| 1608 | DebugCallbackContext context; |
Jeff Hao | b7cefc7 | 2013-11-14 14:51:09 -0800 | [diff] [blame] | 1609 | context.method = m; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1610 | context.pReply = pReply; |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1611 | context.variable_count = 0; |
| 1612 | context.with_generic = with_generic; |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1613 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1614 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1615 | if (code_item != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1616 | m->GetDexFile()->DecodeDebugInfo( |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1617 | code_item, m->IsStatic(), m->GetDexMethodIndex(), nullptr, DebugCallbackContext::Callback, |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1618 | &context); |
Sebastien Hertz | cb19ebf | 2014-03-11 15:26:35 +0100 | [diff] [blame] | 1619 | } |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 1620 | |
Elliott Hughes | c5b734a | 2011-12-01 17:20:58 -0800 | [diff] [blame] | 1621 | JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1624 | void Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value, |
| 1625 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1626 | ArtMethod* m = FromMethodId(method_id); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1627 | JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1628 | OutputJValue(tag, return_value, pReply); |
| 1629 | } |
| 1630 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1631 | void Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value, |
| 1632 | JDWP::ExpandBuf* pReply) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1633 | ArtField* f = FromFieldId(field_id); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1634 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1635 | OutputJValue(tag, field_value, pReply); |
| 1636 | } |
| 1637 | |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1638 | JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id, |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1639 | std::vector<uint8_t>* bytecodes) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1640 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1641 | if (m == nullptr) { |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1642 | return JDWP::ERR_INVALID_METHODID; |
| 1643 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1644 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1645 | size_t byte_count = code_item->insns_size_in_code_units_ * 2; |
| 1646 | const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_); |
| 1647 | const uint8_t* end = begin + byte_count; |
| 1648 | for (const uint8_t* p = begin; p != end; ++p) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1649 | bytecodes->push_back(*p); |
Elliott Hughes | 9777ba2 | 2013-01-17 09:04:19 -0800 | [diff] [blame] | 1650 | } |
| 1651 | return JDWP::ERR_NONE; |
| 1652 | } |
| 1653 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1654 | JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1655 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1658 | JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1659 | return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1662 | static JValue GetArtFieldValue(ArtField* f, mirror::Object* o) |
| 1663 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1664 | Primitive::Type fieldType = f->GetTypeAsPrimitiveType(); |
| 1665 | JValue field_value; |
| 1666 | switch (fieldType) { |
| 1667 | case Primitive::kPrimBoolean: |
| 1668 | field_value.SetZ(f->GetBoolean(o)); |
| 1669 | return field_value; |
| 1670 | |
| 1671 | case Primitive::kPrimByte: |
| 1672 | field_value.SetB(f->GetByte(o)); |
| 1673 | return field_value; |
| 1674 | |
| 1675 | case Primitive::kPrimChar: |
| 1676 | field_value.SetC(f->GetChar(o)); |
| 1677 | return field_value; |
| 1678 | |
| 1679 | case Primitive::kPrimShort: |
| 1680 | field_value.SetS(f->GetShort(o)); |
| 1681 | return field_value; |
| 1682 | |
| 1683 | case Primitive::kPrimInt: |
| 1684 | case Primitive::kPrimFloat: |
| 1685 | // Int and Float must be treated as 32-bit values in JDWP. |
| 1686 | field_value.SetI(f->GetInt(o)); |
| 1687 | return field_value; |
| 1688 | |
| 1689 | case Primitive::kPrimLong: |
| 1690 | case Primitive::kPrimDouble: |
| 1691 | // Long and Double must be treated as 64-bit values in JDWP. |
| 1692 | field_value.SetJ(f->GetLong(o)); |
| 1693 | return field_value; |
| 1694 | |
| 1695 | case Primitive::kPrimNot: |
| 1696 | field_value.SetL(f->GetObject(o)); |
| 1697 | return field_value; |
| 1698 | |
| 1699 | case Primitive::kPrimVoid: |
| 1700 | LOG(FATAL) << "Attempt to read from field of type 'void'"; |
| 1701 | UNREACHABLE(); |
| 1702 | } |
| 1703 | LOG(FATAL) << "Attempt to read from field of unknown type"; |
| 1704 | UNREACHABLE(); |
| 1705 | } |
| 1706 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1707 | static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id, |
| 1708 | JDWP::FieldId field_id, JDWP::ExpandBuf* pReply, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1709 | bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1710 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1711 | JDWP::JdwpError error; |
| 1712 | mirror::Class* c = DecodeClass(ref_type_id, &error); |
| 1713 | if (ref_type_id != 0 && c == nullptr) { |
| 1714 | return error; |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1715 | } |
| 1716 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1717 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1718 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1719 | return JDWP::ERR_INVALID_OBJECT; |
| 1720 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1721 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1722 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1723 | mirror::Class* receiver_class = c; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1724 | if (receiver_class == nullptr && o != nullptr) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1725 | receiver_class = o->GetClass(); |
| 1726 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1727 | // TODO: should we give up now if receiver_class is null? |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1728 | if (receiver_class != nullptr && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) { |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1729 | LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1730 | return JDWP::ERR_INVALID_FIELDID; |
| 1731 | } |
Elliott Hughes | aed4be9 | 2011-12-02 16:16:23 -0800 | [diff] [blame] | 1732 | |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1733 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1734 | // TODO: should we change the tests and check both? |
| 1735 | if (is_static) { |
| 1736 | if (!f->IsStatic()) { |
| 1737 | return JDWP::ERR_INVALID_FIELDID; |
| 1738 | } |
| 1739 | } else { |
| 1740 | if (f->IsStatic()) { |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1741 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.GetValues" |
| 1742 | << " on static field " << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1743 | } |
| 1744 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1745 | if (f->IsStatic()) { |
| 1746 | o = f->GetDeclaringClass(); |
| 1747 | } |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1748 | |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1749 | JValue field_value(GetArtFieldValue(f, o)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1750 | JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1751 | Dbg::OutputJValue(tag, &field_value, pReply); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1752 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1755 | JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1756 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1757 | return GetFieldValueImpl(0, object_id, field_id, pReply, false); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1758 | } |
| 1759 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1760 | JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, |
| 1761 | JDWP::ExpandBuf* pReply) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1762 | return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true); |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1765 | static JDWP::JdwpError SetArtFieldValue(ArtField* f, mirror::Object* o, uint64_t value, int width) |
| 1766 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1767 | Primitive::Type fieldType = f->GetTypeAsPrimitiveType(); |
| 1768 | // Debugging only happens at runtime so we know we are not running in a transaction. |
| 1769 | static constexpr bool kNoTransactionMode = false; |
| 1770 | switch (fieldType) { |
| 1771 | case Primitive::kPrimBoolean: |
| 1772 | CHECK_EQ(width, 1); |
| 1773 | f->SetBoolean<kNoTransactionMode>(o, static_cast<uint8_t>(value)); |
| 1774 | return JDWP::ERR_NONE; |
| 1775 | |
| 1776 | case Primitive::kPrimByte: |
| 1777 | CHECK_EQ(width, 1); |
| 1778 | f->SetByte<kNoTransactionMode>(o, static_cast<uint8_t>(value)); |
| 1779 | return JDWP::ERR_NONE; |
| 1780 | |
| 1781 | case Primitive::kPrimChar: |
| 1782 | CHECK_EQ(width, 2); |
| 1783 | f->SetChar<kNoTransactionMode>(o, static_cast<uint16_t>(value)); |
| 1784 | return JDWP::ERR_NONE; |
| 1785 | |
| 1786 | case Primitive::kPrimShort: |
| 1787 | CHECK_EQ(width, 2); |
| 1788 | f->SetShort<kNoTransactionMode>(o, static_cast<int16_t>(value)); |
| 1789 | return JDWP::ERR_NONE; |
| 1790 | |
| 1791 | case Primitive::kPrimInt: |
| 1792 | case Primitive::kPrimFloat: |
| 1793 | CHECK_EQ(width, 4); |
| 1794 | // Int and Float must be treated as 32-bit values in JDWP. |
| 1795 | f->SetInt<kNoTransactionMode>(o, static_cast<int32_t>(value)); |
| 1796 | return JDWP::ERR_NONE; |
| 1797 | |
| 1798 | case Primitive::kPrimLong: |
| 1799 | case Primitive::kPrimDouble: |
| 1800 | CHECK_EQ(width, 8); |
| 1801 | // Long and Double must be treated as 64-bit values in JDWP. |
| 1802 | f->SetLong<kNoTransactionMode>(o, value); |
| 1803 | return JDWP::ERR_NONE; |
| 1804 | |
| 1805 | case Primitive::kPrimNot: { |
| 1806 | JDWP::JdwpError error; |
| 1807 | mirror::Object* v = Dbg::GetObjectRegistry()->Get<mirror::Object*>(value, &error); |
| 1808 | if (error != JDWP::ERR_NONE) { |
| 1809 | return JDWP::ERR_INVALID_OBJECT; |
| 1810 | } |
| 1811 | if (v != nullptr) { |
| 1812 | mirror::Class* field_type; |
| 1813 | { |
| 1814 | StackHandleScope<2> hs(Thread::Current()); |
| 1815 | HandleWrapper<mirror::Object> h_v(hs.NewHandleWrapper(&v)); |
| 1816 | HandleWrapper<mirror::Object> h_o(hs.NewHandleWrapper(&o)); |
| 1817 | field_type = f->GetType<true>(); |
| 1818 | } |
| 1819 | if (!field_type->IsAssignableFrom(v->GetClass())) { |
| 1820 | return JDWP::ERR_INVALID_OBJECT; |
| 1821 | } |
| 1822 | } |
| 1823 | f->SetObject<kNoTransactionMode>(o, v); |
| 1824 | return JDWP::ERR_NONE; |
| 1825 | } |
| 1826 | |
| 1827 | case Primitive::kPrimVoid: |
| 1828 | LOG(FATAL) << "Attempt to write to field of type 'void'"; |
| 1829 | UNREACHABLE(); |
| 1830 | } |
| 1831 | LOG(FATAL) << "Attempt to write to field of unknown type"; |
| 1832 | UNREACHABLE(); |
| 1833 | } |
| 1834 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1835 | static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1836 | uint64_t value, int width, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1837 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1838 | JDWP::JdwpError error; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1839 | mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1840 | if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1841 | return JDWP::ERR_INVALID_OBJECT; |
| 1842 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1843 | ArtField* f = FromFieldId(field_id); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1844 | |
| 1845 | // The RI only enforces the static/non-static mismatch in one direction. |
| 1846 | // TODO: should we change the tests and check both? |
| 1847 | if (is_static) { |
| 1848 | if (!f->IsStatic()) { |
| 1849 | return JDWP::ERR_INVALID_FIELDID; |
| 1850 | } |
| 1851 | } else { |
| 1852 | if (f->IsStatic()) { |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1853 | LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues" |
| 1854 | << " on static field " << PrettyField(f); |
Elliott Hughes | 0cf7433 | 2012-02-23 23:14:00 -0800 | [diff] [blame] | 1855 | } |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 1856 | } |
jeffhao | 0dfbb7e | 2012-11-28 15:26:03 -0800 | [diff] [blame] | 1857 | if (f->IsStatic()) { |
| 1858 | o = f->GetDeclaringClass(); |
| 1859 | } |
Sebastien Hertz | 05c26b3 | 2015-06-11 18:42:58 +0200 | [diff] [blame] | 1860 | return SetArtFieldValue(f, o, value, width); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1863 | 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] | 1864 | int width) { |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1865 | return SetFieldValueImpl(object_id, field_id, value, width, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1866 | } |
| 1867 | |
Elliott Hughes | 88d6309 | 2013-01-09 09:55:54 -0800 | [diff] [blame] | 1868 | JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) { |
| 1869 | return SetFieldValueImpl(0, field_id, value, width, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1872 | JDWP::JdwpError Dbg::StringToUtf8(JDWP::ObjectId string_id, std::string* str) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1873 | JDWP::JdwpError error; |
Sebastien Hertz | b0b0b49 | 2014-09-15 11:27:27 +0200 | [diff] [blame] | 1874 | mirror::Object* obj = gRegistry->Get<mirror::Object*>(string_id, &error); |
| 1875 | if (error != JDWP::ERR_NONE) { |
| 1876 | return error; |
| 1877 | } |
| 1878 | if (obj == nullptr) { |
| 1879 | return JDWP::ERR_INVALID_OBJECT; |
| 1880 | } |
| 1881 | { |
| 1882 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1883 | mirror::Class* java_lang_String = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_String); |
| 1884 | if (!java_lang_String->IsAssignableFrom(obj->GetClass())) { |
| 1885 | // This isn't a string. |
| 1886 | return JDWP::ERR_INVALID_STRING; |
| 1887 | } |
| 1888 | } |
| 1889 | *str = obj->AsString()->ToModifiedUtf8(); |
| 1890 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1893 | void Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) { |
| 1894 | if (IsPrimitiveTag(tag)) { |
| 1895 | expandBufAdd1(pReply, tag); |
| 1896 | if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) { |
| 1897 | expandBufAdd1(pReply, return_value->GetI()); |
| 1898 | } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) { |
| 1899 | expandBufAdd2BE(pReply, return_value->GetI()); |
| 1900 | } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) { |
| 1901 | expandBufAdd4BE(pReply, return_value->GetI()); |
| 1902 | } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) { |
| 1903 | expandBufAdd8BE(pReply, return_value->GetJ()); |
| 1904 | } else { |
| 1905 | CHECK_EQ(tag, JDWP::JT_VOID); |
| 1906 | } |
| 1907 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1908 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1909 | mirror::Object* value = return_value->GetL(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1910 | expandBufAdd1(pReply, TagFromObject(soa, value)); |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 1911 | expandBufAddObjectId(pReply, gRegistry->Add(value)); |
| 1912 | } |
| 1913 | } |
| 1914 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1915 | JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string* name) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 1916 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1917 | JDWP::JdwpError error; |
| 1918 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 1919 | UNUSED(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1920 | if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1921 | return error; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 1922 | } |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1923 | |
| 1924 | // 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] | 1925 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 1926 | CHECK(thread_object != nullptr) << error; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1927 | ArtField* java_lang_Thread_name_field = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1928 | soa.DecodeField(WellKnownClasses::java_lang_Thread_name); |
| 1929 | mirror::String* s = |
| 1930 | reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1931 | if (s != nullptr) { |
| 1932 | *name = s->ToModifiedUtf8(); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1933 | } |
| 1934 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1937 | JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1938 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1939 | JDWP::JdwpError error; |
| 1940 | mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 1941 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1942 | return JDWP::ERR_INVALID_OBJECT; |
| 1943 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1944 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroup"); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1945 | // Okay, so it's an object, but is it actually a thread? |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 1946 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 1947 | UNUSED(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1948 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 1949 | // Zombie threads are in the null group. |
| 1950 | expandBufAddObjectId(pReply, JDWP::ObjectId(0)); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1951 | error = JDWP::ERR_NONE; |
| 1952 | } else if (error == JDWP::ERR_NONE) { |
| 1953 | mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread); |
| 1954 | CHECK(c != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1955 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 1956 | CHECK(f != nullptr); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1957 | mirror::Object* group = f->GetObject(thread_object); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 1958 | CHECK(group != nullptr); |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1959 | JDWP::ObjectId thread_group_id = gRegistry->Add(group); |
| 1960 | expandBufAddObjectId(pReply, thread_group_id); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 1961 | } |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 1962 | return error; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1965 | static mirror::Object* DecodeThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 1966 | JDWP::ObjectId thread_group_id, JDWP::JdwpError* error) |
| 1967 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 1968 | mirror::Object* thread_group = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_group_id, |
| 1969 | error); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1970 | if (*error != JDWP::ERR_NONE) { |
| 1971 | return nullptr; |
| 1972 | } |
| 1973 | if (thread_group == nullptr) { |
| 1974 | *error = JDWP::ERR_INVALID_OBJECT; |
| 1975 | return nullptr; |
| 1976 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1977 | mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup); |
| 1978 | CHECK(c != nullptr); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1979 | if (!c->IsAssignableFrom(thread_group->GetClass())) { |
| 1980 | // This is not a java.lang.ThreadGroup. |
| 1981 | *error = JDWP::ERR_INVALID_THREAD_GROUP; |
| 1982 | return nullptr; |
| 1983 | } |
| 1984 | *error = JDWP::ERR_NONE; |
| 1985 | return thread_group; |
| 1986 | } |
| 1987 | |
| 1988 | JDWP::JdwpError Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
| 1989 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1990 | JDWP::JdwpError error; |
| 1991 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 1992 | if (error != JDWP::ERR_NONE) { |
| 1993 | return error; |
| 1994 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 1995 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupName"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1996 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1997 | CHECK(f != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1998 | mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group)); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 1999 | |
| 2000 | std::string thread_group_name(s->ToModifiedUtf8()); |
| 2001 | expandBufAddUtf8String(pReply, thread_group_name); |
| 2002 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2005 | JDWP::JdwpError Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2006 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2007 | JDWP::JdwpError error; |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2008 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2009 | if (error != JDWP::ERR_NONE) { |
| 2010 | return error; |
| 2011 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2012 | mirror::Object* parent; |
| 2013 | { |
| 2014 | ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupParent"); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2015 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_parent); |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2016 | CHECK(f != nullptr); |
| 2017 | parent = f->GetObject(thread_group); |
| 2018 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2019 | JDWP::ObjectId parent_group_id = gRegistry->Add(parent); |
| 2020 | expandBufAddObjectId(pReply, parent_group_id); |
| 2021 | return JDWP::ERR_NONE; |
| 2022 | } |
| 2023 | |
| 2024 | static void GetChildThreadGroups(ScopedObjectAccessUnchecked& soa, mirror::Object* thread_group, |
| 2025 | std::vector<JDWP::ObjectId>* child_thread_group_ids) |
| 2026 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2027 | CHECK(thread_group != nullptr); |
| 2028 | |
| 2029 | // Get the ArrayList<ThreadGroup> "groups" out of this thread group... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2030 | ArtField* groups_field = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_groups); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2031 | mirror::Object* groups_array_list = groups_field->GetObject(thread_group); |
Sebastien Hertz | e49e195 | 2014-10-13 11:27:13 +0200 | [diff] [blame] | 2032 | { |
| 2033 | // The "groups" field is declared as a java.util.List: check it really is |
| 2034 | // an instance of java.util.ArrayList. |
| 2035 | CHECK(groups_array_list != nullptr); |
| 2036 | mirror::Class* java_util_ArrayList_class = |
| 2037 | soa.Decode<mirror::Class*>(WellKnownClasses::java_util_ArrayList); |
| 2038 | CHECK(groups_array_list->InstanceOf(java_util_ArrayList_class)); |
| 2039 | } |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2040 | |
| 2041 | // Get the array and size out of the ArrayList<ThreadGroup>... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2042 | ArtField* array_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_array); |
| 2043 | ArtField* size_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_size); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2044 | mirror::ObjectArray<mirror::Object>* groups_array = |
| 2045 | array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>(); |
| 2046 | const int32_t size = size_field->GetInt(groups_array_list); |
| 2047 | |
| 2048 | // Copy the first 'size' elements out of the array into the result. |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2049 | ObjectRegistry* registry = Dbg::GetObjectRegistry(); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2050 | for (int32_t i = 0; i < size; ++i) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2051 | child_thread_group_ids->push_back(registry->Add(groups_array->Get(i))); |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | JDWP::JdwpError Dbg::GetThreadGroupChildren(JDWP::ObjectId thread_group_id, |
| 2056 | JDWP::ExpandBuf* pReply) { |
| 2057 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 2058 | JDWP::JdwpError error; |
| 2059 | mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error); |
| 2060 | if (error != JDWP::ERR_NONE) { |
| 2061 | return error; |
| 2062 | } |
| 2063 | |
| 2064 | // Add child threads. |
| 2065 | { |
| 2066 | std::vector<JDWP::ObjectId> child_thread_ids; |
| 2067 | GetThreads(thread_group, &child_thread_ids); |
| 2068 | expandBufAdd4BE(pReply, child_thread_ids.size()); |
| 2069 | for (JDWP::ObjectId child_thread_id : child_thread_ids) { |
| 2070 | expandBufAddObjectId(pReply, child_thread_id); |
| 2071 | } |
| 2072 | } |
| 2073 | |
| 2074 | // Add child thread groups. |
| 2075 | { |
| 2076 | std::vector<JDWP::ObjectId> child_thread_groups_ids; |
| 2077 | GetChildThreadGroups(soa, thread_group, &child_thread_groups_ids); |
| 2078 | expandBufAdd4BE(pReply, child_thread_groups_ids.size()); |
| 2079 | for (JDWP::ObjectId child_thread_group_id : child_thread_groups_ids) { |
| 2080 | expandBufAddObjectId(pReply, child_thread_group_id); |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | JDWP::ObjectId Dbg::GetSystemThreadGroupId() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2088 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2089 | ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2090 | mirror::Object* group = f->GetObject(f->GetDeclaringClass()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2091 | return gRegistry->Add(group); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2094 | JDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) { |
| 2095 | switch (state) { |
| 2096 | case kBlocked: |
| 2097 | return JDWP::TS_MONITOR; |
| 2098 | case kNative: |
| 2099 | case kRunnable: |
| 2100 | case kSuspended: |
| 2101 | return JDWP::TS_RUNNING; |
| 2102 | case kSleeping: |
| 2103 | return JDWP::TS_SLEEPING; |
| 2104 | case kStarting: |
| 2105 | case kTerminated: |
| 2106 | return JDWP::TS_ZOMBIE; |
| 2107 | case kTimedWaiting: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2108 | case kWaitingForCheckPointsToRun: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2109 | case kWaitingForDebuggerSend: |
| 2110 | case kWaitingForDebuggerSuspension: |
| 2111 | case kWaitingForDebuggerToAttach: |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 2112 | case kWaitingForDeoptimization: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2113 | case kWaitingForGcToComplete: |
Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 2114 | case kWaitingForGetObjectsAllocated: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2115 | case kWaitingForJniOnLoad: |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 2116 | case kWaitingForMethodTracingStart: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2117 | case kWaitingForSignalCatcherOutput: |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 2118 | case kWaitingForVisitObjects: |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2119 | case kWaitingInMainDebuggerLoop: |
| 2120 | case kWaitingInMainSignalCatcherLoop: |
| 2121 | case kWaitingPerformingGc: |
| 2122 | case kWaiting: |
| 2123 | return JDWP::TS_WAIT; |
| 2124 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 2125 | } |
| 2126 | LOG(FATAL) << "Unknown thread state: " << state; |
| 2127 | return JDWP::TS_ZOMBIE; |
| 2128 | } |
| 2129 | |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 2130 | JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, |
| 2131 | JDWP::JdwpSuspendStatus* pSuspendStatus) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2132 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2133 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2134 | *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED; |
| 2135 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2136 | JDWP::JdwpError error; |
| 2137 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2138 | if (error != JDWP::ERR_NONE) { |
| 2139 | if (error == JDWP::ERR_THREAD_NOT_ALIVE) { |
| 2140 | *pThreadStatus = JDWP::TS_ZOMBIE; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2141 | return JDWP::ERR_NONE; |
| 2142 | } |
| 2143 | return error; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2144 | } |
| 2145 | |
Elliott Hughes | 9e0c175 | 2013-01-09 14:02:58 -0800 | [diff] [blame] | 2146 | if (IsSuspendedForDebugger(soa, thread)) { |
| 2147 | *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED; |
Elliott Hughes | 499c513 | 2011-11-17 14:55:11 -0800 | [diff] [blame] | 2148 | } |
| 2149 | |
Jeff Hao | 920af3e | 2013-08-28 15:46:38 -0700 | [diff] [blame] | 2150 | *pThreadStatus = ToJdwpThreadStatus(thread->GetState()); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2151 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2154 | JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2155 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2156 | JDWP::JdwpError error; |
| 2157 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2158 | if (error != JDWP::ERR_NONE) { |
| 2159 | return error; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2160 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2161 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2162 | expandBufAdd4BE(pReply, thread->GetDebugSuspendCount()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2163 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2166 | JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) { |
| 2167 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2168 | JDWP::JdwpError error; |
| 2169 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2170 | if (error != JDWP::ERR_NONE) { |
| 2171 | return error; |
| 2172 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 2173 | thread->Interrupt(soa.Self()); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 2174 | return JDWP::ERR_NONE; |
| 2175 | } |
| 2176 | |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2177 | static bool IsInDesiredThreadGroup(ScopedObjectAccessUnchecked& soa, |
| 2178 | mirror::Object* desired_thread_group, mirror::Object* peer) |
| 2179 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2180 | // Do we want threads from all thread groups? |
| 2181 | if (desired_thread_group == nullptr) { |
| 2182 | return true; |
| 2183 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2184 | ArtField* thread_group_field = soa.DecodeField(WellKnownClasses::java_lang_Thread_group); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2185 | DCHECK(thread_group_field != nullptr); |
| 2186 | mirror::Object* group = thread_group_field->GetObject(peer); |
| 2187 | return (group == desired_thread_group); |
| 2188 | } |
| 2189 | |
Sebastien Hertz | a06430c | 2014-09-15 19:21:30 +0200 | [diff] [blame] | 2190 | 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] | 2191 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 2192 | std::list<Thread*> all_threads_list; |
| 2193 | { |
| 2194 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 2195 | all_threads_list = Runtime::Current()->GetThreadList()->GetList(); |
| 2196 | } |
| 2197 | for (Thread* t : all_threads_list) { |
| 2198 | if (t == Dbg::GetDebugThread()) { |
| 2199 | // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and |
| 2200 | // query all threads, so it's easier if we just don't tell them about this thread. |
| 2201 | continue; |
| 2202 | } |
| 2203 | if (t->IsStillStarting()) { |
| 2204 | // This thread is being started (and has been registered in the thread list). However, it is |
| 2205 | // not completely started yet so we must ignore it. |
| 2206 | continue; |
| 2207 | } |
| 2208 | mirror::Object* peer = t->GetPeer(); |
| 2209 | if (peer == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2210 | // 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] | 2211 | // this thread yet. |
| 2212 | // TODO: if we identified threads to the debugger by their Thread* |
| 2213 | // rather than their peer's mirror::Object*, we could fix this. |
| 2214 | // Doing so might help us report ZOMBIE threads too. |
| 2215 | continue; |
| 2216 | } |
| 2217 | if (IsInDesiredThreadGroup(soa, thread_group, peer)) { |
| 2218 | thread_ids->push_back(gRegistry->Add(peer)); |
| 2219 | } |
| 2220 | } |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 2221 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2222 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2223 | static int GetStackDepth(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2224 | struct CountStackDepthVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2225 | explicit CountStackDepthVisitor(Thread* thread_in) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2226 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2227 | depth(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2228 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 2229 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2230 | // annotalysis. |
| 2231 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2232 | if (!GetMethod()->IsRuntimeMethod()) { |
Elliott Hughes | f8a2df7 | 2011-12-01 12:19:54 -0800 | [diff] [blame] | 2233 | ++depth; |
| 2234 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2235 | return true; |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2236 | } |
| 2237 | size_t depth; |
| 2238 | }; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2239 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2240 | CountStackDepthVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2241 | visitor.WalkStack(); |
Elliott Hughes | a2e54f6 | 2011-11-17 13:01:30 -0800 | [diff] [blame] | 2242 | return visitor.depth; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2245 | JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t* result) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2246 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2247 | JDWP::JdwpError error; |
| 2248 | *result = 0; |
| 2249 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2250 | if (error != JDWP::ERR_NONE) { |
| 2251 | return error; |
| 2252 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2253 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2254 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2255 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2256 | *result = GetStackDepth(thread); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2257 | return JDWP::ERR_NONE; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2258 | } |
| 2259 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 2260 | JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame, |
| 2261 | size_t frame_count, JDWP::ExpandBuf* buf) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2262 | class GetFrameVisitor : public StackVisitor { |
| 2263 | public: |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2264 | GetFrameVisitor(Thread* thread, size_t start_frame_in, size_t frame_count_in, |
| 2265 | JDWP::ExpandBuf* buf_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2266 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2267 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2268 | depth_(0), |
| 2269 | start_frame_(start_frame_in), |
| 2270 | frame_count_(frame_count_in), |
| 2271 | buf_(buf_in) { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2272 | expandBufAdd4BE(buf_, frame_count_); |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2273 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2274 | |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2275 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2276 | if (GetMethod()->IsRuntimeMethod()) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2277 | 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] | 2278 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2279 | if (depth_ >= start_frame_ + frame_count_) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2280 | return false; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2281 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2282 | if (depth_ >= start_frame_) { |
| 2283 | JDWP::FrameId frame_id(GetFrameId()); |
| 2284 | JDWP::JdwpLocation location; |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2285 | SetJdwpLocation(&location, GetMethod(), GetDexPc()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2286 | VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2287 | expandBufAdd8BE(buf_, frame_id); |
| 2288 | expandBufAddLocation(buf_, location); |
| 2289 | } |
| 2290 | ++depth_; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 2291 | return true; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2292 | } |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2293 | |
| 2294 | private: |
| 2295 | size_t depth_; |
| 2296 | const size_t start_frame_; |
| 2297 | const size_t frame_count_; |
| 2298 | JDWP::ExpandBuf* buf_; |
Elliott Hughes | 03181a8 | 2011-11-17 17:22:21 -0800 | [diff] [blame] | 2299 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2300 | |
| 2301 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2302 | JDWP::JdwpError error; |
| 2303 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2304 | if (error != JDWP::ERR_NONE) { |
| 2305 | return error; |
| 2306 | } |
Elliott Hughes | f15f4a0 | 2013-01-09 10:09:38 -0800 | [diff] [blame] | 2307 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2308 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
| 2309 | } |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2310 | GetFrameVisitor visitor(thread, start_frame, frame_count, buf); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2311 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2312 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | JDWP::ObjectId Dbg::GetThreadSelfId() { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2316 | return GetThreadId(Thread::Current()); |
| 2317 | } |
| 2318 | |
| 2319 | JDWP::ObjectId Dbg::GetThreadId(Thread* thread) { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 2320 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2321 | return gRegistry->Add(thread->GetPeer()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2324 | void Dbg::SuspendVM() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2325 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | void Dbg::ResumeVM() { |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 2329 | Runtime::Current()->GetThreadList()->ResumeAllForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2330 | } |
| 2331 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2332 | JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2333 | Thread* self = Thread::Current(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2334 | ScopedLocalRef<jobject> peer(self->GetJniEnv(), nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2335 | { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 2336 | ScopedObjectAccess soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2337 | JDWP::JdwpError error; |
| 2338 | peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id, &error))); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2339 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2340 | if (peer.get() == nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2341 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2342 | } |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 2343 | // Suspend thread to build stack trace. |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2344 | bool timed_out; |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 2345 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 2346 | Thread* thread = thread_list->SuspendThreadByPeer(peer.get(), request_suspension, true, |
| 2347 | &timed_out); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2348 | if (thread != nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2349 | return JDWP::ERR_NONE; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 2350 | } else if (timed_out) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2351 | return JDWP::ERR_INTERNAL; |
| 2352 | } else { |
| 2353 | return JDWP::ERR_THREAD_NOT_ALIVE; |
| 2354 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2355 | } |
| 2356 | |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2357 | void Dbg::ResumeThread(JDWP::ObjectId thread_id) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2358 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2359 | JDWP::JdwpError error; |
| 2360 | mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id, &error); |
| 2361 | CHECK(peer != nullptr) << error; |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 2362 | Thread* thread; |
| 2363 | { |
| 2364 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
| 2365 | thread = Thread::FromManagedThread(soa, peer); |
| 2366 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 2367 | if (thread == nullptr) { |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 2368 | LOG(WARNING) << "No such thread for resume: " << peer; |
| 2369 | return; |
| 2370 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2371 | bool needs_resume; |
| 2372 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2373 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2374 | needs_resume = thread->GetSuspendCount() > 0; |
| 2375 | } |
| 2376 | if (needs_resume) { |
Elliott Hughes | 546b986 | 2012-06-20 16:06:13 -0700 | [diff] [blame] | 2377 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
| 2378 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | void Dbg::SuspendSelf() { |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 2382 | Runtime::Current()->GetThreadList()->SuspendSelfForDebugger(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2383 | } |
| 2384 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2385 | struct GetThisVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2386 | GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id_in) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2387 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2388 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2389 | this_object(nullptr), |
| 2390 | frame_id(frame_id_in) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2391 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2392 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2393 | // annotalysis. |
| 2394 | virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2395 | if (frame_id != GetFrameId()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2396 | return true; // continue |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2397 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2398 | this_object = GetThisObject(); |
| 2399 | return false; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2400 | } |
Elliott Hughes | 86b0010 | 2011-12-05 17:54:26 -0800 | [diff] [blame] | 2401 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2402 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2403 | mirror::Object* this_object; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2404 | JDWP::FrameId frame_id; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2405 | }; |
| 2406 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2407 | JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, |
| 2408 | JDWP::ObjectId* result) { |
| 2409 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2410 | JDWP::JdwpError error; |
| 2411 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2412 | if (error != JDWP::ERR_NONE) { |
| 2413 | return error; |
| 2414 | } |
| 2415 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2416 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2417 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2418 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2419 | GetThisVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2420 | visitor.WalkStack(); |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 2421 | *result = gRegistry->Add(visitor.this_object); |
| 2422 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2423 | } |
| 2424 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2425 | // Walks the stack until we find the frame with the given FrameId. |
| 2426 | class FindFrameVisitor FINAL : public StackVisitor { |
| 2427 | public: |
| 2428 | FindFrameVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id) |
| 2429 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2430 | : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 2431 | frame_id_(frame_id), |
| 2432 | error_(JDWP::ERR_INVALID_FRAMEID) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 2433 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2434 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 2435 | // annotalysis. |
| 2436 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
| 2437 | if (GetFrameId() != frame_id_) { |
| 2438 | return true; // Not our frame, carry on. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2439 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2440 | ArtMethod* m = GetMethod(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2441 | if (m->IsNative()) { |
| 2442 | // We can't read/write local value from/into native method. |
| 2443 | error_ = JDWP::ERR_OPAQUE_FRAME; |
| 2444 | } else { |
| 2445 | // We found our frame. |
| 2446 | error_ = JDWP::ERR_NONE; |
| 2447 | } |
| 2448 | return false; |
| 2449 | } |
| 2450 | |
| 2451 | JDWP::JdwpError GetError() const { |
| 2452 | return error_; |
| 2453 | } |
| 2454 | |
| 2455 | private: |
| 2456 | const JDWP::FrameId frame_id_; |
| 2457 | JDWP::JdwpError error_; |
| 2458 | }; |
| 2459 | |
| 2460 | JDWP::JdwpError Dbg::GetLocalValues(JDWP::Request* request, JDWP::ExpandBuf* pReply) { |
| 2461 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2462 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2463 | |
| 2464 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2465 | JDWP::JdwpError error; |
| 2466 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2467 | if (error != JDWP::ERR_NONE) { |
| 2468 | return error; |
| 2469 | } |
| 2470 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2471 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2472 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2473 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2474 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2475 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2476 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2477 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2478 | return visitor.GetError(); |
| 2479 | } |
| 2480 | |
| 2481 | // Read the values from visitor's context. |
| 2482 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2483 | expandBufAdd4BE(pReply, slot_count); /* "int values" */ |
| 2484 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2485 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2486 | JDWP::JdwpTag reqSigByte = request->ReadTag(); |
| 2487 | |
| 2488 | VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte; |
| 2489 | |
| 2490 | size_t width = Dbg::GetTagWidth(reqSigByte); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 2491 | uint8_t* ptr = expandBufAddSpace(pReply, width + 1); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2492 | error = Dbg::GetLocalValue(visitor, soa, slot, reqSigByte, ptr, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2493 | if (error != JDWP::ERR_NONE) { |
| 2494 | return error; |
| 2495 | } |
| 2496 | } |
| 2497 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2498 | } |
| 2499 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2500 | constexpr JDWP::JdwpError kStackFrameLocalAccessError = JDWP::ERR_ABSENT_INFORMATION; |
| 2501 | |
| 2502 | static std::string GetStackContextAsString(const StackVisitor& visitor) |
| 2503 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2504 | return StringPrintf(" at DEX pc 0x%08x in method %s", visitor.GetDexPc(false), |
| 2505 | PrettyMethod(visitor.GetMethod()).c_str()); |
| 2506 | } |
| 2507 | |
| 2508 | static JDWP::JdwpError FailGetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2509 | JDWP::JdwpTag tag) |
| 2510 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2511 | LOG(ERROR) << "Failed to read " << tag << " local from register v" << vreg |
| 2512 | << GetStackContextAsString(visitor); |
| 2513 | return kStackFrameLocalAccessError; |
| 2514 | } |
| 2515 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2516 | JDWP::JdwpError Dbg::GetLocalValue(const StackVisitor& visitor, ScopedObjectAccessUnchecked& soa, |
| 2517 | int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2518 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2519 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2520 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2521 | if (error != JDWP::ERR_NONE) { |
| 2522 | return error; |
| 2523 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2524 | // 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] | 2525 | switch (tag) { |
| 2526 | case JDWP::JT_BOOLEAN: { |
| 2527 | CHECK_EQ(width, 1U); |
| 2528 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2529 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2530 | return FailGetLocalValue(visitor, vreg, tag); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2531 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2532 | VLOG(jdwp) << "get boolean local " << vreg << " = " << intVal; |
| 2533 | JDWP::Set1(buf + 1, intVal != 0); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2534 | break; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2535 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2536 | case JDWP::JT_BYTE: { |
| 2537 | CHECK_EQ(width, 1U); |
| 2538 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2539 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2540 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2541 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2542 | VLOG(jdwp) << "get byte local " << vreg << " = " << intVal; |
| 2543 | JDWP::Set1(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2544 | break; |
| 2545 | } |
| 2546 | case JDWP::JT_SHORT: |
| 2547 | case JDWP::JT_CHAR: { |
| 2548 | CHECK_EQ(width, 2U); |
| 2549 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2550 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2551 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2552 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2553 | VLOG(jdwp) << "get short/char local " << vreg << " = " << intVal; |
| 2554 | JDWP::Set2BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2555 | break; |
| 2556 | } |
| 2557 | case JDWP::JT_INT: { |
| 2558 | CHECK_EQ(width, 4U); |
| 2559 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2560 | if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) { |
| 2561 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2562 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2563 | VLOG(jdwp) << "get int local " << vreg << " = " << intVal; |
| 2564 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2565 | break; |
| 2566 | } |
| 2567 | case JDWP::JT_FLOAT: { |
| 2568 | CHECK_EQ(width, 4U); |
| 2569 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2570 | if (!visitor.GetVReg(m, vreg, kFloatVReg, &intVal)) { |
| 2571 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2572 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2573 | VLOG(jdwp) << "get float local " << vreg << " = " << intVal; |
| 2574 | JDWP::Set4BE(buf + 1, intVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2575 | break; |
| 2576 | } |
| 2577 | case JDWP::JT_ARRAY: |
| 2578 | case JDWP::JT_CLASS_LOADER: |
| 2579 | case JDWP::JT_CLASS_OBJECT: |
| 2580 | case JDWP::JT_OBJECT: |
| 2581 | case JDWP::JT_STRING: |
| 2582 | case JDWP::JT_THREAD: |
| 2583 | case JDWP::JT_THREAD_GROUP: { |
| 2584 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
| 2585 | uint32_t intVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2586 | if (!visitor.GetVReg(m, vreg, kReferenceVReg, &intVal)) { |
| 2587 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2588 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2589 | mirror::Object* o = reinterpret_cast<mirror::Object*>(intVal); |
| 2590 | VLOG(jdwp) << "get " << tag << " object local " << vreg << " = " << o; |
| 2591 | if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) { |
| 2592 | LOG(FATAL) << StringPrintf("Found invalid object %#" PRIxPTR " in register v%u", |
| 2593 | reinterpret_cast<uintptr_t>(o), vreg) |
| 2594 | << GetStackContextAsString(visitor); |
| 2595 | UNREACHABLE(); |
| 2596 | } |
| 2597 | tag = TagFromObject(soa, o); |
| 2598 | JDWP::SetObjectId(buf + 1, gRegistry->Add(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2599 | break; |
| 2600 | } |
| 2601 | case JDWP::JT_DOUBLE: { |
| 2602 | CHECK_EQ(width, 8U); |
| 2603 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2604 | if (!visitor.GetVRegPair(m, vreg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) { |
| 2605 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2606 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2607 | VLOG(jdwp) << "get double local " << vreg << " = " << longVal; |
| 2608 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2609 | break; |
| 2610 | } |
| 2611 | case JDWP::JT_LONG: { |
| 2612 | CHECK_EQ(width, 8U); |
| 2613 | uint64_t longVal; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2614 | if (!visitor.GetVRegPair(m, vreg, kLongLoVReg, kLongHiVReg, &longVal)) { |
| 2615 | return FailGetLocalValue(visitor, vreg, tag); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2616 | } |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2617 | VLOG(jdwp) << "get long local " << vreg << " = " << longVal; |
| 2618 | JDWP::Set8BE(buf + 1, longVal); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2619 | break; |
| 2620 | } |
| 2621 | default: |
| 2622 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2623 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2624 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2625 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2626 | // Prepend tag, which may have been updated. |
| 2627 | JDWP::Set1(buf, tag); |
| 2628 | return JDWP::ERR_NONE; |
| 2629 | } |
| 2630 | |
| 2631 | JDWP::JdwpError Dbg::SetLocalValues(JDWP::Request* request) { |
| 2632 | JDWP::ObjectId thread_id = request->ReadThreadId(); |
| 2633 | JDWP::FrameId frame_id = request->ReadFrameId(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2634 | |
| 2635 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2636 | JDWP::JdwpError error; |
| 2637 | Thread* thread = DecodeThread(soa, thread_id, &error); |
| 2638 | if (error != JDWP::ERR_NONE) { |
| 2639 | return error; |
| 2640 | } |
| 2641 | if (!IsSuspendedForDebugger(soa, thread)) { |
| 2642 | return JDWP::ERR_THREAD_NOT_SUSPENDED; |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 2643 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2644 | // Find the frame with the given frame_id. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2645 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2646 | FindFrameVisitor visitor(thread, context.get(), frame_id); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2647 | visitor.WalkStack(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2648 | if (visitor.GetError() != JDWP::ERR_NONE) { |
| 2649 | return visitor.GetError(); |
| 2650 | } |
| 2651 | |
| 2652 | // Writes the values into visitor's context. |
| 2653 | int32_t slot_count = request->ReadSigned32("slot count"); |
| 2654 | for (int32_t i = 0; i < slot_count; ++i) { |
| 2655 | uint32_t slot = request->ReadUnsigned32("slot"); |
| 2656 | JDWP::JdwpTag sigByte = request->ReadTag(); |
| 2657 | size_t width = Dbg::GetTagWidth(sigByte); |
| 2658 | uint64_t value = request->ReadValue(width); |
| 2659 | |
| 2660 | VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value; |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 2661 | error = Dbg::SetLocalValue(visitor, slot, sigByte, value, width); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2662 | if (error != JDWP::ERR_NONE) { |
| 2663 | return error; |
| 2664 | } |
| 2665 | } |
| 2666 | return JDWP::ERR_NONE; |
| 2667 | } |
| 2668 | |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2669 | template<typename T> |
| 2670 | static JDWP::JdwpError FailSetLocalValue(const StackVisitor& visitor, uint16_t vreg, |
| 2671 | JDWP::JdwpTag tag, T value) |
| 2672 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2673 | LOG(ERROR) << "Failed to write " << tag << " local " << value |
| 2674 | << " (0x" << std::hex << value << ") into register v" << vreg |
| 2675 | << GetStackContextAsString(visitor); |
| 2676 | return kStackFrameLocalAccessError; |
| 2677 | } |
| 2678 | |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2679 | JDWP::JdwpError Dbg::SetLocalValue(StackVisitor& visitor, int slot, JDWP::JdwpTag tag, |
| 2680 | uint64_t value, size_t width) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2681 | ArtMethod* m = visitor.GetMethod(); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2682 | JDWP::JdwpError error = JDWP::ERR_NONE; |
| 2683 | uint16_t vreg = DemangleSlot(slot, m, &error); |
| 2684 | if (error != JDWP::ERR_NONE) { |
| 2685 | return error; |
| 2686 | } |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2687 | // 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] | 2688 | switch (tag) { |
| 2689 | case JDWP::JT_BOOLEAN: |
| 2690 | case JDWP::JT_BYTE: |
| 2691 | CHECK_EQ(width, 1U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2692 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2693 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2694 | } |
| 2695 | break; |
| 2696 | case JDWP::JT_SHORT: |
| 2697 | case JDWP::JT_CHAR: |
| 2698 | CHECK_EQ(width, 2U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2699 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2700 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2701 | } |
| 2702 | break; |
| 2703 | case JDWP::JT_INT: |
| 2704 | CHECK_EQ(width, 4U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2705 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) { |
| 2706 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2707 | } |
| 2708 | break; |
| 2709 | case JDWP::JT_FLOAT: |
| 2710 | CHECK_EQ(width, 4U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2711 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kFloatVReg)) { |
| 2712 | return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2713 | } |
| 2714 | break; |
| 2715 | case JDWP::JT_ARRAY: |
| 2716 | case JDWP::JT_CLASS_LOADER: |
| 2717 | case JDWP::JT_CLASS_OBJECT: |
| 2718 | case JDWP::JT_OBJECT: |
| 2719 | case JDWP::JT_STRING: |
| 2720 | case JDWP::JT_THREAD: |
| 2721 | case JDWP::JT_THREAD_GROUP: { |
| 2722 | CHECK_EQ(width, sizeof(JDWP::ObjectId)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2723 | mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value), |
| 2724 | &error); |
| 2725 | if (error != JDWP::ERR_NONE) { |
| 2726 | VLOG(jdwp) << tag << " object " << o << " is an invalid object"; |
| 2727 | return JDWP::ERR_INVALID_OBJECT; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2728 | } |
| 2729 | if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), |
| 2730 | kReferenceVReg)) { |
| 2731 | return FailSetLocalValue(visitor, vreg, tag, reinterpret_cast<uintptr_t>(o)); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2732 | } |
| 2733 | break; |
| 2734 | } |
| 2735 | case JDWP::JT_DOUBLE: { |
| 2736 | CHECK_EQ(width, 8U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2737 | if (!visitor.SetVRegPair(m, vreg, value, kDoubleLoVReg, kDoubleHiVReg)) { |
| 2738 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2739 | } |
| 2740 | break; |
| 2741 | } |
| 2742 | case JDWP::JT_LONG: { |
| 2743 | CHECK_EQ(width, 8U); |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2744 | if (!visitor.SetVRegPair(m, vreg, value, kLongLoVReg, kLongHiVReg)) { |
| 2745 | return FailSetLocalValue(visitor, vreg, tag, value); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2746 | } |
| 2747 | break; |
| 2748 | } |
| 2749 | default: |
| 2750 | LOG(FATAL) << "Unknown tag " << tag; |
Sebastien Hertz | abbabc8 | 2015-03-26 08:47:47 +0100 | [diff] [blame] | 2751 | UNREACHABLE(); |
Sebastien Hertz | 8009f39 | 2014-09-01 17:07:11 +0200 | [diff] [blame] | 2752 | } |
| 2753 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2754 | } |
| 2755 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2756 | static void SetEventLocation(JDWP::EventLocation* location, ArtMethod* m, uint32_t dex_pc) |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2757 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2758 | DCHECK(location != nullptr); |
| 2759 | if (m == nullptr) { |
| 2760 | memset(location, 0, sizeof(*location)); |
| 2761 | } else { |
| 2762 | location->method = m; |
| 2763 | 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] | 2764 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2765 | } |
| 2766 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2767 | void Dbg::PostLocationEvent(ArtMethod* m, int dex_pc, mirror::Object* this_object, |
Jeff Hao | 579b024 | 2013-11-18 13:16:49 -0800 | [diff] [blame] | 2768 | int event_flags, const JValue* return_value) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2769 | if (!IsDebuggerActive()) { |
| 2770 | return; |
| 2771 | } |
| 2772 | DCHECK(m != nullptr); |
| 2773 | DCHECK_EQ(m->IsStatic(), this_object == nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2774 | JDWP::EventLocation location; |
| 2775 | SetEventLocation(&location, m, dex_pc); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2776 | |
Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2777 | // We need to be sure no exception is pending when calling JdwpState::PostLocationEvent. |
| 2778 | // This is required to be able to call JNI functions to create JDWP ids. To achieve this, |
| 2779 | // we temporarily clear the current thread's exception (if any) and will restore it after |
| 2780 | // the call. |
| 2781 | // Note: the only way to get a pending exception here is to suspend on a move-exception |
| 2782 | // instruction. |
| 2783 | Thread* const self = Thread::Current(); |
| 2784 | StackHandleScope<1> hs(self); |
| 2785 | Handle<mirror::Throwable> pending_exception(hs.NewHandle(self->GetException())); |
| 2786 | self->ClearException(); |
| 2787 | if (kIsDebugBuild && pending_exception.Get() != nullptr) { |
| 2788 | const DexFile::CodeItem* code_item = location.method->GetCodeItem(); |
| 2789 | const Instruction* instr = Instruction::At(&code_item->insns_[location.dex_pc]); |
| 2790 | CHECK_EQ(Instruction::MOVE_EXCEPTION, instr->Opcode()); |
| 2791 | } |
| 2792 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2793 | gJdwpState->PostLocationEvent(&location, this_object, event_flags, return_value); |
Sebastien Hertz | de48aa6 | 2015-05-26 11:53:39 +0200 | [diff] [blame] | 2794 | |
| 2795 | if (pending_exception.Get() != nullptr) { |
| 2796 | self->SetException(pending_exception.Get()); |
| 2797 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2798 | } |
| 2799 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2800 | void Dbg::PostFieldAccessEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2801 | mirror::Object* this_object, ArtField* f) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2802 | if (!IsDebuggerActive()) { |
| 2803 | return; |
| 2804 | } |
| 2805 | DCHECK(m != nullptr); |
| 2806 | DCHECK(f != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2807 | JDWP::EventLocation location; |
| 2808 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2809 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2810 | gJdwpState->PostFieldEvent(&location, f, this_object, nullptr, false); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2811 | } |
| 2812 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2813 | void Dbg::PostFieldModificationEvent(ArtMethod* m, int dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2814 | mirror::Object* this_object, ArtField* f, |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2815 | const JValue* field_value) { |
| 2816 | if (!IsDebuggerActive()) { |
| 2817 | return; |
| 2818 | } |
| 2819 | DCHECK(m != nullptr); |
| 2820 | DCHECK(f != nullptr); |
| 2821 | DCHECK(field_value != nullptr); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2822 | JDWP::EventLocation location; |
| 2823 | SetEventLocation(&location, m, dex_pc); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2824 | |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2825 | gJdwpState->PostFieldEvent(&location, f, this_object, field_value, true); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 2826 | } |
| 2827 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2828 | /** |
| 2829 | * Finds the location where this exception will be caught. We search until we reach the top |
| 2830 | * frame, in which case this exception is considered uncaught. |
| 2831 | */ |
| 2832 | class CatchLocationFinder : public StackVisitor { |
| 2833 | public: |
| 2834 | CatchLocationFinder(Thread* self, const Handle<mirror::Throwable>& exception, Context* context) |
| 2835 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 2836 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2837 | self_(self), |
| 2838 | exception_(exception), |
| 2839 | handle_scope_(self), |
| 2840 | this_at_throw_(handle_scope_.NewHandle<mirror::Object>(nullptr)), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2841 | catch_method_(nullptr), |
| 2842 | throw_method_(nullptr), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2843 | catch_dex_pc_(DexFile::kDexNoIndex), |
| 2844 | throw_dex_pc_(DexFile::kDexNoIndex) { |
| 2845 | } |
| 2846 | |
| 2847 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2848 | ArtMethod* method = GetMethod(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2849 | DCHECK(method != nullptr); |
| 2850 | if (method->IsRuntimeMethod()) { |
| 2851 | // Ignore callee save method. |
| 2852 | DCHECK(method->IsCalleeSaveMethod()); |
| 2853 | return true; |
| 2854 | } |
| 2855 | |
| 2856 | uint32_t dex_pc = GetDexPc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2857 | if (throw_method_ == nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2858 | // First Java method found. It is either the method that threw the exception, |
| 2859 | // or the Java native method that is reporting an exception thrown by |
| 2860 | // native code. |
| 2861 | this_at_throw_.Assign(GetThisObject()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2862 | throw_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2863 | throw_dex_pc_ = dex_pc; |
| 2864 | } |
| 2865 | |
| 2866 | if (dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2867 | StackHandleScope<1> hs(self_); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2868 | uint32_t found_dex_pc; |
| 2869 | Handle<mirror::Class> exception_class(hs.NewHandle(exception_->GetClass())); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2870 | bool unused_clear_exception; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2871 | found_dex_pc = method->FindCatchBlock(exception_class, dex_pc, &unused_clear_exception); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2872 | if (found_dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2873 | catch_method_ = method; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2874 | catch_dex_pc_ = found_dex_pc; |
| 2875 | return false; // End stack walk. |
| 2876 | } |
| 2877 | } |
| 2878 | return true; // Continue stack walk. |
| 2879 | } |
| 2880 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2881 | ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2882 | return catch_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2885 | ArtMethod* GetThrowMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2886 | return throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | mirror::Object* GetThisAtThrow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2890 | return this_at_throw_.Get(); |
| 2891 | } |
| 2892 | |
| 2893 | uint32_t GetCatchDexPc() const { |
| 2894 | return catch_dex_pc_; |
| 2895 | } |
| 2896 | |
| 2897 | uint32_t GetThrowDexPc() const { |
| 2898 | return throw_dex_pc_; |
| 2899 | } |
| 2900 | |
| 2901 | private: |
| 2902 | Thread* const self_; |
| 2903 | const Handle<mirror::Throwable>& exception_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2904 | StackHandleScope<1> handle_scope_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2905 | MutableHandle<mirror::Object> this_at_throw_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2906 | ArtMethod* catch_method_; |
| 2907 | ArtMethod* throw_method_; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2908 | uint32_t catch_dex_pc_; |
| 2909 | uint32_t throw_dex_pc_; |
| 2910 | |
| 2911 | DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder); |
| 2912 | }; |
| 2913 | |
| 2914 | void Dbg::PostException(mirror::Throwable* exception_object) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2915 | if (!IsDebuggerActive()) { |
Ian Rogers | 0ad5bb8 | 2011-12-07 10:16:32 -0800 | [diff] [blame] | 2916 | return; |
| 2917 | } |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 2918 | Thread* const self = Thread::Current(); |
| 2919 | StackHandleScope<1> handle_scope(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2920 | Handle<mirror::Throwable> h_exception(handle_scope.NewHandle(exception_object)); |
| 2921 | std::unique_ptr<Context> context(Context::Create()); |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 2922 | CatchLocationFinder clf(self, h_exception, context.get()); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2923 | clf.WalkStack(/* include_transitions */ false); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2924 | JDWP::EventLocation exception_throw_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2925 | SetEventLocation(&exception_throw_location, clf.GetThrowMethod(), clf.GetThrowDexPc()); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2926 | JDWP::EventLocation exception_catch_location; |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2927 | SetEventLocation(&exception_catch_location, clf.GetCatchMethod(), clf.GetCatchDexPc()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2928 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2929 | gJdwpState->PostException(&exception_throw_location, h_exception.Get(), &exception_catch_location, |
| 2930 | clf.GetThisAtThrow()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2931 | } |
| 2932 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2933 | void Dbg::PostClassPrepare(mirror::Class* c) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 2934 | if (!IsDebuggerActive()) { |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2935 | return; |
| 2936 | } |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 2937 | gJdwpState->PostClassPrepare(c); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 2938 | } |
| 2939 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2940 | void Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2941 | ArtMethod* m, uint32_t dex_pc, |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 2942 | int event_flags, const JValue* return_value) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2943 | if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) { |
Elliott Hughes | 2aa2e39 | 2012-02-17 17:15:43 -0800 | [diff] [blame] | 2944 | return; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2945 | } |
| 2946 | |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2947 | if (IsBreakpoint(m, dex_pc)) { |
| 2948 | event_flags |= kBreakpoint; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2949 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2950 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2951 | // If the debugger is single-stepping one of our threads, check to |
| 2952 | // see if we're that thread and we've reached a step point. |
| 2953 | const SingleStepControl* single_step_control = thread->GetSingleStepControl(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2954 | if (single_step_control != nullptr) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2955 | CHECK(!m->IsNative()); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2956 | if (single_step_control->GetStepDepth() == JDWP::SD_INTO) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2957 | // Step into method calls. We break when the line number |
| 2958 | // or method pointer changes. If we're in SS_MIN mode, we |
| 2959 | // always stop. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2960 | if (single_step_control->GetMethod() != m) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2961 | event_flags |= kSingleStep; |
| 2962 | VLOG(jdwp) << "SS new method"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2963 | } else if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2964 | event_flags |= kSingleStep; |
| 2965 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 2966 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2967 | event_flags |= kSingleStep; |
| 2968 | VLOG(jdwp) << "SS new line"; |
| 2969 | } |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2970 | } else if (single_step_control->GetStepDepth() == JDWP::SD_OVER) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2971 | // Step over method calls. We break when the line number is |
| 2972 | // different and the frame depth is <= the original frame |
| 2973 | // depth. (We can't just compare on the method, because we |
| 2974 | // might get unrolled past it by an exception, and it's tricky |
| 2975 | // to identify recursion.) |
| 2976 | |
| 2977 | int stack_depth = GetStackDepth(thread); |
| 2978 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2979 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2980 | // Popped up one or more frames, always trigger. |
| 2981 | event_flags |= kSingleStep; |
| 2982 | VLOG(jdwp) << "SS method pop"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2983 | } else if (stack_depth == single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2984 | // Same depth, see if we moved. |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2985 | if (single_step_control->GetStepSize() == JDWP::SS_MIN) { |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 2986 | event_flags |= kSingleStep; |
| 2987 | VLOG(jdwp) << "SS new instruction"; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 2988 | } else if (single_step_control->ContainsDexPc(dex_pc)) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 2989 | event_flags |= kSingleStep; |
| 2990 | VLOG(jdwp) << "SS new line"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2991 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2992 | } |
| 2993 | } else { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 2994 | CHECK_EQ(single_step_control->GetStepDepth(), JDWP::SD_OUT); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2995 | // Return from the current method. We break when the frame |
| 2996 | // depth pops up. |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 2997 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 2998 | // This differs from the "method exit" break in that it stops |
| 2999 | // with the PC at the next instruction in the returned-to |
| 3000 | // function, rather than the end of the returning function. |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3001 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3002 | int stack_depth = GetStackDepth(thread); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3003 | if (stack_depth < single_step_control->GetStackDepth()) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3004 | event_flags |= kSingleStep; |
| 3005 | VLOG(jdwp) << "SS method pop"; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3006 | } |
| 3007 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3008 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3009 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3010 | // If there's something interesting going on, see if it matches one |
| 3011 | // of the debugger filters. |
| 3012 | if (event_flags != 0) { |
Sebastien Hertz | 8379b22 | 2014-02-24 17:38:15 +0100 | [diff] [blame] | 3013 | Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 3014 | } |
| 3015 | } |
| 3016 | |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3017 | size_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) { |
| 3018 | switch (instrumentation_event) { |
| 3019 | case instrumentation::Instrumentation::kMethodEntered: |
| 3020 | return &method_enter_event_ref_count_; |
| 3021 | case instrumentation::Instrumentation::kMethodExited: |
| 3022 | return &method_exit_event_ref_count_; |
| 3023 | case instrumentation::Instrumentation::kDexPcMoved: |
| 3024 | return &dex_pc_change_event_ref_count_; |
| 3025 | case instrumentation::Instrumentation::kFieldRead: |
| 3026 | return &field_read_event_ref_count_; |
| 3027 | case instrumentation::Instrumentation::kFieldWritten: |
| 3028 | return &field_write_event_ref_count_; |
| 3029 | case instrumentation::Instrumentation::kExceptionCaught: |
| 3030 | return &exception_catch_event_ref_count_; |
| 3031 | default: |
| 3032 | return nullptr; |
| 3033 | } |
| 3034 | } |
| 3035 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3036 | // Process request while all mutator threads are suspended. |
| 3037 | void Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3038 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3039 | switch (request.GetKind()) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3040 | case DeoptimizationRequest::kNothing: |
| 3041 | LOG(WARNING) << "Ignoring empty deoptimization request."; |
| 3042 | break; |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3043 | case DeoptimizationRequest::kRegisterForEvent: |
| 3044 | VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3045 | request.InstrumentationEvent()); |
| 3046 | instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent()); |
| 3047 | instrumentation_events_ |= request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3048 | break; |
| 3049 | case DeoptimizationRequest::kUnregisterForEvent: |
| 3050 | VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3051 | request.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3052 | instrumentation->RemoveListener(&gDebugInstrumentationListener, |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3053 | request.InstrumentationEvent()); |
| 3054 | instrumentation_events_ &= ~request.InstrumentationEvent(); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3055 | break; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3056 | case DeoptimizationRequest::kFullDeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3057 | VLOG(jdwp) << "Deoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3058 | instrumentation->DeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3059 | VLOG(jdwp) << "Deoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3060 | break; |
| 3061 | case DeoptimizationRequest::kFullUndeoptimization: |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3062 | VLOG(jdwp) << "Undeoptimize the world ..."; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 3063 | instrumentation->UndeoptimizeEverything(kDbgInstrumentationKey); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3064 | VLOG(jdwp) << "Undeoptimize the world DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3065 | break; |
| 3066 | case DeoptimizationRequest::kSelectiveDeoptimization: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3067 | VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " ..."; |
| 3068 | instrumentation->Deoptimize(request.Method()); |
| 3069 | VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3070 | break; |
| 3071 | case DeoptimizationRequest::kSelectiveUndeoptimization: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3072 | VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " ..."; |
| 3073 | instrumentation->Undeoptimize(request.Method()); |
| 3074 | VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " DONE"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3075 | break; |
| 3076 | default: |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3077 | LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3078 | break; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3079 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3080 | } |
| 3081 | |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3082 | void Dbg::RequestDeoptimization(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3083 | if (req.GetKind() == DeoptimizationRequest::kNothing) { |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3084 | // Nothing to do. |
| 3085 | return; |
| 3086 | } |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3087 | MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3088 | RequestDeoptimizationLocked(req); |
| 3089 | } |
| 3090 | |
| 3091 | void Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3092 | switch (req.GetKind()) { |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3093 | case DeoptimizationRequest::kRegisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3094 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3095 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3096 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3097 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3098 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3099 | 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] | 3100 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3101 | deoptimization_requests_.push_back(req); |
| 3102 | } |
| 3103 | *counter = *counter + 1; |
| 3104 | break; |
| 3105 | } |
| 3106 | case DeoptimizationRequest::kUnregisterForEvent: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3107 | DCHECK_NE(req.InstrumentationEvent(), 0u); |
| 3108 | size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3109 | CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x", |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3110 | req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3111 | *counter = *counter - 1; |
| 3112 | if (*counter == 0) { |
Sebastien Hertz | 7d2ae43 | 2014-05-15 11:26:34 +0200 | [diff] [blame] | 3113 | 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] | 3114 | deoptimization_requests_.size(), req.InstrumentationEvent()); |
Sebastien Hertz | 42cd43f | 2014-05-13 14:15:41 +0200 | [diff] [blame] | 3115 | deoptimization_requests_.push_back(req); |
| 3116 | } |
| 3117 | break; |
| 3118 | } |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3119 | case DeoptimizationRequest::kFullDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3120 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3121 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3122 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3123 | << " for full deoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3124 | deoptimization_requests_.push_back(req); |
| 3125 | } |
| 3126 | ++full_deoptimization_event_count_; |
| 3127 | break; |
| 3128 | } |
| 3129 | case DeoptimizationRequest::kFullUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3130 | DCHECK(req.Method() == nullptr); |
Sebastien Hertz | e713d93 | 2014-05-15 10:48:53 +0200 | [diff] [blame] | 3131 | DCHECK_GT(full_deoptimization_event_count_, 0U); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3132 | --full_deoptimization_event_count_; |
| 3133 | if (full_deoptimization_event_count_ == 0) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3134 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
| 3135 | << " for full undeoptimization"; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3136 | deoptimization_requests_.push_back(req); |
| 3137 | } |
| 3138 | break; |
| 3139 | } |
| 3140 | case DeoptimizationRequest::kSelectiveDeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3141 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3142 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3143 | << " for deoptimization of " << PrettyMethod(req.Method()); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3144 | deoptimization_requests_.push_back(req); |
| 3145 | break; |
| 3146 | } |
| 3147 | case DeoptimizationRequest::kSelectiveUndeoptimization: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3148 | DCHECK(req.Method() != nullptr); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3149 | VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size() |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3150 | << " for undeoptimization of " << PrettyMethod(req.Method()); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3151 | deoptimization_requests_.push_back(req); |
| 3152 | break; |
| 3153 | } |
| 3154 | default: { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3155 | LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind(); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3156 | break; |
| 3157 | } |
| 3158 | } |
| 3159 | } |
| 3160 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3161 | void Dbg::ManageDeoptimization() { |
| 3162 | Thread* const self = Thread::Current(); |
| 3163 | { |
| 3164 | // Avoid suspend/resume if there is no pending request. |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3165 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3166 | if (deoptimization_requests_.empty()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3167 | return; |
| 3168 | } |
| 3169 | } |
| 3170 | CHECK_EQ(self->GetState(), kRunnable); |
| 3171 | self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization); |
| 3172 | // We need to suspend mutator threads first. |
| 3173 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 3174 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3175 | const ThreadState old_state = self->SetStateUnsafe(kRunnable); |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3176 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 3177 | MutexLock mu(self, *Locks::deoptimization_lock_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3178 | size_t req_index = 0; |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3179 | for (DeoptimizationRequest& request : deoptimization_requests_) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 3180 | VLOG(jdwp) << "Process deoptimization request #" << req_index++; |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 3181 | ProcessDeoptimizationRequest(request); |
| 3182 | } |
| 3183 | deoptimization_requests_.clear(); |
| 3184 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3185 | CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable); |
| 3186 | runtime->GetThreadList()->ResumeAll(); |
| 3187 | self->TransitionFromSuspendedToRunnable(); |
| 3188 | } |
| 3189 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3190 | static bool IsMethodPossiblyInlined(Thread* self, ArtMethod* m) |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3191 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3192 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3193 | if (code_item == nullptr) { |
| 3194 | // TODO We should not be asked to watch location in a native or abstract method so the code item |
| 3195 | // should never be null. We could just check we never encounter this case. |
| 3196 | return false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3197 | } |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3198 | // Note: method verifier may cause thread suspension. |
| 3199 | self->AssertThreadSuspensionIsAllowable(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3200 | StackHandleScope<2> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3201 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 3202 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 3203 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3204 | verifier::MethodVerifier verifier(self, dex_cache->GetDexFile(), dex_cache, class_loader, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3205 | &m->GetClassDef(), code_item, m->GetDexMethodIndex(), m, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 3206 | m->GetAccessFlags(), false, true, false, true); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3207 | // Note: we don't need to verify the method. |
| 3208 | return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr); |
| 3209 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3210 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3211 | static const Breakpoint* FindFirstBreakpointForMethod(ArtMethod* m) |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3212 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3213 | for (Breakpoint& breakpoint : gBreakpoints) { |
| 3214 | if (breakpoint.Method() == m) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3215 | return &breakpoint; |
| 3216 | } |
| 3217 | } |
| 3218 | return nullptr; |
| 3219 | } |
| 3220 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3221 | bool Dbg::MethodHasAnyBreakpoints(ArtMethod* method) { |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 3222 | ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
| 3223 | return FindFirstBreakpointForMethod(method) != nullptr; |
| 3224 | } |
| 3225 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3226 | // Sanity checks all existing breakpoints on the same method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3227 | static void SanityCheckExistingBreakpoints(ArtMethod* m, |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3228 | DeoptimizationRequest::Kind deoptimization_kind) |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3229 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3230 | for (const Breakpoint& breakpoint : gBreakpoints) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3231 | if (breakpoint.Method() == m) { |
| 3232 | CHECK_EQ(deoptimization_kind, breakpoint.GetDeoptimizationKind()); |
| 3233 | } |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3234 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3235 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 3236 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3237 | // We should have deoptimized everything but not "selectively" deoptimized this method. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3238 | CHECK(instrumentation->AreAllMethodsDeoptimized()); |
| 3239 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3240 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3241 | // We should have "selectively" deoptimized this method. |
| 3242 | // Note: while we have not deoptimized everything for this method, we may have done it for |
| 3243 | // another event. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3244 | CHECK(instrumentation->IsDeoptimized(m)); |
| 3245 | } else { |
| 3246 | // This method does not require deoptimization. |
| 3247 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3248 | CHECK(!instrumentation->IsDeoptimized(m)); |
| 3249 | } |
| 3250 | } |
| 3251 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3252 | // Returns the deoptimization kind required to set a breakpoint in a method. |
| 3253 | // If a breakpoint has already been set, we also return the first breakpoint |
| 3254 | // through the given 'existing_brkpt' pointer. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3255 | static DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3256 | ArtMethod* m, |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3257 | const Breakpoint** existing_brkpt) |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3258 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3259 | if (!Dbg::RequiresDeoptimization()) { |
| 3260 | // We already run in interpreter-only mode so we don't need to deoptimize anything. |
| 3261 | VLOG(jdwp) << "No need for deoptimization when fully running with interpreter for method " |
| 3262 | << PrettyMethod(m); |
| 3263 | return DeoptimizationRequest::kNothing; |
| 3264 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3265 | const Breakpoint* first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3266 | { |
| 3267 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3268 | first_breakpoint = FindFirstBreakpointForMethod(m); |
| 3269 | *existing_brkpt = first_breakpoint; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3270 | } |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3271 | |
| 3272 | if (first_breakpoint == nullptr) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3273 | // There is no breakpoint on this method yet: we need to deoptimize. If this method may be |
| 3274 | // inlined, we deoptimize everything; otherwise we deoptimize only this method. |
| 3275 | // Note: IsMethodPossiblyInlined goes into the method verifier and may cause thread suspension. |
| 3276 | // Therefore we must not hold any lock when we call it. |
| 3277 | bool need_full_deoptimization = IsMethodPossiblyInlined(self, m); |
| 3278 | if (need_full_deoptimization) { |
| 3279 | VLOG(jdwp) << "Need full deoptimization because of possible inlining of method " |
| 3280 | << PrettyMethod(m); |
| 3281 | return DeoptimizationRequest::kFullDeoptimization; |
| 3282 | } else { |
| 3283 | // We don't need to deoptimize if the method has not been compiled. |
| 3284 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 3285 | const bool is_compiled = class_linker->GetOatMethodQuickCodeFor(m) != nullptr; |
| 3286 | if (is_compiled) { |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 3287 | // If the method may be called through its direct code pointer (without loading |
| 3288 | // its updated entrypoint), we need full deoptimization to not miss the breakpoint. |
| 3289 | if (class_linker->MayBeCalledWithDirectCodePointer(m)) { |
| 3290 | VLOG(jdwp) << "Need full deoptimization because of possible direct code call " |
| 3291 | << "into image for compiled method " << PrettyMethod(m); |
| 3292 | return DeoptimizationRequest::kFullDeoptimization; |
| 3293 | } else { |
| 3294 | VLOG(jdwp) << "Need selective deoptimization for compiled method " << PrettyMethod(m); |
| 3295 | return DeoptimizationRequest::kSelectiveDeoptimization; |
| 3296 | } |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3297 | } else { |
| 3298 | // Method is not compiled: we don't need to deoptimize. |
| 3299 | VLOG(jdwp) << "No need for deoptimization for non-compiled method " << PrettyMethod(m); |
| 3300 | return DeoptimizationRequest::kNothing; |
| 3301 | } |
| 3302 | } |
| 3303 | } else { |
| 3304 | // There is at least one breakpoint for this method: we don't need to deoptimize. |
| 3305 | // Let's check that all breakpoints are configured the same way for deoptimization. |
| 3306 | VLOG(jdwp) << "Breakpoint already set: no deoptimization is required"; |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3307 | DeoptimizationRequest::Kind deoptimization_kind = first_breakpoint->GetDeoptimizationKind(); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3308 | if (kIsDebugBuild) { |
| 3309 | ReaderMutexLock mu(self, *Locks::breakpoint_lock_); |
| 3310 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
| 3311 | } |
| 3312 | return DeoptimizationRequest::kNothing; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3313 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3314 | } |
| 3315 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3316 | // Installs a breakpoint at the specified location. Also indicates through the deoptimization |
| 3317 | // request if we need to deoptimize. |
| 3318 | void Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
| 3319 | Thread* const self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3320 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3321 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3322 | |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3323 | const Breakpoint* existing_breakpoint = nullptr; |
| 3324 | const DeoptimizationRequest::Kind deoptimization_kind = |
| 3325 | GetRequiredDeoptimizationKind(self, m, &existing_breakpoint); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3326 | req->SetKind(deoptimization_kind); |
| 3327 | if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
| 3328 | req->SetMethod(m); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3329 | } else { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3330 | CHECK(deoptimization_kind == DeoptimizationRequest::kNothing || |
| 3331 | deoptimization_kind == DeoptimizationRequest::kFullDeoptimization); |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3332 | req->SetMethod(nullptr); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 3333 | } |
| 3334 | |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3335 | { |
| 3336 | WriterMutexLock mu(self, *Locks::breakpoint_lock_); |
Sebastien Hertz | abe93e0 | 2014-12-17 16:35:50 +0100 | [diff] [blame] | 3337 | // If there is at least one existing breakpoint on the same method, the new breakpoint |
| 3338 | // must have the same deoptimization kind than the existing breakpoint(s). |
| 3339 | DeoptimizationRequest::Kind breakpoint_deoptimization_kind; |
| 3340 | if (existing_breakpoint != nullptr) { |
| 3341 | breakpoint_deoptimization_kind = existing_breakpoint->GetDeoptimizationKind(); |
| 3342 | } else { |
| 3343 | breakpoint_deoptimization_kind = deoptimization_kind; |
| 3344 | } |
| 3345 | gBreakpoints.push_back(Breakpoint(m, location->dex_pc, breakpoint_deoptimization_kind)); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3346 | VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " |
| 3347 | << gBreakpoints[gBreakpoints.size() - 1]; |
| 3348 | } |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | // Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization |
| 3352 | // request if we need to undeoptimize. |
| 3353 | void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) { |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 3354 | WriterMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3355 | ArtMethod* m = FromMethodId(location->method_id); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3356 | DCHECK(m != nullptr) << "No method for method id " << location->method_id; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3357 | DeoptimizationRequest::Kind deoptimization_kind = DeoptimizationRequest::kNothing; |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3358 | for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3359 | if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].Method() == m) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3360 | VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i]; |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3361 | deoptimization_kind = gBreakpoints[i].GetDeoptimizationKind(); |
| 3362 | DCHECK_EQ(deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization, |
| 3363 | Runtime::Current()->GetInstrumentation()->IsDeoptimized(m)); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3364 | gBreakpoints.erase(gBreakpoints.begin() + i); |
| 3365 | break; |
| 3366 | } |
| 3367 | } |
| 3368 | const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m); |
| 3369 | if (existing_breakpoint == nullptr) { |
| 3370 | // There is no more breakpoint on this method: we need to undeoptimize. |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3371 | if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3372 | // This method required full deoptimization: we need to undeoptimize everything. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3373 | req->SetKind(DeoptimizationRequest::kFullUndeoptimization); |
| 3374 | req->SetMethod(nullptr); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3375 | } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) { |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3376 | // This method required selective deoptimization: we need to undeoptimize only that method. |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 3377 | req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization); |
| 3378 | req->SetMethod(m); |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3379 | } else { |
| 3380 | // This method had no need for deoptimization: do nothing. |
| 3381 | CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing); |
| 3382 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3383 | req->SetMethod(nullptr); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 3384 | } |
| 3385 | } else { |
| 3386 | // 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] | 3387 | req->SetKind(DeoptimizationRequest::kNothing); |
| 3388 | req->SetMethod(nullptr); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3389 | if (kIsDebugBuild) { |
Sebastien Hertz | f392879 | 2014-11-17 19:00:37 +0100 | [diff] [blame] | 3390 | SanityCheckExistingBreakpoints(m, deoptimization_kind); |
Sebastien Hertz | 4d1e9ab | 2014-09-18 16:03:34 +0200 | [diff] [blame] | 3391 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3392 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3393 | } |
| 3394 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3395 | bool Dbg::IsForcedInterpreterNeededForCallingImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3396 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3397 | if (ssc == nullptr) { |
| 3398 | // If we are not single-stepping, then we don't have to force interpreter. |
| 3399 | return false; |
| 3400 | } |
| 3401 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
| 3402 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3403 | return false; |
| 3404 | } |
| 3405 | |
| 3406 | if (!m->IsNative() && !m->IsProxyMethod()) { |
| 3407 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3408 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3409 | return true; |
| 3410 | } |
| 3411 | } |
| 3412 | return false; |
| 3413 | } |
| 3414 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3415 | bool Dbg::IsForcedInterpreterNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 3416 | instrumentation::Instrumentation* const instrumentation = |
| 3417 | Runtime::Current()->GetInstrumentation(); |
| 3418 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3419 | if (instrumentation->InterpretOnly()) { |
| 3420 | return false; |
| 3421 | } |
| 3422 | // We can only interpret pure Java method. |
| 3423 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3424 | return false; |
| 3425 | } |
| 3426 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3427 | if (ssc != nullptr) { |
| 3428 | // If we want to step into a method, then we have to force interpreter on that call. |
| 3429 | if (ssc->GetStepDepth() == JDWP::SD_INTO) { |
| 3430 | return true; |
| 3431 | } |
| 3432 | // If we are stepping out from a static initializer, by issuing a step |
| 3433 | // in or step over, that was implicitly invoked by calling a static method, |
| 3434 | // then we need to step into that method. Having a lower stack depth than |
| 3435 | // the one the single step control has indicates that the step originates |
| 3436 | // from the static initializer. |
| 3437 | if (ssc->GetStepDepth() != JDWP::SD_OUT && |
| 3438 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3439 | return true; |
| 3440 | } |
| 3441 | } |
| 3442 | // There are cases where we have to force interpreter on deoptimized methods, |
| 3443 | // because in some cases the call will not be performed by invoking an entry |
| 3444 | // point that has been replaced by the deoptimization, but instead by directly |
| 3445 | // invoking the compiled code of the method, for example. |
| 3446 | return instrumentation->IsDeoptimized(m); |
| 3447 | } |
| 3448 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3449 | bool Dbg::IsForcedInstrumentationNeededForResolutionImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3450 | // 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] | 3451 | if (m == nullptr) { |
| 3452 | return false; |
| 3453 | } |
| 3454 | instrumentation::Instrumentation* const instrumentation = |
| 3455 | Runtime::Current()->GetInstrumentation(); |
| 3456 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3457 | if (instrumentation->InterpretOnly()) { |
| 3458 | return false; |
| 3459 | } |
| 3460 | // We can only interpret pure Java method. |
| 3461 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3462 | return false; |
| 3463 | } |
| 3464 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3465 | if (ssc != nullptr) { |
| 3466 | // If we are stepping out from a static initializer, by issuing a step |
| 3467 | // out, that was implicitly invoked by calling a static method, then we |
| 3468 | // need to step into the caller of that method. Having a lower stack |
| 3469 | // depth than the one the single step control has indicates that the |
| 3470 | // step originates from the static initializer. |
| 3471 | if (ssc->GetStepDepth() == JDWP::SD_OUT && |
| 3472 | ssc->GetStackDepth() > GetStackDepth(thread)) { |
| 3473 | return true; |
| 3474 | } |
| 3475 | } |
| 3476 | // If we are returning from a static intializer, that was implicitly |
| 3477 | // invoked by calling a static method and the caller is deoptimized, |
| 3478 | // then we have to deoptimize the stack without forcing interpreter |
| 3479 | // on the static method that was called originally. This problem can |
| 3480 | // be solved easily by forcing instrumentation on the called method, |
| 3481 | // because the instrumentation exit hook will recognise the need of |
| 3482 | // stack deoptimization by calling IsForcedInterpreterNeededForUpcall. |
| 3483 | return instrumentation->IsDeoptimized(m); |
| 3484 | } |
| 3485 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3486 | bool Dbg::IsForcedInterpreterNeededForUpcallImpl(Thread* thread, ArtMethod* m) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3487 | // 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] | 3488 | if (m == nullptr) { |
| 3489 | return false; |
| 3490 | } |
| 3491 | instrumentation::Instrumentation* const instrumentation = |
| 3492 | Runtime::Current()->GetInstrumentation(); |
| 3493 | // If we are in interpreter only mode, then we don't have to force interpreter. |
| 3494 | if (instrumentation->InterpretOnly()) { |
| 3495 | return false; |
| 3496 | } |
| 3497 | // We can only interpret pure Java method. |
| 3498 | if (m->IsNative() || m->IsProxyMethod()) { |
| 3499 | return false; |
| 3500 | } |
| 3501 | const SingleStepControl* const ssc = thread->GetSingleStepControl(); |
| 3502 | if (ssc != nullptr) { |
| 3503 | // The debugger is not interested in what is happening under the level |
| 3504 | // of the step, thus we only force interpreter when we are not below of |
| 3505 | // the step. |
| 3506 | if (ssc->GetStackDepth() >= GetStackDepth(thread)) { |
| 3507 | return true; |
| 3508 | } |
| 3509 | } |
| 3510 | // We have to require stack deoptimization if the upcall is deoptimized. |
| 3511 | return instrumentation->IsDeoptimized(m); |
| 3512 | } |
| 3513 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3514 | // Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't |
| 3515 | // cause suspension if the thread is the current thread. |
| 3516 | class ScopedThreadSuspension { |
| 3517 | public: |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3518 | ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id) |
Sebastien Hertz | 52d131d | 2014-03-13 16:17:40 +0100 | [diff] [blame] | 3519 | LOCKS_EXCLUDED(Locks::thread_list_lock_) |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3520 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3521 | thread_(nullptr), |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3522 | error_(JDWP::ERR_NONE), |
| 3523 | self_suspend_(false), |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 3524 | other_suspend_(false) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3525 | ScopedObjectAccessUnchecked soa(self); |
Sebastien Hertz | 6920639 | 2015-04-07 15:54:25 +0200 | [diff] [blame] | 3526 | thread_ = DecodeThread(soa, thread_id, &error_); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3527 | if (error_ == JDWP::ERR_NONE) { |
| 3528 | if (thread_ == soa.Self()) { |
| 3529 | self_suspend_ = true; |
| 3530 | } else { |
| 3531 | soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 3532 | jobject thread_peer = Dbg::GetObjectRegistry()->GetJObject(thread_id); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3533 | bool timed_out; |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 3534 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 3535 | Thread* suspended_thread = thread_list->SuspendThreadByPeer(thread_peer, true, true, |
| 3536 | &timed_out); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3537 | CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension); |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 3538 | if (suspended_thread == nullptr) { |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3539 | // Thread terminated from under us while suspending. |
| 3540 | error_ = JDWP::ERR_INVALID_THREAD; |
| 3541 | } else { |
| 3542 | CHECK_EQ(suspended_thread, thread_); |
| 3543 | other_suspend_ = true; |
| 3544 | } |
| 3545 | } |
| 3546 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3547 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3548 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3549 | Thread* GetThread() const { |
| 3550 | return thread_; |
| 3551 | } |
| 3552 | |
| 3553 | JDWP::JdwpError GetError() const { |
| 3554 | return error_; |
| 3555 | } |
| 3556 | |
| 3557 | ~ScopedThreadSuspension() { |
| 3558 | if (other_suspend_) { |
| 3559 | Runtime::Current()->GetThreadList()->Resume(thread_, true); |
| 3560 | } |
| 3561 | } |
| 3562 | |
| 3563 | private: |
| 3564 | Thread* thread_; |
| 3565 | JDWP::JdwpError error_; |
| 3566 | bool self_suspend_; |
| 3567 | bool other_suspend_; |
| 3568 | }; |
| 3569 | |
| 3570 | JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size, |
| 3571 | JDWP::JdwpStepDepth step_depth) { |
| 3572 | Thread* self = Thread::Current(); |
| 3573 | ScopedThreadSuspension sts(self, thread_id); |
| 3574 | if (sts.GetError() != JDWP::ERR_NONE) { |
| 3575 | return sts.GetError(); |
| 3576 | } |
| 3577 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3578 | // 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] | 3579 | // is for step-out. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3580 | struct SingleStepStackVisitor : public StackVisitor { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3581 | explicit SingleStepStackVisitor(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 3582 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 3583 | stack_depth(0), |
| 3584 | method(nullptr), |
| 3585 | line_number(-1) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 3586 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3587 | // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses |
| 3588 | // annotalysis. |
| 3589 | bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3590 | ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3591 | if (!m->IsRuntimeMethod()) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3592 | ++stack_depth; |
| 3593 | if (method == nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3594 | mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3595 | method = m; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3596 | if (dex_cache != nullptr) { |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 3597 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3598 | line_number = dex_file.GetLineNumFromPC(m, GetDexPc()); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3599 | } |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3600 | } |
| 3601 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 3602 | return true; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3603 | } |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3604 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3605 | int stack_depth; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3606 | ArtMethod* method; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3607 | int32_t line_number; |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3608 | }; |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 3609 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3610 | Thread* const thread = sts.GetThread(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3611 | SingleStepStackVisitor visitor(thread); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 3612 | visitor.WalkStack(); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3613 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3614 | // 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] | 3615 | struct DebugCallbackContext { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3616 | explicit DebugCallbackContext(SingleStepControl* single_step_control_cb, |
| 3617 | int32_t line_number_cb, const DexFile::CodeItem* code_item) |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3618 | : single_step_control_(single_step_control_cb), line_number_(line_number_cb), |
| 3619 | code_item_(code_item), last_pc_valid(false), last_pc(0) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3620 | } |
| 3621 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3622 | 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] | 3623 | DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 3624 | if (static_cast<int32_t>(line_number_cb) == context->line_number_) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3625 | if (!context->last_pc_valid) { |
| 3626 | // Everything from this address until the next line change is ours. |
| 3627 | context->last_pc = address; |
| 3628 | context->last_pc_valid = true; |
| 3629 | } |
| 3630 | // Otherwise, if we're already in a valid range for this line, |
| 3631 | // just keep going (shouldn't really happen)... |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3632 | } else if (context->last_pc_valid) { // and the line number is new |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3633 | // Add everything from the last entry up until here to the set |
| 3634 | 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] | 3635 | context->single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3636 | } |
| 3637 | context->last_pc_valid = false; |
| 3638 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3639 | return false; // There may be multiple entries for any given line. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3640 | } |
| 3641 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3642 | ~DebugCallbackContext() { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3643 | // If the line number was the last in the position table... |
| 3644 | if (last_pc_valid) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3645 | size_t end = code_item_->insns_size_in_code_units_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3646 | for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3647 | single_step_control_->AddDexPc(dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3648 | } |
| 3649 | } |
| 3650 | } |
| 3651 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3652 | SingleStepControl* const single_step_control_; |
| 3653 | const int32_t line_number_; |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3654 | const DexFile::CodeItem* const code_item_; |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3655 | bool last_pc_valid; |
| 3656 | uint32_t last_pc; |
| 3657 | }; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3658 | |
| 3659 | // Allocate single step. |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3660 | SingleStepControl* single_step_control = |
| 3661 | new (std::nothrow) SingleStepControl(step_size, step_depth, |
| 3662 | visitor.stack_depth, visitor.method); |
| 3663 | if (single_step_control == nullptr) { |
| 3664 | LOG(ERROR) << "Failed to allocate SingleStepControl"; |
| 3665 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3666 | } |
| 3667 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3668 | ArtMethod* m = single_step_control->GetMethod(); |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3669 | const int32_t line_number = visitor.line_number; |
Sebastien Hertz | 52f5f93 | 2015-05-28 11:00:57 +0200 | [diff] [blame] | 3670 | // Note: if the thread is not running Java code (pure native thread), there is no "current" |
| 3671 | // method on the stack (and no line number either). |
| 3672 | if (m != nullptr && !m->IsNative()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3673 | const DexFile::CodeItem* const code_item = m->GetCodeItem(); |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3674 | DebugCallbackContext context(single_step_control, line_number, code_item); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3675 | m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3676 | DebugCallbackContext::Callback, nullptr, &context); |
Elliott Hughes | 3e2e1a2 | 2012-02-21 11:33:41 -0800 | [diff] [blame] | 3677 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3678 | |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3679 | // Activate single-step in the thread. |
| 3680 | thread->ActivateSingleStepControl(single_step_control); |
Elliott Hughes | 8696433 | 2012-02-15 19:37:42 -0800 | [diff] [blame] | 3681 | |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3682 | if (VLOG_IS_ON(jdwp)) { |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3683 | VLOG(jdwp) << "Single-step thread: " << *thread; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3684 | VLOG(jdwp) << "Single-step step size: " << single_step_control->GetStepSize(); |
| 3685 | VLOG(jdwp) << "Single-step step depth: " << single_step_control->GetStepDepth(); |
| 3686 | VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->GetMethod()); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3687 | VLOG(jdwp) << "Single-step current line: " << line_number; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3688 | VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->GetStackDepth(); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3689 | VLOG(jdwp) << "Single-step dex_pc values:"; |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3690 | for (uint32_t dex_pc : single_step_control->GetDexPcs()) { |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 3691 | VLOG(jdwp) << StringPrintf(" %#x", dex_pc); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 3692 | } |
| 3693 | } |
| 3694 | |
| 3695 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3696 | } |
| 3697 | |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3698 | void Dbg::UnconfigureStep(JDWP::ObjectId thread_id) { |
| 3699 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3700 | JDWP::JdwpError error; |
| 3701 | Thread* thread = DecodeThread(soa, thread_id, &error); |
Sebastien Hertz | 87118ed | 2013-11-26 17:57:18 +0100 | [diff] [blame] | 3702 | if (error == JDWP::ERR_NONE) { |
Sebastien Hertz | 597c4f0 | 2015-01-26 17:37:14 +0100 | [diff] [blame] | 3703 | thread->DeactivateSingleStepControl(); |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 3704 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3705 | } |
| 3706 | |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3707 | static char JdwpTagToShortyChar(JDWP::JdwpTag tag) { |
| 3708 | switch (tag) { |
| 3709 | default: |
| 3710 | LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag); |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 3711 | UNREACHABLE(); |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3712 | |
| 3713 | // Primitives. |
| 3714 | case JDWP::JT_BYTE: return 'B'; |
| 3715 | case JDWP::JT_CHAR: return 'C'; |
| 3716 | case JDWP::JT_FLOAT: return 'F'; |
| 3717 | case JDWP::JT_DOUBLE: return 'D'; |
| 3718 | case JDWP::JT_INT: return 'I'; |
| 3719 | case JDWP::JT_LONG: return 'J'; |
| 3720 | case JDWP::JT_SHORT: return 'S'; |
| 3721 | case JDWP::JT_VOID: return 'V'; |
| 3722 | case JDWP::JT_BOOLEAN: return 'Z'; |
| 3723 | |
| 3724 | // Reference types. |
| 3725 | case JDWP::JT_ARRAY: |
| 3726 | case JDWP::JT_OBJECT: |
| 3727 | case JDWP::JT_STRING: |
| 3728 | case JDWP::JT_THREAD: |
| 3729 | case JDWP::JT_THREAD_GROUP: |
| 3730 | case JDWP::JT_CLASS_LOADER: |
| 3731 | case JDWP::JT_CLASS_OBJECT: |
| 3732 | return 'L'; |
| 3733 | } |
| 3734 | } |
| 3735 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3736 | JDWP::JdwpError Dbg::PrepareInvokeMethod(uint32_t request_id, JDWP::ObjectId thread_id, |
| 3737 | JDWP::ObjectId object_id, JDWP::RefTypeId class_id, |
| 3738 | JDWP::MethodId method_id, uint32_t arg_count, |
| 3739 | uint64_t arg_values[], JDWP::JdwpTag* arg_types, |
| 3740 | uint32_t options) { |
| 3741 | Thread* const self = Thread::Current(); |
| 3742 | CHECK_EQ(self, GetDebugThread()) << "This must be called by the JDWP thread"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3743 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3744 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3745 | Thread* targetThread = nullptr; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3746 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3747 | ScopedObjectAccessUnchecked soa(self); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3748 | JDWP::JdwpError error; |
| 3749 | targetThread = DecodeThread(soa, thread_id, &error); |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 3750 | if (error != JDWP::ERR_NONE) { |
| 3751 | LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id; |
| 3752 | return error; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3753 | } |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3754 | if (targetThread->GetInvokeReq() != nullptr) { |
| 3755 | // Thread is already invoking a method on behalf of the debugger. |
| 3756 | LOG(ERROR) << "InvokeMethod request for thread already invoking a method: " << *targetThread; |
| 3757 | return JDWP::ERR_ALREADY_INVOKING; |
| 3758 | } |
| 3759 | if (!targetThread->IsReadyForDebugInvoke()) { |
| 3760 | // 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] | 3761 | LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread; |
| 3762 | return JDWP::ERR_INVALID_THREAD; |
| 3763 | } |
| 3764 | |
| 3765 | /* |
| 3766 | * We currently have a bug where we don't successfully resume the |
| 3767 | * target thread if the suspend count is too deep. We're expected to |
| 3768 | * require one "resume" for each "suspend", but when asked to execute |
| 3769 | * a method we have to resume fully and then re-suspend it back to the |
| 3770 | * same level. (The easiest way to cause this is to type "suspend" |
| 3771 | * multiple times in jdb.) |
| 3772 | * |
| 3773 | * It's unclear what this means when the event specifies "resume all" |
| 3774 | * and some threads are suspended more deeply than others. This is |
| 3775 | * a rare problem, so for now we just prevent it from hanging forever |
| 3776 | * by rejecting the method invocation request. Without this, we will |
| 3777 | * be stuck waiting on a suspended thread. |
| 3778 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3779 | int suspend_count; |
| 3780 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3781 | MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3782 | suspend_count = targetThread->GetSuspendCount(); |
| 3783 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3784 | if (suspend_count > 1) { |
| 3785 | LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3786 | return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here. |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3787 | } |
| 3788 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3789 | mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id, &error); |
| 3790 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3791 | return JDWP::ERR_INVALID_OBJECT; |
| 3792 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3793 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3794 | gRegistry->Get<mirror::Object*>(thread_id, &error); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3795 | if (error != JDWP::ERR_NONE) { |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3796 | return JDWP::ERR_INVALID_OBJECT; |
| 3797 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3798 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3799 | mirror::Class* c = DecodeClass(class_id, &error); |
| 3800 | if (c == nullptr) { |
| 3801 | return error; |
Elliott Hughes | 3f4d58f | 2012-02-18 20:05:37 -0800 | [diff] [blame] | 3802 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3803 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3804 | ArtMethod* m = FromMethodId(method_id); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3805 | if (m->IsStatic() != (receiver == nullptr)) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3806 | return JDWP::ERR_INVALID_METHODID; |
| 3807 | } |
| 3808 | if (m->IsStatic()) { |
| 3809 | if (m->GetDeclaringClass() != c) { |
| 3810 | return JDWP::ERR_INVALID_METHODID; |
| 3811 | } |
| 3812 | } else { |
| 3813 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
| 3814 | return JDWP::ERR_INVALID_METHODID; |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | // Check the argument list matches the method. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3819 | uint32_t shorty_len = 0; |
| 3820 | const char* shorty = m->GetShorty(&shorty_len); |
| 3821 | if (shorty_len - 1 != arg_count) { |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3822 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3823 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3824 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3825 | { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3826 | StackHandleScope<2> hs(soa.Self()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3827 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver)); |
| 3828 | HandleWrapper<mirror::Class> h_klass(hs.NewHandleWrapper(&c)); |
| 3829 | const DexFile::TypeList* types = m->GetParameterTypeList(); |
| 3830 | for (size_t i = 0; i < arg_count; ++i) { |
| 3831 | if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) { |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3832 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3833 | } |
| 3834 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3835 | if (shorty[i + 1] == 'L') { |
| 3836 | // Did we really get an argument of an appropriate reference type? |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 3837 | mirror::Class* parameter_type = |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3838 | m->GetClassFromTypeIndex(types->GetTypeItem(i).type_idx_, true); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3839 | mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i], &error); |
| 3840 | if (error != JDWP::ERR_NONE) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3841 | return JDWP::ERR_INVALID_OBJECT; |
| 3842 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 3843 | if (argument != nullptr && !argument->InstanceOf(parameter_type)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3844 | return JDWP::ERR_ILLEGAL_ARGUMENT; |
| 3845 | } |
| 3846 | |
| 3847 | // Turn the on-the-wire ObjectId into a jobject. |
| 3848 | jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]); |
| 3849 | v.l = gRegistry->GetJObject(arg_values[i]); |
| 3850 | } |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 3851 | } |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3852 | } |
| 3853 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3854 | // Allocates a DebugInvokeReq. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3855 | DebugInvokeReq* req = new (std::nothrow) DebugInvokeReq(request_id, thread_id, receiver, c, m, |
| 3856 | options, arg_values, arg_count); |
| 3857 | if (req == nullptr) { |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3858 | LOG(ERROR) << "Failed to allocate DebugInvokeReq"; |
| 3859 | return JDWP::ERR_OUT_OF_MEMORY; |
| 3860 | } |
| 3861 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3862 | // Attaches the DebugInvokeReq to the target thread so it executes the method when |
| 3863 | // it is resumed. Once the invocation completes, the target thread will delete it before |
| 3864 | // suspending itself (see ThreadList::SuspendSelfForDebugger). |
| 3865 | targetThread->SetDebugInvokeReq(req); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | // The fact that we've released the thread list lock is a bit risky --- if the thread goes |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3869 | // away we're sitting high and dry -- but we must release this before the UndoDebuggerSuspensions |
| 3870 | // call. |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3871 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3872 | if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3873 | VLOG(jdwp) << " Resuming all threads"; |
| 3874 | thread_list->UndoDebuggerSuspensions(); |
| 3875 | } else { |
| 3876 | VLOG(jdwp) << " Resuming event thread only"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3877 | thread_list->Resume(targetThread, true); |
| 3878 | } |
| 3879 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3880 | return JDWP::ERR_NONE; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | void Dbg::ExecuteMethod(DebugInvokeReq* pReq) { |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3884 | Thread* const self = Thread::Current(); |
| 3885 | CHECK_NE(self, GetDebugThread()) << "This must be called by the event thread"; |
| 3886 | |
| 3887 | ScopedObjectAccess soa(self); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3888 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 3889 | // We can be called while an exception is pending. We need |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3890 | // to preserve that across the method invocation. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3891 | StackHandleScope<1> hs(soa.Self()); |
| 3892 | Handle<mirror::Throwable> old_exception = hs.NewHandle(soa.Self()->GetException()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3893 | soa.Self()->ClearException(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3894 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3895 | // Execute the method then sends reply to the debugger. |
| 3896 | ExecuteMethodWithoutPendingException(soa, pReq); |
| 3897 | |
| 3898 | // If an exception was pending before the invoke, restore it now. |
| 3899 | if (old_exception.Get() != nullptr) { |
| 3900 | soa.Self()->SetException(old_exception.Get()); |
| 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | // Helper function: write a variable-width value into the output input buffer. |
| 3905 | static void WriteValue(JDWP::ExpandBuf* pReply, int width, uint64_t value) { |
| 3906 | switch (width) { |
| 3907 | case 1: |
| 3908 | expandBufAdd1(pReply, value); |
| 3909 | break; |
| 3910 | case 2: |
| 3911 | expandBufAdd2BE(pReply, value); |
| 3912 | break; |
| 3913 | case 4: |
| 3914 | expandBufAdd4BE(pReply, value); |
| 3915 | break; |
| 3916 | case 8: |
| 3917 | expandBufAdd8BE(pReply, value); |
| 3918 | break; |
| 3919 | default: |
| 3920 | LOG(FATAL) << width; |
| 3921 | UNREACHABLE(); |
| 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | void Dbg::ExecuteMethodWithoutPendingException(ScopedObjectAccess& soa, DebugInvokeReq* pReq) { |
| 3926 | soa.Self()->AssertNoPendingException(); |
| 3927 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3928 | // Translate the method through the vtable, unless the debugger wants to suppress it. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3929 | ArtMethod* m = pReq->method; |
| 3930 | size_t image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3931 | if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver.Read() != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3932 | ArtMethod* actual_method = |
| 3933 | pReq->klass.Read()->FindVirtualMethodForVirtualOrInterface(m, image_pointer_size); |
| 3934 | if (actual_method != m) { |
| 3935 | VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3936 | << " to " << PrettyMethod(actual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3937 | m = actual_method; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 3938 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3939 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3940 | VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m) |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3941 | << " receiver=" << pReq->receiver.Read() |
Sebastien Hertz | d38667a | 2013-11-25 15:43:54 +0100 | [diff] [blame] | 3942 | << " arg_count=" << pReq->arg_count; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3943 | CHECK(m != nullptr); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3944 | |
Roland Levillain | 33d6903 | 2015-06-18 18:20:59 +0100 | [diff] [blame^] | 3945 | static_assert(sizeof(jvalue) == sizeof(uint64_t), "jvalue and uint64_t have different sizes."); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3946 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3947 | // Invoke the method. |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 3948 | ScopedLocalRef<jobject> ref(soa.Env(), soa.AddLocalReference<jobject>(pReq->receiver.Read())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3949 | JValue result = InvokeWithJValues(soa, ref.get(), soa.EncodeMethod(m), |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3950 | reinterpret_cast<jvalue*>(pReq->arg_values.get())); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3951 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3952 | // Prepare JDWP ids for the reply. |
| 3953 | JDWP::JdwpTag result_tag = BasicTagFromDescriptor(m->GetShorty()); |
| 3954 | const bool is_object_result = (result_tag == JDWP::JT_OBJECT); |
| 3955 | StackHandleScope<2> hs(soa.Self()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3956 | Handle<mirror::Object> object_result = hs.NewHandle(is_object_result ? result.GetL() : nullptr); |
| 3957 | Handle<mirror::Throwable> exception = hs.NewHandle(soa.Self()->GetException()); |
| 3958 | soa.Self()->ClearException(); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3959 | |
| 3960 | if (!IsDebuggerActive()) { |
| 3961 | // The debugger detached: we must not re-suspend threads. We also don't need to fill the reply |
| 3962 | // because it won't be sent either. |
| 3963 | return; |
| 3964 | } |
| 3965 | |
| 3966 | JDWP::ObjectId exceptionObjectId = gRegistry->Add(exception); |
| 3967 | uint64_t result_value = 0; |
| 3968 | if (exceptionObjectId != 0) { |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3969 | VLOG(jdwp) << " JDWP invocation returning with exception=" << exception.Get() |
| 3970 | << " " << exception->Dump(); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3971 | result_value = 0; |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3972 | } else if (is_object_result) { |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3973 | /* if no exception was thrown, examine object result more closely */ |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3974 | JDWP::JdwpTag new_tag = TagFromObject(soa, object_result.Get()); |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3975 | if (new_tag != result_tag) { |
| 3976 | VLOG(jdwp) << " JDWP promoted result from " << result_tag << " to " << new_tag; |
| 3977 | result_tag = new_tag; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 3978 | } |
| 3979 | |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3980 | // Register the object in the registry and reference its ObjectId. This ensures |
| 3981 | // GC safety and prevents from accessing stale reference if the object is moved. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3982 | result_value = gRegistry->Add(object_result.Get()); |
Sebastien Hertz | 1558b57 | 2015-02-25 15:05:59 +0100 | [diff] [blame] | 3983 | } else { |
| 3984 | // Primitive result. |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 3985 | DCHECK(IsPrimitiveTag(result_tag)); |
| 3986 | result_value = result.GetJ(); |
| 3987 | } |
| 3988 | const bool is_constructor = m->IsConstructor() && !m->IsStatic(); |
| 3989 | if (is_constructor) { |
| 3990 | // If we invoked a constructor (which actually returns void), return the receiver, |
| 3991 | // unless we threw, in which case we return null. |
| 3992 | result_tag = JDWP::JT_OBJECT; |
| 3993 | if (exceptionObjectId == 0) { |
| 3994 | // TODO we could keep the receiver ObjectId in the DebugInvokeReq to avoid looking into the |
| 3995 | // object registry. |
| 3996 | result_value = GetObjectRegistry()->Add(pReq->receiver.Read()); |
| 3997 | } else { |
| 3998 | result_value = 0; |
| 3999 | } |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4000 | } |
| 4001 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 4002 | // Suspend other threads if the invoke is not single-threaded. |
| 4003 | if ((pReq->options & JDWP::INVOKE_SINGLE_THREADED) == 0) { |
| 4004 | soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension); |
| 4005 | VLOG(jdwp) << " Suspending all threads"; |
| 4006 | Runtime::Current()->GetThreadList()->SuspendAllForDebugger(); |
| 4007 | soa.Self()->TransitionFromSuspendedToRunnable(); |
| 4008 | } |
| 4009 | |
| 4010 | VLOG(jdwp) << " --> returned " << result_tag |
| 4011 | << StringPrintf(" %#" PRIx64 " (except=%#" PRIx64 ")", result_value, |
| 4012 | exceptionObjectId); |
| 4013 | |
| 4014 | // Show detailed debug output. |
| 4015 | if (result_tag == JDWP::JT_STRING && exceptionObjectId == 0) { |
| 4016 | if (result_value != 0) { |
| 4017 | if (VLOG_IS_ON(jdwp)) { |
| 4018 | std::string result_string; |
| 4019 | JDWP::JdwpError error = Dbg::StringToUtf8(result_value, &result_string); |
| 4020 | CHECK_EQ(error, JDWP::ERR_NONE); |
| 4021 | VLOG(jdwp) << " string '" << result_string << "'"; |
| 4022 | } |
| 4023 | } else { |
| 4024 | VLOG(jdwp) << " string (null)"; |
| 4025 | } |
| 4026 | } |
| 4027 | |
| 4028 | // Attach the reply to DebugInvokeReq so it can be sent to the debugger when the event thread |
| 4029 | // is ready to suspend. |
| 4030 | BuildInvokeReply(pReq->reply, pReq->request_id, result_tag, result_value, exceptionObjectId); |
| 4031 | } |
| 4032 | |
| 4033 | void Dbg::BuildInvokeReply(JDWP::ExpandBuf* pReply, uint32_t request_id, JDWP::JdwpTag result_tag, |
| 4034 | uint64_t result_value, JDWP::ObjectId exception) { |
| 4035 | // Make room for the JDWP header since we do not know the size of the reply yet. |
| 4036 | JDWP::expandBufAddSpace(pReply, kJDWPHeaderLen); |
| 4037 | |
| 4038 | size_t width = GetTagWidth(result_tag); |
| 4039 | JDWP::expandBufAdd1(pReply, result_tag); |
| 4040 | if (width != 0) { |
| 4041 | WriteValue(pReply, width, result_value); |
| 4042 | } |
| 4043 | JDWP::expandBufAdd1(pReply, JDWP::JT_OBJECT); |
| 4044 | JDWP::expandBufAddObjectId(pReply, exception); |
| 4045 | |
| 4046 | // Now we know the size, we can complete the JDWP header. |
| 4047 | uint8_t* buf = expandBufGetBuffer(pReply); |
| 4048 | JDWP::Set4BE(buf + kJDWPHeaderSizeOffset, expandBufGetLength(pReply)); |
| 4049 | JDWP::Set4BE(buf + kJDWPHeaderIdOffset, request_id); |
| 4050 | JDWP::Set1(buf + kJDWPHeaderFlagsOffset, kJDWPFlagReply); // flags |
| 4051 | JDWP::Set2BE(buf + kJDWPHeaderErrorCodeOffset, JDWP::ERR_NONE); |
| 4052 | } |
| 4053 | |
| 4054 | void Dbg::FinishInvokeMethod(DebugInvokeReq* pReq) { |
| 4055 | CHECK_NE(Thread::Current(), GetDebugThread()) << "This must be called by the event thread"; |
| 4056 | |
| 4057 | JDWP::ExpandBuf* const pReply = pReq->reply; |
| 4058 | CHECK(pReply != nullptr) << "No reply attached to DebugInvokeReq"; |
| 4059 | |
| 4060 | // We need to prevent other threads (including JDWP thread) from interacting with the debugger |
| 4061 | // while we send the reply but are not yet suspended. The JDWP token will be released just before |
| 4062 | // we suspend ourself again (see ThreadList::SuspendSelfForDebugger). |
| 4063 | gJdwpState->AcquireJdwpTokenForEvent(pReq->thread_id); |
| 4064 | |
| 4065 | // Send the reply unless the debugger detached before the completion of the method. |
| 4066 | if (IsDebuggerActive()) { |
| 4067 | const size_t replyDataLength = expandBufGetLength(pReply) - kJDWPHeaderLen; |
| 4068 | VLOG(jdwp) << StringPrintf("REPLY INVOKE id=0x%06x (length=%zu)", |
| 4069 | pReq->request_id, replyDataLength); |
| 4070 | |
| 4071 | gJdwpState->SendRequest(pReply); |
| 4072 | } else { |
| 4073 | VLOG(jdwp) << "Not sending invoke reply because debugger detached"; |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4074 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4075 | } |
| 4076 | |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 4077 | /* |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4078 | * "request" contains a full JDWP packet, possibly with multiple chunks. We |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4079 | * need to process each, accumulate the replies, and ship the whole thing |
| 4080 | * back. |
| 4081 | * |
| 4082 | * Returns "true" if we have a reply. The reply buffer is newly allocated, |
| 4083 | * and includes the chunk type/length, followed by the data. |
| 4084 | * |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 4085 | * 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] | 4086 | * chunk. If this becomes inconvenient we will need to adapt. |
| 4087 | */ |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4088 | bool Dbg::DdmHandlePacket(JDWP::Request* request, uint8_t** pReplyBuf, int* pReplyLen) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4089 | Thread* self = Thread::Current(); |
| 4090 | JNIEnv* env = self->GetJniEnv(); |
| 4091 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4092 | uint32_t type = request->ReadUnsigned32("type"); |
| 4093 | uint32_t length = request->ReadUnsigned32("length"); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4094 | |
| 4095 | // Create a byte[] corresponding to 'request'. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4096 | size_t request_length = request->size(); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4097 | ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4098 | if (dataArray.get() == nullptr) { |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4099 | LOG(WARNING) << "byte[] allocation failed: " << request_length; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4100 | env->ExceptionClear(); |
| 4101 | return false; |
| 4102 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4103 | env->SetByteArrayRegion(dataArray.get(), 0, request_length, |
| 4104 | reinterpret_cast<const jbyte*>(request->data())); |
| 4105 | request->Skip(request_length); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4106 | |
| 4107 | // Run through and find all chunks. [Currently just find the first.] |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4108 | ScopedByteArrayRO contents(env, dataArray.get()); |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4109 | if (length != request_length) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4110 | 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] | 4111 | return false; |
| 4112 | } |
| 4113 | |
| 4114 | // 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] | 4115 | ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4116 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch, |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4117 | type, dataArray.get(), 0, length)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4118 | if (env->ExceptionCheck()) { |
| 4119 | LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type); |
| 4120 | env->ExceptionDescribe(); |
| 4121 | env->ExceptionClear(); |
| 4122 | return false; |
| 4123 | } |
| 4124 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4125 | if (chunk.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4126 | return false; |
| 4127 | } |
| 4128 | |
| 4129 | /* |
| 4130 | * Pull the pieces out of the chunk. We copy the results into a |
| 4131 | * newly-allocated buffer that the caller can free. We don't want to |
| 4132 | * continue using the Chunk object because nothing has a reference to it. |
| 4133 | * |
| 4134 | * We could avoid this by returning type/data/offset/length and having |
| 4135 | * the caller be aware of the object lifetime issues, but that |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 4136 | * 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] | 4137 | * if we have responses for multiple chunks. |
| 4138 | * |
| 4139 | * So we're pretty much stuck with copying data around multiple times. |
| 4140 | */ |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4141 | 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] | 4142 | 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] | 4143 | 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] | 4144 | 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] | 4145 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4146 | 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] | 4147 | if (length == 0 || replyData.get() == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4148 | return false; |
| 4149 | } |
| 4150 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4151 | const int kChunkHdrLen = 8; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4152 | uint8_t* reply = new uint8_t[length + kChunkHdrLen]; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4153 | if (reply == nullptr) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4154 | LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen); |
| 4155 | return false; |
| 4156 | } |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 4157 | JDWP::Set4BE(reply + 0, type); |
| 4158 | JDWP::Set4BE(reply + 4, length); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4159 | env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 4160 | |
| 4161 | *pReplyBuf = reply; |
| 4162 | *pReplyLen = length + kChunkHdrLen; |
| 4163 | |
Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame] | 4164 | 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] | 4165 | return true; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4166 | } |
| 4167 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4168 | void Dbg::DdmBroadcast(bool connect) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4169 | VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "..."; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4170 | |
| 4171 | Thread* self = Thread::Current(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4172 | if (self->GetState() != kRunnable) { |
| 4173 | LOG(ERROR) << "DDM broadcast in thread state " << self->GetState(); |
| 4174 | /* try anyway? */ |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4178 | jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 4179 | env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer, |
| 4180 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast, |
| 4181 | event); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4182 | if (env->ExceptionCheck()) { |
| 4183 | LOG(ERROR) << "DdmServer.broadcast " << event << " failed"; |
| 4184 | env->ExceptionDescribe(); |
| 4185 | env->ExceptionClear(); |
| 4186 | } |
| 4187 | } |
| 4188 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4189 | void Dbg::DdmConnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4190 | Dbg::DdmBroadcast(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | void Dbg::DdmDisconnected() { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4194 | Dbg::DdmBroadcast(false); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4195 | gDdmThreadNotification = false; |
| 4196 | } |
| 4197 | |
| 4198 | /* |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4199 | * Send a notification when a thread starts, stops, or changes its name. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4200 | * |
| 4201 | * Because we broadcast the full set of threads when the notifications are |
| 4202 | * first enabled, it's possible for "thread" to be actively executing. |
| 4203 | */ |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4204 | void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4205 | if (!gDdmThreadNotification) { |
| 4206 | return; |
| 4207 | } |
| 4208 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4209 | if (type == CHUNK_TYPE("THDE")) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4210 | uint8_t buf[4]; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4211 | JDWP::Set4BE(&buf[0], t->GetThreadId()); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4212 | Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4213 | } else { |
| 4214 | CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4215 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4216 | StackHandleScope<1> hs(soa.Self()); |
| 4217 | Handle<mirror::String> name(hs.NewHandle(t->GetThreadName(soa))); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4218 | size_t char_count = (name.Get() != nullptr) ? name->GetLength() : 0; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 4219 | const jchar* chars = (name.Get() != nullptr) ? name->GetValue() : nullptr; |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4220 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4221 | std::vector<uint8_t> bytes; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 4222 | JDWP::Append4BE(bytes, t->GetThreadId()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4223 | JDWP::AppendUtf16BE(bytes, chars, char_count); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4224 | CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2); |
| 4225 | Dbg::DdmSendChunk(type, bytes); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4226 | } |
| 4227 | } |
| 4228 | |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4229 | void Dbg::DdmSetThreadNotification(bool enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4230 | // Enable/disable thread notifications. |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4231 | gDdmThreadNotification = enable; |
| 4232 | if (enable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4233 | // Suspend the VM then post thread start notifications for all threads. Threads attaching will |
| 4234 | // see a suspension in progress and block until that ends. They then post their own start |
| 4235 | // notification. |
| 4236 | SuspendVM(); |
| 4237 | std::list<Thread*> threads; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4238 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4239 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4240 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4241 | threads = Runtime::Current()->GetThreadList()->GetList(); |
| 4242 | } |
| 4243 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4244 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4245 | for (Thread* thread : threads) { |
| 4246 | Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR")); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4247 | } |
| 4248 | } |
| 4249 | ResumeVM(); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4250 | } |
| 4251 | } |
| 4252 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4253 | void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 4254 | if (IsDebuggerActive()) { |
Sebastien Hertz | 6995c60 | 2014-09-09 12:10:13 +0200 | [diff] [blame] | 4255 | gJdwpState->PostThreadChange(t, type == CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4256 | } |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4257 | Dbg::DdmSendThreadNotification(t, type); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | void Dbg::PostThreadStart(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4261 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR")); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | void Dbg::PostThreadDeath(Thread* t) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4265 | Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE")); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4266 | } |
| 4267 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 4268 | 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] | 4269 | CHECK(buf != nullptr); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4270 | iovec vec[1]; |
| 4271 | vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf)); |
| 4272 | vec[0].iov_len = byte_count; |
| 4273 | Dbg::DdmSendChunkV(type, vec, 1); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4274 | } |
| 4275 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4276 | void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) { |
| 4277 | DdmSendChunk(type, bytes.size(), &bytes[0]); |
| 4278 | } |
| 4279 | |
Brian Carlstrom | f529352 | 2013-07-19 00:24:00 -0700 | [diff] [blame] | 4280 | void Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4281 | if (gJdwpState == nullptr) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4282 | VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4283 | } else { |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 4284 | gJdwpState->DdmSendChunkV(type, iov, iov_count); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 4285 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4286 | } |
| 4287 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 4288 | JDWP::JdwpState* Dbg::GetJdwpState() { |
| 4289 | return gJdwpState; |
| 4290 | } |
| 4291 | |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4292 | int Dbg::DdmHandleHpifChunk(HpifWhen when) { |
| 4293 | if (when == HPIF_WHEN_NOW) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4294 | DdmSendHeapInfo(when); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4295 | return true; |
| 4296 | } |
| 4297 | |
| 4298 | if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) { |
| 4299 | LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when); |
| 4300 | return false; |
| 4301 | } |
| 4302 | |
| 4303 | gDdmHpifWhen = when; |
| 4304 | return true; |
| 4305 | } |
| 4306 | |
| 4307 | bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) { |
| 4308 | if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) { |
| 4309 | LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when); |
| 4310 | return false; |
| 4311 | } |
| 4312 | |
| 4313 | if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) { |
| 4314 | LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what); |
| 4315 | return false; |
| 4316 | } |
| 4317 | |
| 4318 | if (native) { |
| 4319 | gDdmNhsgWhen = when; |
| 4320 | gDdmNhsgWhat = what; |
| 4321 | } else { |
| 4322 | gDdmHpsgWhen = when; |
| 4323 | gDdmHpsgWhat = what; |
| 4324 | } |
| 4325 | return true; |
| 4326 | } |
| 4327 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 4328 | void Dbg::DdmSendHeapInfo(HpifWhen reason) { |
| 4329 | // If there's a one-shot 'when', reset it. |
| 4330 | if (reason == gDdmHpifWhen) { |
| 4331 | if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) { |
| 4332 | gDdmHpifWhen = HPIF_WHEN_NEVER; |
| 4333 | } |
| 4334 | } |
| 4335 | |
| 4336 | /* |
| 4337 | * Chunk HPIF (client --> server) |
| 4338 | * |
| 4339 | * Heap Info. General information about the heap, |
| 4340 | * suitable for a summary display. |
| 4341 | * |
| 4342 | * [u4]: number of heaps |
| 4343 | * |
| 4344 | * For each heap: |
| 4345 | * [u4]: heap ID |
| 4346 | * [u8]: timestamp in ms since Unix epoch |
| 4347 | * [u1]: capture reason (same as 'when' value from server) |
| 4348 | * [u4]: max heap size in bytes (-Xmx) |
| 4349 | * [u4]: current heap size in bytes |
| 4350 | * [u4]: current number of bytes allocated |
| 4351 | * [u4]: current number of objects allocated |
| 4352 | */ |
| 4353 | uint8_t heap_count = 1; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4354 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4355 | std::vector<uint8_t> bytes; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4356 | JDWP::Append4BE(bytes, heap_count); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4357 | JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap). |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4358 | JDWP::Append8BE(bytes, MilliTime()); |
| 4359 | JDWP::Append1BE(bytes, reason); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4360 | JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes. |
| 4361 | JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4362 | JDWP::Append4BE(bytes, heap->GetBytesAllocated()); |
| 4363 | JDWP::Append4BE(bytes, heap->GetObjectsAllocated()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 4364 | CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4))); |
| 4365 | Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 4366 | } |
| 4367 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4368 | enum HpsgSolidity { |
| 4369 | SOLIDITY_FREE = 0, |
| 4370 | SOLIDITY_HARD = 1, |
| 4371 | SOLIDITY_SOFT = 2, |
| 4372 | SOLIDITY_WEAK = 3, |
| 4373 | SOLIDITY_PHANTOM = 4, |
| 4374 | SOLIDITY_FINALIZABLE = 5, |
| 4375 | SOLIDITY_SWEEP = 6, |
| 4376 | }; |
| 4377 | |
| 4378 | enum HpsgKind { |
| 4379 | KIND_OBJECT = 0, |
| 4380 | KIND_CLASS_OBJECT = 1, |
| 4381 | KIND_ARRAY_1 = 2, |
| 4382 | KIND_ARRAY_2 = 3, |
| 4383 | KIND_ARRAY_4 = 4, |
| 4384 | KIND_ARRAY_8 = 5, |
| 4385 | KIND_UNKNOWN = 6, |
| 4386 | KIND_NATIVE = 7, |
| 4387 | }; |
| 4388 | |
| 4389 | #define HPSG_PARTIAL (1<<7) |
| 4390 | #define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7))) |
| 4391 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4392 | class HeapChunkContext { |
| 4393 | public: |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4394 | // Maximum chunk size. Obtain this from the formula: |
| 4395 | // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2 |
| 4396 | HeapChunkContext(bool merge, bool native) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4397 | : buf_(16384 - 16), |
| 4398 | type_(0), |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4399 | chunk_overhead_(0) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4400 | Reset(); |
| 4401 | if (native) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4402 | type_ = CHUNK_TYPE("NHSG"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4403 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4404 | type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO"); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4405 | } |
| 4406 | } |
| 4407 | |
| 4408 | ~HeapChunkContext() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4409 | if (p_ > &buf_[0]) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4410 | Flush(); |
| 4411 | } |
| 4412 | } |
| 4413 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4414 | void SetChunkOverhead(size_t chunk_overhead) { |
| 4415 | chunk_overhead_ = chunk_overhead; |
| 4416 | } |
| 4417 | |
| 4418 | void ResetStartOfNextChunk() { |
| 4419 | startOfNextMemoryChunk_ = nullptr; |
| 4420 | } |
| 4421 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4422 | void EnsureHeader(const void* chunk_ptr) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4423 | if (!needHeader_) { |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4424 | return; |
| 4425 | } |
| 4426 | |
| 4427 | // Start a new HPSx chunk. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4428 | JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap). |
| 4429 | JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4430 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4431 | JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start. |
| 4432 | 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] | 4433 | // [u4]: length of piece, in allocation units |
| 4434 | // 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] | 4435 | pieceLenField_ = p_; |
| 4436 | JDWP::Write4BE(&p_, 0x55555555); |
| 4437 | needHeader_ = false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4438 | } |
| 4439 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4440 | void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4441 | if (pieceLenField_ == nullptr) { |
Ian Rogers | d636b06 | 2013-01-18 17:51:18 -0800 | [diff] [blame] | 4442 | // Flush immediately post Reset (maybe back-to-back Flush). Ignore. |
| 4443 | CHECK(needHeader_); |
| 4444 | return; |
| 4445 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4446 | // Patch the "length of piece" field. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4447 | CHECK_LE(&buf_[0], pieceLenField_); |
| 4448 | CHECK_LE(pieceLenField_, p_); |
| 4449 | JDWP::Set4BE(pieceLenField_, totalAllocationUnits_); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4450 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4451 | Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4452 | Reset(); |
| 4453 | } |
| 4454 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4455 | 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] | 4456 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 4457 | Locks::mutator_lock_) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4458 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkJavaCallback(start, end, used_bytes); |
| 4459 | } |
| 4460 | |
| 4461 | static void HeapChunkNativeCallback(void* start, void* end, size_t used_bytes, void* arg) |
| 4462 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4463 | reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkNativeCallback(start, end, used_bytes); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4464 | } |
| 4465 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4466 | private: |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4467 | enum { ALLOCATION_UNIT_SIZE = 8 }; |
| 4468 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4469 | void Reset() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4470 | p_ = &buf_[0]; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4471 | ResetStartOfNextChunk(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4472 | totalAllocationUnits_ = 0; |
| 4473 | needHeader_ = true; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4474 | pieceLenField_ = nullptr; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4475 | } |
| 4476 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4477 | bool IsNative() const { |
| 4478 | return type_ == CHUNK_TYPE("NHSG"); |
| 4479 | } |
| 4480 | |
| 4481 | // Returns true if the object is not an empty chunk. |
| 4482 | 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] | 4483 | // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken |
| 4484 | // 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] | 4485 | if (used_bytes == 0) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4486 | if (start == nullptr) { |
| 4487 | // Reset for start of new heap. |
| 4488 | startOfNextMemoryChunk_ = nullptr; |
| 4489 | Flush(); |
| 4490 | } |
| 4491 | // Only process in use memory so that free region information |
| 4492 | // also includes dlmalloc book keeping. |
| 4493 | return false; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4494 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4495 | if (startOfNextMemoryChunk_ != nullptr) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4496 | // Transmit any pending free memory. Native free memory of over kMaxFreeLen could be because |
| 4497 | // of the use of mmaps, so don't report. If not free memory then start a new segment. |
| 4498 | bool flush = true; |
| 4499 | if (start > startOfNextMemoryChunk_) { |
| 4500 | const size_t kMaxFreeLen = 2 * kPageSize; |
| 4501 | void* free_start = startOfNextMemoryChunk_; |
| 4502 | void* free_end = start; |
| 4503 | const size_t free_len = |
| 4504 | reinterpret_cast<uintptr_t>(free_end) - reinterpret_cast<uintptr_t>(free_start); |
| 4505 | if (!IsNative() || free_len < kMaxFreeLen) { |
| 4506 | AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), free_start, free_len, IsNative()); |
| 4507 | flush = false; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4508 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4509 | } |
| 4510 | if (flush) { |
| 4511 | startOfNextMemoryChunk_ = nullptr; |
| 4512 | Flush(); |
| 4513 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4514 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4515 | return true; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4516 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4517 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4518 | void HeapChunkNativeCallback(void* start, void* /*end*/, size_t used_bytes) |
| 4519 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4520 | if (ProcessRecord(start, used_bytes)) { |
| 4521 | uint8_t state = ExamineNativeObject(start); |
| 4522 | AppendChunk(state, start, used_bytes + chunk_overhead_, true /*is_native*/); |
| 4523 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4524 | } |
| 4525 | } |
| 4526 | |
| 4527 | void HeapChunkJavaCallback(void* start, void* /*end*/, size_t used_bytes) |
| 4528 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 4529 | if (ProcessRecord(start, used_bytes)) { |
| 4530 | // Determine the type of this chunk. |
| 4531 | // OLD-TODO: if context.merge, see if this chunk is different from the last chunk. |
| 4532 | // If it's the same, we should combine them. |
| 4533 | uint8_t state = ExamineJavaObject(reinterpret_cast<mirror::Object*>(start)); |
| 4534 | AppendChunk(state, start, used_bytes + chunk_overhead_, false /*is_native*/); |
| 4535 | startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_; |
| 4536 | } |
| 4537 | } |
| 4538 | |
| 4539 | 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] | 4540 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4541 | // Make sure there's enough room left in the buffer. |
| 4542 | // We need to use two bytes for every fractional 256 allocation units used by the chunk plus |
| 4543 | // 17 bytes for any header. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4544 | const size_t needed = ((RoundUp(length / ALLOCATION_UNIT_SIZE, 256) / 256) * 2) + 17; |
| 4545 | size_t byte_left = &buf_.back() - p_; |
| 4546 | if (byte_left < needed) { |
| 4547 | if (is_native) { |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4548 | // Cannot trigger memory allocation while walking native heap. |
Pavel Vyssotski | 7522c74 | 2014-12-08 13:38:26 +0600 | [diff] [blame] | 4549 | return; |
| 4550 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4551 | Flush(); |
| 4552 | } |
| 4553 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4554 | byte_left = &buf_.back() - p_; |
| 4555 | if (byte_left < needed) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4556 | LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", " |
| 4557 | << needed << " bytes)"; |
| 4558 | return; |
| 4559 | } |
| 4560 | EnsureHeader(ptr); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4561 | // Write out the chunk description. |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4562 | length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units. |
| 4563 | totalAllocationUnits_ += length; |
| 4564 | while (length > 256) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4565 | *p_++ = state | HPSG_PARTIAL; |
| 4566 | *p_++ = 255; // length - 1 |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4567 | length -= 256; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4568 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4569 | *p_++ = state; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4570 | *p_++ = length - 1; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4571 | } |
| 4572 | |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4573 | uint8_t ExamineNativeObject(const void* p) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4574 | return p == nullptr ? HPSG_STATE(SOLIDITY_FREE, 0) : HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4575 | } |
| 4576 | |
| 4577 | uint8_t ExamineJavaObject(mirror::Object* o) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4578 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4579 | if (o == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4580 | return HPSG_STATE(SOLIDITY_FREE, 0); |
| 4581 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4582 | // It's an allocated chunk. Figure out what it is. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4583 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 4584 | if (!heap->IsLiveObjectLocked(o)) { |
| 4585 | LOG(ERROR) << "Invalid object in managed heap: " << o; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4586 | return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE); |
| 4587 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4588 | mirror::Class* c = o->GetClass(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4589 | if (c == nullptr) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4590 | // The object was probably just created but hasn't been initialized yet. |
| 4591 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4592 | } |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4593 | if (!heap->IsValidObjectAddress(c)) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4594 | LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c; |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4595 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4596 | } |
Mathieu Chartier | f26e1b3 | 2015-01-29 10:47:10 -0800 | [diff] [blame] | 4597 | if (c->GetClass() == nullptr) { |
| 4598 | LOG(ERROR) << "Null class of class " << c << " for object " << o; |
| 4599 | return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN); |
| 4600 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4601 | if (c->IsClassClass()) { |
| 4602 | return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT); |
| 4603 | } |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4604 | if (c->IsArrayClass()) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4605 | switch (c->GetComponentSize()) { |
| 4606 | case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1); |
| 4607 | case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2); |
| 4608 | case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4); |
| 4609 | case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8); |
| 4610 | } |
| 4611 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4612 | return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT); |
| 4613 | } |
| 4614 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4615 | std::vector<uint8_t> buf_; |
| 4616 | uint8_t* p_; |
| 4617 | uint8_t* pieceLenField_; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 4618 | void* startOfNextMemoryChunk_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4619 | size_t totalAllocationUnits_; |
| 4620 | uint32_t type_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4621 | bool needHeader_; |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4622 | size_t chunk_overhead_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 4623 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4624 | DISALLOW_COPY_AND_ASSIGN(HeapChunkContext); |
| 4625 | }; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4626 | |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4627 | static void BumpPointerSpaceCallback(mirror::Object* obj, void* arg) |
| 4628 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 4629 | const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4630 | HeapChunkContext::HeapChunkJavaCallback( |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4631 | obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, arg); |
| 4632 | } |
| 4633 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4634 | void Dbg::DdmSendHeapSegments(bool native) { |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4635 | Dbg::HpsgWhen when = native ? gDdmNhsgWhen : gDdmHpsgWhen; |
| 4636 | Dbg::HpsgWhat what = native ? gDdmNhsgWhat : gDdmHpsgWhat; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4637 | if (when == HPSG_WHEN_NEVER) { |
| 4638 | return; |
| 4639 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4640 | // Figure out what kind of chunks we'll be sending. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4641 | CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) |
| 4642 | << static_cast<int>(what); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4643 | |
| 4644 | // First, send a heap start chunk. |
| 4645 | uint8_t heap_id[4]; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4646 | 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] | 4647 | 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] | 4648 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4649 | Locks::mutator_lock_->AssertSharedHeld(self); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 4650 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4651 | // Send a series of heap segment chunks. |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4652 | HeapChunkContext context(what == HPSG_WHAT_MERGED_OBJECTS, native); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4653 | if (native) { |
Ian Rogers | 872dd82 | 2014-10-30 11:19:14 -0700 | [diff] [blame] | 4654 | #if defined(HAVE_ANDROID_OS) && defined(USE_DLMALLOC) |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4655 | dlmalloc_inspect_all(HeapChunkContext::HeapChunkNativeCallback, &context); |
| 4656 | HeapChunkContext::HeapChunkNativeCallback(nullptr, nullptr, 0, &context); // Indicate end of a space. |
Christopher Ferris | c4ddc04 | 2014-05-13 14:47:50 -0700 | [diff] [blame] | 4657 | #else |
| 4658 | UNIMPLEMENTED(WARNING) << "Native heap inspection is only supported with dlmalloc"; |
| 4659 | #endif |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4660 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 4661 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4662 | for (const auto& space : heap->GetContinuousSpaces()) { |
| 4663 | if (space->IsDlMallocSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4664 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4665 | // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an |
| 4666 | // allocation then the first sizeof(size_t) may belong to it. |
| 4667 | context.SetChunkOverhead(sizeof(size_t)); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4668 | space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4669 | } else if (space->IsRosAllocSpace()) { |
| 4670 | context.SetChunkOverhead(0); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4671 | // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since |
| 4672 | // RosAlloc's internal logic doesn't know to release and reacquire the heap bitmap lock. |
| 4673 | self->TransitionFromRunnableToSuspended(kSuspended); |
| 4674 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 4675 | tl->SuspendAll(__FUNCTION__); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4676 | { |
| 4677 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4678 | space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4679 | } |
| 4680 | tl->ResumeAll(); |
| 4681 | self->TransitionFromSuspendedToRunnable(); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4682 | } else if (space->IsBumpPointerSpace()) { |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4683 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4684 | context.SetChunkOverhead(0); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4685 | space->AsBumpPointerSpace()->Walk(BumpPointerSpaceCallback, &context); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4686 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4687 | } else if (space->IsRegionSpace()) { |
| 4688 | heap->IncrementDisableMovingGC(self); |
| 4689 | self->TransitionFromRunnableToSuspended(kSuspended); |
| 4690 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 4691 | tl->SuspendAll(__FUNCTION__); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 4692 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 4693 | context.SetChunkOverhead(0); |
| 4694 | space->AsRegionSpace()->Walk(BumpPointerSpaceCallback, &context); |
| 4695 | HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context); |
| 4696 | tl->ResumeAll(); |
| 4697 | self->TransitionFromSuspendedToRunnable(); |
| 4698 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4699 | } else { |
| 4700 | UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4701 | } |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4702 | context.ResetStartOfNextChunk(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 4703 | } |
Mathieu Chartier | 4c69d7f | 2014-10-10 12:45:50 -0700 | [diff] [blame] | 4704 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 4705 | // Walk the large objects, these are not in the AllocSpace. |
Mathieu Chartier | 36dab36 | 2014-07-30 14:59:56 -0700 | [diff] [blame] | 4706 | context.SetChunkOverhead(0); |
Mathieu Chartier | bc689b7 | 2014-12-14 17:01:31 -0800 | [diff] [blame] | 4707 | heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 4708 | } |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 4709 | |
| 4710 | // Finally, send a heap end chunk. |
| 4711 | 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] | 4712 | } |
| 4713 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4714 | void Dbg::SetAllocTrackingEnabled(bool enable) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4715 | gc::AllocRecordObjectMap::SetAllocTrackingEnabled(enable); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4716 | } |
| 4717 | |
| 4718 | void Dbg::DumpRecentAllocations() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4719 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4720 | MutexLock mu(soa.Self(), *Locks::alloc_tracker_lock_); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4721 | if (!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4722 | LOG(INFO) << "Not recording tracked allocations"; |
| 4723 | return; |
| 4724 | } |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4725 | gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords(); |
| 4726 | CHECK(records != nullptr); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4727 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4728 | const uint16_t capped_count = CappedAllocRecordCount(records->Size()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4729 | uint16_t count = capped_count; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4730 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4731 | LOG(INFO) << "Tracked allocations, (count=" << count << ")"; |
| 4732 | for (auto it = records->RBegin(), end = records->REnd(); |
| 4733 | count > 0 && it != end; count--, it++) { |
| 4734 | const gc::AllocRecord* record = it->second; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4735 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4736 | LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->GetTid(), record->ByteCount()) |
| 4737 | << PrettyClass(it->first.Read()->GetClass()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4738 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4739 | for (size_t stack_frame = 0, depth = record->GetDepth(); stack_frame < depth; ++stack_frame) { |
| 4740 | const gc::AllocRecordStackTraceElement& stack_element = record->StackElement(stack_frame); |
| 4741 | ArtMethod* m = stack_element.GetMethod(); |
| 4742 | LOG(INFO) << " " << PrettyMethod(m) << " line " << stack_element.ComputeLineNumber(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4743 | } |
| 4744 | |
| 4745 | // pause periodically to help logcat catch up |
| 4746 | if ((count % 5) == 0) { |
| 4747 | usleep(40000); |
| 4748 | } |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4749 | } |
| 4750 | } |
| 4751 | |
| 4752 | class StringTable { |
| 4753 | public: |
| 4754 | StringTable() { |
| 4755 | } |
| 4756 | |
Mathieu Chartier | 4345c46 | 2014-06-27 10:20:14 -0700 | [diff] [blame] | 4757 | void Add(const std::string& str) { |
| 4758 | table_.insert(str); |
| 4759 | } |
| 4760 | |
| 4761 | void Add(const char* str) { |
| 4762 | table_.insert(str); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4763 | } |
| 4764 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4765 | size_t IndexOf(const char* s) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4766 | auto it = table_.find(s); |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4767 | if (it == table_.end()) { |
| 4768 | LOG(FATAL) << "IndexOf(\"" << s << "\") failed"; |
| 4769 | } |
| 4770 | return std::distance(table_.begin(), it); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4771 | } |
| 4772 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4773 | size_t Size() const { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4774 | return table_.size(); |
| 4775 | } |
| 4776 | |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4777 | void WriteTo(std::vector<uint8_t>& bytes) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4778 | for (const std::string& str : table_) { |
| 4779 | const char* s = str.c_str(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4780 | size_t s_len = CountModifiedUtf8Chars(s); |
Christopher Ferris | 8a35405 | 2015-04-24 17:23:53 -0700 | [diff] [blame] | 4781 | std::unique_ptr<uint16_t[]> s_utf16(new uint16_t[s_len]); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4782 | ConvertModifiedUtf8ToUtf16(s_utf16.get(), s); |
| 4783 | JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4784 | } |
| 4785 | } |
| 4786 | |
| 4787 | private: |
Elliott Hughes | a8f93cb | 2012-06-08 17:08:48 -0700 | [diff] [blame] | 4788 | std::set<std::string> table_; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4789 | DISALLOW_COPY_AND_ASSIGN(StringTable); |
| 4790 | }; |
| 4791 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4792 | static const char* GetMethodSourceFile(ArtMethod* method) |
Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 4793 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4794 | DCHECK(method != nullptr); |
| 4795 | const char* source_file = method->GetDeclaringClassSourceFile(); |
Sebastien Hertz | 280286a | 2014-04-28 09:26:50 +0200 | [diff] [blame] | 4796 | return (source_file != nullptr) ? source_file : ""; |
| 4797 | } |
| 4798 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4799 | /* |
| 4800 | * The data we send to DDMS contains everything we have recorded. |
| 4801 | * |
| 4802 | * Message header (all values big-endian): |
| 4803 | * (1b) message header len (to allow future expansion); includes itself |
| 4804 | * (1b) entry header len |
| 4805 | * (1b) stack frame len |
| 4806 | * (2b) number of entries |
| 4807 | * (4b) offset to string table from start of message |
| 4808 | * (2b) number of class name strings |
| 4809 | * (2b) number of method name strings |
| 4810 | * (2b) number of source file name strings |
| 4811 | * For each entry: |
| 4812 | * (4b) total allocation size |
Elliott Hughes | 221229c | 2013-01-08 18:17:50 -0800 | [diff] [blame] | 4813 | * (2b) thread id |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4814 | * (2b) allocated object's class name index |
| 4815 | * (1b) stack depth |
| 4816 | * For each stack frame: |
| 4817 | * (2b) method's class name |
| 4818 | * (2b) method name |
| 4819 | * (2b) method source file |
| 4820 | * (2b) line number, clipped to 32767; -2 if native; -1 if no source |
| 4821 | * (xb) class name strings |
| 4822 | * (xb) method name strings |
| 4823 | * (xb) source file strings |
| 4824 | * |
| 4825 | * As with other DDM traffic, strings are sent as a 4-byte length |
| 4826 | * followed by UTF-16 data. |
| 4827 | * |
| 4828 | * 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] | 4829 | * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4830 | * each table, but in practice there should be far fewer. |
| 4831 | * |
| 4832 | * The chief reason for using a string table here is to keep the size of |
| 4833 | * the DDMS message to a minimum. This is partly to make the protocol |
| 4834 | * efficient, but also because we have to form the whole thing up all at |
| 4835 | * once in a memory buffer. |
| 4836 | * |
| 4837 | * We use separate string tables for class names, method names, and source |
| 4838 | * files to keep the indexes small. There will generally be no overlap |
| 4839 | * between the contents of these tables. |
| 4840 | */ |
| 4841 | jbyteArray Dbg::GetRecentAllocations() { |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 4842 | if ((false)) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4843 | DumpRecentAllocations(); |
| 4844 | } |
| 4845 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4846 | Thread* self = Thread::Current(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4847 | std::vector<uint8_t> bytes; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4848 | { |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4849 | MutexLock mu(self, *Locks::alloc_tracker_lock_); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4850 | gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords(); |
| 4851 | // In case this method is called when allocation tracker is disabled, |
| 4852 | // we should still send some data back. |
| 4853 | gc::AllocRecordObjectMap dummy; |
| 4854 | if (records == nullptr) { |
| 4855 | CHECK(!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()); |
| 4856 | records = &dummy; |
| 4857 | } |
| 4858 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4859 | // |
| 4860 | // Part 1: generate string tables. |
| 4861 | // |
| 4862 | StringTable class_names; |
| 4863 | StringTable method_names; |
| 4864 | StringTable filenames; |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4865 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4866 | const uint16_t capped_count = CappedAllocRecordCount(records->Size()); |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4867 | uint16_t count = capped_count; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4868 | for (auto it = records->RBegin(), end = records->REnd(); |
| 4869 | count > 0 && it != end; count--, it++) { |
| 4870 | const gc::AllocRecord* record = it->second; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4871 | std::string temp; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4872 | class_names.Add(it->first.Read()->GetClass()->GetDescriptor(&temp)); |
| 4873 | for (size_t i = 0, depth = record->GetDepth(); i < depth; i++) { |
| 4874 | ArtMethod* m = record->StackElement(i).GetMethod(); |
| 4875 | class_names.Add(m->GetDeclaringClassDescriptor()); |
| 4876 | method_names.Add(m->GetName()); |
| 4877 | filenames.Add(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4878 | } |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4879 | } |
| 4880 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4881 | LOG(INFO) << "recent allocation records: " << capped_count; |
| 4882 | LOG(INFO) << "allocation records all objects: " << records->Size(); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4883 | |
| 4884 | // |
| 4885 | // Part 2: Generate the output and store it in the buffer. |
| 4886 | // |
| 4887 | |
| 4888 | // (1b) message header len (to allow future expansion); includes itself |
| 4889 | // (1b) entry header len |
| 4890 | // (1b) stack frame len |
| 4891 | const int kMessageHeaderLen = 15; |
| 4892 | const int kEntryHeaderLen = 9; |
| 4893 | const int kStackFrameLen = 8; |
| 4894 | JDWP::Append1BE(bytes, kMessageHeaderLen); |
| 4895 | JDWP::Append1BE(bytes, kEntryHeaderLen); |
| 4896 | JDWP::Append1BE(bytes, kStackFrameLen); |
| 4897 | |
| 4898 | // (2b) number of entries |
| 4899 | // (4b) offset to string table from start of message |
| 4900 | // (2b) number of class name strings |
| 4901 | // (2b) number of method name strings |
| 4902 | // (2b) number of source file name strings |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 4903 | JDWP::Append2BE(bytes, capped_count); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4904 | size_t string_table_offset = bytes.size(); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4905 | JDWP::Append4BE(bytes, 0); // We'll patch this later... |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4906 | JDWP::Append2BE(bytes, class_names.Size()); |
| 4907 | JDWP::Append2BE(bytes, method_names.Size()); |
| 4908 | JDWP::Append2BE(bytes, filenames.Size()); |
| 4909 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4910 | std::string temp; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4911 | count = capped_count; |
| 4912 | // The last "count" number of allocation records in "records" are the most recent "count" number |
| 4913 | // of allocations. Reverse iterate to get them. The most recent allocation is sent first. |
| 4914 | for (auto it = records->RBegin(), end = records->REnd(); |
| 4915 | count > 0 && it != end; count--, it++) { |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4916 | // For each entry: |
| 4917 | // (4b) total allocation size |
| 4918 | // (2b) thread id |
| 4919 | // (2b) allocated object's class name index |
| 4920 | // (1b) stack depth |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4921 | const gc::AllocRecord* record = it->second; |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4922 | size_t stack_depth = record->GetDepth(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4923 | size_t allocated_object_class_name_index = |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4924 | class_names.IndexOf(it->first.Read()->GetClass()->GetDescriptor(&temp)); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 4925 | JDWP::Append4BE(bytes, record->ByteCount()); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4926 | JDWP::Append2BE(bytes, static_cast<uint16_t>(record->GetTid())); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4927 | JDWP::Append2BE(bytes, allocated_object_class_name_index); |
| 4928 | JDWP::Append1BE(bytes, stack_depth); |
| 4929 | |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4930 | for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) { |
| 4931 | // For each stack frame: |
| 4932 | // (2b) method's class name |
| 4933 | // (2b) method name |
| 4934 | // (2b) method source file |
| 4935 | // (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] | 4936 | ArtMethod* m = record->StackElement(stack_frame).GetMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4937 | size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor()); |
| 4938 | size_t method_name_index = method_names.IndexOf(m->GetName()); |
| 4939 | size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m)); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4940 | JDWP::Append2BE(bytes, class_name_index); |
| 4941 | JDWP::Append2BE(bytes, method_name_index); |
| 4942 | JDWP::Append2BE(bytes, file_name_index); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 4943 | JDWP::Append2BE(bytes, record->StackElement(stack_frame).ComputeLineNumber()); |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4944 | } |
Mathieu Chartier | 46e811b | 2013-07-10 17:09:14 -0700 | [diff] [blame] | 4945 | } |
| 4946 | |
| 4947 | // (xb) class name strings |
| 4948 | // (xb) method name strings |
| 4949 | // (xb) source file strings |
| 4950 | JDWP::Set4BE(&bytes[string_table_offset], bytes.size()); |
| 4951 | class_names.WriteTo(bytes); |
| 4952 | method_names.WriteTo(bytes); |
| 4953 | filenames.WriteTo(bytes); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4954 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 4955 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4956 | jbyteArray result = env->NewByteArray(bytes.size()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 4957 | if (result != nullptr) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 4958 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 4959 | } |
| 4960 | return result; |
| 4961 | } |
| 4962 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4963 | ArtMethod* DeoptimizationRequest::Method() const { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 4964 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 4965 | return soa.DecodeMethod(method_); |
| 4966 | } |
| 4967 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4968 | void DeoptimizationRequest::SetMethod(ArtMethod* m) { |
Hiroshi Yamauchi | 0ec17d2 | 2014-07-07 13:07:08 -0700 | [diff] [blame] | 4969 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 4970 | method_ = soa.EncodeMethod(m); |
| 4971 | } |
| 4972 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 4973 | } // namespace art |