blob: f1f1a204369e159238de3cf0a6c73ed43209f459 [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
Elliott Hughes545a0642011-11-08 19:10:03 -080021#include <set>
22
Ian Rogers166db042013-07-26 12:05:57 -070023#include "arch/context.h"
Elliott Hughes545a0642011-11-08 19:10:03 -080024#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070027#include "dex_instruction.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
29#include "gc/space/large_object_space.h"
30#include "gc/space/space-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031#include "handle_scope.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080032#include "jdwp/object_registry.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070033#include "mirror/art_field-inl.h"
34#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class.h"
36#include "mirror/class-inl.h"
37#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070040#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/throwable.h"
Sebastien Hertza76a6d42014-03-20 16:40:17 +010042#include "quick/inline_method_analyser.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070043#include "reflection.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070044#include "safe_map.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080045#include "scoped_thread_state_change.h"
Elliott Hughes6a5bd492011-10-28 14:33:57 -070046#include "ScopedLocalRef.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070047#include "ScopedPrimitiveArray.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070048#include "handle_scope-inl.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070049#include "thread_list.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050#include "utf.h"
Sebastien Hertza76a6d42014-03-20 16:40:17 +010051#include "verifier/method_verifier-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070052#include "well_known_classes.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070053
Brian Carlstrom3d92d522013-07-12 09:03:08 -070054#ifdef HAVE_ANDROID_OS
55#include "cutils/properties.h"
56#endif
57
Elliott Hughes872d4ec2011-10-21 17:07:15 -070058namespace art {
59
Brian Carlstrom7934ac22013-07-26 10:54:15 -070060static const size_t kMaxAllocRecordStackDepth = 16; // Max 255.
Brian Carlstrom306db812014-09-05 13:01:41 -070061static const size_t kDefaultNumAllocRecords = 64*1024; // Must be a power of 2. 2BE can hold 64k-1.
62
63// Limit alloc_record_count to the 2BE value that is the limit of the current protocol.
64static uint16_t CappedAllocRecordCount(size_t alloc_record_count) {
65 if (alloc_record_count > 0xffff) {
66 return 0xffff;
67 }
68 return alloc_record_count;
69}
Elliott Hughes475fc232011-10-25 15:00:35 -070070
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070071class AllocRecordStackTraceElement {
72 public:
73 AllocRecordStackTraceElement() : method_(nullptr), dex_pc_(0) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080074 }
75
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070076 int32_t LineNumber() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
77 mirror::ArtMethod* method = Method();
78 DCHECK(method != nullptr);
79 return method->GetLineNumFromDexPC(DexPc());
Elliott Hughes545a0642011-11-08 19:10:03 -080080 }
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070081
82 mirror::ArtMethod* Method() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier4345c462014-06-27 10:20:14 -070083 ScopedObjectAccessUnchecked soa(Thread::Current());
84 return soa.DecodeMethod(method_);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070085 }
86
87 void SetMethod(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
88 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier4345c462014-06-27 10:20:14 -070089 method_ = soa.EncodeMethod(m);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070090 }
91
92 uint32_t DexPc() const {
93 return dex_pc_;
94 }
95
96 void SetDexPc(uint32_t pc) {
97 dex_pc_ = pc;
98 }
99
100 private:
Mathieu Chartier4345c462014-06-27 10:20:14 -0700101 jmethodID method_;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700102 uint32_t dex_pc_;
Elliott Hughes545a0642011-11-08 19:10:03 -0800103};
104
Mathieu Chartier4345c462014-06-27 10:20:14 -0700105jobject Dbg::TypeCache::Add(mirror::Class* t) {
106 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier4c4d6092015-01-22 17:02:27 -0800107 JNIEnv* const env = soa.Env();
108 ScopedLocalRef<jobject> local_ref(soa.Env(), soa.AddLocalReference<jobject>(t));
109 const int32_t hash_code = soa.Decode<mirror::Class*>(local_ref.get())->IdentityHashCode();
Mathieu Chartier4345c462014-06-27 10:20:14 -0700110 auto range = objects_.equal_range(hash_code);
111 for (auto it = range.first; it != range.second; ++it) {
Mathieu Chartier4c4d6092015-01-22 17:02:27 -0800112 if (soa.Decode<mirror::Class*>(it->second) == soa.Decode<mirror::Class*>(local_ref.get())) {
Mathieu Chartier4345c462014-06-27 10:20:14 -0700113 // Found a matching weak global, return it.
114 return it->second;
115 }
116 }
Mathieu Chartier4c4d6092015-01-22 17:02:27 -0800117 const jobject weak_global = env->NewWeakGlobalRef(local_ref.get());
Mathieu Chartier4345c462014-06-27 10:20:14 -0700118 objects_.insert(std::make_pair(hash_code, weak_global));
119 return weak_global;
120}
121
122void Dbg::TypeCache::Clear() {
Brian Carlstrom306db812014-09-05 13:01:41 -0700123 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
124 Thread* self = Thread::Current();
Mathieu Chartier4345c462014-06-27 10:20:14 -0700125 for (const auto& p : objects_) {
Brian Carlstrom306db812014-09-05 13:01:41 -0700126 vm->DeleteWeakGlobalRef(self, p.second);
Mathieu Chartier4345c462014-06-27 10:20:14 -0700127 }
128 objects_.clear();
129}
130
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700131class AllocRecord {
132 public:
133 AllocRecord() : type_(nullptr), byte_count_(0), thin_lock_id_(0) {}
Elliott Hughes545a0642011-11-08 19:10:03 -0800134
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700135 mirror::Class* Type() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier4345c462014-06-27 10:20:14 -0700136 return down_cast<mirror::Class*>(Thread::Current()->DecodeJObject(type_));
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700137 }
138
Brian Carlstrom306db812014-09-05 13:01:41 -0700139 void SetType(mirror::Class* t) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_,
140 Locks::alloc_tracker_lock_) {
141 type_ = Dbg::type_cache_.Add(t);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700142 }
143
144 size_t GetDepth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes545a0642011-11-08 19:10:03 -0800145 size_t depth = 0;
Ian Rogersc0542af2014-09-03 16:16:56 -0700146 while (depth < kMaxAllocRecordStackDepth && stack_[depth].Method() != nullptr) {
Elliott Hughes545a0642011-11-08 19:10:03 -0800147 ++depth;
148 }
149 return depth;
150 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800151
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700152 size_t ByteCount() const {
153 return byte_count_;
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800154 }
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700155
156 void SetByteCount(size_t count) {
157 byte_count_ = count;
158 }
159
160 uint16_t ThinLockId() const {
161 return thin_lock_id_;
162 }
163
164 void SetThinLockId(uint16_t id) {
165 thin_lock_id_ = id;
166 }
167
168 AllocRecordStackTraceElement* StackElement(size_t index) {
169 DCHECK_LT(index, kMaxAllocRecordStackDepth);
170 return &stack_[index];
171 }
172
173 private:
174 jobject type_; // This is a weak global.
175 size_t byte_count_;
176 uint16_t thin_lock_id_;
Ian Rogersc0542af2014-09-03 16:16:56 -0700177 AllocRecordStackTraceElement stack_[kMaxAllocRecordStackDepth]; // Unused entries have nullptr method.
Elliott Hughes545a0642011-11-08 19:10:03 -0800178};
179
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700180class Breakpoint {
181 public:
Sebastien Hertzf3928792014-11-17 19:00:37 +0100182 Breakpoint(mirror::ArtMethod* method, uint32_t dex_pc,
183 DeoptimizationRequest::Kind deoptimization_kind)
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertzf3928792014-11-17 19:00:37 +0100185 : method_(nullptr), dex_pc_(dex_pc), deoptimization_kind_(deoptimization_kind) {
186 CHECK(deoptimization_kind_ == DeoptimizationRequest::kNothing ||
187 deoptimization_kind_ == DeoptimizationRequest::kSelectiveDeoptimization ||
188 deoptimization_kind_ == DeoptimizationRequest::kFullDeoptimization);
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700189 ScopedObjectAccessUnchecked soa(Thread::Current());
190 method_ = soa.EncodeMethod(method);
191 }
192
193 Breakpoint(const Breakpoint& other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
194 : method_(nullptr), dex_pc_(other.dex_pc_),
Sebastien Hertzf3928792014-11-17 19:00:37 +0100195 deoptimization_kind_(other.deoptimization_kind_) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700196 ScopedObjectAccessUnchecked soa(Thread::Current());
197 method_ = soa.EncodeMethod(other.Method());
198 }
199
200 mirror::ArtMethod* Method() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
201 ScopedObjectAccessUnchecked soa(Thread::Current());
202 return soa.DecodeMethod(method_);
203 }
204
205 uint32_t DexPc() const {
206 return dex_pc_;
207 }
208
Sebastien Hertzf3928792014-11-17 19:00:37 +0100209 DeoptimizationRequest::Kind GetDeoptimizationKind() const {
210 return deoptimization_kind_;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700211 }
212
213 private:
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100214 // The location of this breakpoint.
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700215 jmethodID method_;
216 uint32_t dex_pc_;
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100217
218 // Indicates whether breakpoint needs full deoptimization or selective deoptimization.
Sebastien Hertzf3928792014-11-17 19:00:37 +0100219 DeoptimizationRequest::Kind deoptimization_kind_;
Elliott Hughes86964332012-02-15 19:37:42 -0800220};
221
Sebastien Hertzed2be172014-08-19 15:33:43 +0200222static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700224 os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.Method()).c_str(), rhs.DexPc());
Elliott Hughes86964332012-02-15 19:37:42 -0800225 return os;
226}
227
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200228class DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener {
Ian Rogers62d6c772013-02-27 08:32:07 -0800229 public:
230 DebugInstrumentationListener() {}
231 virtual ~DebugInstrumentationListener() {}
232
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200233 void MethodEntered(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700234 uint32_t dex_pc ATTRIBUTE_UNUSED)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200235 OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800236 if (method->IsNative()) {
237 // TODO: post location events is a suspension point and native method entry stubs aren't.
238 return;
239 }
Sebastien Hertz8379b222014-02-24 17:38:15 +0100240 Dbg::UpdateDebugger(thread, this_object, method, 0, Dbg::kMethodEntry, nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800241 }
242
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200243 void MethodExited(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
244 uint32_t dex_pc, const JValue& return_value)
245 OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800246 if (method->IsNative()) {
247 // TODO: post location events is a suspension point and native method entry stubs aren't.
248 return;
249 }
Sebastien Hertz8379b222014-02-24 17:38:15 +0100250 Dbg::UpdateDebugger(thread, this_object, method, dex_pc, Dbg::kMethodExit, &return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800251 }
252
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200253 void MethodUnwind(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
254 uint32_t dex_pc)
255 OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800256 // We're not recorded to listen to this kind of event, so complain.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700257 UNUSED(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method)
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100259 << " " << dex_pc;
Ian Rogers62d6c772013-02-27 08:32:07 -0800260 }
261
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200262 void DexPcMoved(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
263 uint32_t new_dex_pc)
264 OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz8379b222014-02-24 17:38:15 +0100265 Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc, 0, nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800266 }
267
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200268 void FieldRead(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
269 uint32_t dex_pc, mirror::ArtField* field)
270 OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700271 UNUSED(thread);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200272 Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field);
Ian Rogers62d6c772013-02-27 08:32:07 -0800273 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200274
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700275 void FieldWritten(Thread* thread ATTRIBUTE_UNUSED, mirror::Object* this_object,
276 mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field,
277 const JValue& field_value)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200278 OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
279 Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value);
280 }
281
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000282 void ExceptionCaught(Thread* thread ATTRIBUTE_UNUSED, mirror::Throwable* exception_object)
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200283 OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000284 Dbg::PostException(exception_object);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200285 }
286
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800287 // We only care about how many backward branches were executed in the Jit.
288 void BackwardBranch(Thread* /*thread*/, mirror::ArtMethod* method, int32_t dex_pc_offset)
289 OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
290 LOG(ERROR) << "Unexpected backward branch event in debugger " << PrettyMethod(method)
291 << " " << dex_pc_offset;
292 }
293
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200294 private:
295 DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800296} gDebugInstrumentationListener;
297
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700298// JDWP is allowed unless the Zygote forbids it.
299static bool gJdwpAllowed = true;
300
Elliott Hughesc0f09332012-03-26 13:27:06 -0700301// Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line?
Elliott Hughes3bb81562011-10-21 18:52:59 -0700302static bool gJdwpConfigured = false;
303
Sebastien Hertzb3b173b2015-02-06 09:16:32 +0100304// JDWP options for debugging. Only valid if IsJdwpConfigured() is true.
305static JDWP::JdwpOptions gJdwpOptions;
306
Elliott Hughes3bb81562011-10-21 18:52:59 -0700307// Runtime JDWP state.
Ian Rogersc0542af2014-09-03 16:16:56 -0700308static JDWP::JdwpState* gJdwpState = nullptr;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700309static bool gDebuggerConnected; // debugger or DDMS is connected.
Elliott Hughes86964332012-02-15 19:37:42 -0800310static bool gDisposed; // debugger called VirtualMachine.Dispose, so we should drop the connection.
Elliott Hughes3bb81562011-10-21 18:52:59 -0700311
Elliott Hughes47fce012011-10-25 18:37:19 -0700312static bool gDdmThreadNotification = false;
313
Elliott Hughes767a1472011-10-26 18:49:02 -0700314// DDMS GC-related settings.
315static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
316static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
317static Dbg::HpsgWhat gDdmHpsgWhat;
318static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
319static Dbg::HpsgWhat gDdmNhsgWhat;
320
Daniel Mihalyieb076692014-08-22 17:33:31 +0200321bool Dbg::gDebuggerActive = false;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200322ObjectRegistry* Dbg::gRegistry = nullptr;
Elliott Hughes475fc232011-10-25 15:00:35 -0700323
Elliott Hughes545a0642011-11-08 19:10:03 -0800324// Recent allocation tracking.
Ian Rogers719d1a32014-03-06 12:13:39 -0800325AllocRecord* Dbg::recent_allocation_records_ = nullptr; // TODO: CircularBuffer<AllocRecord>
326size_t Dbg::alloc_record_max_ = 0;
327size_t Dbg::alloc_record_head_ = 0;
328size_t Dbg::alloc_record_count_ = 0;
Mathieu Chartier4345c462014-06-27 10:20:14 -0700329Dbg::TypeCache Dbg::type_cache_;
Elliott Hughes545a0642011-11-08 19:10:03 -0800330
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100331// Deoptimization support.
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100332std::vector<DeoptimizationRequest> Dbg::deoptimization_requests_;
333size_t Dbg::full_deoptimization_event_count_ = 0;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100334
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200335// Instrumentation event reference counters.
336size_t Dbg::dex_pc_change_event_ref_count_ = 0;
337size_t Dbg::method_enter_event_ref_count_ = 0;
338size_t Dbg::method_exit_event_ref_count_ = 0;
339size_t Dbg::field_read_event_ref_count_ = 0;
340size_t Dbg::field_write_event_ref_count_ = 0;
341size_t Dbg::exception_catch_event_ref_count_ = 0;
342uint32_t Dbg::instrumentation_events_ = 0;
343
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100344// Breakpoints.
jeffhao09bfc6a2012-12-11 18:11:43 -0800345static std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_);
Elliott Hughes86964332012-02-15 19:37:42 -0800346
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800347void DebugInvokeReq::VisitRoots(RootCallback* callback, void* arg, const RootInfo& root_info) {
Sebastien Hertz1558b572015-02-25 15:05:59 +0100348 receiver.VisitRootIfNonNull(callback, arg, root_info); // null for static method call.
349 klass.VisitRoot(callback, arg, root_info);
350 method.VisitRoot(callback, arg, root_info);
Sebastien Hertzbb43b432014-04-14 11:59:08 +0200351}
352
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800353void SingleStepControl::VisitRoots(RootCallback* callback, void* arg, const RootInfo& root_info) {
Sebastien Hertz597c4f02015-01-26 17:37:14 +0100354 if (method_ != nullptr) {
355 callback(reinterpret_cast<mirror::Object**>(&method_), arg, root_info);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700356 }
357}
358
Sebastien Hertz597c4f02015-01-26 17:37:14 +0100359void SingleStepControl::AddDexPc(uint32_t dex_pc) {
360 dex_pcs_.insert(dex_pc);
Sebastien Hertzbb43b432014-04-14 11:59:08 +0200361}
362
Sebastien Hertz597c4f02015-01-26 17:37:14 +0100363bool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const {
364 return dex_pcs_.find(dex_pc) == dex_pcs_.end();
Sebastien Hertzbb43b432014-04-14 11:59:08 +0200365}
366
Brian Carlstromea46f952013-07-30 01:26:50 -0700367static bool IsBreakpoint(const mirror::ArtMethod* m, uint32_t dex_pc)
jeffhao09bfc6a2012-12-11 18:11:43 -0800368 LOCKS_EXCLUDED(Locks::breakpoint_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700369 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzed2be172014-08-19 15:33:43 +0200370 ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100371 for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -0700372 if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].Method() == m) {
Elliott Hughes86964332012-02-15 19:37:42 -0800373 VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
374 return true;
375 }
376 }
377 return false;
378}
379
Sebastien Hertz52d131d2014-03-13 16:17:40 +0100380static bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread)
381 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) {
Elliott Hughes9e0c1752013-01-09 14:02:58 -0800382 MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
383 // A thread may be suspended for GC; in this code, we really want to know whether
384 // there's a debugger suspension active.
385 return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
386}
387
Ian Rogersc0542af2014-09-03 16:16:56 -0700388static mirror::Array* DecodeNonNullArray(JDWP::RefTypeId id, JDWP::JdwpError* error)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz6995c602014-09-09 12:10:13 +0200390 mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error);
Ian Rogersc0542af2014-09-03 16:16:56 -0700391 if (o == nullptr) {
392 *error = JDWP::ERR_INVALID_OBJECT;
393 return nullptr;
Elliott Hughes436e3722012-02-17 20:01:47 -0800394 }
395 if (!o->IsArrayInstance()) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700396 *error = JDWP::ERR_INVALID_ARRAY;
397 return nullptr;
Elliott Hughes436e3722012-02-17 20:01:47 -0800398 }
Ian Rogersc0542af2014-09-03 16:16:56 -0700399 *error = JDWP::ERR_NONE;
Elliott Hughes436e3722012-02-17 20:01:47 -0800400 return o->AsArray();
401}
402
Ian Rogersc0542af2014-09-03 16:16:56 -0700403static mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError* error)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700404 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz6995c602014-09-09 12:10:13 +0200405 mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id, error);
Ian Rogersc0542af2014-09-03 16:16:56 -0700406 if (o == nullptr) {
407 *error = JDWP::ERR_INVALID_OBJECT;
408 return nullptr;
Elliott Hughes436e3722012-02-17 20:01:47 -0800409 }
410 if (!o->IsClass()) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700411 *error = JDWP::ERR_INVALID_CLASS;
412 return nullptr;
Elliott Hughes436e3722012-02-17 20:01:47 -0800413 }
Ian Rogersc0542af2014-09-03 16:16:56 -0700414 *error = JDWP::ERR_NONE;
Elliott Hughes436e3722012-02-17 20:01:47 -0800415 return o->AsClass();
416}
417
Ian Rogersc0542af2014-09-03 16:16:56 -0700418static Thread* DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id,
419 JDWP::JdwpError* error)
jeffhaoa77f0f62012-12-05 17:19:31 -0800420 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700421 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
422 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz6995c602014-09-09 12:10:13 +0200423 mirror::Object* thread_peer = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_id, error);
Ian Rogersc0542af2014-09-03 16:16:56 -0700424 if (thread_peer == nullptr) {
Elliott Hughes221229c2013-01-08 18:17:50 -0800425 // This isn't even an object.
Ian Rogersc0542af2014-09-03 16:16:56 -0700426 *error = JDWP::ERR_INVALID_OBJECT;
427 return nullptr;
Elliott Hughes436e3722012-02-17 20:01:47 -0800428 }
Elliott Hughes221229c2013-01-08 18:17:50 -0800429
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430 mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
Elliott Hughes221229c2013-01-08 18:17:50 -0800431 if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
432 // This isn't a thread.
Ian Rogersc0542af2014-09-03 16:16:56 -0700433 *error = JDWP::ERR_INVALID_THREAD;
434 return nullptr;
Elliott Hughes221229c2013-01-08 18:17:50 -0800435 }
436
Ian Rogersc0542af2014-09-03 16:16:56 -0700437 Thread* thread = Thread::FromManagedThread(soa, thread_peer);
438 // If thread is null then this a java.lang.Thread without a Thread*. Must be a un-started or a
439 // zombie.
440 *error = (thread == nullptr) ? JDWP::ERR_THREAD_NOT_ALIVE : JDWP::ERR_NONE;
441 return thread;
Elliott Hughes436e3722012-02-17 20:01:47 -0800442}
443
Elliott Hughes24437992011-11-30 14:49:33 -0800444static JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
445 // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
446 // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
447 return static_cast<JDWP::JdwpTag>(descriptor[0]);
448}
449
Ian Rogers1ff3c982014-08-12 02:30:58 -0700450static JDWP::JdwpTag BasicTagFromClass(mirror::Class* klass)
451 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
452 std::string temp;
453 const char* descriptor = klass->GetDescriptor(&temp);
454 return BasicTagFromDescriptor(descriptor);
455}
456
Ian Rogers98379392014-02-24 16:53:16 -0800457static JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700458 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700459 CHECK(c != nullptr);
Elliott Hughes24437992011-11-30 14:49:33 -0800460 if (c->IsArrayClass()) {
461 return JDWP::JT_ARRAY;
462 }
Elliott Hughes24437992011-11-30 14:49:33 -0800463 if (c->IsStringClass()) {
464 return JDWP::JT_STRING;
Elliott Hughes24437992011-11-30 14:49:33 -0800465 }
Ian Rogers98379392014-02-24 16:53:16 -0800466 if (c->IsClassClass()) {
467 return JDWP::JT_CLASS_OBJECT;
468 }
469 {
470 mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
471 if (thread_class->IsAssignableFrom(c)) {
472 return JDWP::JT_THREAD;
473 }
474 }
475 {
476 mirror::Class* thread_group_class =
477 soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
478 if (thread_group_class->IsAssignableFrom(c)) {
479 return JDWP::JT_THREAD_GROUP;
480 }
481 }
482 {
483 mirror::Class* class_loader_class =
484 soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader);
485 if (class_loader_class->IsAssignableFrom(c)) {
486 return JDWP::JT_CLASS_LOADER;
487 }
488 }
489 return JDWP::JT_OBJECT;
Elliott Hughes24437992011-11-30 14:49:33 -0800490}
491
492/*
493 * Objects declared to hold Object might actually hold a more specific
494 * type. The debugger may take a special interest in these (e.g. it
495 * wants to display the contents of Strings), so we want to return an
496 * appropriate tag.
497 *
498 * Null objects are tagged JT_OBJECT.
499 */
Sebastien Hertz6995c602014-09-09 12:10:13 +0200500JDWP::JdwpTag Dbg::TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700501 return (o == nullptr) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass());
Elliott Hughes24437992011-11-30 14:49:33 -0800502}
503
504static bool IsPrimitiveTag(JDWP::JdwpTag tag) {
505 switch (tag) {
506 case JDWP::JT_BOOLEAN:
507 case JDWP::JT_BYTE:
508 case JDWP::JT_CHAR:
509 case JDWP::JT_FLOAT:
510 case JDWP::JT_DOUBLE:
511 case JDWP::JT_INT:
512 case JDWP::JT_LONG:
513 case JDWP::JT_SHORT:
514 case JDWP::JT_VOID:
515 return true;
516 default:
517 return false;
518 }
519}
520
Sebastien Hertzb3b173b2015-02-06 09:16:32 +0100521void Dbg::StartJdwp() {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700522 if (!gJdwpAllowed || !IsJdwpConfigured()) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700523 // No JDWP for you!
524 return;
525 }
526
Ian Rogers719d1a32014-03-06 12:13:39 -0800527 CHECK(gRegistry == nullptr);
Elliott Hughes475fc232011-10-25 15:00:35 -0700528 gRegistry = new ObjectRegistry;
529
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700530 // Init JDWP if the debugger is enabled. This may connect out to a
531 // debugger, passively listen for a debugger, or block waiting for a
532 // debugger.
Sebastien Hertzb3b173b2015-02-06 09:16:32 +0100533 gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
Ian Rogersc0542af2014-09-03 16:16:56 -0700534 if (gJdwpState == nullptr) {
Elliott Hughesf8a2df72011-12-01 12:19:54 -0800535 // We probably failed because some other process has the port already, which means that
536 // if we don't abort the user is likely to think they're talking to us when they're actually
537 // talking to that other process.
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800538 LOG(FATAL) << "Debugger thread failed to initialize";
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700539 }
540
541 // If a debugger has already attached, send the "welcome" message.
542 // This may cause us to suspend all threads.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700543 if (gJdwpState->IsActive()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700544 ScopedObjectAccess soa(Thread::Current());
Sebastien Hertz7d955652014-10-22 10:57:10 +0200545 gJdwpState->PostVMStart();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700546 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700547}
548
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700549void Dbg::StopJdwp() {
Sebastien Hertzc6345ef2014-08-18 19:26:39 +0200550 // Post VM_DEATH event before the JDWP connection is closed (either by the JDWP thread or the
551 // destruction of gJdwpState).
552 if (gJdwpState != nullptr && gJdwpState->IsActive()) {
553 gJdwpState->PostVMDeath();
554 }
Sebastien Hertz0376e6b2014-02-06 18:12:59 +0100555 // Prevent the JDWP thread from processing JDWP incoming packets after we close the connection.
556 Disposed();
Elliott Hughes376a7a02011-10-24 18:35:55 -0700557 delete gJdwpState;
Ian Rogers719d1a32014-03-06 12:13:39 -0800558 gJdwpState = nullptr;
Elliott Hughes475fc232011-10-25 15:00:35 -0700559 delete gRegistry;
Ian Rogers719d1a32014-03-06 12:13:39 -0800560 gRegistry = nullptr;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700561}
562
Elliott Hughes767a1472011-10-26 18:49:02 -0700563void Dbg::GcDidFinish() {
564 if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700565 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700566 VLOG(jdwp) << "Sending heap info to DDM";
Elliott Hughes7162ad92011-10-27 14:08:42 -0700567 DdmSendHeapInfo(gDdmHpifWhen);
Elliott Hughes767a1472011-10-26 18:49:02 -0700568 }
569 if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700570 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700571 VLOG(jdwp) << "Dumping heap to DDM";
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700572 DdmSendHeapSegments(false);
Elliott Hughes767a1472011-10-26 18:49:02 -0700573 }
574 if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700575 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700576 VLOG(jdwp) << "Dumping native heap to DDM";
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700577 DdmSendHeapSegments(true);
Elliott Hughes767a1472011-10-26 18:49:02 -0700578 }
579}
580
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700581void Dbg::SetJdwpAllowed(bool allowed) {
582 gJdwpAllowed = allowed;
583}
584
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700585DebugInvokeReq* Dbg::GetInvokeReq() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700586 return Thread::Current()->GetInvokeReq();
587}
588
589Thread* Dbg::GetDebugThread() {
Ian Rogersc0542af2014-09-03 16:16:56 -0700590 return (gJdwpState != nullptr) ? gJdwpState->GetDebugThread() : nullptr;
Elliott Hughes475fc232011-10-25 15:00:35 -0700591}
592
593void Dbg::ClearWaitForEventThread() {
Sebastien Hertz2bf93f42015-01-09 18:44:05 +0100594 gJdwpState->ReleaseJdwpTokenForEvent();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700595}
596
597void Dbg::Connected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700598 CHECK(!gDebuggerConnected);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800599 VLOG(jdwp) << "JDWP has attached";
Elliott Hughes3bb81562011-10-21 18:52:59 -0700600 gDebuggerConnected = true;
Elliott Hughes86964332012-02-15 19:37:42 -0800601 gDisposed = false;
602}
603
604void Dbg::Disposed() {
605 gDisposed = true;
606}
607
608bool Dbg::IsDisposed() {
609 return gDisposed;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700610}
611
Sebastien Hertzf3928792014-11-17 19:00:37 +0100612bool Dbg::RequiresDeoptimization() {
613 // We don't need deoptimization if everything runs with interpreter after
614 // enabling -Xint mode.
615 return !Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly();
616}
617
Elliott Hughesa2155262011-11-16 16:26:58 -0800618void Dbg::GoActive() {
619 // Enable all debugging features, including scans for breakpoints.
620 // This is a no-op if we're already active.
621 // Only called from the JDWP handler thread.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200622 if (IsDebuggerActive()) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800623 return;
624 }
625
Elliott Hughesc0f09332012-03-26 13:27:06 -0700626 {
627 // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
Sebastien Hertzed2be172014-08-19 15:33:43 +0200628 ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Elliott Hughesc0f09332012-03-26 13:27:06 -0700629 CHECK_EQ(gBreakpoints.size(), 0U);
630 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800631
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100632 {
Brian Carlstrom306db812014-09-05 13:01:41 -0700633 MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100634 CHECK_EQ(deoptimization_requests_.size(), 0U);
635 CHECK_EQ(full_deoptimization_event_count_, 0U);
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200636 CHECK_EQ(dex_pc_change_event_ref_count_, 0U);
637 CHECK_EQ(method_enter_event_ref_count_, 0U);
638 CHECK_EQ(method_exit_event_ref_count_, 0U);
639 CHECK_EQ(field_read_event_ref_count_, 0U);
640 CHECK_EQ(field_write_event_ref_count_, 0U);
641 CHECK_EQ(exception_catch_event_ref_count_, 0U);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100642 }
643
Ian Rogers62d6c772013-02-27 08:32:07 -0800644 Runtime* runtime = Runtime::Current();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -0700645 runtime->GetThreadList()->SuspendAll(__FUNCTION__);
Ian Rogers62d6c772013-02-27 08:32:07 -0800646 Thread* self = Thread::Current();
647 ThreadState old_state = self->SetStateUnsafe(kRunnable);
648 CHECK_NE(old_state, kRunnable);
Sebastien Hertzf3928792014-11-17 19:00:37 +0100649 if (RequiresDeoptimization()) {
650 runtime->GetInstrumentation()->EnableDeoptimization();
651 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200652 instrumentation_events_ = 0;
Elliott Hughesa2155262011-11-16 16:26:58 -0800653 gDebuggerActive = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800654 CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
655 runtime->GetThreadList()->ResumeAll();
656
657 LOG(INFO) << "Debugger is active";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700658}
659
660void Dbg::Disconnected() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700661 CHECK(gDebuggerConnected);
662
Elliott Hughesc0f09332012-03-26 13:27:06 -0700663 LOG(INFO) << "Debugger is no longer active";
Elliott Hughes234ab152011-10-26 14:02:26 -0700664
Ian Rogers62d6c772013-02-27 08:32:07 -0800665 // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread
666 // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener
667 // and clear the object registry.
668 Runtime* runtime = Runtime::Current();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -0700669 runtime->GetThreadList()->SuspendAll(__FUNCTION__);
Ian Rogers62d6c772013-02-27 08:32:07 -0800670 Thread* self = Thread::Current();
671 ThreadState old_state = self->SetStateUnsafe(kRunnable);
Sebastien Hertzaaea7342014-02-25 15:10:04 +0100672
673 // Debugger may not be active at this point.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200674 if (IsDebuggerActive()) {
Sebastien Hertzaaea7342014-02-25 15:10:04 +0100675 {
676 // Since we're going to disable deoptimization, we clear the deoptimization requests queue.
677 // This prevents us from having any pending deoptimization request when the debugger attaches
678 // to us again while no event has been requested yet.
Brian Carlstrom306db812014-09-05 13:01:41 -0700679 MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100680 deoptimization_requests_.clear();
681 full_deoptimization_event_count_ = 0U;
Sebastien Hertzaaea7342014-02-25 15:10:04 +0100682 }
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200683 if (instrumentation_events_ != 0) {
684 runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener,
685 instrumentation_events_);
686 instrumentation_events_ = 0;
687 }
Sebastien Hertzf3928792014-11-17 19:00:37 +0100688 if (RequiresDeoptimization()) {
689 runtime->GetInstrumentation()->DisableDeoptimization();
690 }
Sebastien Hertzaaea7342014-02-25 15:10:04 +0100691 gDebuggerActive = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100692 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800693 CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
694 runtime->GetThreadList()->ResumeAll();
Sebastien Hertz55f65342015-01-13 22:48:34 +0100695
696 {
697 ScopedObjectAccess soa(self);
698 gRegistry->Clear();
699 }
700
701 gDebuggerConnected = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700702}
703
Sebastien Hertzb3b173b2015-02-06 09:16:32 +0100704void Dbg::ConfigureJdwp(const JDWP::JdwpOptions& jdwp_options) {
705 CHECK_NE(jdwp_options.transport, JDWP::kJdwpTransportUnknown);
706 gJdwpOptions = jdwp_options;
707 gJdwpConfigured = true;
708}
709
Elliott Hughesc0f09332012-03-26 13:27:06 -0700710bool Dbg::IsJdwpConfigured() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700711 return gJdwpConfigured;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700712}
713
714int64_t Dbg::LastDebuggerActivity() {
Elliott Hughesca951522011-12-05 12:01:32 -0800715 return gJdwpState->LastDebuggerActivity();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700716}
717
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700718void Dbg::UndoDebuggerSuspensions() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700719 Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700720}
721
Elliott Hughes88d63092013-01-09 09:55:54 -0800722std::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700723 JDWP::JdwpError error;
724 mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id, &error);
725 if (o == nullptr) {
726 if (error == JDWP::ERR_NONE) {
727 return "NULL";
728 } else {
729 return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
730 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800731 }
732 if (!o->IsClass()) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700733 return StringPrintf("non-class %p", o); // This is only used for debugging output anyway.
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800734 }
Sebastien Hertz6995c602014-09-09 12:10:13 +0200735 return GetClassName(o->AsClass());
736}
737
738std::string Dbg::GetClassName(mirror::Class* klass) {
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +0200739 if (klass == nullptr) {
740 return "NULL";
741 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700742 std::string temp;
Sebastien Hertz6995c602014-09-09 12:10:13 +0200743 return DescriptorToName(klass->GetDescriptor(&temp));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700744}
745
Ian Rogersc0542af2014-09-03 16:16:56 -0700746JDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId* class_object_id) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800747 JDWP::JdwpError status;
Ian Rogersc0542af2014-09-03 16:16:56 -0700748 mirror::Class* c = DecodeClass(id, &status);
749 if (c == nullptr) {
750 *class_object_id = 0;
Elliott Hughes436e3722012-02-17 20:01:47 -0800751 return status;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800752 }
Ian Rogersc0542af2014-09-03 16:16:56 -0700753 *class_object_id = gRegistry->Add(c);
Elliott Hughes436e3722012-02-17 20:01:47 -0800754 return JDWP::ERR_NONE;
Elliott Hughes86964332012-02-15 19:37:42 -0800755}
756
Ian Rogersc0542af2014-09-03 16:16:56 -0700757JDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId* superclass_id) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800758 JDWP::JdwpError status;
Ian Rogersc0542af2014-09-03 16:16:56 -0700759 mirror::Class* c = DecodeClass(id, &status);
760 if (c == nullptr) {
761 *superclass_id = 0;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800762 return status;
763 }
764 if (c->IsInterface()) {
765 // http://code.google.com/p/android/issues/detail?id=20856
Ian Rogersc0542af2014-09-03 16:16:56 -0700766 *superclass_id = 0;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800767 } else {
Ian Rogersc0542af2014-09-03 16:16:56 -0700768 *superclass_id = gRegistry->Add(c->GetSuperClass());
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800769 }
770 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700771}
772
Elliott Hughes436e3722012-02-17 20:01:47 -0800773JDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700774 JDWP::JdwpError error;
775 mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error);
776 if (o == nullptr) {
Elliott Hughes436e3722012-02-17 20:01:47 -0800777 return JDWP::ERR_INVALID_OBJECT;
778 }
779 expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader()));
780 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700781}
782
Elliott Hughes436e3722012-02-17 20:01:47 -0800783JDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700784 JDWP::JdwpError error;
785 mirror::Class* c = DecodeClass(id, &error);
786 if (c == nullptr) {
787 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800788 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800789
790 uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
791
Yevgeny Roubande34eea2014-02-15 01:06:03 +0700792 // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set,
793 // not interfaces.
Elliott Hughes436e3722012-02-17 20:01:47 -0800794 // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
Yevgeny Roubande34eea2014-02-15 01:06:03 +0700795 if ((access_flags & kAccInterface) == 0) {
796 access_flags |= kAccSuper;
797 }
Elliott Hughes436e3722012-02-17 20:01:47 -0800798
799 expandBufAdd4BE(pReply, access_flags);
800
801 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700802}
803
Ian Rogersc0542af2014-09-03 16:16:56 -0700804JDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) {
805 JDWP::JdwpError error;
806 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error);
807 if (o == nullptr) {
Elliott Hughesf327e072013-01-09 16:01:26 -0800808 return JDWP::ERR_INVALID_OBJECT;
809 }
810
811 // Ensure all threads are suspended while we read objects' lock words.
812 Thread* self = Thread::Current();
Sebastien Hertz54263242014-03-19 18:16:50 +0100813 CHECK_EQ(self->GetState(), kRunnable);
814 self->TransitionFromRunnableToSuspended(kSuspended);
Mathieu Chartierbf9fc582015-03-13 17:21:25 -0700815 Runtime::Current()->GetThreadList()->SuspendAll(__FUNCTION__);
Elliott Hughesf327e072013-01-09 16:01:26 -0800816
817 MonitorInfo monitor_info(o);
818
Sebastien Hertz54263242014-03-19 18:16:50 +0100819 Runtime::Current()->GetThreadList()->ResumeAll();
820 self->TransitionFromSuspendedToRunnable();
Elliott Hughesf327e072013-01-09 16:01:26 -0800821
Ian Rogersc0542af2014-09-03 16:16:56 -0700822 if (monitor_info.owner_ != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700823 expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer()));
Elliott Hughesf327e072013-01-09 16:01:26 -0800824 } else {
Ian Rogersc0542af2014-09-03 16:16:56 -0700825 expandBufAddObjectId(reply, gRegistry->Add(nullptr));
Elliott Hughesf327e072013-01-09 16:01:26 -0800826 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700827 expandBufAdd4BE(reply, monitor_info.entry_count_);
828 expandBufAdd4BE(reply, monitor_info.waiters_.size());
829 for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) {
830 expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer()));
Elliott Hughesf327e072013-01-09 16:01:26 -0800831 }
832 return JDWP::ERR_NONE;
833}
834
Elliott Hughes734b8c62013-01-11 15:32:45 -0800835JDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
Ian Rogersc0542af2014-09-03 16:16:56 -0700836 std::vector<JDWP::ObjectId>* monitors,
837 std::vector<uint32_t>* stack_depths) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800838 struct OwnedMonitorVisitor : public StackVisitor {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700839 OwnedMonitorVisitor(Thread* thread, Context* context,
Hiroshi Yamauchicc8c5c52014-06-13 15:08:05 -0700840 std::vector<JDWP::ObjectId>* monitor_vector,
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700841 std::vector<uint32_t>* stack_depth_vector)
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800842 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700843 : StackVisitor(thread, context), current_stack_depth(0),
844 monitors(monitor_vector), stack_depths(stack_depth_vector) {}
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800845
846 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
847 // annotalysis.
848 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
849 if (!GetMethod()->IsRuntimeMethod()) {
850 Monitor::VisitLocks(this, AppendOwnedMonitors, this);
Elliott Hughes734b8c62013-01-11 15:32:45 -0800851 ++current_stack_depth;
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800852 }
853 return true;
854 }
855
Hiroshi Yamauchicc8c5c52014-06-13 15:08:05 -0700856 static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg)
857 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800858 OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
Hiroshi Yamauchicc8c5c52014-06-13 15:08:05 -0700859 visitor->monitors->push_back(gRegistry->Add(owned_monitor));
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700860 visitor->stack_depths->push_back(visitor->current_stack_depth);
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800861 }
862
Elliott Hughes734b8c62013-01-11 15:32:45 -0800863 size_t current_stack_depth;
Ian Rogersc0542af2014-09-03 16:16:56 -0700864 std::vector<JDWP::ObjectId>* const monitors;
865 std::vector<uint32_t>* const stack_depths;
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800866 };
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800867
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700868 ScopedObjectAccessUnchecked soa(Thread::Current());
Hiroshi Yamauchicc8c5c52014-06-13 15:08:05 -0700869 Thread* thread;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700870 {
871 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -0700872 JDWP::JdwpError error;
873 thread = DecodeThread(soa, thread_id, &error);
874 if (thread == nullptr) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700875 return error;
876 }
877 if (!IsSuspendedForDebugger(soa, thread)) {
878 return JDWP::ERR_THREAD_NOT_SUSPENDED;
879 }
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700880 }
Hiroshi Yamauchicc8c5c52014-06-13 15:08:05 -0700881 std::unique_ptr<Context> context(Context::Create());
Ian Rogersc0542af2014-09-03 16:16:56 -0700882 OwnedMonitorVisitor visitor(thread, context.get(), monitors, stack_depths);
Hiroshi Yamauchicc8c5c52014-06-13 15:08:05 -0700883 visitor.WalkStack();
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800884 return JDWP::ERR_NONE;
885}
886
Sebastien Hertz52d131d2014-03-13 16:17:40 +0100887JDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id,
Ian Rogersc0542af2014-09-03 16:16:56 -0700888 JDWP::ObjectId* contended_monitor) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700889 mirror::Object* contended_monitor_obj;
Elliott Hughesf9501702013-01-11 11:22:27 -0800890 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -0700891 *contended_monitor = 0;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700892 {
893 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -0700894 JDWP::JdwpError error;
895 Thread* thread = DecodeThread(soa, thread_id, &error);
896 if (thread == nullptr) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700897 return error;
898 }
899 if (!IsSuspendedForDebugger(soa, thread)) {
900 return JDWP::ERR_THREAD_NOT_SUSPENDED;
901 }
902 contended_monitor_obj = Monitor::GetContendedMonitor(thread);
Elliott Hughesf9501702013-01-11 11:22:27 -0800903 }
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700904 // Add() requires the thread_list_lock_ not held to avoid the lock
905 // level violation.
Ian Rogersc0542af2014-09-03 16:16:56 -0700906 *contended_monitor = gRegistry->Add(contended_monitor_obj);
Elliott Hughesf9501702013-01-11 11:22:27 -0800907 return JDWP::ERR_NONE;
908}
909
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800910JDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
Ian Rogersc0542af2014-09-03 16:16:56 -0700911 std::vector<uint64_t>* counts) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800912 gc::Heap* heap = Runtime::Current()->GetHeap();
913 heap->CollectGarbage(false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800914 std::vector<mirror::Class*> classes;
Ian Rogersc0542af2014-09-03 16:16:56 -0700915 counts->clear();
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800916 for (size_t i = 0; i < class_ids.size(); ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700917 JDWP::JdwpError error;
918 mirror::Class* c = DecodeClass(class_ids[i], &error);
919 if (c == nullptr) {
920 return error;
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800921 }
922 classes.push_back(c);
Ian Rogersc0542af2014-09-03 16:16:56 -0700923 counts->push_back(0);
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800924 }
Ian Rogersc0542af2014-09-03 16:16:56 -0700925 heap->CountInstances(classes, false, &(*counts)[0]);
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800926 return JDWP::ERR_NONE;
927}
928
Ian Rogersc0542af2014-09-03 16:16:56 -0700929JDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count,
930 std::vector<JDWP::ObjectId>* instances) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800931 gc::Heap* heap = Runtime::Current()->GetHeap();
932 // We only want reachable instances, so do a GC.
933 heap->CollectGarbage(false);
Ian Rogersc0542af2014-09-03 16:16:56 -0700934 JDWP::JdwpError error;
935 mirror::Class* c = DecodeClass(class_id, &error);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800936 if (c == nullptr) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700937 return error;
Elliott Hughes3b78c942013-01-15 17:35:41 -0800938 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800939 std::vector<mirror::Object*> raw_instances;
Elliott Hughes3b78c942013-01-15 17:35:41 -0800940 Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances);
941 for (size_t i = 0; i < raw_instances.size(); ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700942 instances->push_back(gRegistry->Add(raw_instances[i]));
Elliott Hughes3b78c942013-01-15 17:35:41 -0800943 }
944 return JDWP::ERR_NONE;
945}
946
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800947JDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
Ian Rogersc0542af2014-09-03 16:16:56 -0700948 std::vector<JDWP::ObjectId>* referring_objects) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800949 gc::Heap* heap = Runtime::Current()->GetHeap();
950 heap->CollectGarbage(false);
Ian Rogersc0542af2014-09-03 16:16:56 -0700951 JDWP::JdwpError error;
952 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error);
953 if (o == nullptr) {
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800954 return JDWP::ERR_INVALID_OBJECT;
955 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800956 std::vector<mirror::Object*> raw_instances;
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800957 heap->GetReferringObjects(o, max_count, raw_instances);
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800958 for (size_t i = 0; i < raw_instances.size(); ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700959 referring_objects->push_back(gRegistry->Add(raw_instances[i]));
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800960 }
961 return JDWP::ERR_NONE;
962}
963
Ian Rogersc0542af2014-09-03 16:16:56 -0700964JDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id) {
965 JDWP::JdwpError error;
966 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error);
967 if (o == nullptr) {
Sebastien Hertze96060a2013-12-11 12:06:28 +0100968 return JDWP::ERR_INVALID_OBJECT;
969 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800970 gRegistry->DisableCollection(object_id);
971 return JDWP::ERR_NONE;
972}
973
Ian Rogersc0542af2014-09-03 16:16:56 -0700974JDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id) {
975 JDWP::JdwpError error;
976 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100977 // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI
978 // also ignores these cases and never return an error. However it's not obvious why this command
979 // should behave differently from DisableCollection and IsCollected commands. So let's be more
980 // strict and return an error if this happens.
Ian Rogersc0542af2014-09-03 16:16:56 -0700981 if (o == nullptr) {
Sebastien Hertze96060a2013-12-11 12:06:28 +0100982 return JDWP::ERR_INVALID_OBJECT;
983 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800984 gRegistry->EnableCollection(object_id);
985 return JDWP::ERR_NONE;
986}
987
Ian Rogersc0542af2014-09-03 16:16:56 -0700988JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool* is_collected) {
989 *is_collected = true;
Sebastien Hertz65637eb2014-01-10 17:40:02 +0100990 if (object_id == 0) {
991 // Null object id is invalid.
Sebastien Hertze96060a2013-12-11 12:06:28 +0100992 return JDWP::ERR_INVALID_OBJECT;
993 }
Sebastien Hertz65637eb2014-01-10 17:40:02 +0100994 // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However
995 // the RI seems to ignore this and assume object has been collected.
Ian Rogersc0542af2014-09-03 16:16:56 -0700996 JDWP::JdwpError error;
997 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error);
998 if (o != nullptr) {
999 *is_collected = gRegistry->IsCollected(object_id);
Sebastien Hertz65637eb2014-01-10 17:40:02 +01001000 }
Elliott Hughes64f574f2013-02-20 14:57:12 -08001001 return JDWP::ERR_NONE;
1002}
1003
Ian Rogersc0542af2014-09-03 16:16:56 -07001004void Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) {
Elliott Hughes64f574f2013-02-20 14:57:12 -08001005 gRegistry->DisposeObject(object_id, reference_count);
1006}
1007
Sebastien Hertz6995c602014-09-09 12:10:13 +02001008JDWP::JdwpTypeTag Dbg::GetTypeTag(mirror::Class* klass) {
Sebastien Hertz4d8fd492014-03-28 16:29:41 +01001009 DCHECK(klass != nullptr);
1010 if (klass->IsArrayClass()) {
1011 return JDWP::TT_ARRAY;
1012 } else if (klass->IsInterface()) {
1013 return JDWP::TT_INTERFACE;
1014 } else {
1015 return JDWP::TT_CLASS;
1016 }
1017}
1018
Elliott Hughes88d63092013-01-09 09:55:54 -08001019JDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001020 JDWP::JdwpError error;
1021 mirror::Class* c = DecodeClass(class_id, &error);
1022 if (c == nullptr) {
1023 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001024 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001025
Sebastien Hertz4d8fd492014-03-28 16:29:41 +01001026 JDWP::JdwpTypeTag type_tag = GetTypeTag(c);
1027 expandBufAdd1(pReply, type_tag);
Elliott Hughes88d63092013-01-09 09:55:54 -08001028 expandBufAddRefTypeId(pReply, class_id);
Elliott Hughes436e3722012-02-17 20:01:47 -08001029 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001030}
1031
Ian Rogersc0542af2014-09-03 16:16:56 -07001032void Dbg::GetClassList(std::vector<JDWP::RefTypeId>* classes) {
Elliott Hughesa2155262011-11-16 16:26:58 -08001033 // Get the complete list of reference classes (i.e. all classes except
1034 // the primitive types).
1035 // Returns a newly-allocated buffer full of RefTypeId values.
1036 struct ClassListCreator {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001037 explicit ClassListCreator(std::vector<JDWP::RefTypeId>* classes_in) : classes(classes_in) {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001038 }
1039
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001040 static bool Visit(mirror::Class* c, void* arg) {
Elliott Hughesa2155262011-11-16 16:26:58 -08001041 return reinterpret_cast<ClassListCreator*>(arg)->Visit(c);
1042 }
1043
Elliott Hughes64f574f2013-02-20 14:57:12 -08001044 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
1045 // annotalysis.
1046 bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughesa2155262011-11-16 16:26:58 -08001047 if (!c->IsPrimitive()) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001048 classes->push_back(gRegistry->AddRefType(c));
Elliott Hughesa2155262011-11-16 16:26:58 -08001049 }
1050 return true;
1051 }
1052
Ian Rogersc0542af2014-09-03 16:16:56 -07001053 std::vector<JDWP::RefTypeId>* const classes;
Elliott Hughesa2155262011-11-16 16:26:58 -08001054 };
1055
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001056 ClassListCreator clc(classes);
Sebastien Hertz4537c412014-08-28 14:41:50 +02001057 Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(ClassListCreator::Visit,
1058 &clc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001059}
1060
Ian Rogers1ff3c982014-08-12 02:30:58 -07001061JDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag,
1062 uint32_t* pStatus, std::string* pDescriptor) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001063 JDWP::JdwpError error;
1064 mirror::Class* c = DecodeClass(class_id, &error);
1065 if (c == nullptr) {
1066 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001067 }
1068
Elliott Hughesa2155262011-11-16 16:26:58 -08001069 if (c->IsArrayClass()) {
1070 *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
1071 *pTypeTag = JDWP::TT_ARRAY;
1072 } else {
1073 if (c->IsErroneous()) {
1074 *pStatus = JDWP::CS_ERROR;
1075 } else {
1076 *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
1077 }
1078 *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1079 }
1080
Ian Rogersc0542af2014-09-03 16:16:56 -07001081 if (pDescriptor != nullptr) {
Ian Rogers1ff3c982014-08-12 02:30:58 -07001082 std::string temp;
1083 *pDescriptor = c->GetDescriptor(&temp);
Elliott Hughesa2155262011-11-16 16:26:58 -08001084 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001085 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001086}
1087
Ian Rogersc0542af2014-09-03 16:16:56 -07001088void Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>* ids) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001089 std::vector<mirror::Class*> classes;
Elliott Hughes6fa602d2011-12-02 17:54:25 -08001090 Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
Ian Rogersc0542af2014-09-03 16:16:56 -07001091 ids->clear();
Elliott Hughes6fa602d2011-12-02 17:54:25 -08001092 for (size_t i = 0; i < classes.size(); ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001093 ids->push_back(gRegistry->Add(classes[i]));
Elliott Hughes6fa602d2011-12-02 17:54:25 -08001094 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001095}
1096
Ian Rogersc0542af2014-09-03 16:16:56 -07001097JDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) {
1098 JDWP::JdwpError error;
1099 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error);
1100 if (o == nullptr) {
Elliott Hughes2435a572012-02-17 16:07:41 -08001101 return JDWP::ERR_INVALID_OBJECT;
Elliott Hughes499c5132011-11-17 14:55:11 -08001102 }
Elliott Hughes2435a572012-02-17 16:07:41 -08001103
Sebastien Hertz4d8fd492014-03-28 16:29:41 +01001104 JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass());
Elliott Hughes64f574f2013-02-20 14:57:12 -08001105 JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
Elliott Hughes2435a572012-02-17 16:07:41 -08001106
1107 expandBufAdd1(pReply, type_tag);
1108 expandBufAddRefTypeId(pReply, type_id);
1109
1110 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001111}
1112
Ian Rogersfc0e94b2013-09-23 23:51:32 -07001113JDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001114 JDWP::JdwpError error;
1115 mirror::Class* c = DecodeClass(class_id, &error);
1116 if (c == nullptr) {
1117 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001118 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001119 std::string temp;
1120 *signature = c->GetDescriptor(&temp);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -08001121 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001122}
1123
Ian Rogersc0542af2014-09-03 16:16:56 -07001124JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string* result) {
1125 JDWP::JdwpError error;
1126 mirror::Class* c = DecodeClass(class_id, &error);
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001127 if (c == nullptr) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001128 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001129 }
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001130 const char* source_file = c->GetSourceFile();
1131 if (source_file == nullptr) {
Sebastien Hertzb7054ba2014-03-13 11:52:31 +01001132 return JDWP::ERR_ABSENT_INFORMATION;
1133 }
Ian Rogersc0542af2014-09-03 16:16:56 -07001134 *result = source_file;
Elliott Hughes436e3722012-02-17 20:01:47 -08001135 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001136}
1137
Ian Rogersc0542af2014-09-03 16:16:56 -07001138JDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t* tag) {
Ian Rogers98379392014-02-24 16:53:16 -08001139 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07001140 JDWP::JdwpError error;
1141 mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id, &error);
1142 if (error != JDWP::ERR_NONE) {
1143 *tag = JDWP::JT_VOID;
1144 return error;
Elliott Hughes546b9862012-06-20 16:06:13 -07001145 }
Ian Rogersc0542af2014-09-03 16:16:56 -07001146 *tag = TagFromObject(soa, o);
Elliott Hughes546b9862012-06-20 16:06:13 -07001147 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001148}
1149
Elliott Hughesaed4be92011-12-02 16:16:23 -08001150size_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001151 switch (tag) {
1152 case JDWP::JT_VOID:
1153 return 0;
1154 case JDWP::JT_BYTE:
1155 case JDWP::JT_BOOLEAN:
1156 return 1;
1157 case JDWP::JT_CHAR:
1158 case JDWP::JT_SHORT:
1159 return 2;
1160 case JDWP::JT_FLOAT:
1161 case JDWP::JT_INT:
1162 return 4;
1163 case JDWP::JT_ARRAY:
1164 case JDWP::JT_OBJECT:
1165 case JDWP::JT_STRING:
1166 case JDWP::JT_THREAD:
1167 case JDWP::JT_THREAD_GROUP:
1168 case JDWP::JT_CLASS_LOADER:
1169 case JDWP::JT_CLASS_OBJECT:
1170 return sizeof(JDWP::ObjectId);
1171 case JDWP::JT_DOUBLE:
1172 case JDWP::JT_LONG:
1173 return 8;
1174 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08001175 LOG(FATAL) << "Unknown tag " << tag;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001176 return -1;
1177 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001178}
1179
Ian Rogersc0542af2014-09-03 16:16:56 -07001180JDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int32_t* length) {
1181 JDWP::JdwpError error;
1182 mirror::Array* a = DecodeNonNullArray(array_id, &error);
1183 if (a == nullptr) {
1184 return error;
Elliott Hughes24437992011-11-30 14:49:33 -08001185 }
Ian Rogersc0542af2014-09-03 16:16:56 -07001186 *length = a->GetLength();
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001187 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001188}
1189
Elliott Hughes88d63092013-01-09 09:55:54 -08001190JDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001191 JDWP::JdwpError error;
1192 mirror::Array* a = DecodeNonNullArray(array_id, &error);
Ian Rogers98379392014-02-24 16:53:16 -08001193 if (a == nullptr) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001194 return error;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001195 }
Elliott Hughes24437992011-11-30 14:49:33 -08001196
1197 if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
1198 LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001199 return JDWP::ERR_INVALID_LENGTH;
Elliott Hughes24437992011-11-30 14:49:33 -08001200 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001201 JDWP::JdwpTag element_tag = BasicTagFromClass(a->GetClass()->GetComponentType());
1202 expandBufAdd1(pReply, element_tag);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001203 expandBufAdd4BE(pReply, count);
1204
Ian Rogers1ff3c982014-08-12 02:30:58 -07001205 if (IsPrimitiveTag(element_tag)) {
1206 size_t width = GetTagWidth(element_tag);
Elliott Hughes24437992011-11-30 14:49:33 -08001207 uint8_t* dst = expandBufAddSpace(pReply, count * width);
1208 if (width == 8) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001209 const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t), 0));
Elliott Hughes24437992011-11-30 14:49:33 -08001210 for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
1211 } else if (width == 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001212 const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t), 0));
Elliott Hughes24437992011-11-30 14:49:33 -08001213 for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
1214 } else if (width == 2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001215 const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t), 0));
Elliott Hughes24437992011-11-30 14:49:33 -08001216 for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
1217 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001218 const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t), 0));
Elliott Hughes24437992011-11-30 14:49:33 -08001219 memcpy(dst, &src[offset * width], count * width);
1220 }
1221 } else {
Ian Rogers98379392014-02-24 16:53:16 -08001222 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001223 mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>();
Elliott Hughes24437992011-11-30 14:49:33 -08001224 for (int i = 0; i < count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001225 mirror::Object* element = oa->Get(offset + i);
Ian Rogers98379392014-02-24 16:53:16 -08001226 JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element)
Ian Rogers1ff3c982014-08-12 02:30:58 -07001227 : element_tag;
Elliott Hughes24437992011-11-30 14:49:33 -08001228 expandBufAdd1(pReply, specific_tag);
1229 expandBufAddObjectId(pReply, gRegistry->Add(element));
1230 }
1231 }
1232
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001233 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001234}
1235
Ian Rogersef7d42f2014-01-06 12:55:46 -08001236template <typename T>
Ian Rogersc0542af2014-09-03 16:16:56 -07001237static void CopyArrayData(mirror::Array* a, JDWP::Request* src, int offset, int count)
Ian Rogersef7d42f2014-01-06 12:55:46 -08001238 NO_THREAD_SAFETY_ANALYSIS {
1239 // TODO: fix when annotalysis correctly handles non-member functions.
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001240 DCHECK(a->GetClass()->IsPrimitiveArray());
1241
Ian Rogersef7d42f2014-01-06 12:55:46 -08001242 T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset));
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001243 for (int i = 0; i < count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001244 *dst++ = src->ReadValue(sizeof(T));
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001245 }
1246}
1247
Elliott Hughes88d63092013-01-09 09:55:54 -08001248JDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
Ian Rogersc0542af2014-09-03 16:16:56 -07001249 JDWP::Request* request) {
1250 JDWP::JdwpError error;
1251 mirror::Array* dst = DecodeNonNullArray(array_id, &error);
1252 if (dst == nullptr) {
1253 return error;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001254 }
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001255
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001256 if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001257 LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001258 return JDWP::ERR_INVALID_LENGTH;
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001259 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001260 JDWP::JdwpTag element_tag = BasicTagFromClass(dst->GetClass()->GetComponentType());
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001261
Ian Rogers1ff3c982014-08-12 02:30:58 -07001262 if (IsPrimitiveTag(element_tag)) {
1263 size_t width = GetTagWidth(element_tag);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001264 if (width == 8) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001265 CopyArrayData<uint64_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001266 } else if (width == 4) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001267 CopyArrayData<uint32_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001268 } else if (width == 2) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001269 CopyArrayData<uint16_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001270 } else {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001271 CopyArrayData<uint8_t>(dst, request, offset, count);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001272 }
1273 } else {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001274 mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>();
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001275 for (int i = 0; i < count; ++i) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001276 JDWP::ObjectId id = request->ReadObjectId();
Ian Rogersc0542af2014-09-03 16:16:56 -07001277 mirror::Object* o = gRegistry->Get<mirror::Object*>(id, &error);
1278 if (error != JDWP::ERR_NONE) {
1279 return error;
Elliott Hughes436e3722012-02-17 20:01:47 -08001280 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001281 oa->Set<false>(offset + i, o);
Elliott Hughesf03b8f62011-12-02 14:26:25 -08001282 }
1283 }
1284
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001285 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001286}
1287
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001288JDWP::JdwpError Dbg::CreateString(const std::string& str, JDWP::ObjectId* new_string_id) {
1289 Thread* self = Thread::Current();
1290 mirror::String* new_string = mirror::String::AllocFromModifiedUtf8(self, str.c_str());
1291 if (new_string == nullptr) {
1292 DCHECK(self->IsExceptionPending());
1293 self->ClearException();
1294 LOG(ERROR) << "Could not allocate string";
1295 *new_string_id = 0;
1296 return JDWP::ERR_OUT_OF_MEMORY;
1297 }
1298 *new_string_id = gRegistry->Add(new_string);
1299 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001300}
1301
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001302JDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId* new_object_id) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001303 JDWP::JdwpError error;
1304 mirror::Class* c = DecodeClass(class_id, &error);
1305 if (c == nullptr) {
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001306 *new_object_id = 0;
Ian Rogersc0542af2014-09-03 16:16:56 -07001307 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001308 }
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001309 Thread* self = Thread::Current();
1310 mirror::Object* new_object = c->AllocObject(self);
1311 if (new_object == nullptr) {
1312 DCHECK(self->IsExceptionPending());
1313 self->ClearException();
1314 LOG(ERROR) << "Could not allocate object of type " << PrettyDescriptor(c);
1315 *new_object_id = 0;
1316 return JDWP::ERR_OUT_OF_MEMORY;
1317 }
1318 *new_object_id = gRegistry->Add(new_object);
Elliott Hughes436e3722012-02-17 20:01:47 -08001319 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001320}
1321
Elliott Hughesbf13d362011-12-08 15:51:37 -08001322/*
1323 * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1324 */
Elliott Hughes88d63092013-01-09 09:55:54 -08001325JDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001326 JDWP::ObjectId* new_array_id) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001327 JDWP::JdwpError error;
1328 mirror::Class* c = DecodeClass(array_class_id, &error);
1329 if (c == nullptr) {
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001330 *new_array_id = 0;
Ian Rogersc0542af2014-09-03 16:16:56 -07001331 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001332 }
Sebastien Hertz2c3e77a2015-04-02 16:26:48 +02001333 Thread* self = Thread::Current();
1334 gc::Heap* heap = Runtime::Current()->GetHeap();
1335 mirror::Array* new_array = mirror::Array::Alloc<true>(self, c, length,
1336 c->GetComponentSizeShift(),
1337 heap->GetCurrentAllocator());
1338 if (new_array == nullptr) {
1339 DCHECK(self->IsExceptionPending());
1340 self->ClearException();
1341 LOG(ERROR) << "Could not allocate array of type " << PrettyDescriptor(c);
1342 *new_array_id = 0;
1343 return JDWP::ERR_OUT_OF_MEMORY;
1344 }
1345 *new_array_id = gRegistry->Add(new_array);
Elliott Hughes436e3722012-02-17 20:01:47 -08001346 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001347}
1348
Sebastien Hertz6995c602014-09-09 12:10:13 +02001349JDWP::FieldId Dbg::ToFieldId(const mirror::ArtField* f) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001350 CHECK(!kMovingFields);
Elliott Hughes03181a82011-11-17 17:22:21 -08001351 return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
Elliott Hughes03181a82011-11-17 17:22:21 -08001352}
1353
Brian Carlstromea46f952013-07-30 01:26:50 -07001354static JDWP::MethodId ToMethodId(const mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001356 CHECK(!kMovingMethods);
Elliott Hughes03181a82011-11-17 17:22:21 -08001357 return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m));
Elliott Hughes03181a82011-11-17 17:22:21 -08001358}
1359
Brian Carlstromea46f952013-07-30 01:26:50 -07001360static mirror::ArtField* FromFieldId(JDWP::FieldId fid)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001361 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001362 CHECK(!kMovingFields);
Brian Carlstromea46f952013-07-30 01:26:50 -07001363 return reinterpret_cast<mirror::ArtField*>(static_cast<uintptr_t>(fid));
Elliott Hughesaed4be92011-12-02 16:16:23 -08001364}
1365
Brian Carlstromea46f952013-07-30 01:26:50 -07001366static mirror::ArtMethod* FromMethodId(JDWP::MethodId mid)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001367 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001368 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -07001369 return reinterpret_cast<mirror::ArtMethod*>(static_cast<uintptr_t>(mid));
Elliott Hughes03181a82011-11-17 17:22:21 -08001370}
1371
Sebastien Hertz6995c602014-09-09 12:10:13 +02001372bool Dbg::MatchThread(JDWP::ObjectId expected_thread_id, Thread* event_thread) {
1373 CHECK(event_thread != nullptr);
1374 JDWP::JdwpError error;
1375 mirror::Object* expected_thread_peer = gRegistry->Get<mirror::Object*>(expected_thread_id,
1376 &error);
1377 return expected_thread_peer == event_thread->GetPeer();
1378}
1379
1380bool Dbg::MatchLocation(const JDWP::JdwpLocation& expected_location,
1381 const JDWP::EventLocation& event_location) {
1382 if (expected_location.dex_pc != event_location.dex_pc) {
1383 return false;
1384 }
1385 mirror::ArtMethod* m = FromMethodId(expected_location.method_id);
1386 return m == event_location.method;
1387}
1388
1389bool Dbg::MatchType(mirror::Class* event_class, JDWP::RefTypeId class_id) {
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001390 if (event_class == nullptr) {
1391 return false;
1392 }
Sebastien Hertz6995c602014-09-09 12:10:13 +02001393 JDWP::JdwpError error;
1394 mirror::Class* expected_class = DecodeClass(class_id, &error);
1395 CHECK(expected_class != nullptr);
1396 return expected_class->IsAssignableFrom(event_class);
1397}
1398
1399bool Dbg::MatchField(JDWP::RefTypeId expected_type_id, JDWP::FieldId expected_field_id,
1400 mirror::ArtField* event_field) {
1401 mirror::ArtField* expected_field = FromFieldId(expected_field_id);
1402 if (expected_field != event_field) {
1403 return false;
1404 }
1405 return Dbg::MatchType(event_field->GetDeclaringClass(), expected_type_id);
1406}
1407
1408bool Dbg::MatchInstance(JDWP::ObjectId expected_instance_id, mirror::Object* event_instance) {
1409 JDWP::JdwpError error;
1410 mirror::Object* modifier_instance = gRegistry->Get<mirror::Object*>(expected_instance_id, &error);
1411 return modifier_instance == event_instance;
1412}
1413
1414void Dbg::SetJdwpLocation(JDWP::JdwpLocation* location, mirror::ArtMethod* m, uint32_t dex_pc)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001415 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001416 if (m == nullptr) {
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001417 memset(location, 0, sizeof(*location));
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001418 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001419 mirror::Class* c = m->GetDeclaringClass();
Ian Rogersc0542af2014-09-03 16:16:56 -07001420 location->type_tag = GetTypeTag(c);
1421 location->class_id = gRegistry->AddRefType(c);
1422 location->method_id = ToMethodId(m);
1423 location->dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint64_t>(-1) : dex_pc;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08001424 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08001425}
1426
Ian Rogersc0542af2014-09-03 16:16:56 -07001427std::string Dbg::GetMethodName(JDWP::MethodId method_id) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001428 mirror::ArtMethod* m = FromMethodId(method_id);
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001429 if (m == nullptr) {
1430 return "NULL";
1431 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001432 return m->GetName();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001433}
1434
Ian Rogersc0542af2014-09-03 16:16:56 -07001435std::string Dbg::GetFieldName(JDWP::FieldId field_id) {
Sebastien Hertza9aa0ff2014-09-19 12:07:51 +02001436 mirror::ArtField* f = FromFieldId(field_id);
1437 if (f == nullptr) {
1438 return "NULL";
1439 }
1440 return f->GetName();
Elliott Hughesa96836a2013-01-17 12:27:49 -08001441}
1442
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001443/*
1444 * Augment the access flags for synthetic methods and fields by setting
1445 * the (as described by the spec) "0xf0000000 bit". Also, strip out any
1446 * flags not specified by the Java programming language.
1447 */
1448static uint32_t MangleAccessFlags(uint32_t accessFlags) {
1449 accessFlags &= kAccJavaFlagsMask;
1450 if ((accessFlags & kAccSynthetic) != 0) {
1451 accessFlags |= 0xf0000000;
1452 }
1453 return accessFlags;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001454}
1455
Elliott Hughesdbb40792011-11-18 17:05:22 -08001456/*
Jeff Haob7cefc72013-11-14 14:51:09 -08001457 * Circularly shifts registers so that arguments come first. Debuggers
1458 * expect slots to begin with arguments, but dex code places them at
1459 * the end.
Elliott Hughesdbb40792011-11-18 17:05:22 -08001460 */
Jeff Haob7cefc72013-11-14 14:51:09 -08001461static uint16_t MangleSlot(uint16_t slot, mirror::ArtMethod* m)
1462 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001463 const DexFile::CodeItem* code_item = m->GetCodeItem();
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001464 if (code_item == nullptr) {
1465 // We should not get here for a method without code (native, proxy or abstract). Log it and
1466 // return the slot as is since all registers are arguments.
1467 LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m);
1468 return slot;
1469 }
Jeff Haob7cefc72013-11-14 14:51:09 -08001470 uint16_t ins_size = code_item->ins_size_;
1471 uint16_t locals_size = code_item->registers_size_ - ins_size;
1472 if (slot >= locals_size) {
1473 return slot - locals_size;
1474 } else {
1475 return slot + ins_size;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001476 }
Elliott Hughesdbb40792011-11-18 17:05:22 -08001477}
1478
Jeff Haob7cefc72013-11-14 14:51:09 -08001479/*
1480 * Circularly shifts registers so that arguments come last. Reverts
1481 * slots to dex style argument placement.
1482 */
Sebastien Hertzabbabc82015-03-26 08:47:47 +01001483static uint16_t DemangleSlot(uint16_t slot, mirror::ArtMethod* m, JDWP::JdwpError* error)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001484 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001485 const DexFile::CodeItem* code_item = m->GetCodeItem();
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001486 if (code_item == nullptr) {
1487 // We should not get here for a method without code (native, proxy or abstract). Log it and
1488 // return the slot as is since all registers are arguments.
1489 LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m);
Sebastien Hertzabbabc82015-03-26 08:47:47 +01001490 uint16_t vreg_count = mirror::ArtMethod::NumArgRegisters(m->GetShorty());
1491 if (slot < vreg_count) {
1492 *error = JDWP::ERR_NONE;
1493 return slot;
1494 }
Jeff Haob7cefc72013-11-14 14:51:09 -08001495 } else {
Sebastien Hertzabbabc82015-03-26 08:47:47 +01001496 if (slot < code_item->registers_size_) {
1497 uint16_t ins_size = code_item->ins_size_;
1498 uint16_t locals_size = code_item->registers_size_ - ins_size;
1499 *error = JDWP::ERR_NONE;
1500 return (slot < ins_size) ? slot + locals_size : slot - ins_size;
1501 }
Elliott Hughesdbb40792011-11-18 17:05:22 -08001502 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01001503
1504 // Slot is invalid in the method.
1505 LOG(ERROR) << "Invalid local slot " << slot << " for method " << PrettyMethod(m);
1506 *error = JDWP::ERR_INVALID_SLOT;
1507 return DexFile::kDexNoIndex16;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001508}
1509
Elliott Hughes88d63092013-01-09 09:55:54 -08001510JDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001511 JDWP::JdwpError error;
1512 mirror::Class* c = DecodeClass(class_id, &error);
1513 if (c == nullptr) {
1514 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001515 }
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001516
1517 size_t instance_field_count = c->NumInstanceFields();
1518 size_t static_field_count = c->NumStaticFields();
1519
1520 expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1521
1522 for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001523 mirror::ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count);
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001524 expandBufAddFieldId(pReply, ToFieldId(f));
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001525 expandBufAddUtf8String(pReply, f->GetName());
1526 expandBufAddUtf8String(pReply, f->GetTypeDescriptor());
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001527 if (with_generic) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001528 static const char genericSignature[1] = "";
1529 expandBufAddUtf8String(pReply, genericSignature);
1530 }
1531 expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1532 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001533 return JDWP::ERR_NONE;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001534}
1535
Elliott Hughes88d63092013-01-09 09:55:54 -08001536JDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001537 JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001538 JDWP::JdwpError error;
1539 mirror::Class* c = DecodeClass(class_id, &error);
1540 if (c == nullptr) {
1541 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001542 }
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001543
1544 size_t direct_method_count = c->NumDirectMethods();
1545 size_t virtual_method_count = c->NumVirtualMethods();
1546
1547 expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
1548
1549 for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001550 mirror::ArtMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001551 expandBufAddMethodId(pReply, ToMethodId(m));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001552 expandBufAddUtf8String(pReply, m->GetName());
1553 expandBufAddUtf8String(pReply, m->GetSignature().ToString());
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001554 if (with_generic) {
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001555 static const char genericSignature[1] = "";
1556 expandBufAddUtf8String(pReply, genericSignature);
1557 }
1558 expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags()));
1559 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001560 return JDWP::ERR_NONE;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001561}
1562
Elliott Hughes88d63092013-01-09 09:55:54 -08001563JDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001564 JDWP::JdwpError error;
Mathieu Chartierf8322842014-05-16 10:59:25 -07001565 Thread* self = Thread::Current();
1566 StackHandleScope<1> hs(self);
Ian Rogersc0542af2014-09-03 16:16:56 -07001567 Handle<mirror::Class> c(hs.NewHandle(DecodeClass(class_id, &error)));
Mathieu Chartierf8322842014-05-16 10:59:25 -07001568 if (c.Get() == nullptr) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001569 return error;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001570 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001571 size_t interface_count = c->NumDirectInterfaces();
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001572 expandBufAdd4BE(pReply, interface_count);
1573 for (size_t i = 0; i < interface_count; ++i) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001574 expandBufAddRefTypeId(pReply,
1575 gRegistry->AddRefType(mirror::Class::GetDirectInterface(self, c, i)));
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001576 }
Elliott Hughes436e3722012-02-17 20:01:47 -08001577 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001578}
1579
Ian Rogersc0542af2014-09-03 16:16:56 -07001580void Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001581 struct DebugCallbackContext {
1582 int numItems;
1583 JDWP::ExpandBuf* pReply;
1584
Elliott Hughes2435a572012-02-17 16:07:41 -08001585 static bool Callback(void* context, uint32_t address, uint32_t line_number) {
Elliott Hughes03181a82011-11-17 17:22:21 -08001586 DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1587 expandBufAdd8BE(pContext->pReply, address);
Elliott Hughes2435a572012-02-17 16:07:41 -08001588 expandBufAdd4BE(pContext->pReply, line_number);
Elliott Hughes03181a82011-11-17 17:22:21 -08001589 pContext->numItems++;
Sebastien Hertzf2910ee2013-10-19 16:39:24 +02001590 return false;
Elliott Hughes03181a82011-11-17 17:22:21 -08001591 }
1592 };
Brian Carlstromea46f952013-07-30 01:26:50 -07001593 mirror::ArtMethod* m = FromMethodId(method_id);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001594 const DexFile::CodeItem* code_item = m->GetCodeItem();
Elliott Hughes03181a82011-11-17 17:22:21 -08001595 uint64_t start, end;
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001596 if (code_item == nullptr) {
1597 DCHECK(m->IsNative() || m->IsProxyMethod());
Elliott Hughes03181a82011-11-17 17:22:21 -08001598 start = -1;
1599 end = -1;
1600 } else {
1601 start = 0;
jeffhao14f0db92012-12-14 17:50:42 -08001602 // Return the index of the last instruction
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001603 end = code_item->insns_size_in_code_units_ - 1;
Elliott Hughes03181a82011-11-17 17:22:21 -08001604 }
1605
1606 expandBufAdd8BE(pReply, start);
1607 expandBufAdd8BE(pReply, end);
1608
1609 // Add numLines later
1610 size_t numLinesOffset = expandBufGetLength(pReply);
1611 expandBufAdd4BE(pReply, 0);
1612
1613 DebugCallbackContext context;
1614 context.numItems = 0;
1615 context.pReply = pReply;
1616
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001617 if (code_item != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001618 m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
Ian Rogersc0542af2014-09-03 16:16:56 -07001619 DebugCallbackContext::Callback, nullptr, &context);
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001620 }
Elliott Hughes03181a82011-11-17 17:22:21 -08001621
1622 JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001623}
1624
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001625void Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic,
1626 JDWP::ExpandBuf* pReply) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001627 struct DebugCallbackContext {
Jeff Haob7cefc72013-11-14 14:51:09 -08001628 mirror::ArtMethod* method;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001629 JDWP::ExpandBuf* pReply;
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001630 size_t variable_count;
1631 bool with_generic;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001632
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001633 static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress,
1634 const char* name, const char* descriptor, const char* signature)
Jeff Haob7cefc72013-11-14 14:51:09 -08001635 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001636 DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1637
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001638 VLOG(jdwp) << StringPrintf(" %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d",
1639 pContext->variable_count, startAddress, endAddress - startAddress,
1640 name, descriptor, signature, slot,
1641 MangleSlot(slot, pContext->method));
Elliott Hughesdbb40792011-11-18 17:05:22 -08001642
Jeff Haob7cefc72013-11-14 14:51:09 -08001643 slot = MangleSlot(slot, pContext->method);
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001644
Elliott Hughesdbb40792011-11-18 17:05:22 -08001645 expandBufAdd8BE(pContext->pReply, startAddress);
1646 expandBufAddUtf8String(pContext->pReply, name);
1647 expandBufAddUtf8String(pContext->pReply, descriptor);
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001648 if (pContext->with_generic) {
Elliott Hughesdbb40792011-11-18 17:05:22 -08001649 expandBufAddUtf8String(pContext->pReply, signature);
1650 }
1651 expandBufAdd4BE(pContext->pReply, endAddress - startAddress);
1652 expandBufAdd4BE(pContext->pReply, slot);
1653
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001654 ++pContext->variable_count;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001655 }
1656 };
Brian Carlstromea46f952013-07-30 01:26:50 -07001657 mirror::ArtMethod* m = FromMethodId(method_id);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001658
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001659 // arg_count considers doubles and longs to take 2 units.
1660 // variable_count considers everything to take 1 unit.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001661 std::string shorty(m->GetShorty());
Brian Carlstromea46f952013-07-30 01:26:50 -07001662 expandBufAdd4BE(pReply, mirror::ArtMethod::NumArgRegisters(shorty));
Elliott Hughesdbb40792011-11-18 17:05:22 -08001663
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001664 // We don't know the total number of variables yet, so leave a blank and update it later.
1665 size_t variable_count_offset = expandBufGetLength(pReply);
Elliott Hughesdbb40792011-11-18 17:05:22 -08001666 expandBufAdd4BE(pReply, 0);
1667
1668 DebugCallbackContext context;
Jeff Haob7cefc72013-11-14 14:51:09 -08001669 context.method = m;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001670 context.pReply = pReply;
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001671 context.variable_count = 0;
1672 context.with_generic = with_generic;
Elliott Hughesdbb40792011-11-18 17:05:22 -08001673
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001674 const DexFile::CodeItem* code_item = m->GetCodeItem();
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001675 if (code_item != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001676 m->GetDexFile()->DecodeDebugInfo(
Ian Rogersc0542af2014-09-03 16:16:56 -07001677 code_item, m->IsStatic(), m->GetDexMethodIndex(), nullptr, DebugCallbackContext::Callback,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001678 &context);
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +01001679 }
Elliott Hughesdbb40792011-11-18 17:05:22 -08001680
Elliott Hughesc5b734a2011-12-01 17:20:58 -08001681 JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001682}
1683
Jeff Hao579b0242013-11-18 13:16:49 -08001684void Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value,
1685 JDWP::ExpandBuf* pReply) {
1686 mirror::ArtMethod* m = FromMethodId(method_id);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001687 JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty());
Jeff Hao579b0242013-11-18 13:16:49 -08001688 OutputJValue(tag, return_value, pReply);
1689}
1690
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001691void Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value,
1692 JDWP::ExpandBuf* pReply) {
1693 mirror::ArtField* f = FromFieldId(field_id);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001694 JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001695 OutputJValue(tag, field_value, pReply);
1696}
1697
Elliott Hughes9777ba22013-01-17 09:04:19 -08001698JDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
Ian Rogersc0542af2014-09-03 16:16:56 -07001699 std::vector<uint8_t>* bytecodes) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001700 mirror::ArtMethod* m = FromMethodId(method_id);
Ian Rogersc0542af2014-09-03 16:16:56 -07001701 if (m == nullptr) {
Elliott Hughes9777ba22013-01-17 09:04:19 -08001702 return JDWP::ERR_INVALID_METHODID;
1703 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001704 const DexFile::CodeItem* code_item = m->GetCodeItem();
Elliott Hughes9777ba22013-01-17 09:04:19 -08001705 size_t byte_count = code_item->insns_size_in_code_units_ * 2;
1706 const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_);
1707 const uint8_t* end = begin + byte_count;
1708 for (const uint8_t* p = begin; p != end; ++p) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001709 bytecodes->push_back(*p);
Elliott Hughes9777ba22013-01-17 09:04:19 -08001710 }
1711 return JDWP::ERR_NONE;
1712}
1713
Elliott Hughes88d63092013-01-09 09:55:54 -08001714JDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001715 return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001716}
1717
Elliott Hughes88d63092013-01-09 09:55:54 -08001718JDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001719 return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001720}
1721
Elliott Hughes88d63092013-01-09 09:55:54 -08001722static JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
1723 JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001725 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001726 JDWP::JdwpError error;
1727 mirror::Class* c = DecodeClass(ref_type_id, &error);
1728 if (ref_type_id != 0 && c == nullptr) {
1729 return error;
Elliott Hughes0cf74332012-02-23 23:14:00 -08001730 }
1731
Sebastien Hertz6995c602014-09-09 12:10:13 +02001732 mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001733 if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001734 return JDWP::ERR_INVALID_OBJECT;
1735 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001736 mirror::ArtField* f = FromFieldId(field_id);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001737
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001738 mirror::Class* receiver_class = c;
Ian Rogersc0542af2014-09-03 16:16:56 -07001739 if (receiver_class == nullptr && o != nullptr) {
Elliott Hughes0cf74332012-02-23 23:14:00 -08001740 receiver_class = o->GetClass();
1741 }
Ian Rogersc0542af2014-09-03 16:16:56 -07001742 // TODO: should we give up now if receiver_class is nullptr?
1743 if (receiver_class != nullptr && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
Elliott Hughes0cf74332012-02-23 23:14:00 -08001744 LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001745 return JDWP::ERR_INVALID_FIELDID;
1746 }
Elliott Hughesaed4be92011-12-02 16:16:23 -08001747
Elliott Hughes0cf74332012-02-23 23:14:00 -08001748 // The RI only enforces the static/non-static mismatch in one direction.
1749 // TODO: should we change the tests and check both?
1750 if (is_static) {
1751 if (!f->IsStatic()) {
1752 return JDWP::ERR_INVALID_FIELDID;
1753 }
1754 } else {
1755 if (f->IsStatic()) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001756 LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues on static field "
1757 << PrettyField(f);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001758 }
1759 }
jeffhao0dfbb7e2012-11-28 15:26:03 -08001760 if (f->IsStatic()) {
1761 o = f->GetDeclaringClass();
1762 }
Elliott Hughes0cf74332012-02-23 23:14:00 -08001763
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001764 JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
Jeff Hao579b0242013-11-18 13:16:49 -08001765 JValue field_value;
1766 if (tag == JDWP::JT_VOID) {
1767 LOG(FATAL) << "Unknown tag: " << tag;
1768 } else if (!IsPrimitiveTag(tag)) {
1769 field_value.SetL(f->GetObject(o));
1770 } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1771 field_value.SetJ(f->Get64(o));
Elliott Hughesaed4be92011-12-02 16:16:23 -08001772 } else {
Jeff Hao579b0242013-11-18 13:16:49 -08001773 field_value.SetI(f->Get32(o));
Elliott Hughesaed4be92011-12-02 16:16:23 -08001774 }
Jeff Hao579b0242013-11-18 13:16:49 -08001775 Dbg::OutputJValue(tag, &field_value, pReply);
1776
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001777 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001778}
1779
Elliott Hughes88d63092013-01-09 09:55:54 -08001780JDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001781 JDWP::ExpandBuf* pReply) {
Elliott Hughes88d63092013-01-09 09:55:54 -08001782 return GetFieldValueImpl(0, object_id, field_id, pReply, false);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001783}
1784
Ian Rogersc0542af2014-09-03 16:16:56 -07001785JDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id,
1786 JDWP::ExpandBuf* pReply) {
Elliott Hughes88d63092013-01-09 09:55:54 -08001787 return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001788}
1789
Elliott Hughes88d63092013-01-09 09:55:54 -08001790static JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001791 uint64_t value, int width, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001792 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001793 JDWP::JdwpError error;
Sebastien Hertz6995c602014-09-09 12:10:13 +02001794 mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001795 if ((!is_static && o == nullptr) || error != JDWP::ERR_NONE) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001796 return JDWP::ERR_INVALID_OBJECT;
1797 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001798 mirror::ArtField* f = FromFieldId(field_id);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001799
1800 // The RI only enforces the static/non-static mismatch in one direction.
1801 // TODO: should we change the tests and check both?
1802 if (is_static) {
1803 if (!f->IsStatic()) {
1804 return JDWP::ERR_INVALID_FIELDID;
1805 }
1806 } else {
1807 if (f->IsStatic()) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001808 LOG(WARNING) << "Ignoring non-nullptr receiver for ObjectReference.SetValues on static field " << PrettyField(f);
Elliott Hughes0cf74332012-02-23 23:14:00 -08001809 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001810 }
jeffhao0dfbb7e2012-11-28 15:26:03 -08001811 if (f->IsStatic()) {
1812 o = f->GetDeclaringClass();
1813 }
Elliott Hughesaed4be92011-12-02 16:16:23 -08001814
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001815 JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
Elliott Hughesaed4be92011-12-02 16:16:23 -08001816
1817 if (IsPrimitiveTag(tag)) {
1818 if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001819 CHECK_EQ(width, 8);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001820 // Debugging can't use transactional mode (runtime only).
1821 f->Set64<false>(o, value);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001822 } else {
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001823 CHECK_LE(width, 4);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001824 // Debugging can't use transactional mode (runtime only).
1825 f->Set32<false>(o, value);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001826 }
1827 } else {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001828 mirror::Object* v = Dbg::GetObjectRegistry()->Get<mirror::Object*>(value, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07001829 if (error != JDWP::ERR_NONE) {
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001830 return JDWP::ERR_INVALID_OBJECT;
1831 }
Ian Rogersc0542af2014-09-03 16:16:56 -07001832 if (v != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001833 mirror::Class* field_type;
1834 {
1835 StackHandleScope<3> hs(Thread::Current());
1836 HandleWrapper<mirror::Object> h_v(hs.NewHandleWrapper(&v));
1837 HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f));
1838 HandleWrapper<mirror::Object> h_o(hs.NewHandleWrapper(&o));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001839 field_type = h_f->GetType<true>();
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001840 }
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08001841 if (!field_type->IsAssignableFrom(v->GetClass())) {
1842 return JDWP::ERR_INVALID_OBJECT;
1843 }
1844 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001845 // Debugging can't use transactional mode (runtime only).
1846 f->SetObject<false>(o, v);
Elliott Hughesaed4be92011-12-02 16:16:23 -08001847 }
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -08001848
1849 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001850}
1851
Elliott Hughes88d63092013-01-09 09:55:54 -08001852JDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001853 int width) {
Elliott Hughes88d63092013-01-09 09:55:54 -08001854 return SetFieldValueImpl(object_id, field_id, value, width, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001855}
1856
Elliott Hughes88d63092013-01-09 09:55:54 -08001857JDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
1858 return SetFieldValueImpl(0, field_id, value, width, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001859}
1860
Sebastien Hertzb0b0b492014-09-15 11:27:27 +02001861JDWP::JdwpError Dbg::StringToUtf8(JDWP::ObjectId string_id, std::string* str) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001862 JDWP::JdwpError error;
Sebastien Hertzb0b0b492014-09-15 11:27:27 +02001863 mirror::Object* obj = gRegistry->Get<mirror::Object*>(string_id, &error);
1864 if (error != JDWP::ERR_NONE) {
1865 return error;
1866 }
1867 if (obj == nullptr) {
1868 return JDWP::ERR_INVALID_OBJECT;
1869 }
1870 {
1871 ScopedObjectAccessUnchecked soa(Thread::Current());
1872 mirror::Class* java_lang_String = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_String);
1873 if (!java_lang_String->IsAssignableFrom(obj->GetClass())) {
1874 // This isn't a string.
1875 return JDWP::ERR_INVALID_STRING;
1876 }
1877 }
1878 *str = obj->AsString()->ToModifiedUtf8();
1879 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001880}
1881
Jeff Hao579b0242013-11-18 13:16:49 -08001882void Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) {
1883 if (IsPrimitiveTag(tag)) {
1884 expandBufAdd1(pReply, tag);
1885 if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
1886 expandBufAdd1(pReply, return_value->GetI());
1887 } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
1888 expandBufAdd2BE(pReply, return_value->GetI());
1889 } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
1890 expandBufAdd4BE(pReply, return_value->GetI());
1891 } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1892 expandBufAdd8BE(pReply, return_value->GetJ());
1893 } else {
1894 CHECK_EQ(tag, JDWP::JT_VOID);
1895 }
1896 } else {
Ian Rogers98379392014-02-24 16:53:16 -08001897 ScopedObjectAccessUnchecked soa(Thread::Current());
Jeff Hao579b0242013-11-18 13:16:49 -08001898 mirror::Object* value = return_value->GetL();
Ian Rogers98379392014-02-24 16:53:16 -08001899 expandBufAdd1(pReply, TagFromObject(soa, value));
Jeff Hao579b0242013-11-18 13:16:49 -08001900 expandBufAddObjectId(pReply, gRegistry->Add(value));
1901 }
1902}
1903
Ian Rogersc0542af2014-09-03 16:16:56 -07001904JDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string* name) {
jeffhaoa77f0f62012-12-05 17:19:31 -08001905 ScopedObjectAccessUnchecked soa(Thread::Current());
1906 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07001907 JDWP::JdwpError error;
1908 Thread* thread = DecodeThread(soa, thread_id, &error);
1909 UNUSED(thread);
Elliott Hughes221229c2013-01-08 18:17:50 -08001910 if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
1911 return error;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08001912 }
Elliott Hughes221229c2013-01-08 18:17:50 -08001913
1914 // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
Ian Rogersc0542af2014-09-03 16:16:56 -07001915 mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error);
1916 CHECK(thread_object != nullptr) << error;
Brian Carlstromea46f952013-07-30 01:26:50 -07001917 mirror::ArtField* java_lang_Thread_name_field =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001918 soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
1919 mirror::String* s =
1920 reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object));
Ian Rogersc0542af2014-09-03 16:16:56 -07001921 if (s != nullptr) {
1922 *name = s->ToModifiedUtf8();
Elliott Hughes221229c2013-01-08 18:17:50 -08001923 }
1924 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001925}
1926
Elliott Hughes221229c2013-01-08 18:17:50 -08001927JDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
Sebastien Hertza06430c2014-09-15 19:21:30 +02001928 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07001929 JDWP::JdwpError error;
1930 mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id, &error);
1931 if (error != JDWP::ERR_NONE) {
Elliott Hughes2435a572012-02-17 16:07:41 -08001932 return JDWP::ERR_INVALID_OBJECT;
1933 }
Mathieu Chartier2d5f39e2014-09-19 17:52:37 -07001934 ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroup");
Elliott Hughes2435a572012-02-17 16:07:41 -08001935 // Okay, so it's an object, but is it actually a thread?
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07001936 {
1937 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07001938 Thread* thread = DecodeThread(soa, thread_id, &error);
1939 UNUSED(thread);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07001940 }
Elliott Hughes221229c2013-01-08 18:17:50 -08001941 if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1942 // Zombie threads are in the null group.
1943 expandBufAddObjectId(pReply, JDWP::ObjectId(0));
Sebastien Hertz52d131d2014-03-13 16:17:40 +01001944 error = JDWP::ERR_NONE;
1945 } else if (error == JDWP::ERR_NONE) {
1946 mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
1947 CHECK(c != nullptr);
Sebastien Hertze49e1952014-10-13 11:27:13 +02001948 mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_group);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07001949 CHECK(f != nullptr);
Sebastien Hertz52d131d2014-03-13 16:17:40 +01001950 mirror::Object* group = f->GetObject(thread_object);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07001951 CHECK(group != nullptr);
Sebastien Hertz52d131d2014-03-13 16:17:40 +01001952 JDWP::ObjectId thread_group_id = gRegistry->Add(group);
1953 expandBufAddObjectId(pReply, thread_group_id);
Elliott Hughes221229c2013-01-08 18:17:50 -08001954 }
Sebastien Hertz52d131d2014-03-13 16:17:40 +01001955 return error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001956}
1957
Sebastien Hertza06430c2014-09-15 19:21:30 +02001958static mirror::Object* DecodeThreadGroup(ScopedObjectAccessUnchecked& soa,
1959 JDWP::ObjectId thread_group_id, JDWP::JdwpError* error)
1960 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02001961 mirror::Object* thread_group = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_group_id,
1962 error);
Sebastien Hertza06430c2014-09-15 19:21:30 +02001963 if (*error != JDWP::ERR_NONE) {
1964 return nullptr;
1965 }
1966 if (thread_group == nullptr) {
1967 *error = JDWP::ERR_INVALID_OBJECT;
1968 return nullptr;
1969 }
Ian Rogers98379392014-02-24 16:53:16 -08001970 mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
1971 CHECK(c != nullptr);
Sebastien Hertza06430c2014-09-15 19:21:30 +02001972 if (!c->IsAssignableFrom(thread_group->GetClass())) {
1973 // This is not a java.lang.ThreadGroup.
1974 *error = JDWP::ERR_INVALID_THREAD_GROUP;
1975 return nullptr;
1976 }
1977 *error = JDWP::ERR_NONE;
1978 return thread_group;
1979}
1980
1981JDWP::JdwpError Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) {
1982 ScopedObjectAccessUnchecked soa(Thread::Current());
1983 JDWP::JdwpError error;
1984 mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
1985 if (error != JDWP::ERR_NONE) {
1986 return error;
1987 }
Mathieu Chartier2d5f39e2014-09-19 17:52:37 -07001988 ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupName");
Sebastien Hertze49e1952014-10-13 11:27:13 +02001989 mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name);
Ian Rogersc0542af2014-09-03 16:16:56 -07001990 CHECK(f != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001991 mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group));
Sebastien Hertza06430c2014-09-15 19:21:30 +02001992
1993 std::string thread_group_name(s->ToModifiedUtf8());
1994 expandBufAddUtf8String(pReply, thread_group_name);
1995 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001996}
1997
Sebastien Hertza06430c2014-09-15 19:21:30 +02001998JDWP::JdwpError Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) {
Ian Rogers98379392014-02-24 16:53:16 -08001999 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07002000 JDWP::JdwpError error;
Sebastien Hertza06430c2014-09-15 19:21:30 +02002001 mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
2002 if (error != JDWP::ERR_NONE) {
2003 return error;
2004 }
Mathieu Chartier2d5f39e2014-09-19 17:52:37 -07002005 mirror::Object* parent;
2006 {
2007 ScopedAssertNoThreadSuspension ants(soa.Self(), "Debugger: GetThreadGroupParent");
Sebastien Hertze49e1952014-10-13 11:27:13 +02002008 mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_parent);
Mathieu Chartier2d5f39e2014-09-19 17:52:37 -07002009 CHECK(f != nullptr);
2010 parent = f->GetObject(thread_group);
2011 }
Sebastien Hertza06430c2014-09-15 19:21:30 +02002012 JDWP::ObjectId parent_group_id = gRegistry->Add(parent);
2013 expandBufAddObjectId(pReply, parent_group_id);
2014 return JDWP::ERR_NONE;
2015}
2016
2017static void GetChildThreadGroups(ScopedObjectAccessUnchecked& soa, mirror::Object* thread_group,
2018 std::vector<JDWP::ObjectId>* child_thread_group_ids)
2019 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2020 CHECK(thread_group != nullptr);
2021
2022 // Get the ArrayList<ThreadGroup> "groups" out of this thread group...
Sebastien Hertze49e1952014-10-13 11:27:13 +02002023 mirror::ArtField* groups_field = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_groups);
Sebastien Hertza06430c2014-09-15 19:21:30 +02002024 mirror::Object* groups_array_list = groups_field->GetObject(thread_group);
Sebastien Hertze49e1952014-10-13 11:27:13 +02002025 {
2026 // The "groups" field is declared as a java.util.List: check it really is
2027 // an instance of java.util.ArrayList.
2028 CHECK(groups_array_list != nullptr);
2029 mirror::Class* java_util_ArrayList_class =
2030 soa.Decode<mirror::Class*>(WellKnownClasses::java_util_ArrayList);
2031 CHECK(groups_array_list->InstanceOf(java_util_ArrayList_class));
2032 }
Sebastien Hertza06430c2014-09-15 19:21:30 +02002033
2034 // Get the array and size out of the ArrayList<ThreadGroup>...
Sebastien Hertze49e1952014-10-13 11:27:13 +02002035 mirror::ArtField* array_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_array);
2036 mirror::ArtField* size_field = soa.DecodeField(WellKnownClasses::java_util_ArrayList_size);
Sebastien Hertza06430c2014-09-15 19:21:30 +02002037 mirror::ObjectArray<mirror::Object>* groups_array =
2038 array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>();
2039 const int32_t size = size_field->GetInt(groups_array_list);
2040
2041 // Copy the first 'size' elements out of the array into the result.
Sebastien Hertz6995c602014-09-09 12:10:13 +02002042 ObjectRegistry* registry = Dbg::GetObjectRegistry();
Sebastien Hertza06430c2014-09-15 19:21:30 +02002043 for (int32_t i = 0; i < size; ++i) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02002044 child_thread_group_ids->push_back(registry->Add(groups_array->Get(i)));
Sebastien Hertza06430c2014-09-15 19:21:30 +02002045 }
2046}
2047
2048JDWP::JdwpError Dbg::GetThreadGroupChildren(JDWP::ObjectId thread_group_id,
2049 JDWP::ExpandBuf* pReply) {
2050 ScopedObjectAccessUnchecked soa(Thread::Current());
2051 JDWP::JdwpError error;
2052 mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
2053 if (error != JDWP::ERR_NONE) {
2054 return error;
2055 }
2056
2057 // Add child threads.
2058 {
2059 std::vector<JDWP::ObjectId> child_thread_ids;
2060 GetThreads(thread_group, &child_thread_ids);
2061 expandBufAdd4BE(pReply, child_thread_ids.size());
2062 for (JDWP::ObjectId child_thread_id : child_thread_ids) {
2063 expandBufAddObjectId(pReply, child_thread_id);
2064 }
2065 }
2066
2067 // Add child thread groups.
2068 {
2069 std::vector<JDWP::ObjectId> child_thread_groups_ids;
2070 GetChildThreadGroups(soa, thread_group, &child_thread_groups_ids);
2071 expandBufAdd4BE(pReply, child_thread_groups_ids.size());
2072 for (JDWP::ObjectId child_thread_group_id : child_thread_groups_ids) {
2073 expandBufAddObjectId(pReply, child_thread_group_id);
2074 }
2075 }
2076
2077 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002078}
2079
2080JDWP::ObjectId Dbg::GetSystemThreadGroupId() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002081 ScopedObjectAccessUnchecked soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -07002082 mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002083 mirror::Object* group = f->GetObject(f->GetDeclaringClass());
Ian Rogers365c1022012-06-22 15:05:28 -07002084 return gRegistry->Add(group);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002085}
2086
Jeff Hao920af3e2013-08-28 15:46:38 -07002087JDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) {
2088 switch (state) {
2089 case kBlocked:
2090 return JDWP::TS_MONITOR;
2091 case kNative:
2092 case kRunnable:
2093 case kSuspended:
2094 return JDWP::TS_RUNNING;
2095 case kSleeping:
2096 return JDWP::TS_SLEEPING;
2097 case kStarting:
2098 case kTerminated:
2099 return JDWP::TS_ZOMBIE;
2100 case kTimedWaiting:
Sebastien Hertzbae182c2013-12-17 10:42:03 +01002101 case kWaitingForCheckPointsToRun:
Jeff Hao920af3e2013-08-28 15:46:38 -07002102 case kWaitingForDebuggerSend:
2103 case kWaitingForDebuggerSuspension:
2104 case kWaitingForDebuggerToAttach:
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01002105 case kWaitingForDeoptimization:
Jeff Hao920af3e2013-08-28 15:46:38 -07002106 case kWaitingForGcToComplete:
Jeff Hao920af3e2013-08-28 15:46:38 -07002107 case kWaitingForJniOnLoad:
Sebastien Hertzbae182c2013-12-17 10:42:03 +01002108 case kWaitingForMethodTracingStart:
Jeff Hao920af3e2013-08-28 15:46:38 -07002109 case kWaitingForSignalCatcherOutput:
Hiroshi Yamauchi0c8c3032015-01-16 16:54:35 -08002110 case kWaitingForVisitObjects:
Jeff Hao920af3e2013-08-28 15:46:38 -07002111 case kWaitingInMainDebuggerLoop:
2112 case kWaitingInMainSignalCatcherLoop:
2113 case kWaitingPerformingGc:
2114 case kWaiting:
2115 return JDWP::TS_WAIT;
2116 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
2117 }
2118 LOG(FATAL) << "Unknown thread state: " << state;
2119 return JDWP::TS_ZOMBIE;
2120}
2121
Sebastien Hertz52d131d2014-03-13 16:17:40 +01002122JDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus,
2123 JDWP::JdwpSuspendStatus* pSuspendStatus) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002124 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes499c5132011-11-17 14:55:11 -08002125
Elliott Hughes9e0c1752013-01-09 14:02:58 -08002126 *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
2127
Ian Rogers50b35e22012-10-04 10:09:15 -07002128 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07002129 JDWP::JdwpError error;
2130 Thread* thread = DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08002131 if (error != JDWP::ERR_NONE) {
2132 if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
2133 *pThreadStatus = JDWP::TS_ZOMBIE;
Elliott Hughes221229c2013-01-08 18:17:50 -08002134 return JDWP::ERR_NONE;
2135 }
2136 return error;
Elliott Hughes499c5132011-11-17 14:55:11 -08002137 }
2138
Elliott Hughes9e0c1752013-01-09 14:02:58 -08002139 if (IsSuspendedForDebugger(soa, thread)) {
2140 *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
Elliott Hughes499c5132011-11-17 14:55:11 -08002141 }
2142
Jeff Hao920af3e2013-08-28 15:46:38 -07002143 *pThreadStatus = ToJdwpThreadStatus(thread->GetState());
Elliott Hughes221229c2013-01-08 18:17:50 -08002144 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002145}
2146
Elliott Hughes221229c2013-01-08 18:17:50 -08002147JDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002148 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -07002149 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07002150 JDWP::JdwpError error;
2151 Thread* thread = DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08002152 if (error != JDWP::ERR_NONE) {
2153 return error;
Elliott Hughes2435a572012-02-17 16:07:41 -08002154 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002155 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002156 expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
Elliott Hughes2435a572012-02-17 16:07:41 -08002157 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002158}
2159
Elliott Hughesf9501702013-01-11 11:22:27 -08002160JDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
2161 ScopedObjectAccess soa(Thread::Current());
2162 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07002163 JDWP::JdwpError error;
2164 Thread* thread = DecodeThread(soa, thread_id, &error);
Elliott Hughesf9501702013-01-11 11:22:27 -08002165 if (error != JDWP::ERR_NONE) {
2166 return error;
2167 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07002168 thread->Interrupt(soa.Self());
Elliott Hughesf9501702013-01-11 11:22:27 -08002169 return JDWP::ERR_NONE;
2170}
2171
Sebastien Hertz070f7322014-09-09 12:08:49 +02002172static bool IsInDesiredThreadGroup(ScopedObjectAccessUnchecked& soa,
2173 mirror::Object* desired_thread_group, mirror::Object* peer)
2174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2175 // Do we want threads from all thread groups?
2176 if (desired_thread_group == nullptr) {
2177 return true;
2178 }
2179 mirror::ArtField* thread_group_field = soa.DecodeField(WellKnownClasses::java_lang_Thread_group);
2180 DCHECK(thread_group_field != nullptr);
2181 mirror::Object* group = thread_group_field->GetObject(peer);
2182 return (group == desired_thread_group);
2183}
2184
Sebastien Hertza06430c2014-09-15 19:21:30 +02002185void Dbg::GetThreads(mirror::Object* thread_group, std::vector<JDWP::ObjectId>* thread_ids) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002186 ScopedObjectAccessUnchecked soa(Thread::Current());
Sebastien Hertz070f7322014-09-09 12:08:49 +02002187 std::list<Thread*> all_threads_list;
2188 {
2189 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
2190 all_threads_list = Runtime::Current()->GetThreadList()->GetList();
2191 }
2192 for (Thread* t : all_threads_list) {
2193 if (t == Dbg::GetDebugThread()) {
2194 // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
2195 // query all threads, so it's easier if we just don't tell them about this thread.
2196 continue;
2197 }
2198 if (t->IsStillStarting()) {
2199 // This thread is being started (and has been registered in the thread list). However, it is
2200 // not completely started yet so we must ignore it.
2201 continue;
2202 }
2203 mirror::Object* peer = t->GetPeer();
2204 if (peer == nullptr) {
2205 // peer might be NULL if the thread is still starting up. We can't tell the debugger about
2206 // this thread yet.
2207 // TODO: if we identified threads to the debugger by their Thread*
2208 // rather than their peer's mirror::Object*, we could fix this.
2209 // Doing so might help us report ZOMBIE threads too.
2210 continue;
2211 }
2212 if (IsInDesiredThreadGroup(soa, thread_group, peer)) {
2213 thread_ids->push_back(gRegistry->Add(peer));
2214 }
2215 }
Elliott Hughescaf76542012-06-28 16:08:22 -07002216}
Elliott Hughesa2155262011-11-16 16:26:58 -08002217
Ian Rogersc0542af2014-09-03 16:16:56 -07002218static int GetStackDepth(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -07002219 struct CountStackDepthVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002220 explicit CountStackDepthVisitor(Thread* thread_in)
2221 : StackVisitor(thread_in, nullptr), depth(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -07002222
Elliott Hughes64f574f2013-02-20 14:57:12 -08002223 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2224 // annotalysis.
2225 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers0399dde2012-06-06 17:09:28 -07002226 if (!GetMethod()->IsRuntimeMethod()) {
Elliott Hughesf8a2df72011-12-01 12:19:54 -08002227 ++depth;
2228 }
Elliott Hughes530fa002012-03-12 11:44:49 -07002229 return true;
Elliott Hughesa2e54f62011-11-17 13:01:30 -08002230 }
2231 size_t depth;
2232 };
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002233
Ian Rogers7a22fa62013-01-23 12:16:16 -08002234 CountStackDepthVisitor visitor(thread);
Ian Rogers0399dde2012-06-06 17:09:28 -07002235 visitor.WalkStack();
Elliott Hughesa2e54f62011-11-17 13:01:30 -08002236 return visitor.depth;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002237}
2238
Ian Rogersc0542af2014-09-03 16:16:56 -07002239JDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t* result) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002240 ScopedObjectAccess soa(Thread::Current());
jeffhaoa77f0f62012-12-05 17:19:31 -08002241 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07002242 JDWP::JdwpError error;
2243 *result = 0;
2244 Thread* thread = DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08002245 if (error != JDWP::ERR_NONE) {
2246 return error;
2247 }
Elliott Hughesf15f4a02013-01-09 10:09:38 -08002248 if (!IsSuspendedForDebugger(soa, thread)) {
2249 return JDWP::ERR_THREAD_NOT_SUSPENDED;
2250 }
Ian Rogersc0542af2014-09-03 16:16:56 -07002251 *result = GetStackDepth(thread);
Elliott Hughes221229c2013-01-08 18:17:50 -08002252 return JDWP::ERR_NONE;
Elliott Hughes86964332012-02-15 19:37:42 -08002253}
2254
Ian Rogers306057f2012-11-26 12:45:53 -08002255JDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame,
2256 size_t frame_count, JDWP::ExpandBuf* buf) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002257 class GetFrameVisitor : public StackVisitor {
2258 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002259 GetFrameVisitor(Thread* thread, size_t start_frame_in, size_t frame_count_in,
2260 JDWP::ExpandBuf* buf_in)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersc0542af2014-09-03 16:16:56 -07002262 : StackVisitor(thread, nullptr), depth_(0),
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002263 start_frame_(start_frame_in), frame_count_(frame_count_in), buf_(buf_in) {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002264 expandBufAdd4BE(buf_, frame_count_);
Elliott Hughes03181a82011-11-17 17:22:21 -08002265 }
Ian Rogers0399dde2012-06-06 17:09:28 -07002266
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002267 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2268 // annotalysis.
2269 virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers0399dde2012-06-06 17:09:28 -07002270 if (GetMethod()->IsRuntimeMethod()) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002271 return true; // The debugger can't do anything useful with a frame that has no Method*.
Elliott Hughes03181a82011-11-17 17:22:21 -08002272 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002273 if (depth_ >= start_frame_ + frame_count_) {
Elliott Hughes530fa002012-03-12 11:44:49 -07002274 return false;
Elliott Hughes03181a82011-11-17 17:22:21 -08002275 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002276 if (depth_ >= start_frame_) {
2277 JDWP::FrameId frame_id(GetFrameId());
2278 JDWP::JdwpLocation location;
Sebastien Hertz6995c602014-09-09 12:10:13 +02002279 SetJdwpLocation(&location, GetMethod(), GetDexPc());
Ian Rogersef7d42f2014-01-06 12:55:46 -08002280 VLOG(jdwp) << StringPrintf(" Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002281 expandBufAdd8BE(buf_, frame_id);
2282 expandBufAddLocation(buf_, location);
2283 }
2284 ++depth_;
Elliott Hughes530fa002012-03-12 11:44:49 -07002285 return true;
Elliott Hughes03181a82011-11-17 17:22:21 -08002286 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002287
2288 private:
2289 size_t depth_;
2290 const size_t start_frame_;
2291 const size_t frame_count_;
2292 JDWP::ExpandBuf* buf_;
Elliott Hughes03181a82011-11-17 17:22:21 -08002293 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002294
2295 ScopedObjectAccessUnchecked soa(Thread::Current());
jeffhaoa77f0f62012-12-05 17:19:31 -08002296 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
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 return error;
2301 }
Elliott Hughesf15f4a02013-01-09 10:09:38 -08002302 if (!IsSuspendedForDebugger(soa, thread)) {
2303 return JDWP::ERR_THREAD_NOT_SUSPENDED;
2304 }
Ian Rogers7a22fa62013-01-23 12:16:16 -08002305 GetFrameVisitor visitor(thread, start_frame, frame_count, buf);
Ian Rogers0399dde2012-06-06 17:09:28 -07002306 visitor.WalkStack();
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002307 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002308}
2309
2310JDWP::ObjectId Dbg::GetThreadSelfId() {
Sebastien Hertz6995c602014-09-09 12:10:13 +02002311 return GetThreadId(Thread::Current());
2312}
2313
2314JDWP::ObjectId Dbg::GetThreadId(Thread* thread) {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -07002315 ScopedObjectAccessUnchecked soa(Thread::Current());
Sebastien Hertz6995c602014-09-09 12:10:13 +02002316 return gRegistry->Add(thread->GetPeer());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002317}
2318
Elliott Hughes475fc232011-10-25 15:00:35 -07002319void Dbg::SuspendVM() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002320 Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002321}
2322
2323void Dbg::ResumeVM() {
Sebastien Hertz253fa552014-10-14 17:27:15 +02002324 Runtime::Current()->GetThreadList()->ResumeAllForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002325}
2326
Elliott Hughes221229c2013-01-08 18:17:50 -08002327JDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
Ian Rogersf3d874c2014-07-17 18:52:42 -07002328 Thread* self = Thread::Current();
Ian Rogersc0542af2014-09-03 16:16:56 -07002329 ScopedLocalRef<jobject> peer(self->GetJniEnv(), nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002330 {
Ian Rogersf3d874c2014-07-17 18:52:42 -07002331 ScopedObjectAccess soa(self);
Ian Rogersc0542af2014-09-03 16:16:56 -07002332 JDWP::JdwpError error;
2333 peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id, &error)));
Elliott Hughes4e235312011-12-02 11:34:15 -08002334 }
Ian Rogersc0542af2014-09-03 16:16:56 -07002335 if (peer.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002336 return JDWP::ERR_THREAD_NOT_ALIVE;
2337 }
Ian Rogers4ad5cd32014-11-11 23:08:07 -08002338 // Suspend thread to build stack trace.
Elliott Hughesf327e072013-01-09 16:01:26 -08002339 bool timed_out;
Brian Carlstromba32de42014-08-27 23:43:46 -07002340 ThreadList* thread_list = Runtime::Current()->GetThreadList();
2341 Thread* thread = thread_list->SuspendThreadByPeer(peer.get(), request_suspension, true,
2342 &timed_out);
Ian Rogersc0542af2014-09-03 16:16:56 -07002343 if (thread != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002344 return JDWP::ERR_NONE;
Elliott Hughesf327e072013-01-09 16:01:26 -08002345 } else if (timed_out) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002346 return JDWP::ERR_INTERNAL;
2347 } else {
2348 return JDWP::ERR_THREAD_NOT_ALIVE;
2349 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002350}
2351
Elliott Hughes221229c2013-01-08 18:17:50 -08002352void Dbg::ResumeThread(JDWP::ObjectId thread_id) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002353 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogersc0542af2014-09-03 16:16:56 -07002354 JDWP::JdwpError error;
2355 mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id, &error);
2356 CHECK(peer != nullptr) << error;
jeffhaoa77f0f62012-12-05 17:19:31 -08002357 Thread* thread;
2358 {
2359 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2360 thread = Thread::FromManagedThread(soa, peer);
2361 }
Ian Rogersc0542af2014-09-03 16:16:56 -07002362 if (thread == nullptr) {
Elliott Hughes4e235312011-12-02 11:34:15 -08002363 LOG(WARNING) << "No such thread for resume: " << peer;
2364 return;
2365 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002366 bool needs_resume;
2367 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002368 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002369 needs_resume = thread->GetSuspendCount() > 0;
2370 }
2371 if (needs_resume) {
Elliott Hughes546b9862012-06-20 16:06:13 -07002372 Runtime::Current()->GetThreadList()->Resume(thread, true);
2373 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002374}
2375
2376void Dbg::SuspendSelf() {
Elliott Hughes475fc232011-10-25 15:00:35 -07002377 Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002378}
2379
Ian Rogers0399dde2012-06-06 17:09:28 -07002380struct GetThisVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002381 GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id_in)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002382 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002383 : StackVisitor(thread, context), this_object(nullptr), frame_id(frame_id_in) {}
Ian Rogers0399dde2012-06-06 17:09:28 -07002384
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002385 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2386 // annotalysis.
2387 virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002388 if (frame_id != GetFrameId()) {
Ian Rogers0399dde2012-06-06 17:09:28 -07002389 return true; // continue
Ian Rogers0399dde2012-06-06 17:09:28 -07002390 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -08002391 this_object = GetThisObject();
2392 return false;
Ian Rogers0399dde2012-06-06 17:09:28 -07002393 }
Elliott Hughes86b00102011-12-05 17:54:26 -08002394 }
Ian Rogers0399dde2012-06-06 17:09:28 -07002395
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002396 mirror::Object* this_object;
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002397 JDWP::FrameId frame_id;
Ian Rogers0399dde2012-06-06 17:09:28 -07002398};
2399
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002400JDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
2401 JDWP::ObjectId* result) {
2402 ScopedObjectAccessUnchecked soa(Thread::Current());
2403 Thread* thread;
2404 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002405 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07002406 JDWP::JdwpError error;
2407 thread = DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08002408 if (error != JDWP::ERR_NONE) {
2409 return error;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002410 }
Elliott Hughes9e0c1752013-01-09 14:02:58 -08002411 if (!IsSuspendedForDebugger(soa, thread)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002412 return JDWP::ERR_THREAD_NOT_SUSPENDED;
2413 }
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002414 }
Ian Rogers700a4022014-05-19 16:49:03 -07002415 std::unique_ptr<Context> context(Context::Create());
Ian Rogers7a22fa62013-01-23 12:16:16 -08002416 GetThisVisitor visitor(thread, context.get(), frame_id);
Ian Rogers0399dde2012-06-06 17:09:28 -07002417 visitor.WalkStack();
Elliott Hughes6e9d22c2012-06-22 15:02:37 -07002418 *result = gRegistry->Add(visitor.this_object);
2419 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002420}
2421
Sebastien Hertz8009f392014-09-01 17:07:11 +02002422// Walks the stack until we find the frame with the given FrameId.
2423class FindFrameVisitor FINAL : public StackVisitor {
2424 public:
2425 FindFrameVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
2426 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
2427 : StackVisitor(thread, context), frame_id_(frame_id), error_(JDWP::ERR_INVALID_FRAMEID) {}
Ian Rogersca190662012-06-26 15:45:57 -07002428
Sebastien Hertz8009f392014-09-01 17:07:11 +02002429 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
2430 // annotalysis.
2431 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
2432 if (GetFrameId() != frame_id_) {
2433 return true; // Not our frame, carry on.
Ian Rogers0399dde2012-06-06 17:09:28 -07002434 }
Sebastien Hertz8009f392014-09-01 17:07:11 +02002435 mirror::ArtMethod* m = GetMethod();
2436 if (m->IsNative()) {
2437 // We can't read/write local value from/into native method.
2438 error_ = JDWP::ERR_OPAQUE_FRAME;
2439 } else {
2440 // We found our frame.
2441 error_ = JDWP::ERR_NONE;
2442 }
2443 return false;
2444 }
2445
2446 JDWP::JdwpError GetError() const {
2447 return error_;
2448 }
2449
2450 private:
2451 const JDWP::FrameId frame_id_;
2452 JDWP::JdwpError error_;
2453};
2454
2455JDWP::JdwpError Dbg::GetLocalValues(JDWP::Request* request, JDWP::ExpandBuf* pReply) {
2456 JDWP::ObjectId thread_id = request->ReadThreadId();
2457 JDWP::FrameId frame_id = request->ReadFrameId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002458
2459 ScopedObjectAccessUnchecked soa(Thread::Current());
Sebastien Hertz8009f392014-09-01 17:07:11 +02002460 Thread* thread;
2461 {
2462 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2463 JDWP::JdwpError error;
2464 thread = DecodeThread(soa, thread_id, &error);
2465 if (error != JDWP::ERR_NONE) {
2466 return error;
2467 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002468 if (!IsSuspendedForDebugger(soa, thread)) {
2469 return JDWP::ERR_THREAD_NOT_SUSPENDED;
2470 }
Elliott Hughes221229c2013-01-08 18:17:50 -08002471 }
Sebastien Hertz8009f392014-09-01 17:07:11 +02002472 // Find the frame with the given frame_id.
Ian Rogers700a4022014-05-19 16:49:03 -07002473 std::unique_ptr<Context> context(Context::Create());
Sebastien Hertz8009f392014-09-01 17:07:11 +02002474 FindFrameVisitor visitor(thread, context.get(), frame_id);
Ian Rogers0399dde2012-06-06 17:09:28 -07002475 visitor.WalkStack();
Sebastien Hertz8009f392014-09-01 17:07:11 +02002476 if (visitor.GetError() != JDWP::ERR_NONE) {
2477 return visitor.GetError();
2478 }
2479
2480 // Read the values from visitor's context.
2481 int32_t slot_count = request->ReadSigned32("slot count");
2482 expandBufAdd4BE(pReply, slot_count); /* "int values" */
2483 for (int32_t i = 0; i < slot_count; ++i) {
2484 uint32_t slot = request->ReadUnsigned32("slot");
2485 JDWP::JdwpTag reqSigByte = request->ReadTag();
2486
2487 VLOG(jdwp) << " --> slot " << slot << " " << reqSigByte;
2488
2489 size_t width = Dbg::GetTagWidth(reqSigByte);
Sebastien Hertz7d955652014-10-22 10:57:10 +02002490 uint8_t* ptr = expandBufAddSpace(pReply, width + 1);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002491 JDWP::JdwpError error = Dbg::GetLocalValue(visitor, soa, slot, reqSigByte, ptr, width);
2492 if (error != JDWP::ERR_NONE) {
2493 return error;
2494 }
2495 }
2496 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002497}
2498
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002499constexpr JDWP::JdwpError kStackFrameLocalAccessError = JDWP::ERR_ABSENT_INFORMATION;
2500
2501static std::string GetStackContextAsString(const StackVisitor& visitor)
2502 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2503 return StringPrintf(" at DEX pc 0x%08x in method %s", visitor.GetDexPc(false),
2504 PrettyMethod(visitor.GetMethod()).c_str());
2505}
2506
2507static JDWP::JdwpError FailGetLocalValue(const StackVisitor& visitor, uint16_t vreg,
2508 JDWP::JdwpTag tag)
2509 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2510 LOG(ERROR) << "Failed to read " << tag << " local from register v" << vreg
2511 << GetStackContextAsString(visitor);
2512 return kStackFrameLocalAccessError;
2513}
2514
Sebastien Hertz8009f392014-09-01 17:07:11 +02002515JDWP::JdwpError Dbg::GetLocalValue(const StackVisitor& visitor, ScopedObjectAccessUnchecked& soa,
2516 int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) {
2517 mirror::ArtMethod* m = visitor.GetMethod();
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002518 JDWP::JdwpError error = JDWP::ERR_NONE;
2519 uint16_t vreg = DemangleSlot(slot, m, &error);
2520 if (error != JDWP::ERR_NONE) {
2521 return error;
2522 }
Sebastien Hertz8009f392014-09-01 17:07:11 +02002523 // TODO: check that the tag is compatible with the actual type of the slot!
Sebastien Hertz8009f392014-09-01 17:07:11 +02002524 switch (tag) {
2525 case JDWP::JT_BOOLEAN: {
2526 CHECK_EQ(width, 1U);
2527 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002528 if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) {
2529 return FailGetLocalValue(visitor, vreg, tag);
Ian Rogers0399dde2012-06-06 17:09:28 -07002530 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002531 VLOG(jdwp) << "get boolean local " << vreg << " = " << intVal;
2532 JDWP::Set1(buf + 1, intVal != 0);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002533 break;
Ian Rogers0399dde2012-06-06 17:09:28 -07002534 }
Sebastien Hertz8009f392014-09-01 17:07:11 +02002535 case JDWP::JT_BYTE: {
2536 CHECK_EQ(width, 1U);
2537 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002538 if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) {
2539 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002540 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002541 VLOG(jdwp) << "get byte local " << vreg << " = " << intVal;
2542 JDWP::Set1(buf + 1, intVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002543 break;
2544 }
2545 case JDWP::JT_SHORT:
2546 case JDWP::JT_CHAR: {
2547 CHECK_EQ(width, 2U);
2548 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002549 if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) {
2550 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002551 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002552 VLOG(jdwp) << "get short/char local " << vreg << " = " << intVal;
2553 JDWP::Set2BE(buf + 1, intVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002554 break;
2555 }
2556 case JDWP::JT_INT: {
2557 CHECK_EQ(width, 4U);
2558 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002559 if (!visitor.GetVReg(m, vreg, kIntVReg, &intVal)) {
2560 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002561 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002562 VLOG(jdwp) << "get int local " << vreg << " = " << intVal;
2563 JDWP::Set4BE(buf + 1, intVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002564 break;
2565 }
2566 case JDWP::JT_FLOAT: {
2567 CHECK_EQ(width, 4U);
2568 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002569 if (!visitor.GetVReg(m, vreg, kFloatVReg, &intVal)) {
2570 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002571 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002572 VLOG(jdwp) << "get float local " << vreg << " = " << intVal;
2573 JDWP::Set4BE(buf + 1, intVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002574 break;
2575 }
2576 case JDWP::JT_ARRAY:
2577 case JDWP::JT_CLASS_LOADER:
2578 case JDWP::JT_CLASS_OBJECT:
2579 case JDWP::JT_OBJECT:
2580 case JDWP::JT_STRING:
2581 case JDWP::JT_THREAD:
2582 case JDWP::JT_THREAD_GROUP: {
2583 CHECK_EQ(width, sizeof(JDWP::ObjectId));
2584 uint32_t intVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002585 if (!visitor.GetVReg(m, vreg, kReferenceVReg, &intVal)) {
2586 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002587 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002588 mirror::Object* o = reinterpret_cast<mirror::Object*>(intVal);
2589 VLOG(jdwp) << "get " << tag << " object local " << vreg << " = " << o;
2590 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
2591 LOG(FATAL) << StringPrintf("Found invalid object %#" PRIxPTR " in register v%u",
2592 reinterpret_cast<uintptr_t>(o), vreg)
2593 << GetStackContextAsString(visitor);
2594 UNREACHABLE();
2595 }
2596 tag = TagFromObject(soa, o);
2597 JDWP::SetObjectId(buf + 1, gRegistry->Add(o));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002598 break;
2599 }
2600 case JDWP::JT_DOUBLE: {
2601 CHECK_EQ(width, 8U);
2602 uint64_t longVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002603 if (!visitor.GetVRegPair(m, vreg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) {
2604 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002605 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002606 VLOG(jdwp) << "get double local " << vreg << " = " << longVal;
2607 JDWP::Set8BE(buf + 1, longVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002608 break;
2609 }
2610 case JDWP::JT_LONG: {
2611 CHECK_EQ(width, 8U);
2612 uint64_t longVal;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002613 if (!visitor.GetVRegPair(m, vreg, kLongLoVReg, kLongHiVReg, &longVal)) {
2614 return FailGetLocalValue(visitor, vreg, tag);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002615 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002616 VLOG(jdwp) << "get long local " << vreg << " = " << longVal;
2617 JDWP::Set8BE(buf + 1, longVal);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002618 break;
2619 }
2620 default:
2621 LOG(FATAL) << "Unknown tag " << tag;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002622 UNREACHABLE();
Sebastien Hertz8009f392014-09-01 17:07:11 +02002623 }
Ian Rogers0399dde2012-06-06 17:09:28 -07002624
Sebastien Hertz8009f392014-09-01 17:07:11 +02002625 // Prepend tag, which may have been updated.
2626 JDWP::Set1(buf, tag);
2627 return JDWP::ERR_NONE;
2628}
2629
2630JDWP::JdwpError Dbg::SetLocalValues(JDWP::Request* request) {
2631 JDWP::ObjectId thread_id = request->ReadThreadId();
2632 JDWP::FrameId frame_id = request->ReadFrameId();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002633
2634 ScopedObjectAccessUnchecked soa(Thread::Current());
Sebastien Hertz8009f392014-09-01 17:07:11 +02002635 Thread* thread;
2636 {
2637 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2638 JDWP::JdwpError error;
2639 thread = DecodeThread(soa, thread_id, &error);
2640 if (error != JDWP::ERR_NONE) {
2641 return error;
2642 }
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002643 if (!IsSuspendedForDebugger(soa, thread)) {
2644 return JDWP::ERR_THREAD_NOT_SUSPENDED;
2645 }
Elliott Hughes221229c2013-01-08 18:17:50 -08002646 }
Sebastien Hertz8009f392014-09-01 17:07:11 +02002647 // Find the frame with the given frame_id.
Ian Rogers700a4022014-05-19 16:49:03 -07002648 std::unique_ptr<Context> context(Context::Create());
Sebastien Hertz8009f392014-09-01 17:07:11 +02002649 FindFrameVisitor visitor(thread, context.get(), frame_id);
Ian Rogers0399dde2012-06-06 17:09:28 -07002650 visitor.WalkStack();
Sebastien Hertz8009f392014-09-01 17:07:11 +02002651 if (visitor.GetError() != JDWP::ERR_NONE) {
2652 return visitor.GetError();
2653 }
2654
2655 // Writes the values into visitor's context.
2656 int32_t slot_count = request->ReadSigned32("slot count");
2657 for (int32_t i = 0; i < slot_count; ++i) {
2658 uint32_t slot = request->ReadUnsigned32("slot");
2659 JDWP::JdwpTag sigByte = request->ReadTag();
2660 size_t width = Dbg::GetTagWidth(sigByte);
2661 uint64_t value = request->ReadValue(width);
2662
2663 VLOG(jdwp) << " --> slot " << slot << " " << sigByte << " " << value;
2664 JDWP::JdwpError error = Dbg::SetLocalValue(visitor, slot, sigByte, value, width);
2665 if (error != JDWP::ERR_NONE) {
2666 return error;
2667 }
2668 }
2669 return JDWP::ERR_NONE;
2670}
2671
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002672template<typename T>
2673static JDWP::JdwpError FailSetLocalValue(const StackVisitor& visitor, uint16_t vreg,
2674 JDWP::JdwpTag tag, T value)
2675 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2676 LOG(ERROR) << "Failed to write " << tag << " local " << value
2677 << " (0x" << std::hex << value << ") into register v" << vreg
2678 << GetStackContextAsString(visitor);
2679 return kStackFrameLocalAccessError;
2680}
2681
Sebastien Hertz8009f392014-09-01 17:07:11 +02002682JDWP::JdwpError Dbg::SetLocalValue(StackVisitor& visitor, int slot, JDWP::JdwpTag tag,
2683 uint64_t value, size_t width) {
2684 mirror::ArtMethod* m = visitor.GetMethod();
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002685 JDWP::JdwpError error = JDWP::ERR_NONE;
2686 uint16_t vreg = DemangleSlot(slot, m, &error);
2687 if (error != JDWP::ERR_NONE) {
2688 return error;
2689 }
Sebastien Hertz8009f392014-09-01 17:07:11 +02002690 // TODO: check that the tag is compatible with the actual type of the slot!
Sebastien Hertz8009f392014-09-01 17:07:11 +02002691 switch (tag) {
2692 case JDWP::JT_BOOLEAN:
2693 case JDWP::JT_BYTE:
2694 CHECK_EQ(width, 1U);
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002695 if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) {
2696 return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002697 }
2698 break;
2699 case JDWP::JT_SHORT:
2700 case JDWP::JT_CHAR:
2701 CHECK_EQ(width, 2U);
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002702 if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) {
2703 return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002704 }
2705 break;
2706 case JDWP::JT_INT:
2707 CHECK_EQ(width, 4U);
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002708 if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kIntVReg)) {
2709 return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002710 }
2711 break;
2712 case JDWP::JT_FLOAT:
2713 CHECK_EQ(width, 4U);
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002714 if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(value), kFloatVReg)) {
2715 return FailSetLocalValue(visitor, vreg, tag, static_cast<uint32_t>(value));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002716 }
2717 break;
2718 case JDWP::JT_ARRAY:
2719 case JDWP::JT_CLASS_LOADER:
2720 case JDWP::JT_CLASS_OBJECT:
2721 case JDWP::JT_OBJECT:
2722 case JDWP::JT_STRING:
2723 case JDWP::JT_THREAD:
2724 case JDWP::JT_THREAD_GROUP: {
2725 CHECK_EQ(width, sizeof(JDWP::ObjectId));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002726 mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value),
2727 &error);
2728 if (error != JDWP::ERR_NONE) {
2729 VLOG(jdwp) << tag << " object " << o << " is an invalid object";
2730 return JDWP::ERR_INVALID_OBJECT;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002731 }
2732 if (!visitor.SetVReg(m, vreg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)),
2733 kReferenceVReg)) {
2734 return FailSetLocalValue(visitor, vreg, tag, reinterpret_cast<uintptr_t>(o));
Sebastien Hertz8009f392014-09-01 17:07:11 +02002735 }
2736 break;
2737 }
2738 case JDWP::JT_DOUBLE: {
2739 CHECK_EQ(width, 8U);
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002740 if (!visitor.SetVRegPair(m, vreg, value, kDoubleLoVReg, kDoubleHiVReg)) {
2741 return FailSetLocalValue(visitor, vreg, tag, value);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002742 }
2743 break;
2744 }
2745 case JDWP::JT_LONG: {
2746 CHECK_EQ(width, 8U);
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002747 if (!visitor.SetVRegPair(m, vreg, value, kLongLoVReg, kLongHiVReg)) {
2748 return FailSetLocalValue(visitor, vreg, tag, value);
Sebastien Hertz8009f392014-09-01 17:07:11 +02002749 }
2750 break;
2751 }
2752 default:
2753 LOG(FATAL) << "Unknown tag " << tag;
Sebastien Hertzabbabc82015-03-26 08:47:47 +01002754 UNREACHABLE();
Sebastien Hertz8009f392014-09-01 17:07:11 +02002755 }
2756 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002757}
2758
Sebastien Hertz6995c602014-09-09 12:10:13 +02002759static void SetEventLocation(JDWP::EventLocation* location, mirror::ArtMethod* m, uint32_t dex_pc)
2760 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2761 DCHECK(location != nullptr);
2762 if (m == nullptr) {
2763 memset(location, 0, sizeof(*location));
2764 } else {
2765 location->method = m;
2766 location->dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint32_t>(-1) : dex_pc;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002767 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002768}
2769
Ian Rogersef7d42f2014-01-06 12:55:46 -08002770void Dbg::PostLocationEvent(mirror::ArtMethod* m, int dex_pc, mirror::Object* this_object,
Jeff Hao579b0242013-11-18 13:16:49 -08002771 int event_flags, const JValue* return_value) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002772 if (!IsDebuggerActive()) {
2773 return;
2774 }
2775 DCHECK(m != nullptr);
2776 DCHECK_EQ(m->IsStatic(), this_object == nullptr);
Sebastien Hertz6995c602014-09-09 12:10:13 +02002777 JDWP::EventLocation location;
2778 SetEventLocation(&location, m, dex_pc);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002779
Sebastien Hertz6995c602014-09-09 12:10:13 +02002780 gJdwpState->PostLocationEvent(&location, this_object, event_flags, return_value);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002781}
2782
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002783void Dbg::PostFieldAccessEvent(mirror::ArtMethod* m, int dex_pc,
2784 mirror::Object* this_object, mirror::ArtField* f) {
2785 if (!IsDebuggerActive()) {
2786 return;
2787 }
2788 DCHECK(m != nullptr);
2789 DCHECK(f != nullptr);
Sebastien Hertz6995c602014-09-09 12:10:13 +02002790 JDWP::EventLocation location;
2791 SetEventLocation(&location, m, dex_pc);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002792
Sebastien Hertz6995c602014-09-09 12:10:13 +02002793 gJdwpState->PostFieldEvent(&location, f, this_object, nullptr, false);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002794}
2795
2796void Dbg::PostFieldModificationEvent(mirror::ArtMethod* m, int dex_pc,
2797 mirror::Object* this_object, mirror::ArtField* f,
2798 const JValue* field_value) {
2799 if (!IsDebuggerActive()) {
2800 return;
2801 }
2802 DCHECK(m != nullptr);
2803 DCHECK(f != nullptr);
2804 DCHECK(field_value != nullptr);
Sebastien Hertz6995c602014-09-09 12:10:13 +02002805 JDWP::EventLocation location;
2806 SetEventLocation(&location, m, dex_pc);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002807
Sebastien Hertz6995c602014-09-09 12:10:13 +02002808 gJdwpState->PostFieldEvent(&location, f, this_object, field_value, true);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02002809}
2810
Nicolas Geoffray14691c52015-03-05 10:40:17 +00002811/**
2812 * Finds the location where this exception will be caught. We search until we reach the top
2813 * frame, in which case this exception is considered uncaught.
2814 */
2815class CatchLocationFinder : public StackVisitor {
2816 public:
2817 CatchLocationFinder(Thread* self, const Handle<mirror::Throwable>& exception, Context* context)
2818 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
2819 : StackVisitor(self, context),
2820 self_(self),
2821 exception_(exception),
2822 handle_scope_(self),
2823 this_at_throw_(handle_scope_.NewHandle<mirror::Object>(nullptr)),
2824 catch_method_(handle_scope_.NewHandle<mirror::ArtMethod>(nullptr)),
2825 throw_method_(handle_scope_.NewHandle<mirror::ArtMethod>(nullptr)),
2826 catch_dex_pc_(DexFile::kDexNoIndex),
2827 throw_dex_pc_(DexFile::kDexNoIndex) {
2828 }
2829
2830 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2831 mirror::ArtMethod* method = GetMethod();
2832 DCHECK(method != nullptr);
2833 if (method->IsRuntimeMethod()) {
2834 // Ignore callee save method.
2835 DCHECK(method->IsCalleeSaveMethod());
2836 return true;
2837 }
2838
2839 uint32_t dex_pc = GetDexPc();
2840 if (throw_method_.Get() == nullptr) {
2841 // First Java method found. It is either the method that threw the exception,
2842 // or the Java native method that is reporting an exception thrown by
2843 // native code.
2844 this_at_throw_.Assign(GetThisObject());
2845 throw_method_.Assign(method);
2846 throw_dex_pc_ = dex_pc;
2847 }
2848
2849 if (dex_pc != DexFile::kDexNoIndex) {
2850 StackHandleScope<2> hs(self_);
2851 uint32_t found_dex_pc;
2852 Handle<mirror::Class> exception_class(hs.NewHandle(exception_->GetClass()));
2853 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
2854 bool unused_clear_exception;
2855 found_dex_pc = mirror::ArtMethod::FindCatchBlock(
2856 h_method, exception_class, dex_pc, &unused_clear_exception);
2857 if (found_dex_pc != DexFile::kDexNoIndex) {
2858 catch_method_.Assign(method);
2859 catch_dex_pc_ = found_dex_pc;
2860 return false; // End stack walk.
2861 }
2862 }
2863 return true; // Continue stack walk.
2864 }
2865
2866 mirror::ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2867 return catch_method_.Get();
2868 }
2869
2870 mirror::ArtMethod* GetThrowMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2871 return throw_method_.Get();
2872 }
2873
2874 mirror::Object* GetThisAtThrow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2875 return this_at_throw_.Get();
2876 }
2877
2878 uint32_t GetCatchDexPc() const {
2879 return catch_dex_pc_;
2880 }
2881
2882 uint32_t GetThrowDexPc() const {
2883 return throw_dex_pc_;
2884 }
2885
2886 private:
2887 Thread* const self_;
2888 const Handle<mirror::Throwable>& exception_;
2889 StackHandleScope<3> handle_scope_;
2890 MutableHandle<mirror::Object> this_at_throw_;
2891 MutableHandle<mirror::ArtMethod> catch_method_;
2892 MutableHandle<mirror::ArtMethod> throw_method_;
2893 uint32_t catch_dex_pc_;
2894 uint32_t throw_dex_pc_;
2895
2896 DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder);
2897};
2898
2899void Dbg::PostException(mirror::Throwable* exception_object) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07002900 if (!IsDebuggerActive()) {
Ian Rogers0ad5bb82011-12-07 10:16:32 -08002901 return;
2902 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +00002903 StackHandleScope<1> handle_scope(Thread::Current());
2904 Handle<mirror::Throwable> h_exception(handle_scope.NewHandle(exception_object));
2905 std::unique_ptr<Context> context(Context::Create());
2906 CatchLocationFinder clf(Thread::Current(), h_exception, context.get());
2907 clf.WalkStack(/* include_transitions */ false);
Sebastien Hertz6995c602014-09-09 12:10:13 +02002908 JDWP::EventLocation exception_throw_location;
Nicolas Geoffray14691c52015-03-05 10:40:17 +00002909 SetEventLocation(&exception_throw_location, clf.GetThrowMethod(), clf.GetThrowDexPc());
Sebastien Hertz6995c602014-09-09 12:10:13 +02002910 JDWP::EventLocation exception_catch_location;
Nicolas Geoffray14691c52015-03-05 10:40:17 +00002911 SetEventLocation(&exception_catch_location, clf.GetCatchMethod(), clf.GetCatchDexPc());
Elliott Hughes4740cdf2011-12-07 14:07:12 -08002912
Nicolas Geoffray14691c52015-03-05 10:40:17 +00002913 gJdwpState->PostException(&exception_throw_location, h_exception.Get(), &exception_catch_location,
2914 clf.GetThisAtThrow());
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002915}
2916
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002917void Dbg::PostClassPrepare(mirror::Class* c) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07002918 if (!IsDebuggerActive()) {
Elliott Hughes4740cdf2011-12-07 14:07:12 -08002919 return;
2920 }
Sebastien Hertz6995c602014-09-09 12:10:13 +02002921 gJdwpState->PostClassPrepare(c);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07002922}
2923
Ian Rogers62d6c772013-02-27 08:32:07 -08002924void Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object,
Sebastien Hertz8379b222014-02-24 17:38:15 +01002925 mirror::ArtMethod* m, uint32_t dex_pc,
2926 int event_flags, const JValue* return_value) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002927 if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
Elliott Hughes2aa2e392012-02-17 17:15:43 -08002928 return;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002929 }
2930
Elliott Hughes86964332012-02-15 19:37:42 -08002931 if (IsBreakpoint(m, dex_pc)) {
2932 event_flags |= kBreakpoint;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002933 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002934
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002935 // If the debugger is single-stepping one of our threads, check to
2936 // see if we're that thread and we've reached a step point.
2937 const SingleStepControl* single_step_control = thread->GetSingleStepControl();
Sebastien Hertz597c4f02015-01-26 17:37:14 +01002938 if (single_step_control != nullptr) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002939 CHECK(!m->IsNative());
Sebastien Hertz597c4f02015-01-26 17:37:14 +01002940 if (single_step_control->GetStepDepth() == JDWP::SD_INTO) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002941 // Step into method calls. We break when the line number
2942 // or method pointer changes. If we're in SS_MIN mode, we
2943 // always stop.
Sebastien Hertz597c4f02015-01-26 17:37:14 +01002944 if (single_step_control->GetMethod() != m) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002945 event_flags |= kSingleStep;
2946 VLOG(jdwp) << "SS new method";
Sebastien Hertz597c4f02015-01-26 17:37:14 +01002947 } else if (single_step_control->GetStepSize() == JDWP::SS_MIN) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002948 event_flags |= kSingleStep;
2949 VLOG(jdwp) << "SS new instruction";
Sebastien Hertzbb43b432014-04-14 11:59:08 +02002950 } else if (single_step_control->ContainsDexPc(dex_pc)) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002951 event_flags |= kSingleStep;
2952 VLOG(jdwp) << "SS new line";
2953 }
Sebastien Hertz597c4f02015-01-26 17:37:14 +01002954 } else if (single_step_control->GetStepDepth() == JDWP::SD_OVER) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002955 // Step over method calls. We break when the line number is
2956 // different and the frame depth is <= the original frame
2957 // depth. (We can't just compare on the method, because we
2958 // might get unrolled past it by an exception, and it's tricky
2959 // to identify recursion.)
2960
2961 int stack_depth = GetStackDepth(thread);
2962
Sebastien Hertz597c4f02015-01-26 17:37:14 +01002963 if (stack_depth < single_step_control->GetStackDepth()) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002964 // Popped up one or more frames, always trigger.
2965 event_flags |= kSingleStep;
2966 VLOG(jdwp) << "SS method pop";
Sebastien Hertz597c4f02015-01-26 17:37:14 +01002967 } else if (stack_depth == single_step_control->GetStackDepth()) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002968 // Same depth, see if we moved.
Sebastien Hertz597c4f02015-01-26 17:37:14 +01002969 if (single_step_control->GetStepSize() == JDWP::SS_MIN) {
Elliott Hughes86964332012-02-15 19:37:42 -08002970 event_flags |= kSingleStep;
2971 VLOG(jdwp) << "SS new instruction";
Sebastien Hertzbb43b432014-04-14 11:59:08 +02002972 } else if (single_step_control->ContainsDexPc(dex_pc)) {
Elliott Hughes2435a572012-02-17 16:07:41 -08002973 event_flags |= kSingleStep;
2974 VLOG(jdwp) << "SS new line";
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002975 }
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002976 }
2977 } else {
Sebastien Hertz597c4f02015-01-26 17:37:14 +01002978 CHECK_EQ(single_step_control->GetStepDepth(), JDWP::SD_OUT);
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002979 // Return from the current method. We break when the frame
2980 // depth pops up.
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002981
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002982 // This differs from the "method exit" break in that it stops
2983 // with the PC at the next instruction in the returned-to
2984 // function, rather than the end of the returning function.
Elliott Hughes86964332012-02-15 19:37:42 -08002985
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002986 int stack_depth = GetStackDepth(thread);
Sebastien Hertz597c4f02015-01-26 17:37:14 +01002987 if (stack_depth < single_step_control->GetStackDepth()) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01002988 event_flags |= kSingleStep;
2989 VLOG(jdwp) << "SS method pop";
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002990 }
2991 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002992 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002993
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002994 // If there's something interesting going on, see if it matches one
2995 // of the debugger filters.
2996 if (event_flags != 0) {
Sebastien Hertz8379b222014-02-24 17:38:15 +01002997 Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -08002998 }
2999}
3000
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003001size_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) {
3002 switch (instrumentation_event) {
3003 case instrumentation::Instrumentation::kMethodEntered:
3004 return &method_enter_event_ref_count_;
3005 case instrumentation::Instrumentation::kMethodExited:
3006 return &method_exit_event_ref_count_;
3007 case instrumentation::Instrumentation::kDexPcMoved:
3008 return &dex_pc_change_event_ref_count_;
3009 case instrumentation::Instrumentation::kFieldRead:
3010 return &field_read_event_ref_count_;
3011 case instrumentation::Instrumentation::kFieldWritten:
3012 return &field_write_event_ref_count_;
3013 case instrumentation::Instrumentation::kExceptionCaught:
3014 return &exception_catch_event_ref_count_;
3015 default:
3016 return nullptr;
3017 }
3018}
3019
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003020// Process request while all mutator threads are suspended.
3021void Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003022 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003023 switch (request.GetKind()) {
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003024 case DeoptimizationRequest::kNothing:
3025 LOG(WARNING) << "Ignoring empty deoptimization request.";
3026 break;
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003027 case DeoptimizationRequest::kRegisterForEvent:
3028 VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003029 request.InstrumentationEvent());
3030 instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent());
3031 instrumentation_events_ |= request.InstrumentationEvent();
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003032 break;
3033 case DeoptimizationRequest::kUnregisterForEvent:
3034 VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003035 request.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003036 instrumentation->RemoveListener(&gDebugInstrumentationListener,
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003037 request.InstrumentationEvent());
3038 instrumentation_events_ &= ~request.InstrumentationEvent();
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003039 break;
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003040 case DeoptimizationRequest::kFullDeoptimization:
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003041 VLOG(jdwp) << "Deoptimize the world ...";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003042 instrumentation->DeoptimizeEverything();
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003043 VLOG(jdwp) << "Deoptimize the world DONE";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003044 break;
3045 case DeoptimizationRequest::kFullUndeoptimization:
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003046 VLOG(jdwp) << "Undeoptimize the world ...";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003047 instrumentation->UndeoptimizeEverything();
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003048 VLOG(jdwp) << "Undeoptimize the world DONE";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003049 break;
3050 case DeoptimizationRequest::kSelectiveDeoptimization:
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003051 VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " ...";
3052 instrumentation->Deoptimize(request.Method());
3053 VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " DONE";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003054 break;
3055 case DeoptimizationRequest::kSelectiveUndeoptimization:
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003056 VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " ...";
3057 instrumentation->Undeoptimize(request.Method());
3058 VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " DONE";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003059 break;
3060 default:
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003061 LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind();
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003062 break;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003063 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003064}
3065
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003066void Dbg::RequestDeoptimization(const DeoptimizationRequest& req) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003067 if (req.GetKind() == DeoptimizationRequest::kNothing) {
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003068 // Nothing to do.
3069 return;
3070 }
Brian Carlstrom306db812014-09-05 13:01:41 -07003071 MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003072 RequestDeoptimizationLocked(req);
3073}
3074
3075void Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003076 switch (req.GetKind()) {
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003077 case DeoptimizationRequest::kRegisterForEvent: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003078 DCHECK_NE(req.InstrumentationEvent(), 0u);
3079 size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003080 CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003081 req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003082 if (*counter == 0) {
Sebastien Hertz7d2ae432014-05-15 11:26:34 +02003083 VLOG(jdwp) << StringPrintf("Queue request #%zd to start listening to instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003084 deoptimization_requests_.size(), req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003085 deoptimization_requests_.push_back(req);
3086 }
3087 *counter = *counter + 1;
3088 break;
3089 }
3090 case DeoptimizationRequest::kUnregisterForEvent: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003091 DCHECK_NE(req.InstrumentationEvent(), 0u);
3092 size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003093 CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003094 req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003095 *counter = *counter - 1;
3096 if (*counter == 0) {
Sebastien Hertz7d2ae432014-05-15 11:26:34 +02003097 VLOG(jdwp) << StringPrintf("Queue request #%zd to stop listening to instrumentation event 0x%x",
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003098 deoptimization_requests_.size(), req.InstrumentationEvent());
Sebastien Hertz42cd43f2014-05-13 14:15:41 +02003099 deoptimization_requests_.push_back(req);
3100 }
3101 break;
3102 }
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003103 case DeoptimizationRequest::kFullDeoptimization: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003104 DCHECK(req.Method() == nullptr);
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003105 if (full_deoptimization_event_count_ == 0) {
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003106 VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
3107 << " for full deoptimization";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003108 deoptimization_requests_.push_back(req);
3109 }
3110 ++full_deoptimization_event_count_;
3111 break;
3112 }
3113 case DeoptimizationRequest::kFullUndeoptimization: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003114 DCHECK(req.Method() == nullptr);
Sebastien Hertze713d932014-05-15 10:48:53 +02003115 DCHECK_GT(full_deoptimization_event_count_, 0U);
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003116 --full_deoptimization_event_count_;
3117 if (full_deoptimization_event_count_ == 0) {
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003118 VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
3119 << " for full undeoptimization";
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003120 deoptimization_requests_.push_back(req);
3121 }
3122 break;
3123 }
3124 case DeoptimizationRequest::kSelectiveDeoptimization: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003125 DCHECK(req.Method() != nullptr);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003126 VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003127 << " for deoptimization of " << PrettyMethod(req.Method());
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003128 deoptimization_requests_.push_back(req);
3129 break;
3130 }
3131 case DeoptimizationRequest::kSelectiveUndeoptimization: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003132 DCHECK(req.Method() != nullptr);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003133 VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003134 << " for undeoptimization of " << PrettyMethod(req.Method());
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003135 deoptimization_requests_.push_back(req);
3136 break;
3137 }
3138 default: {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003139 LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind();
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003140 break;
3141 }
3142 }
3143}
3144
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003145void Dbg::ManageDeoptimization() {
3146 Thread* const self = Thread::Current();
3147 {
3148 // Avoid suspend/resume if there is no pending request.
Brian Carlstrom306db812014-09-05 13:01:41 -07003149 MutexLock mu(self, *Locks::deoptimization_lock_);
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003150 if (deoptimization_requests_.empty()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003151 return;
3152 }
3153 }
3154 CHECK_EQ(self->GetState(), kRunnable);
3155 self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization);
3156 // We need to suspend mutator threads first.
3157 Runtime* const runtime = Runtime::Current();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -07003158 runtime->GetThreadList()->SuspendAll(__FUNCTION__);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003159 const ThreadState old_state = self->SetStateUnsafe(kRunnable);
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003160 {
Brian Carlstrom306db812014-09-05 13:01:41 -07003161 MutexLock mu(self, *Locks::deoptimization_lock_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003162 size_t req_index = 0;
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003163 for (DeoptimizationRequest& request : deoptimization_requests_) {
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01003164 VLOG(jdwp) << "Process deoptimization request #" << req_index++;
Sebastien Hertz4d25df32014-03-21 17:44:46 +01003165 ProcessDeoptimizationRequest(request);
3166 }
3167 deoptimization_requests_.clear();
3168 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003169 CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
3170 runtime->GetThreadList()->ResumeAll();
3171 self->TransitionFromSuspendedToRunnable();
3172}
3173
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003174static bool IsMethodPossiblyInlined(Thread* self, mirror::ArtMethod* m)
3175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003176 const DexFile::CodeItem* code_item = m->GetCodeItem();
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003177 if (code_item == nullptr) {
3178 // TODO We should not be asked to watch location in a native or abstract method so the code item
3179 // should never be null. We could just check we never encounter this case.
3180 return false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003181 }
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003182 // Note: method verifier may cause thread suspension.
3183 self->AssertThreadSuspensionIsAllowable();
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07003184 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003185 mirror::Class* declaring_class = m->GetDeclaringClass();
3186 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
3187 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07003188 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -07003189 verifier::MethodVerifier verifier(self, dex_cache->GetDexFile(), dex_cache, class_loader,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07003190 &m->GetClassDef(), code_item, m->GetDexMethodIndex(), method,
Mathieu Chartier4306ef82014-12-19 18:41:47 -08003191 m->GetAccessFlags(), false, true, false, true);
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003192 // Note: we don't need to verify the method.
3193 return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr);
3194}
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003195
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003196static const Breakpoint* FindFirstBreakpointForMethod(mirror::ArtMethod* m)
Sebastien Hertzed2be172014-08-19 15:33:43 +02003197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003198 for (Breakpoint& breakpoint : gBreakpoints) {
3199 if (breakpoint.Method() == m) {
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003200 return &breakpoint;
3201 }
3202 }
3203 return nullptr;
3204}
3205
Mathieu Chartierd8565452015-03-26 09:41:50 -07003206bool Dbg::MethodHasAnyBreakpoints(mirror::ArtMethod* method) {
3207 ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
3208 return FindFirstBreakpointForMethod(method) != nullptr;
3209}
3210
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003211// Sanity checks all existing breakpoints on the same method.
Sebastien Hertzf3928792014-11-17 19:00:37 +01003212static void SanityCheckExistingBreakpoints(mirror::ArtMethod* m,
3213 DeoptimizationRequest::Kind deoptimization_kind)
Sebastien Hertzed2be172014-08-19 15:33:43 +02003214 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) {
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003215 for (const Breakpoint& breakpoint : gBreakpoints) {
Sebastien Hertzf3928792014-11-17 19:00:37 +01003216 if (breakpoint.Method() == m) {
3217 CHECK_EQ(deoptimization_kind, breakpoint.GetDeoptimizationKind());
3218 }
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003219 }
Sebastien Hertzf3928792014-11-17 19:00:37 +01003220 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
3221 if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) {
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003222 // We should have deoptimized everything but not "selectively" deoptimized this method.
Sebastien Hertzf3928792014-11-17 19:00:37 +01003223 CHECK(instrumentation->AreAllMethodsDeoptimized());
3224 CHECK(!instrumentation->IsDeoptimized(m));
3225 } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003226 // We should have "selectively" deoptimized this method.
3227 // Note: while we have not deoptimized everything for this method, we may have done it for
3228 // another event.
Sebastien Hertzf3928792014-11-17 19:00:37 +01003229 CHECK(instrumentation->IsDeoptimized(m));
3230 } else {
3231 // This method does not require deoptimization.
3232 CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing);
3233 CHECK(!instrumentation->IsDeoptimized(m));
3234 }
3235}
3236
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003237// Returns the deoptimization kind required to set a breakpoint in a method.
3238// If a breakpoint has already been set, we also return the first breakpoint
3239// through the given 'existing_brkpt' pointer.
Sebastien Hertzf3928792014-11-17 19:00:37 +01003240static DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self,
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003241 mirror::ArtMethod* m,
3242 const Breakpoint** existing_brkpt)
Sebastien Hertzf3928792014-11-17 19:00:37 +01003243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3244 if (!Dbg::RequiresDeoptimization()) {
3245 // We already run in interpreter-only mode so we don't need to deoptimize anything.
3246 VLOG(jdwp) << "No need for deoptimization when fully running with interpreter for method "
3247 << PrettyMethod(m);
3248 return DeoptimizationRequest::kNothing;
3249 }
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003250 const Breakpoint* first_breakpoint;
Sebastien Hertzf3928792014-11-17 19:00:37 +01003251 {
3252 ReaderMutexLock mu(self, *Locks::breakpoint_lock_);
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003253 first_breakpoint = FindFirstBreakpointForMethod(m);
3254 *existing_brkpt = first_breakpoint;
Sebastien Hertzf3928792014-11-17 19:00:37 +01003255 }
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003256
3257 if (first_breakpoint == nullptr) {
Sebastien Hertzf3928792014-11-17 19:00:37 +01003258 // There is no breakpoint on this method yet: we need to deoptimize. If this method may be
3259 // inlined, we deoptimize everything; otherwise we deoptimize only this method.
3260 // Note: IsMethodPossiblyInlined goes into the method verifier and may cause thread suspension.
3261 // Therefore we must not hold any lock when we call it.
3262 bool need_full_deoptimization = IsMethodPossiblyInlined(self, m);
3263 if (need_full_deoptimization) {
3264 VLOG(jdwp) << "Need full deoptimization because of possible inlining of method "
3265 << PrettyMethod(m);
3266 return DeoptimizationRequest::kFullDeoptimization;
3267 } else {
3268 // We don't need to deoptimize if the method has not been compiled.
3269 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
3270 const bool is_compiled = class_linker->GetOatMethodQuickCodeFor(m) != nullptr;
3271 if (is_compiled) {
Sebastien Hertz6963e442014-11-26 22:11:27 +01003272 // If the method may be called through its direct code pointer (without loading
3273 // its updated entrypoint), we need full deoptimization to not miss the breakpoint.
3274 if (class_linker->MayBeCalledWithDirectCodePointer(m)) {
3275 VLOG(jdwp) << "Need full deoptimization because of possible direct code call "
3276 << "into image for compiled method " << PrettyMethod(m);
3277 return DeoptimizationRequest::kFullDeoptimization;
3278 } else {
3279 VLOG(jdwp) << "Need selective deoptimization for compiled method " << PrettyMethod(m);
3280 return DeoptimizationRequest::kSelectiveDeoptimization;
3281 }
Sebastien Hertzf3928792014-11-17 19:00:37 +01003282 } else {
3283 // Method is not compiled: we don't need to deoptimize.
3284 VLOG(jdwp) << "No need for deoptimization for non-compiled method " << PrettyMethod(m);
3285 return DeoptimizationRequest::kNothing;
3286 }
3287 }
3288 } else {
3289 // There is at least one breakpoint for this method: we don't need to deoptimize.
3290 // Let's check that all breakpoints are configured the same way for deoptimization.
3291 VLOG(jdwp) << "Breakpoint already set: no deoptimization is required";
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003292 DeoptimizationRequest::Kind deoptimization_kind = first_breakpoint->GetDeoptimizationKind();
Sebastien Hertzf3928792014-11-17 19:00:37 +01003293 if (kIsDebugBuild) {
3294 ReaderMutexLock mu(self, *Locks::breakpoint_lock_);
3295 SanityCheckExistingBreakpoints(m, deoptimization_kind);
3296 }
3297 return DeoptimizationRequest::kNothing;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003298 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003299}
3300
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003301// Installs a breakpoint at the specified location. Also indicates through the deoptimization
3302// request if we need to deoptimize.
3303void Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
3304 Thread* const self = Thread::Current();
Brian Carlstromea46f952013-07-30 01:26:50 -07003305 mirror::ArtMethod* m = FromMethodId(location->method_id);
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003306 DCHECK(m != nullptr) << "No method for method id " << location->method_id;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003307
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003308 const Breakpoint* existing_breakpoint = nullptr;
3309 const DeoptimizationRequest::Kind deoptimization_kind =
3310 GetRequiredDeoptimizationKind(self, m, &existing_breakpoint);
Sebastien Hertzf3928792014-11-17 19:00:37 +01003311 req->SetKind(deoptimization_kind);
3312 if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
3313 req->SetMethod(m);
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003314 } else {
Sebastien Hertzf3928792014-11-17 19:00:37 +01003315 CHECK(deoptimization_kind == DeoptimizationRequest::kNothing ||
3316 deoptimization_kind == DeoptimizationRequest::kFullDeoptimization);
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003317 req->SetMethod(nullptr);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01003318 }
3319
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003320 {
3321 WriterMutexLock mu(self, *Locks::breakpoint_lock_);
Sebastien Hertzabe93e02014-12-17 16:35:50 +01003322 // If there is at least one existing breakpoint on the same method, the new breakpoint
3323 // must have the same deoptimization kind than the existing breakpoint(s).
3324 DeoptimizationRequest::Kind breakpoint_deoptimization_kind;
3325 if (existing_breakpoint != nullptr) {
3326 breakpoint_deoptimization_kind = existing_breakpoint->GetDeoptimizationKind();
3327 } else {
3328 breakpoint_deoptimization_kind = deoptimization_kind;
3329 }
3330 gBreakpoints.push_back(Breakpoint(m, location->dex_pc, breakpoint_deoptimization_kind));
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003331 VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": "
3332 << gBreakpoints[gBreakpoints.size() - 1];
3333 }
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003334}
3335
3336// Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization
3337// request if we need to undeoptimize.
3338void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
Sebastien Hertzed2be172014-08-19 15:33:43 +02003339 WriterMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003340 mirror::ArtMethod* m = FromMethodId(location->method_id);
3341 DCHECK(m != nullptr) << "No method for method id " << location->method_id;
Sebastien Hertzf3928792014-11-17 19:00:37 +01003342 DeoptimizationRequest::Kind deoptimization_kind = DeoptimizationRequest::kNothing;
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003343 for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003344 if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].Method() == m) {
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003345 VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
Sebastien Hertzf3928792014-11-17 19:00:37 +01003346 deoptimization_kind = gBreakpoints[i].GetDeoptimizationKind();
3347 DCHECK_EQ(deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization,
3348 Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003349 gBreakpoints.erase(gBreakpoints.begin() + i);
3350 break;
3351 }
3352 }
3353 const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
3354 if (existing_breakpoint == nullptr) {
3355 // There is no more breakpoint on this method: we need to undeoptimize.
Sebastien Hertzf3928792014-11-17 19:00:37 +01003356 if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) {
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003357 // This method required full deoptimization: we need to undeoptimize everything.
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003358 req->SetKind(DeoptimizationRequest::kFullUndeoptimization);
3359 req->SetMethod(nullptr);
Sebastien Hertzf3928792014-11-17 19:00:37 +01003360 } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003361 // This method required selective deoptimization: we need to undeoptimize only that method.
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003362 req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization);
3363 req->SetMethod(m);
Sebastien Hertzf3928792014-11-17 19:00:37 +01003364 } else {
3365 // This method had no need for deoptimization: do nothing.
3366 CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing);
3367 req->SetKind(DeoptimizationRequest::kNothing);
3368 req->SetMethod(nullptr);
Sebastien Hertza76a6d42014-03-20 16:40:17 +01003369 }
3370 } else {
3371 // There is at least one breakpoint for this method: we don't need to undeoptimize.
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07003372 req->SetKind(DeoptimizationRequest::kNothing);
3373 req->SetMethod(nullptr);
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003374 if (kIsDebugBuild) {
Sebastien Hertzf3928792014-11-17 19:00:37 +01003375 SanityCheckExistingBreakpoints(m, deoptimization_kind);
Sebastien Hertz4d1e9ab2014-09-18 16:03:34 +02003376 }
Elliott Hughes86964332012-02-15 19:37:42 -08003377 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003378}
3379
Daniel Mihalyieb076692014-08-22 17:33:31 +02003380bool Dbg::IsForcedInterpreterNeededForCallingImpl(Thread* thread, mirror::ArtMethod* m) {
3381 const SingleStepControl* const ssc = thread->GetSingleStepControl();
3382 if (ssc == nullptr) {
3383 // If we are not single-stepping, then we don't have to force interpreter.
3384 return false;
3385 }
3386 if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) {
3387 // If we are in interpreter only mode, then we don't have to force interpreter.
3388 return false;
3389 }
3390
3391 if (!m->IsNative() && !m->IsProxyMethod()) {
3392 // If we want to step into a method, then we have to force interpreter on that call.
3393 if (ssc->GetStepDepth() == JDWP::SD_INTO) {
3394 return true;
3395 }
3396 }
3397 return false;
3398}
3399
3400bool Dbg::IsForcedInterpreterNeededForResolutionImpl(Thread* thread, mirror::ArtMethod* m) {
3401 instrumentation::Instrumentation* const instrumentation =
3402 Runtime::Current()->GetInstrumentation();
3403 // If we are in interpreter only mode, then we don't have to force interpreter.
3404 if (instrumentation->InterpretOnly()) {
3405 return false;
3406 }
3407 // We can only interpret pure Java method.
3408 if (m->IsNative() || m->IsProxyMethod()) {
3409 return false;
3410 }
3411 const SingleStepControl* const ssc = thread->GetSingleStepControl();
3412 if (ssc != nullptr) {
3413 // If we want to step into a method, then we have to force interpreter on that call.
3414 if (ssc->GetStepDepth() == JDWP::SD_INTO) {
3415 return true;
3416 }
3417 // If we are stepping out from a static initializer, by issuing a step
3418 // in or step over, that was implicitly invoked by calling a static method,
3419 // then we need to step into that method. Having a lower stack depth than
3420 // the one the single step control has indicates that the step originates
3421 // from the static initializer.
3422 if (ssc->GetStepDepth() != JDWP::SD_OUT &&
3423 ssc->GetStackDepth() > GetStackDepth(thread)) {
3424 return true;
3425 }
3426 }
3427 // There are cases where we have to force interpreter on deoptimized methods,
3428 // because in some cases the call will not be performed by invoking an entry
3429 // point that has been replaced by the deoptimization, but instead by directly
3430 // invoking the compiled code of the method, for example.
3431 return instrumentation->IsDeoptimized(m);
3432}
3433
3434bool Dbg::IsForcedInstrumentationNeededForResolutionImpl(Thread* thread, mirror::ArtMethod* m) {
3435 // The upcall can be nullptr and in that case we don't need to do anything.
3436 if (m == nullptr) {
3437 return false;
3438 }
3439 instrumentation::Instrumentation* const instrumentation =
3440 Runtime::Current()->GetInstrumentation();
3441 // If we are in interpreter only mode, then we don't have to force interpreter.
3442 if (instrumentation->InterpretOnly()) {
3443 return false;
3444 }
3445 // We can only interpret pure Java method.
3446 if (m->IsNative() || m->IsProxyMethod()) {
3447 return false;
3448 }
3449 const SingleStepControl* const ssc = thread->GetSingleStepControl();
3450 if (ssc != nullptr) {
3451 // If we are stepping out from a static initializer, by issuing a step
3452 // out, that was implicitly invoked by calling a static method, then we
3453 // need to step into the caller of that method. Having a lower stack
3454 // depth than the one the single step control has indicates that the
3455 // step originates from the static initializer.
3456 if (ssc->GetStepDepth() == JDWP::SD_OUT &&
3457 ssc->GetStackDepth() > GetStackDepth(thread)) {
3458 return true;
3459 }
3460 }
3461 // If we are returning from a static intializer, that was implicitly
3462 // invoked by calling a static method and the caller is deoptimized,
3463 // then we have to deoptimize the stack without forcing interpreter
3464 // on the static method that was called originally. This problem can
3465 // be solved easily by forcing instrumentation on the called method,
3466 // because the instrumentation exit hook will recognise the need of
3467 // stack deoptimization by calling IsForcedInterpreterNeededForUpcall.
3468 return instrumentation->IsDeoptimized(m);
3469}
3470
3471bool Dbg::IsForcedInterpreterNeededForUpcallImpl(Thread* thread, mirror::ArtMethod* m) {
3472 // The upcall can be nullptr and in that case we don't need to do anything.
3473 if (m == nullptr) {
3474 return false;
3475 }
3476 instrumentation::Instrumentation* const instrumentation =
3477 Runtime::Current()->GetInstrumentation();
3478 // If we are in interpreter only mode, then we don't have to force interpreter.
3479 if (instrumentation->InterpretOnly()) {
3480 return false;
3481 }
3482 // We can only interpret pure Java method.
3483 if (m->IsNative() || m->IsProxyMethod()) {
3484 return false;
3485 }
3486 const SingleStepControl* const ssc = thread->GetSingleStepControl();
3487 if (ssc != nullptr) {
3488 // The debugger is not interested in what is happening under the level
3489 // of the step, thus we only force interpreter when we are not below of
3490 // the step.
3491 if (ssc->GetStackDepth() >= GetStackDepth(thread)) {
3492 return true;
3493 }
3494 }
3495 // We have to require stack deoptimization if the upcall is deoptimized.
3496 return instrumentation->IsDeoptimized(m);
3497}
3498
Jeff Hao449db332013-04-12 18:30:52 -07003499// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
3500// cause suspension if the thread is the current thread.
3501class ScopedThreadSuspension {
3502 public:
Ian Rogers33e95662013-05-20 20:29:14 -07003503 ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
Sebastien Hertz52d131d2014-03-13 16:17:40 +01003504 LOCKS_EXCLUDED(Locks::thread_list_lock_)
Ian Rogers33e95662013-05-20 20:29:14 -07003505 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
Ian Rogersf3d874c2014-07-17 18:52:42 -07003506 thread_(nullptr),
Jeff Hao449db332013-04-12 18:30:52 -07003507 error_(JDWP::ERR_NONE),
3508 self_suspend_(false),
Ian Rogers33e95662013-05-20 20:29:14 -07003509 other_suspend_(false) {
Jeff Hao449db332013-04-12 18:30:52 -07003510 ScopedObjectAccessUnchecked soa(self);
3511 {
3512 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07003513 thread_ = DecodeThread(soa, thread_id, &error_);
Jeff Hao449db332013-04-12 18:30:52 -07003514 }
3515 if (error_ == JDWP::ERR_NONE) {
3516 if (thread_ == soa.Self()) {
3517 self_suspend_ = true;
3518 } else {
3519 soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Sebastien Hertz6995c602014-09-09 12:10:13 +02003520 jobject thread_peer = Dbg::GetObjectRegistry()->GetJObject(thread_id);
Jeff Hao449db332013-04-12 18:30:52 -07003521 bool timed_out;
Ian Rogers4ad5cd32014-11-11 23:08:07 -08003522 ThreadList* thread_list = Runtime::Current()->GetThreadList();
3523 Thread* suspended_thread = thread_list->SuspendThreadByPeer(thread_peer, true, true,
3524 &timed_out);
Jeff Hao449db332013-04-12 18:30:52 -07003525 CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension);
Ian Rogersf3d874c2014-07-17 18:52:42 -07003526 if (suspended_thread == nullptr) {
Jeff Hao449db332013-04-12 18:30:52 -07003527 // Thread terminated from under us while suspending.
3528 error_ = JDWP::ERR_INVALID_THREAD;
3529 } else {
3530 CHECK_EQ(suspended_thread, thread_);
3531 other_suspend_ = true;
3532 }
3533 }
3534 }
Elliott Hughes2435a572012-02-17 16:07:41 -08003535 }
Elliott Hughes86964332012-02-15 19:37:42 -08003536
Jeff Hao449db332013-04-12 18:30:52 -07003537 Thread* GetThread() const {
3538 return thread_;
3539 }
3540
3541 JDWP::JdwpError GetError() const {
3542 return error_;
3543 }
3544
3545 ~ScopedThreadSuspension() {
3546 if (other_suspend_) {
3547 Runtime::Current()->GetThreadList()->Resume(thread_, true);
3548 }
3549 }
3550
3551 private:
3552 Thread* thread_;
3553 JDWP::JdwpError error_;
3554 bool self_suspend_;
3555 bool other_suspend_;
3556};
3557
3558JDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
3559 JDWP::JdwpStepDepth step_depth) {
3560 Thread* self = Thread::Current();
3561 ScopedThreadSuspension sts(self, thread_id);
3562 if (sts.GetError() != JDWP::ERR_NONE) {
3563 return sts.GetError();
3564 }
3565
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003566 // Work out what ArtMethod* we're in, the current line number, and how deep the stack currently
Elliott Hughes2435a572012-02-17 16:07:41 -08003567 // is for step-out.
Ian Rogers0399dde2012-06-06 17:09:28 -07003568 struct SingleStepStackVisitor : public StackVisitor {
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003569 explicit SingleStepStackVisitor(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
3570 : StackVisitor(thread, nullptr), stack_depth(0), method(nullptr), line_number(-1) {
Elliott Hughes86964332012-02-15 19:37:42 -08003571 }
Ian Rogersca190662012-06-26 15:45:57 -07003572
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003573 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
3574 // annotalysis.
3575 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003576 mirror::ArtMethod* m = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -07003577 if (!m->IsRuntimeMethod()) {
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003578 ++stack_depth;
3579 if (method == nullptr) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08003580 mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache();
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003581 method = m;
Ian Rogersc0542af2014-09-03 16:16:56 -07003582 if (dex_cache != nullptr) {
Ian Rogers4445a7e2012-10-05 17:19:13 -07003583 const DexFile& dex_file = *dex_cache->GetDexFile();
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003584 line_number = dex_file.GetLineNumFromPC(m, GetDexPc());
Elliott Hughes2435a572012-02-17 16:07:41 -08003585 }
Elliott Hughes86964332012-02-15 19:37:42 -08003586 }
3587 }
Elliott Hughes530fa002012-03-12 11:44:49 -07003588 return true;
Elliott Hughes86964332012-02-15 19:37:42 -08003589 }
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003590
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003591 int stack_depth;
3592 mirror::ArtMethod* method;
3593 int32_t line_number;
Elliott Hughes86964332012-02-15 19:37:42 -08003594 };
Jeff Hao449db332013-04-12 18:30:52 -07003595
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003596 Thread* const thread = sts.GetThread();
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003597 SingleStepStackVisitor visitor(thread);
Ian Rogers0399dde2012-06-06 17:09:28 -07003598 visitor.WalkStack();
Elliott Hughes86964332012-02-15 19:37:42 -08003599
Elliott Hughes2435a572012-02-17 16:07:41 -08003600 // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
Elliott Hughes2435a572012-02-17 16:07:41 -08003601 struct DebugCallbackContext {
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003602 explicit DebugCallbackContext(SingleStepControl* single_step_control_cb,
3603 int32_t line_number_cb, const DexFile::CodeItem* code_item)
Andreas Gampe277ccbd2014-11-03 21:36:10 -08003604 : single_step_control_(single_step_control_cb), line_number_(line_number_cb),
3605 code_item_(code_item), last_pc_valid(false), last_pc(0) {
Elliott Hughes2435a572012-02-17 16:07:41 -08003606 }
3607
Andreas Gampe277ccbd2014-11-03 21:36:10 -08003608 static bool Callback(void* raw_context, uint32_t address, uint32_t line_number_cb) {
Elliott Hughes2435a572012-02-17 16:07:41 -08003609 DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08003610 if (static_cast<int32_t>(line_number_cb) == context->line_number_) {
Elliott Hughes2435a572012-02-17 16:07:41 -08003611 if (!context->last_pc_valid) {
3612 // Everything from this address until the next line change is ours.
3613 context->last_pc = address;
3614 context->last_pc_valid = true;
3615 }
3616 // Otherwise, if we're already in a valid range for this line,
3617 // just keep going (shouldn't really happen)...
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003618 } else if (context->last_pc_valid) { // and the line number is new
Elliott Hughes2435a572012-02-17 16:07:41 -08003619 // Add everything from the last entry up until here to the set
3620 for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) {
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003621 context->single_step_control_->AddDexPc(dex_pc);
Elliott Hughes2435a572012-02-17 16:07:41 -08003622 }
3623 context->last_pc_valid = false;
3624 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003625 return false; // There may be multiple entries for any given line.
Elliott Hughes2435a572012-02-17 16:07:41 -08003626 }
3627
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003628 ~DebugCallbackContext() {
Elliott Hughes2435a572012-02-17 16:07:41 -08003629 // If the line number was the last in the position table...
3630 if (last_pc_valid) {
Sebastien Hertzbb43b432014-04-14 11:59:08 +02003631 size_t end = code_item_->insns_size_in_code_units_;
Elliott Hughes2435a572012-02-17 16:07:41 -08003632 for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) {
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003633 single_step_control_->AddDexPc(dex_pc);
Elliott Hughes2435a572012-02-17 16:07:41 -08003634 }
3635 }
3636 }
3637
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003638 SingleStepControl* const single_step_control_;
3639 const int32_t line_number_;
Sebastien Hertzbb43b432014-04-14 11:59:08 +02003640 const DexFile::CodeItem* const code_item_;
Elliott Hughes2435a572012-02-17 16:07:41 -08003641 bool last_pc_valid;
3642 uint32_t last_pc;
3643 };
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003644
3645 // Allocate single step.
Sebastien Hertz1558b572015-02-25 15:05:59 +01003646 SingleStepControl* single_step_control =
3647 new (std::nothrow) SingleStepControl(step_size, step_depth,
3648 visitor.stack_depth, visitor.method);
3649 if (single_step_control == nullptr) {
3650 LOG(ERROR) << "Failed to allocate SingleStepControl";
3651 return JDWP::ERR_OUT_OF_MEMORY;
3652 }
3653
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003654 mirror::ArtMethod* m = single_step_control->GetMethod();
3655 const int32_t line_number = visitor.line_number;
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003656 if (!m->IsNative()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003657 const DexFile::CodeItem* const code_item = m->GetCodeItem();
Sebastien Hertzbb43b432014-04-14 11:59:08 +02003658 DebugCallbackContext context(single_step_control, line_number, code_item);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003659 m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
Ian Rogersc0542af2014-09-03 16:16:56 -07003660 DebugCallbackContext::Callback, nullptr, &context);
Elliott Hughes3e2e1a22012-02-21 11:33:41 -08003661 }
Elliott Hughes2435a572012-02-17 16:07:41 -08003662
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003663 // Activate single-step in the thread.
3664 thread->ActivateSingleStepControl(single_step_control);
Elliott Hughes86964332012-02-15 19:37:42 -08003665
Elliott Hughes2435a572012-02-17 16:07:41 -08003666 if (VLOG_IS_ON(jdwp)) {
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003667 VLOG(jdwp) << "Single-step thread: " << *thread;
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003668 VLOG(jdwp) << "Single-step step size: " << single_step_control->GetStepSize();
3669 VLOG(jdwp) << "Single-step step depth: " << single_step_control->GetStepDepth();
3670 VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->GetMethod());
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003671 VLOG(jdwp) << "Single-step current line: " << line_number;
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003672 VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->GetStackDepth();
Elliott Hughes2435a572012-02-17 16:07:41 -08003673 VLOG(jdwp) << "Single-step dex_pc values:";
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003674 for (uint32_t dex_pc : single_step_control->GetDexPcs()) {
Sebastien Hertzbb43b432014-04-14 11:59:08 +02003675 VLOG(jdwp) << StringPrintf(" %#x", dex_pc);
Elliott Hughes2435a572012-02-17 16:07:41 -08003676 }
3677 }
3678
3679 return JDWP::ERR_NONE;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003680}
3681
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003682void Dbg::UnconfigureStep(JDWP::ObjectId thread_id) {
3683 ScopedObjectAccessUnchecked soa(Thread::Current());
3684 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07003685 JDWP::JdwpError error;
3686 Thread* thread = DecodeThread(soa, thread_id, &error);
Sebastien Hertz87118ed2013-11-26 17:57:18 +01003687 if (error == JDWP::ERR_NONE) {
Sebastien Hertz597c4f02015-01-26 17:37:14 +01003688 thread->DeactivateSingleStepControl();
Sebastien Hertz61b7f1b2013-11-15 15:59:30 +01003689 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003690}
3691
Elliott Hughes45651fd2012-02-21 15:48:20 -08003692static char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
3693 switch (tag) {
3694 default:
3695 LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
Ian Rogersfc787ec2014-10-09 21:56:44 -07003696 UNREACHABLE();
Elliott Hughes45651fd2012-02-21 15:48:20 -08003697
3698 // Primitives.
3699 case JDWP::JT_BYTE: return 'B';
3700 case JDWP::JT_CHAR: return 'C';
3701 case JDWP::JT_FLOAT: return 'F';
3702 case JDWP::JT_DOUBLE: return 'D';
3703 case JDWP::JT_INT: return 'I';
3704 case JDWP::JT_LONG: return 'J';
3705 case JDWP::JT_SHORT: return 'S';
3706 case JDWP::JT_VOID: return 'V';
3707 case JDWP::JT_BOOLEAN: return 'Z';
3708
3709 // Reference types.
3710 case JDWP::JT_ARRAY:
3711 case JDWP::JT_OBJECT:
3712 case JDWP::JT_STRING:
3713 case JDWP::JT_THREAD:
3714 case JDWP::JT_THREAD_GROUP:
3715 case JDWP::JT_CLASS_LOADER:
3716 case JDWP::JT_CLASS_OBJECT:
3717 return 'L';
3718 }
3719}
3720
Elliott Hughes88d63092013-01-09 09:55:54 -08003721JDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id,
3722 JDWP::RefTypeId class_id, JDWP::MethodId method_id,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003723 uint32_t arg_count, uint64_t* arg_values,
3724 JDWP::JdwpTag* arg_types, uint32_t options,
3725 JDWP::JdwpTag* pResultTag, uint64_t* pResultValue,
3726 JDWP::ObjectId* pExceptionId) {
Elliott Hughesd07986f2011-12-06 18:27:45 -08003727 ThreadList* thread_list = Runtime::Current()->GetThreadList();
3728
Ian Rogersc0542af2014-09-03 16:16:56 -07003729 Thread* targetThread = nullptr;
Sebastien Hertz1558b572015-02-25 15:05:59 +01003730 std::unique_ptr<DebugInvokeReq> req;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003731 Thread* self = Thread::Current();
Elliott Hughesd07986f2011-12-06 18:27:45 -08003732 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003733 ScopedObjectAccessUnchecked soa(self);
Ian Rogers50b35e22012-10-04 10:09:15 -07003734 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07003735 JDWP::JdwpError error;
3736 targetThread = DecodeThread(soa, thread_id, &error);
Elliott Hughes221229c2013-01-08 18:17:50 -08003737 if (error != JDWP::ERR_NONE) {
3738 LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
3739 return error;
Elliott Hughesd07986f2011-12-06 18:27:45 -08003740 }
Sebastien Hertz1558b572015-02-25 15:05:59 +01003741 if (targetThread->GetInvokeReq() != nullptr) {
3742 // Thread is already invoking a method on behalf of the debugger.
3743 LOG(ERROR) << "InvokeMethod request for thread already invoking a method: " << *targetThread;
3744 return JDWP::ERR_ALREADY_INVOKING;
3745 }
3746 if (!targetThread->IsReadyForDebugInvoke()) {
3747 // Thread is not suspended by an event so it cannot invoke a method.
Elliott Hughesd07986f2011-12-06 18:27:45 -08003748 LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
3749 return JDWP::ERR_INVALID_THREAD;
3750 }
3751
3752 /*
3753 * We currently have a bug where we don't successfully resume the
3754 * target thread if the suspend count is too deep. We're expected to
3755 * require one "resume" for each "suspend", but when asked to execute
3756 * a method we have to resume fully and then re-suspend it back to the
3757 * same level. (The easiest way to cause this is to type "suspend"
3758 * multiple times in jdb.)
3759 *
3760 * It's unclear what this means when the event specifies "resume all"
3761 * and some threads are suspended more deeply than others. This is
3762 * a rare problem, so for now we just prevent it from hanging forever
3763 * by rejecting the method invocation request. Without this, we will
3764 * be stuck waiting on a suspended thread.
3765 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003766 int suspend_count;
3767 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003768 MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003769 suspend_count = targetThread->GetSuspendCount();
3770 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08003771 if (suspend_count > 1) {
3772 LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003773 return JDWP::ERR_THREAD_SUSPENDED; // Probably not expected here.
Elliott Hughesd07986f2011-12-06 18:27:45 -08003774 }
3775
Ian Rogersc0542af2014-09-03 16:16:56 -07003776 mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id, &error);
3777 if (error != JDWP::ERR_NONE) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08003778 return JDWP::ERR_INVALID_OBJECT;
3779 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08003780
Sebastien Hertz1558b572015-02-25 15:05:59 +01003781 gRegistry->Get<mirror::Object*>(thread_id, &error);
Ian Rogersc0542af2014-09-03 16:16:56 -07003782 if (error != JDWP::ERR_NONE) {
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08003783 return JDWP::ERR_INVALID_OBJECT;
3784 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08003785
Ian Rogersc0542af2014-09-03 16:16:56 -07003786 mirror::Class* c = DecodeClass(class_id, &error);
3787 if (c == nullptr) {
3788 return error;
Elliott Hughes3f4d58f2012-02-18 20:05:37 -08003789 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08003790
Brian Carlstromea46f952013-07-30 01:26:50 -07003791 mirror::ArtMethod* m = FromMethodId(method_id);
Ian Rogersc0542af2014-09-03 16:16:56 -07003792 if (m->IsStatic() != (receiver == nullptr)) {
Elliott Hughes45651fd2012-02-21 15:48:20 -08003793 return JDWP::ERR_INVALID_METHODID;
3794 }
3795 if (m->IsStatic()) {
3796 if (m->GetDeclaringClass() != c) {
3797 return JDWP::ERR_INVALID_METHODID;
3798 }
3799 } else {
3800 if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
3801 return JDWP::ERR_INVALID_METHODID;
3802 }
3803 }
3804
3805 // Check the argument list matches the method.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003806 uint32_t shorty_len = 0;
3807 const char* shorty = m->GetShorty(&shorty_len);
3808 if (shorty_len - 1 != arg_count) {
Elliott Hughes45651fd2012-02-21 15:48:20 -08003809 return JDWP::ERR_ILLEGAL_ARGUMENT;
3810 }
Elliott Hughes09201632013-04-15 15:50:07 -07003811
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003812 {
3813 StackHandleScope<3> hs(soa.Self());
Ian Rogersa0485602014-12-02 15:48:04 -08003814 HandleWrapper<mirror::ArtMethod> h_m(hs.NewHandleWrapper(&m));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003815 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver));
3816 HandleWrapper<mirror::Class> h_klass(hs.NewHandleWrapper(&c));
3817 const DexFile::TypeList* types = m->GetParameterTypeList();
3818 for (size_t i = 0; i < arg_count; ++i) {
3819 if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
Elliott Hughes09201632013-04-15 15:50:07 -07003820 return JDWP::ERR_ILLEGAL_ARGUMENT;
3821 }
3822
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003823 if (shorty[i + 1] == 'L') {
3824 // Did we really get an argument of an appropriate reference type?
Ian Rogersa0485602014-12-02 15:48:04 -08003825 mirror::Class* parameter_type =
3826 h_m->GetClassFromTypeIndex(types->GetTypeItem(i).type_idx_, true);
Ian Rogersc0542af2014-09-03 16:16:56 -07003827 mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i], &error);
3828 if (error != JDWP::ERR_NONE) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003829 return JDWP::ERR_INVALID_OBJECT;
3830 }
Ian Rogersc0542af2014-09-03 16:16:56 -07003831 if (argument != nullptr && !argument->InstanceOf(parameter_type)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003832 return JDWP::ERR_ILLEGAL_ARGUMENT;
3833 }
3834
3835 // Turn the on-the-wire ObjectId into a jobject.
3836 jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
3837 v.l = gRegistry->GetJObject(arg_values[i]);
3838 }
Elliott Hughes09201632013-04-15 15:50:07 -07003839 }
Elliott Hughes45651fd2012-02-21 15:48:20 -08003840 }
3841
Sebastien Hertz1558b572015-02-25 15:05:59 +01003842 // Allocates a DebugInvokeReq.
3843 req.reset(new (std::nothrow) DebugInvokeReq(receiver, c, m, options, arg_values, arg_count));
3844 if (req.get() == nullptr) {
3845 LOG(ERROR) << "Failed to allocate DebugInvokeReq";
3846 return JDWP::ERR_OUT_OF_MEMORY;
3847 }
3848
3849 // Attach the DebugInvokeReq to the target thread so it executes the method when
3850 // it is resumed. Once the invocation completes, it will detach it and signal us
3851 // before suspending itself.
3852 targetThread->SetDebugInvokeReq(req.get());
Elliott Hughesd07986f2011-12-06 18:27:45 -08003853 }
3854
3855 // The fact that we've released the thread list lock is a bit risky --- if the thread goes
3856 // away we're sitting high and dry -- but we must release this before the ResumeAllThreads
3857 // call, and it's unwise to hold it during WaitForSuspend.
3858
3859 {
3860 /*
3861 * We change our (JDWP thread) status, which should be THREAD_RUNNING,
Elliott Hughes81ff3182012-03-23 20:35:56 -07003862 * so we can suspend for a GC if the invoke request causes us to
Elliott Hughesd07986f2011-12-06 18:27:45 -08003863 * run out of memory. It's also a good idea to change it before locking
3864 * the invokeReq mutex, although that should never be held for long.
3865 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003866 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
Elliott Hughesd07986f2011-12-06 18:27:45 -08003867
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003868 VLOG(jdwp) << " Transferring control to event thread";
Elliott Hughesd07986f2011-12-06 18:27:45 -08003869 {
Sebastien Hertzd38667a2013-11-25 15:43:54 +01003870 MutexLock mu(self, req->lock);
Elliott Hughesd07986f2011-12-06 18:27:45 -08003871
3872 if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003873 VLOG(jdwp) << " Resuming all threads";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003874 thread_list->UndoDebuggerSuspensions();
Elliott Hughesd07986f2011-12-06 18:27:45 -08003875 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003876 VLOG(jdwp) << " Resuming event thread only";
Elliott Hughesd07986f2011-12-06 18:27:45 -08003877 thread_list->Resume(targetThread, true);
3878 }
3879
Sebastien Hertz2bf93f42015-01-09 18:44:05 +01003880 // The target thread is resumed but needs the JDWP token we're holding.
3881 // We release it now and will acquire it again when the invocation is
3882 // complete and the target thread suspends itself.
3883 gJdwpState->ReleaseJdwpTokenForCommand();
3884
Elliott Hughesd07986f2011-12-06 18:27:45 -08003885 // Wait for the request to finish executing.
Sebastien Hertz1558b572015-02-25 15:05:59 +01003886 while (targetThread->GetInvokeReq() != nullptr) {
Sebastien Hertzd38667a2013-11-25 15:43:54 +01003887 req->cond.Wait(self);
Elliott Hughesd07986f2011-12-06 18:27:45 -08003888 }
3889 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003890 VLOG(jdwp) << " Control has returned from event thread";
Elliott Hughesd07986f2011-12-06 18:27:45 -08003891
3892 /* wait for thread to re-suspend itself */
Brian Carlstromdf629502013-07-17 22:39:56 -07003893 SuspendThread(thread_id, false /* request_suspension */);
Sebastien Hertz2bf93f42015-01-09 18:44:05 +01003894
3895 // Now the thread is suspended again, we can re-acquire the JDWP token.
3896 gJdwpState->AcquireJdwpTokenForCommand();
3897
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003898 self->TransitionFromSuspendedToRunnable();
Elliott Hughesd07986f2011-12-06 18:27:45 -08003899 }
3900
3901 /*
3902 * Suspend the threads. We waited for the target thread to suspend
3903 * itself, so all we need to do is suspend the others.
3904 *
Sebastien Hertz2bf93f42015-01-09 18:44:05 +01003905 * The SuspendAllForDebugger() call will double-suspend the event thread,
Elliott Hughesd07986f2011-12-06 18:27:45 -08003906 * so we want to resume the target thread once to keep the books straight.
3907 */
3908 if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003909 self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003910 VLOG(jdwp) << " Suspending all threads";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003911 thread_list->SuspendAllForDebugger();
3912 self->TransitionFromSuspendedToRunnable();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003913 VLOG(jdwp) << " Resuming event thread to balance the count";
Elliott Hughesd07986f2011-12-06 18:27:45 -08003914 thread_list->Resume(targetThread, true);
3915 }
3916
3917 // Copy the result.
3918 *pResultTag = req->result_tag;
Sebastien Hertz1558b572015-02-25 15:05:59 +01003919 *pResultValue = req->result_value;
Elliott Hughesd07986f2011-12-06 18:27:45 -08003920 *pExceptionId = req->exception;
3921 return req->error;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003922}
3923
3924void Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003925 ScopedObjectAccess soa(Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -08003926
Elliott Hughes81ff3182012-03-23 20:35:56 -07003927 // We can be called while an exception is pending. We need
Elliott Hughesd07986f2011-12-06 18:27:45 -08003928 // to preserve that across the method invocation.
Sebastien Hertz1558b572015-02-25 15:05:59 +01003929 StackHandleScope<4> hs(soa.Self());
3930 auto old_exception = hs.NewHandle<mirror::Throwable>(soa.Self()->GetException());
3931 soa.Self()->ClearException();
Elliott Hughesd07986f2011-12-06 18:27:45 -08003932
3933 // Translate the method through the vtable, unless the debugger wants to suppress it.
Sebastien Hertz1558b572015-02-25 15:05:59 +01003934 MutableHandle<mirror::ArtMethod> m(hs.NewHandle(pReq->method.Read()));
3935 if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver.Read() != nullptr) {
3936 mirror::ArtMethod* actual_method = pReq->klass.Read()->FindVirtualMethodForVirtualOrInterface(m.Get());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003937 if (actual_method != m.Get()) {
Sebastien Hertz1558b572015-02-25 15:05:59 +01003938 VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m.Get())
3939 << " to " << PrettyMethod(actual_method);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003940 m.Assign(actual_method);
Elliott Hughes45651fd2012-02-21 15:48:20 -08003941 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08003942 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003943 VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m.Get())
Sebastien Hertz1558b572015-02-25 15:05:59 +01003944 << " receiver=" << pReq->receiver.Read()
Sebastien Hertzd38667a2013-11-25 15:43:54 +01003945 << " arg_count=" << pReq->arg_count;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003946 CHECK(m.Get() != nullptr);
Elliott Hughesd07986f2011-12-06 18:27:45 -08003947
3948 CHECK_EQ(sizeof(jvalue), sizeof(uint64_t));
3949
Sebastien Hertz1558b572015-02-25 15:05:59 +01003950 JValue result = InvokeWithJValues(soa, pReq->receiver.Read(), soa.EncodeMethod(m.Get()),
3951 reinterpret_cast<jvalue*>(pReq->arg_values));
Elliott Hughesd07986f2011-12-06 18:27:45 -08003952
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003953 pReq->result_tag = BasicTagFromDescriptor(m.Get()->GetShorty());
Sebastien Hertz1558b572015-02-25 15:05:59 +01003954 const bool is_object_result = (pReq->result_tag == JDWP::JT_OBJECT);
3955 Handle<mirror::Object> object_result = hs.NewHandle(is_object_result ? result.GetL() : nullptr);
3956 Handle<mirror::Throwable> exception = hs.NewHandle(soa.Self()->GetException());
3957 soa.Self()->ClearException();
3958 pReq->exception = gRegistry->Add(exception.Get());
Elliott Hughesd07986f2011-12-06 18:27:45 -08003959 if (pReq->exception != 0) {
Sebastien Hertz1558b572015-02-25 15:05:59 +01003960 VLOG(jdwp) << " JDWP invocation returning with exception=" << exception.Get()
3961 << " " << exception->Dump();
3962 pReq->result_value = 0;
3963 } else if (is_object_result) {
Elliott Hughesd07986f2011-12-06 18:27:45 -08003964 /* if no exception thrown, examine object result more closely */
Sebastien Hertz1558b572015-02-25 15:05:59 +01003965 JDWP::JdwpTag new_tag = TagFromObject(soa, object_result.Get());
Elliott Hughesd07986f2011-12-06 18:27:45 -08003966 if (new_tag != pReq->result_tag) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003967 VLOG(jdwp) << " JDWP promoted result from " << pReq->result_tag << " to " << new_tag;
Elliott Hughesd07986f2011-12-06 18:27:45 -08003968 pReq->result_tag = new_tag;
3969 }
3970
Sebastien Hertz1558b572015-02-25 15:05:59 +01003971 // Register the object in the registry and reference its ObjectId. This ensures
3972 // GC safety and prevents from accessing stale reference if the object is moved.
3973 pReq->result_value = gRegistry->Add(object_result.Get());
3974 } else {
3975 // Primitive result.
3976 DCHECK(IsPrimitiveTag(pReq->result_tag));
3977 pReq->result_value = result.GetJ();
Elliott Hughesd07986f2011-12-06 18:27:45 -08003978 }
3979
Ian Rogersc0542af2014-09-03 16:16:56 -07003980 if (old_exception.Get() != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +00003981 soa.Self()->SetException(old_exception.Get());
Elliott Hughesd07986f2011-12-06 18:27:45 -08003982 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003983}
3984
Elliott Hughesd07986f2011-12-06 18:27:45 -08003985/*
Elliott Hughes4b9702c2013-02-20 18:13:24 -08003986 * "request" contains a full JDWP packet, possibly with multiple chunks. We
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07003987 * need to process each, accumulate the replies, and ship the whole thing
3988 * back.
3989 *
3990 * Returns "true" if we have a reply. The reply buffer is newly allocated,
3991 * and includes the chunk type/length, followed by the data.
3992 *
Elliott Hughes3d30d9b2011-12-07 17:35:48 -08003993 * OLD-TODO: we currently assume that the request and reply include a single
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07003994 * chunk. If this becomes inconvenient we will need to adapt.
3995 */
Ian Rogersc0542af2014-09-03 16:16:56 -07003996bool Dbg::DdmHandlePacket(JDWP::Request* request, uint8_t** pReplyBuf, int* pReplyLen) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07003997 Thread* self = Thread::Current();
3998 JNIEnv* env = self->GetJniEnv();
3999
Ian Rogersc0542af2014-09-03 16:16:56 -07004000 uint32_t type = request->ReadUnsigned32("type");
4001 uint32_t length = request->ReadUnsigned32("length");
Elliott Hughes4b9702c2013-02-20 18:13:24 -08004002
4003 // Create a byte[] corresponding to 'request'.
Ian Rogersc0542af2014-09-03 16:16:56 -07004004 size_t request_length = request->size();
Elliott Hughes4b9702c2013-02-20 18:13:24 -08004005 ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length));
Ian Rogersc0542af2014-09-03 16:16:56 -07004006 if (dataArray.get() == nullptr) {
Elliott Hughes4b9702c2013-02-20 18:13:24 -08004007 LOG(WARNING) << "byte[] allocation failed: " << request_length;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004008 env->ExceptionClear();
4009 return false;
4010 }
Ian Rogersc0542af2014-09-03 16:16:56 -07004011 env->SetByteArrayRegion(dataArray.get(), 0, request_length,
4012 reinterpret_cast<const jbyte*>(request->data()));
4013 request->Skip(request_length);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004014
4015 // Run through and find all chunks. [Currently just find the first.]
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004016 ScopedByteArrayRO contents(env, dataArray.get());
Elliott Hughes4b9702c2013-02-20 18:13:24 -08004017 if (length != request_length) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08004018 LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%zd)", length, request_length);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004019 return false;
4020 }
4021
4022 // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
Elliott Hugheseac76672012-05-24 21:56:51 -07004023 ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
4024 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
Elliott Hughes4b9702c2013-02-20 18:13:24 -08004025 type, dataArray.get(), 0, length));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004026 if (env->ExceptionCheck()) {
4027 LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
4028 env->ExceptionDescribe();
4029 env->ExceptionClear();
4030 return false;
4031 }
4032
Ian Rogersc0542af2014-09-03 16:16:56 -07004033 if (chunk.get() == nullptr) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004034 return false;
4035 }
4036
4037 /*
4038 * Pull the pieces out of the chunk. We copy the results into a
4039 * newly-allocated buffer that the caller can free. We don't want to
4040 * continue using the Chunk object because nothing has a reference to it.
4041 *
4042 * We could avoid this by returning type/data/offset/length and having
4043 * the caller be aware of the object lifetime issues, but that
Elliott Hughes81ff3182012-03-23 20:35:56 -07004044 * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004045 * if we have responses for multiple chunks.
4046 *
4047 * So we're pretty much stuck with copying data around multiple times.
4048 */
Elliott Hugheseac76672012-05-24 21:56:51 -07004049 ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
Elliott Hughes4b9702c2013-02-20 18:13:24 -08004050 jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
Elliott Hugheseac76672012-05-24 21:56:51 -07004051 length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
Elliott Hugheseac76672012-05-24 21:56:51 -07004052 type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004053
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08004054 VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length);
Ian Rogersc0542af2014-09-03 16:16:56 -07004055 if (length == 0 || replyData.get() == nullptr) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004056 return false;
4057 }
4058
Elliott Hughes4b9702c2013-02-20 18:13:24 -08004059 const int kChunkHdrLen = 8;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004060 uint8_t* reply = new uint8_t[length + kChunkHdrLen];
Ian Rogersc0542af2014-09-03 16:16:56 -07004061 if (reply == nullptr) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004062 LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
4063 return false;
4064 }
Elliott Hughesf7c3b662011-10-27 12:04:56 -07004065 JDWP::Set4BE(reply + 0, type);
4066 JDWP::Set4BE(reply + 4, length);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004067 env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004068
4069 *pReplyBuf = reply;
4070 *pReplyLen = length + kChunkHdrLen;
4071
Elliott Hughes4b9702c2013-02-20 18:13:24 -08004072 VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07004073 return true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004074}
4075
Elliott Hughesa2155262011-11-16 16:26:58 -08004076void Dbg::DdmBroadcast(bool connect) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08004077 VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
Elliott Hughes47fce012011-10-25 18:37:19 -07004078
4079 Thread* self = Thread::Current();
Ian Rogers50b35e22012-10-04 10:09:15 -07004080 if (self->GetState() != kRunnable) {
4081 LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
4082 /* try anyway? */
Elliott Hughes47fce012011-10-25 18:37:19 -07004083 }
4084
4085 JNIEnv* env = self->GetJniEnv();
Elliott Hughes47fce012011-10-25 18:37:19 -07004086 jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
Elliott Hugheseac76672012-05-24 21:56:51 -07004087 env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
4088 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
4089 event);
Elliott Hughes47fce012011-10-25 18:37:19 -07004090 if (env->ExceptionCheck()) {
4091 LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
4092 env->ExceptionDescribe();
4093 env->ExceptionClear();
4094 }
4095}
4096
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004097void Dbg::DdmConnected() {
Elliott Hughesa2155262011-11-16 16:26:58 -08004098 Dbg::DdmBroadcast(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004099}
4100
4101void Dbg::DdmDisconnected() {
Elliott Hughesa2155262011-11-16 16:26:58 -08004102 Dbg::DdmBroadcast(false);
Elliott Hughes47fce012011-10-25 18:37:19 -07004103 gDdmThreadNotification = false;
4104}
4105
4106/*
Elliott Hughes82188472011-11-07 18:11:48 -08004107 * Send a notification when a thread starts, stops, or changes its name.
Elliott Hughes47fce012011-10-25 18:37:19 -07004108 *
4109 * Because we broadcast the full set of threads when the notifications are
4110 * first enabled, it's possible for "thread" to be actively executing.
4111 */
Elliott Hughes82188472011-11-07 18:11:48 -08004112void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
Elliott Hughes47fce012011-10-25 18:37:19 -07004113 if (!gDdmThreadNotification) {
4114 return;
4115 }
4116
Elliott Hughes82188472011-11-07 18:11:48 -08004117 if (type == CHUNK_TYPE("THDE")) {
Elliott Hughes47fce012011-10-25 18:37:19 -07004118 uint8_t buf[4];
Ian Rogersd9c4fc92013-10-01 19:45:43 -07004119 JDWP::Set4BE(&buf[0], t->GetThreadId());
Elliott Hughes47fce012011-10-25 18:37:19 -07004120 Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
Elliott Hughes82188472011-11-07 18:11:48 -08004121 } else {
4122 CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004123 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07004124 StackHandleScope<1> hs(soa.Self());
4125 Handle<mirror::String> name(hs.NewHandle(t->GetThreadName(soa)));
Ian Rogersc0542af2014-09-03 16:16:56 -07004126 size_t char_count = (name.Get() != nullptr) ? name->GetLength() : 0;
4127 const jchar* chars = (name.Get() != nullptr) ? name->GetCharArray()->GetData() : nullptr;
Elliott Hughes82188472011-11-07 18:11:48 -08004128
Elliott Hughes21f32d72011-11-09 17:44:13 -08004129 std::vector<uint8_t> bytes;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07004130 JDWP::Append4BE(bytes, t->GetThreadId());
Elliott Hughes545a0642011-11-08 19:10:03 -08004131 JDWP::AppendUtf16BE(bytes, chars, char_count);
Elliott Hughes21f32d72011-11-09 17:44:13 -08004132 CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
4133 Dbg::DdmSendChunk(type, bytes);
Elliott Hughes47fce012011-10-25 18:37:19 -07004134 }
4135}
4136
Elliott Hughes47fce012011-10-25 18:37:19 -07004137void Dbg::DdmSetThreadNotification(bool enable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004138 // Enable/disable thread notifications.
Elliott Hughes47fce012011-10-25 18:37:19 -07004139 gDdmThreadNotification = enable;
4140 if (enable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004141 // Suspend the VM then post thread start notifications for all threads. Threads attaching will
4142 // see a suspension in progress and block until that ends. They then post their own start
4143 // notification.
4144 SuspendVM();
4145 std::list<Thread*> threads;
Ian Rogers50b35e22012-10-04 10:09:15 -07004146 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004147 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004148 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004149 threads = Runtime::Current()->GetThreadList()->GetList();
4150 }
4151 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004152 ScopedObjectAccess soa(self);
Mathieu Chartier02e25112013-08-14 16:14:24 -07004153 for (Thread* thread : threads) {
4154 Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR"));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004155 }
4156 }
4157 ResumeVM();
Elliott Hughes47fce012011-10-25 18:37:19 -07004158 }
4159}
4160
Elliott Hughesa2155262011-11-16 16:26:58 -08004161void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
Elliott Hughesc0f09332012-03-26 13:27:06 -07004162 if (IsDebuggerActive()) {
Sebastien Hertz6995c602014-09-09 12:10:13 +02004163 gJdwpState->PostThreadChange(t, type == CHUNK_TYPE("THCR"));
Elliott Hughes47fce012011-10-25 18:37:19 -07004164 }
Elliott Hughes82188472011-11-07 18:11:48 -08004165 Dbg::DdmSendThreadNotification(t, type);
Elliott Hughes47fce012011-10-25 18:37:19 -07004166}
4167
4168void Dbg::PostThreadStart(Thread* t) {
Elliott Hughesa2155262011-11-16 16:26:58 -08004169 Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
Elliott Hughes47fce012011-10-25 18:37:19 -07004170}
4171
4172void Dbg::PostThreadDeath(Thread* t) {
Elliott Hughesa2155262011-11-16 16:26:58 -08004173 Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004174}
4175
Elliott Hughes82188472011-11-07 18:11:48 -08004176void Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) {
Ian Rogersc0542af2014-09-03 16:16:56 -07004177 CHECK(buf != nullptr);
Elliott Hughes3bb81562011-10-21 18:52:59 -07004178 iovec vec[1];
4179 vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
4180 vec[0].iov_len = byte_count;
4181 Dbg::DdmSendChunkV(type, vec, 1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004182}
4183
Elliott Hughes21f32d72011-11-09 17:44:13 -08004184void Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) {
4185 DdmSendChunk(type, bytes.size(), &bytes[0]);
4186}
4187
Brian Carlstromf5293522013-07-19 00:24:00 -07004188void Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
Ian Rogersc0542af2014-09-03 16:16:56 -07004189 if (gJdwpState == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08004190 VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
Elliott Hughes3bb81562011-10-21 18:52:59 -07004191 } else {
Elliott Hughescccd84f2011-12-05 16:51:54 -08004192 gJdwpState->DdmSendChunkV(type, iov, iov_count);
Elliott Hughes3bb81562011-10-21 18:52:59 -07004193 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -07004194}
4195
Mathieu Chartierad466ad2015-01-08 16:28:08 -08004196JDWP::JdwpState* Dbg::GetJdwpState() {
4197 return gJdwpState;
4198}
4199
Elliott Hughes767a1472011-10-26 18:49:02 -07004200int Dbg::DdmHandleHpifChunk(HpifWhen when) {
4201 if (when == HPIF_WHEN_NOW) {
Elliott Hughes7162ad92011-10-27 14:08:42 -07004202 DdmSendHeapInfo(when);
Elliott Hughes767a1472011-10-26 18:49:02 -07004203 return true;
4204 }
4205
4206 if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
4207 LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
4208 return false;
4209 }
4210
4211 gDdmHpifWhen = when;
4212 return true;
4213}
4214
4215bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
4216 if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
4217 LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
4218 return false;
4219 }
4220
4221 if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
4222 LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
4223 return false;
4224 }
4225
4226 if (native) {
4227 gDdmNhsgWhen = when;
4228 gDdmNhsgWhat = what;
4229 } else {
4230 gDdmHpsgWhen = when;
4231 gDdmHpsgWhat = what;
4232 }
4233 return true;
4234}
4235
Elliott Hughes7162ad92011-10-27 14:08:42 -07004236void Dbg::DdmSendHeapInfo(HpifWhen reason) {
4237 // If there's a one-shot 'when', reset it.
4238 if (reason == gDdmHpifWhen) {
4239 if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
4240 gDdmHpifWhen = HPIF_WHEN_NEVER;
4241 }
4242 }
4243
4244 /*
4245 * Chunk HPIF (client --> server)
4246 *
4247 * Heap Info. General information about the heap,
4248 * suitable for a summary display.
4249 *
4250 * [u4]: number of heaps
4251 *
4252 * For each heap:
4253 * [u4]: heap ID
4254 * [u8]: timestamp in ms since Unix epoch
4255 * [u1]: capture reason (same as 'when' value from server)
4256 * [u4]: max heap size in bytes (-Xmx)
4257 * [u4]: current heap size in bytes
4258 * [u4]: current number of bytes allocated
4259 * [u4]: current number of objects allocated
4260 */
4261 uint8_t heap_count = 1;
Ian Rogers1d54e732013-05-02 21:10:01 -07004262 gc::Heap* heap = Runtime::Current()->GetHeap();
Elliott Hughes21f32d72011-11-09 17:44:13 -08004263 std::vector<uint8_t> bytes;
Elliott Hughes545a0642011-11-08 19:10:03 -08004264 JDWP::Append4BE(bytes, heap_count);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004265 JDWP::Append4BE(bytes, 1); // Heap id (bogus; we only have one heap).
Elliott Hughes545a0642011-11-08 19:10:03 -08004266 JDWP::Append8BE(bytes, MilliTime());
4267 JDWP::Append1BE(bytes, reason);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004268 JDWP::Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes.
4269 JDWP::Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004270 JDWP::Append4BE(bytes, heap->GetBytesAllocated());
4271 JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
Elliott Hughes21f32d72011-11-09 17:44:13 -08004272 CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
4273 Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes);
Elliott Hughes767a1472011-10-26 18:49:02 -07004274}
4275
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004276enum HpsgSolidity {
4277 SOLIDITY_FREE = 0,
4278 SOLIDITY_HARD = 1,
4279 SOLIDITY_SOFT = 2,
4280 SOLIDITY_WEAK = 3,
4281 SOLIDITY_PHANTOM = 4,
4282 SOLIDITY_FINALIZABLE = 5,
4283 SOLIDITY_SWEEP = 6,
4284};
4285
4286enum HpsgKind {
4287 KIND_OBJECT = 0,
4288 KIND_CLASS_OBJECT = 1,
4289 KIND_ARRAY_1 = 2,
4290 KIND_ARRAY_2 = 3,
4291 KIND_ARRAY_4 = 4,
4292 KIND_ARRAY_8 = 5,
4293 KIND_UNKNOWN = 6,
4294 KIND_NATIVE = 7,
4295};
4296
4297#define HPSG_PARTIAL (1<<7)
4298#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
4299
Ian Rogers30fab402012-01-23 15:43:46 -08004300class HeapChunkContext {
4301 public:
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004302 // Maximum chunk size. Obtain this from the formula:
4303 // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
4304 HeapChunkContext(bool merge, bool native)
Ian Rogers30fab402012-01-23 15:43:46 -08004305 : buf_(16384 - 16),
4306 type_(0),
Mathieu Chartier36dab362014-07-30 14:59:56 -07004307 chunk_overhead_(0) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004308 Reset();
4309 if (native) {
Ian Rogers30fab402012-01-23 15:43:46 -08004310 type_ = CHUNK_TYPE("NHSG");
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004311 } else {
Ian Rogers30fab402012-01-23 15:43:46 -08004312 type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004313 }
4314 }
4315
4316 ~HeapChunkContext() {
Ian Rogers30fab402012-01-23 15:43:46 -08004317 if (p_ > &buf_[0]) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004318 Flush();
4319 }
4320 }
4321
Mathieu Chartier36dab362014-07-30 14:59:56 -07004322 void SetChunkOverhead(size_t chunk_overhead) {
4323 chunk_overhead_ = chunk_overhead;
4324 }
4325
4326 void ResetStartOfNextChunk() {
4327 startOfNextMemoryChunk_ = nullptr;
4328 }
4329
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004330 void EnsureHeader(const void* chunk_ptr) {
Ian Rogers30fab402012-01-23 15:43:46 -08004331 if (!needHeader_) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004332 return;
4333 }
4334
4335 // Start a new HPSx chunk.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004336 JDWP::Write4BE(&p_, 1); // Heap id (bogus; we only have one heap).
4337 JDWP::Write1BE(&p_, 8); // Size of allocation unit, in bytes.
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004338
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004339 JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start.
4340 JDWP::Write4BE(&p_, 0); // offset of this piece (relative to the virtual address).
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004341 // [u4]: length of piece, in allocation units
4342 // 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 -08004343 pieceLenField_ = p_;
4344 JDWP::Write4BE(&p_, 0x55555555);
4345 needHeader_ = false;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004346 }
4347
Ian Rogersb726dcb2012-09-05 08:57:23 -07004348 void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07004349 if (pieceLenField_ == nullptr) {
Ian Rogersd636b062013-01-18 17:51:18 -08004350 // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
4351 CHECK(needHeader_);
4352 return;
4353 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004354 // Patch the "length of piece" field.
Ian Rogers30fab402012-01-23 15:43:46 -08004355 CHECK_LE(&buf_[0], pieceLenField_);
4356 CHECK_LE(pieceLenField_, p_);
4357 JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004358
Ian Rogers30fab402012-01-23 15:43:46 -08004359 Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004360 Reset();
4361 }
4362
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004363 static void HeapChunkJavaCallback(void* start, void* end, size_t used_bytes, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07004364 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
4365 Locks::mutator_lock_) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004366 reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkJavaCallback(start, end, used_bytes);
4367 }
4368
4369 static void HeapChunkNativeCallback(void* start, void* end, size_t used_bytes, void* arg)
4370 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4371 reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkNativeCallback(start, end, used_bytes);
Elliott Hughesa2155262011-11-16 16:26:58 -08004372 }
4373
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004374 private:
Elliott Hughesa2155262011-11-16 16:26:58 -08004375 enum { ALLOCATION_UNIT_SIZE = 8 };
4376
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004377 void Reset() {
Ian Rogers30fab402012-01-23 15:43:46 -08004378 p_ = &buf_[0];
Mathieu Chartier36dab362014-07-30 14:59:56 -07004379 ResetStartOfNextChunk();
Ian Rogers30fab402012-01-23 15:43:46 -08004380 totalAllocationUnits_ = 0;
4381 needHeader_ = true;
Ian Rogersc0542af2014-09-03 16:16:56 -07004382 pieceLenField_ = nullptr;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004383 }
4384
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004385 bool IsNative() const {
4386 return type_ == CHUNK_TYPE("NHSG");
4387 }
4388
4389 // Returns true if the object is not an empty chunk.
4390 bool ProcessRecord(void* start, size_t used_bytes) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers30fab402012-01-23 15:43:46 -08004391 // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
4392 // in the following code not to allocate memory, by ensuring buf_ is of the correct size
Ian Rogers15bf2d32012-08-28 17:33:04 -07004393 if (used_bytes == 0) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004394 if (start == nullptr) {
4395 // Reset for start of new heap.
4396 startOfNextMemoryChunk_ = nullptr;
4397 Flush();
4398 }
4399 // Only process in use memory so that free region information
4400 // also includes dlmalloc book keeping.
4401 return false;
Elliott Hughesa2155262011-11-16 16:26:58 -08004402 }
Ian Rogersc0542af2014-09-03 16:16:56 -07004403 if (startOfNextMemoryChunk_ != nullptr) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004404 // Transmit any pending free memory. Native free memory of over kMaxFreeLen could be because
4405 // of the use of mmaps, so don't report. If not free memory then start a new segment.
4406 bool flush = true;
4407 if (start > startOfNextMemoryChunk_) {
4408 const size_t kMaxFreeLen = 2 * kPageSize;
4409 void* free_start = startOfNextMemoryChunk_;
4410 void* free_end = start;
4411 const size_t free_len =
4412 reinterpret_cast<uintptr_t>(free_end) - reinterpret_cast<uintptr_t>(free_start);
4413 if (!IsNative() || free_len < kMaxFreeLen) {
4414 AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), free_start, free_len, IsNative());
4415 flush = false;
Ian Rogers15bf2d32012-08-28 17:33:04 -07004416 }
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004417 }
4418 if (flush) {
4419 startOfNextMemoryChunk_ = nullptr;
4420 Flush();
4421 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07004422 }
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004423 return true;
Ian Rogers15bf2d32012-08-28 17:33:04 -07004424 }
Elliott Hughesa2155262011-11-16 16:26:58 -08004425
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004426 void HeapChunkNativeCallback(void* start, void* /*end*/, size_t used_bytes)
4427 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4428 if (ProcessRecord(start, used_bytes)) {
4429 uint8_t state = ExamineNativeObject(start);
4430 AppendChunk(state, start, used_bytes + chunk_overhead_, true /*is_native*/);
4431 startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_;
4432 }
4433 }
4434
4435 void HeapChunkJavaCallback(void* start, void* /*end*/, size_t used_bytes)
4436 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
4437 if (ProcessRecord(start, used_bytes)) {
4438 // Determine the type of this chunk.
4439 // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
4440 // If it's the same, we should combine them.
4441 uint8_t state = ExamineJavaObject(reinterpret_cast<mirror::Object*>(start));
4442 AppendChunk(state, start, used_bytes + chunk_overhead_, false /*is_native*/);
4443 startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_;
4444 }
4445 }
4446
4447 void AppendChunk(uint8_t state, void* ptr, size_t length, bool is_native)
Ian Rogersb726dcb2012-09-05 08:57:23 -07004448 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07004449 // Make sure there's enough room left in the buffer.
4450 // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
4451 // 17 bytes for any header.
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004452 const size_t needed = ((RoundUp(length / ALLOCATION_UNIT_SIZE, 256) / 256) * 2) + 17;
4453 size_t byte_left = &buf_.back() - p_;
4454 if (byte_left < needed) {
4455 if (is_native) {
Pavel Vyssotski7522c742014-12-08 13:38:26 +06004456 // Cannot trigger memory allocation while walking native heap.
Pavel Vyssotski7522c742014-12-08 13:38:26 +06004457 return;
4458 }
Ian Rogers15bf2d32012-08-28 17:33:04 -07004459 Flush();
4460 }
4461
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004462 byte_left = &buf_.back() - p_;
4463 if (byte_left < needed) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07004464 LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
4465 << needed << " bytes)";
4466 return;
4467 }
4468 EnsureHeader(ptr);
Elliott Hughesa2155262011-11-16 16:26:58 -08004469 // Write out the chunk description.
Ian Rogers15bf2d32012-08-28 17:33:04 -07004470 length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units.
4471 totalAllocationUnits_ += length;
4472 while (length > 256) {
Ian Rogers30fab402012-01-23 15:43:46 -08004473 *p_++ = state | HPSG_PARTIAL;
4474 *p_++ = 255; // length - 1
Ian Rogers15bf2d32012-08-28 17:33:04 -07004475 length -= 256;
Elliott Hughesa2155262011-11-16 16:26:58 -08004476 }
Ian Rogers30fab402012-01-23 15:43:46 -08004477 *p_++ = state;
Ian Rogers15bf2d32012-08-28 17:33:04 -07004478 *p_++ = length - 1;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004479 }
4480
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004481 uint8_t ExamineNativeObject(const void* p) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4482 return p == nullptr ? HPSG_STATE(SOLIDITY_FREE, 0) : HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
4483 }
4484
4485 uint8_t ExamineJavaObject(mirror::Object* o)
Ian Rogersef7d42f2014-01-06 12:55:46 -08004486 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -07004487 if (o == nullptr) {
Elliott Hughesa2155262011-11-16 16:26:58 -08004488 return HPSG_STATE(SOLIDITY_FREE, 0);
4489 }
Elliott Hughesa2155262011-11-16 16:26:58 -08004490 // It's an allocated chunk. Figure out what it is.
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004491 gc::Heap* heap = Runtime::Current()->GetHeap();
4492 if (!heap->IsLiveObjectLocked(o)) {
4493 LOG(ERROR) << "Invalid object in managed heap: " << o;
Elliott Hughesa2155262011-11-16 16:26:58 -08004494 return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
4495 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004496 mirror::Class* c = o->GetClass();
Ian Rogersc0542af2014-09-03 16:16:56 -07004497 if (c == nullptr) {
Elliott Hughesa2155262011-11-16 16:26:58 -08004498 // The object was probably just created but hasn't been initialized yet.
4499 return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
4500 }
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004501 if (!heap->IsValidObjectAddress(c)) {
Ian Rogers15bf2d32012-08-28 17:33:04 -07004502 LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
Elliott Hughesa2155262011-11-16 16:26:58 -08004503 return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
4504 }
Mathieu Chartierf26e1b32015-01-29 10:47:10 -08004505 if (c->GetClass() == nullptr) {
4506 LOG(ERROR) << "Null class of class " << c << " for object " << o;
4507 return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
4508 }
Elliott Hughesa2155262011-11-16 16:26:58 -08004509 if (c->IsClassClass()) {
4510 return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
4511 }
Elliott Hughesa2155262011-11-16 16:26:58 -08004512 if (c->IsArrayClass()) {
Elliott Hughesa2155262011-11-16 16:26:58 -08004513 switch (c->GetComponentSize()) {
4514 case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
4515 case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
4516 case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
4517 case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
4518 }
4519 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004520 return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
4521 }
4522
Ian Rogers30fab402012-01-23 15:43:46 -08004523 std::vector<uint8_t> buf_;
4524 uint8_t* p_;
4525 uint8_t* pieceLenField_;
Ian Rogers15bf2d32012-08-28 17:33:04 -07004526 void* startOfNextMemoryChunk_;
Ian Rogers30fab402012-01-23 15:43:46 -08004527 size_t totalAllocationUnits_;
4528 uint32_t type_;
Ian Rogers30fab402012-01-23 15:43:46 -08004529 bool needHeader_;
Mathieu Chartier36dab362014-07-30 14:59:56 -07004530 size_t chunk_overhead_;
Ian Rogers30fab402012-01-23 15:43:46 -08004531
Elliott Hughesa2155262011-11-16 16:26:58 -08004532 DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
4533};
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004534
Mathieu Chartier36dab362014-07-30 14:59:56 -07004535static void BumpPointerSpaceCallback(mirror::Object* obj, void* arg)
4536 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
4537 const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment);
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004538 HeapChunkContext::HeapChunkJavaCallback(
Mathieu Chartier36dab362014-07-30 14:59:56 -07004539 obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, arg);
4540}
4541
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004542void Dbg::DdmSendHeapSegments(bool native) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004543 Dbg::HpsgWhen when = native ? gDdmNhsgWhen : gDdmHpsgWhen;
4544 Dbg::HpsgWhat what = native ? gDdmNhsgWhat : gDdmHpsgWhat;
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004545 if (when == HPSG_WHEN_NEVER) {
4546 return;
4547 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004548 // Figure out what kind of chunks we'll be sending.
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004549 CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS)
4550 << static_cast<int>(what);
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004551
4552 // First, send a heap start chunk.
4553 uint8_t heap_id[4];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004554 JDWP::Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap).
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004555 Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07004556 Thread* self = Thread::Current();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07004557 Locks::mutator_lock_->AssertSharedHeld(self);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07004558
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004559 // Send a series of heap segment chunks.
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004560 HeapChunkContext context(what == HPSG_WHAT_MERGED_OBJECTS, native);
Elliott Hughesa2155262011-11-16 16:26:58 -08004561 if (native) {
Ian Rogers872dd822014-10-30 11:19:14 -07004562#if defined(HAVE_ANDROID_OS) && defined(USE_DLMALLOC)
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004563 dlmalloc_inspect_all(HeapChunkContext::HeapChunkNativeCallback, &context);
4564 HeapChunkContext::HeapChunkNativeCallback(nullptr, nullptr, 0, &context); // Indicate end of a space.
Christopher Ferrisc4ddc042014-05-13 14:47:50 -07004565#else
4566 UNIMPLEMENTED(WARNING) << "Native heap inspection is only supported with dlmalloc";
4567#endif
Elliott Hughesa2155262011-11-16 16:26:58 -08004568 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -07004569 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier36dab362014-07-30 14:59:56 -07004570 for (const auto& space : heap->GetContinuousSpaces()) {
4571 if (space->IsDlMallocSpace()) {
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -07004572 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier36dab362014-07-30 14:59:56 -07004573 // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
4574 // allocation then the first sizeof(size_t) may belong to it.
4575 context.SetChunkOverhead(sizeof(size_t));
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004576 space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context);
Mathieu Chartier36dab362014-07-30 14:59:56 -07004577 } else if (space->IsRosAllocSpace()) {
4578 context.SetChunkOverhead(0);
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -07004579 // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since
4580 // RosAlloc's internal logic doesn't know to release and reacquire the heap bitmap lock.
4581 self->TransitionFromRunnableToSuspended(kSuspended);
4582 ThreadList* tl = Runtime::Current()->GetThreadList();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -07004583 tl->SuspendAll(__FUNCTION__);
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -07004584 {
4585 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004586 space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context);
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -07004587 }
4588 tl->ResumeAll();
4589 self->TransitionFromSuspendedToRunnable();
Mathieu Chartier36dab362014-07-30 14:59:56 -07004590 } else if (space->IsBumpPointerSpace()) {
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -07004591 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier36dab362014-07-30 14:59:56 -07004592 context.SetChunkOverhead(0);
Mathieu Chartier36dab362014-07-30 14:59:56 -07004593 space->AsBumpPointerSpace()->Walk(BumpPointerSpaceCallback, &context);
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004594 HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -08004595 } else if (space->IsRegionSpace()) {
4596 heap->IncrementDisableMovingGC(self);
4597 self->TransitionFromRunnableToSuspended(kSuspended);
4598 ThreadList* tl = Runtime::Current()->GetThreadList();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -07004599 tl->SuspendAll(__FUNCTION__);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -08004600 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
4601 context.SetChunkOverhead(0);
4602 space->AsRegionSpace()->Walk(BumpPointerSpaceCallback, &context);
4603 HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context);
4604 tl->ResumeAll();
4605 self->TransitionFromSuspendedToRunnable();
4606 heap->DecrementDisableMovingGC(self);
Mathieu Chartier36dab362014-07-30 14:59:56 -07004607 } else {
4608 UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07004609 }
Mathieu Chartier36dab362014-07-30 14:59:56 -07004610 context.ResetStartOfNextChunk();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07004611 }
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -07004612 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07004613 // Walk the large objects, these are not in the AllocSpace.
Mathieu Chartier36dab362014-07-30 14:59:56 -07004614 context.SetChunkOverhead(0);
Mathieu Chartierbc689b72014-12-14 17:01:31 -08004615 heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context);
Elliott Hughesa2155262011-11-16 16:26:58 -08004616 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -07004617
4618 // Finally, send a heap end chunk.
4619 Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id);
Elliott Hughes767a1472011-10-26 18:49:02 -07004620}
4621
Elliott Hughesb1a58792013-07-11 18:10:58 -07004622static size_t GetAllocTrackerMax() {
4623#ifdef HAVE_ANDROID_OS
4624 // Check whether there's a system property overriding the number of records.
4625 const char* propertyName = "dalvik.vm.allocTrackerMax";
4626 char allocRecordMaxString[PROPERTY_VALUE_MAX];
4627 if (property_get(propertyName, allocRecordMaxString, "") > 0) {
4628 char* end;
4629 size_t value = strtoul(allocRecordMaxString, &end, 10);
4630 if (*end != '\0') {
Ruben Brunk3e47a742013-09-09 17:56:07 -07004631 LOG(ERROR) << "Ignoring " << propertyName << " '" << allocRecordMaxString
4632 << "' --- invalid";
Elliott Hughesb1a58792013-07-11 18:10:58 -07004633 return kDefaultNumAllocRecords;
4634 }
4635 if (!IsPowerOfTwo(value)) {
Ruben Brunk3e47a742013-09-09 17:56:07 -07004636 LOG(ERROR) << "Ignoring " << propertyName << " '" << allocRecordMaxString
4637 << "' --- not power of two";
Elliott Hughesb1a58792013-07-11 18:10:58 -07004638 return kDefaultNumAllocRecords;
4639 }
4640 return value;
4641 }
4642#endif
4643 return kDefaultNumAllocRecords;
4644}
4645
Brian Carlstrom306db812014-09-05 13:01:41 -07004646void Dbg::SetAllocTrackingEnabled(bool enable) {
4647 Thread* self = Thread::Current();
4648 if (enable) {
Sebastien Hertzb98063a2014-03-26 10:57:20 +01004649 {
Brian Carlstrom306db812014-09-05 13:01:41 -07004650 MutexLock mu(self, *Locks::alloc_tracker_lock_);
4651 if (recent_allocation_records_ != nullptr) {
4652 return; // Already enabled, bail.
Sebastien Hertzb98063a2014-03-26 10:57:20 +01004653 }
Brian Carlstrom306db812014-09-05 13:01:41 -07004654 alloc_record_max_ = GetAllocTrackerMax();
4655 LOG(INFO) << "Enabling alloc tracker (" << alloc_record_max_ << " entries of "
4656 << kMaxAllocRecordStackDepth << " frames, taking "
4657 << PrettySize(sizeof(AllocRecord) * alloc_record_max_) << ")";
4658 DCHECK_EQ(alloc_record_head_, 0U);
4659 DCHECK_EQ(alloc_record_count_, 0U);
4660 recent_allocation_records_ = new AllocRecord[alloc_record_max_];
4661 CHECK(recent_allocation_records_ != nullptr);
Elliott Hughes545a0642011-11-08 19:10:03 -08004662 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -07004663 Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints();
Elliott Hughes545a0642011-11-08 19:10:03 -08004664 } else {
Sebastien Hertzb98063a2014-03-26 10:57:20 +01004665 {
Brian Carlstrom306db812014-09-05 13:01:41 -07004666 ScopedObjectAccess soa(self); // For type_cache_.Clear();
4667 MutexLock mu(self, *Locks::alloc_tracker_lock_);
4668 if (recent_allocation_records_ == nullptr) {
4669 return; // Already disabled, bail.
4670 }
Mathieu Chartier4345c462014-06-27 10:20:14 -07004671 LOG(INFO) << "Disabling alloc tracker";
Sebastien Hertzb98063a2014-03-26 10:57:20 +01004672 delete[] recent_allocation_records_;
Ian Rogersc0542af2014-09-03 16:16:56 -07004673 recent_allocation_records_ = nullptr;
Brian Carlstrom306db812014-09-05 13:01:41 -07004674 alloc_record_head_ = 0;
4675 alloc_record_count_ = 0;
Mathieu Chartier4345c462014-06-27 10:20:14 -07004676 type_cache_.Clear();
Sebastien Hertzb98063a2014-03-26 10:57:20 +01004677 }
Brian Carlstrom306db812014-09-05 13:01:41 -07004678 // If an allocation comes in before we uninstrument, we will safely drop it on the floor.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -07004679 Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
Elliott Hughes545a0642011-11-08 19:10:03 -08004680 }
4681}
4682
Ian Rogers0399dde2012-06-06 17:09:28 -07004683struct AllocRecordStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08004684 AllocRecordStackVisitor(Thread* thread, AllocRecord* record_in)
Ian Rogersb726dcb2012-09-05 08:57:23 -07004685 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Andreas Gampe277ccbd2014-11-03 21:36:10 -08004686 : StackVisitor(thread, nullptr), record(record_in), depth(0) {}
Elliott Hughes545a0642011-11-08 19:10:03 -08004687
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004688 // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
4689 // annotalysis.
4690 bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
Elliott Hughes545a0642011-11-08 19:10:03 -08004691 if (depth >= kMaxAllocRecordStackDepth) {
Elliott Hughes530fa002012-03-12 11:44:49 -07004692 return false;
Elliott Hughes545a0642011-11-08 19:10:03 -08004693 }
Brian Carlstromea46f952013-07-30 01:26:50 -07004694 mirror::ArtMethod* m = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -07004695 if (!m->IsRuntimeMethod()) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07004696 record->StackElement(depth)->SetMethod(m);
4697 record->StackElement(depth)->SetDexPc(GetDexPc());
Elliott Hughes530fa002012-03-12 11:44:49 -07004698 ++depth;
Elliott Hughes545a0642011-11-08 19:10:03 -08004699 }
Elliott Hughes530fa002012-03-12 11:44:49 -07004700 return true;
Elliott Hughes545a0642011-11-08 19:10:03 -08004701 }
4702
4703 ~AllocRecordStackVisitor() {
4704 // Clear out any unused stack trace elements.
4705 for (; depth < kMaxAllocRecordStackDepth; ++depth) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07004706 record->StackElement(depth)->SetMethod(nullptr);
4707 record->StackElement(depth)->SetDexPc(0);
Elliott Hughes545a0642011-11-08 19:10:03 -08004708 }
4709 }
4710
4711 AllocRecord* record;
4712 size_t depth;
4713};
4714
Ian Rogers844506b2014-09-12 19:59:33 -07004715void Dbg::RecordAllocation(Thread* self, mirror::Class* type, size_t byte_count) {
Brian Carlstrom306db812014-09-05 13:01:41 -07004716 MutexLock mu(self, *Locks::alloc_tracker_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07004717 if (recent_allocation_records_ == nullptr) {
Brian Carlstrom306db812014-09-05 13:01:41 -07004718 // In the process of shutting down recording, bail.
Elliott Hughes545a0642011-11-08 19:10:03 -08004719 return;
4720 }
4721
4722 // Advance and clip.
Ian Rogers719d1a32014-03-06 12:13:39 -08004723 if (++alloc_record_head_ == alloc_record_max_) {
4724 alloc_record_head_ = 0;
Elliott Hughes545a0642011-11-08 19:10:03 -08004725 }
4726
4727 // Fill in the basics.
Ian Rogers719d1a32014-03-06 12:13:39 -08004728 AllocRecord* record = &recent_allocation_records_[alloc_record_head_];
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07004729 record->SetType(type);
4730 record->SetByteCount(byte_count);
4731 record->SetThinLockId(self->GetThreadId());
Elliott Hughes545a0642011-11-08 19:10:03 -08004732
4733 // Fill in the stack trace.
Ian Rogers7a22fa62013-01-23 12:16:16 -08004734 AllocRecordStackVisitor visitor(self, record);
Ian Rogers0399dde2012-06-06 17:09:28 -07004735 visitor.WalkStack();
Elliott Hughes545a0642011-11-08 19:10:03 -08004736
Ian Rogers719d1a32014-03-06 12:13:39 -08004737 if (alloc_record_count_ < alloc_record_max_) {
4738 ++alloc_record_count_;
Elliott Hughes545a0642011-11-08 19:10:03 -08004739 }
4740}
4741
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004742// Returns the index of the head element.
4743//
Brian Carlstrom306db812014-09-05 13:01:41 -07004744// We point at the most-recently-written record, so if alloc_record_count_ is 1
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004745// we want to use the current element. Take "head+1" and subtract count
4746// from it.
4747//
4748// We need to handle underflow in our circular buffer, so we add
Brian Carlstrom306db812014-09-05 13:01:41 -07004749// alloc_record_max_ and then mask it back down.
Ian Rogers719d1a32014-03-06 12:13:39 -08004750size_t Dbg::HeadIndex() {
4751 return (Dbg::alloc_record_head_ + 1 + Dbg::alloc_record_max_ - Dbg::alloc_record_count_) &
4752 (Dbg::alloc_record_max_ - 1);
Elliott Hughes545a0642011-11-08 19:10:03 -08004753}
4754
4755void Dbg::DumpRecentAllocations() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004756 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom306db812014-09-05 13:01:41 -07004757 MutexLock mu(soa.Self(), *Locks::alloc_tracker_lock_);
Ian Rogersc0542af2014-09-03 16:16:56 -07004758 if (recent_allocation_records_ == nullptr) {
Elliott Hughes545a0642011-11-08 19:10:03 -08004759 LOG(INFO) << "Not recording tracked allocations";
4760 return;
4761 }
4762
4763 // "i" is the head of the list. We want to start at the end of the
4764 // list and move forward to the tail.
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004765 size_t i = HeadIndex();
Brian Carlstrom306db812014-09-05 13:01:41 -07004766 const uint16_t capped_count = CappedAllocRecordCount(Dbg::alloc_record_count_);
4767 uint16_t count = capped_count;
Elliott Hughes545a0642011-11-08 19:10:03 -08004768
Ian Rogers719d1a32014-03-06 12:13:39 -08004769 LOG(INFO) << "Tracked allocations, (head=" << alloc_record_head_ << " count=" << count << ")";
Elliott Hughes545a0642011-11-08 19:10:03 -08004770 while (count--) {
4771 AllocRecord* record = &recent_allocation_records_[i];
4772
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07004773 LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->ThinLockId(), record->ByteCount())
4774 << PrettyClass(record->Type());
Elliott Hughes545a0642011-11-08 19:10:03 -08004775
4776 for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07004777 AllocRecordStackTraceElement* stack_element = record->StackElement(stack_frame);
4778 mirror::ArtMethod* m = stack_element->Method();
Ian Rogersc0542af2014-09-03 16:16:56 -07004779 if (m == nullptr) {
Elliott Hughes545a0642011-11-08 19:10:03 -08004780 break;
4781 }
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07004782 LOG(INFO) << " " << PrettyMethod(m) << " line " << stack_element->LineNumber();
Elliott Hughes545a0642011-11-08 19:10:03 -08004783 }
4784
4785 // pause periodically to help logcat catch up
4786 if ((count % 5) == 0) {
4787 usleep(40000);
4788 }
4789
Ian Rogers719d1a32014-03-06 12:13:39 -08004790 i = (i + 1) & (alloc_record_max_ - 1);
Elliott Hughes545a0642011-11-08 19:10:03 -08004791 }
4792}
4793
4794class StringTable {
4795 public:
4796 StringTable() {
4797 }
4798
Mathieu Chartier4345c462014-06-27 10:20:14 -07004799 void Add(const std::string& str) {
4800 table_.insert(str);
4801 }
4802
4803 void Add(const char* str) {
4804 table_.insert(str);
Elliott Hughes545a0642011-11-08 19:10:03 -08004805 }
4806
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004807 size_t IndexOf(const char* s) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -07004808 auto it = table_.find(s);
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004809 if (it == table_.end()) {
4810 LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
4811 }
4812 return std::distance(table_.begin(), it);
Elliott Hughes545a0642011-11-08 19:10:03 -08004813 }
4814
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004815 size_t Size() const {
Elliott Hughes545a0642011-11-08 19:10:03 -08004816 return table_.size();
4817 }
4818
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004819 void WriteTo(std::vector<uint8_t>& bytes) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -07004820 for (const std::string& str : table_) {
4821 const char* s = str.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08004822 size_t s_len = CountModifiedUtf8Chars(s);
Ian Rogers700a4022014-05-19 16:49:03 -07004823 std::unique_ptr<uint16_t> s_utf16(new uint16_t[s_len]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08004824 ConvertModifiedUtf8ToUtf16(s_utf16.get(), s);
4825 JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
Elliott Hughes545a0642011-11-08 19:10:03 -08004826 }
4827 }
4828
4829 private:
Elliott Hughesa8f93cb2012-06-08 17:08:48 -07004830 std::set<std::string> table_;
Elliott Hughes545a0642011-11-08 19:10:03 -08004831 DISALLOW_COPY_AND_ASSIGN(StringTable);
4832};
4833
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004834static const char* GetMethodSourceFile(mirror::ArtMethod* method)
Sebastien Hertz280286a2014-04-28 09:26:50 +02004835 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004836 DCHECK(method != nullptr);
4837 const char* source_file = method->GetDeclaringClassSourceFile();
Sebastien Hertz280286a2014-04-28 09:26:50 +02004838 return (source_file != nullptr) ? source_file : "";
4839}
4840
Elliott Hughes545a0642011-11-08 19:10:03 -08004841/*
4842 * The data we send to DDMS contains everything we have recorded.
4843 *
4844 * Message header (all values big-endian):
4845 * (1b) message header len (to allow future expansion); includes itself
4846 * (1b) entry header len
4847 * (1b) stack frame len
4848 * (2b) number of entries
4849 * (4b) offset to string table from start of message
4850 * (2b) number of class name strings
4851 * (2b) number of method name strings
4852 * (2b) number of source file name strings
4853 * For each entry:
4854 * (4b) total allocation size
Elliott Hughes221229c2013-01-08 18:17:50 -08004855 * (2b) thread id
Elliott Hughes545a0642011-11-08 19:10:03 -08004856 * (2b) allocated object's class name index
4857 * (1b) stack depth
4858 * For each stack frame:
4859 * (2b) method's class name
4860 * (2b) method name
4861 * (2b) method source file
4862 * (2b) line number, clipped to 32767; -2 if native; -1 if no source
4863 * (xb) class name strings
4864 * (xb) method name strings
4865 * (xb) source file strings
4866 *
4867 * As with other DDM traffic, strings are sent as a 4-byte length
4868 * followed by UTF-16 data.
4869 *
4870 * We send up 16-bit unsigned indexes into string tables. In theory there
Brian Carlstrom306db812014-09-05 13:01:41 -07004871 * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in
Elliott Hughes545a0642011-11-08 19:10:03 -08004872 * each table, but in practice there should be far fewer.
4873 *
4874 * The chief reason for using a string table here is to keep the size of
4875 * the DDMS message to a minimum. This is partly to make the protocol
4876 * efficient, but also because we have to form the whole thing up all at
4877 * once in a memory buffer.
4878 *
4879 * We use separate string tables for class names, method names, and source
4880 * files to keep the indexes small. There will generally be no overlap
4881 * between the contents of these tables.
4882 */
4883jbyteArray Dbg::GetRecentAllocations() {
Ian Rogerscf7f1912014-10-22 22:06:39 -07004884 if ((false)) {
Elliott Hughes545a0642011-11-08 19:10:03 -08004885 DumpRecentAllocations();
4886 }
4887
Ian Rogers50b35e22012-10-04 10:09:15 -07004888 Thread* self = Thread::Current();
Elliott Hughes545a0642011-11-08 19:10:03 -08004889 std::vector<uint8_t> bytes;
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004890 {
Brian Carlstrom306db812014-09-05 13:01:41 -07004891 MutexLock mu(self, *Locks::alloc_tracker_lock_);
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004892 //
4893 // Part 1: generate string tables.
4894 //
4895 StringTable class_names;
4896 StringTable method_names;
4897 StringTable filenames;
Elliott Hughes545a0642011-11-08 19:10:03 -08004898
Brian Carlstrom306db812014-09-05 13:01:41 -07004899 const uint16_t capped_count = CappedAllocRecordCount(Dbg::alloc_record_count_);
4900 uint16_t count = capped_count;
4901 size_t idx = HeadIndex();
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004902 while (count--) {
4903 AllocRecord* record = &recent_allocation_records_[idx];
Ian Rogers1ff3c982014-08-12 02:30:58 -07004904 std::string temp;
4905 class_names.Add(record->Type()->GetDescriptor(&temp));
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004906 for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07004907 mirror::ArtMethod* m = record->StackElement(i)->Method();
Ian Rogersc0542af2014-09-03 16:16:56 -07004908 if (m != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004909 class_names.Add(m->GetDeclaringClassDescriptor());
4910 method_names.Add(m->GetName());
4911 filenames.Add(GetMethodSourceFile(m));
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004912 }
4913 }
Elliott Hughes545a0642011-11-08 19:10:03 -08004914
Ian Rogers719d1a32014-03-06 12:13:39 -08004915 idx = (idx + 1) & (alloc_record_max_ - 1);
Elliott Hughes545a0642011-11-08 19:10:03 -08004916 }
4917
Brian Carlstrom306db812014-09-05 13:01:41 -07004918 LOG(INFO) << "allocation records: " << capped_count;
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004919
4920 //
4921 // Part 2: Generate the output and store it in the buffer.
4922 //
4923
4924 // (1b) message header len (to allow future expansion); includes itself
4925 // (1b) entry header len
4926 // (1b) stack frame len
4927 const int kMessageHeaderLen = 15;
4928 const int kEntryHeaderLen = 9;
4929 const int kStackFrameLen = 8;
4930 JDWP::Append1BE(bytes, kMessageHeaderLen);
4931 JDWP::Append1BE(bytes, kEntryHeaderLen);
4932 JDWP::Append1BE(bytes, kStackFrameLen);
4933
4934 // (2b) number of entries
4935 // (4b) offset to string table from start of message
4936 // (2b) number of class name strings
4937 // (2b) number of method name strings
4938 // (2b) number of source file name strings
Brian Carlstrom306db812014-09-05 13:01:41 -07004939 JDWP::Append2BE(bytes, capped_count);
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004940 size_t string_table_offset = bytes.size();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004941 JDWP::Append4BE(bytes, 0); // We'll patch this later...
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004942 JDWP::Append2BE(bytes, class_names.Size());
4943 JDWP::Append2BE(bytes, method_names.Size());
4944 JDWP::Append2BE(bytes, filenames.Size());
4945
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004946 idx = HeadIndex();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004947 std::string temp;
Brian Carlstrom306db812014-09-05 13:01:41 -07004948 for (count = capped_count; count != 0; --count) {
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004949 // For each entry:
4950 // (4b) total allocation size
4951 // (2b) thread id
4952 // (2b) allocated object's class name index
4953 // (1b) stack depth
4954 AllocRecord* record = &recent_allocation_records_[idx];
4955 size_t stack_depth = record->GetDepth();
Mathieu Chartierf8322842014-05-16 10:59:25 -07004956 size_t allocated_object_class_name_index =
Ian Rogers1ff3c982014-08-12 02:30:58 -07004957 class_names.IndexOf(record->Type()->GetDescriptor(&temp));
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07004958 JDWP::Append4BE(bytes, record->ByteCount());
4959 JDWP::Append2BE(bytes, record->ThinLockId());
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004960 JDWP::Append2BE(bytes, allocated_object_class_name_index);
4961 JDWP::Append1BE(bytes, stack_depth);
4962
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004963 for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
4964 // For each stack frame:
4965 // (2b) method's class name
4966 // (2b) method name
4967 // (2b) method source file
4968 // (2b) line number, clipped to 32767; -2 if native; -1 if no source
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07004969 mirror::ArtMethod* m = record->StackElement(stack_frame)->Method();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004970 size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor());
4971 size_t method_name_index = method_names.IndexOf(m->GetName());
4972 size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m));
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004973 JDWP::Append2BE(bytes, class_name_index);
4974 JDWP::Append2BE(bytes, method_name_index);
4975 JDWP::Append2BE(bytes, file_name_index);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -07004976 JDWP::Append2BE(bytes, record->StackElement(stack_frame)->LineNumber());
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004977 }
Ian Rogers719d1a32014-03-06 12:13:39 -08004978 idx = (idx + 1) & (alloc_record_max_ - 1);
Mathieu Chartier46e811b2013-07-10 17:09:14 -07004979 }
4980
4981 // (xb) class name strings
4982 // (xb) method name strings
4983 // (xb) source file strings
4984 JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
4985 class_names.WriteTo(bytes);
4986 method_names.WriteTo(bytes);
4987 filenames.WriteTo(bytes);
Elliott Hughes545a0642011-11-08 19:10:03 -08004988 }
Ian Rogers50b35e22012-10-04 10:09:15 -07004989 JNIEnv* env = self->GetJniEnv();
Elliott Hughes545a0642011-11-08 19:10:03 -08004990 jbyteArray result = env->NewByteArray(bytes.size());
Ian Rogersc0542af2014-09-03 16:16:56 -07004991 if (result != nullptr) {
Elliott Hughes545a0642011-11-08 19:10:03 -08004992 env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
4993 }
4994 return result;
4995}
4996
Hiroshi Yamauchi0ec17d22014-07-07 13:07:08 -07004997mirror::ArtMethod* DeoptimizationRequest::Method() const {
4998 ScopedObjectAccessUnchecked soa(Thread::Current());
4999 return soa.DecodeMethod(method_);
5000}
5001
5002void DeoptimizationRequest::SetMethod(mirror::ArtMethod* m) {
5003 ScopedObjectAccessUnchecked soa(Thread::Current());
5004 method_ = soa.EncodeMethod(m);
5005}
5006
Elliott Hughes872d4ec2011-10-21 17:07:15 -07005007} // namespace art