blob: 4cc3583496bc3ae9ba9ce5bcdadcf3aaa369a2f2 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
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 Hughes3bb81562011-10-21 18:52:59 -070019#include <sys/uio.h>
20
Alex Light8c2b9292017-11-09 13:21:01 -080021#include <functional>
Andreas Gampef774a4e2017-07-06 22:15:18 -070022#include <memory>
Elliott Hughes545a0642011-11-08 19:10:03 -080023#include <set>
Andreas Gampef774a4e2017-07-06 22:15:18 -070024#include <vector>
Elliott Hughes545a0642011-11-08 19:10:03 -080025
Alex Lightb7c640d2019-03-20 15:52:13 -070026#include "android-base/macros.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080027#include "android-base/stringprintf.h"
28
Ian Rogers166db042013-07-26 12:05:57 -070029#include "arch/context.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070030#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070032#include "base/enums.h"
Alex Lightb7c640d2019-03-20 15:52:13 -070033#include "base/memory_tool.h"
David Sehr67bf42e2018-02-26 16:43:04 -080034#include "base/safe_map.h"
Andreas Gampef774a4e2017-07-06 22:15:18 -070035#include "base/strlcpy.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010036#include "base/time_utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "class_linker-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070038#include "class_linker.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080039#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080040#include "dex/dex_file-inl.h"
41#include "dex/dex_file_annotations.h"
42#include "dex/dex_file_types.h"
43#include "dex/dex_instruction.h"
David Sehr0225f8e2018-01-31 08:52:24 +000044#include "dex/utf.h"
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -080045#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070046#include "gc/accounting/card_table-inl.h"
Man Cao8c2ff642015-05-27 17:25:30 -070047#include "gc/allocation_record.h"
Andreas Gampe94c589d2017-12-27 12:43:01 -080048#include "gc/gc_cause.h"
Mathieu Chartieraa516822015-10-02 15:53:37 -070049#include "gc/scoped_gc_critical_section.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070050#include "gc/space/bump_pointer_space-walk-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070051#include "gc/space/large_object_space.h"
52#include "gc/space/space-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070053#include "handle_scope-inl.h"
Alex Lightb7c640d2019-03-20 15:52:13 -070054#include "instrumentation.h"
Sebastien Hertzcbc50642015-06-01 17:33:12 +020055#include "jdwp/jdwp_priv.h"
Vladimir Marko83114892019-04-11 13:05:50 +010056#include "jdwp/object_registry-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010057#include "jni/jni_internal.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070058#include "jvalue-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070059#include "mirror/array-alloc-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070060#include "mirror/class-alloc-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061#include "mirror/class-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070062#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064#include "mirror/object-inl.h"
65#include "mirror/object_array-inl.h"
Andreas Gampefd63bbf2018-10-29 12:55:35 -070066#include "mirror/string-alloc-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070067#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068#include "mirror/throwable.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070069#include "nativehelper/scoped_local_ref.h"
70#include "nativehelper/scoped_primitive_array.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000071#include "oat_file.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070072#include "obj_ptr-inl.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070073#include "reflection.h"
David Srbecky28f6cff2018-10-16 15:07:28 +010074#include "runtime-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070075#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070076#include "stack.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070077#include "thread_list.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070078#include "well_known_classes.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070079
Elliott Hughes872d4ec2011-10-21 17:07:15 -070080namespace art {
81
Andreas Gampe46ee31b2016-12-14 10:11:49 -080082using android::base::StringPrintf;
83
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020084// The key identifying the debugger to update instrumentation.
85static constexpr const char* kDbgInstrumentationKey = "Debugger";
86
Man Cao8c2ff642015-05-27 17:25:30 -070087// Limit alloc_record_count to the 2BE value (64k-1) that is the limit of the current protocol.
Brian Carlstrom306db812014-09-05 13:01:41 -070088static uint16_t CappedAllocRecordCount(size_t alloc_record_count) {
Man Cao1ed11b92015-06-11 22:47:35 -070089 const size_t cap = 0xffff;
Man Cao8c2ff642015-05-27 17:25:30 -070090 if (alloc_record_count > cap) {
91 return cap;
Brian Carlstrom306db812014-09-05 13:01:41 -070092 }
93 return alloc_record_count;
94}
Elliott Hughes475fc232011-10-25 15:00:35 -070095
Mathieu Chartier41af5e52015-10-28 11:10:46 -070096class Breakpoint : public ValueObject {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -070097 public:
Mathieu Chartier41af5e52015-10-28 11:10:46 -070098 Breakpoint(ArtMethod* method, uint32_t dex_pc, DeoptimizationRequest::Kind deoptimization_kind)
Alex Light97e78032017-06-27 17:51:55 -070099 : method_(method->GetCanonicalMethod(kRuntimePointerSize)),
Mathieu Chartier41af5e52015-10-28 11:10:46 -0700100 dex_pc_(dex_pc),
101 deoptimization_kind_(deoptimization_kind) {
Sebastien Hertzf3928792014-11-17 19:00:37 +0100102 CHECK(deoptimization_kind_ == DeoptimizationRequest::kNothing ||
103 deoptimization_kind_ == DeoptimizationRequest::kSelectiveDeoptimization ||
104 deoptimization_kind_ == DeoptimizationRequest::kFullDeoptimization);
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700105 }
106
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 Breakpoint(const Breakpoint& other) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier41af5e52015-10-28 11:10:46 -0700108 : method_(other.method_),
109 dex_pc_(other.dex_pc_),
110 deoptimization_kind_(other.deoptimization_kind_) {}
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700111
Mathieu Chartier41af5e52015-10-28 11:10:46 -0700112 // Method() is called from root visiting, do not use ScopedObjectAccess here or it can cause
113 // GC to deadlock if another thread tries to call SuspendAll while the GC is in a runnable state.
114 ArtMethod* Method() const {
115 return method_;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700116 }
117
118 uint32_t DexPc() const {
119 return dex_pc_;
120 }
121
Sebastien Hertzf3928792014-11-17 19:00:37 +0100122 DeoptimizationRequest::Kind GetDeoptimizationKind() const {
123 return deoptimization_kind_;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700124 }
125
Alex Light6c8467f2015-11-20 15:03:26 -0800126 // Returns true if the method of this breakpoint and the passed in method should be considered the
127 // same. That is, they are either the same method or they are copied from the same method.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700128 bool IsInMethod(ArtMethod* m) const REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light97e78032017-06-27 17:51:55 -0700129 return method_ == m->GetCanonicalMethod(kRuntimePointerSize);
Alex Light6c8467f2015-11-20 15:03:26 -0800130 }
131
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700132 private:
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100133 // The location of this breakpoint.
Mathieu Chartier41af5e52015-10-28 11:10:46 -0700134 ArtMethod* method_;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700135 uint32_t dex_pc_;
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100136
137 // Indicates whether breakpoint needs full deoptimization or selective deoptimization.
Sebastien Hertzf3928792014-11-17 19:00:37 +0100138 DeoptimizationRequest::Kind deoptimization_kind_;
Elliott Hughes86964332012-02-15 19:37:42 -0800139};
140
Sebastien Hertzed2be172014-08-19 15:33:43 +0200141static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700142 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700143 os << StringPrintf("Breakpoint[%s @%#x]", ArtMethod::PrettyMethod(rhs.Method()).c_str(),
144 rhs.DexPc());
Elliott Hughes86964332012-02-15 19:37:42 -0800145 return os;
146}
147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100148class DebugInstrumentationListener final : public instrumentation::InstrumentationListener {
Ian Rogers62d6c772013-02-27 08:32:07 -0800149 public:
150 DebugInstrumentationListener() {}
151 virtual ~DebugInstrumentationListener() {}
152
Alex Lightd7661582017-05-01 13:48:16 -0700153 void MethodEntered(Thread* thread,
154 Handle<mirror::Object> this_object,
155 ArtMethod* method,
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200156 uint32_t dex_pc)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100157 override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800158 if (method->IsNative()) {
159 // TODO: post location events is a suspension point and native method entry stubs aren't.
160 return;
161 }
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200162 if (IsListeningToDexPcMoved()) {
163 // We also listen to kDexPcMoved instrumentation event so we know the DexPcMoved method is
164 // going to be called right after us. To avoid sending JDWP events twice for this location,
165 // we report the event in DexPcMoved. However, we must remind this is method entry so we
166 // send the METHOD_ENTRY event. And we can also group it with other events for this location
167 // like BREAKPOINT or SINGLE_STEP (or even METHOD_EXIT if this is a RETURN instruction).
168 thread->SetDebugMethodEntry();
169 } else if (IsListeningToMethodExit() && IsReturn(method, dex_pc)) {
170 // We also listen to kMethodExited instrumentation event and the current instruction is a
171 // RETURN so we know the MethodExited method is going to be called right after us. To avoid
172 // sending JDWP events twice for this location, we report the event(s) in MethodExited.
173 // However, we must remind this is method entry so we send the METHOD_ENTRY event. And we can
174 // also group it with other events for this location like BREAKPOINT or SINGLE_STEP.
175 thread->SetDebugMethodEntry();
176 } else {
Alex Lightd7661582017-05-01 13:48:16 -0700177 Dbg::UpdateDebugger(thread, this_object.Get(), method, 0, Dbg::kMethodEntry, nullptr);
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200178 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800179 }
180
Alex Lightd7661582017-05-01 13:48:16 -0700181 void MethodExited(Thread* thread,
182 Handle<mirror::Object> this_object,
183 ArtMethod* method,
184 uint32_t dex_pc,
Alex Lightb7c640d2019-03-20 15:52:13 -0700185 instrumentation::OptionalFrame frame ATTRIBUTE_UNUSED,
186 JValue& return_value)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100187 override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800188 if (method->IsNative()) {
189 // TODO: post location events is a suspension point and native method entry stubs aren't.
190 return;
191 }
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200192 uint32_t events = Dbg::kMethodExit;
193 if (thread->IsDebugMethodEntry()) {
194 // It is also the method entry.
195 DCHECK(IsReturn(method, dex_pc));
196 events |= Dbg::kMethodEntry;
197 thread->ClearDebugMethodEntry();
198 }
Alex Lightd7661582017-05-01 13:48:16 -0700199 Dbg::UpdateDebugger(thread, this_object.Get(), method, dex_pc, events, &return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800200 }
201
Alex Lightd7661582017-05-01 13:48:16 -0700202 void MethodUnwind(Thread* thread ATTRIBUTE_UNUSED,
203 Handle<mirror::Object> this_object ATTRIBUTE_UNUSED,
204 ArtMethod* method,
205 uint32_t dex_pc)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100206 override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800207 // We're not recorded to listen to this kind of event, so complain.
David Sehr709b0702016-10-13 09:12:37 -0700208 LOG(ERROR) << "Unexpected method unwind event in debugger " << ArtMethod::PrettyMethod(method)
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100209 << " " << dex_pc;
Ian Rogers62d6c772013-02-27 08:32:07 -0800210 }
211
Alex Lightd7661582017-05-01 13:48:16 -0700212 void DexPcMoved(Thread* thread,
213 Handle<mirror::Object> this_object,
214 ArtMethod* method,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200215 uint32_t new_dex_pc)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100216 override REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200217 if (IsListeningToMethodExit() && IsReturn(method, new_dex_pc)) {
218 // We also listen to kMethodExited instrumentation event and the current instruction is a
219 // RETURN so we know the MethodExited method is going to be called right after us. Like in
220 // MethodEntered, we delegate event reporting to MethodExited.
221 // Besides, if this RETURN instruction is the only one in the method, we can send multiple
222 // JDWP events in the same packet: METHOD_ENTRY, METHOD_EXIT, BREAKPOINT and/or SINGLE_STEP.
223 // Therefore, we must not clear the debug method entry flag here.
224 } else {
225 uint32_t events = 0;
226 if (thread->IsDebugMethodEntry()) {
227 // It is also the method entry.
228 events = Dbg::kMethodEntry;
229 thread->ClearDebugMethodEntry();
230 }
Alex Lightd7661582017-05-01 13:48:16 -0700231 Dbg::UpdateDebugger(thread, this_object.Get(), method, new_dex_pc, events, nullptr);
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200232 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800233 }
234
Alex Lightd7661582017-05-01 13:48:16 -0700235 void FieldRead(Thread* thread ATTRIBUTE_UNUSED,
236 Handle<mirror::Object> this_object,
237 ArtMethod* method,
238 uint32_t dex_pc,
239 ArtField* field)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100240 override REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightd7661582017-05-01 13:48:16 -0700241 Dbg::PostFieldAccessEvent(method, dex_pc, this_object.Get(), field);
Ian Rogers62d6c772013-02-27 08:32:07 -0800242 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200243
Alex Lightd7661582017-05-01 13:48:16 -0700244 void FieldWritten(Thread* thread ATTRIBUTE_UNUSED,
245 Handle<mirror::Object> this_object,
246 ArtMethod* method,
247 uint32_t dex_pc,
248 ArtField* field,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700249 const JValue& field_value)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100250 override REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightd7661582017-05-01 13:48:16 -0700251 Dbg::PostFieldModificationEvent(method, dex_pc, this_object.Get(), field, &field_value);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200252 }
253
Alex Light6e1607e2017-08-23 10:06:18 -0700254 void ExceptionThrown(Thread* thread ATTRIBUTE_UNUSED,
Alex Lightd7661582017-05-01 13:48:16 -0700255 Handle<mirror::Throwable> exception_object)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100256 override REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightd7661582017-05-01 13:48:16 -0700257 Dbg::PostException(exception_object.Get());
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200258 }
259
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000260 // We only care about branches in the Jit.
261 void Branch(Thread* /*thread*/, ArtMethod* method, uint32_t dex_pc, int32_t dex_pc_offset)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100262 override REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700263 LOG(ERROR) << "Unexpected branch event in debugger " << ArtMethod::PrettyMethod(method)
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000264 << " " << dex_pc << ", " << dex_pc_offset;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800265 }
266
Alex Light798eab02017-08-23 12:54:53 -0700267 // TODO Might be worth it to post ExceptionCatch event.
268 void ExceptionHandled(Thread* thread ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100269 Handle<mirror::Throwable> throwable ATTRIBUTE_UNUSED) override {
Alex Light798eab02017-08-23 12:54:53 -0700270 LOG(ERROR) << "Unexpected exception handled event in debugger";
271 }
272
Alex Light05f47742017-09-14 00:34:44 +0000273 // TODO Might be worth it to implement this.
274 void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100275 const ShadowFrame& frame ATTRIBUTE_UNUSED) override {
Alex Light05f47742017-09-14 00:34:44 +0000276 LOG(ERROR) << "Unexpected WatchedFramePop event in debugger";
277 }
Alex Light798eab02017-08-23 12:54:53 -0700278
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200279 private:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800280 static bool IsReturn(ArtMethod* method, uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) {
281 return method->DexInstructions().InstructionAt(dex_pc).IsReturn();
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200282 }
283
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700284 static bool IsListeningToDexPcMoved() REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200285 return IsListeningTo(instrumentation::Instrumentation::kDexPcMoved);
286 }
287
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700288 static bool IsListeningToMethodExit() REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200289 return IsListeningTo(instrumentation::Instrumentation::kMethodExited);
290 }
291
292 static bool IsListeningTo(instrumentation::Instrumentation::InstrumentationEvent event)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700293 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz9d6bf692015-04-10 12:12:33 +0200294 return (Dbg::GetInstrumentationEvents() & event) != 0;
295 }
296
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200297 DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800298} gDebugInstrumentationListener;
299
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700300// JDWP is allowed unless the Zygote forbids it.
301static bool gJdwpAllowed = true;
302
Elliott Hughesc0f09332012-03-26 13:27:06 -0700303// Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line?
Elliott Hughes3bb81562011-10-21 18:52:59 -0700304static bool gJdwpConfigured = false;
305
Sebastien Hertzb3b173b2015-02-06 09:16:32 +0100306// JDWP options for debugging. Only valid if IsJdwpConfigured() is true.
307static JDWP::JdwpOptions gJdwpOptions;
308
Elliott Hughes3bb81562011-10-21 18:52:59 -0700309// Runtime JDWP state.
Ian Rogersc0542af2014-09-03 16:16:56 -0700310static JDWP::JdwpState* gJdwpState = nullptr;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700311static bool gDebuggerConnected; // debugger or DDMS is connected.
Elliott Hughes3bb81562011-10-21 18:52:59 -0700312
Elliott Hughes47fce012011-10-25 18:37:19 -0700313static bool gDdmThreadNotification = false;
314
Elliott Hughes767a1472011-10-26 18:49:02 -0700315// DDMS GC-related settings.
316static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
317static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
318static Dbg::HpsgWhat gDdmHpsgWhat;
319static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
320static Dbg::HpsgWhat gDdmNhsgWhat;
321
Daniel Mihalyieb076692014-08-22 17:33:31 +0200322bool Dbg::gDebuggerActive = false;
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100323bool Dbg::gDisposed = false;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200324ObjectRegistry* Dbg::gRegistry = nullptr;
Alex Light21611932017-09-26 13:07:39 -0700325DebuggerActiveMethodInspectionCallback Dbg::gDebugActiveCallback;
Alex Light8c2b9292017-11-09 13:21:01 -0800326DebuggerDdmCallback Dbg::gDebugDdmCallback;
Alex Light40320712017-12-14 11:52:04 -0800327InternalDebuggerControlCallback Dbg::gDebuggerControlCallback;
Elliott Hughes475fc232011-10-25 15:00:35 -0700328
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100329// Deoptimization support.
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100330std::vector<DeoptimizationRequest> Dbg::deoptimization_requests_;
331size_t Dbg::full_deoptimization_event_count_ = 0;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100332
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200333// Instrumentation event reference counters.
334size_t Dbg::dex_pc_change_event_ref_count_ = 0;
335size_t Dbg::method_enter_event_ref_count_ = 0;
336size_t Dbg::method_exit_event_ref_count_ = 0;
337size_t Dbg::field_read_event_ref_count_ = 0;
338size_t Dbg::field_write_event_ref_count_ = 0;
339size_t Dbg::exception_catch_event_ref_count_ = 0;
340uint32_t Dbg::instrumentation_events_ = 0;
341
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000342Dbg::DbgThreadLifecycleCallback Dbg::thread_lifecycle_callback_;
Andreas Gampe0f01b582017-01-18 15:22:37 -0800343Dbg::DbgClassLoadCallback Dbg::class_load_callback_;
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000344
Alex Light8c2b9292017-11-09 13:21:01 -0800345void DebuggerDdmCallback::DdmPublishChunk(uint32_t type, const ArrayRef<const uint8_t>& data) {
Alex Light772099a2017-11-21 14:05:04 -0800346 if (gJdwpState == nullptr) {
347 VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
348 } else {
349 iovec vec[1];
350 vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(data.data()));
351 vec[0].iov_len = data.size();
352 gJdwpState->DdmSendChunkV(type, vec, 1);
353 }
Alex Light8c2b9292017-11-09 13:21:01 -0800354}
355
Alex Light21611932017-09-26 13:07:39 -0700356bool DebuggerActiveMethodInspectionCallback::IsMethodBeingInspected(ArtMethod* m ATTRIBUTE_UNUSED) {
357 return Dbg::IsDebuggerActive();
358}
359
Alex Light0fa17862017-10-24 13:43:05 -0700360bool DebuggerActiveMethodInspectionCallback::IsMethodSafeToJit(ArtMethod* m) {
361 return !Dbg::MethodHasAnyBreakpoints(m);
362}
363
Alex Lightf2858632018-04-02 11:28:50 -0700364bool DebuggerActiveMethodInspectionCallback::MethodNeedsDebugVersion(
365 ArtMethod* m ATTRIBUTE_UNUSED) {
366 return Dbg::IsDebuggerActive();
367}
368
Alex Light40320712017-12-14 11:52:04 -0800369void InternalDebuggerControlCallback::StartDebugger() {
370 // Release the mutator lock.
371 ScopedThreadStateChange stsc(art::Thread::Current(), kNative);
372 Dbg::StartJdwp();
373}
374
375void InternalDebuggerControlCallback::StopDebugger() {
376 Dbg::StopJdwp();
377}
378
379bool InternalDebuggerControlCallback::IsDebuggerConfigured() {
380 return Dbg::IsJdwpConfigured();
381}
382
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100383// Breakpoints.
jeffhao09bfc6a2012-12-11 18:11:43 -0800384static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_);
Elliott Hughes86964332012-02-15 19:37:42 -0800385
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700386void DebugInvokeReq::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
387 receiver.VisitRootIfNonNull(visitor, root_info); // null for static method call.
388 klass.VisitRoot(visitor, root_info);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700389}
390
Sebastien Hertz597c4f02015-01-26 17:37:14 +0100391void SingleStepControl::AddDexPc(uint32_t dex_pc) {
392 dex_pcs_.insert(dex_pc);
Sebastien Hertzbb43b432014-04-14 11:59:08 +0200393}
394
Sebastien Hertz597c4f02015-01-26 17:37:14 +0100395bool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const {
396 return dex_pcs_.find(dex_pc) == dex_pcs_.end();
Sebastien Hertzbb43b432014-04-14 11:59:08 +0200397}
398
Alex Light6c8467f2015-11-20 15:03:26 -0800399static bool IsBreakpoint(ArtMethod* m, uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700400 REQUIRES(!Locks::breakpoint_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700401 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzed2be172014-08-19 15:33:43 +0200402 ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100403 for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
Alex Light6c8467f2015-11-20 15:03:26 -0800404 if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].IsInMethod(m)) {
Elliott Hughes86964332012-02-15 19:37:42 -0800405 VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
406 return true;
407 }
408 }
409 return false;
410}
411
Sebastien Hertz52d131d2014-03-13 16:17:40 +0100412static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread)
Mathieu Chartier90443472015-07-16 20:32:27 -0700413 REQUIRES(!Locks::thread_suspend_count_lock_) {
Elliott Hughes9e0c1752013-01-09 14:02:58 -0800414 MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
415 // A thread may be suspended for GC; in this code, we really want to know whether
416 // there's a debugger suspension active.
417 return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
418}
419
Vladimir Marko4617d582019-03-28 13:48:31 +0000420static ObjPtr<mirror::Array> DecodeNonNullArray(JDWP::RefTypeId id, JDWP::JdwpError* error)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700421 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko83114892019-04-11 13:05:50 +0100422 ObjPtr<mirror::Object> o = Dbg::GetObjectRegistry()->Get<mirror::Object>(id, error);
Ian Rogersc0542af2014-09-03 16:16:56 -0700423 if (o == nullptr) {
424 *error = JDWP::ERR_INVALID_OBJECT;
425 return nullptr;
Elliott Hughes436e3722012-02-17 20:01:47 -0800426 }
427 if (!o->IsArrayInstance()) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700428 *error = JDWP::ERR_INVALID_ARRAY;
429 return nullptr;
Elliott Hughes436e3722012-02-17 20:01:47 -0800430 }
Ian Rogersc0542af2014-09-03 16:16:56 -0700431 *error = JDWP::ERR_NONE;
Elliott Hughes436e3722012-02-17 20:01:47 -0800432 return o->AsArray();
433}
434
Vladimir Marko4617d582019-03-28 13:48:31 +0000435static ObjPtr<mirror::Class> DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError* error)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700436 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko83114892019-04-11 13:05:50 +0100437 ObjPtr<mirror::Object> o = Dbg::GetObjectRegistry()->Get<mirror::Object>(id, error);
Ian Rogersc0542af2014-09-03 16:16:56 -0700438 if (o == nullptr) {
439 *error = JDWP::ERR_INVALID_OBJECT;
440 return nullptr;
Elliott Hughes436e3722012-02-17 20:01:47 -0800441 }
442 if (!o->IsClass()) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700443 *error = JDWP::ERR_INVALID_CLASS;
444 return nullptr;
Elliott Hughes436e3722012-02-17 20:01:47 -0800445 }
Ian Rogersc0542af2014-09-03 16:16:56 -0700446 *error = JDWP::ERR_NONE;
Elliott Hughes436e3722012-02-17 20:01:47 -0800447 return o->AsClass();
448}
449
Ian Rogersc0542af2014-09-03 16:16:56 -0700450static Thread* DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id,
451 JDWP::JdwpError* error)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700452 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700453 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) {
Vladimir Marko4617d582019-03-28 13:48:31 +0000454 ObjPtr<mirror::Object> thread_peer =
Vladimir Marko83114892019-04-11 13:05:50 +0100455 Dbg::GetObjectRegistry()->Get<mirror::Object>(thread_id, error);
Ian Rogersc0542af2014-09-03 16:16:56 -0700456 if (thread_peer == nullptr) {
Elliott Hughes221229c2013-01-08 18:17:50 -0800457 // This isn't even an object.
Ian Rogersc0542af2014-09-03 16:16:56 -0700458 *error = JDWP::ERR_INVALID_OBJECT;
459 return nullptr;
Elliott Hughes436e3722012-02-17 20:01:47 -0800460 }
Elliott Hughes221229c2013-01-08 18:17:50 -0800461
Mathieu Chartier0795f232016-09-27 18:43:30 -0700462 ObjPtr<mirror::Class> java_lang_Thread =
463 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_Thread);
Elliott Hughes221229c2013-01-08 18:17:50 -0800464 if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
465 // This isn't a thread.
Ian Rogersc0542af2014-09-03 16:16:56 -0700466 *error = JDWP::ERR_INVALID_THREAD;
467 return nullptr;
Elliott Hughes221229c2013-01-08 18:17:50 -0800468 }
469
Sebastien Hertz69206392015-04-07 15:54:25 +0200470 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -0700471 Thread* thread = Thread::FromManagedThread(soa, thread_peer);
472 // If thread is null then this a java.lang.Thread without a Thread*. Must be a un-started or a
473 // zombie.
474 *error = (thread == nullptr) ? JDWP::ERR_THREAD_NOT_ALIVE : JDWP::ERR_NONE;
475 return thread;
Elliott Hughes436e3722012-02-17 20:01:47 -0800476}
477
Elliott Hughes24437992011-11-30 14:49:33 -0800478static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
479 // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
480 // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
481 return static_cast<JDWP::JdwpTag>(descriptor[0]);
482}
483
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000484static JDWP::JdwpTag BasicTagFromClass(ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700485 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700486 std::string temp;
487 const char* descriptor = klass->GetDescriptor(&temp);
488 return BasicTagFromDescriptor(descriptor);
489}
490
Vladimir Marko83114892019-04-11 13:05:50 +0100491static JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, ObjPtr<mirror::Class> c)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700492 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700493 CHECK(c != nullptr);
Elliott Hughes24437992011-11-30 14:49:33 -0800494 if (c->IsArrayClass()) {
495 return JDWP::JT_ARRAY;
496 }
Elliott Hughes24437992011-11-30 14:49:33 -0800497 if (c->IsStringClass()) {
498 return JDWP::JT_STRING;
Elliott Hughes24437992011-11-30 14:49:33 -0800499 }
Ian Rogers98379392014-02-24 16:53:16 -0800500 if (c->IsClassClass()) {
501 return JDWP::JT_CLASS_OBJECT;
502 }
503 {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700504 ObjPtr<mirror::Class> thread_class =
505 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_Thread);
Ian Rogers98379392014-02-24 16:53:16 -0800506 if (thread_class->IsAssignableFrom(c)) {
507 return JDWP::JT_THREAD;
508 }
509 }
510 {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700511 ObjPtr<mirror::Class> thread_group_class =
512 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_ThreadGroup);
Ian Rogers98379392014-02-24 16:53:16 -0800513 if (thread_group_class->IsAssignableFrom(c)) {
514 return JDWP::JT_THREAD_GROUP;
515 }
516 }
517 {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700518 ObjPtr<mirror::Class> class_loader_class =
519 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_ClassLoader);
Ian Rogers98379392014-02-24 16:53:16 -0800520 if (class_loader_class->IsAssignableFrom(c)) {
521 return JDWP::JT_CLASS_LOADER;
522 }
523 }
524 return JDWP::JT_OBJECT;
Elliott Hughes24437992011-11-30 14:49:33 -0800525}
526
527/*
528 * Objects declared to hold Object might actually hold a more specific
529 * type. The debugger may take a special interest in these (e.g. it
530 * wants to display the contents of Strings), so we want to return an
531 * appropriate tag.
532 *
533 * Null objects are tagged JT_OBJECT.
534 */
Vladimir Marko423bebb2019-03-26 15:17:21 +0000535JDWP::JdwpTag Dbg::TagFromObject(const ScopedObjectAccessUnchecked& soa, ObjPtr<mirror::Object> o) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700536 return (o == nullptr) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass());
Elliott Hughes24437992011-11-30 14:49:33 -0800537}
538
539static bool IsPrimitiveTag(JDWP::JdwpTag tag) {
540 switch (tag) {
541 case JDWP::JT_BOOLEAN:
542 case JDWP::JT_BYTE:
543 case JDWP::JT_CHAR:
544 case JDWP::JT_FLOAT:
545 case JDWP::JT_DOUBLE:
546 case JDWP::JT_INT:
547 case JDWP::JT_LONG:
548 case JDWP::JT_SHORT:
549 case JDWP::JT_VOID:
550 return true;
551 default:
552 return false;
553 }
554}
555
Sebastien Hertzb3b173b2015-02-06 09:16:32 +0100556void Dbg::StartJdwp() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700557 if (!gJdwpAllowed || !IsJdwpConfigured()) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700558 // No JDWP for you!
559 return;
560 }
561
Ian Rogers719d1a32014-03-06 12:13:39 -0800562 CHECK(gRegistry == nullptr);
Elliott Hughes475fc232011-10-25 15:00:35 -0700563 gRegistry = new ObjectRegistry;
564
Alex Light8c2b9292017-11-09 13:21:01 -0800565 {
566 // Setup the Ddm listener
567 ScopedObjectAccess soa(Thread::Current());
568 Runtime::Current()->GetRuntimeCallbacks()->AddDdmCallback(&gDebugDdmCallback);
569 }
570
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700571 // Init JDWP if the debugger is enabled. This may connect out to a
572 // debugger, passively listen for a debugger, or block waiting for a
573 // debugger.
Sebastien Hertzb3b173b2015-02-06 09:16:32 +0100574 gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
Ian Rogersc0542af2014-09-03 16:16:56 -0700575 if (gJdwpState == nullptr) {
Elliott Hughesf8a2df72011-12-01 12:19:54 -0800576 // We probably failed because some other process has the port already, which means that
577 // if we don't abort the user is likely to think they're talking to us when they're actually
578 // talking to that other process.
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800579 LOG(FATAL) << "Debugger thread failed to initialize";
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700580 }
581
582 // If a debugger has already attached, send the "welcome" message.
583 // This may cause us to suspend all threads.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700584 if (gJdwpState->IsActive()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700585 ScopedObjectAccess soa(Thread::Current());
Sebastien Hertz7d955652014-10-22 10:57:10 +0200586 gJdwpState->PostVMStart();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700587 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700588}
589
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700590void Dbg::StopJdwp() {
Sebastien Hertzc6345ef2014-08-18 19:26:39 +0200591 // Post VM_DEATH event before the JDWP connection is closed (either by the JDWP thread or the
592 // destruction of gJdwpState).
593 if (gJdwpState != nullptr && gJdwpState->IsActive()) {
594 gJdwpState->PostVMDeath();
595 }
Sebastien Hertz0376e6b2014-02-06 18:12:59 +0100596 // Prevent the JDWP thread from processing JDWP incoming packets after we close the connection.
Sebastien Hertz4e5b2082015-03-24 19:03:40 +0100597 Dispose();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700598 delete gJdwpState;
Ian Rogers719d1a32014-03-06 12:13:39 -0800599 gJdwpState = nullptr;
Elliott Hughes475fc232011-10-25 15:00:35 -0700600 delete gRegistry;
Ian Rogers719d1a32014-03-06 12:13:39 -0800601 gRegistry = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700602}
603
Elliott Hughes767a1472011-10-26 18:49:02 -0700604void Dbg::GcDidFinish() {
605 if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700607 VLOG(jdwp) << "Sending heap info to DDM";
Elliott Hughes7162ad92011-10-27 14:08:42 -0700608 DdmSendHeapInfo(gDdmHpifWhen);
Elliott Hughes767a1472011-10-26 18:49:02 -0700609 }
610 if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700612 VLOG(jdwp) << "Dumping heap to DDM";
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700613 DdmSendHeapSegments(false);
Elliott Hughes767a1472011-10-26 18:49:02 -0700614 }
615 if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700616 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700617 VLOG(jdwp) << "Dumping native heap to DDM";
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700618 DdmSendHeapSegments(true);
Elliott Hughes767a1472011-10-26 18:49:02 -0700619 }
620}
621
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700622void Dbg::SetJdwpAllowed(bool allowed) {
623 gJdwpAllowed = allowed;
624}
625
Leonard Mosescueb842212016-10-06 17:26:36 -0700626bool Dbg::IsJdwpAllowed() {
627 return gJdwpAllowed;
628}
629
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700630DebugInvokeReq* Dbg::GetInvokeReq() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700631 return Thread::Current()->GetInvokeReq();
632}
633
634Thread* Dbg::GetDebugThread() {
Ian Rogersc0542af2014-09-03 16:16:56 -0700635 return (gJdwpState != nullptr) ? gJdwpState->GetDebugThread() : nullptr;
Elliott Hughes475fc232011-10-25 15:00:35 -0700636}
637
638void Dbg::ClearWaitForEventThread() {
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100639 gJdwpState->ReleaseJdwpTokenForEvent();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700640}
641
642void Dbg::Connected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700643 CHECK(!gDebuggerConnected);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800644 VLOG(jdwp) << "JDWP has attached";
Elliott Hughes3bb81562011-10-21 18:52:59 -0700645 gDebuggerConnected = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800646 gDisposed = false;
647}
648
Sebastien Hertzf3928792014-11-17 19:00:37 +0100649bool Dbg::RequiresDeoptimization() {
650 // We don't need deoptimization if everything runs with interpreter after
651 // enabling -Xint mode.
652 return !Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly();
653}
654
Elliott Hughesa2155262011-11-16 16:26:58 -0800655void Dbg::GoActive() {
656 // Enable all debugging features, including scans for breakpoints.
657 // This is a no-op if we're already active.
658 // Only called from the JDWP handler thread.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200659 if (IsDebuggerActive()) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800660 return;
661 }
662
Mathieu Chartieraa516822015-10-02 15:53:37 -0700663 Thread* const self = Thread::Current();
Elliott Hughesc0f09332012-03-26 13:27:06 -0700664 {
665 // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
Mathieu Chartieraa516822015-10-02 15:53:37 -0700666 ReaderMutexLock mu(self, *Locks::breakpoint_lock_);
Elliott Hughesc0f09332012-03-26 13:27:06 -0700667 CHECK_EQ(gBreakpoints.size(), 0U);
668 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800669
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100670 {
Mathieu Chartieraa516822015-10-02 15:53:37 -0700671 MutexLock mu(self, *Locks::deoptimization_lock_);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100672 CHECK_EQ(deoptimization_requests_.size(), 0U);
673 CHECK_EQ(full_deoptimization_event_count_, 0U);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200674 CHECK_EQ(dex_pc_change_event_ref_count_, 0U);
675 CHECK_EQ(method_enter_event_ref_count_, 0U);
676 CHECK_EQ(method_exit_event_ref_count_, 0U);
677 CHECK_EQ(field_read_event_ref_count_, 0U);
678 CHECK_EQ(field_write_event_ref_count_, 0U);
679 CHECK_EQ(exception_catch_event_ref_count_, 0U);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100680 }
681
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 Runtime* runtime = Runtime::Current();
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000683 // Best effort deoptimization if the runtime is non-Java debuggable. This happens when
684 // ro.debuggable is set, but the application is not debuggable, or when a standalone
685 // dalvikvm invocation is not passed the debuggable option (-Xcompiler-option --debuggable).
686 //
687 // The performance cost of this is non-negligible during native-debugging due to the
David Srbeckyf4480162016-03-16 00:06:24 +0000688 // forced JIT, so we keep the AOT code in that case in exchange for limited native debugging.
Nicolas Geoffray226805d2018-12-14 10:59:02 +0000689 ScopedSuspendAll ssa(__FUNCTION__);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000690 if (!runtime->IsJavaDebuggable() &&
691 !runtime->GetInstrumentation()->IsForcedInterpretOnly() &&
692 !runtime->IsNativeDebuggable()) {
693 runtime->DeoptimizeBootImage();
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800694 }
695
Sebastien Hertzf3928792014-11-17 19:00:37 +0100696 if (RequiresDeoptimization()) {
697 runtime->GetInstrumentation()->EnableDeoptimization();
698 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200699 instrumentation_events_ = 0;
David Srbecky28f6cff2018-10-16 15:07:28 +0100700 Runtime::DoAndMaybeSwitchInterpreter([=](){ gDebuggerActive = true; });
Andreas Gampe380f0d82019-06-10 11:01:43 -0700701 runtime->GetRuntimeCallbacks()->AddClassLoadCallback(Dbg::GetClassLoadCallback());
702 runtime->GetRuntimeCallbacks()->AddMethodInspectionCallback(&gDebugActiveCallback);
Ian Rogers62d6c772013-02-27 08:32:07 -0800703 LOG(INFO) << "Debugger is active";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700704}
705
706void Dbg::Disconnected() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700707 CHECK(gDebuggerConnected);
708
Elliott Hughesc0f09332012-03-26 13:27:06 -0700709 LOG(INFO) << "Debugger is no longer active";
Elliott Hughes234ab152011-10-26 14:02:26 -0700710
Hiroshi Yamauchi98810e32016-05-24 14:55:40 -0700711 // Suspend all threads and exclusively acquire the mutator lock. Remove the debugger as a listener
Ian Rogers62d6c772013-02-27 08:32:07 -0800712 // and clear the object registry.
713 Runtime* runtime = Runtime::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800714 Thread* self = Thread::Current();
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700715 {
Mathieu Chartieraa516822015-10-02 15:53:37 -0700716 // Required for DisableDeoptimization.
717 gc::ScopedGCCriticalSection gcs(self,
718 gc::kGcCauseInstrumentation,
719 gc::kCollectorTypeInstrumentation);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700720 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700721 // Debugger may not be active at this point.
722 if (IsDebuggerActive()) {
723 {
724 // Since we're going to disable deoptimization, we clear the deoptimization requests queue.
725 // This prevents us from having any pending deoptimization request when the debugger attaches
726 // to us again while no event has been requested yet.
Mathieu Chartiera6b1ead2015-10-06 10:32:38 -0700727 MutexLock mu(self, *Locks::deoptimization_lock_);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700728 deoptimization_requests_.clear();
729 full_deoptimization_event_count_ = 0U;
730 }
731 if (instrumentation_events_ != 0) {
732 runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener,
733 instrumentation_events_);
734 instrumentation_events_ = 0;
735 }
736 if (RequiresDeoptimization()) {
737 runtime->GetInstrumentation()->DisableDeoptimization(kDbgInstrumentationKey);
738 }
David Srbecky28f6cff2018-10-16 15:07:28 +0100739 Runtime::DoAndMaybeSwitchInterpreter([=](){ gDebuggerActive = false; });
Andreas Gampe380f0d82019-06-10 11:01:43 -0700740 runtime->GetRuntimeCallbacks()->RemoveClassLoadCallback(Dbg::GetClassLoadCallback());
741 runtime->GetRuntimeCallbacks()->RemoveMethodInspectionCallback(
Alex Light21611932017-09-26 13:07:39 -0700742 &gDebugActiveCallback);
Sebastien Hertzaaea7342014-02-25 15:10:04 +0100743 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100744 }
Sebastien Hertz55f65342015-01-13 22:48:34 +0100745
746 {
747 ScopedObjectAccess soa(self);
748 gRegistry->Clear();
749 }
750
751 gDebuggerConnected = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700752}
753
Sebastien Hertzb3b173b2015-02-06 09:16:32 +0100754void Dbg::ConfigureJdwp(const JDWP::JdwpOptions& jdwp_options) {
755 CHECK_NE(jdwp_options.transport, JDWP::kJdwpTransportUnknown);
756 gJdwpOptions = jdwp_options;
757 gJdwpConfigured = true;
Alex Light40320712017-12-14 11:52:04 -0800758 Runtime::Current()->GetRuntimeCallbacks()->AddDebuggerControlCallback(&gDebuggerControlCallback);
Sebastien Hertzb3b173b2015-02-06 09:16:32 +0100759}
760
Elliott Hughesc0f09332012-03-26 13:27:06 -0700761bool Dbg::IsJdwpConfigured() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700762 return gJdwpConfigured;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700763}
764
765int64_t Dbg::LastDebuggerActivity() {
Elliott Hughesca951522011-12-05 12:01:32 -0800766 return gJdwpState->LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700767}
768
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700769void Dbg::UndoDebuggerSuspensions() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700770 Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700771}
772
Elliott Hughes88d63092013-01-09 09:55:54 -0800773std::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700774 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +0100775 ObjPtr<mirror::Object> o = gRegistry->Get<mirror::Object>(class_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -0700776 if (o == nullptr) {
777 if (error == JDWP::ERR_NONE) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700778 return "null";
Ian Rogersc0542af2014-09-03 16:16:56 -0700779 } else {
780 return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
781 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800782 }
783 if (!o->IsClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +0000784 return StringPrintf("non-class %p", o.Ptr()); // This is only used for debugging output anyway.
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800785 }
Sebastien Hertz6995c602014-09-09 12:10:13 +0200786 return GetClassName(o->AsClass());
787}
788
Vladimir Marko4617d582019-03-28 13:48:31 +0000789std::string Dbg::GetClassName(ObjPtr<mirror::Class> klass) {
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +0200790 if (klass == nullptr) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700791 return "null";
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +0200792 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700793 std::string temp;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200794 return DescriptorToName(klass->GetDescriptor(&temp));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700795}
796
Ian Rogersc0542af2014-09-03 16:16:56 -0700797JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId* class_object_id) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800798 JDWP::JdwpError status;
Vladimir Marko4617d582019-03-28 13:48:31 +0000799 ObjPtr<mirror::Class> c = DecodeClass(id, &status);
Ian Rogersc0542af2014-09-03 16:16:56 -0700800 if (c == nullptr) {
801 *class_object_id = 0;
Elliott Hughes436e3722012-02-17 20:01:47 -0800802 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800803 }
Ian Rogersc0542af2014-09-03 16:16:56 -0700804 *class_object_id = gRegistry->Add(c);
Elliott Hughes436e3722012-02-17 20:01:47 -0800805 return JDWP::ERR_NONE;
Elliott Hughes86964332012-02-15 19:37:42 -0800806}
807
Ian Rogersc0542af2014-09-03 16:16:56 -0700808JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId* superclass_id) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800809 JDWP::JdwpError status;
Vladimir Marko4617d582019-03-28 13:48:31 +0000810 ObjPtr<mirror::Class> c = DecodeClass(id, &status);
Ian Rogersc0542af2014-09-03 16:16:56 -0700811 if (c == nullptr) {
812 *superclass_id = 0;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800813 return status;
814 }
815 if (c->IsInterface()) {
816 // http://code.google.com/p/android/issues/detail?id=20856
Ian Rogersc0542af2014-09-03 16:16:56 -0700817 *superclass_id = 0;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800818 } else {
Ian Rogersc0542af2014-09-03 16:16:56 -0700819 *superclass_id = gRegistry->Add(c->GetSuperClass());
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800820 }
821 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700822}
823
Elliott Hughes436e3722012-02-17 20:01:47 -0800824JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700825 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +0000826 ObjPtr<mirror::Class> c = DecodeClass(id, &error);
Andreas Gampe7929a482015-12-30 19:33:49 -0800827 if (c == nullptr) {
828 return error;
Elliott Hughes436e3722012-02-17 20:01:47 -0800829 }
Andreas Gampe7929a482015-12-30 19:33:49 -0800830 expandBufAddObjectId(pReply, gRegistry->Add(c->GetClassLoader()));
Elliott Hughes436e3722012-02-17 20:01:47 -0800831 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700832}
833
Elliott Hughes436e3722012-02-17 20:01:47 -0800834JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700835 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +0000836 ObjPtr<mirror::Class> c = DecodeClass(id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -0700837 if (c == nullptr) {
838 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800839 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800840
841 uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
842
Yevgeny Roubande34eea2014-02-15 01:06:03 +0700843 // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set,
844 // not interfaces.
Elliott Hughes436e3722012-02-17 20:01:47 -0800845 // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
Yevgeny Roubande34eea2014-02-15 01:06:03 +0700846 if ((access_flags & kAccInterface) == 0) {
847 access_flags |= kAccSuper;
848 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800849
850 expandBufAdd4BE(pReply, access_flags);
851
852 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700853}
854
Ian Rogersc0542af2014-09-03 16:16:56 -0700855JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) {
856 JDWP::JdwpError error;
Vladimir Marko67f0e9c2019-03-29 14:00:12 +0000857 Thread* self = Thread::Current();
858 StackHandleScope<1u> hs(self);
Vladimir Marko83114892019-04-11 13:05:50 +0100859 Handle<mirror::Object> o = hs.NewHandle(gRegistry->Get<mirror::Object>(object_id, &error));
Ian Rogersc0542af2014-09-03 16:16:56 -0700860 if (o == nullptr) {
Elliott Hughesf327e072013-01-09 16:01:26 -0800861 return JDWP::ERR_INVALID_OBJECT;
862 }
863
864 // Ensure all threads are suspended while we read objects' lock words.
Sebastien Hertz54263242014-03-19 18:16:50 +0100865 CHECK_EQ(self->GetState(), kRunnable);
Elliott Hughesf327e072013-01-09 16:01:26 -0800866
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700867 MonitorInfo monitor_info;
868 {
869 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700870 ScopedSuspendAll ssa(__FUNCTION__);
Vladimir Marko67f0e9c2019-03-29 14:00:12 +0000871 monitor_info = MonitorInfo(o.Get());
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700872 }
Ian Rogersc0542af2014-09-03 16:16:56 -0700873 if (monitor_info.owner_ != nullptr) {
Nicolas Geoffraycafa0812017-02-15 18:27:34 +0000874 expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeerFromOtherThread()));
Elliott Hughesf327e072013-01-09 16:01:26 -0800875 } else {
Ian Rogersc0542af2014-09-03 16:16:56 -0700876 expandBufAddObjectId(reply, gRegistry->Add(nullptr));
Elliott Hughesf327e072013-01-09 16:01:26 -0800877 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700878 expandBufAdd4BE(reply, monitor_info.entry_count_);
879 expandBufAdd4BE(reply, monitor_info.waiters_.size());
880 for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) {
Nicolas Geoffraycafa0812017-02-15 18:27:34 +0000881 expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeerFromOtherThread()));
Elliott Hughesf327e072013-01-09 16:01:26 -0800882 }
883 return JDWP::ERR_NONE;
884}
885
Elliott Hughes734b8c62013-01-11 15:32:45 -0800886JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
Ian Rogersc0542af2014-09-03 16:16:56 -0700887 std::vector<JDWP::ObjectId>* monitors,
888 std::vector<uint32_t>* stack_depths) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800889 struct OwnedMonitorVisitor : public StackVisitor {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700890 OwnedMonitorVisitor(Thread* thread, Context* context,
Hiroshi Yamauchicc8c5c52014-06-13 15:08:05 -0700891 std::vector<JDWP::ObjectId>* monitor_vector,
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700892 std::vector<uint32_t>* stack_depth_vector)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700893 REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100894 : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
895 current_stack_depth(0),
896 monitors(monitor_vector),
897 stack_depths(stack_depth_vector) {}
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800898
899 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
900 // annotalysis.
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700901 bool VisitFrame() override NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800902 if (!GetMethod()->IsRuntimeMethod()) {
903 Monitor::VisitLocks(this, AppendOwnedMonitors, this);
Elliott Hughes734b8c62013-01-11 15:32:45 -0800904 ++current_stack_depth;
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800905 }
906 return true;
907 }
908
Vladimir Markof52d92f2019-03-29 12:33:02 +0000909 static void AppendOwnedMonitors(ObjPtr<mirror::Object> owned_monitor, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700910 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800911 OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
Hiroshi Yamauchicc8c5c52014-06-13 15:08:05 -0700912 visitor->monitors->push_back(gRegistry->Add(owned_monitor));
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700913 visitor->stack_depths->push_back(visitor->current_stack_depth);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800914 }
915
Elliott Hughes734b8c62013-01-11 15:32:45 -0800916 size_t current_stack_depth;
Ian Rogersc0542af2014-09-03 16:16:56 -0700917 std::vector<JDWP::ObjectId>* const monitors;
918 std::vector<uint32_t>* const stack_depths;
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800919 };
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800920
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700921 ScopedObjectAccessUnchecked soa(Thread::Current());
Sebastien Hertz69206392015-04-07 15:54:25 +0200922 JDWP::JdwpError error;
923 Thread* thread = DecodeThread(soa, thread_id, &error);
924 if (thread == nullptr) {
925 return error;
926 }
927 if (!IsSuspendedForDebugger(soa, thread)) {
928 return JDWP::ERR_THREAD_NOT_SUSPENDED;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700929 }
Hiroshi Yamauchicc8c5c52014-06-13 15:08:05 -0700930 std::unique_ptr<Context> context(Context::Create());
Ian Rogersc0542af2014-09-03 16:16:56 -0700931 OwnedMonitorVisitor visitor(thread, context.get(), monitors, stack_depths);
Hiroshi Yamauchicc8c5c52014-06-13 15:08:05 -0700932 visitor.WalkStack();
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800933 return JDWP::ERR_NONE;
934}
935
Sebastien Hertz52d131d2014-03-13 16:17:40 +0100936JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id,
Ian Rogersc0542af2014-09-03 16:16:56 -0700937 JDWP::ObjectId* contended_monitor) {
Elliott Hughesf9501702013-01-11 11:22:27 -0800938 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -0700939 *contended_monitor = 0;
Sebastien Hertz69206392015-04-07 15:54:25 +0200940 JDWP::JdwpError error;
941 Thread* thread = DecodeThread(soa, thread_id, &error);
942 if (thread == nullptr) {
943 return error;
Elliott Hughesf9501702013-01-11 11:22:27 -0800944 }
Sebastien Hertz69206392015-04-07 15:54:25 +0200945 if (!IsSuspendedForDebugger(soa, thread)) {
946 return JDWP::ERR_THREAD_NOT_SUSPENDED;
947 }
Vladimir Markof52d92f2019-03-29 12:33:02 +0000948 ObjPtr<mirror::Object> contended_monitor_obj = Monitor::GetContendedMonitor(thread);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700949 // Add() requires the thread_list_lock_ not held to avoid the lock
950 // level violation.
Ian Rogersc0542af2014-09-03 16:16:56 -0700951 *contended_monitor = gRegistry->Add(contended_monitor_obj);
Elliott Hughesf9501702013-01-11 11:22:27 -0800952 return JDWP::ERR_NONE;
953}
954
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800955JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
Ian Rogersc0542af2014-09-03 16:16:56 -0700956 std::vector<uint64_t>* counts) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800957 gc::Heap* heap = Runtime::Current()->GetHeap();
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700958 heap->CollectGarbage(/* clear_soft_references= */ false, gc::GcCause::kGcCauseDebugger);
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700959 VariableSizedHandleScope hs(Thread::Current());
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700960 std::vector<Handle<mirror::Class>> classes;
Ian Rogersc0542af2014-09-03 16:16:56 -0700961 counts->clear();
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800962 for (size_t i = 0; i < class_ids.size(); ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700963 JDWP::JdwpError error;
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700964 ObjPtr<mirror::Class> c = DecodeClass(class_ids[i], &error);
Ian Rogersc0542af2014-09-03 16:16:56 -0700965 if (c == nullptr) {
966 return error;
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800967 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700968 classes.push_back(hs.NewHandle(c));
Ian Rogersc0542af2014-09-03 16:16:56 -0700969 counts->push_back(0);
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800970 }
Ian Rogersc0542af2014-09-03 16:16:56 -0700971 heap->CountInstances(classes, false, &(*counts)[0]);
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800972 return JDWP::ERR_NONE;
973}
974
Ian Rogersc0542af2014-09-03 16:16:56 -0700975JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count,
976 std::vector<JDWP::ObjectId>* instances) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800977 gc::Heap* heap = Runtime::Current()->GetHeap();
978 // We only want reachable instances, so do a GC.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700979 heap->CollectGarbage(/* clear_soft_references= */ false, gc::GcCause::kGcCauseDebugger);
Ian Rogersc0542af2014-09-03 16:16:56 -0700980 JDWP::JdwpError error;
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700981 ObjPtr<mirror::Class> c = DecodeClass(class_id, &error);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800982 if (c == nullptr) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700983 return error;
Elliott Hughes3b78c942013-01-15 17:35:41 -0800984 }
Mathieu Chartier2d855952016-10-12 19:37:59 -0700985 VariableSizedHandleScope hs(Thread::Current());
986 std::vector<Handle<mirror::Object>> raw_instances;
Richard Uhler660be6f2017-11-22 16:12:29 +0000987 Runtime::Current()->GetHeap()->GetInstances(hs,
988 hs.NewHandle(c),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700989 /* use_is_assignable_from= */ false,
Richard Uhler660be6f2017-11-22 16:12:29 +0000990 max_count,
991 raw_instances);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800992 for (size_t i = 0; i < raw_instances.size(); ++i) {
Mathieu Chartier2d855952016-10-12 19:37:59 -0700993 instances->push_back(gRegistry->Add(raw_instances[i].Get()));
Elliott Hughes3b78c942013-01-15 17:35:41 -0800994 }
995 return JDWP::ERR_NONE;
996}
997
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800998JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
Ian Rogersc0542af2014-09-03 16:16:56 -0700999 std::vector<JDWP::ObjectId>* referring_objects) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -08001000 gc::Heap* heap = Runtime::Current()->GetHeap();
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001001 heap->CollectGarbage(/* clear_soft_references= */ false, gc::GcCause::kGcCauseDebugger);
Ian Rogersc0542af2014-09-03 16:16:56 -07001002 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01001003 ObjPtr<mirror::Object> o = gRegistry->Get<mirror::Object>(object_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001004 if (o == nullptr) {
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001005 return JDWP::ERR_INVALID_OBJECT;
1006 }
Mathieu Chartieraea9bfb2016-10-12 19:19:56 -07001007 VariableSizedHandleScope hs(Thread::Current());
1008 std::vector<Handle<mirror::Object>> raw_instances;
1009 heap->GetReferringObjects(hs, hs.NewHandle(o), max_count, raw_instances);
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001010 for (size_t i = 0; i < raw_instances.size(); ++i) {
Mathieu Chartieraea9bfb2016-10-12 19:19:56 -07001011 referring_objects->push_back(gRegistry->Add(raw_instances[i].Get()));
Elliott Hughes0cbaff52013-01-16 15:28:01 -08001012 }
1013 return JDWP::ERR_NONE;
1014}
1015
Ian Rogersc0542af2014-09-03 16:16:56 -07001016JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id) {
1017 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01001018 ObjPtr<mirror::Object> o = gRegistry->Get<mirror::Object>(object_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001019 if (o == nullptr) {
Sebastien Hertze96060a2013-12-11 12:06:28 +01001020 return JDWP::ERR_INVALID_OBJECT;
1021 }
Elliott Hughes64f574f2013-02-20 14:57:12 -08001022 gRegistry->DisableCollection(object_id);
1023 return JDWP::ERR_NONE;
1024}
1025
Ian Rogersc0542af2014-09-03 16:16:56 -07001026JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id) {
1027 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01001028 ObjPtr<mirror::Object> o = gRegistry->Get<mirror::Object>(object_id, &error);
Sebastien Hertze96060a2013-12-11 12:06:28 +01001029 // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI
1030 // also ignores these cases and never return an error. However it's not obvious why this command
1031 // should behave differently from DisableCollection and IsCollected commands. So let's be more
1032 // strict and return an error if this happens.
Ian Rogersc0542af2014-09-03 16:16:56 -07001033 if (o == nullptr) {
Sebastien Hertze96060a2013-12-11 12:06:28 +01001034 return JDWP::ERR_INVALID_OBJECT;
1035 }
Elliott Hughes64f574f2013-02-20 14:57:12 -08001036 gRegistry->EnableCollection(object_id);
1037 return JDWP::ERR_NONE;
1038}
1039
Ian Rogersc0542af2014-09-03 16:16:56 -07001040JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool* is_collected) {
1041 *is_collected = true;
Sebastien Hertz65637eb2014-01-10 17:40:02 +01001042 if (object_id == 0) {
1043 // Null object id is invalid.
Sebastien Hertze96060a2013-12-11 12:06:28 +01001044 return JDWP::ERR_INVALID_OBJECT;
1045 }
Sebastien Hertz65637eb2014-01-10 17:40:02 +01001046 // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However
1047 // the RI seems to ignore this and assume object has been collected.
Ian Rogersc0542af2014-09-03 16:16:56 -07001048 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01001049 ObjPtr<mirror::Object> o = gRegistry->Get<mirror::Object>(object_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001050 if (o != nullptr) {
1051 *is_collected = gRegistry->IsCollected(object_id);
Sebastien Hertz65637eb2014-01-10 17:40:02 +01001052 }
Elliott Hughes64f574f2013-02-20 14:57:12 -08001053 return JDWP::ERR_NONE;
1054}
1055
Ian Rogersc0542af2014-09-03 16:16:56 -07001056void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) {
Elliott Hughes64f574f2013-02-20 14:57:12 -08001057 gRegistry->DisposeObject(object_id, reference_count);
1058}
1059
Mathieu Chartier3398c782016-09-30 10:27:43 -07001060JDWP::JdwpTypeTag Dbg::GetTypeTag(ObjPtr<mirror::Class> klass) {
Sebastien Hertz4d8fd492014-03-28 16:29:41 +01001061 DCHECK(klass != nullptr);
1062 if (klass->IsArrayClass()) {
1063 return JDWP::TT_ARRAY;
1064 } else if (klass->IsInterface()) {
1065 return JDWP::TT_INTERFACE;
1066 } else {
1067 return JDWP::TT_CLASS;
1068 }
1069}
1070
Elliott Hughes88d63092013-01-09 09:55:54 -08001071JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001072 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001073 ObjPtr<mirror::Class> c = DecodeClass(class_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001074 if (c == nullptr) {
1075 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001076 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001077
Sebastien Hertz4d8fd492014-03-28 16:29:41 +01001078 JDWP::JdwpTypeTag type_tag = GetTypeTag(c);
1079 expandBufAdd1(pReply, type_tag);
Elliott Hughes88d63092013-01-09 09:55:54 -08001080 expandBufAddRefTypeId(pReply, class_id);
Elliott Hughes436e3722012-02-17 20:01:47 -08001081 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001082}
1083
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001084// Get the complete list of reference classes (i.e. all classes except
1085// the primitive types).
1086// Returns a newly-allocated buffer full of RefTypeId values.
1087class ClassListCreator : public ClassVisitor {
1088 public:
1089 explicit ClassListCreator(std::vector<JDWP::RefTypeId>* classes) : classes_(classes) {}
1090
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001091 bool operator()(ObjPtr<mirror::Class> c) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001092 if (!c->IsPrimitive()) {
1093 classes_->push_back(Dbg::GetObjectRegistry()->AddRefType(c));
1094 }
1095 return true;
1096 }
1097
1098 private:
1099 std::vector<JDWP::RefTypeId>* const classes_;
1100};
1101
Ian Rogersc0542af2014-09-03 16:16:56 -07001102void Dbg::GetClassList(std::vector<JDWP::RefTypeId>* classes) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001103 ClassListCreator clc(classes);
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001104 Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(&clc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001105}
1106
Ian Rogers1ff3c982014-08-12 02:30:58 -07001107JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag,
1108 uint32_t* pStatus, std::string* pDescriptor) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001109 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001110 ObjPtr<mirror::Class> c = DecodeClass(class_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001111 if (c == nullptr) {
1112 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001113 }
1114
Elliott Hughesa2155262011-11-16 16:26:58 -08001115 if (c->IsArrayClass()) {
1116 *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
1117 *pTypeTag = JDWP::TT_ARRAY;
1118 } else {
1119 if (c->IsErroneous()) {
1120 *pStatus = JDWP::CS_ERROR;
1121 } else {
1122 *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
1123 }
1124 *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1125 }
1126
Ian Rogersc0542af2014-09-03 16:16:56 -07001127 if (pDescriptor != nullptr) {
Ian Rogers1ff3c982014-08-12 02:30:58 -07001128 std::string temp;
1129 *pDescriptor = c->GetDescriptor(&temp);
Elliott Hughesa2155262011-11-16 16:26:58 -08001130 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001131 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001132}
1133
Ian Rogersc0542af2014-09-03 16:16:56 -07001134void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>* ids) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -07001135 std::vector<ObjPtr<mirror::Class>> classes;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001136 Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
Ian Rogersc0542af2014-09-03 16:16:56 -07001137 ids->clear();
Mathieu Chartier28357fa2016-10-18 16:27:40 -07001138 for (ObjPtr<mirror::Class> c : classes) {
1139 ids->push_back(gRegistry->Add(c));
Elliott Hughes6fa602d2011-12-02 17:54:25 -08001140 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001141}
1142
Ian Rogersc0542af2014-09-03 16:16:56 -07001143JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) {
1144 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01001145 ObjPtr<mirror::Object> o = gRegistry->Get<mirror::Object>(object_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001146 if (o == nullptr) {
Elliott Hughes2435a572012-02-17 16:07:41 -08001147 return JDWP::ERR_INVALID_OBJECT;
Elliott Hughes499c5132011-11-17 14:55:11 -08001148 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001149
Sebastien Hertz4d8fd492014-03-28 16:29:41 +01001150 JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass());
Elliott Hughes64f574f2013-02-20 14:57:12 -08001151 JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
Elliott Hughes2435a572012-02-17 16:07:41 -08001152
1153 expandBufAdd1(pReply, type_tag);
1154 expandBufAddRefTypeId(pReply, type_id);
1155
1156 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001157}
1158
Ian Rogersfc0e94b2013-09-23 23:51:32 -07001159JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001160 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001161 ObjPtr<mirror::Class> c = DecodeClass(class_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001162 if (c == nullptr) {
1163 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001164 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001165 std::string temp;
1166 *signature = c->GetDescriptor(&temp);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -08001167 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001168}
1169
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001170JDWP::JdwpError Dbg::GetSourceDebugExtension(JDWP::RefTypeId class_id,
1171 std::string* extension_data) {
1172 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001173 ObjPtr<mirror::Class> c = DecodeClass(class_id, &error);
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001174 if (c == nullptr) {
1175 return error;
1176 }
1177 StackHandleScope<1> hs(Thread::Current());
1178 Handle<mirror::Class> klass(hs.NewHandle(c));
1179 const char* data = annotations::GetSourceDebugExtension(klass);
1180 if (data == nullptr) {
1181 return JDWP::ERR_ABSENT_INFORMATION;
1182 }
1183 *extension_data = data;
1184 return JDWP::ERR_NONE;
1185}
1186
Ian Rogersc0542af2014-09-03 16:16:56 -07001187JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string* result) {
1188 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001189 ObjPtr<mirror::Class> c = DecodeClass(class_id, &error);
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001190 if (c == nullptr) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001191 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001192 }
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001193 const char* source_file = c->GetSourceFile();
1194 if (source_file == nullptr) {
Sebastien Hertzb7054ba2014-03-13 11:52:31 +01001195 return JDWP::ERR_ABSENT_INFORMATION;
1196 }
Ian Rogersc0542af2014-09-03 16:16:56 -07001197 *result = source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -08001198 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001199}
1200
Ian Rogersc0542af2014-09-03 16:16:56 -07001201JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t* tag) {
Ian Rogers98379392014-02-24 16:53:16 -08001202 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07001203 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01001204 ObjPtr<mirror::Object> o = gRegistry->Get<mirror::Object>(object_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001205 if (error != JDWP::ERR_NONE) {
1206 *tag = JDWP::JT_VOID;
1207 return error;
Elliott Hughes546b9862012-06-20 16:06:13 -07001208 }
Ian Rogersc0542af2014-09-03 16:16:56 -07001209 *tag = TagFromObject(soa, o);
Elliott Hughes546b9862012-06-20 16:06:13 -07001210 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001211}
1212
Elliott Hughesaed4be92011-12-02 16:16:23 -08001213size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001214 switch (tag) {
1215 case JDWP::JT_VOID:
1216 return 0;
1217 case JDWP::JT_BYTE:
1218 case JDWP::JT_BOOLEAN:
1219 return 1;
1220 case JDWP::JT_CHAR:
1221 case JDWP::JT_SHORT:
1222 return 2;
1223 case JDWP::JT_FLOAT:
1224 case JDWP::JT_INT:
1225 return 4;
1226 case JDWP::JT_ARRAY:
1227 case JDWP::JT_OBJECT:
1228 case JDWP::JT_STRING:
1229 case JDWP::JT_THREAD:
1230 case JDWP::JT_THREAD_GROUP:
1231 case JDWP::JT_CLASS_LOADER:
1232 case JDWP::JT_CLASS_OBJECT:
1233 return sizeof(JDWP::ObjectId);
1234 case JDWP::JT_DOUBLE:
1235 case JDWP::JT_LONG:
1236 return 8;
1237 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08001238 LOG(FATAL) << "Unknown tag " << tag;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001239 UNREACHABLE();
Elliott Hughesdbb40792011-11-18 17:05:22 -08001240 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001241}
1242
Ian Rogersc0542af2014-09-03 16:16:56 -07001243JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int32_t* length) {
1244 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001245 ObjPtr<mirror::Array> a = DecodeNonNullArray(array_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001246 if (a == nullptr) {
1247 return error;
Elliott Hughes24437992011-11-30 14:49:33 -08001248 }
Ian Rogersc0542af2014-09-03 16:16:56 -07001249 *length = a->GetLength();
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001250 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001251}
1252
Vladimir Marko4617d582019-03-28 13:48:31 +00001253JDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id,
1254 int offset,
1255 int count,
1256 JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001257 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001258 ObjPtr<mirror::Array> a = DecodeNonNullArray(array_id, &error);
Ian Rogers98379392014-02-24 16:53:16 -08001259 if (a == nullptr) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001260 return error;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001261 }
Elliott Hughes24437992011-11-30 14:49:33 -08001262
1263 if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
1264 LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001265 return JDWP::ERR_INVALID_LENGTH;
Elliott Hughes24437992011-11-30 14:49:33 -08001266 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001267 JDWP::JdwpTag element_tag = BasicTagFromClass(a->GetClass()->GetComponentType());
1268 expandBufAdd1(pReply, element_tag);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001269 expandBufAdd4BE(pReply, count);
1270
Ian Rogers1ff3c982014-08-12 02:30:58 -07001271 if (IsPrimitiveTag(element_tag)) {
1272 size_t width = GetTagWidth(element_tag);
Elliott Hughes24437992011-11-30 14:49:33 -08001273 uint8_t* dst = expandBufAddSpace(pReply, count * width);
1274 if (width == 8) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001275 const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t), 0));
Elliott Hughes24437992011-11-30 14:49:33 -08001276 for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
1277 } else if (width == 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001278 const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t), 0));
Elliott Hughes24437992011-11-30 14:49:33 -08001279 for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
1280 } else if (width == 2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001281 const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t), 0));
Elliott Hughes24437992011-11-30 14:49:33 -08001282 for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
1283 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001284 const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t), 0));
Elliott Hughes24437992011-11-30 14:49:33 -08001285 memcpy(dst, &src[offset * width], count * width);
1286 }
1287 } else {
Ian Rogers98379392014-02-24 16:53:16 -08001288 ScopedObjectAccessUnchecked soa(Thread::Current());
Vladimir Marko4617d582019-03-28 13:48:31 +00001289 ObjPtr<mirror::ObjectArray<mirror::Object>> oa = a->AsObjectArray<mirror::Object>();
Elliott Hughes24437992011-11-30 14:49:33 -08001290 for (int i = 0; i < count; ++i) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00001291 ObjPtr<mirror::Object> element = oa->Get(offset + i);
Ian Rogers98379392014-02-24 16:53:16 -08001292 JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element)
Ian Rogers1ff3c982014-08-12 02:30:58 -07001293 : element_tag;
Elliott Hughes24437992011-11-30 14:49:33 -08001294 expandBufAdd1(pReply, specific_tag);
1295 expandBufAddObjectId(pReply, gRegistry->Add(element));
1296 }
1297 }
1298
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001299 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001300}
1301
Ian Rogersef7d42f2014-01-06 12:55:46 -08001302template <typename T>
Vladimir Marko4617d582019-03-28 13:48:31 +00001303static void CopyArrayData(ObjPtr<mirror::Array> a, JDWP::Request* src, int offset, int count)
Ian Rogersef7d42f2014-01-06 12:55:46 -08001304 NO_THREAD_SAFETY_ANALYSIS {
1305 // TODO: fix when annotalysis correctly handles non-member functions.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001306 DCHECK(a->GetClass()->IsPrimitiveArray());
1307
Ian Rogersef7d42f2014-01-06 12:55:46 -08001308 T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset));
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001309 for (int i = 0; i < count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001310 *dst++ = src->ReadValue(sizeof(T));
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001311 }
1312}
1313
Elliott Hughes88d63092013-01-09 09:55:54 -08001314JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
Ian Rogersc0542af2014-09-03 16:16:56 -07001315 JDWP::Request* request) {
1316 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001317 ObjPtr<mirror::Array> dst = DecodeNonNullArray(array_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001318 if (dst == nullptr) {
1319 return error;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001320 }
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001321
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001322 if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001323 LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001324 return JDWP::ERR_INVALID_LENGTH;
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001325 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001326 JDWP::JdwpTag element_tag = BasicTagFromClass(dst->GetClass()->GetComponentType());
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001327
Ian Rogers1ff3c982014-08-12 02:30:58 -07001328 if (IsPrimitiveTag(element_tag)) {
1329 size_t width = GetTagWidth(element_tag);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001330 if (width == 8) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001331 CopyArrayData<uint64_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001332 } else if (width == 4) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001333 CopyArrayData<uint32_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001334 } else if (width == 2) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001335 CopyArrayData<uint16_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001336 } else {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001337 CopyArrayData<uint8_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001338 }
1339 } else {
Vladimir Marko4617d582019-03-28 13:48:31 +00001340 ObjPtr<mirror::ObjectArray<mirror::Object>> oa = dst->AsObjectArray<mirror::Object>();
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001341 for (int i = 0; i < count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001342 JDWP::ObjectId id = request->ReadObjectId();
Vladimir Marko83114892019-04-11 13:05:50 +01001343 ObjPtr<mirror::Object> o = gRegistry->Get<mirror::Object>(id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001344 if (error != JDWP::ERR_NONE) {
1345 return error;
Elliott Hughes436e3722012-02-17 20:01:47 -08001346 }
Sebastien Hertz2e1c16d2015-08-28 11:57:49 +02001347 // Check if the object's type is compatible with the array's type.
1348 if (o != nullptr && !o->InstanceOf(oa->GetClass()->GetComponentType())) {
1349 return JDWP::ERR_TYPE_MISMATCH;
1350 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001351 oa->Set<false>(offset + i, o);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001352 }
1353 }
1354
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001355 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001356}
1357
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001358JDWP::JdwpError Dbg::CreateString(const std::string& str, JDWP::ObjectId* new_string_id) {
1359 Thread* self = Thread::Current();
Vladimir Marko179b7c62019-03-22 13:38:57 +00001360 ObjPtr<mirror::String> new_string = mirror::String::AllocFromModifiedUtf8(self, str.c_str());
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001361 if (new_string == nullptr) {
1362 DCHECK(self->IsExceptionPending());
1363 self->ClearException();
1364 LOG(ERROR) << "Could not allocate string";
1365 *new_string_id = 0;
1366 return JDWP::ERR_OUT_OF_MEMORY;
1367 }
1368 *new_string_id = gRegistry->Add(new_string);
1369 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001370}
1371
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001372JDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId* new_object_id) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001373 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001374 ObjPtr<mirror::Class> c = DecodeClass(class_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001375 if (c == nullptr) {
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001376 *new_object_id = 0;
Ian Rogersc0542af2014-09-03 16:16:56 -07001377 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001378 }
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001379 Thread* self = Thread::Current();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001380 ObjPtr<mirror::Object> new_object;
Sebastien Hertz56d5e502015-11-03 17:38:35 +01001381 if (c->IsStringClass()) {
1382 // Special case for java.lang.String.
1383 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Vladimir Marko9b81ac32019-05-16 16:47:08 +01001384 new_object = mirror::String::AllocEmptyString(self, allocator_type);
Sebastien Hertz56d5e502015-11-03 17:38:35 +01001385 } else {
1386 new_object = c->AllocObject(self);
1387 }
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001388 if (new_object == nullptr) {
1389 DCHECK(self->IsExceptionPending());
1390 self->ClearException();
David Sehr709b0702016-10-13 09:12:37 -07001391 LOG(ERROR) << "Could not allocate object of type " << mirror::Class::PrettyDescriptor(c);
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001392 *new_object_id = 0;
1393 return JDWP::ERR_OUT_OF_MEMORY;
1394 }
Vladimir Markobcf17522018-06-01 13:14:32 +01001395 *new_object_id = gRegistry->Add(new_object);
Elliott Hughes436e3722012-02-17 20:01:47 -08001396 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001397}
1398
Elliott Hughesbf13d362011-12-08 15:51:37 -08001399/*
1400 * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1401 */
Elliott Hughes88d63092013-01-09 09:55:54 -08001402JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001403 JDWP::ObjectId* new_array_id) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001404 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001405 ObjPtr<mirror::Class> c = DecodeClass(array_class_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001406 if (c == nullptr) {
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001407 *new_array_id = 0;
Ian Rogersc0542af2014-09-03 16:16:56 -07001408 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001409 }
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001410 Thread* self = Thread::Current();
Vladimir Markobcf17522018-06-01 13:14:32 +01001411 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
1412 ObjPtr<mirror::Array> new_array =
Vladimir Marko9b81ac32019-05-16 16:47:08 +01001413 mirror::Array::Alloc(self, c, length, c->GetComponentSizeShift(), allocator_type);
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001414 if (new_array == nullptr) {
1415 DCHECK(self->IsExceptionPending());
1416 self->ClearException();
David Sehr709b0702016-10-13 09:12:37 -07001417 LOG(ERROR) << "Could not allocate array of type " << mirror::Class::PrettyDescriptor(c);
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001418 *new_array_id = 0;
1419 return JDWP::ERR_OUT_OF_MEMORY;
1420 }
1421 *new_array_id = gRegistry->Add(new_array);
Elliott Hughes436e3722012-02-17 20:01:47 -08001422 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001423}
1424
Mathieu Chartierc7853442015-03-27 14:35:38 -07001425JDWP::FieldId Dbg::ToFieldId(const ArtField* f) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001426 return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
Elliott Hughes03181a82011-11-17 17:22:21 -08001427}
1428
Alex Light6c8467f2015-11-20 15:03:26 -08001429static JDWP::MethodId ToMethodId(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001430 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light97e78032017-06-27 17:51:55 -07001431 return static_cast<JDWP::MethodId>(
1432 reinterpret_cast<uintptr_t>(m->GetCanonicalMethod(kRuntimePointerSize)));
Elliott Hughes03181a82011-11-17 17:22:21 -08001433}
1434
Mathieu Chartierc7853442015-03-27 14:35:38 -07001435static ArtField* FromFieldId(JDWP::FieldId fid)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001436 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001437 return reinterpret_cast<ArtField*>(static_cast<uintptr_t>(fid));
Elliott Hughesaed4be92011-12-02 16:16:23 -08001438}
1439
Mathieu Chartiere401d142015-04-22 13:56:20 -07001440static ArtMethod* FromMethodId(JDWP::MethodId mid)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001441 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001442 return reinterpret_cast<ArtMethod*>(static_cast<uintptr_t>(mid));
Elliott Hughes03181a82011-11-17 17:22:21 -08001443}
1444
Sebastien Hertz6995c602014-09-09 12:10:13 +02001445bool Dbg::MatchThread(JDWP::ObjectId expected_thread_id, Thread* event_thread) {
1446 CHECK(event_thread != nullptr);
1447 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01001448 ObjPtr<mirror::Object> expected_thread_peer = gRegistry->Get<mirror::Object>(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001449 expected_thread_id, &error);
Nicolas Geoffraycafa0812017-02-15 18:27:34 +00001450 return expected_thread_peer == event_thread->GetPeerFromOtherThread();
Sebastien Hertz6995c602014-09-09 12:10:13 +02001451}
1452
1453bool Dbg::MatchLocation(const JDWP::JdwpLocation& expected_location,
1454 const JDWP::EventLocation& event_location) {
1455 if (expected_location.dex_pc != event_location.dex_pc) {
1456 return false;
1457 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001458 ArtMethod* m = FromMethodId(expected_location.method_id);
Sebastien Hertz6995c602014-09-09 12:10:13 +02001459 return m == event_location.method;
1460}
1461
Mathieu Chartier3398c782016-09-30 10:27:43 -07001462bool Dbg::MatchType(ObjPtr<mirror::Class> event_class, JDWP::RefTypeId class_id) {
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001463 if (event_class == nullptr) {
1464 return false;
1465 }
Sebastien Hertz6995c602014-09-09 12:10:13 +02001466 JDWP::JdwpError error;
Mathieu Chartier3398c782016-09-30 10:27:43 -07001467 ObjPtr<mirror::Class> expected_class = DecodeClass(class_id, &error);
Sebastien Hertz6995c602014-09-09 12:10:13 +02001468 CHECK(expected_class != nullptr);
1469 return expected_class->IsAssignableFrom(event_class);
1470}
1471
Vladimir Marko83114892019-04-11 13:05:50 +01001472bool Dbg::MatchField(JDWP::RefTypeId expected_type_id,
1473 JDWP::FieldId expected_field_id,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001474 ArtField* event_field) {
1475 ArtField* expected_field = FromFieldId(expected_field_id);
Sebastien Hertz6995c602014-09-09 12:10:13 +02001476 if (expected_field != event_field) {
1477 return false;
1478 }
1479 return Dbg::MatchType(event_field->GetDeclaringClass(), expected_type_id);
1480}
1481
Vladimir Marko83114892019-04-11 13:05:50 +01001482bool Dbg::MatchInstance(JDWP::ObjectId expected_instance_id,
1483 ObjPtr<mirror::Object> event_instance) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001484 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01001485 ObjPtr<mirror::Object> modifier_instance =
1486 gRegistry->Get<mirror::Object>(expected_instance_id, &error);
Sebastien Hertz6995c602014-09-09 12:10:13 +02001487 return modifier_instance == event_instance;
1488}
1489
Mathieu Chartier90443472015-07-16 20:32:27 -07001490void Dbg::SetJdwpLocation(JDWP::JdwpLocation* location, ArtMethod* m, uint32_t dex_pc) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001491 if (m == nullptr) {
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001492 memset(location, 0, sizeof(*location));
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001493 } else {
Vladimir Markod93e3742018-07-18 10:58:13 +01001494 ObjPtr<mirror::Class> c = m->GetDeclaringClass();
Ian Rogersc0542af2014-09-03 16:16:56 -07001495 location->type_tag = GetTypeTag(c);
1496 location->class_id = gRegistry->AddRefType(c);
Alex Light73376312017-04-06 10:10:51 -07001497 // The RI Seems to return 0 for all obsolete methods. For compatibility we shall do the same.
1498 location->method_id = m->IsObsolete() ? 0 : ToMethodId(m);
Ian Rogersc0542af2014-09-03 16:16:56 -07001499 location->dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint64_t>(-1) : dex_pc;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001500 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08001501}
1502
Ian Rogersc0542af2014-09-03 16:16:56 -07001503std::string Dbg::GetMethodName(JDWP::MethodId method_id) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001504 ArtMethod* m = FromMethodId(method_id);
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001505 if (m == nullptr) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001506 return "null";
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001507 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001508 return m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetName();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001509}
1510
Alex Light73376312017-04-06 10:10:51 -07001511bool Dbg::IsMethodObsolete(JDWP::MethodId method_id) {
1512 ArtMethod* m = FromMethodId(method_id);
1513 if (m == nullptr) {
1514 // NB Since we return 0 as MID for obsolete methods we want to default to true here.
1515 return true;
1516 }
1517 return m->IsObsolete();
1518}
1519
Ian Rogersc0542af2014-09-03 16:16:56 -07001520std::string Dbg::GetFieldName(JDWP::FieldId field_id) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001521 ArtField* f = FromFieldId(field_id);
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001522 if (f == nullptr) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001523 return "null";
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001524 }
1525 return f->GetName();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001526}
1527
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001528/*
1529 * Augment the access flags for synthetic methods and fields by setting
1530 * the (as described by the spec) "0xf0000000 bit". Also, strip out any
1531 * flags not specified by the Java programming language.
1532 */
1533static uint32_t MangleAccessFlags(uint32_t accessFlags) {
1534 accessFlags &= kAccJavaFlagsMask;
1535 if ((accessFlags & kAccSynthetic) != 0) {
1536 accessFlags |= 0xf0000000;
1537 }
1538 return accessFlags;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001539}
1540
Elliott Hughesdbb40792011-11-18 17:05:22 -08001541/*
Jeff Haob7cefc72013-11-14 14:51:09 -08001542 * Circularly shifts registers so that arguments come first. Debuggers
1543 * expect slots to begin with arguments, but dex code places them at
1544 * the end.
Elliott Hughesdbb40792011-11-18 17:05:22 -08001545 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07001546static uint16_t MangleSlot(uint16_t slot, ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001547 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr0225f8e2018-01-31 08:52:24 +00001548 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001549 if (!accessor.HasCodeItem()) {
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001550 // We should not get here for a method without code (native, proxy or abstract). Log it and
1551 // return the slot as is since all registers are arguments.
David Sehr709b0702016-10-13 09:12:37 -07001552 LOG(WARNING) << "Trying to mangle slot for method without code " << m->PrettyMethod();
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001553 return slot;
1554 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001555 uint16_t ins_size = accessor.InsSize();
1556 uint16_t locals_size = accessor.RegistersSize() - ins_size;
Jeff Haob7cefc72013-11-14 14:51:09 -08001557 if (slot >= locals_size) {
1558 return slot - locals_size;
1559 } else {
1560 return slot + ins_size;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001561 }
Elliott Hughesdbb40792011-11-18 17:05:22 -08001562}
1563
Sebastien Hertzaef0c912016-08-08 10:20:28 +02001564static size_t GetMethodNumArgRegistersIncludingThis(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001565 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzaef0c912016-08-08 10:20:28 +02001566 uint32_t num_registers = ArtMethod::NumArgRegisters(method->GetShorty());
1567 if (!method->IsStatic()) {
1568 ++num_registers;
1569 }
1570 return num_registers;
1571}
1572
Jeff Haob7cefc72013-11-14 14:51:09 -08001573/*
1574 * Circularly shifts registers so that arguments come last. Reverts
1575 * slots to dex style argument placement.
1576 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07001577static uint16_t DemangleSlot(uint16_t slot, ArtMethod* m, JDWP::JdwpError* error)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001578 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr0225f8e2018-01-31 08:52:24 +00001579 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001580 if (!accessor.HasCodeItem()) {
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001581 // We should not get here for a method without code (native, proxy or abstract). Log it and
1582 // return the slot as is since all registers are arguments.
David Sehr709b0702016-10-13 09:12:37 -07001583 LOG(WARNING) << "Trying to demangle slot for method without code "
1584 << m->PrettyMethod();
Sebastien Hertzaef0c912016-08-08 10:20:28 +02001585 uint16_t vreg_count = GetMethodNumArgRegistersIncludingThis(m);
Sebastien Hertzabbabc82015-03-26 08:47:47 +01001586 if (slot < vreg_count) {
1587 *error = JDWP::ERR_NONE;
1588 return slot;
1589 }
Jeff Haob7cefc72013-11-14 14:51:09 -08001590 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001591 if (slot < accessor.RegistersSize()) {
1592 uint16_t ins_size = accessor.InsSize();
1593 uint16_t locals_size = accessor.RegistersSize() - ins_size;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01001594 *error = JDWP::ERR_NONE;
1595 return (slot < ins_size) ? slot + locals_size : slot - ins_size;
1596 }
Elliott Hughesdbb40792011-11-18 17:05:22 -08001597 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01001598
1599 // Slot is invalid in the method.
David Sehr709b0702016-10-13 09:12:37 -07001600 LOG(ERROR) << "Invalid local slot " << slot << " for method " << m->PrettyMethod();
Sebastien Hertzabbabc82015-03-26 08:47:47 +01001601 *error = JDWP::ERR_INVALID_SLOT;
1602 return DexFile::kDexNoIndex16;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001603}
1604
Mathieu Chartier90443472015-07-16 20:32:27 -07001605JDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic,
1606 JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001607 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001608 ObjPtr<mirror::Class> c = DecodeClass(class_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001609 if (c == nullptr) {
1610 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001611 }
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001612
1613 size_t instance_field_count = c->NumInstanceFields();
1614 size_t static_field_count = c->NumStaticFields();
1615
1616 expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1617
1618 for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
Mathieu Chartier90443472015-07-16 20:32:27 -07001619 ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) :
1620 c->GetStaticField(i - instance_field_count);
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001621 expandBufAddFieldId(pReply, ToFieldId(f));
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001622 expandBufAddUtf8String(pReply, f->GetName());
1623 expandBufAddUtf8String(pReply, f->GetTypeDescriptor());
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001624 if (with_generic) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001625 static const char genericSignature[1] = "";
1626 expandBufAddUtf8String(pReply, genericSignature);
1627 }
1628 expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1629 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001630 return JDWP::ERR_NONE;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001631}
1632
Elliott Hughes88d63092013-01-09 09:55:54 -08001633JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001634 JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001635 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001636 ObjPtr<mirror::Class> c = DecodeClass(class_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001637 if (c == nullptr) {
1638 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001639 }
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001640
Alex Light51a64d52015-12-17 13:55:59 -08001641 expandBufAdd4BE(pReply, c->NumMethods());
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001642
Mathieu Chartiere401d142015-04-22 13:56:20 -07001643 auto* cl = Runtime::Current()->GetClassLinker();
1644 auto ptr_size = cl->GetImagePointerSize();
Alex Light51a64d52015-12-17 13:55:59 -08001645 for (ArtMethod& m : c->GetMethods(ptr_size)) {
1646 expandBufAddMethodId(pReply, ToMethodId(&m));
Andreas Gampe542451c2016-07-26 09:02:02 -07001647 expandBufAddUtf8String(pReply, m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetName());
1648 expandBufAddUtf8String(
1649 pReply, m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetSignature().ToString());
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001650 if (with_generic) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001651 const char* generic_signature = "";
1652 expandBufAddUtf8String(pReply, generic_signature);
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001653 }
Alex Light51a64d52015-12-17 13:55:59 -08001654 expandBufAdd4BE(pReply, MangleAccessFlags(m.GetAccessFlags()));
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001655 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001656 return JDWP::ERR_NONE;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001657}
1658
Elliott Hughes88d63092013-01-09 09:55:54 -08001659JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001660 JDWP::JdwpError error;
Mathieu Chartierf8322842014-05-16 10:59:25 -07001661 Thread* self = Thread::Current();
Vladimir Marko19a4d372016-12-08 14:41:46 +00001662 ObjPtr<mirror::Class> c = DecodeClass(class_id, &error);
1663 if (c == nullptr) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001664 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001665 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001666 size_t interface_count = c->NumDirectInterfaces();
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001667 expandBufAdd4BE(pReply, interface_count);
1668 for (size_t i = 0; i < interface_count; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001669 ObjPtr<mirror::Class> interface = mirror::Class::GetDirectInterface(self, c, i);
1670 DCHECK(interface != nullptr);
1671 expandBufAddRefTypeId(pReply, gRegistry->AddRefType(interface));
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001672 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001673 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001674}
1675
Ian Rogersc0542af2014-09-03 16:16:56 -07001676void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001677 ArtMethod* m = FromMethodId(method_id);
David Sehr0225f8e2018-01-31 08:52:24 +00001678 CodeItemDebugInfoAccessor accessor(m->DexInstructionDebugInfo());
Elliott Hughes03181a82011-11-17 17:22:21 -08001679 uint64_t start, end;
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -08001680 if (!accessor.HasCodeItem()) {
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001681 DCHECK(m->IsNative() || m->IsProxyMethod());
Elliott Hughes03181a82011-11-17 17:22:21 -08001682 start = -1;
1683 end = -1;
1684 } else {
1685 start = 0;
jeffhao14f0db92012-12-14 17:50:42 -08001686 // Return the index of the last instruction
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -08001687 end = accessor.InsnsSizeInCodeUnits() - 1;
Elliott Hughes03181a82011-11-17 17:22:21 -08001688 }
1689
1690 expandBufAdd8BE(pReply, start);
1691 expandBufAdd8BE(pReply, end);
1692
1693 // Add numLines later
1694 size_t numLinesOffset = expandBufGetLength(pReply);
1695 expandBufAdd4BE(pReply, 0);
1696
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001697 int numItems = 0;
1698 accessor.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
1699 expandBufAdd8BE(pReply, entry.address_);
1700 expandBufAdd4BE(pReply, entry.line_);
1701 numItems++;
1702 return false;
1703 });
Elliott Hughes03181a82011-11-17 17:22:21 -08001704
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001705 JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, numItems);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001706}
1707
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001708void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic,
1709 JDWP::ExpandBuf* pReply) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001710 ArtMethod* m = FromMethodId(method_id);
David Sehr0225f8e2018-01-31 08:52:24 +00001711 CodeItemDebugInfoAccessor accessor(m->DexInstructionDebugInfo());
Elliott Hughesdbb40792011-11-18 17:05:22 -08001712
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001713 // arg_count considers doubles and longs to take 2 units.
1714 // variable_count considers everything to take 1 unit.
Sebastien Hertzaef0c912016-08-08 10:20:28 +02001715 expandBufAdd4BE(pReply, GetMethodNumArgRegistersIncludingThis(m));
Elliott Hughesdbb40792011-11-18 17:05:22 -08001716
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001717 // We don't know the total number of variables yet, so leave a blank and update it later.
1718 size_t variable_count_offset = expandBufGetLength(pReply);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001719 expandBufAdd4BE(pReply, 0);
1720
Mathieu Chartiere5afbf32018-09-12 17:51:54 -07001721 size_t variable_count = 0;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001722
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -08001723 if (accessor.HasCodeItem()) {
Mathieu Chartiere5afbf32018-09-12 17:51:54 -07001724 accessor.DecodeDebugLocalInfo(m->IsStatic(),
1725 m->GetDexMethodIndex(),
1726 [&](const DexFile::LocalInfo& entry)
1727 REQUIRES_SHARED(Locks::mutator_lock_) {
1728 uint16_t slot = entry.reg_;
1729 VLOG(jdwp) << StringPrintf(" %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d",
1730 variable_count,
1731 entry.start_address_,
1732 entry.end_address_ - entry.start_address_,
1733 entry.name_,
1734 entry.descriptor_, entry.signature_,
1735 slot,
1736 MangleSlot(slot, m));
1737
1738 slot = MangleSlot(slot, m);
1739
1740 expandBufAdd8BE(pReply, entry.start_address_);
1741 expandBufAddUtf8String(pReply, entry.name_);
1742 expandBufAddUtf8String(pReply, entry.descriptor_);
1743 if (with_generic) {
1744 expandBufAddUtf8String(pReply, entry.signature_);
1745 }
1746 expandBufAdd4BE(pReply, entry.end_address_- entry.start_address_);
1747 expandBufAdd4BE(pReply, slot);
1748
1749 ++variable_count;
1750 });
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001751 }
Elliott Hughesdbb40792011-11-18 17:05:22 -08001752
Mathieu Chartiere5afbf32018-09-12 17:51:54 -07001753 JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, variable_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001754}
1755
Jeff Hao579b0242013-11-18 13:16:49 -08001756void Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value,
1757 JDWP::ExpandBuf* pReply) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001758 ArtMethod* m = FromMethodId(method_id);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001759 JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty());
Jeff Hao579b0242013-11-18 13:16:49 -08001760 OutputJValue(tag, return_value, pReply);
1761}
1762
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001763void Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value,
1764 JDWP::ExpandBuf* pReply) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001765 ArtField* f = FromFieldId(field_id);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001766 JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001767 OutputJValue(tag, field_value, pReply);
1768}
1769
Elliott Hughes9777ba22013-01-17 09:04:19 -08001770JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
Ian Rogersc0542af2014-09-03 16:16:56 -07001771 std::vector<uint8_t>* bytecodes) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001772 ArtMethod* m = FromMethodId(method_id);
Ian Rogersc0542af2014-09-03 16:16:56 -07001773 if (m == nullptr) {
Elliott Hughes9777ba22013-01-17 09:04:19 -08001774 return JDWP::ERR_INVALID_METHODID;
1775 }
David Sehr0225f8e2018-01-31 08:52:24 +00001776 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001777 size_t byte_count = accessor.InsnsSizeInCodeUnits() * 2;
1778 const uint8_t* begin = reinterpret_cast<const uint8_t*>(accessor.Insns());
Elliott Hughes9777ba22013-01-17 09:04:19 -08001779 const uint8_t* end = begin + byte_count;
1780 for (const uint8_t* p = begin; p != end; ++p) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001781 bytecodes->push_back(*p);
Elliott Hughes9777ba22013-01-17 09:04:19 -08001782 }
1783 return JDWP::ERR_NONE;
1784}
1785
Elliott Hughes88d63092013-01-09 09:55:54 -08001786JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001787 return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001788}
1789
Elliott Hughes88d63092013-01-09 09:55:54 -08001790JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001791 return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001792}
1793
Vladimir Marko83114892019-04-11 13:05:50 +01001794static JValue GetArtFieldValue(ArtField* f, ObjPtr<mirror::Object> o)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001795 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz05c26b32015-06-11 18:42:58 +02001796 Primitive::Type fieldType = f->GetTypeAsPrimitiveType();
1797 JValue field_value;
1798 switch (fieldType) {
1799 case Primitive::kPrimBoolean:
1800 field_value.SetZ(f->GetBoolean(o));
1801 return field_value;
1802
1803 case Primitive::kPrimByte:
1804 field_value.SetB(f->GetByte(o));
1805 return field_value;
1806
1807 case Primitive::kPrimChar:
1808 field_value.SetC(f->GetChar(o));
1809 return field_value;
1810
1811 case Primitive::kPrimShort:
1812 field_value.SetS(f->GetShort(o));
1813 return field_value;
1814
1815 case Primitive::kPrimInt:
1816 case Primitive::kPrimFloat:
1817 // Int and Float must be treated as 32-bit values in JDWP.
1818 field_value.SetI(f->GetInt(o));
1819 return field_value;
1820
1821 case Primitive::kPrimLong:
1822 case Primitive::kPrimDouble:
1823 // Long and Double must be treated as 64-bit values in JDWP.
1824 field_value.SetJ(f->GetLong(o));
1825 return field_value;
1826
1827 case Primitive::kPrimNot:
Vladimir Markobcf17522018-06-01 13:14:32 +01001828 field_value.SetL(f->GetObject(o));
Sebastien Hertz05c26b32015-06-11 18:42:58 +02001829 return field_value;
1830
1831 case Primitive::kPrimVoid:
1832 LOG(FATAL) << "Attempt to read from field of type 'void'";
1833 UNREACHABLE();
1834 }
1835 LOG(FATAL) << "Attempt to read from field of unknown type";
1836 UNREACHABLE();
1837}
1838
Elliott Hughes88d63092013-01-09 09:55:54 -08001839static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
1840 JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001841 bool is_static)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001842 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001843 JDWP::JdwpError error;
Vladimir Marko4617d582019-03-28 13:48:31 +00001844 ObjPtr<mirror::Class> c = DecodeClass(ref_type_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001845 if (ref_type_id != 0 && c == nullptr) {
1846 return error;
Elliott Hughes0cf74332012-02-23 23:14:00 -08001847 }
1848
Jeff Haode19a252016-09-14 15:56:35 -07001849 Thread* self = Thread::Current();
1850 StackHandleScope<2> hs(self);
1851 MutableHandle<mirror::Object>
Vladimir Marko83114892019-04-11 13:05:50 +01001852 o(hs.NewHandle(Dbg::GetObjectRegistry()->Get<mirror::Object>(object_id, &error)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001853 if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001854 return JDWP::ERR_INVALID_OBJECT;
1855 }
Mathieu Chartierc7853442015-03-27 14:35:38 -07001856 ArtField* f = FromFieldId(field_id);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001857
Vladimir Marko4617d582019-03-28 13:48:31 +00001858 ObjPtr<mirror::Class> receiver_class = c;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001859 if (receiver_class == nullptr && o != nullptr) {
Elliott Hughes0cf74332012-02-23 23:14:00 -08001860 receiver_class = o->GetClass();
1861 }
Jeff Haode19a252016-09-14 15:56:35 -07001862
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001863 // TODO: should we give up now if receiver_class is null?
Ian Rogersc0542af2014-09-03 16:16:56 -07001864 if (receiver_class != nullptr && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
David Sehr709b0702016-10-13 09:12:37 -07001865 LOG(INFO) << "ERR_INVALID_FIELDID: " << f->PrettyField() << " "
1866 << receiver_class->PrettyClass();
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001867 return JDWP::ERR_INVALID_FIELDID;
1868 }
Elliott Hughesaed4be92011-12-02 16:16:23 -08001869
Jeff Haode19a252016-09-14 15:56:35 -07001870 // Ensure the field's class is initialized.
1871 Handle<mirror::Class> klass(hs.NewHandle(f->GetDeclaringClass()));
1872 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, klass, true, false)) {
David Sehr709b0702016-10-13 09:12:37 -07001873 LOG(WARNING) << "Not able to initialize class for SetValues: "
1874 << mirror::Class::PrettyClass(klass.Get());
Jeff Haode19a252016-09-14 15:56:35 -07001875 }
1876
Elliott Hughes0cf74332012-02-23 23:14:00 -08001877 // The RI only enforces the static/non-static mismatch in one direction.
1878 // TODO: should we change the tests and check both?
1879 if (is_static) {
1880 if (!f->IsStatic()) {
1881 return JDWP::ERR_INVALID_FIELDID;
1882 }
1883 } else {
1884 if (f->IsStatic()) {
Sebastien Hertz05c26b32015-06-11 18:42:58 +02001885 LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.GetValues"
David Sehr709b0702016-10-13 09:12:37 -07001886 << " on static field " << f->PrettyField();
Elliott Hughes0cf74332012-02-23 23:14:00 -08001887 }
1888 }
jeffhao0dfbb7e2012-11-28 15:26:03 -08001889 if (f->IsStatic()) {
Jeff Haode19a252016-09-14 15:56:35 -07001890 o.Assign(f->GetDeclaringClass());
jeffhao0dfbb7e2012-11-28 15:26:03 -08001891 }
Elliott Hughes0cf74332012-02-23 23:14:00 -08001892
Jeff Haode19a252016-09-14 15:56:35 -07001893 JValue field_value(GetArtFieldValue(f, o.Get()));
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001894 JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
Jeff Hao579b0242013-11-18 13:16:49 -08001895 Dbg::OutputJValue(tag, &field_value, pReply);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001896 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001897}
1898
Elliott Hughes88d63092013-01-09 09:55:54 -08001899JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001900 JDWP::ExpandBuf* pReply) {
Elliott Hughes88d63092013-01-09 09:55:54 -08001901 return GetFieldValueImpl(0, object_id, field_id, pReply, false);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001902}
1903
Ian Rogersc0542af2014-09-03 16:16:56 -07001904JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id,
1905 JDWP::ExpandBuf* pReply) {
Elliott Hughes88d63092013-01-09 09:55:54 -08001906 return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001907}
1908
Vladimir Marko83114892019-04-11 13:05:50 +01001909static JDWP::JdwpError SetArtFieldValue(ArtField* f,
1910 ObjPtr<mirror::Object> o,
1911 uint64_t value,
1912 int width)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001913 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz05c26b32015-06-11 18:42:58 +02001914 Primitive::Type fieldType = f->GetTypeAsPrimitiveType();
1915 // Debugging only happens at runtime so we know we are not running in a transaction.
1916 static constexpr bool kNoTransactionMode = false;
1917 switch (fieldType) {
1918 case Primitive::kPrimBoolean:
1919 CHECK_EQ(width, 1);
1920 f->SetBoolean<kNoTransactionMode>(o, static_cast<uint8_t>(value));
1921 return JDWP::ERR_NONE;
1922
1923 case Primitive::kPrimByte:
1924 CHECK_EQ(width, 1);
1925 f->SetByte<kNoTransactionMode>(o, static_cast<uint8_t>(value));
1926 return JDWP::ERR_NONE;
1927
1928 case Primitive::kPrimChar:
1929 CHECK_EQ(width, 2);
1930 f->SetChar<kNoTransactionMode>(o, static_cast<uint16_t>(value));
1931 return JDWP::ERR_NONE;
1932
1933 case Primitive::kPrimShort:
1934 CHECK_EQ(width, 2);
1935 f->SetShort<kNoTransactionMode>(o, static_cast<int16_t>(value));
1936 return JDWP::ERR_NONE;
1937
1938 case Primitive::kPrimInt:
1939 case Primitive::kPrimFloat:
1940 CHECK_EQ(width, 4);
1941 // Int and Float must be treated as 32-bit values in JDWP.
1942 f->SetInt<kNoTransactionMode>(o, static_cast<int32_t>(value));
1943 return JDWP::ERR_NONE;
1944
1945 case Primitive::kPrimLong:
1946 case Primitive::kPrimDouble:
1947 CHECK_EQ(width, 8);
1948 // Long and Double must be treated as 64-bit values in JDWP.
1949 f->SetLong<kNoTransactionMode>(o, value);
1950 return JDWP::ERR_NONE;
1951
1952 case Primitive::kPrimNot: {
1953 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01001954 ObjPtr<mirror::Object> v = Dbg::GetObjectRegistry()->Get<mirror::Object>(value, &error);
Sebastien Hertz05c26b32015-06-11 18:42:58 +02001955 if (error != JDWP::ERR_NONE) {
1956 return JDWP::ERR_INVALID_OBJECT;
1957 }
1958 if (v != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07001959 ObjPtr<mirror::Class> field_type;
Sebastien Hertz05c26b32015-06-11 18:42:58 +02001960 {
1961 StackHandleScope<2> hs(Thread::Current());
Vladimir Marko83114892019-04-11 13:05:50 +01001962 HandleWrapperObjPtr<mirror::Object> h_v(hs.NewHandleWrapper(&v));
1963 HandleWrapperObjPtr<mirror::Object> h_o(hs.NewHandleWrapper(&o));
Vladimir Marko4098a7a2017-11-06 16:00:51 +00001964 field_type = f->ResolveType();
Sebastien Hertz05c26b32015-06-11 18:42:58 +02001965 }
1966 if (!field_type->IsAssignableFrom(v->GetClass())) {
1967 return JDWP::ERR_INVALID_OBJECT;
1968 }
1969 }
1970 f->SetObject<kNoTransactionMode>(o, v);
1971 return JDWP::ERR_NONE;
1972 }
1973
1974 case Primitive::kPrimVoid:
1975 LOG(FATAL) << "Attempt to write to field of type 'void'";
1976 UNREACHABLE();
1977 }
1978 LOG(FATAL) << "Attempt to write to field of unknown type";
1979 UNREACHABLE();
1980}
1981
Elliott Hughes88d63092013-01-09 09:55:54 -08001982static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001983 uint64_t value, int width, bool is_static)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001984 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001985 JDWP::JdwpError error;
Jeff Haode19a252016-09-14 15:56:35 -07001986 Thread* self = Thread::Current();
1987 StackHandleScope<2> hs(self);
1988 MutableHandle<mirror::Object>
Vladimir Marko83114892019-04-11 13:05:50 +01001989 o(hs.NewHandle(Dbg::GetObjectRegistry()->Get<mirror::Object>(object_id, &error)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001990 if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001991 return JDWP::ERR_INVALID_OBJECT;
1992 }
Mathieu Chartierc7853442015-03-27 14:35:38 -07001993 ArtField* f = FromFieldId(field_id);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001994
Jeff Haode19a252016-09-14 15:56:35 -07001995 // Ensure the field's class is initialized.
1996 Handle<mirror::Class> klass(hs.NewHandle(f->GetDeclaringClass()));
1997 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, klass, true, false)) {
David Sehr709b0702016-10-13 09:12:37 -07001998 LOG(WARNING) << "Not able to initialize class for SetValues: "
1999 << mirror::Class::PrettyClass(klass.Get());
Jeff Haode19a252016-09-14 15:56:35 -07002000 }
2001
Elliott Hughes0cf74332012-02-23 23:14:00 -08002002 // The RI only enforces the static/non-static mismatch in one direction.
2003 // TODO: should we change the tests and check both?
2004 if (is_static) {
2005 if (!f->IsStatic()) {
2006 return JDWP::ERR_INVALID_FIELDID;
2007 }
2008 } else {
2009 if (f->IsStatic()) {
Sebastien Hertz05c26b32015-06-11 18:42:58 +02002010 LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues"
David Sehr709b0702016-10-13 09:12:37 -07002011 << " on static field " << f->PrettyField();
Elliott Hughes0cf74332012-02-23 23:14:00 -08002012 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08002013 }
jeffhao0dfbb7e2012-11-28 15:26:03 -08002014 if (f->IsStatic()) {
Jeff Haode19a252016-09-14 15:56:35 -07002015 o.Assign(f->GetDeclaringClass());
jeffhao0dfbb7e2012-11-28 15:26:03 -08002016 }
Jeff Haode19a252016-09-14 15:56:35 -07002017 return SetArtFieldValue(f, o.Get(), value, width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002018}
2019
Elliott Hughes88d63092013-01-09 09:55:54 -08002020JDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002021 int width) {
Elliott Hughes88d63092013-01-09 09:55:54 -08002022 return SetFieldValueImpl(object_id, field_id, value, width, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002023}
2024
Elliott Hughes88d63092013-01-09 09:55:54 -08002025JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
2026 return SetFieldValueImpl(0, field_id, value, width, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002027}
2028
Sebastien Hertzb0b0b492014-09-15 11:27:27 +02002029JDWP::JdwpError Dbg::StringToUtf8(JDWP::ObjectId string_id, std::string* str) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002030 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01002031 ObjPtr<mirror::Object> obj = gRegistry->Get<mirror::Object>(string_id, &error);
Sebastien Hertzb0b0b492014-09-15 11:27:27 +02002032 if (error != JDWP::ERR_NONE) {
2033 return error;
2034 }
2035 if (obj == nullptr) {
2036 return JDWP::ERR_INVALID_OBJECT;
2037 }
2038 {
2039 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier0795f232016-09-27 18:43:30 -07002040 ObjPtr<mirror::Class> java_lang_String =
2041 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_String);
Sebastien Hertzb0b0b492014-09-15 11:27:27 +02002042 if (!java_lang_String->IsAssignableFrom(obj->GetClass())) {
2043 // This isn't a string.
2044 return JDWP::ERR_INVALID_STRING;
2045 }
2046 }
2047 *str = obj->AsString()->ToModifiedUtf8();
2048 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002049}
2050
Jeff Hao579b0242013-11-18 13:16:49 -08002051void Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) {
2052 if (IsPrimitiveTag(tag)) {
2053 expandBufAdd1(pReply, tag);
2054 if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
2055 expandBufAdd1(pReply, return_value->GetI());
2056 } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
2057 expandBufAdd2BE(pReply, return_value->GetI());
2058 } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
2059 expandBufAdd4BE(pReply, return_value->GetI());
2060 } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
2061 expandBufAdd8BE(pReply, return_value->GetJ());
2062 } else {
2063 CHECK_EQ(tag, JDWP::JT_VOID);
2064 }
2065 } else {
Ian Rogers98379392014-02-24 16:53:16 -08002066 ScopedObjectAccessUnchecked soa(Thread::Current());
Vladimir Marko83114892019-04-11 13:05:50 +01002067 ObjPtr<mirror::Object> value = return_value->GetL();
Ian Rogers98379392014-02-24 16:53:16 -08002068 expandBufAdd1(pReply, TagFromObject(soa, value));
Jeff Hao579b0242013-11-18 13:16:49 -08002069 expandBufAddObjectId(pReply, gRegistry->Add(value));
2070 }
2071}
2072
Ian Rogersc0542af2014-09-03 16:16:56 -07002073JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string* name) {
jeffhaoa77f0f62012-12-05 17:19:31 -08002074 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07002075 JDWP::JdwpError error;
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002076 DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08002077 if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
2078 return error;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08002079 }
Elliott Hughes221229c2013-01-08 18:17:50 -08002080
2081 // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
Vladimir Marko83114892019-04-11 13:05:50 +01002082 ObjPtr<mirror::Object> thread_object = gRegistry->Get<mirror::Object>(thread_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07002083 CHECK(thread_object != nullptr) << error;
Mathieu Chartierc7853442015-03-27 14:35:38 -07002084 ArtField* java_lang_Thread_name_field =
Andreas Gampe08883de2016-11-08 13:20:52 -08002085 jni::DecodeArtField(WellKnownClasses::java_lang_Thread_name);
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -07002086 ObjPtr<mirror::String> s(java_lang_Thread_name_field->GetObject(thread_object)->AsString());
Ian Rogersc0542af2014-09-03 16:16:56 -07002087 if (s != nullptr) {
2088 *name = s->ToModifiedUtf8();
Elliott Hughes221229c2013-01-08 18:17:50 -08002089 }
2090 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002091}
2092
Elliott Hughes221229c2013-01-08 18:17:50 -08002093JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
Sebastien Hertza06430c2014-09-15 19:21:30 +02002094 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07002095 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01002096 ObjPtr<mirror::Object> thread_object = gRegistry->Get<mirror::Object>(thread_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07002097 if (error != JDWP::ERR_NONE) {
Elliott Hughes2435a572012-02-17 16:07:41 -08002098 return JDWP::ERR_INVALID_OBJECT;
2099 }
Mathieu Chartier268764d2016-09-13 12:09:38 -07002100 ScopedAssertNoThreadSuspension ants("Debugger: GetThreadGroup");
Elliott Hughes2435a572012-02-17 16:07:41 -08002101 // Okay, so it's an object, but is it actually a thread?
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002102 DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08002103 if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
2104 // Zombie threads are in the null group.
2105 expandBufAddObjectId(pReply, JDWP::ObjectId(0));
Sebastien Hertz52d131d2014-03-13 16:17:40 +01002106 error = JDWP::ERR_NONE;
2107 } else if (error == JDWP::ERR_NONE) {
Mathieu Chartier0795f232016-09-27 18:43:30 -07002108 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(WellKnownClasses::java_lang_Thread);
Sebastien Hertz52d131d2014-03-13 16:17:40 +01002109 CHECK(c != nullptr);
Andreas Gampe08883de2016-11-08 13:20:52 -08002110 ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_group);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07002111 CHECK(f != nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -07002112 ObjPtr<mirror::Object> group = f->GetObject(thread_object);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07002113 CHECK(group != nullptr);
Sebastien Hertz52d131d2014-03-13 16:17:40 +01002114 JDWP::ObjectId thread_group_id = gRegistry->Add(group);
2115 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes221229c2013-01-08 18:17:50 -08002116 }
Sebastien Hertz52d131d2014-03-13 16:17:40 +01002117 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002118}
2119
Vladimir Marko83114892019-04-11 13:05:50 +01002120static ObjPtr<mirror::Object> DecodeThreadGroup(ScopedObjectAccessUnchecked& soa,
2121 JDWP::ObjectId thread_group_id,
2122 JDWP::JdwpError* error)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002123 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko83114892019-04-11 13:05:50 +01002124 ObjPtr<mirror::Object> thread_group =
2125 Dbg::GetObjectRegistry()->Get<mirror::Object>(thread_group_id, error);
Sebastien Hertza06430c2014-09-15 19:21:30 +02002126 if (*error != JDWP::ERR_NONE) {
2127 return nullptr;
2128 }
2129 if (thread_group == nullptr) {
2130 *error = JDWP::ERR_INVALID_OBJECT;
2131 return nullptr;
2132 }
Mathieu Chartier0795f232016-09-27 18:43:30 -07002133 ObjPtr<mirror::Class> c =
2134 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_ThreadGroup);
Ian Rogers98379392014-02-24 16:53:16 -08002135 CHECK(c != nullptr);
Sebastien Hertza06430c2014-09-15 19:21:30 +02002136 if (!c->IsAssignableFrom(thread_group->GetClass())) {
2137 // This is not a java.lang.ThreadGroup.
2138 *error = JDWP::ERR_INVALID_THREAD_GROUP;
2139 return nullptr;
2140 }
2141 *error = JDWP::ERR_NONE;
2142 return thread_group;
2143}
2144
2145JDWP::JdwpError Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) {
2146 ScopedObjectAccessUnchecked soa(Thread::Current());
2147 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01002148 ObjPtr<mirror::Object> thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
Sebastien Hertza06430c2014-09-15 19:21:30 +02002149 if (error != JDWP::ERR_NONE) {
2150 return error;
2151 }
Mathieu Chartier268764d2016-09-13 12:09:38 -07002152 ScopedAssertNoThreadSuspension ants("Debugger: GetThreadGroupName");
Andreas Gampe08883de2016-11-08 13:20:52 -08002153 ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_name);
Ian Rogersc0542af2014-09-03 16:16:56 -07002154 CHECK(f != nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -07002155 ObjPtr<mirror::String> s = f->GetObject(thread_group)->AsString();
Sebastien Hertza06430c2014-09-15 19:21:30 +02002156
2157 std::string thread_group_name(s->ToModifiedUtf8());
2158 expandBufAddUtf8String(pReply, thread_group_name);
2159 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002160}
2161
Sebastien Hertza06430c2014-09-15 19:21:30 +02002162JDWP::JdwpError Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) {
Ian Rogers98379392014-02-24 16:53:16 -08002163 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07002164 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01002165 ObjPtr<mirror::Object> thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
Sebastien Hertza06430c2014-09-15 19:21:30 +02002166 if (error != JDWP::ERR_NONE) {
2167 return error;
2168 }
Mathieu Chartier3398c782016-09-30 10:27:43 -07002169 ObjPtr<mirror::Object> parent;
Mathieu Chartier2d5f39e2014-09-19 17:52:37 -07002170 {
Mathieu Chartier268764d2016-09-13 12:09:38 -07002171 ScopedAssertNoThreadSuspension ants("Debugger: GetThreadGroupParent");
Andreas Gampe08883de2016-11-08 13:20:52 -08002172 ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_parent);
Mathieu Chartier2d5f39e2014-09-19 17:52:37 -07002173 CHECK(f != nullptr);
2174 parent = f->GetObject(thread_group);
2175 }
Sebastien Hertza06430c2014-09-15 19:21:30 +02002176 JDWP::ObjectId parent_group_id = gRegistry->Add(parent);
2177 expandBufAddObjectId(pReply, parent_group_id);
2178 return JDWP::ERR_NONE;
2179}
2180
Vladimir Marko83114892019-04-11 13:05:50 +01002181static void GetChildThreadGroups(ObjPtr<mirror::Object> thread_group,
Sebastien Hertza06430c2014-09-15 19:21:30 +02002182 std::vector<JDWP::ObjectId>* child_thread_group_ids)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002183 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertza06430c2014-09-15 19:21:30 +02002184 CHECK(thread_group != nullptr);
2185
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +00002186 // Get the int "ngroups" count of this thread group...
Andreas Gampe08883de2016-11-08 13:20:52 -08002187 ArtField* ngroups_field = jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_ngroups);
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +00002188 CHECK(ngroups_field != nullptr);
2189 const int32_t size = ngroups_field->GetInt(thread_group);
2190 if (size == 0) {
2191 return;
Sebastien Hertze49e1952014-10-13 11:27:13 +02002192 }
Sebastien Hertza06430c2014-09-15 19:21:30 +02002193
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +00002194 // Get the ThreadGroup[] "groups" out of this thread group...
Andreas Gampe08883de2016-11-08 13:20:52 -08002195 ArtField* groups_field = jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_groups);
Mathieu Chartier3398c782016-09-30 10:27:43 -07002196 ObjPtr<mirror::Object> groups_array = groups_field->GetObject(thread_group);
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +00002197
2198 CHECK(groups_array != nullptr);
2199 CHECK(groups_array->IsObjectArray());
2200
Mathieu Chartier3398c782016-09-30 10:27:43 -07002201 ObjPtr<mirror::ObjectArray<mirror::Object>> groups_array_as_array =
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +00002202 groups_array->AsObjectArray<mirror::Object>();
Sebastien Hertza06430c2014-09-15 19:21:30 +02002203
2204 // Copy the first 'size' elements out of the array into the result.
Sebastien Hertz6995c602014-09-09 12:10:13 +02002205 ObjectRegistry* registry = Dbg::GetObjectRegistry();
Sebastien Hertza06430c2014-09-15 19:21:30 +02002206 for (int32_t i = 0; i < size; ++i) {
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +00002207 child_thread_group_ids->push_back(registry->Add(groups_array_as_array->Get(i)));
Sebastien Hertza06430c2014-09-15 19:21:30 +02002208 }
2209}
2210
2211JDWP::JdwpError Dbg::GetThreadGroupChildren(JDWP::ObjectId thread_group_id,
2212 JDWP::ExpandBuf* pReply) {
2213 ScopedObjectAccessUnchecked soa(Thread::Current());
2214 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01002215 ObjPtr<mirror::Object> thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
Sebastien Hertza06430c2014-09-15 19:21:30 +02002216 if (error != JDWP::ERR_NONE) {
2217 return error;
2218 }
2219
2220 // Add child threads.
2221 {
2222 std::vector<JDWP::ObjectId> child_thread_ids;
2223 GetThreads(thread_group, &child_thread_ids);
2224 expandBufAdd4BE(pReply, child_thread_ids.size());
2225 for (JDWP::ObjectId child_thread_id : child_thread_ids) {
2226 expandBufAddObjectId(pReply, child_thread_id);
2227 }
2228 }
2229
2230 // Add child thread groups.
2231 {
2232 std::vector<JDWP::ObjectId> child_thread_groups_ids;
Andreas Gampe08883de2016-11-08 13:20:52 -08002233 GetChildThreadGroups(thread_group, &child_thread_groups_ids);
Sebastien Hertza06430c2014-09-15 19:21:30 +02002234 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
2235 for (JDWP::ObjectId child_thread_group_id : child_thread_groups_ids) {
2236 expandBufAddObjectId(pReply, child_thread_group_id);
2237 }
2238 }
2239
2240 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002241}
2242
2243JDWP::ObjectId Dbg::GetSystemThreadGroupId() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002244 ScopedObjectAccessUnchecked soa(Thread::Current());
Andreas Gampe08883de2016-11-08 13:20:52 -08002245 ArtField* f = jni::DecodeArtField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
Mathieu Chartier3398c782016-09-30 10:27:43 -07002246 ObjPtr<mirror::Object> group = f->GetObject(f->GetDeclaringClass());
Ian Rogers365c1022012-06-22 15:05:28 -07002247 return gRegistry->Add(group);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002248}
2249
Jeff Hao920af3e2013-08-28 15:46:38 -07002250JDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) {
2251 switch (state) {
2252 case kBlocked:
2253 return JDWP::TS_MONITOR;
2254 case kNative:
2255 case kRunnable:
2256 case kSuspended:
2257 return JDWP::TS_RUNNING;
2258 case kSleeping:
2259 return JDWP::TS_SLEEPING;
2260 case kStarting:
2261 case kTerminated:
2262 return JDWP::TS_ZOMBIE;
2263 case kTimedWaiting:
Alex Light77fee872017-09-05 14:51:49 -07002264 case kWaitingForTaskProcessor:
2265 case kWaitingForLockInflation:
Sebastien Hertzbae182c2013-12-17 10:42:03 +01002266 case kWaitingForCheckPointsToRun:
Jeff Hao920af3e2013-08-28 15:46:38 -07002267 case kWaitingForDebuggerSend:
2268 case kWaitingForDebuggerSuspension:
2269 case kWaitingForDebuggerToAttach:
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01002270 case kWaitingForDeoptimization:
Jeff Hao920af3e2013-08-28 15:46:38 -07002271 case kWaitingForGcToComplete:
Mathieu Chartierb43390c2015-05-12 10:47:11 -07002272 case kWaitingForGetObjectsAllocated:
Jeff Hao920af3e2013-08-28 15:46:38 -07002273 case kWaitingForJniOnLoad:
Sebastien Hertzbae182c2013-12-17 10:42:03 +01002274 case kWaitingForMethodTracingStart:
Jeff Hao920af3e2013-08-28 15:46:38 -07002275 case kWaitingForSignalCatcherOutput:
Hiroshi Yamauchi0c8c3032015-01-16 16:54:35 -08002276 case kWaitingForVisitObjects:
Jeff Hao920af3e2013-08-28 15:46:38 -07002277 case kWaitingInMainDebuggerLoop:
2278 case kWaitingInMainSignalCatcherLoop:
2279 case kWaitingPerformingGc:
Mathieu Chartier90ef3db2015-08-04 15:19:41 -07002280 case kWaitingWeakGcRootRead:
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07002281 case kWaitingForGcThreadFlip:
Koji Fukui34857b52019-03-20 19:13:00 +09002282 case kNativeForAbort:
Jeff Hao920af3e2013-08-28 15:46:38 -07002283 case kWaiting:
2284 return JDWP::TS_WAIT;
2285 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
2286 }
2287 LOG(FATAL) << "Unknown thread state: " << state;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002288 UNREACHABLE();
Jeff Hao920af3e2013-08-28 15:46:38 -07002289}
2290
Sebastien Hertz52d131d2014-03-13 16:17:40 +01002291JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus,
2292 JDWP::JdwpSuspendStatus* pSuspendStatus) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002293 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes499c5132011-11-17 14:55:11 -08002294
Elliott Hughes9e0c1752013-01-09 14:02:58 -08002295 *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
2296
Ian Rogersc0542af2014-09-03 16:16:56 -07002297 JDWP::JdwpError error;
2298 Thread* thread = DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08002299 if (error != JDWP::ERR_NONE) {
2300 if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
2301 *pThreadStatus = JDWP::TS_ZOMBIE;
Elliott Hughes221229c2013-01-08 18:17:50 -08002302 return JDWP::ERR_NONE;
2303 }
2304 return error;
Elliott Hughes499c5132011-11-17 14:55:11 -08002305 }
2306
Elliott Hughes9e0c1752013-01-09 14:02:58 -08002307 if (IsSuspendedForDebugger(soa, thread)) {
2308 *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
Elliott Hughes499c5132011-11-17 14:55:11 -08002309 }
2310
Jeff Hao920af3e2013-08-28 15:46:38 -07002311 *pThreadStatus = ToJdwpThreadStatus(thread->GetState());
Elliott Hughes221229c2013-01-08 18:17:50 -08002312 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002313}
2314
Elliott Hughes221229c2013-01-08 18:17:50 -08002315JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002316 ScopedObjectAccess soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07002317 JDWP::JdwpError error;
2318 Thread* thread = DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08002319 if (error != JDWP::ERR_NONE) {
2320 return error;
Elliott Hughes2435a572012-02-17 16:07:41 -08002321 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002322 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002323 expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
Elliott Hughes2435a572012-02-17 16:07:41 -08002324 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002325}
2326
Elliott Hughesf9501702013-01-11 11:22:27 -08002327JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
2328 ScopedObjectAccess soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07002329 JDWP::JdwpError error;
2330 Thread* thread = DecodeThread(soa, thread_id, &error);
Elliott Hughesf9501702013-01-11 11:22:27 -08002331 if (error != JDWP::ERR_NONE) {
2332 return error;
2333 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07002334 thread->Interrupt(soa.Self());
Elliott Hughesf9501702013-01-11 11:22:27 -08002335 return JDWP::ERR_NONE;
2336}
2337
Vladimir Marko83114892019-04-11 13:05:50 +01002338static bool IsInDesiredThreadGroup(ObjPtr<mirror::Object> desired_thread_group,
2339 ObjPtr<mirror::Object> peer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002340 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz070f7322014-09-09 12:08:49 +02002341 // Do we want threads from all thread groups?
2342 if (desired_thread_group == nullptr) {
2343 return true;
2344 }
Andreas Gampe08883de2016-11-08 13:20:52 -08002345 ArtField* thread_group_field = jni::DecodeArtField(WellKnownClasses::java_lang_Thread_group);
Sebastien Hertz070f7322014-09-09 12:08:49 +02002346 DCHECK(thread_group_field != nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -07002347 ObjPtr<mirror::Object> group = thread_group_field->GetObject(peer);
Sebastien Hertz070f7322014-09-09 12:08:49 +02002348 return (group == desired_thread_group);
2349}
2350
Vladimir Marko83114892019-04-11 13:05:50 +01002351void Dbg::GetThreads(ObjPtr<mirror::Object> thread_group, std::vector<JDWP::ObjectId>* thread_ids) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002352 ScopedObjectAccessUnchecked soa(Thread::Current());
Sebastien Hertz070f7322014-09-09 12:08:49 +02002353 std::list<Thread*> all_threads_list;
2354 {
2355 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
2356 all_threads_list = Runtime::Current()->GetThreadList()->GetList();
2357 }
2358 for (Thread* t : all_threads_list) {
2359 if (t == Dbg::GetDebugThread()) {
2360 // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
2361 // query all threads, so it's easier if we just don't tell them about this thread.
2362 continue;
2363 }
2364 if (t->IsStillStarting()) {
2365 // This thread is being started (and has been registered in the thread list). However, it is
2366 // not completely started yet so we must ignore it.
2367 continue;
2368 }
Vladimir Marko83114892019-04-11 13:05:50 +01002369 ObjPtr<mirror::Object> peer = t->GetPeerFromOtherThread();
Sebastien Hertz070f7322014-09-09 12:08:49 +02002370 if (peer == nullptr) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002371 // peer might be null if the thread is still starting up. We can't tell the debugger about
Sebastien Hertz070f7322014-09-09 12:08:49 +02002372 // this thread yet.
2373 // TODO: if we identified threads to the debugger by their Thread*
2374 // rather than their peer's mirror::Object*, we could fix this.
2375 // Doing so might help us report ZOMBIE threads too.
2376 continue;
2377 }
Andreas Gampe08883de2016-11-08 13:20:52 -08002378 if (IsInDesiredThreadGroup(thread_group, peer)) {
Sebastien Hertz070f7322014-09-09 12:08:49 +02002379 thread_ids->push_back(gRegistry->Add(peer));
2380 }
2381 }
Elliott Hughescaf76542012-06-28 16:08:22 -07002382}
Elliott Hughesa2155262011-11-16 16:26:58 -08002383
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002384static int GetStackDepth(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec7d878d2018-11-19 18:42:06 +00002385 size_t depth = 0u;
2386 StackVisitor::WalkStack(
2387 [&depth](const StackVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
2388 if (!visitor->GetMethod()->IsRuntimeMethod()) {
2389 ++depth;
2390 }
2391 return true;
2392 },
2393 thread,
2394 /* context= */ nullptr,
2395 StackVisitor::StackWalkKind::kIncludeInlinedFrames);
2396 return depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002397}
2398
Ian Rogersc0542af2014-09-03 16:16:56 -07002399JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t* result) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002400 ScopedObjectAccess soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07002401 JDWP::JdwpError error;
2402 *result = 0;
2403 Thread* thread = DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08002404 if (error != JDWP::ERR_NONE) {
2405 return error;
2406 }
Elliott Hughesf15f4a02013-01-09 10:09:38 -08002407 if (!IsSuspendedForDebugger(soa, thread)) {
2408 return JDWP::ERR_THREAD_NOT_SUSPENDED;
2409 }
Ian Rogersc0542af2014-09-03 16:16:56 -07002410 *result = GetStackDepth(thread);
Elliott Hughes221229c2013-01-08 18:17:50 -08002411 return JDWP::ERR_NONE;
Elliott Hughes86964332012-02-15 19:37:42 -08002412}
2413
Andreas Gampec7d878d2018-11-19 18:42:06 +00002414JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id,
2415 const size_t start_frame,
2416 const size_t frame_count,
2417 JDWP::ExpandBuf* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002418 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07002419 JDWP::JdwpError error;
2420 Thread* thread = DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08002421 if (error != JDWP::ERR_NONE) {
2422 return error;
2423 }
Elliott Hughesf15f4a02013-01-09 10:09:38 -08002424 if (!IsSuspendedForDebugger(soa, thread)) {
2425 return JDWP::ERR_THREAD_NOT_SUSPENDED;
2426 }
Andreas Gampec7d878d2018-11-19 18:42:06 +00002427
2428 expandBufAdd4BE(buf, frame_count);
2429
2430 size_t depth = 0u;
2431 StackVisitor::WalkStack(
2432 [&](StackVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
2433 if (visitor->GetMethod()->IsRuntimeMethod()) {
2434 return true; // The debugger can't do anything useful with a frame that has no Method*.
2435 }
2436 if (depth >= start_frame + frame_count) {
2437 return false;
2438 }
2439 if (depth >= start_frame) {
2440 JDWP::FrameId frame_id(visitor->GetFrameId());
2441 JDWP::JdwpLocation location;
2442 SetJdwpLocation(&location, visitor->GetMethod(), visitor->GetDexPc());
2443 VLOG(jdwp)
2444 << StringPrintf(" Frame %3zd: id=%3" PRIu64 " ", depth, frame_id) << location;
2445 expandBufAdd8BE(buf, frame_id);
2446 expandBufAddLocation(buf, location);
2447 }
2448 ++depth;
2449 return true;
2450 },
2451 thread,
2452 /* context= */ nullptr,
2453 StackVisitor::StackWalkKind::kIncludeInlinedFrames);
2454
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002455 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002456}
2457
2458JDWP::ObjectId Dbg::GetThreadSelfId() {
Sebastien Hertz6995c602014-09-09 12:10:13 +02002459 return GetThreadId(Thread::Current());
2460}
2461
2462JDWP::ObjectId Dbg::GetThreadId(Thread* thread) {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -07002463 ScopedObjectAccessUnchecked soa(Thread::Current());
Nicolas Geoffraycafa0812017-02-15 18:27:34 +00002464 return gRegistry->Add(thread->GetPeerFromOtherThread());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002465}
2466
Elliott Hughes475fc232011-10-25 15:00:35 -07002467void Dbg::SuspendVM() {
Hiroshi Yamauchi8f95cf32016-04-19 11:14:06 -07002468 // Avoid a deadlock between GC and debugger where GC gets suspended during GC. b/25800335.
2469 gc::ScopedGCCriticalSection gcs(Thread::Current(),
2470 gc::kGcCauseDebugger,
2471 gc::kCollectorTypeDebugger);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002472 Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002473}
2474
2475void Dbg::ResumeVM() {
Sebastien Hertz253fa552014-10-14 17:27:15 +02002476 Runtime::Current()->GetThreadList()->ResumeAllForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002477}
2478
Elliott Hughes221229c2013-01-08 18:17:50 -08002479JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
Ian Rogersf3d874c2014-07-17 18:52:42 -07002480 Thread* self = Thread::Current();
Ian Rogersc0542af2014-09-03 16:16:56 -07002481 ScopedLocalRef<jobject> peer(self->GetJniEnv(), nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002482 {
Ian Rogersf3d874c2014-07-17 18:52:42 -07002483 ScopedObjectAccess soa(self);
Ian Rogersc0542af2014-09-03 16:16:56 -07002484 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01002485 peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object>(thread_id, &error)));
Elliott Hughes4e235312011-12-02 11:34:15 -08002486 }
Ian Rogersc0542af2014-09-03 16:16:56 -07002487 if (peer.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002488 return JDWP::ERR_THREAD_NOT_ALIVE;
2489 }
Ian Rogers4ad5cd32014-11-11 23:08:07 -08002490 // Suspend thread to build stack trace.
Elliott Hughesf327e072013-01-09 16:01:26 -08002491 bool timed_out;
Brian Carlstromba32de42014-08-27 23:43:46 -07002492 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Sebastien Hertzcdd798d2017-04-18 18:28:51 +02002493 Thread* thread = thread_list->SuspendThreadByPeer(peer.get(),
2494 request_suspension,
Alex Light46f93402017-06-29 11:59:50 -07002495 SuspendReason::kForDebugger,
Brian Carlstromba32de42014-08-27 23:43:46 -07002496 &timed_out);
Ian Rogersc0542af2014-09-03 16:16:56 -07002497 if (thread != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002498 return JDWP::ERR_NONE;
Elliott Hughesf327e072013-01-09 16:01:26 -08002499 } else if (timed_out) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002500 return JDWP::ERR_INTERNAL;
2501 } else {
2502 return JDWP::ERR_THREAD_NOT_ALIVE;
2503 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002504}
2505
Elliott Hughes221229c2013-01-08 18:17:50 -08002506void Dbg::ResumeThread(JDWP::ObjectId thread_id) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002507 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07002508 JDWP::JdwpError error;
Vladimir Marko83114892019-04-11 13:05:50 +01002509 ObjPtr<mirror::Object> peer = gRegistry->Get<mirror::Object>(thread_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07002510 CHECK(peer != nullptr) << error;
jeffhaoa77f0f62012-12-05 17:19:31 -08002511 Thread* thread;
2512 {
2513 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2514 thread = Thread::FromManagedThread(soa, peer);
2515 }
Ian Rogersc0542af2014-09-03 16:16:56 -07002516 if (thread == nullptr) {
Elliott Hughes4e235312011-12-02 11:34:15 -08002517 LOG(WARNING) << "No such thread for resume: " << peer;
2518 return;
2519 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002520 bool needs_resume;
2521 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002522 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Sebastien Hertz70d60272017-04-14 14:18:36 +02002523 needs_resume = thread->GetDebugSuspendCount() > 0;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002524 }
2525 if (needs_resume) {
Alex Light88fd7202017-06-30 08:31:59 -07002526 bool resumed = Runtime::Current()->GetThreadList()->Resume(thread, SuspendReason::kForDebugger);
2527 DCHECK(resumed);
Elliott Hughes546b9862012-06-20 16:06:13 -07002528 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002529}
2530
2531void Dbg::SuspendSelf() {
Elliott Hughes475fc232011-10-25 15:00:35 -07002532 Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002533}
2534
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002535JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
2536 JDWP::ObjectId* result) {
2537 ScopedObjectAccessUnchecked soa(Thread::Current());
Sebastien Hertz69206392015-04-07 15:54:25 +02002538 JDWP::JdwpError error;
2539 Thread* thread = DecodeThread(soa, thread_id, &error);
2540 if (error != JDWP::ERR_NONE) {
2541 return error;
2542 }
2543 if (!IsSuspendedForDebugger(soa, thread)) {
2544 return JDWP::ERR_THREAD_NOT_SUSPENDED;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002545 }
Ian Rogers700a4022014-05-19 16:49:03 -07002546 std::unique_ptr<Context> context(Context::Create());
Vladimir Marko83114892019-04-11 13:05:50 +01002547 ObjPtr<mirror::Object> this_object = nullptr;
Andreas Gampec7d878d2018-11-19 18:42:06 +00002548 StackVisitor::WalkStack(
2549 [&](art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
2550 if (frame_id != stack_visitor->GetFrameId()) {
2551 return true; // continue
2552 } else {
2553 this_object = stack_visitor->GetThisObject();
2554 return false;
2555 }
2556 },
2557 thread,
2558 context.get(),
2559 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
2560 *result = gRegistry->Add(this_object);
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002561 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002562}
2563
Andreas Gampec7d878d2018-11-19 18:42:06 +00002564template <typename FrameHandler>
2565static JDWP::JdwpError FindAndHandleNonNativeFrame(Thread* thread,
2566 JDWP::FrameId frame_id,
2567 const FrameHandler& handler)
2568 REQUIRES_SHARED(Locks::mutator_lock_) {
2569 JDWP::JdwpError result = JDWP::ERR_INVALID_FRAMEID;
2570 std::unique_ptr<Context> context(Context::Create());
2571 StackVisitor::WalkStack(
2572 [&](art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
2573 if (stack_visitor->GetFrameId() != frame_id) {
2574 return true; // Not our frame, carry on.
2575 }
2576 ArtMethod* m = stack_visitor->GetMethod();
2577 if (m->IsNative()) {
2578 // We can't read/write local value from/into native method.
2579 result = JDWP::ERR_OPAQUE_FRAME;
2580 } else {
2581 // We found our frame.
2582 result = handler(stack_visitor);
2583 }
2584 return false;
2585 },
2586 thread,
2587 context.get(),
2588 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
2589 return result;
2590}
Sebastien Hertz8009f392014-09-01 17:07:11 +02002591
2592JDWP::JdwpError Dbg::GetLocalValues(JDWP::Request* request, JDWP::ExpandBuf* pReply) {
2593 JDWP::ObjectId thread_id = request->ReadThreadId();
2594 JDWP::FrameId frame_id = request->ReadFrameId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002595
2596 ScopedObjectAccessUnchecked soa(Thread::Current());
Sebastien Hertz69206392015-04-07 15:54:25 +02002597 JDWP::JdwpError error;
2598 Thread* thread = DecodeThread(soa, thread_id, &error);
2599 if (error != JDWP::ERR_NONE) {
2600 return error;
2601 }
2602 if (!IsSuspendedForDebugger(soa, thread)) {
2603 return JDWP::ERR_THREAD_NOT_SUSPENDED;
Elliott Hughes221229c2013-01-08 18:17:50 -08002604 }
Sebastien Hertz8009f392014-09-01 17:07:11 +02002605
Andreas Gampec7d878d2018-11-19 18:42:06 +00002606 return FindAndHandleNonNativeFrame(
2607 thread,
2608 frame_id,
2609 [&](art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
2610 // Read the values from visitor's context.
2611 int32_t slot_count = request->ReadSigned32("slot count");
2612 expandBufAdd4BE(pReply, slot_count); /* "int values" */
2613 for (int32_t i = 0; i < slot_count; ++i) {
2614 uint32_t slot = request->ReadUnsigned32("slot");
2615 JDWP::JdwpTag reqSigByte = request->ReadTag();
Sebastien Hertz8009f392014-09-01 17:07:11 +02002616
Andreas Gampec7d878d2018-11-19 18:42:06 +00002617 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
Sebastien Hertz8009f392014-09-01 17:07:11 +02002618
Andreas Gampec7d878d2018-11-19 18:42:06 +00002619 size_t width = Dbg::GetTagWidth(reqSigByte);
2620 uint8_t* ptr = expandBufAddSpace(pReply, width + 1);
2621 error = Dbg::GetLocalValue(*stack_visitor, soa, slot, reqSigByte, ptr, width);
2622 if (error != JDWP::ERR_NONE) {
2623 return error;
2624 }
2625 }
2626 return JDWP::ERR_NONE;
2627 });
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002628}
2629
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002630constexpr JDWP::JdwpError kStackFrameLocalAccessError = JDWP::ERR_ABSENT_INFORMATION;
2631
2632static std::string GetStackContextAsString(const StackVisitor& visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002633 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002634 return StringPrintf(" at DEX pc 0x%08x in method %s", visitor.GetDexPc(false),
David Sehr709b0702016-10-13 09:12:37 -07002635 ArtMethod::PrettyMethod(visitor.GetMethod()).c_str());
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002636}
2637
2638static JDWP::JdwpError FailGetLocalValue(const StackVisitor& visitor, uint16_t vreg,
2639 JDWP::JdwpTag tag)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002640 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002641 LOG(ERROR) << "Failed to read " << tag << " local from register v" << vreg
2642 << GetStackContextAsString(visitor);
2643 return kStackFrameLocalAccessError;
2644}
2645
Sebastien Hertz8009f392014-09-01 17:07:11 +02002646JDWP::JdwpError Dbg::GetLocalValue(const StackVisitor& visitor, ScopedObjectAccessUnchecked& soa,
2647 int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002648 ArtMethod* m = visitor.GetMethod();
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002649 JDWP::JdwpError error = JDWP::ERR_NONE;
2650 uint16_t vreg = DemangleSlot(slot, m, &error);
2651 if (error != JDWP::ERR_NONE) {
2652 return error;
2653 }
Sebastien Hertz8009f392014-09-01 17:07:11 +02002654 // TODO: check that the tag is compatible with the actual type of the slot!
Sebastien Hertz8009f392014-09-01 17:07:11 +02002655 switch (tag) {
2656 case JDWP::JT_BOOLEAN: {
2657 CHECK_EQ(width, 1U);
2658 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002659 if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) {
2660 return FailGetLocalValue(visitor, vreg, tag);
Ian Rogers0399dde2012-06-06 17:09:28 -07002661 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002662 VLOG(jdwp) << "get boolean local " << vreg << " = " << intVal;
2663 JDWP::Set1(buf + 1, intVal != 0);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002664 break;
Ian Rogers0399dde2012-06-06 17:09:28 -07002665 }
Sebastien Hertz8009f392014-09-01 17:07:11 +02002666 case JDWP::JT_BYTE: {
2667 CHECK_EQ(width, 1U);
2668 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002669 if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) {
2670 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002671 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002672 VLOG(jdwp) << "get byte local " << vreg << " = " << intVal;
2673 JDWP::Set1(buf + 1, intVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002674 break;
2675 }
2676 case JDWP::JT_SHORT:
2677 case JDWP::JT_CHAR: {
2678 CHECK_EQ(width, 2U);
2679 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002680 if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) {
2681 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002682 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002683 VLOG(jdwp) << "get short/char local " << vreg << " = " << intVal;
2684 JDWP::Set2BE(buf + 1, intVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002685 break;
2686 }
2687 case JDWP::JT_INT: {
2688 CHECK_EQ(width, 4U);
2689 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002690 if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) {
2691 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002692 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002693 VLOG(jdwp) << "get int local " << vreg << " = " << intVal;
2694 JDWP::Set4BE(buf + 1, intVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002695 break;
2696 }
2697 case JDWP::JT_FLOAT: {
2698 CHECK_EQ(width, 4U);
2699 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002700 if (!visitor.GetVReg(m, vreg, kFloatVReg, &intVal)) {
2701 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002702 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002703 VLOG(jdwp) << "get float local " << vreg << " = " << intVal;
2704 JDWP::Set4BE(buf + 1, intVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002705 break;
2706 }
2707 case JDWP::JT_ARRAY:
2708 case JDWP::JT_CLASS_LOADER:
2709 case JDWP::JT_CLASS_OBJECT:
2710 case JDWP::JT_OBJECT:
2711 case JDWP::JT_STRING:
2712 case JDWP::JT_THREAD:
2713 case JDWP::JT_THREAD_GROUP: {
2714 CHECK_EQ(width, sizeof(JDWP::ObjectId));
2715 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002716 if (!visitor.GetVReg(m, vreg, kReferenceVReg, &intVal)) {
2717 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002718 }
Vladimir Marko83114892019-04-11 13:05:50 +01002719 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(intVal);
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002720 VLOG(jdwp) << "get " << tag << " object local " << vreg << " = " << o;
Vladimir Marko83114892019-04-11 13:05:50 +01002721 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o.Ptr())) {
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002722 LOG(FATAL) << StringPrintf("Found invalid object %#" PRIxPTR " in register v%u",
Vladimir Marko83114892019-04-11 13:05:50 +01002723 reinterpret_cast<uintptr_t>(o.Ptr()), vreg)
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002724 << GetStackContextAsString(visitor);
2725 UNREACHABLE();
2726 }
2727 tag = TagFromObject(soa, o);
2728 JDWP::SetObjectId(buf + 1, gRegistry->Add(o));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002729 break;
2730 }
2731 case JDWP::JT_DOUBLE: {
2732 CHECK_EQ(width, 8U);
2733 uint64_t longVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002734 if (!visitor.GetVRegPair(m, vreg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) {
2735 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002736 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002737 VLOG(jdwp) << "get double local " << vreg << " = " << longVal;
2738 JDWP::Set8BE(buf + 1, longVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002739 break;
2740 }
2741 case JDWP::JT_LONG: {
2742 CHECK_EQ(width, 8U);
2743 uint64_t longVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002744 if (!visitor.GetVRegPair(m, vreg, kLongLoVReg, kLongHiVReg, &longVal)) {
2745 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002746 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002747 VLOG(jdwp) << "get long local " << vreg << " = " << longVal;
2748 JDWP::Set8BE(buf + 1, longVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002749 break;
2750 }
2751 default:
2752 LOG(FATAL) << "Unknown tag " << tag;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002753 UNREACHABLE();
Sebastien Hertz8009f392014-09-01 17:07:11 +02002754 }
Ian Rogers0399dde2012-06-06 17:09:28 -07002755
Sebastien Hertz8009f392014-09-01 17:07:11 +02002756 // Prepend tag, which may have been updated.
2757 JDWP::Set1(buf, tag);
2758 return JDWP::ERR_NONE;
2759}
2760
2761JDWP::JdwpError Dbg::SetLocalValues(JDWP::Request* request) {
2762 JDWP::ObjectId thread_id = request->ReadThreadId();
2763 JDWP::FrameId frame_id = request->ReadFrameId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002764
2765 ScopedObjectAccessUnchecked soa(Thread::Current());
Sebastien Hertz69206392015-04-07 15:54:25 +02002766 JDWP::JdwpError error;
2767 Thread* thread = DecodeThread(soa, thread_id, &error);
2768 if (error != JDWP::ERR_NONE) {
2769 return error;
2770 }
2771 if (!IsSuspendedForDebugger(soa, thread)) {
2772 return JDWP::ERR_THREAD_NOT_SUSPENDED;
Elliott Hughes221229c2013-01-08 18:17:50 -08002773 }
Sebastien Hertz8009f392014-09-01 17:07:11 +02002774
Andreas Gampec7d878d2018-11-19 18:42:06 +00002775 return FindAndHandleNonNativeFrame(
2776 thread,
2777 frame_id,
2778 [&](art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
2779 // Writes the values into visitor's context.
2780 int32_t slot_count = request->ReadSigned32("slot count");
2781 for (int32_t i = 0; i < slot_count; ++i) {
2782 uint32_t slot = request->ReadUnsigned32("slot");
2783 JDWP::JdwpTag sigByte = request->ReadTag();
2784 size_t width = Dbg::GetTagWidth(sigByte);
2785 uint64_t value = request->ReadValue(width);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002786
Andreas Gampec7d878d2018-11-19 18:42:06 +00002787 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
2788 error = Dbg::SetLocalValue(thread, *stack_visitor, slot, sigByte, value, width);
2789 if (error != JDWP::ERR_NONE) {
2790 return error;
2791 }
2792 }
2793 return JDWP::ERR_NONE;
2794 });
Sebastien Hertz8009f392014-09-01 17:07:11 +02002795}
2796
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002797template<typename T>
2798static JDWP::JdwpError FailSetLocalValue(const StackVisitor& visitor, uint16_t vreg,
2799 JDWP::JdwpTag tag, T value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002800 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002801 LOG(ERROR) << "Failed to write " << tag << " local " << value
2802 << " (0x" << std::hex << value << ") into register v" << vreg
2803 << GetStackContextAsString(visitor);
2804 return kStackFrameLocalAccessError;
2805}
2806
Mingyao Yang99170c62015-07-06 11:10:37 -07002807JDWP::JdwpError Dbg::SetLocalValue(Thread* thread, StackVisitor& visitor, int slot,
2808 JDWP::JdwpTag tag, uint64_t value, size_t width) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002809 ArtMethod* m = visitor.GetMethod();
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002810 JDWP::JdwpError error = JDWP::ERR_NONE;
2811 uint16_t vreg = DemangleSlot(slot, m, &error);
2812 if (error != JDWP::ERR_NONE) {
2813 return error;
2814 }
Sebastien Hertz8009f392014-09-01 17:07:11 +02002815 // TODO: check that the tag is compatible with the actual type of the slot!
Sebastien Hertz8009f392014-09-01 17:07:11 +02002816 switch (tag) {
2817 case JDWP::JT_BOOLEAN:
2818 case JDWP::JT_BYTE:
2819 CHECK_EQ(width, 1U);
Mingyao Yang636b9252015-07-31 16:40:24 -07002820 if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) {
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002821 return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002822 }
2823 break;
2824 case JDWP::JT_SHORT:
2825 case JDWP::JT_CHAR:
2826 CHECK_EQ(width, 2U);
Mingyao Yang636b9252015-07-31 16:40:24 -07002827 if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) {
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002828 return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002829 }
2830 break;
2831 case JDWP::JT_INT:
2832 CHECK_EQ(width, 4U);
Mingyao Yang636b9252015-07-31 16:40:24 -07002833 if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) {
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002834 return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002835 }
2836 break;
2837 case JDWP::JT_FLOAT:
2838 CHECK_EQ(width, 4U);
Mingyao Yang636b9252015-07-31 16:40:24 -07002839 if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kFloatVReg)) {
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002840 return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002841 }
2842 break;
2843 case JDWP::JT_ARRAY:
2844 case JDWP::JT_CLASS_LOADER:
2845 case JDWP::JT_CLASS_OBJECT:
2846 case JDWP::JT_OBJECT:
2847 case JDWP::JT_STRING:
2848 case JDWP::JT_THREAD:
2849 case JDWP::JT_THREAD_GROUP: {
2850 CHECK_EQ(width, sizeof(JDWP::ObjectId));
Vladimir Marko83114892019-04-11 13:05:50 +01002851 ObjPtr<mirror::Object> o =
2852 gRegistry->Get<mirror::Object>(static_cast<JDWP::ObjectId>(value), &error);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002853 if (error != JDWP::ERR_NONE) {
2854 VLOG(jdwp) << tag << " object " << o << " is an invalid object";
2855 return JDWP::ERR_INVALID_OBJECT;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002856 }
Vladimir Marko439d1262019-04-12 14:45:07 +01002857 if (!visitor.SetVRegReference(m, vreg, o)) {
Vladimir Marko83114892019-04-11 13:05:50 +01002858 return FailSetLocalValue(visitor, vreg, tag, reinterpret_cast<uintptr_t>(o.Ptr()));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002859 }
2860 break;
2861 }
2862 case JDWP::JT_DOUBLE: {
2863 CHECK_EQ(width, 8U);
Mingyao Yang636b9252015-07-31 16:40:24 -07002864 if (!visitor.SetVRegPair(m, vreg, value, kDoubleLoVReg, kDoubleHiVReg)) {
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002865 return FailSetLocalValue(visitor, vreg, tag, value);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002866 }
2867 break;
2868 }
2869 case JDWP::JT_LONG: {
2870 CHECK_EQ(width, 8U);
Mingyao Yang636b9252015-07-31 16:40:24 -07002871 if (!visitor.SetVRegPair(m, vreg, value, kLongLoVReg, kLongHiVReg)) {
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002872 return FailSetLocalValue(visitor, vreg, tag, value);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002873 }
2874 break;
2875 }
2876 default:
2877 LOG(FATAL) << "Unknown tag " << tag;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002878 UNREACHABLE();
Sebastien Hertz8009f392014-09-01 17:07:11 +02002879 }
Mingyao Yang99170c62015-07-06 11:10:37 -07002880
2881 // If we set the local variable in a compiled frame, we need to trigger a deoptimization of
2882 // the stack so we continue execution with the interpreter using the new value(s) of the updated
2883 // local variable(s). To achieve this, we install instrumentation exit stub on each method of the
2884 // thread's stack. The stub will cause the deoptimization to happen.
2885 if (!visitor.IsShadowFrame() && thread->HasDebuggerShadowFrames()) {
2886 Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(thread);
2887 }
2888
Sebastien Hertz8009f392014-09-01 17:07:11 +02002889 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002890}
2891
Mathieu Chartiere401d142015-04-22 13:56:20 -07002892static void SetEventLocation(JDWP::EventLocation* location, ArtMethod* m, uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002893 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02002894 DCHECK(location != nullptr);
2895 if (m == nullptr) {
2896 memset(location, 0, sizeof(*location));
2897 } else {
Alex Light97e78032017-06-27 17:51:55 -07002898 location->method = m->GetCanonicalMethod(kRuntimePointerSize);
Sebastien Hertz6995c602014-09-09 12:10:13 +02002899 location->dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint32_t>(-1) : dex_pc;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002900 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002901}
2902
Vladimir Marko83114892019-04-11 13:05:50 +01002903void Dbg::PostLocationEvent(ArtMethod* m,
2904 int dex_pc,
2905 ObjPtr<mirror::Object> this_object,
2906 int event_flags,
2907 const JValue* return_value) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002908 if (!IsDebuggerActive()) {
2909 return;
2910 }
2911 DCHECK(m != nullptr);
2912 DCHECK_EQ(m->IsStatic(), this_object == nullptr);
Sebastien Hertz6995c602014-09-09 12:10:13 +02002913 JDWP::EventLocation location;
2914 SetEventLocation(&location, m, dex_pc);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002915
Sebastien Hertzde48aa62015-05-26 11:53:39 +02002916 // We need to be sure no exception is pending when calling JdwpState::PostLocationEvent.
2917 // This is required to be able to call JNI functions to create JDWP ids. To achieve this,
2918 // we temporarily clear the current thread's exception (if any) and will restore it after
2919 // the call.
2920 // Note: the only way to get a pending exception here is to suspend on a move-exception
2921 // instruction.
2922 Thread* const self = Thread::Current();
2923 StackHandleScope<1> hs(self);
2924 Handle<mirror::Throwable> pending_exception(hs.NewHandle(self->GetException()));
2925 self->ClearException();
Andreas Gampefa4333d2017-02-14 11:10:34 -08002926 if (kIsDebugBuild && pending_exception != nullptr) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002927 const Instruction& instr = location.method->DexInstructions().InstructionAt(location.dex_pc);
2928 CHECK_EQ(Instruction::MOVE_EXCEPTION, instr.Opcode());
Sebastien Hertzde48aa62015-05-26 11:53:39 +02002929 }
2930
Sebastien Hertz6995c602014-09-09 12:10:13 +02002931 gJdwpState->PostLocationEvent(&location, this_object, event_flags, return_value);
Sebastien Hertzde48aa62015-05-26 11:53:39 +02002932
Andreas Gampefa4333d2017-02-14 11:10:34 -08002933 if (pending_exception != nullptr) {
Sebastien Hertzde48aa62015-05-26 11:53:39 +02002934 self->SetException(pending_exception.Get());
2935 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002936}
2937
Vladimir Marko83114892019-04-11 13:05:50 +01002938void Dbg::PostFieldAccessEvent(ArtMethod* m,
2939 int dex_pc,
2940 ObjPtr<mirror::Object> this_object,
2941 ArtField* f) {
Alex Lighte00ec302017-06-16 08:56:43 -07002942 // TODO We should send events for native methods.
2943 if (!IsDebuggerActive() || m->IsNative()) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002944 return;
2945 }
2946 DCHECK(m != nullptr);
2947 DCHECK(f != nullptr);
Sebastien Hertz6995c602014-09-09 12:10:13 +02002948 JDWP::EventLocation location;
2949 SetEventLocation(&location, m, dex_pc);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002950
Sebastien Hertz6995c602014-09-09 12:10:13 +02002951 gJdwpState->PostFieldEvent(&location, f, this_object, nullptr, false);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002952}
2953
Vladimir Marko83114892019-04-11 13:05:50 +01002954void Dbg::PostFieldModificationEvent(ArtMethod* m,
2955 int dex_pc,
2956 ObjPtr<mirror::Object> this_object,
2957 ArtField* f,
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002958 const JValue* field_value) {
Alex Lighte00ec302017-06-16 08:56:43 -07002959 // TODO We should send events for native methods.
2960 if (!IsDebuggerActive() || m->IsNative()) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002961 return;
2962 }
2963 DCHECK(m != nullptr);
2964 DCHECK(f != nullptr);
2965 DCHECK(field_value != nullptr);
Sebastien Hertz6995c602014-09-09 12:10:13 +02002966 JDWP::EventLocation location;
2967 SetEventLocation(&location, m, dex_pc);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002968
Sebastien Hertz6995c602014-09-09 12:10:13 +02002969 gJdwpState->PostFieldEvent(&location, f, this_object, field_value, true);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002970}
2971
Vladimir Marko83114892019-04-11 13:05:50 +01002972void Dbg::PostException(ObjPtr<mirror::Throwable> exception_object) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07002973 if (!IsDebuggerActive()) {
Ian Rogers0ad5bb82011-12-07 10:16:32 -08002974 return;
2975 }
Sebastien Hertz261bc042015-04-08 09:36:07 +02002976 Thread* const self = Thread::Current();
Andreas Gampec7d878d2018-11-19 18:42:06 +00002977 StackHandleScope<2> handle_scope(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +00002978 Handle<mirror::Throwable> h_exception(handle_scope.NewHandle(exception_object));
Andreas Gampec7d878d2018-11-19 18:42:06 +00002979 MutableHandle<mirror::Object> this_at_throw = handle_scope.NewHandle<mirror::Object>(nullptr);
Nicolas Geoffray14691c52015-03-05 10:40:17 +00002980 std::unique_ptr<Context> context(Context::Create());
Andreas Gampe3d477f32018-11-16 16:40:45 +00002981
Andreas Gampec7d878d2018-11-19 18:42:06 +00002982 ArtMethod* catch_method = nullptr;
2983 ArtMethod* throw_method = nullptr;
2984 uint32_t catch_dex_pc = dex::kDexNoIndex;
2985 uint32_t throw_dex_pc = dex::kDexNoIndex;
2986 StackVisitor::WalkStack(
2987 /**
2988 * Finds the location where this exception will be caught. We search until we reach the top
2989 * frame, in which case this exception is considered uncaught.
2990 */
2991 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
2992 ArtMethod* method = stack_visitor->GetMethod();
2993 DCHECK(method != nullptr);
2994 if (method->IsRuntimeMethod()) {
2995 // Ignore callee save method.
2996 DCHECK(method->IsCalleeSaveMethod());
2997 return true;
2998 }
2999
3000 uint32_t dex_pc = stack_visitor->GetDexPc();
3001 if (throw_method == nullptr) {
3002 // First Java method found. It is either the method that threw the exception,
3003 // or the Java native method that is reporting an exception thrown by
3004 // native code.
3005 this_at_throw.Assign(stack_visitor->GetThisObject());
3006 throw_method = method;
3007 throw_dex_pc = dex_pc;
3008 }
3009
3010 if (dex_pc != dex::kDexNoIndex) {
3011 StackHandleScope<1> hs(stack_visitor->GetThread());
3012 uint32_t found_dex_pc;
3013 Handle<mirror::Class> exception_class(hs.NewHandle(h_exception->GetClass()));
3014 bool unused_clear_exception;
3015 found_dex_pc = method->FindCatchBlock(exception_class, dex_pc, &unused_clear_exception);
3016 if (found_dex_pc != dex::kDexNoIndex) {
3017 catch_method = method;
3018 catch_dex_pc = found_dex_pc;
3019 return false; // End stack walk.
3020 }
3021 }
3022 return true; // Continue stack walk.
3023 },
3024 self,
3025 context.get(),
3026 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
3027
3028 JDWP::EventLocation exception_throw_location;
3029 SetEventLocation(&exception_throw_location, throw_method, throw_dex_pc);
3030 JDWP::EventLocation exception_catch_location;
3031 SetEventLocation(&exception_catch_location, catch_method, catch_dex_pc);
3032
3033 gJdwpState->PostException(&exception_throw_location,
3034 h_exception.Get(),
3035 &exception_catch_location,
3036 this_at_throw.Get());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003037}
3038
Vladimir Marko83114892019-04-11 13:05:50 +01003039void Dbg::PostClassPrepare(ObjPtr<mirror::Class> c) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07003040 if (!IsDebuggerActive()) {
Elliott Hughes4740cdf2011-12-07 14:07:12 -08003041 return;
3042 }
Sebastien Hertz6995c602014-09-09 12:10:13 +02003043 gJdwpState->PostClassPrepare(c);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003044}
3045
Vladimir Marko83114892019-04-11 13:05:50 +01003046void Dbg::UpdateDebugger(Thread* thread,
3047 ObjPtr<mirror::Object> this_object,
3048 ArtMethod* m,
3049 uint32_t dex_pc,
3050 int event_flags,
3051 const JValue* return_value) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003052 if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
Elliott Hughes2aa2e392012-02-17 17:15:43 -08003053 return;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08003054 }
3055
Elliott Hughes86964332012-02-15 19:37:42 -08003056 if (IsBreakpoint(m, dex_pc)) {
3057 event_flags |= kBreakpoint;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08003058 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08003059
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003060 // If the debugger is single-stepping one of our threads, check to
3061 // see if we're that thread and we've reached a step point.
3062 const SingleStepControl* single_step_control = thread->GetSingleStepControl();
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003063 if (single_step_control != nullptr) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003064 CHECK(!m->IsNative());
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003065 if (single_step_control->GetStepDepth() == JDWP::SD_INTO) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003066 // Step into method calls. We break when the line number
3067 // or method pointer changes. If we're in SS_MIN mode, we
3068 // always stop.
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003069 if (single_step_control->GetMethod() != m) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003070 event_flags |= kSingleStep;
3071 VLOG(jdwp) << "SS new method";
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003072 } else if (single_step_control->GetStepSize() == JDWP::SS_MIN) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003073 event_flags |= kSingleStep;
3074 VLOG(jdwp) << "SS new instruction";
Sebastien Hertzbb43b432014-04-14 11:59:08 +02003075 } else if (single_step_control->ContainsDexPc(dex_pc)) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003076 event_flags |= kSingleStep;
3077 VLOG(jdwp) << "SS new line";
3078 }
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003079 } else if (single_step_control->GetStepDepth() == JDWP::SD_OVER) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003080 // Step over method calls. We break when the line number is
3081 // different and the frame depth is <= the original frame
3082 // depth. (We can't just compare on the method, because we
3083 // might get unrolled past it by an exception, and it's tricky
3084 // to identify recursion.)
3085
3086 int stack_depth = GetStackDepth(thread);
3087
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003088 if (stack_depth < single_step_control->GetStackDepth()) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003089 // Popped up one or more frames, always trigger.
3090 event_flags |= kSingleStep;
3091 VLOG(jdwp) << "SS method pop";
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003092 } else if (stack_depth == single_step_control->GetStackDepth()) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003093 // Same depth, see if we moved.
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003094 if (single_step_control->GetStepSize() == JDWP::SS_MIN) {
Elliott Hughes86964332012-02-15 19:37:42 -08003095 event_flags |= kSingleStep;
3096 VLOG(jdwp) << "SS new instruction";
Sebastien Hertzbb43b432014-04-14 11:59:08 +02003097 } else if (single_step_control->ContainsDexPc(dex_pc)) {
Elliott Hughes2435a572012-02-17 16:07:41 -08003098 event_flags |= kSingleStep;
3099 VLOG(jdwp) << "SS new line";
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08003100 }
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003101 }
3102 } else {
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003103 CHECK_EQ(single_step_control->GetStepDepth(), JDWP::SD_OUT);
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003104 // Return from the current method. We break when the frame
3105 // depth pops up.
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08003106
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003107 // This differs from the "method exit" break in that it stops
3108 // with the PC at the next instruction in the returned-to
3109 // function, rather than the end of the returning function.
Elliott Hughes86964332012-02-15 19:37:42 -08003110
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003111 int stack_depth = GetStackDepth(thread);
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003112 if (stack_depth < single_step_control->GetStackDepth()) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003113 event_flags |= kSingleStep;
3114 VLOG(jdwp) << "SS method pop";
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08003115 }
3116 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08003117 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08003118
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08003119 // If there's something interesting going on, see if it matches one
3120 // of the debugger filters.
3121 if (event_flags != 0) {
Sebastien Hertz8379b222014-02-24 17:38:15 +01003122 Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08003123 }
3124}
3125
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003126size_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) {
3127 switch (instrumentation_event) {
3128 case instrumentation::Instrumentation::kMethodEntered:
3129 return &method_enter_event_ref_count_;
3130 case instrumentation::Instrumentation::kMethodExited:
3131 return &method_exit_event_ref_count_;
3132 case instrumentation::Instrumentation::kDexPcMoved:
3133 return &dex_pc_change_event_ref_count_;
3134 case instrumentation::Instrumentation::kFieldRead:
3135 return &field_read_event_ref_count_;
3136 case instrumentation::Instrumentation::kFieldWritten:
3137 return &field_write_event_ref_count_;
Alex Light6e1607e2017-08-23 10:06:18 -07003138 case instrumentation::Instrumentation::kExceptionThrown:
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003139 return &exception_catch_event_ref_count_;
3140 default:
3141 return nullptr;
3142 }
3143}
3144
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003145// Process request while all mutator threads are suspended.
3146void Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003147 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003148 switch (request.GetKind()) {
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003149 case DeoptimizationRequest::kNothing:
3150 LOG(WARNING) << "Ignoring empty deoptimization request.";
3151 break;
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003152 case DeoptimizationRequest::kRegisterForEvent:
3153 VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003154 request.InstrumentationEvent());
3155 instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent());
3156 instrumentation_events_ |= request.InstrumentationEvent();
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003157 break;
3158 case DeoptimizationRequest::kUnregisterForEvent:
3159 VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003160 request.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003161 instrumentation->RemoveListener(&gDebugInstrumentationListener,
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003162 request.InstrumentationEvent());
3163 instrumentation_events_ &= ~request.InstrumentationEvent();
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003164 break;
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003165 case DeoptimizationRequest::kFullDeoptimization:
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003166 VLOG(jdwp) << "Deoptimize the world ...";
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02003167 instrumentation->DeoptimizeEverything(kDbgInstrumentationKey);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003168 VLOG(jdwp) << "Deoptimize the world DONE";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003169 break;
3170 case DeoptimizationRequest::kFullUndeoptimization:
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003171 VLOG(jdwp) << "Undeoptimize the world ...";
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02003172 instrumentation->UndeoptimizeEverything(kDbgInstrumentationKey);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003173 VLOG(jdwp) << "Undeoptimize the world DONE";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003174 break;
3175 case DeoptimizationRequest::kSelectiveDeoptimization:
David Sehr709b0702016-10-13 09:12:37 -07003176 VLOG(jdwp) << "Deoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " ...";
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003177 instrumentation->Deoptimize(request.Method());
David Sehr709b0702016-10-13 09:12:37 -07003178 VLOG(jdwp) << "Deoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " DONE";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003179 break;
3180 case DeoptimizationRequest::kSelectiveUndeoptimization:
David Sehr709b0702016-10-13 09:12:37 -07003181 VLOG(jdwp) << "Undeoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " ...";
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003182 instrumentation->Undeoptimize(request.Method());
David Sehr709b0702016-10-13 09:12:37 -07003183 VLOG(jdwp) << "Undeoptimize method " << ArtMethod::PrettyMethod(request.Method()) << " DONE";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003184 break;
3185 default:
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003186 LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003187 UNREACHABLE();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003188 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003189}
3190
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003191void Dbg::RequestDeoptimization(const DeoptimizationRequest& req) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003192 if (req.GetKind() == DeoptimizationRequest::kNothing) {
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003193 // Nothing to do.
3194 return;
3195 }
Brian Carlstrom306db812014-09-05 13:01:41 -07003196 MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003197 RequestDeoptimizationLocked(req);
3198}
3199
3200void Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003201 switch (req.GetKind()) {
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003202 case DeoptimizationRequest::kRegisterForEvent: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003203 DCHECK_NE(req.InstrumentationEvent(), 0u);
3204 size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003205 CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003206 req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003207 if (*counter == 0) {
Sebastien Hertz7d2ae432014-05-15 11:26:34 +02003208 VLOG(jdwp) << StringPrintf("Queue request #%zd to start listening to instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003209 deoptimization_requests_.size(), req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003210 deoptimization_requests_.push_back(req);
3211 }
3212 *counter = *counter + 1;
3213 break;
3214 }
3215 case DeoptimizationRequest::kUnregisterForEvent: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003216 DCHECK_NE(req.InstrumentationEvent(), 0u);
3217 size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003218 CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003219 req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003220 *counter = *counter - 1;
3221 if (*counter == 0) {
Sebastien Hertz7d2ae432014-05-15 11:26:34 +02003222 VLOG(jdwp) << StringPrintf("Queue request #%zd to stop listening to instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003223 deoptimization_requests_.size(), req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003224 deoptimization_requests_.push_back(req);
3225 }
3226 break;
3227 }
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003228 case DeoptimizationRequest::kFullDeoptimization: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003229 DCHECK(req.Method() == nullptr);
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003230 if (full_deoptimization_event_count_ == 0) {
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003231 VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
3232 << " for full deoptimization";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003233 deoptimization_requests_.push_back(req);
3234 }
3235 ++full_deoptimization_event_count_;
3236 break;
3237 }
3238 case DeoptimizationRequest::kFullUndeoptimization: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003239 DCHECK(req.Method() == nullptr);
Sebastien Hertze713d932014-05-15 10:48:53 +02003240 DCHECK_GT(full_deoptimization_event_count_, 0U);
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003241 --full_deoptimization_event_count_;
3242 if (full_deoptimization_event_count_ == 0) {
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003243 VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
3244 << " for full undeoptimization";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003245 deoptimization_requests_.push_back(req);
3246 }
3247 break;
3248 }
3249 case DeoptimizationRequest::kSelectiveDeoptimization: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003250 DCHECK(req.Method() != nullptr);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003251 VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
David Sehr709b0702016-10-13 09:12:37 -07003252 << " for deoptimization of " << req.Method()->PrettyMethod();
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003253 deoptimization_requests_.push_back(req);
3254 break;
3255 }
3256 case DeoptimizationRequest::kSelectiveUndeoptimization: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003257 DCHECK(req.Method() != nullptr);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003258 VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
David Sehr709b0702016-10-13 09:12:37 -07003259 << " for undeoptimization of " << req.Method()->PrettyMethod();
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003260 deoptimization_requests_.push_back(req);
3261 break;
3262 }
3263 default: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003264 LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003265 UNREACHABLE();
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003266 }
3267 }
3268}
3269
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003270void Dbg::ManageDeoptimization() {
3271 Thread* const self = Thread::Current();
3272 {
3273 // Avoid suspend/resume if there is no pending request.
Brian Carlstrom306db812014-09-05 13:01:41 -07003274 MutexLock mu(self, *Locks::deoptimization_lock_);
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003275 if (deoptimization_requests_.empty()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003276 return;
3277 }
3278 }
3279 CHECK_EQ(self->GetState(), kRunnable);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07003280 ScopedThreadSuspension sts(self, kWaitingForDeoptimization);
Mathieu Chartieraa516822015-10-02 15:53:37 -07003281 // Required for ProcessDeoptimizationRequest.
3282 gc::ScopedGCCriticalSection gcs(self,
3283 gc::kGcCauseInstrumentation,
3284 gc::kCollectorTypeInstrumentation);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003285 // We need to suspend mutator threads first.
Mathieu Chartier4f55e222015-09-04 13:26:21 -07003286 ScopedSuspendAll ssa(__FUNCTION__);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003287 const ThreadState old_state = self->SetStateUnsafe(kRunnable);
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003288 {
Brian Carlstrom306db812014-09-05 13:01:41 -07003289 MutexLock mu(self, *Locks::deoptimization_lock_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003290 size_t req_index = 0;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003291 for (DeoptimizationRequest& request : deoptimization_requests_) {
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003292 VLOG(jdwp) << "Process deoptimization request #" << req_index++;
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003293 ProcessDeoptimizationRequest(request);
3294 }
3295 deoptimization_requests_.clear();
3296 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003297 CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003298}
3299
Mathieu Chartiere401d142015-04-22 13:56:20 -07003300static const Breakpoint* FindFirstBreakpointForMethod(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07003301 REQUIRES_SHARED(Locks::mutator_lock_, Locks::breakpoint_lock_) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003302 for (Breakpoint& breakpoint : gBreakpoints) {
Alex Light6c8467f2015-11-20 15:03:26 -08003303 if (breakpoint.IsInMethod(m)) {
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003304 return &breakpoint;
3305 }
3306 }
3307 return nullptr;
3308}
3309
Mathieu Chartiere401d142015-04-22 13:56:20 -07003310bool Dbg::MethodHasAnyBreakpoints(ArtMethod* method) {
Mathieu Chartierd8565452015-03-26 09:41:50 -07003311 ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
3312 return FindFirstBreakpointForMethod(method) != nullptr;
3313}
3314
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003315// Sanity checks all existing breakpoints on the same method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07003316static void SanityCheckExistingBreakpoints(ArtMethod* m,
Sebastien Hertzf3928792014-11-17 19:00:37 +01003317 DeoptimizationRequest::Kind deoptimization_kind)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07003318 REQUIRES_SHARED(Locks::mutator_lock_, Locks::breakpoint_lock_) {
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003319 for (const Breakpoint& breakpoint : gBreakpoints) {
Alex Light6c8467f2015-11-20 15:03:26 -08003320 if (breakpoint.IsInMethod(m)) {
Sebastien Hertzf3928792014-11-17 19:00:37 +01003321 CHECK_EQ(deoptimization_kind, breakpoint.GetDeoptimizationKind());
3322 }
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003323 }
Sebastien Hertzf3928792014-11-17 19:00:37 +01003324 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
3325 if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) {
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003326 // We should have deoptimized everything but not "selectively" deoptimized this method.
Sebastien Hertzf3928792014-11-17 19:00:37 +01003327 CHECK(instrumentation->AreAllMethodsDeoptimized());
3328 CHECK(!instrumentation->IsDeoptimized(m));
3329 } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003330 // We should have "selectively" deoptimized this method.
3331 // Note: while we have not deoptimized everything for this method, we may have done it for
3332 // another event.
Sebastien Hertzf3928792014-11-17 19:00:37 +01003333 CHECK(instrumentation->IsDeoptimized(m));
3334 } else {
3335 // This method does not require deoptimization.
3336 CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing);
3337 CHECK(!instrumentation->IsDeoptimized(m));
3338 }
3339}
3340
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003341// Returns the deoptimization kind required to set a breakpoint in a method.
3342// If a breakpoint has already been set, we also return the first breakpoint
3343// through the given 'existing_brkpt' pointer.
Sebastien Hertzf3928792014-11-17 19:00:37 +01003344static DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003345 ArtMethod* m,
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003346 const Breakpoint** existing_brkpt)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07003347 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzf3928792014-11-17 19:00:37 +01003348 if (!Dbg::RequiresDeoptimization()) {
3349 // We already run in interpreter-only mode so we don't need to deoptimize anything.
3350 VLOG(jdwp) << "No need for deoptimization when fully running with interpreter for method "
David Sehr709b0702016-10-13 09:12:37 -07003351 << ArtMethod::PrettyMethod(m);
Sebastien Hertzf3928792014-11-17 19:00:37 +01003352 return DeoptimizationRequest::kNothing;
3353 }
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003354 const Breakpoint* first_breakpoint;
Sebastien Hertzf3928792014-11-17 19:00:37 +01003355 {
3356 ReaderMutexLock mu(self, *Locks::breakpoint_lock_);
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003357 first_breakpoint = FindFirstBreakpointForMethod(m);
3358 *existing_brkpt = first_breakpoint;
Sebastien Hertzf3928792014-11-17 19:00:37 +01003359 }
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003360
3361 if (first_breakpoint == nullptr) {
Nicolas Geoffray6300fd72016-03-18 09:40:17 +00003362 // There is no breakpoint on this method yet: we need to deoptimize. If this method is default,
3363 // we deoptimize everything; otherwise we deoptimize only this method. We
Alex Light6c8467f2015-11-20 15:03:26 -08003364 // deoptimize with defaults because we do not know everywhere they are used. It is possible some
Nicolas Geoffray6300fd72016-03-18 09:40:17 +00003365 // of the copies could be missed.
Alex Light6c8467f2015-11-20 15:03:26 -08003366 // TODO Deoptimizing on default methods might not be necessary in all cases.
Nicolas Geoffray6300fd72016-03-18 09:40:17 +00003367 bool need_full_deoptimization = m->IsDefault();
Sebastien Hertzf3928792014-11-17 19:00:37 +01003368 if (need_full_deoptimization) {
Nicolas Geoffray6300fd72016-03-18 09:40:17 +00003369 VLOG(jdwp) << "Need full deoptimization because of copying of method "
David Sehr709b0702016-10-13 09:12:37 -07003370 << ArtMethod::PrettyMethod(m);
Sebastien Hertzf3928792014-11-17 19:00:37 +01003371 return DeoptimizationRequest::kFullDeoptimization;
3372 } else {
3373 // We don't need to deoptimize if the method has not been compiled.
Nicolas Geoffraya5891e82015-11-06 14:18:27 +00003374 const bool is_compiled = m->HasAnyCompiledCode();
Sebastien Hertzf3928792014-11-17 19:00:37 +01003375 if (is_compiled) {
David Sehr709b0702016-10-13 09:12:37 -07003376 VLOG(jdwp) << "Need selective deoptimization for compiled method "
3377 << ArtMethod::PrettyMethod(m);
Nicolas Geoffray6300fd72016-03-18 09:40:17 +00003378 return DeoptimizationRequest::kSelectiveDeoptimization;
Sebastien Hertzf3928792014-11-17 19:00:37 +01003379 } else {
3380 // Method is not compiled: we don't need to deoptimize.
David Sehr709b0702016-10-13 09:12:37 -07003381 VLOG(jdwp) << "No need for deoptimization for non-compiled method "
3382 << ArtMethod::PrettyMethod(m);
Sebastien Hertzf3928792014-11-17 19:00:37 +01003383 return DeoptimizationRequest::kNothing;
3384 }
3385 }
3386 } else {
3387 // There is at least one breakpoint for this method: we don't need to deoptimize.
3388 // Let's check that all breakpoints are configured the same way for deoptimization.
3389 VLOG(jdwp) << "Breakpoint already set: no deoptimization is required";
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003390 DeoptimizationRequest::Kind deoptimization_kind = first_breakpoint->GetDeoptimizationKind();
Sebastien Hertzf3928792014-11-17 19:00:37 +01003391 if (kIsDebugBuild) {
3392 ReaderMutexLock mu(self, *Locks::breakpoint_lock_);
3393 SanityCheckExistingBreakpoints(m, deoptimization_kind);
3394 }
3395 return DeoptimizationRequest::kNothing;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003396 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003397}
3398
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003399// Installs a breakpoint at the specified location. Also indicates through the deoptimization
3400// request if we need to deoptimize.
3401void Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
3402 Thread* const self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003403 ArtMethod* m = FromMethodId(location->method_id);
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003404 DCHECK(m != nullptr) << "No method for method id " << location->method_id;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003405
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003406 const Breakpoint* existing_breakpoint = nullptr;
3407 const DeoptimizationRequest::Kind deoptimization_kind =
3408 GetRequiredDeoptimizationKind(self, m, &existing_breakpoint);
Sebastien Hertzf3928792014-11-17 19:00:37 +01003409 req->SetKind(deoptimization_kind);
3410 if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
3411 req->SetMethod(m);
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003412 } else {
Sebastien Hertzf3928792014-11-17 19:00:37 +01003413 CHECK(deoptimization_kind == DeoptimizationRequest::kNothing ||
3414 deoptimization_kind == DeoptimizationRequest::kFullDeoptimization);
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003415 req->SetMethod(nullptr);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003416 }
3417
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003418 {
3419 WriterMutexLock mu(self, *Locks::breakpoint_lock_);
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003420 // If there is at least one existing breakpoint on the same method, the new breakpoint
3421 // must have the same deoptimization kind than the existing breakpoint(s).
3422 DeoptimizationRequest::Kind breakpoint_deoptimization_kind;
3423 if (existing_breakpoint != nullptr) {
3424 breakpoint_deoptimization_kind = existing_breakpoint->GetDeoptimizationKind();
3425 } else {
3426 breakpoint_deoptimization_kind = deoptimization_kind;
3427 }
3428 gBreakpoints.push_back(Breakpoint(m, location->dex_pc, breakpoint_deoptimization_kind));
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003429 VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": "
3430 << gBreakpoints[gBreakpoints.size() - 1];
3431 }
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003432}
3433
3434// Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization
3435// request if we need to undeoptimize.
3436void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
Sebastien Hertzed2be172014-08-19 15:33:43 +02003437 WriterMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003438 ArtMethod* m = FromMethodId(location->method_id);
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003439 DCHECK(m != nullptr) << "No method for method id " << location->method_id;
Sebastien Hertzf3928792014-11-17 19:00:37 +01003440 DeoptimizationRequest::Kind deoptimization_kind = DeoptimizationRequest::kNothing;
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003441 for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
Alex Light6c8467f2015-11-20 15:03:26 -08003442 if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].IsInMethod(m)) {
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003443 VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
Sebastien Hertzf3928792014-11-17 19:00:37 +01003444 deoptimization_kind = gBreakpoints[i].GetDeoptimizationKind();
3445 DCHECK_EQ(deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization,
3446 Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003447 gBreakpoints.erase(gBreakpoints.begin() + i);
3448 break;
3449 }
3450 }
3451 const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
3452 if (existing_breakpoint == nullptr) {
3453 // There is no more breakpoint on this method: we need to undeoptimize.
Sebastien Hertzf3928792014-11-17 19:00:37 +01003454 if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) {
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003455 // This method required full deoptimization: we need to undeoptimize everything.
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003456 req->SetKind(DeoptimizationRequest::kFullUndeoptimization);
3457 req->SetMethod(nullptr);
Sebastien Hertzf3928792014-11-17 19:00:37 +01003458 } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003459 // This method required selective deoptimization: we need to undeoptimize only that method.
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003460 req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization);
3461 req->SetMethod(m);
Sebastien Hertzf3928792014-11-17 19:00:37 +01003462 } else {
3463 // This method had no need for deoptimization: do nothing.
3464 CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing);
3465 req->SetKind(DeoptimizationRequest::kNothing);
3466 req->SetMethod(nullptr);
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003467 }
3468 } else {
3469 // There is at least one breakpoint for this method: we don't need to undeoptimize.
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003470 req->SetKind(DeoptimizationRequest::kNothing);
3471 req->SetMethod(nullptr);
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003472 if (kIsDebugBuild) {
Sebastien Hertzf3928792014-11-17 19:00:37 +01003473 SanityCheckExistingBreakpoints(m, deoptimization_kind);
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003474 }
Elliott Hughes86964332012-02-15 19:37:42 -08003475 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003476}
3477
Mathieu Chartiere401d142015-04-22 13:56:20 -07003478bool Dbg::IsForcedInterpreterNeededForCallingImpl(Thread* thread, ArtMethod* m) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02003479 const SingleStepControl* const ssc = thread->GetSingleStepControl();
3480 if (ssc == nullptr) {
3481 // If we are not single-stepping, then we don't have to force interpreter.
3482 return false;
3483 }
3484 if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) {
3485 // If we are in interpreter only mode, then we don't have to force interpreter.
3486 return false;
3487 }
3488
3489 if (!m->IsNative() && !m->IsProxyMethod()) {
3490 // If we want to step into a method, then we have to force interpreter on that call.
3491 if (ssc->GetStepDepth() == JDWP::SD_INTO) {
3492 return true;
3493 }
3494 }
3495 return false;
3496}
3497
Mathieu Chartiere401d142015-04-22 13:56:20 -07003498bool Dbg::IsForcedInterpreterNeededForResolutionImpl(Thread* thread, ArtMethod* m) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02003499 instrumentation::Instrumentation* const instrumentation =
3500 Runtime::Current()->GetInstrumentation();
3501 // If we are in interpreter only mode, then we don't have to force interpreter.
3502 if (instrumentation->InterpretOnly()) {
3503 return false;
3504 }
3505 // We can only interpret pure Java method.
3506 if (m->IsNative() || m->IsProxyMethod()) {
3507 return false;
3508 }
3509 const SingleStepControl* const ssc = thread->GetSingleStepControl();
3510 if (ssc != nullptr) {
3511 // If we want to step into a method, then we have to force interpreter on that call.
3512 if (ssc->GetStepDepth() == JDWP::SD_INTO) {
3513 return true;
3514 }
3515 // If we are stepping out from a static initializer, by issuing a step
3516 // in or step over, that was implicitly invoked by calling a static method,
3517 // then we need to step into that method. Having a lower stack depth than
3518 // the one the single step control has indicates that the step originates
3519 // from the static initializer.
3520 if (ssc->GetStepDepth() != JDWP::SD_OUT &&
3521 ssc->GetStackDepth() > GetStackDepth(thread)) {
3522 return true;
3523 }
3524 }
3525 // There are cases where we have to force interpreter on deoptimized methods,
3526 // because in some cases the call will not be performed by invoking an entry
3527 // point that has been replaced by the deoptimization, but instead by directly
3528 // invoking the compiled code of the method, for example.
3529 return instrumentation->IsDeoptimized(m);
3530}
3531
Mathieu Chartiere401d142015-04-22 13:56:20 -07003532bool Dbg::IsForcedInstrumentationNeededForResolutionImpl(Thread* thread, ArtMethod* m) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003533 // The upcall can be null and in that case we don't need to do anything.
Daniel Mihalyieb076692014-08-22 17:33:31 +02003534 if (m == nullptr) {
3535 return false;
3536 }
3537 instrumentation::Instrumentation* const instrumentation =
3538 Runtime::Current()->GetInstrumentation();
3539 // If we are in interpreter only mode, then we don't have to force interpreter.
3540 if (instrumentation->InterpretOnly()) {
3541 return false;
3542 }
3543 // We can only interpret pure Java method.
3544 if (m->IsNative() || m->IsProxyMethod()) {
3545 return false;
3546 }
3547 const SingleStepControl* const ssc = thread->GetSingleStepControl();
3548 if (ssc != nullptr) {
3549 // If we are stepping out from a static initializer, by issuing a step
3550 // out, that was implicitly invoked by calling a static method, then we
3551 // need to step into the caller of that method. Having a lower stack
3552 // depth than the one the single step control has indicates that the
3553 // step originates from the static initializer.
3554 if (ssc->GetStepDepth() == JDWP::SD_OUT &&
3555 ssc->GetStackDepth() > GetStackDepth(thread)) {
3556 return true;
3557 }
3558 }
3559 // If we are returning from a static intializer, that was implicitly
3560 // invoked by calling a static method and the caller is deoptimized,
3561 // then we have to deoptimize the stack without forcing interpreter
3562 // on the static method that was called originally. This problem can
3563 // be solved easily by forcing instrumentation on the called method,
3564 // because the instrumentation exit hook will recognise the need of
3565 // stack deoptimization by calling IsForcedInterpreterNeededForUpcall.
3566 return instrumentation->IsDeoptimized(m);
3567}
3568
Mathieu Chartiere401d142015-04-22 13:56:20 -07003569bool Dbg::IsForcedInterpreterNeededForUpcallImpl(Thread* thread, ArtMethod* m) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003570 // The upcall can be null and in that case we don't need to do anything.
Daniel Mihalyieb076692014-08-22 17:33:31 +02003571 if (m == nullptr) {
3572 return false;
3573 }
3574 instrumentation::Instrumentation* const instrumentation =
3575 Runtime::Current()->GetInstrumentation();
3576 // If we are in interpreter only mode, then we don't have to force interpreter.
3577 if (instrumentation->InterpretOnly()) {
3578 return false;
3579 }
3580 // We can only interpret pure Java method.
3581 if (m->IsNative() || m->IsProxyMethod()) {
3582 return false;
3583 }
3584 const SingleStepControl* const ssc = thread->GetSingleStepControl();
3585 if (ssc != nullptr) {
3586 // The debugger is not interested in what is happening under the level
3587 // of the step, thus we only force interpreter when we are not below of
3588 // the step.
3589 if (ssc->GetStackDepth() >= GetStackDepth(thread)) {
3590 return true;
3591 }
3592 }
Mingyao Yang99170c62015-07-06 11:10:37 -07003593 if (thread->HasDebuggerShadowFrames()) {
3594 // We need to deoptimize the stack for the exception handling flow so that
3595 // we don't miss any deoptimization that should be done when there are
3596 // debugger shadow frames.
3597 return true;
3598 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02003599 // We have to require stack deoptimization if the upcall is deoptimized.
3600 return instrumentation->IsDeoptimized(m);
3601}
3602
Sebastien Hertz520633b2015-09-08 17:03:36 +02003603// Do we need to deoptimize the stack to handle an exception?
3604bool Dbg::IsForcedInterpreterNeededForExceptionImpl(Thread* thread) {
3605 const SingleStepControl* const ssc = thread->GetSingleStepControl();
3606 if (ssc != nullptr) {
3607 // We deopt to step into the catch handler.
3608 return true;
3609 }
3610 // Deoptimization is required if at least one method in the stack needs it. However we
3611 // skip frames that will be unwound (thus not executed).
Andreas Gampec7d878d2018-11-19 18:42:06 +00003612 bool needs_deoptimization = false;
3613 StackVisitor::WalkStack(
3614 [&](art::StackVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
3615 // The visitor is meant to be used when handling exception from compiled code only.
3616 CHECK(!visitor->IsShadowFrame()) << "We only expect to visit compiled frame: "
3617 << ArtMethod::PrettyMethod(visitor->GetMethod());
3618 ArtMethod* method = visitor->GetMethod();
3619 if (method == nullptr) {
3620 // We reach an upcall and don't need to deoptimize this part of the stack (ManagedFragment)
3621 // so we can stop the visit.
3622 DCHECK(!needs_deoptimization);
3623 return false;
3624 }
3625 if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) {
3626 // We found a compiled frame in the stack but instrumentation is set to interpret
3627 // everything: we need to deoptimize.
3628 needs_deoptimization = true;
3629 return false;
3630 }
3631 if (Runtime::Current()->GetInstrumentation()->IsDeoptimized(method)) {
3632 // We found a deoptimized method in the stack.
3633 needs_deoptimization = true;
3634 return false;
3635 }
3636 ShadowFrame* frame = visitor->GetThread()->FindDebuggerShadowFrame(visitor->GetFrameId());
3637 if (frame != nullptr) {
3638 // The debugger allocated a ShadowFrame to update a variable in the stack: we need to
3639 // deoptimize the stack to execute (and deallocate) this frame.
3640 needs_deoptimization = true;
3641 return false;
3642 }
3643 return true;
3644 },
3645 thread,
3646 /* context= */ nullptr,
3647 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames,
3648 /* check_suspended */ true,
3649 /* include_transitions */ true);
3650 return needs_deoptimization;
Sebastien Hertz520633b2015-09-08 17:03:36 +02003651}
3652
Jeff Hao449db332013-04-12 18:30:52 -07003653// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
3654// cause suspension if the thread is the current thread.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07003655class ScopedDebuggerThreadSuspension {
Jeff Hao449db332013-04-12 18:30:52 -07003656 public:
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07003657 ScopedDebuggerThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
Mathieu Chartier90443472015-07-16 20:32:27 -07003658 REQUIRES(!Locks::thread_list_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07003659 REQUIRES_SHARED(Locks::mutator_lock_) :
Ian Rogersf3d874c2014-07-17 18:52:42 -07003660 thread_(nullptr),
Jeff Hao449db332013-04-12 18:30:52 -07003661 error_(JDWP::ERR_NONE),
3662 self_suspend_(false),
Ian Rogers33e95662013-05-20 20:29:14 -07003663 other_suspend_(false) {
Jeff Hao449db332013-04-12 18:30:52 -07003664 ScopedObjectAccessUnchecked soa(self);
Sebastien Hertz69206392015-04-07 15:54:25 +02003665 thread_ = DecodeThread(soa, thread_id, &error_);
Jeff Hao449db332013-04-12 18:30:52 -07003666 if (error_ == JDWP::ERR_NONE) {
3667 if (thread_ == soa.Self()) {
3668 self_suspend_ = true;
3669 } else {
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07003670 Thread* suspended_thread;
3671 {
3672 ScopedThreadSuspension sts(self, kWaitingForDebuggerSuspension);
3673 jobject thread_peer = Dbg::GetObjectRegistry()->GetJObject(thread_id);
3674 bool timed_out;
3675 ThreadList* const thread_list = Runtime::Current()->GetThreadList();
Sebastien Hertzcdd798d2017-04-18 18:28:51 +02003676 suspended_thread = thread_list->SuspendThreadByPeer(thread_peer,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07003677 /* request_suspension= */ true,
Alex Light46f93402017-06-29 11:59:50 -07003678 SuspendReason::kForDebugger,
Sebastien Hertzcdd798d2017-04-18 18:28:51 +02003679 &timed_out);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07003680 }
Ian Rogersf3d874c2014-07-17 18:52:42 -07003681 if (suspended_thread == nullptr) {
Jeff Hao449db332013-04-12 18:30:52 -07003682 // Thread terminated from under us while suspending.
3683 error_ = JDWP::ERR_INVALID_THREAD;
3684 } else {
3685 CHECK_EQ(suspended_thread, thread_);
3686 other_suspend_ = true;
3687 }
3688 }
3689 }
Elliott Hughes2435a572012-02-17 16:07:41 -08003690 }
Elliott Hughes86964332012-02-15 19:37:42 -08003691
Jeff Hao449db332013-04-12 18:30:52 -07003692 Thread* GetThread() const {
3693 return thread_;
3694 }
3695
3696 JDWP::JdwpError GetError() const {
3697 return error_;
3698 }
3699
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07003700 ~ScopedDebuggerThreadSuspension() {
Jeff Hao449db332013-04-12 18:30:52 -07003701 if (other_suspend_) {
Alex Light88fd7202017-06-30 08:31:59 -07003702 bool resumed = Runtime::Current()->GetThreadList()->Resume(thread_,
3703 SuspendReason::kForDebugger);
3704 DCHECK(resumed);
Jeff Hao449db332013-04-12 18:30:52 -07003705 }
3706 }
3707
3708 private:
3709 Thread* thread_;
3710 JDWP::JdwpError error_;
3711 bool self_suspend_;
3712 bool other_suspend_;
3713};
3714
3715JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
3716 JDWP::JdwpStepDepth step_depth) {
3717 Thread* self = Thread::Current();
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07003718 ScopedDebuggerThreadSuspension sts(self, thread_id);
Jeff Hao449db332013-04-12 18:30:52 -07003719 if (sts.GetError() != JDWP::ERR_NONE) {
3720 return sts.GetError();
3721 }
3722
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003723 // Work out what ArtMethod* we're in, the current line number, and how deep the stack currently
Elliott Hughes2435a572012-02-17 16:07:41 -08003724 // is for step-out.
Ian Rogers0399dde2012-06-06 17:09:28 -07003725 struct SingleStepStackVisitor : public StackVisitor {
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07003726 explicit SingleStepStackVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01003727 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
3728 stack_depth(0),
3729 method(nullptr),
3730 line_number(-1) {}
Ian Rogersca190662012-06-26 15:45:57 -07003731
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003732 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
3733 // annotalysis.
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003734 bool VisitFrame() override NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003735 ArtMethod* m = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -07003736 if (!m->IsRuntimeMethod()) {
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003737 ++stack_depth;
3738 if (method == nullptr) {
Alex Light73376312017-04-06 10:10:51 -07003739 const DexFile* dex_file = m->GetDexFile();
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003740 method = m;
Alex Light73376312017-04-06 10:10:51 -07003741 if (dex_file != nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -07003742 line_number = annotations::GetLineNumFromPC(dex_file, m, GetDexPc());
Elliott Hughes2435a572012-02-17 16:07:41 -08003743 }
Elliott Hughes86964332012-02-15 19:37:42 -08003744 }
3745 }
Elliott Hughes530fa002012-03-12 11:44:49 -07003746 return true;
Elliott Hughes86964332012-02-15 19:37:42 -08003747 }
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003748
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003749 int stack_depth;
Mathieu Chartiere401d142015-04-22 13:56:20 -07003750 ArtMethod* method;
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003751 int32_t line_number;
Elliott Hughes86964332012-02-15 19:37:42 -08003752 };
Jeff Hao449db332013-04-12 18:30:52 -07003753
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003754 Thread* const thread = sts.GetThread();
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003755 SingleStepStackVisitor visitor(thread);
Ian Rogers0399dde2012-06-06 17:09:28 -07003756 visitor.WalkStack();
Elliott Hughes86964332012-02-15 19:37:42 -08003757
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003758 // Allocate single step.
Sebastien Hertz1558b572015-02-25 15:05:59 +01003759 SingleStepControl* single_step_control =
3760 new (std::nothrow) SingleStepControl(step_size, step_depth,
3761 visitor.stack_depth, visitor.method);
3762 if (single_step_control == nullptr) {
3763 LOG(ERROR) << "Failed to allocate SingleStepControl";
3764 return JDWP::ERR_OUT_OF_MEMORY;
3765 }
3766
Mathieu Chartiere401d142015-04-22 13:56:20 -07003767 ArtMethod* m = single_step_control->GetMethod();
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003768 const int32_t line_number = visitor.line_number;
Sebastien Hertz52f5f932015-05-28 11:00:57 +02003769 // Note: if the thread is not running Java code (pure native thread), there is no "current"
3770 // method on the stack (and no line number either).
3771 if (m != nullptr && !m->IsNative()) {
David Sehr0225f8e2018-01-31 08:52:24 +00003772 CodeItemDebugInfoAccessor accessor(m->DexInstructionDebugInfo());
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07003773 bool last_pc_valid = false;
3774 uint32_t last_pc = 0u;
3775 // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
3776 accessor.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
3777 if (static_cast<int32_t>(entry.line_) == line_number) {
3778 if (!last_pc_valid) {
3779 // Everything from this address until the next line change is ours.
3780 last_pc = entry.address_;
3781 last_pc_valid = true;
3782 }
3783 // Otherwise, if we're already in a valid range for this line,
3784 // just keep going (shouldn't really happen)...
3785 } else if (last_pc_valid) { // and the line number is new
3786 // Add everything from the last entry up until here to the set
3787 for (uint32_t dex_pc = last_pc; dex_pc < entry.address_; ++dex_pc) {
3788 single_step_control->AddDexPc(dex_pc);
3789 }
3790 last_pc_valid = false;
3791 }
3792 return false; // There may be multiple entries for any given line.
3793 });
3794 // If the line number was the last in the position table...
3795 if (last_pc_valid) {
3796 for (uint32_t dex_pc = last_pc; dex_pc < accessor.InsnsSizeInCodeUnits(); ++dex_pc) {
3797 single_step_control->AddDexPc(dex_pc);
3798 }
3799 }
Elliott Hughes3e2e1a22012-02-21 11:33:41 -08003800 }
Elliott Hughes2435a572012-02-17 16:07:41 -08003801
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003802 // Activate single-step in the thread.
3803 thread->ActivateSingleStepControl(single_step_control);
Elliott Hughes86964332012-02-15 19:37:42 -08003804
Elliott Hughes2435a572012-02-17 16:07:41 -08003805 if (VLOG_IS_ON(jdwp)) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003806 VLOG(jdwp) << "Single-step thread: " << *thread;
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003807 VLOG(jdwp) << "Single-step step size: " << single_step_control->GetStepSize();
3808 VLOG(jdwp) << "Single-step step depth: " << single_step_control->GetStepDepth();
David Sehr709b0702016-10-13 09:12:37 -07003809 VLOG(jdwp) << "Single-step current method: "
3810 << ArtMethod::PrettyMethod(single_step_control->GetMethod());
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003811 VLOG(jdwp) << "Single-step current line: " << line_number;
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003812 VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->GetStackDepth();
Elliott Hughes2435a572012-02-17 16:07:41 -08003813 VLOG(jdwp) << "Single-step dex_pc values:";
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003814 for (uint32_t dex_pc : single_step_control->GetDexPcs()) {
Sebastien Hertzbb43b432014-04-14 11:59:08 +02003815 VLOG(jdwp) << StringPrintf(" %#x", dex_pc);
Elliott Hughes2435a572012-02-17 16:07:41 -08003816 }
3817 }
3818
3819 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003820}
3821
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003822void Dbg::UnconfigureStep(JDWP::ObjectId thread_id) {
3823 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07003824 JDWP::JdwpError error;
3825 Thread* thread = DecodeThread(soa, thread_id, &error);
Sebastien Hertz87118ed2013-11-26 17:57:18 +01003826 if (error == JDWP::ERR_NONE) {
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003827 thread->DeactivateSingleStepControl();
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003828 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003829}
3830
Elliott Hughes45651fd2012-02-21 15:48:20 -08003831static char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
3832 switch (tag) {
3833 default:
3834 LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
Ian Rogersfc787ec2014-10-09 21:56:44 -07003835 UNREACHABLE();
Elliott Hughes45651fd2012-02-21 15:48:20 -08003836
3837 // Primitives.
3838 case JDWP::JT_BYTE: return 'B';
3839 case JDWP::JT_CHAR: return 'C';
3840 case JDWP::JT_FLOAT: return 'F';
3841 case JDWP::JT_DOUBLE: return 'D';
3842 case JDWP::JT_INT: return 'I';
3843 case JDWP::JT_LONG: return 'J';
3844 case JDWP::JT_SHORT: return 'S';
3845 case JDWP::JT_VOID: return 'V';
3846 case JDWP::JT_BOOLEAN: return 'Z';
3847
3848 // Reference types.
3849 case JDWP::JT_ARRAY:
3850 case JDWP::JT_OBJECT:
3851 case JDWP::JT_STRING:
3852 case JDWP::JT_THREAD:
3853 case JDWP::JT_THREAD_GROUP:
3854 case JDWP::JT_CLASS_LOADER:
3855 case JDWP::JT_CLASS_OBJECT:
3856 return 'L';
3857 }
3858}
3859
Sebastien Hertzcbc50642015-06-01 17:33:12 +02003860JDWP::JdwpError Dbg::PrepareInvokeMethod(uint32_t request_id, JDWP::ObjectId thread_id,
3861 JDWP::ObjectId object_id, JDWP::RefTypeId class_id,
3862 JDWP::MethodId method_id, uint32_t arg_count,
3863 uint64_t arg_values[], JDWP::JdwpTag* arg_types,
3864 uint32_t options) {
3865 Thread* const self = Thread::Current();
3866 CHECK_EQ(self, GetDebugThread()) << "This must be called by the JDWP thread";
Sebastien Hertzd4032e42015-06-12 15:47:34 +02003867 const bool resume_all_threads = ((options & JDWP::INVOKE_SINGLE_THREADED) == 0);
Elliott Hughesd07986f2011-12-06 18:27:45 -08003868
Sebastien Hertzcbc50642015-06-01 17:33:12 +02003869 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Ian Rogersc0542af2014-09-03 16:16:56 -07003870 Thread* targetThread = nullptr;
Elliott Hughesd07986f2011-12-06 18:27:45 -08003871 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003872 ScopedObjectAccessUnchecked soa(self);
Ian Rogersc0542af2014-09-03 16:16:56 -07003873 JDWP::JdwpError error;
3874 targetThread = DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08003875 if (error != JDWP::ERR_NONE) {
3876 LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
3877 return error;
Elliott Hughesd07986f2011-12-06 18:27:45 -08003878 }
Sebastien Hertz1558b572015-02-25 15:05:59 +01003879 if (targetThread->GetInvokeReq() != nullptr) {
3880 // Thread is already invoking a method on behalf of the debugger.
3881 LOG(ERROR) << "InvokeMethod request for thread already invoking a method: " << *targetThread;
3882 return JDWP::ERR_ALREADY_INVOKING;
3883 }
3884 if (!targetThread->IsReadyForDebugInvoke()) {
3885 // Thread is not suspended by an event so it cannot invoke a method.
Elliott Hughesd07986f2011-12-06 18:27:45 -08003886 LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
3887 return JDWP::ERR_INVALID_THREAD;
3888 }
3889
3890 /*
Sebastien Hertzd4032e42015-06-12 15:47:34 +02003891 * According to the JDWP specs, we are expected to resume all threads (or only the
3892 * target thread) once. So if a thread has been suspended more than once (either by
3893 * the debugger for an event or by the runtime for GC), it will remain suspended before
3894 * the invoke is executed. This means the debugger is responsible to properly resume all
3895 * the threads it has suspended so the target thread can execute the method.
Elliott Hughesd07986f2011-12-06 18:27:45 -08003896 *
Sebastien Hertzd4032e42015-06-12 15:47:34 +02003897 * However, for compatibility reason with older versions of debuggers (like Eclipse), we
3898 * fully resume all threads (by canceling *all* debugger suspensions) when the debugger
3899 * wants us to resume all threads. This is to avoid ending up in deadlock situation.
3900 *
3901 * On the other hand, if we are asked to only resume the target thread, then we follow the
3902 * JDWP specs by resuming that thread only once. This means the thread will remain suspended
3903 * if it has been suspended more than once before the invoke (and again, this is the
3904 * responsibility of the debugger to properly resume that thread before invoking a method).
Elliott Hughesd07986f2011-12-06 18:27:45 -08003905 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003906 int suspend_count;
3907 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003908 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003909 suspend_count = targetThread->GetSuspendCount();
3910 }
Sebastien Hertzd4032e42015-06-12 15:47:34 +02003911 if (suspend_count > 1 && resume_all_threads) {
3912 // The target thread will remain suspended even after we resume it. Let's emit a warning
3913 // to indicate the invoke won't be executed until the thread is resumed.
3914 LOG(WARNING) << *targetThread << " suspended more than once (suspend count == "
3915 << suspend_count << "). This thread will invoke the method only once "
3916 << "it is fully resumed.";
Elliott Hughesd07986f2011-12-06 18:27:45 -08003917 }
3918
Vladimir Marko83114892019-04-11 13:05:50 +01003919 ObjPtr<mirror::Object> receiver = gRegistry->Get<mirror::Object>(object_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07003920 if (error != JDWP::ERR_NONE) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08003921 return JDWP::ERR_INVALID_OBJECT;
3922 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08003923
Vladimir Marko83114892019-04-11 13:05:50 +01003924 gRegistry->Get<mirror::Object>(thread_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07003925 if (error != JDWP::ERR_NONE) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08003926 return JDWP::ERR_INVALID_OBJECT;
3927 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08003928
Vladimir Marko4617d582019-03-28 13:48:31 +00003929 ObjPtr<mirror::Class> c = DecodeClass(class_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07003930 if (c == nullptr) {
3931 return error;
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08003932 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08003933
Mathieu Chartiere401d142015-04-22 13:56:20 -07003934 ArtMethod* m = FromMethodId(method_id);
Ian Rogersc0542af2014-09-03 16:16:56 -07003935 if (m->IsStatic() != (receiver == nullptr)) {
Elliott Hughes45651fd2012-02-21 15:48:20 -08003936 return JDWP::ERR_INVALID_METHODID;
3937 }
3938 if (m->IsStatic()) {
3939 if (m->GetDeclaringClass() != c) {
3940 return JDWP::ERR_INVALID_METHODID;
3941 }
3942 } else {
3943 if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
3944 return JDWP::ERR_INVALID_METHODID;
3945 }
3946 }
3947
3948 // Check the argument list matches the method.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003949 uint32_t shorty_len = 0;
3950 const char* shorty = m->GetShorty(&shorty_len);
3951 if (shorty_len - 1 != arg_count) {
Elliott Hughes45651fd2012-02-21 15:48:20 -08003952 return JDWP::ERR_ILLEGAL_ARGUMENT;
3953 }
Elliott Hughes09201632013-04-15 15:50:07 -07003954
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003955 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003956 StackHandleScope<2> hs(soa.Self());
Vladimir Marko4617d582019-03-28 13:48:31 +00003957 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver));
3958 HandleWrapperObjPtr<mirror::Class> h_klass(hs.NewHandleWrapper(&c));
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003959 const dex::TypeList* types = m->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003960 for (size_t i = 0; i < arg_count; ++i) {
3961 if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
Elliott Hughes09201632013-04-15 15:50:07 -07003962 return JDWP::ERR_ILLEGAL_ARGUMENT;
3963 }
3964
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003965 if (shorty[i + 1] == 'L') {
3966 // Did we really get an argument of an appropriate reference type?
Vladimir Markob45528c2017-07-27 14:14:28 +01003967 ObjPtr<mirror::Class> parameter_type =
3968 m->ResolveClassFromTypeIndex(types->GetTypeItem(i).type_idx_);
Vladimir Marko83114892019-04-11 13:05:50 +01003969 ObjPtr<mirror::Object> argument = gRegistry->Get<mirror::Object>(arg_values[i], &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07003970 if (error != JDWP::ERR_NONE) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003971 return JDWP::ERR_INVALID_OBJECT;
3972 }
Ian Rogersc0542af2014-09-03 16:16:56 -07003973 if (argument != nullptr && !argument->InstanceOf(parameter_type)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003974 return JDWP::ERR_ILLEGAL_ARGUMENT;
3975 }
3976
3977 // Turn the on-the-wire ObjectId into a jobject.
3978 jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
3979 v.l = gRegistry->GetJObject(arg_values[i]);
3980 }
Elliott Hughes09201632013-04-15 15:50:07 -07003981 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08003982 }
3983
Sebastien Hertz1558b572015-02-25 15:05:59 +01003984 // Allocates a DebugInvokeReq.
Vladimir Marko4617d582019-03-28 13:48:31 +00003985 DebugInvokeReq* req = new (std::nothrow) DebugInvokeReq(
3986 request_id, thread_id, receiver, c, m, options, arg_values, arg_count);
Sebastien Hertzcbc50642015-06-01 17:33:12 +02003987 if (req == nullptr) {
Sebastien Hertz1558b572015-02-25 15:05:59 +01003988 LOG(ERROR) << "Failed to allocate DebugInvokeReq";
3989 return JDWP::ERR_OUT_OF_MEMORY;
3990 }
3991
Sebastien Hertzcbc50642015-06-01 17:33:12 +02003992 // Attaches the DebugInvokeReq to the target thread so it executes the method when
3993 // it is resumed. Once the invocation completes, the target thread will delete it before
3994 // suspending itself (see ThreadList::SuspendSelfForDebugger).
3995 targetThread->SetDebugInvokeReq(req);
Elliott Hughesd07986f2011-12-06 18:27:45 -08003996 }
3997
3998 // The fact that we've released the thread list lock is a bit risky --- if the thread goes
Sebastien Hertzcbc50642015-06-01 17:33:12 +02003999 // away we're sitting high and dry -- but we must release this before the UndoDebuggerSuspensions
4000 // call.
Sebastien Hertzd4032e42015-06-12 15:47:34 +02004001 if (resume_all_threads) {
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004002 VLOG(jdwp) << " Resuming all threads";
4003 thread_list->UndoDebuggerSuspensions();
4004 } else {
4005 VLOG(jdwp) << " Resuming event thread only";
Alex Light88fd7202017-06-30 08:31:59 -07004006 bool resumed = thread_list->Resume(targetThread, SuspendReason::kForDebugger);
4007 DCHECK(resumed);
Elliott Hughesd07986f2011-12-06 18:27:45 -08004008 }
4009
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004010 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004011}
4012
4013void Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004014 Thread* const self = Thread::Current();
4015 CHECK_NE(self, GetDebugThread()) << "This must be called by the event thread";
4016
4017 ScopedObjectAccess soa(self);
Elliott Hughesd07986f2011-12-06 18:27:45 -08004018
Elliott Hughes81ff3182012-03-23 20:35:56 -07004019 // We can be called while an exception is pending. We need
Elliott Hughesd07986f2011-12-06 18:27:45 -08004020 // to preserve that across the method invocation.
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004021 StackHandleScope<1> hs(soa.Self());
4022 Handle<mirror::Throwable> old_exception = hs.NewHandle(soa.Self()->GetException());
Sebastien Hertz1558b572015-02-25 15:05:59 +01004023 soa.Self()->ClearException();
Elliott Hughesd07986f2011-12-06 18:27:45 -08004024
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004025 // Execute the method then sends reply to the debugger.
4026 ExecuteMethodWithoutPendingException(soa, pReq);
4027
4028 // If an exception was pending before the invoke, restore it now.
Andreas Gampefa4333d2017-02-14 11:10:34 -08004029 if (old_exception != nullptr) {
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004030 soa.Self()->SetException(old_exception.Get());
4031 }
4032}
4033
4034// Helper function: write a variable-width value into the output input buffer.
4035static void WriteValue(JDWP::ExpandBuf* pReply, int width, uint64_t value) {
4036 switch (width) {
4037 case 1:
4038 expandBufAdd1(pReply, value);
4039 break;
4040 case 2:
4041 expandBufAdd2BE(pReply, value);
4042 break;
4043 case 4:
4044 expandBufAdd4BE(pReply, value);
4045 break;
4046 case 8:
4047 expandBufAdd8BE(pReply, value);
4048 break;
4049 default:
4050 LOG(FATAL) << width;
4051 UNREACHABLE();
4052 }
4053}
4054
4055void Dbg::ExecuteMethodWithoutPendingException(ScopedObjectAccess& soa, DebugInvokeReq* pReq) {
4056 soa.Self()->AssertNoPendingException();
4057
Elliott Hughesd07986f2011-12-06 18:27:45 -08004058 // Translate the method through the vtable, unless the debugger wants to suppress it.
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004059 ArtMethod* m = pReq->method;
Andreas Gampe542451c2016-07-26 09:02:02 -07004060 PointerSize image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Sebastien Hertz1558b572015-02-25 15:05:59 +01004061 if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver.Read() != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004062 ArtMethod* actual_method =
4063 pReq->klass.Read()->FindVirtualMethodForVirtualOrInterface(m, image_pointer_size);
4064 if (actual_method != m) {
David Sehr709b0702016-10-13 09:12:37 -07004065 VLOG(jdwp) << "ExecuteMethod translated " << ArtMethod::PrettyMethod(m)
4066 << " to " << ArtMethod::PrettyMethod(actual_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004067 m = actual_method;
Elliott Hughes45651fd2012-02-21 15:48:20 -08004068 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08004069 }
David Sehr709b0702016-10-13 09:12:37 -07004070 VLOG(jdwp) << "ExecuteMethod " << ArtMethod::PrettyMethod(m)
Sebastien Hertz1558b572015-02-25 15:05:59 +01004071 << " receiver=" << pReq->receiver.Read()
Sebastien Hertzd38667a2013-11-25 15:43:54 +01004072 << " arg_count=" << pReq->arg_count;
Mathieu Chartiere401d142015-04-22 13:56:20 -07004073 CHECK(m != nullptr);
Elliott Hughesd07986f2011-12-06 18:27:45 -08004074
Roland Levillain33d69032015-06-18 18:20:59 +01004075 static_assert(sizeof(jvalue) == sizeof(uint64_t), "jvalue and uint64_t have different sizes.");
Elliott Hughesd07986f2011-12-06 18:27:45 -08004076
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004077 // Invoke the method.
Jeff Hao39b6c242015-05-19 20:30:23 -07004078 ScopedLocalRef<jobject> ref(soa.Env(), soa.AddLocalReference<jobject>(pReq->receiver.Read()));
Andreas Gampe13b27842016-11-07 16:48:23 -08004079 JValue result = InvokeWithJValues(soa, ref.get(), jni::EncodeArtMethod(m),
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004080 reinterpret_cast<jvalue*>(pReq->arg_values.get()));
Elliott Hughesd07986f2011-12-06 18:27:45 -08004081
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004082 // Prepare JDWP ids for the reply.
4083 JDWP::JdwpTag result_tag = BasicTagFromDescriptor(m->GetShorty());
4084 const bool is_object_result = (result_tag == JDWP::JT_OBJECT);
Jeff Hao064d24e2016-08-25 03:52:40 +00004085 StackHandleScope<3> hs(soa.Self());
Sebastien Hertz1558b572015-02-25 15:05:59 +01004086 Handle<mirror::Object> object_result = hs.NewHandle(is_object_result ? result.GetL() : nullptr);
4087 Handle<mirror::Throwable> exception = hs.NewHandle(soa.Self()->GetException());
4088 soa.Self()->ClearException();
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004089
4090 if (!IsDebuggerActive()) {
4091 // The debugger detached: we must not re-suspend threads. We also don't need to fill the reply
4092 // because it won't be sent either.
4093 return;
4094 }
4095
4096 JDWP::ObjectId exceptionObjectId = gRegistry->Add(exception);
4097 uint64_t result_value = 0;
4098 if (exceptionObjectId != 0) {
Sebastien Hertz1558b572015-02-25 15:05:59 +01004099 VLOG(jdwp) << " JDWP invocation returning with exception=" << exception.Get()
4100 << " " << exception->Dump();
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004101 result_value = 0;
Sebastien Hertz1558b572015-02-25 15:05:59 +01004102 } else if (is_object_result) {
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004103 /* if no exception was thrown, examine object result more closely */
Sebastien Hertz1558b572015-02-25 15:05:59 +01004104 JDWP::JdwpTag new_tag = TagFromObject(soa, object_result.Get());
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004105 if (new_tag != result_tag) {
4106 VLOG(jdwp) << " JDWP promoted result from " << result_tag << " to " << new_tag;
4107 result_tag = new_tag;
Elliott Hughesd07986f2011-12-06 18:27:45 -08004108 }
4109
Sebastien Hertz1558b572015-02-25 15:05:59 +01004110 // Register the object in the registry and reference its ObjectId. This ensures
4111 // GC safety and prevents from accessing stale reference if the object is moved.
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004112 result_value = gRegistry->Add(object_result.Get());
Sebastien Hertz1558b572015-02-25 15:05:59 +01004113 } else {
4114 // Primitive result.
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004115 DCHECK(IsPrimitiveTag(result_tag));
4116 result_value = result.GetJ();
4117 }
4118 const bool is_constructor = m->IsConstructor() && !m->IsStatic();
4119 if (is_constructor) {
4120 // If we invoked a constructor (which actually returns void), return the receiver,
4121 // unless we threw, in which case we return null.
Sebastien Hertza3e13772015-11-05 12:09:44 +01004122 DCHECK_EQ(JDWP::JT_VOID, result_tag);
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004123 if (exceptionObjectId == 0) {
Jeff Hao064d24e2016-08-25 03:52:40 +00004124 if (m->GetDeclaringClass()->IsStringClass()) {
4125 // For string constructors, the new string is remapped to the receiver (stored in ref).
4126 Handle<mirror::Object> decoded_ref = hs.NewHandle(soa.Self()->DecodeJObject(ref.get()));
4127 result_value = gRegistry->Add(decoded_ref);
4128 result_tag = TagFromObject(soa, decoded_ref.Get());
4129 } else {
4130 // TODO we could keep the receiver ObjectId in the DebugInvokeReq to avoid looking into the
4131 // object registry.
4132 result_value = GetObjectRegistry()->Add(pReq->receiver.Read());
4133 result_tag = TagFromObject(soa, pReq->receiver.Read());
4134 }
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004135 } else {
4136 result_value = 0;
Sebastien Hertza3e13772015-11-05 12:09:44 +01004137 result_tag = JDWP::JT_OBJECT;
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004138 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08004139 }
4140
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004141 // Suspend other threads if the invoke is not single-threaded.
4142 if ((pReq->options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07004143 ScopedThreadSuspension sts(soa.Self(), kWaitingForDebuggerSuspension);
Hiroshi Yamauchi8f95cf32016-04-19 11:14:06 -07004144 // Avoid a deadlock between GC and debugger where GC gets suspended during GC. b/25800335.
4145 gc::ScopedGCCriticalSection gcs(soa.Self(), gc::kGcCauseDebugger, gc::kCollectorTypeDebugger);
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004146 VLOG(jdwp) << " Suspending all threads";
4147 Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
Sebastien Hertzcbc50642015-06-01 17:33:12 +02004148 }
4149
4150 VLOG(jdwp) << " --> returned " << result_tag
4151 << StringPrintf(" %#" PRIx64 " (except=%#" PRIx64 ")", result_value,
4152 exceptionObjectId);
4153
4154 // Show detailed debug output.
4155 if (result_tag == JDWP::JT_STRING && exceptionObjectId == 0) {
4156 if (result_value != 0) {
4157 if (VLOG_IS_ON(jdwp)) {
4158 std::string result_string;
4159 JDWP::JdwpError error = Dbg::StringToUtf8(result_value, &result_string);
4160 CHECK_EQ(error, JDWP::ERR_NONE);
4161 VLOG(jdwp) << " string '" << result_string << "'";
4162 }
4163 } else {
4164 VLOG(jdwp) << " string (null)";
4165 }
4166 }
4167
4168 // Attach the reply to DebugInvokeReq so it can be sent to the debugger when the event thread
4169 // is ready to suspend.
4170 BuildInvokeReply(pReq->reply, pReq->request_id, result_tag, result_value, exceptionObjectId);
4171}
4172
4173void Dbg::BuildInvokeReply(JDWP::ExpandBuf* pReply, uint32_t request_id, JDWP::JdwpTag result_tag,
4174 uint64_t result_value, JDWP::ObjectId exception) {
4175 // Make room for the JDWP header since we do not know the size of the reply yet.
4176 JDWP::expandBufAddSpace(pReply, kJDWPHeaderLen);
4177
4178 size_t width = GetTagWidth(result_tag);
4179 JDWP::expandBufAdd1(pReply, result_tag);
4180 if (width != 0) {
4181 WriteValue(pReply, width, result_value);
4182 }
4183 JDWP::expandBufAdd1(pReply, JDWP::JT_OBJECT);
4184 JDWP::expandBufAddObjectId(pReply, exception);
4185
4186 // Now we know the size, we can complete the JDWP header.
4187 uint8_t* buf = expandBufGetBuffer(pReply);
4188 JDWP::Set4BE(buf + kJDWPHeaderSizeOffset, expandBufGetLength(pReply));
4189 JDWP::Set4BE(buf + kJDWPHeaderIdOffset, request_id);
4190 JDWP::Set1(buf + kJDWPHeaderFlagsOffset, kJDWPFlagReply); // flags
4191 JDWP::Set2BE(buf + kJDWPHeaderErrorCodeOffset, JDWP::ERR_NONE);
4192}
4193
4194void Dbg::FinishInvokeMethod(DebugInvokeReq* pReq) {
4195 CHECK_NE(Thread::Current(), GetDebugThread()) << "This must be called by the event thread";
4196
4197 JDWP::ExpandBuf* const pReply = pReq->reply;
4198 CHECK(pReply != nullptr) << "No reply attached to DebugInvokeReq";
4199
4200 // We need to prevent other threads (including JDWP thread) from interacting with the debugger
4201 // while we send the reply but are not yet suspended. The JDWP token will be released just before
4202 // we suspend ourself again (see ThreadList::SuspendSelfForDebugger).
4203 gJdwpState->AcquireJdwpTokenForEvent(pReq->thread_id);
4204
4205 // Send the reply unless the debugger detached before the completion of the method.
4206 if (IsDebuggerActive()) {
4207 const size_t replyDataLength = expandBufGetLength(pReply) - kJDWPHeaderLen;
4208 VLOG(jdwp) << StringPrintf("REPLY INVOKE id=0x%06x (length=%zu)",
4209 pReq->request_id, replyDataLength);
4210
4211 gJdwpState->SendRequest(pReply);
4212 } else {
4213 VLOG(jdwp) << "Not sending invoke reply because debugger detached";
Elliott Hughesd07986f2011-12-06 18:27:45 -08004214 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004215}
4216
Alex Light8c2b9292017-11-09 13:21:01 -08004217bool Dbg::DdmHandleChunk(JNIEnv* env,
4218 uint32_t type,
4219 const ArrayRef<const jbyte>& data,
4220 /*out*/uint32_t* out_type,
4221 /*out*/std::vector<uint8_t>* out_data) {
4222 ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(data.size()));
Ian Rogersc0542af2014-09-03 16:16:56 -07004223 if (dataArray.get() == nullptr) {
Alex Light8c2b9292017-11-09 13:21:01 -08004224 LOG(WARNING) << "byte[] allocation failed: " << data.size();
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004225 env->ExceptionClear();
4226 return false;
4227 }
Alex Light8c2b9292017-11-09 13:21:01 -08004228 env->SetByteArrayRegion(dataArray.get(),
4229 0,
4230 data.size(),
4231 reinterpret_cast<const jbyte*>(data.data()));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004232 // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
Alex Light8c2b9292017-11-09 13:21:01 -08004233 ScopedLocalRef<jobject> chunk(
4234 env,
4235 env->CallStaticObjectMethod(
4236 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
4237 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
4238 type, dataArray.get(), 0, data.size()));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004239 if (env->ExceptionCheck()) {
Alex Light1eeefa62018-03-19 13:47:56 -07004240 Thread* self = Thread::Current();
4241 ScopedObjectAccess soa(self);
4242 LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type) << std::endl
4243 << self->GetException()->Dump();
4244 self->ClearException();
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004245 return false;
4246 }
4247
Ian Rogersc0542af2014-09-03 16:16:56 -07004248 if (chunk.get() == nullptr) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004249 return false;
4250 }
4251
4252 /*
4253 * Pull the pieces out of the chunk. We copy the results into a
4254 * newly-allocated buffer that the caller can free. We don't want to
4255 * continue using the Chunk object because nothing has a reference to it.
4256 *
4257 * We could avoid this by returning type/data/offset/length and having
4258 * the caller be aware of the object lifetime issues, but that
Elliott Hughes81ff3182012-03-23 20:35:56 -07004259 * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004260 * if we have responses for multiple chunks.
4261 *
4262 * So we're pretty much stuck with copying data around multiple times.
4263 */
Alex Light8c2b9292017-11-09 13:21:01 -08004264 ScopedLocalRef<jbyteArray> replyData(
4265 env,
4266 reinterpret_cast<jbyteArray>(
4267 env->GetObjectField(
4268 chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
4269 jint offset = env->GetIntField(chunk.get(),
4270 WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
4271 jint length = env->GetIntField(chunk.get(),
4272 WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
4273 *out_type = env->GetIntField(chunk.get(),
4274 WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004275
Alex Light8c2b9292017-11-09 13:21:01 -08004276 VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d",
4277 type,
4278 replyData.get(),
4279 offset,
4280 length);
Alex Light8c2b9292017-11-09 13:21:01 -08004281 out_data->resize(length);
4282 env->GetByteArrayRegion(replyData.get(),
4283 offset,
4284 length,
4285 reinterpret_cast<jbyte*>(out_data->data()));
Alex Light6f2a6342017-12-12 09:55:05 -08004286
Alex Lighte5463a82017-12-12 13:33:28 -08004287 if (env->ExceptionCheck()) {
Alex Light1eeefa62018-03-19 13:47:56 -07004288 Thread* self = Thread::Current();
4289 ScopedObjectAccess soa(self);
Alex Lighte5463a82017-12-12 13:33:28 -08004290 LOG(INFO) << StringPrintf("Exception thrown when reading response data from dispatcher 0x%08x",
Alex Light1eeefa62018-03-19 13:47:56 -07004291 type) << std::endl << self->GetException()->Dump();
4292 self->ClearException();
Alex Lighte5463a82017-12-12 13:33:28 -08004293 return false;
4294 }
4295
Alex Light8c2b9292017-11-09 13:21:01 -08004296 return true;
4297}
4298
4299/*
4300 * "request" contains a full JDWP packet, possibly with multiple chunks. We
4301 * need to process each, accumulate the replies, and ship the whole thing
4302 * back.
4303 *
4304 * Returns "true" if we have a reply. The reply buffer is newly allocated,
4305 * and includes the chunk type/length, followed by the data.
4306 *
4307 * OLD-TODO: we currently assume that the request and reply include a single
4308 * chunk. If this becomes inconvenient we will need to adapt.
4309 */
4310bool Dbg::DdmHandlePacket(JDWP::Request* request, uint8_t** pReplyBuf, int* pReplyLen) {
4311 Thread* self = Thread::Current();
4312 JNIEnv* env = self->GetJniEnv();
4313
4314 uint32_t type = request->ReadUnsigned32("type");
4315 uint32_t length = request->ReadUnsigned32("length");
4316
4317 // Create a byte[] corresponding to 'request'.
4318 size_t request_length = request->size();
4319 // Run through and find all chunks. [Currently just find the first.]
4320 if (length != request_length) {
4321 LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%zd)", length, request_length);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004322 return false;
4323 }
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004324
Alex Light8c2b9292017-11-09 13:21:01 -08004325 ArrayRef<const jbyte> data(reinterpret_cast<const jbyte*>(request->data()), request_length);
4326 std::vector<uint8_t> out_data;
4327 uint32_t out_type = 0;
4328 request->Skip(request_length);
Alex Light6f2a6342017-12-12 09:55:05 -08004329 if (!DdmHandleChunk(env, type, data, &out_type, &out_data) || out_data.empty()) {
Alex Light8c2b9292017-11-09 13:21:01 -08004330 return false;
4331 }
4332 const uint32_t kDdmHeaderSize = 8;
4333 *pReplyLen = out_data.size() + kDdmHeaderSize;
4334 *pReplyBuf = new uint8_t[out_data.size() + kDdmHeaderSize];
4335 memcpy((*pReplyBuf) + kDdmHeaderSize, out_data.data(), out_data.size());
4336 JDWP::Set4BE(*pReplyBuf, out_type);
4337 JDWP::Set4BE((*pReplyBuf) + 4, static_cast<uint32_t>(out_data.size()));
4338 VLOG(jdwp)
4339 << StringPrintf("dvmHandleDdm returning type=%.4s", reinterpret_cast<char*>(*pReplyBuf))
4340 << "0x" << std::hex << reinterpret_cast<uintptr_t>(*pReplyBuf) << std::dec
4341 << " len= " << out_data.size();
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004342 return true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004343}
4344
Elliott Hughesa2155262011-11-16 16:26:58 -08004345void Dbg::DdmBroadcast(bool connect) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08004346 VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
Elliott Hughes47fce012011-10-25 18:37:19 -07004347
4348 Thread* self = Thread::Current();
Ian Rogers50b35e22012-10-04 10:09:15 -07004349 if (self->GetState() != kRunnable) {
4350 LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
4351 /* try anyway? */
Elliott Hughes47fce012011-10-25 18:37:19 -07004352 }
4353
4354 JNIEnv* env = self->GetJniEnv();
Elliott Hughes47fce012011-10-25 18:37:19 -07004355 jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
Elliott Hugheseac76672012-05-24 21:56:51 -07004356 env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
4357 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
4358 event);
Elliott Hughes47fce012011-10-25 18:37:19 -07004359 if (env->ExceptionCheck()) {
4360 LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
4361 env->ExceptionDescribe();
4362 env->ExceptionClear();
4363 }
4364}
4365
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004366void Dbg::DdmConnected() {
Elliott Hughesa2155262011-11-16 16:26:58 -08004367 Dbg::DdmBroadcast(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004368}
4369
4370void Dbg::DdmDisconnected() {
Elliott Hughesa2155262011-11-16 16:26:58 -08004371 Dbg::DdmBroadcast(false);
Elliott Hughes47fce012011-10-25 18:37:19 -07004372 gDdmThreadNotification = false;
4373}
4374
4375/*
Elliott Hughes82188472011-11-07 18:11:48 -08004376 * Send a notification when a thread starts, stops, or changes its name.
Elliott Hughes47fce012011-10-25 18:37:19 -07004377 *
4378 * Because we broadcast the full set of threads when the notifications are
4379 * first enabled, it's possible for "thread" to be actively executing.
4380 */
Elliott Hughes82188472011-11-07 18:11:48 -08004381void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
Elliott Hughes47fce012011-10-25 18:37:19 -07004382 if (!gDdmThreadNotification) {
4383 return;
4384 }
4385
Alex Light772099a2017-11-21 14:05:04 -08004386 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Elliott Hughes82188472011-11-07 18:11:48 -08004387 if (type == CHUNK_TYPE("THDE")) {
Elliott Hughes47fce012011-10-25 18:37:19 -07004388 uint8_t buf[4];
Ian Rogersd9c4fc92013-10-01 19:45:43 -07004389 JDWP::Set4BE(&buf[0], t->GetThreadId());
Alex Light772099a2017-11-21 14:05:04 -08004390 cb->DdmPublishChunk(CHUNK_TYPE("THDE"), ArrayRef<const uint8_t>(buf));
Elliott Hughes82188472011-11-07 18:11:48 -08004391 } else {
4392 CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004393 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07004394 StackHandleScope<1> hs(soa.Self());
Andreas Gampe08883de2016-11-08 13:20:52 -08004395 Handle<mirror::String> name(hs.NewHandle(t->GetThreadName()));
Andreas Gampefa4333d2017-02-14 11:10:34 -08004396 size_t char_count = (name != nullptr) ? name->GetLength() : 0;
4397 const jchar* chars = (name != nullptr) ? name->GetValue() : nullptr;
4398 bool is_compressed = (name != nullptr) ? name->IsCompressed() : false;
Elliott Hughes82188472011-11-07 18:11:48 -08004399
Elliott Hughes21f32d72011-11-09 17:44:13 -08004400 std::vector<uint8_t> bytes;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07004401 JDWP::Append4BE(bytes, t->GetThreadId());
jessicahandojo3aaa37b2016-07-29 14:46:37 -07004402 if (is_compressed) {
4403 const uint8_t* chars_compressed = name->GetValueCompressed();
4404 JDWP::AppendUtf16CompressedBE(bytes, chars_compressed, char_count);
4405 } else {
4406 JDWP::AppendUtf16BE(bytes, chars, char_count);
4407 }
Elliott Hughes21f32d72011-11-09 17:44:13 -08004408 CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
Alex Light772099a2017-11-21 14:05:04 -08004409 cb->DdmPublishChunk(type, ArrayRef<const uint8_t>(bytes));
Elliott Hughes47fce012011-10-25 18:37:19 -07004410 }
4411}
4412
Elliott Hughes47fce012011-10-25 18:37:19 -07004413void Dbg::DdmSetThreadNotification(bool enable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004414 // Enable/disable thread notifications.
Elliott Hughes47fce012011-10-25 18:37:19 -07004415 gDdmThreadNotification = enable;
4416 if (enable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004417 // Suspend the VM then post thread start notifications for all threads. Threads attaching will
4418 // see a suspension in progress and block until that ends. They then post their own start
4419 // notification.
4420 SuspendVM();
4421 std::list<Thread*> threads;
Ian Rogers50b35e22012-10-04 10:09:15 -07004422 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004423 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004424 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004425 threads = Runtime::Current()->GetThreadList()->GetList();
4426 }
4427 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004428 ScopedObjectAccess soa(self);
Mathieu Chartier02e25112013-08-14 16:14:24 -07004429 for (Thread* thread : threads) {
4430 Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR"));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004431 }
4432 }
4433 ResumeVM();
Elliott Hughes47fce012011-10-25 18:37:19 -07004434 }
4435}
4436
Elliott Hughesa2155262011-11-16 16:26:58 -08004437void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07004438 if (IsDebuggerActive()) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02004439 gJdwpState->PostThreadChange(t, type == CHUNK_TYPE("THCR"));
Elliott Hughes47fce012011-10-25 18:37:19 -07004440 }
Elliott Hughes82188472011-11-07 18:11:48 -08004441 Dbg::DdmSendThreadNotification(t, type);
Elliott Hughes47fce012011-10-25 18:37:19 -07004442}
4443
4444void Dbg::PostThreadStart(Thread* t) {
Elliott Hughesa2155262011-11-16 16:26:58 -08004445 Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
Elliott Hughes47fce012011-10-25 18:37:19 -07004446}
4447
4448void Dbg::PostThreadDeath(Thread* t) {
Elliott Hughesa2155262011-11-16 16:26:58 -08004449 Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004450}
4451
Mathieu Chartierad466ad2015-01-08 16:28:08 -08004452JDWP::JdwpState* Dbg::GetJdwpState() {
4453 return gJdwpState;
4454}
4455
Elliott Hughes767a1472011-10-26 18:49:02 -07004456int Dbg::DdmHandleHpifChunk(HpifWhen when) {
4457 if (when == HPIF_WHEN_NOW) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07004458 DdmSendHeapInfo(when);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -07004459 return 1;
Elliott Hughes767a1472011-10-26 18:49:02 -07004460 }
4461
4462 if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
4463 LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -07004464 return 0;
Elliott Hughes767a1472011-10-26 18:49:02 -07004465 }
4466
4467 gDdmHpifWhen = when;
Andreas Gampe7c5acbb2018-09-20 13:54:52 -07004468 return 1;
Elliott Hughes767a1472011-10-26 18:49:02 -07004469}
4470
4471bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
4472 if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
4473 LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
4474 return false;
4475 }
4476
4477 if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
4478 LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
4479 return false;
4480 }
4481
4482 if (native) {
4483 gDdmNhsgWhen = when;
4484 gDdmNhsgWhat = what;
4485 } else {
4486 gDdmHpsgWhen = when;
4487 gDdmHpsgWhat = what;
4488 }
4489 return true;
4490}
4491
Elliott Hughes7162ad92011-10-27 14:08:42 -07004492void Dbg::DdmSendHeapInfo(HpifWhen reason) {
4493 // If there's a one-shot 'when', reset it.
4494 if (reason == gDdmHpifWhen) {
4495 if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
4496 gDdmHpifWhen = HPIF_WHEN_NEVER;
4497 }
4498 }
4499
4500 /*
4501 * Chunk HPIF (client --> server)
4502 *
4503 * Heap Info. General information about the heap,
4504 * suitable for a summary display.
4505 *
4506 * [u4]: number of heaps
4507 *
4508 * For each heap:
4509 * [u4]: heap ID
4510 * [u8]: timestamp in ms since Unix epoch
4511 * [u1]: capture reason (same as 'when' value from server)
4512 * [u4]: max heap size in bytes (-Xmx)
4513 * [u4]: current heap size in bytes
4514 * [u4]: current number of bytes allocated
4515 * [u4]: current number of objects allocated
4516 */
4517 uint8_t heap_count = 1;
Ian Rogers1d54e732013-05-02 21:10:01 -07004518 gc::Heap* heap = Runtime::Current()->GetHeap();
Elliott Hughes21f32d72011-11-09 17:44:13 -08004519 std::vector<uint8_t> bytes;
Elliott Hughes545a0642011-11-08 19:10:03 -08004520 JDWP::Append4BE(bytes, heap_count);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004521 JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap).
Elliott Hughes545a0642011-11-08 19:10:03 -08004522 JDWP::Append8BE(bytes, MilliTime());
4523 JDWP::Append1BE(bytes, reason);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004524 JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes.
4525 JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004526 JDWP::Append4BE(bytes, heap->GetBytesAllocated());
4527 JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
Elliott Hughes21f32d72011-11-09 17:44:13 -08004528 CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
Alex Light772099a2017-11-21 14:05:04 -08004529 Runtime::Current()->GetRuntimeCallbacks()->DdmPublishChunk(CHUNK_TYPE("HPIF"),
4530 ArrayRef<const uint8_t>(bytes));
Elliott Hughes767a1472011-10-26 18:49:02 -07004531}
4532
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004533enum HpsgSolidity {
4534 SOLIDITY_FREE = 0,
4535 SOLIDITY_HARD = 1,
4536 SOLIDITY_SOFT = 2,
4537 SOLIDITY_WEAK = 3,
4538 SOLIDITY_PHANTOM = 4,
4539 SOLIDITY_FINALIZABLE = 5,
4540 SOLIDITY_SWEEP = 6,
4541};
4542
4543enum HpsgKind {
4544 KIND_OBJECT = 0,
4545 KIND_CLASS_OBJECT = 1,
4546 KIND_ARRAY_1 = 2,
4547 KIND_ARRAY_2 = 3,
4548 KIND_ARRAY_4 = 4,
4549 KIND_ARRAY_8 = 5,
4550 KIND_UNKNOWN = 6,
4551 KIND_NATIVE = 7,
4552};
4553
4554#define HPSG_PARTIAL (1<<7)
4555#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
4556
Ian Rogers30fab402012-01-23 15:43:46 -08004557class HeapChunkContext {
4558 public:
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004559 // Maximum chunk size. Obtain this from the formula:
4560 // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
4561 HeapChunkContext(bool merge, bool native)
Ian Rogers30fab402012-01-23 15:43:46 -08004562 : buf_(16384 - 16),
4563 type_(0),
Mathieu Chartier36dab362014-07-30 14:59:56 -07004564 chunk_overhead_(0) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004565 Reset();
4566 if (native) {
Ian Rogers30fab402012-01-23 15:43:46 -08004567 type_ = CHUNK_TYPE("NHSG");
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004568 } else {
Ian Rogers30fab402012-01-23 15:43:46 -08004569 type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004570 }
4571 }
4572
4573 ~HeapChunkContext() {
Ian Rogers30fab402012-01-23 15:43:46 -08004574 if (p_ > &buf_[0]) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004575 Flush();
4576 }
4577 }
4578
Mathieu Chartier36dab362014-07-30 14:59:56 -07004579 void SetChunkOverhead(size_t chunk_overhead) {
4580 chunk_overhead_ = chunk_overhead;
4581 }
4582
4583 void ResetStartOfNextChunk() {
4584 startOfNextMemoryChunk_ = nullptr;
4585 }
4586
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004587 void EnsureHeader(const void* chunk_ptr) {
Ian Rogers30fab402012-01-23 15:43:46 -08004588 if (!needHeader_) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004589 return;
4590 }
4591
4592 // Start a new HPSx chunk.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004593 JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap).
4594 JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes.
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004595
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004596 JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start.
4597 JDWP::Write4BE(&p_, 0); // offset of this piece (relative to the virtual address).
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004598 // [u4]: length of piece, in allocation units
4599 // We won't know this until we're done, so save the offset and stuff in a dummy value.
Ian Rogers30fab402012-01-23 15:43:46 -08004600 pieceLenField_ = p_;
4601 JDWP::Write4BE(&p_, 0x55555555);
4602 needHeader_ = false;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004603 }
4604
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004605 void Flush() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07004606 if (pieceLenField_ == nullptr) {
Ian Rogersd636b062013-01-18 17:51:18 -08004607 // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
4608 CHECK(needHeader_);
4609 return;
4610 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004611 // Patch the "length of piece" field.
Ian Rogers30fab402012-01-23 15:43:46 -08004612 CHECK_LE(&buf_[0], pieceLenField_);
4613 CHECK_LE(pieceLenField_, p_);
4614 JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004615
Alex Light772099a2017-11-21 14:05:04 -08004616 ArrayRef<const uint8_t> out(&buf_[0], p_ - &buf_[0]);
4617 Runtime::Current()->GetRuntimeCallbacks()->DdmPublishChunk(type_, out);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004618 Reset();
4619 }
4620
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004621 static void HeapChunkJavaCallback(void* start, void* end, size_t used_bytes, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004622 REQUIRES_SHARED(Locks::heap_bitmap_lock_,
Ian Rogersb726dcb2012-09-05 08:57:23 -07004623 Locks::mutator_lock_) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004624 reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkJavaCallback(start, end, used_bytes);
4625 }
4626
4627 static void HeapChunkNativeCallback(void* start, void* end, size_t used_bytes, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004628 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004629 reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkNativeCallback(start, end, used_bytes);
Elliott Hughesa2155262011-11-16 16:26:58 -08004630 }
4631
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004632 private:
Elliott Hughesa2155262011-11-16 16:26:58 -08004633 enum { ALLOCATION_UNIT_SIZE = 8 };
4634
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004635 void Reset() {
Ian Rogers30fab402012-01-23 15:43:46 -08004636 p_ = &buf_[0];
Mathieu Chartier36dab362014-07-30 14:59:56 -07004637 ResetStartOfNextChunk();
Ian Rogers30fab402012-01-23 15:43:46 -08004638 totalAllocationUnits_ = 0;
4639 needHeader_ = true;
Ian Rogersc0542af2014-09-03 16:16:56 -07004640 pieceLenField_ = nullptr;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004641 }
4642
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004643 bool IsNative() const {
4644 return type_ == CHUNK_TYPE("NHSG");
4645 }
4646
4647 // Returns true if the object is not an empty chunk.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004648 bool ProcessRecord(void* start, size_t used_bytes) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers30fab402012-01-23 15:43:46 -08004649 // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
4650 // in the following code not to allocate memory, by ensuring buf_ is of the correct size
Ian Rogers15bf2d32012-08-28 17:33:04 -07004651 if (used_bytes == 0) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004652 if (start == nullptr) {
4653 // Reset for start of new heap.
4654 startOfNextMemoryChunk_ = nullptr;
4655 Flush();
4656 }
4657 // Only process in use memory so that free region information
4658 // also includes dlmalloc book keeping.
4659 return false;
Elliott Hughesa2155262011-11-16 16:26:58 -08004660 }
Ian Rogersc0542af2014-09-03 16:16:56 -07004661 if (startOfNextMemoryChunk_ != nullptr) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004662 // Transmit any pending free memory. Native free memory of over kMaxFreeLen could be because
4663 // of the use of mmaps, so don't report. If not free memory then start a new segment.
4664 bool flush = true;
4665 if (start > startOfNextMemoryChunk_) {
4666 const size_t kMaxFreeLen = 2 * kPageSize;
4667 void* free_start = startOfNextMemoryChunk_;
4668 void* free_end = start;
4669 const size_t free_len =
4670 reinterpret_cast<uintptr_t>(free_end) - reinterpret_cast<uintptr_t>(free_start);
4671 if (!IsNative() || free_len < kMaxFreeLen) {
4672 AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), free_start, free_len, IsNative());
4673 flush = false;
Ian Rogers15bf2d32012-08-28 17:33:04 -07004674 }
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004675 }
4676 if (flush) {
4677 startOfNextMemoryChunk_ = nullptr;
4678 Flush();
4679 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07004680 }
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004681 return true;
Ian Rogers15bf2d32012-08-28 17:33:04 -07004682 }
Elliott Hughesa2155262011-11-16 16:26:58 -08004683
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004684 void HeapChunkNativeCallback(void* start, void* /*end*/, size_t used_bytes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004685 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004686 if (ProcessRecord(start, used_bytes)) {
4687 uint8_t state = ExamineNativeObject(start);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07004688 AppendChunk(state, start, used_bytes + chunk_overhead_, /*is_native=*/ true);
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004689 startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_;
4690 }
4691 }
4692
4693 void HeapChunkJavaCallback(void* start, void* /*end*/, size_t used_bytes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004694 REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004695 if (ProcessRecord(start, used_bytes)) {
4696 // Determine the type of this chunk.
4697 // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
4698 // If it's the same, we should combine them.
4699 uint8_t state = ExamineJavaObject(reinterpret_cast<mirror::Object*>(start));
Andreas Gampe98ea9d92018-10-19 14:06:15 -07004700 AppendChunk(state, start, used_bytes + chunk_overhead_, /*is_native=*/ false);
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004701 startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_;
4702 }
4703 }
4704
4705 void AppendChunk(uint8_t state, void* ptr, size_t length, bool is_native)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004706 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07004707 // Make sure there's enough room left in the buffer.
4708 // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
4709 // 17 bytes for any header.
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004710 const size_t needed = ((RoundUp(length / ALLOCATION_UNIT_SIZE, 256) / 256) * 2) + 17;
4711 size_t byte_left = &buf_.back() - p_;
4712 if (byte_left < needed) {
4713 if (is_native) {
Pavel Vyssotski7522c742014-12-08 13:38:26 +06004714 // Cannot trigger memory allocation while walking native heap.
Pavel Vyssotski7522c742014-12-08 13:38:26 +06004715 return;
4716 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07004717 Flush();
4718 }
4719
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004720 byte_left = &buf_.back() - p_;
4721 if (byte_left < needed) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07004722 LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
4723 << needed << " bytes)";
4724 return;
4725 }
4726 EnsureHeader(ptr);
Elliott Hughesa2155262011-11-16 16:26:58 -08004727 // Write out the chunk description.
Ian Rogers15bf2d32012-08-28 17:33:04 -07004728 length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units.
4729 totalAllocationUnits_ += length;
4730 while (length > 256) {
Ian Rogers30fab402012-01-23 15:43:46 -08004731 *p_++ = state | HPSG_PARTIAL;
4732 *p_++ = 255; // length - 1
Ian Rogers15bf2d32012-08-28 17:33:04 -07004733 length -= 256;
Elliott Hughesa2155262011-11-16 16:26:58 -08004734 }
Ian Rogers30fab402012-01-23 15:43:46 -08004735 *p_++ = state;
Ian Rogers15bf2d32012-08-28 17:33:04 -07004736 *p_++ = length - 1;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004737 }
4738
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004739 uint8_t ExamineNativeObject(const void* p) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004740 return p == nullptr ? HPSG_STATE(SOLIDITY_FREE, 0) : HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
4741 }
4742
Vladimir Marko83114892019-04-11 13:05:50 +01004743 uint8_t ExamineJavaObject(ObjPtr<mirror::Object> o)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004744 REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07004745 if (o == nullptr) {
Elliott Hughesa2155262011-11-16 16:26:58 -08004746 return HPSG_STATE(SOLIDITY_FREE, 0);
4747 }
Elliott Hughesa2155262011-11-16 16:26:58 -08004748 // It's an allocated chunk. Figure out what it is.
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004749 gc::Heap* heap = Runtime::Current()->GetHeap();
4750 if (!heap->IsLiveObjectLocked(o)) {
4751 LOG(ERROR) << "Invalid object in managed heap: " << o;
Elliott Hughesa2155262011-11-16 16:26:58 -08004752 return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
4753 }
Vladimir Marko4617d582019-03-28 13:48:31 +00004754 ObjPtr<mirror::Class> c = o->GetClass();
Ian Rogersc0542af2014-09-03 16:16:56 -07004755 if (c == nullptr) {
Elliott Hughesa2155262011-11-16 16:26:58 -08004756 // The object was probably just created but hasn't been initialized yet.
4757 return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
4758 }
Vladimir Marko4617d582019-03-28 13:48:31 +00004759 if (!heap->IsValidObjectAddress(c.Ptr())) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07004760 LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
Elliott Hughesa2155262011-11-16 16:26:58 -08004761 return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
4762 }
Mathieu Chartierf26e1b32015-01-29 10:47:10 -08004763 if (c->GetClass() == nullptr) {
4764 LOG(ERROR) << "Null class of class " << c << " for object " << o;
4765 return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
4766 }
Elliott Hughesa2155262011-11-16 16:26:58 -08004767 if (c->IsClassClass()) {
4768 return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
4769 }
Elliott Hughesa2155262011-11-16 16:26:58 -08004770 if (c->IsArrayClass()) {
Elliott Hughesa2155262011-11-16 16:26:58 -08004771 switch (c->GetComponentSize()) {
4772 case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
4773 case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
4774 case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
4775 case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
4776 }
4777 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004778 return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
4779 }
4780
Ian Rogers30fab402012-01-23 15:43:46 -08004781 std::vector<uint8_t> buf_;
4782 uint8_t* p_;
4783 uint8_t* pieceLenField_;
Ian Rogers15bf2d32012-08-28 17:33:04 -07004784 void* startOfNextMemoryChunk_;
Ian Rogers30fab402012-01-23 15:43:46 -08004785 size_t totalAllocationUnits_;
4786 uint32_t type_;
Ian Rogers30fab402012-01-23 15:43:46 -08004787 bool needHeader_;
Mathieu Chartier36dab362014-07-30 14:59:56 -07004788 size_t chunk_overhead_;
Ian Rogers30fab402012-01-23 15:43:46 -08004789
Elliott Hughesa2155262011-11-16 16:26:58 -08004790 DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
4791};
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004792
4793void Dbg::DdmSendHeapSegments(bool native) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004794 Dbg::HpsgWhen when = native ? gDdmNhsgWhen : gDdmHpsgWhen;
4795 Dbg::HpsgWhat what = native ? gDdmNhsgWhat : gDdmHpsgWhat;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004796 if (when == HPSG_WHEN_NEVER) {
4797 return;
4798 }
Alex Light772099a2017-11-21 14:05:04 -08004799 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004800 // Figure out what kind of chunks we'll be sending.
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004801 CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS)
4802 << static_cast<int>(what);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004803
4804 // First, send a heap start chunk.
4805 uint8_t heap_id[4];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004806 JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap).
Alex Light772099a2017-11-21 14:05:04 -08004807 cb->DdmPublishChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"),
4808 ArrayRef<const uint8_t>(heap_id));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07004809 Thread* self = Thread::Current();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07004810 Locks::mutator_lock_->AssertSharedHeld(self);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07004811
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004812 // Send a series of heap segment chunks.
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004813 HeapChunkContext context(what == HPSG_WHAT_MERGED_OBJECTS, native);
Andreas Gampe0c183382017-07-13 22:26:24 -07004814 auto bump_pointer_space_visitor = [&](mirror::Object* obj)
4815 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
4816 const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment);
4817 HeapChunkContext::HeapChunkJavaCallback(
4818 obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, &context);
4819 };
Elliott Hughesa2155262011-11-16 16:26:58 -08004820 if (native) {
Dimitry Ivanove6465bc2015-12-14 18:55:02 -08004821 UNIMPLEMENTED(WARNING) << "Native heap inspection is not supported";
Elliott Hughesa2155262011-11-16 16:26:58 -08004822 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -07004823 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier36dab362014-07-30 14:59:56 -07004824 for (const auto& space : heap->GetContinuousSpaces()) {
4825 if (space->IsDlMallocSpace()) {
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -07004826 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier36dab362014-07-30 14:59:56 -07004827 // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
4828 // allocation then the first sizeof(size_t) may belong to it.
4829 context.SetChunkOverhead(sizeof(size_t));
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004830 space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context);
Mathieu Chartier36dab362014-07-30 14:59:56 -07004831 } else if (space->IsRosAllocSpace()) {
4832 context.SetChunkOverhead(0);
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -07004833 // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since
4834 // RosAlloc's internal logic doesn't know to release and reacquire the heap bitmap lock.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07004835 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07004836 ScopedSuspendAll ssa(__FUNCTION__);
4837 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
4838 space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context);
Mathieu Chartier36dab362014-07-30 14:59:56 -07004839 } else if (space->IsBumpPointerSpace()) {
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -07004840 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier36dab362014-07-30 14:59:56 -07004841 context.SetChunkOverhead(0);
Andreas Gampe0c183382017-07-13 22:26:24 -07004842 space->AsBumpPointerSpace()->Walk(bump_pointer_space_visitor);
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004843 HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -08004844 } else if (space->IsRegionSpace()) {
4845 heap->IncrementDisableMovingGC(self);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07004846 {
4847 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07004848 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07004849 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
4850 context.SetChunkOverhead(0);
Andreas Gampe0c183382017-07-13 22:26:24 -07004851 space->AsRegionSpace()->Walk(bump_pointer_space_visitor);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07004852 HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07004853 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -08004854 heap->DecrementDisableMovingGC(self);
Mathieu Chartier36dab362014-07-30 14:59:56 -07004855 } else {
4856 UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07004857 }
Mathieu Chartier36dab362014-07-30 14:59:56 -07004858 context.ResetStartOfNextChunk();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07004859 }
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -07004860 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07004861 // Walk the large objects, these are not in the AllocSpace.
Mathieu Chartier36dab362014-07-30 14:59:56 -07004862 context.SetChunkOverhead(0);
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004863 heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context);
Elliott Hughesa2155262011-11-16 16:26:58 -08004864 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004865
4866 // Finally, send a heap end chunk.
Alex Light772099a2017-11-21 14:05:04 -08004867 cb->DdmPublishChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"),
4868 ArrayRef<const uint8_t>(heap_id));
Elliott Hughes767a1472011-10-26 18:49:02 -07004869}
4870
Brian Carlstrom306db812014-09-05 13:01:41 -07004871void Dbg::SetAllocTrackingEnabled(bool enable) {
Man Cao8c2ff642015-05-27 17:25:30 -07004872 gc::AllocRecordObjectMap::SetAllocTrackingEnabled(enable);
Elliott Hughes545a0642011-11-08 19:10:03 -08004873}
4874
4875void Dbg::DumpRecentAllocations() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004876 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom306db812014-09-05 13:01:41 -07004877 MutexLock mu(soa.Self(), *Locks::alloc_tracker_lock_);
Man Cao8c2ff642015-05-27 17:25:30 -07004878 if (!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()) {
Elliott Hughes545a0642011-11-08 19:10:03 -08004879 LOG(INFO) << "Not recording tracked allocations";
4880 return;
4881 }
Man Cao8c2ff642015-05-27 17:25:30 -07004882 gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords();
4883 CHECK(records != nullptr);
Elliott Hughes545a0642011-11-08 19:10:03 -08004884
Man Cao1ed11b92015-06-11 22:47:35 -07004885 const uint16_t capped_count = CappedAllocRecordCount(records->GetRecentAllocationSize());
Brian Carlstrom306db812014-09-05 13:01:41 -07004886 uint16_t count = capped_count;
Elliott Hughes545a0642011-11-08 19:10:03 -08004887
Man Cao8c2ff642015-05-27 17:25:30 -07004888 LOG(INFO) << "Tracked allocations, (count=" << count << ")";
4889 for (auto it = records->RBegin(), end = records->REnd();
4890 count > 0 && it != end; count--, it++) {
Mathieu Chartier458b1052016-03-29 14:02:55 -07004891 const gc::AllocRecord* record = &it->second;
Elliott Hughes545a0642011-11-08 19:10:03 -08004892
Man Cao8c2ff642015-05-27 17:25:30 -07004893 LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->GetTid(), record->ByteCount())
David Sehr709b0702016-10-13 09:12:37 -07004894 << mirror::Class::PrettyClass(record->GetClass());
Elliott Hughes545a0642011-11-08 19:10:03 -08004895
Man Cao8c2ff642015-05-27 17:25:30 -07004896 for (size_t stack_frame = 0, depth = record->GetDepth(); stack_frame < depth; ++stack_frame) {
4897 const gc::AllocRecordStackTraceElement& stack_element = record->StackElement(stack_frame);
4898 ArtMethod* m = stack_element.GetMethod();
David Sehr709b0702016-10-13 09:12:37 -07004899 LOG(INFO) << " " << ArtMethod::PrettyMethod(m) << " line "
4900 << stack_element.ComputeLineNumber();
Elliott Hughes545a0642011-11-08 19:10:03 -08004901 }
4902
4903 // pause periodically to help logcat catch up
4904 if ((count % 5) == 0) {
4905 usleep(40000);
4906 }
Elliott Hughes545a0642011-11-08 19:10:03 -08004907 }
4908}
4909
4910class StringTable {
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07004911 private:
4912 struct Entry {
Andreas Gampe84eadb22017-07-07 15:08:01 -07004913 explicit Entry(const char* data_in)
4914 : data(data_in), hash(ComputeModifiedUtf8Hash(data_in)), index(0) {
4915 }
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07004916 Entry(const Entry& entry) = default;
4917 Entry(Entry&& entry) = default;
4918
4919 // Pointer to the actual string data.
4920 const char* data;
Andreas Gampe84eadb22017-07-07 15:08:01 -07004921
4922 // The hash of the data.
4923 const uint32_t hash;
4924
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07004925 // The index. This will be filled in on Finish and is not part of the ordering, so mark it
4926 // mutable.
4927 mutable uint32_t index;
4928
Andreas Gampe84eadb22017-07-07 15:08:01 -07004929 bool operator==(const Entry& other) const {
4930 return strcmp(data, other.data) == 0;
4931 }
4932 };
4933 struct EntryHash {
4934 size_t operator()(const Entry& entry) const {
4935 return entry.hash;
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07004936 }
4937 };
4938
Elliott Hughes545a0642011-11-08 19:10:03 -08004939 public:
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07004940 StringTable() : finished_(false) {
Elliott Hughes545a0642011-11-08 19:10:03 -08004941 }
4942
Andreas Gampef774a4e2017-07-06 22:15:18 -07004943 void Add(const char* str, bool copy_string) {
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07004944 DCHECK(!finished_);
Andreas Gampef774a4e2017-07-06 22:15:18 -07004945 if (UNLIKELY(copy_string)) {
4946 // Check whether it's already there.
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07004947 Entry entry(str);
4948 if (table_.find(entry) != table_.end()) {
Andreas Gampef774a4e2017-07-06 22:15:18 -07004949 return;
4950 }
Mathieu Chartier4345c462014-06-27 10:20:14 -07004951
Andreas Gampef774a4e2017-07-06 22:15:18 -07004952 // Make a copy.
4953 size_t str_len = strlen(str);
4954 char* copy = new char[str_len + 1];
4955 strlcpy(copy, str, str_len + 1);
4956 string_backup_.emplace_back(copy);
4957 str = copy;
4958 }
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07004959 Entry entry(str);
4960 table_.insert(entry);
4961 }
4962
4963 // Update all entries and give them an index. Note that this is likely not the insertion order,
4964 // as the set will with high likelihood reorder elements. Thus, Add must not be called after
4965 // Finish, and Finish must be called before IndexOf. In that case, WriteTo will walk in
4966 // the same order as Finish, and indices will agree. The order invariant, as well as indices,
4967 // are enforced through debug checks.
4968 void Finish() {
4969 DCHECK(!finished_);
4970 finished_ = true;
4971 uint32_t index = 0;
4972 for (auto& entry : table_) {
4973 entry.index = index;
4974 ++index;
4975 }
Elliott Hughes545a0642011-11-08 19:10:03 -08004976 }
4977
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004978 size_t IndexOf(const char* s) const {
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07004979 DCHECK(finished_);
4980 Entry entry(s);
4981 auto it = table_.find(entry);
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004982 if (it == table_.end()) {
4983 LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
4984 }
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07004985 return it->index;
Elliott Hughes545a0642011-11-08 19:10:03 -08004986 }
4987
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004988 size_t Size() const {
Elliott Hughes545a0642011-11-08 19:10:03 -08004989 return table_.size();
4990 }
4991
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004992 void WriteTo(std::vector<uint8_t>& bytes) const {
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07004993 DCHECK(finished_);
4994 uint32_t cur_index = 0;
4995 for (const auto& entry : table_) {
4996 DCHECK_EQ(cur_index++, entry.index);
4997
4998 size_t s_len = CountModifiedUtf8Chars(entry.data);
Christopher Ferris8a354052015-04-24 17:23:53 -07004999 std::unique_ptr<uint16_t[]> s_utf16(new uint16_t[s_len]);
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07005000 ConvertModifiedUtf8ToUtf16(s_utf16.get(), entry.data);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08005001 JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
Elliott Hughes545a0642011-11-08 19:10:03 -08005002 }
5003 }
5004
5005 private:
Andreas Gampe84eadb22017-07-07 15:08:01 -07005006 std::unordered_set<Entry, EntryHash> table_;
Andreas Gampef774a4e2017-07-06 22:15:18 -07005007 std::vector<std::unique_ptr<char[]>> string_backup_;
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07005008
5009 bool finished_;
5010
Elliott Hughes545a0642011-11-08 19:10:03 -08005011 DISALLOW_COPY_AND_ASSIGN(StringTable);
5012};
5013
Mathieu Chartiere401d142015-04-22 13:56:20 -07005014static const char* GetMethodSourceFile(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07005015 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07005016 DCHECK(method != nullptr);
5017 const char* source_file = method->GetDeclaringClassSourceFile();
Sebastien Hertz280286a2014-04-28 09:26:50 +02005018 return (source_file != nullptr) ? source_file : "";
5019}
5020
Elliott Hughes545a0642011-11-08 19:10:03 -08005021/*
5022 * The data we send to DDMS contains everything we have recorded.
5023 *
5024 * Message header (all values big-endian):
5025 * (1b) message header len (to allow future expansion); includes itself
5026 * (1b) entry header len
5027 * (1b) stack frame len
5028 * (2b) number of entries
5029 * (4b) offset to string table from start of message
5030 * (2b) number of class name strings
5031 * (2b) number of method name strings
5032 * (2b) number of source file name strings
5033 * For each entry:
5034 * (4b) total allocation size
Elliott Hughes221229c2013-01-08 18:17:50 -08005035 * (2b) thread id
Elliott Hughes545a0642011-11-08 19:10:03 -08005036 * (2b) allocated object's class name index
5037 * (1b) stack depth
5038 * For each stack frame:
5039 * (2b) method's class name
5040 * (2b) method name
5041 * (2b) method source file
5042 * (2b) line number, clipped to 32767; -2 if native; -1 if no source
5043 * (xb) class name strings
5044 * (xb) method name strings
5045 * (xb) source file strings
5046 *
5047 * As with other DDM traffic, strings are sent as a 4-byte length
5048 * followed by UTF-16 data.
5049 *
5050 * We send up 16-bit unsigned indexes into string tables. In theory there
Brian Carlstrom306db812014-09-05 13:01:41 -07005051 * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in
Elliott Hughes545a0642011-11-08 19:10:03 -08005052 * each table, but in practice there should be far fewer.
5053 *
5054 * The chief reason for using a string table here is to keep the size of
5055 * the DDMS message to a minimum. This is partly to make the protocol
5056 * efficient, but also because we have to form the whole thing up all at
5057 * once in a memory buffer.
5058 *
5059 * We use separate string tables for class names, method names, and source
5060 * files to keep the indexes small. There will generally be no overlap
5061 * between the contents of these tables.
5062 */
5063jbyteArray Dbg::GetRecentAllocations() {
Ian Rogerscf7f1912014-10-22 22:06:39 -07005064 if ((false)) {
Elliott Hughes545a0642011-11-08 19:10:03 -08005065 DumpRecentAllocations();
5066 }
5067
Ian Rogers50b35e22012-10-04 10:09:15 -07005068 Thread* self = Thread::Current();
Elliott Hughes545a0642011-11-08 19:10:03 -08005069 std::vector<uint8_t> bytes;
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005070 {
Brian Carlstrom306db812014-09-05 13:01:41 -07005071 MutexLock mu(self, *Locks::alloc_tracker_lock_);
Man Cao8c2ff642015-05-27 17:25:30 -07005072 gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords();
5073 // In case this method is called when allocation tracker is disabled,
5074 // we should still send some data back.
5075 gc::AllocRecordObjectMap dummy;
5076 if (records == nullptr) {
5077 CHECK(!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled());
5078 records = &dummy;
5079 }
Man Cao41656de2015-07-06 18:53:15 -07005080 // We don't need to wait on the condition variable records->new_record_condition_, because this
5081 // function only reads the class objects, which are already marked so it doesn't change their
5082 // reachability.
Man Cao8c2ff642015-05-27 17:25:30 -07005083
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005084 //
5085 // Part 1: generate string tables.
5086 //
5087 StringTable class_names;
5088 StringTable method_names;
5089 StringTable filenames;
Elliott Hughes545a0642011-11-08 19:10:03 -08005090
Andreas Gampeff29cee2017-07-07 11:11:15 -07005091 VLOG(jdwp) << "Collecting StringTables.";
5092
Man Cao1ed11b92015-06-11 22:47:35 -07005093 const uint16_t capped_count = CappedAllocRecordCount(records->GetRecentAllocationSize());
Brian Carlstrom306db812014-09-05 13:01:41 -07005094 uint16_t count = capped_count;
Andreas Gamped0fc7682017-07-07 14:03:08 -07005095 size_t alloc_byte_count = 0;
Man Cao8c2ff642015-05-27 17:25:30 -07005096 for (auto it = records->RBegin(), end = records->REnd();
5097 count > 0 && it != end; count--, it++) {
Mathieu Chartier458b1052016-03-29 14:02:55 -07005098 const gc::AllocRecord* record = &it->second;
Ian Rogers1ff3c982014-08-12 02:30:58 -07005099 std::string temp;
Andreas Gampef774a4e2017-07-06 22:15:18 -07005100 const char* class_descr = record->GetClassDescriptor(&temp);
5101 class_names.Add(class_descr, !temp.empty());
Andreas Gamped0fc7682017-07-07 14:03:08 -07005102
5103 // Size + tid + class name index + stack depth.
5104 alloc_byte_count += 4u + 2u + 2u + 1u;
5105
Man Cao8c2ff642015-05-27 17:25:30 -07005106 for (size_t i = 0, depth = record->GetDepth(); i < depth; i++) {
5107 ArtMethod* m = record->StackElement(i).GetMethod();
Andreas Gampef774a4e2017-07-06 22:15:18 -07005108 class_names.Add(m->GetDeclaringClassDescriptor(), false);
5109 method_names.Add(m->GetName(), false);
5110 filenames.Add(GetMethodSourceFile(m), false);
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005111 }
Andreas Gamped0fc7682017-07-07 14:03:08 -07005112
5113 // Depth * (class index + method name index + file name index + line number).
5114 alloc_byte_count += record->GetDepth() * (2u + 2u + 2u + 2u);
Elliott Hughes545a0642011-11-08 19:10:03 -08005115 }
5116
Andreas Gampe2eeb01a2017-07-07 14:09:46 -07005117 class_names.Finish();
5118 method_names.Finish();
5119 filenames.Finish();
Andreas Gampeff29cee2017-07-07 11:11:15 -07005120 VLOG(jdwp) << "Done collecting StringTables:" << std::endl
5121 << " ClassNames: " << class_names.Size() << std::endl
5122 << " MethodNames: " << method_names.Size() << std::endl
5123 << " Filenames: " << filenames.Size();
5124
Man Cao8c2ff642015-05-27 17:25:30 -07005125 LOG(INFO) << "recent allocation records: " << capped_count;
5126 LOG(INFO) << "allocation records all objects: " << records->Size();
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005127
5128 //
5129 // Part 2: Generate the output and store it in the buffer.
5130 //
5131
5132 // (1b) message header len (to allow future expansion); includes itself
5133 // (1b) entry header len
5134 // (1b) stack frame len
5135 const int kMessageHeaderLen = 15;
5136 const int kEntryHeaderLen = 9;
5137 const int kStackFrameLen = 8;
5138 JDWP::Append1BE(bytes, kMessageHeaderLen);
5139 JDWP::Append1BE(bytes, kEntryHeaderLen);
5140 JDWP::Append1BE(bytes, kStackFrameLen);
5141
5142 // (2b) number of entries
5143 // (4b) offset to string table from start of message
5144 // (2b) number of class name strings
5145 // (2b) number of method name strings
5146 // (2b) number of source file name strings
Brian Carlstrom306db812014-09-05 13:01:41 -07005147 JDWP::Append2BE(bytes, capped_count);
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005148 size_t string_table_offset = bytes.size();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07005149 JDWP::Append4BE(bytes, 0); // We'll patch this later...
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005150 JDWP::Append2BE(bytes, class_names.Size());
5151 JDWP::Append2BE(bytes, method_names.Size());
5152 JDWP::Append2BE(bytes, filenames.Size());
5153
Andreas Gampeff29cee2017-07-07 11:11:15 -07005154 VLOG(jdwp) << "Dumping allocations with stacks";
5155
Andreas Gamped0fc7682017-07-07 14:03:08 -07005156 // Enlarge the vector for the allocation data.
5157 size_t reserve_size = bytes.size() + alloc_byte_count;
5158 bytes.reserve(reserve_size);
5159
Ian Rogers1ff3c982014-08-12 02:30:58 -07005160 std::string temp;
Man Cao8c2ff642015-05-27 17:25:30 -07005161 count = capped_count;
5162 // The last "count" number of allocation records in "records" are the most recent "count" number
5163 // of allocations. Reverse iterate to get them. The most recent allocation is sent first.
5164 for (auto it = records->RBegin(), end = records->REnd();
5165 count > 0 && it != end; count--, it++) {
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005166 // For each entry:
5167 // (4b) total allocation size
5168 // (2b) thread id
5169 // (2b) allocated object's class name index
5170 // (1b) stack depth
Mathieu Chartier458b1052016-03-29 14:02:55 -07005171 const gc::AllocRecord* record = &it->second;
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005172 size_t stack_depth = record->GetDepth();
Mathieu Chartierf8322842014-05-16 10:59:25 -07005173 size_t allocated_object_class_name_index =
Man Cao41656de2015-07-06 18:53:15 -07005174 class_names.IndexOf(record->GetClassDescriptor(&temp));
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07005175 JDWP::Append4BE(bytes, record->ByteCount());
Man Cao8c2ff642015-05-27 17:25:30 -07005176 JDWP::Append2BE(bytes, static_cast<uint16_t>(record->GetTid()));
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005177 JDWP::Append2BE(bytes, allocated_object_class_name_index);
5178 JDWP::Append1BE(bytes, stack_depth);
5179
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005180 for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
5181 // For each stack frame:
5182 // (2b) method's class name
5183 // (2b) method name
5184 // (2b) method source file
5185 // (2b) line number, clipped to 32767; -2 if native; -1 if no source
Man Cao8c2ff642015-05-27 17:25:30 -07005186 ArtMethod* m = record->StackElement(stack_frame).GetMethod();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07005187 size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor());
5188 size_t method_name_index = method_names.IndexOf(m->GetName());
5189 size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m));
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005190 JDWP::Append2BE(bytes, class_name_index);
5191 JDWP::Append2BE(bytes, method_name_index);
5192 JDWP::Append2BE(bytes, file_name_index);
Man Cao8c2ff642015-05-27 17:25:30 -07005193 JDWP::Append2BE(bytes, record->StackElement(stack_frame).ComputeLineNumber());
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005194 }
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005195 }
5196
Andreas Gamped0fc7682017-07-07 14:03:08 -07005197 CHECK_EQ(bytes.size(), reserve_size);
Andreas Gampeff29cee2017-07-07 11:11:15 -07005198 VLOG(jdwp) << "Dumping tables.";
5199
Mathieu Chartier46e811b2013-07-10 17:09:14 -07005200 // (xb) class name strings
5201 // (xb) method name strings
5202 // (xb) source file strings
5203 JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
5204 class_names.WriteTo(bytes);
5205 method_names.WriteTo(bytes);
5206 filenames.WriteTo(bytes);
Andreas Gampeff29cee2017-07-07 11:11:15 -07005207
5208 VLOG(jdwp) << "GetRecentAllocations: data created. " << bytes.size();
Elliott Hughes545a0642011-11-08 19:10:03 -08005209 }
Ian Rogers50b35e22012-10-04 10:09:15 -07005210 JNIEnv* env = self->GetJniEnv();
Elliott Hughes545a0642011-11-08 19:10:03 -08005211 jbyteArray result = env->NewByteArray(bytes.size());
Ian Rogersc0542af2014-09-03 16:16:56 -07005212 if (result != nullptr) {
Elliott Hughes545a0642011-11-08 19:10:03 -08005213 env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
5214 }
5215 return result;
5216}
5217
Mathieu Chartiere401d142015-04-22 13:56:20 -07005218ArtMethod* DeoptimizationRequest::Method() const {
Andreas Gampe13b27842016-11-07 16:48:23 -08005219 return jni::DecodeArtMethod(method_);
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07005220}
5221
Mathieu Chartiere401d142015-04-22 13:56:20 -07005222void DeoptimizationRequest::SetMethod(ArtMethod* m) {
Andreas Gampe13b27842016-11-07 16:48:23 -08005223 method_ = jni::EncodeArtMethod(m);
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07005224}
5225
Mathieu Chartiera6b1ead2015-10-06 10:32:38 -07005226void Dbg::VisitRoots(RootVisitor* visitor) {
5227 // Visit breakpoint roots, used to prevent unloading of methods with breakpoints.
5228 ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
5229 BufferedRootVisitor<128> root_visitor(visitor, RootInfo(kRootVMInternal));
5230 for (Breakpoint& breakpoint : gBreakpoints) {
Andreas Gampe542451c2016-07-26 09:02:02 -07005231 breakpoint.Method()->VisitRoots(root_visitor, kRuntimePointerSize);
Mathieu Chartiera6b1ead2015-10-06 10:32:38 -07005232 }
5233}
5234
Andreas Gampe04bbb5b2017-01-19 17:49:03 +00005235void Dbg::DbgThreadLifecycleCallback::ThreadStart(Thread* self) {
5236 Dbg::PostThreadStart(self);
5237}
5238
5239void Dbg::DbgThreadLifecycleCallback::ThreadDeath(Thread* self) {
5240 Dbg::PostThreadDeath(self);
5241}
5242
Andreas Gampe0f01b582017-01-18 15:22:37 -08005243void Dbg::DbgClassLoadCallback::ClassLoad(Handle<mirror::Class> klass ATTRIBUTE_UNUSED) {
5244 // Ignore ClassLoad;
5245}
5246void Dbg::DbgClassLoadCallback::ClassPrepare(Handle<mirror::Class> temp_klass ATTRIBUTE_UNUSED,
5247 Handle<mirror::Class> klass) {
5248 Dbg::PostClassPrepare(klass.Get());
5249}
5250
Elliott Hughes872d4ec2011-10-21 17:07:15 -07005251} // namespace art