1872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes/*
2872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * Copyright (C) 2008 The Android Open Source Project
3872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes *
4872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * you may not use this file except in compliance with the License.
6872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * You may obtain a copy of the License at
7872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes *
8872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes *
10872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * Unless required by applicable law or agreed to in writing, software
11872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * See the License for the specific language governing permissions and
14872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * limitations under the License.
15872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes */
16872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
17872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes#include "debugger.h"
18872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes#include <sys/uio.h>
203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
21545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes#include <set>
22545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
23166db04e259ca51838c311891598664deeed85adIan Rogers#include "arch/context.h"
24545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes#include "class_linker.h"
252dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "class_linker-inl.h"
264f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
27776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "dex_instruction.h"
2822d5e735f403c57525fe868304c7123f0ce66399Ian Rogers#include "field_helper.h"
291d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/accounting/card_table-inl.h"
301d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/large_object_space.h"
311d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h"
32eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier#include "handle_scope.h"
3364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "jdwp/object_registry.h"
3422d5e735f403c57525fe868304c7123f0ce66399Ian Rogers#include "method_helper.h"
35ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_field-inl.h"
36ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h"
372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class.h"
382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h"
402dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
42b0fa5dc7769c1e054032f39de0a3f6d6dd06f8cfIan Rogers#include "mirror/string-inl.h"
432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/throwable.h"
44a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz#include "quick/inline_method_analyser.h"
4553b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers#include "reflection.h"
46a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h"
4764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "scoped_thread_state_change.h"
486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#include "ScopedLocalRef.h"
49f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes#include "ScopedPrimitiveArray.h"
50eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier#include "handle_scope-inl.h"
51475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes#include "thread_list.h"
5262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers#include "throw_location.h"
532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "utf.h"
54a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz#include "verifier/method_verifier-inl.h"
55eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "well_known_classes.h"
56475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
573d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#ifdef HAVE_ANDROID_OS
583d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#include "cutils/properties.h"
593d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#endif
603d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom
61872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesnamespace art {
62872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
637934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstromstatic const size_t kMaxAllocRecordStackDepth = 16;  // Max 255.
64f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstromstatic const size_t kDefaultNumAllocRecords = 64*1024;  // Must be a power of 2. 2BE can hold 64k-1.
65f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom
66f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom// Limit alloc_record_count to the 2BE value that is the limit of the current protocol.
67f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstromstatic uint16_t CappedAllocRecordCount(size_t alloc_record_count) {
68f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  if (alloc_record_count > 0xffff) {
69f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    return 0xffff;
70f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  }
71f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  return alloc_record_count;
72f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom}
73475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
74b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchiclass AllocRecordStackTraceElement {
75b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi public:
76b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  AllocRecordStackTraceElement() : method_(nullptr), dex_pc_(0) {
77b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
78545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
79b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  int32_t LineNumber() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
80b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    mirror::ArtMethod* method = Method();
81b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    DCHECK(method != nullptr);
82b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    return method->GetLineNumFromDexPC(DexPc());
83412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
84412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
85b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  mirror::ArtMethod* Method() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
864345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    ScopedObjectAccessUnchecked soa(Thread::Current());
874345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    return soa.DecodeMethod(method_);
88545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
89b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
90b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  void SetMethod(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
91b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    ScopedObjectAccessUnchecked soa(Thread::Current());
924345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    method_ = soa.EncodeMethod(m);
93b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
94b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
95b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  uint32_t DexPc() const {
96b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    return dex_pc_;
97b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
98b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
99b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  void SetDexPc(uint32_t pc) {
100b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    dex_pc_ = pc;
101b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
102b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
103b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi private:
1044345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  jmethodID method_;
105b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  uint32_t dex_pc_;
106545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
107545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
1084345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartierjobject Dbg::TypeCache::Add(mirror::Class* t) {
1094345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  ScopedObjectAccessUnchecked soa(Thread::Current());
1104345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  int32_t hash_code = t->IdentityHashCode();
1114345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  auto range = objects_.equal_range(hash_code);
1124345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  for (auto it = range.first; it != range.second; ++it) {
1134345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    if (soa.Decode<mirror::Class*>(it->second) == t) {
1144345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier      // Found a matching weak global, return it.
1154345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier      return it->second;
1164345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    }
1174345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  }
1184345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  JNIEnv* env = soa.Env();
1194345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  const jobject local_ref = soa.AddLocalReference<jobject>(t);
1204345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  const jobject weak_global = env->NewWeakGlobalRef(local_ref);
1214345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  env->DeleteLocalRef(local_ref);
1224345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  objects_.insert(std::make_pair(hash_code, weak_global));
1234345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  return weak_global;
1244345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier}
1254345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier
1264345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartiervoid Dbg::TypeCache::Clear() {
127f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  JavaVMExt* vm = Runtime::Current()->GetJavaVM();
128f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  Thread* self = Thread::Current();
1294345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  for (const auto& p : objects_) {
130f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    vm->DeleteWeakGlobalRef(self, p.second);
1314345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  }
1324345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  objects_.clear();
1334345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier}
1344345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier
135b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchiclass AllocRecord {
136b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi public:
137b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  AllocRecord() : type_(nullptr), byte_count_(0), thin_lock_id_(0) {}
138b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
139b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  mirror::Class* Type() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1404345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    return down_cast<mirror::Class*>(Thread::Current()->DecodeJObject(type_));
141b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
142b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
143f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  void SetType(mirror::Class* t) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_,
144f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom                                                       Locks::alloc_tracker_lock_) {
145f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    type_ = Dbg::type_cache_.Add(t);
146b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
147545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
148b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  size_t GetDepth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
149545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    size_t depth = 0;
150b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    while (depth < kMaxAllocRecordStackDepth && stack_[depth].Method() != NULL) {
151545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      ++depth;
152545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
153545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return depth;
154545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
155412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
156b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  size_t ByteCount() const {
157b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    return byte_count_;
158b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
159b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
160b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  void SetByteCount(size_t count) {
161b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    byte_count_ = count;
162412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
163b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
164b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  uint16_t ThinLockId() const {
165b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    return thin_lock_id_;
166b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
167b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
168b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  void SetThinLockId(uint16_t id) {
169b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    thin_lock_id_ = id;
170b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
171b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
172b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  AllocRecordStackTraceElement* StackElement(size_t index) {
173b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    DCHECK_LT(index, kMaxAllocRecordStackDepth);
174b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    return &stack_[index];
175b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
176b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
177b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi private:
178b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  jobject type_;  // This is a weak global.
179b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  size_t byte_count_;
180b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  uint16_t thin_lock_id_;
181b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  AllocRecordStackTraceElement stack_[kMaxAllocRecordStackDepth];  // Unused entries have NULL method.
182545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
183545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
1840ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchiclass Breakpoint {
1850ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi public:
186f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  Breakpoint(mirror::ArtMethod* method, uint32_t dex_pc,
187f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz             DeoptimizationRequest::Kind deoptimization_kind)
1880ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
189f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    : method_(nullptr), dex_pc_(dex_pc), deoptimization_kind_(deoptimization_kind) {
190f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK(deoptimization_kind_ == DeoptimizationRequest::kNothing ||
191f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz          deoptimization_kind_ == DeoptimizationRequest::kSelectiveDeoptimization ||
192f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz          deoptimization_kind_ == DeoptimizationRequest::kFullDeoptimization);
1930ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    ScopedObjectAccessUnchecked soa(Thread::Current());
1940ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    method_ = soa.EncodeMethod(method);
1950ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  }
196a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
1970ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  Breakpoint(const Breakpoint& other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
1980ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    : method_(nullptr), dex_pc_(other.dex_pc_),
199f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      deoptimization_kind_(other.deoptimization_kind_) {
2000ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    ScopedObjectAccessUnchecked soa(Thread::Current());
2010ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    method_ = soa.EncodeMethod(other.Method());
2020ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  }
203a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
2040ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  mirror::ArtMethod* Method() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2050ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    ScopedObjectAccessUnchecked soa(Thread::Current());
2060ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    return soa.DecodeMethod(method_);
2070ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  }
2083b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
2090ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  uint32_t DexPc() const {
2100ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    return dex_pc_;
2110ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  }
2120ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
213f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  DeoptimizationRequest::Kind GetDeoptimizationKind() const {
214f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    return deoptimization_kind_;
2153b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2160ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
2170ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi private:
2180ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  // The location of this breakpoint.
2190ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  jmethodID method_;
2200ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  uint32_t dex_pc_;
2210ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
2220ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  // Indicates whether breakpoint needs full deoptimization or selective deoptimization.
223f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  DeoptimizationRequest::Kind deoptimization_kind_;
2248696433d1b3d8ba15288483b777edd888de69135Elliott Hughes};
2258696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
22659d9d668d4f4286813afe2b4e7c6db839222ce96Sebastien Hertzstatic std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs)
227b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2280ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.Method()).c_str(), rhs.DexPc());
2298696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return os;
2308696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
2318696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2323f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzclass DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener {
23362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers public:
23462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  DebugInstrumentationListener() {}
23562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual ~DebugInstrumentationListener() {}
23662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2373f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodEntered(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2383f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                     uint32_t dex_pc)
2393f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
24062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
24162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
24262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
24362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
2448379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::UpdateDebugger(thread, this_object, method, 0, Dbg::kMethodEntry, nullptr);
24562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
24662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2473f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodExited(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2483f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc, const JValue& return_value)
2493f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
25062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
25162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
25262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
25362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
2548379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::UpdateDebugger(thread, this_object, method, dex_pc, Dbg::kMethodExit, &return_value);
25562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
25662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2573f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodUnwind(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2583f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc)
2593f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
26062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    // We're not recorded to listen to this kind of event, so complain.
26162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method)
26251db44a194bafc3810a41164a8b39614f10e79dfSebastien Hertz               << " " << dex_pc;
26362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
26462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2653f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void DexPcMoved(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2663f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                  uint32_t new_dex_pc)
2673f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2688379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc, 0, nullptr);
26962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
27062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2713f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void FieldRead(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2723f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                 uint32_t dex_pc, mirror::ArtField* field)
2733f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2743f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field);
27562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
2763f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2773f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void FieldWritten(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2783f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc, mirror::ArtField* field, const JValue& field_value)
2793f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2803f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value);
2813f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
2823f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2833f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
2843f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                       mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
2853f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                       mirror::Throwable* exception_object)
2863f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2873f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostException(throw_location, catch_method, catch_dex_pc, exception_object);
2883f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
2893f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2903f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz private:
2913f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener);
29262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers} gDebugInstrumentationListener;
29362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2944ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes// JDWP is allowed unless the Zygote forbids it.
2954ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesstatic bool gJdwpAllowed = true;
2964ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
297c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line?
2983bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gJdwpConfigured = false;
2993bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
300c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Broken-down JDWP options. (Only valid if IsJdwpConfigured() is true.)
301376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughesstatic JDWP::JdwpOptions gJdwpOptions;
3023bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
3033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes// Runtime JDWP state.
3043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic JDWP::JdwpState* gJdwpState = NULL;
3053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerConnected;  // debugger or DDMS is connected.
3063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerActive;     // debugger is making requests.
3078696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstatic bool gDisposed;           // debugger called VirtualMachine.Dispose, so we should drop the connection.
3083bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
30947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesstatic bool gDdmThreadNotification = false;
31047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
311767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes// DDMS GC-related settings.
312767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
313767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
314767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmHpsgWhat;
315767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
316767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmNhsgWhat;
317767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
318d539167b7f11136fe570a77aff2ee4935842007aSebastien HertzObjectRegistry* Dbg::gRegistry = nullptr;
319475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
320545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes// Recent allocation tracking.
321719d1a33f6569864f529e5a3fff59e7bca97aad0Ian RogersAllocRecord* Dbg::recent_allocation_records_ = nullptr;  // TODO: CircularBuffer<AllocRecord>
322719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_max_ = 0;
323719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_head_ = 0;
324719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_count_ = 0;
3254345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu ChartierDbg::TypeCache Dbg::type_cache_;
326545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
327138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz// Deoptimization support.
3284d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzstd::vector<DeoptimizationRequest> Dbg::deoptimization_requests_;
3294d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzsize_t Dbg::full_deoptimization_event_count_ = 0;
3307ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzsize_t Dbg::delayed_full_undeoptimization_count_ = 0;
331138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
33242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz// Instrumentation event reference counters.
33342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::dex_pc_change_event_ref_count_ = 0;
33442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::method_enter_event_ref_count_ = 0;
33542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::method_exit_event_ref_count_ = 0;
33642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::field_read_event_ref_count_ = 0;
33742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::field_write_event_ref_count_ = 0;
33842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::exception_catch_event_ref_count_ = 0;
33942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzuint32_t Dbg::instrumentation_events_ = 0;
34042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz
341138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz// Breakpoints.
34209bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhaostatic std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_);
3438696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
34412f7423a2bb4bfab76700d84eb6d4338d211983aMathieu Chartiervoid DebugInvokeReq::VisitRoots(RootCallback* callback, void* arg, const RootInfo& root_info) {
3453b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (receiver != nullptr) {
34612f7423a2bb4bfab76700d84eb6d4338d211983aMathieu Chartier    callback(&receiver, arg, root_info);
3473b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3483b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (thread != nullptr) {
34912f7423a2bb4bfab76700d84eb6d4338d211983aMathieu Chartier    callback(&thread, arg, root_info);
3503b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3513b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (klass != nullptr) {
35212f7423a2bb4bfab76700d84eb6d4338d211983aMathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&klass), arg, root_info);
3533b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3543b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
35512f7423a2bb4bfab76700d84eb6d4338d211983aMathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, root_info);
3563b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3573b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
3583b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
359bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid DebugInvokeReq::Clear() {
360bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  invoke_needed = false;
361bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  receiver = nullptr;
362bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  thread = nullptr;
363bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  klass = nullptr;
364bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
365bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
366bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
36712f7423a2bb4bfab76700d84eb6d4338d211983aMathieu Chartiervoid SingleStepControl::VisitRoots(RootCallback* callback, void* arg, const RootInfo& root_info) {
3683b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
36912f7423a2bb4bfab76700d84eb6d4338d211983aMathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, root_info);
3703b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3713b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
3723b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
373bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzbool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const {
374bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  return dex_pcs.find(dex_pc) == dex_pcs.end();
375bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
376bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
377bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid SingleStepControl::Clear() {
378bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  is_active = false;
379bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
380bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  dex_pcs.clear();
381bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
382bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
383ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic bool IsBreakpoint(const mirror::ArtMethod* m, uint32_t dex_pc)
38409bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    LOCKS_EXCLUDED(Locks::breakpoint_lock_)
385b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
38659d9d668d4f4286813afe2b4e7c6db839222ce96Sebastien Hertz  ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
387138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
3880ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].Method() == m) {
3898696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
3908696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      return true;
3918696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
3928696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
3938696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return false;
3948696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
3958696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
39652d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertzstatic bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread)
39752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) {
3989e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
3999e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // A thread may be suspended for GC; in this code, we really want to know whether
4009e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // there's a debugger suspension active.
4019e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
4029e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes}
4039e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
4042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status)
405b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
406d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id);
40764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
408436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
409436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
410436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
411436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsArrayInstance()) {
412436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_ARRAY;
413436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
414436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
415436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
416436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsArray();
417436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
418436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
4192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status)
420b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
421d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id);
42264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
423436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
424436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
425436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
426436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsClass()) {
427436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_CLASS;
428436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
429436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
430436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
431436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsClass();
432436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
433436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
434221229cb523f849f165fdafbf9785010963715daElliott Hughesstatic JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread)
435a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
436b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
437b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
438d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* thread_peer = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_id);
43964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) {
440221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't even an object.
441221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_OBJECT;
442221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
443221229cb523f849f165fdafbf9785010963715daElliott Hughes
4442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
445221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
446221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't a thread.
447221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_THREAD;
448221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
449221229cb523f849f165fdafbf9785010963715daElliott Hughes
450221229cb523f849f165fdafbf9785010963715daElliott Hughes  thread = Thread::FromManagedThread(soa, thread_peer);
451221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (thread == NULL) {
452221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This is a java.lang.Thread without a Thread*. Must be a zombie.
453221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_THREAD_NOT_ALIVE;
454436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
455221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
456436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
457436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
45824437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
45924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
46024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
46124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  return static_cast<JDWP::JdwpTag>(descriptor[0]);
46224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
46324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
464cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogersstatic JDWP::JdwpTag BasicTagFromClass(mirror::Class* klass)
465cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
466cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  std::string temp;
467cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  const char* descriptor = klass->GetDescriptor(&temp);
468cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  return BasicTagFromDescriptor(descriptor);
469cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers}
470cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers
4719837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogersstatic JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c)
472b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
47386b0010c79ef95b5333cd540b7d3af34a9f1a643Elliott Hughes  CHECK(c != NULL);
47424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsArrayClass()) {
47524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_ARRAY;
47624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
47724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsStringClass()) {
47824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_STRING;
4799837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
4809837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (c->IsClassClass()) {
48124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_CLASS_OBJECT;
48224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
4839837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
4849837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
4859837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_class->IsAssignableFrom(c)) {
4869837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD;
4879837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
4889837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
4899837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
4909837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_group_class =
4919837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
4929837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_group_class->IsAssignableFrom(c)) {
4939837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD_GROUP;
4949837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
4959837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
4969837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
4979837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* class_loader_class =
4989837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader);
4999837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (class_loader_class->IsAssignableFrom(c)) {
5009837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_CLASS_LOADER;
5019837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
5029837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
5039837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return JDWP::JT_OBJECT;
50424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
50524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
50624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes/*
50724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Objects declared to hold Object might actually hold a more specific
50824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * type.  The debugger may take a special interest in these (e.g. it
50924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * wants to display the contents of Strings), so we want to return an
51024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * appropriate tag.
51124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes *
51224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Null objects are tagged JT_OBJECT.
51324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes */
514d539167b7f11136fe570a77aff2ee4935842007aSebastien HertzJDWP::JdwpTag Dbg::TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o) {
5159837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass());
51624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
51724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
51824437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic bool IsPrimitiveTag(JDWP::JdwpTag tag) {
51924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  switch (tag) {
52024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BOOLEAN:
52124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BYTE:
52224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_CHAR:
52324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_FLOAT:
52424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_DOUBLE:
52524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_INT:
52624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_LONG:
52724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_SHORT:
52824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_VOID:
52924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return true;
53024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  default:
53124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return false;
53224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
53324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
53424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
5353bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
5363bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Handle one of the JDWP name/value pairs.
5373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
5383bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * JDWP options are:
5393bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  help: if specified, show help message and bail
5403bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  transport: may be dt_socket or dt_shmem
5413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  address: for dt_socket, "host:port", or just "port" when listening
5423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  server: if "y", wait for debugger to attach; if "n", attach to debugger
5433bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  timeout: how long to wait for debugger to connect / listen
5443bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
5453bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Useful with server=n (these aren't supported yet):
5463bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onthrow=<exception-name>: connect to debugger when exception thrown
5473bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onuncaught=y|n: connect to debugger when uncaught exception thrown
5483bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  launch=<command-line>: launch the debugger itself
5493bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
5503bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * The "transport" option is required, as is "address" if server=n.
5513bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
5523bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool ParseJdwpOption(const std::string& name, const std::string& value) {
5533bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (name == "transport") {
5543bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "dt_socket") {
555376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
5563bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "dt_android_adb") {
557376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
5583bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5593bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP transport not supported: " << value;
5603bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5613bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5623bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "server") {
5633bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
564376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = false;
5653bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
566376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = true;
5673bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5683bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
5693bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5703bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5713bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "suspend") {
5723bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
573376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = false;
5743bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
575376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = true;
5763bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5773bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
5783bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5793bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5803bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "address") {
5813bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* this is either <port> or <host>:<port> */
5823bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string port_string;
583376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.host.clear();
5843bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type colon = value.find(':');
5853bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (colon != std::string::npos) {
586376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.host = value.substr(0, colon);
5873bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value.substr(colon + 1);
5883bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5893bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value;
5903bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5913bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (port_string.empty()) {
5923bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address missing port: " << value;
5933bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5943bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5953bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    char* end;
596ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    uint64_t port = strtoul(port_string.c_str(), &end, 10);
597ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    if (*end != '\0' || port > 0xffff) {
5983bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address has junk in port field: " << value;
5993bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
6003bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
601376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.port = port;
6023bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
6033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* valid but unsupported */
6043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
6053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
6063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
6073bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
6083bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
6093bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
6103bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
6113bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
6123bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
6133bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
6143bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * "transport=dt_socket,address=8000,server=y,suspend=n"
6153bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
6163bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesbool Dbg::ParseJdwpOptions(const std::string& options) {
6174dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "ParseJdwpOptions: " << options;
61847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
6193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  std::vector<std::string> pairs;
6203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Split(options, ',', pairs);
6213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
6223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  for (size_t i = 0; i < pairs.size(); ++i) {
6233bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type equals = pairs[i].find('=');
6243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (equals == std::string::npos) {
6253bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
6263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
6273bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
6283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
6293bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
6303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
631376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
6323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP transport: " << options;
6333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
634376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
6353bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
6363bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    return false;
6373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
6383bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
6393bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gJdwpConfigured = true;
6403bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
6413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
6423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
643d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StartJdwp() {
644c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!gJdwpAllowed || !IsJdwpConfigured()) {
645376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    // No JDWP for you!
646376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    return;
647376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  }
648376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes
649719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  CHECK(gRegistry == nullptr);
650475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gRegistry = new ObjectRegistry;
651475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
652d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // Init JDWP if the debugger is enabled. This may connect out to a
653d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger, passively listen for a debugger, or block waiting for a
654d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger.
655376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
656376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState == NULL) {
657f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // We probably failed because some other process has the port already, which means that
658f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // if we don't abort the user is likely to think they're talking to us when they're actually
659f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // talking to that other process.
6603d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Debugger thread failed to initialize";
661d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
662d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes
663d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // If a debugger has already attached, send the "welcome" message.
664d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // This may cause us to suspend all threads.
665376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState->IsActive()) {
66600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
667376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    if (!gJdwpState->PostVMStart()) {
6683d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes      LOG(WARNING) << "Failed to post 'start' message to debugger";
669d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes    }
670d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
671872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
672872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
673d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StopJdwp() {
674d8b3537b89fa68599534a65afc3b272639cd4a75Sebastien Hertz  // Post VM_DEATH event before the JDWP connection is closed (either by the JDWP thread or the
675d8b3537b89fa68599534a65afc3b272639cd4a75Sebastien Hertz  // destruction of gJdwpState).
676d8b3537b89fa68599534a65afc3b272639cd4a75Sebastien Hertz  if (gJdwpState != nullptr && gJdwpState->IsActive()) {
677d8b3537b89fa68599534a65afc3b272639cd4a75Sebastien Hertz    gJdwpState->PostVMDeath();
678d8b3537b89fa68599534a65afc3b272639cd4a75Sebastien Hertz  }
6790376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  // Prevent the JDWP thread from processing JDWP incoming packets after we close the connection.
6800376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  Disposed();
681376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  delete gJdwpState;
682719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gJdwpState = nullptr;
683475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  delete gRegistry;
684719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gRegistry = nullptr;
685872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
686872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
687767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesvoid Dbg::GcDidFinish() {
688767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
68900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6904d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Sending heap info to DDM";
6917162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(gDdmHpifWhen);
692767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
693767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
69400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6954d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Dumping heap to DDM";
6966a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(false);
697767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
698767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
69900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
7004d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Dumping native heap to DDM";
7016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(true);
702767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
703767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
704767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
7054ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesvoid Dbg::SetJdwpAllowed(bool allowed) {
7064ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes  gJdwpAllowed = allowed;
7074ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes}
7084ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
709872d4ec7225444d9400d30f9027247deb91012fdElliott HughesDebugInvokeReq* Dbg::GetInvokeReq() {
710475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return Thread::Current()->GetInvokeReq();
711475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
712475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
713475fc23a4a7f35d1be87ea0b06c80df317a720acElliott HughesThread* Dbg::GetDebugThread() {
714475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
715475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
716475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
717475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::ClearWaitForEventThread() {
718475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gJdwpState->ClearWaitForEventThread();
719872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
720872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
721872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Connected() {
7223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(!gDebuggerConnected);
7234dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "JDWP has attached";
7243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gDebuggerConnected = true;
7258696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = false;
7268696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
7278696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
7288696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::Disposed() {
7298696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = true;
7308696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
7318696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
7328696433d1b3d8ba15288483b777edd888de69135Elliott Hughesbool Dbg::IsDisposed() {
7338696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return gDisposed;
734872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
735872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
736f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertzbool Dbg::RequiresDeoptimization() {
737f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  // We don't need deoptimization if everything runs with interpreter after
738f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  // enabling -Xint mode.
739f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  return !Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly();
740f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz}
741f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz
742a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::GoActive() {
743a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Enable all debugging features, including scans for breakpoints.
744a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // This is a no-op if we're already active.
745a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Only called from the JDWP handler thread.
746a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (gDebuggerActive) {
747a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return;
748a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
749a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
750c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  {
751c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
75259d9d668d4f4286813afe2b4e7c6db839222ce96Sebastien Hertz    ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
753c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    CHECK_EQ(gBreakpoints.size(), 0U);
754c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  }
755a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
756138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
757f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
7584d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(deoptimization_requests_.size(), 0U);
7594d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(full_deoptimization_event_count_, 0U);
7607ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    CHECK_EQ(delayed_full_undeoptimization_count_, 0U);
76142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(dex_pc_change_event_ref_count_, 0U);
76242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(method_enter_event_ref_count_, 0U);
76342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(method_exit_event_ref_count_, 0U);
76442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(field_read_event_ref_count_, 0U);
76542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(field_write_event_ref_count_, 0U);
76642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(exception_catch_event_ref_count_, 0U);
767138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
768138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
76962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
77062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
77162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
77262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
77362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_NE(old_state, kRunnable);
774f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  if (RequiresDeoptimization()) {
775f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    runtime->GetInstrumentation()->EnableDeoptimization();
776f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  }
77742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  instrumentation_events_ = 0;
778a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  gDebuggerActive = true;
77962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
78062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
78162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
78262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  LOG(INFO) << "Debugger is active";
783872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
784872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
785872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Disconnected() {
786234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  CHECK(gDebuggerConnected);
787234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
788c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  LOG(INFO) << "Debugger is no longer active";
789234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
79062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread
79162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener
79262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // and clear the object registry.
79362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
79462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
79562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
79662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
797aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz
798aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  // Debugger may not be active at this point.
799aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  if (gDebuggerActive) {
800aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    {
801aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // Since we're going to disable deoptimization, we clear the deoptimization requests queue.
802aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // This prevents us from having any pending deoptimization request when the debugger attaches
803aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // to us again while no event has been requested yet.
804f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
8054d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.clear();
8064d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      full_deoptimization_event_count_ = 0U;
8077ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      delayed_full_undeoptimization_count_ = 0U;
808aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    }
80942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    if (instrumentation_events_ != 0) {
81042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener,
81142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz                                                    instrumentation_events_);
81242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      instrumentation_events_ = 0;
81342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
814f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    if (RequiresDeoptimization()) {
815f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      runtime->GetInstrumentation()->DisableDeoptimization();
816f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    }
817aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    gDebuggerActive = false;
818aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  }
819234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gRegistry->Clear();
820234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gDebuggerConnected = false;
82162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
82262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
823872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
824872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
825c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsDebuggerActive() {
8263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gDebuggerActive;
827872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
828872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
829c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsJdwpConfigured() {
8303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gJdwpConfigured;
831872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
832872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
833872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesint64_t Dbg::LastDebuggerActivity() {
834ca9515205010099d006ac2fac244348a1e673dcbElliott Hughes  return gJdwpState->LastDebuggerActivity();
835872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
836872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
837872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::UndoDebuggerSuspensions() {
838234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
839872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
840872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
84188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
8422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id);
8433d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (o == NULL) {
844436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return "NULL";
8457b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
84664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
84788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
8483d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
8493d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (!o->IsClass()) {
8507934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return StringPrintf("non-class %p", o);  // This is only used for debugging output anyway.
8513d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
852d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return GetClassName(o->AsClass());
853d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
854d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
855d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzstd::string Dbg::GetClassName(mirror::Class* klass) {
856bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  if (klass == nullptr) {
857bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz    return "NULL";
858bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  }
859cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  std::string temp;
860d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return DescriptorToName(klass->GetDescriptor(&temp));
8613d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes}
8623d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
86388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) {
864436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
8652dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
866436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
867436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
8682435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
86988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  class_object_id = gRegistry->Add(c);
870436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
8718696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
8728696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
87388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) {
8743d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
8752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
8763d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c == NULL) {
8773d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
8783d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
8793d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c->IsInterface()) {
8803d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    // http://code.google.com/p/android/issues/detail?id=20856
88188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = 0;
8823d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else {
88388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = gRegistry->Add(c->GetSuperClass());
8843d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
8853d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
886872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
887872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
888436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
8892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
89064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
891436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
892436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
893436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader()));
894436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
895872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
896872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
897436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
898436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
8992dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
900436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
901436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
9027b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
903436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
904436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
905436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
906de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set,
907de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // not interfaces.
908436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
909de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  if ((access_flags & kAccInterface) == 0) {
910de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban    access_flags |= kAccSuper;
911de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  }
912436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
913436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAdd4BE(pReply, access_flags);
914436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
915436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
916872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
917872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
918f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott HughesJDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply)
919f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
9202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
92164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
922f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
923f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
924f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
925f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  // Ensure all threads are suspended while we read objects' lock words.
926f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Thread* self = Thread::Current();
9275426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
9285426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromRunnableToSuspended(kSuspended);
9295426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->SuspendAll();
930f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
931f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  MonitorInfo monitor_info(o);
932f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
9335426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->ResumeAll();
9345426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromSuspendedToRunnable();
935f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
936d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (monitor_info.owner_ != NULL) {
937d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer()));
938f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else {
939f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    expandBufAddObjectId(reply, gRegistry->Add(NULL));
940f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
941d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.entry_count_);
942d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.waiters_.size());
943d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) {
944d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer()));
945f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
946f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  return JDWP::ERR_NONE;
947f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes}
948f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
949734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott HughesJDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
950734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes                                      std::vector<JDWP::ObjectId>& monitors,
95152d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                      std::vector<uint32_t>& stack_depths) {
9524993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  struct OwnedMonitorVisitor : public StackVisitor {
953b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    OwnedMonitorVisitor(Thread* thread, Context* context,
954cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi                        std::vector<JDWP::ObjectId>* monitor_vector,
955b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi                        std::vector<uint32_t>* stack_depth_vector)
9564993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
957b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      : StackVisitor(thread, context), current_stack_depth(0),
958b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        monitors(monitor_vector), stack_depths(stack_depth_vector) {}
9594993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
9604993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
9614993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // annotalysis.
9624993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
9634993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      if (!GetMethod()->IsRuntimeMethod()) {
9644993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        Monitor::VisitLocks(this, AppendOwnedMonitors, this);
965734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes        ++current_stack_depth;
9664993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      }
9674993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      return true;
9684993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
9694993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
970cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi    static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg)
971cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
9727a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
973cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi      visitor->monitors->push_back(gRegistry->Add(owned_monitor));
974b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      visitor->stack_depths->push_back(visitor->current_stack_depth);
9754993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
9764993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
977734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    size_t current_stack_depth;
978cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi    std::vector<JDWP::ObjectId>* monitors;
979b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    std::vector<uint32_t>* stack_depths;
9804993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  };
9814993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
982b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  ScopedObjectAccessUnchecked soa(Thread::Current());
983cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  Thread* thread;
984b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  {
985b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
986b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
987b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (error != JDWP::ERR_NONE) {
988b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return error;
989b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
990b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (!IsSuspendedForDebugger(soa, thread)) {
991b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return JDWP::ERR_THREAD_NOT_SUSPENDED;
992b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
9934993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
994cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  std::unique_ptr<Context> context(Context::Create());
995cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  OwnedMonitorVisitor visitor(thread, context.get(), &monitors, &stack_depths);
996cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  visitor.WalkStack();
9974993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  return JDWP::ERR_NONE;
9984993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes}
9994993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
100052d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id,
100152d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                         JDWP::ObjectId& contended_monitor) {
1002b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  mirror::Object* contended_monitor_obj;
1003f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
1004b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  {
1005b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1006b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    Thread* thread;
1007b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1008b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (error != JDWP::ERR_NONE) {
1009b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return error;
1010b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
1011b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (!IsSuspendedForDebugger(soa, thread)) {
1012b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return JDWP::ERR_THREAD_NOT_SUSPENDED;
1013b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
1014b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    contended_monitor_obj = Monitor::GetContendedMonitor(thread);
1015f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
1016b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  // Add() requires the thread_list_lock_ not held to avoid the lock
1017b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  // level violation.
1018b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  contended_monitor = gRegistry->Add(contended_monitor_obj);
1019f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
1020f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
1021f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
1022ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott HughesJDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
1023ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes                                       std::vector<uint64_t>& counts)
1024ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1025412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
1026412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
10272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
1028ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  counts.clear();
1029ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  for (size_t i = 0; i < class_ids.size(); ++i) {
1030ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    JDWP::JdwpError status;
10312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_ids[i], status);
1032ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    if (c == NULL) {
1033ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes      return status;
1034ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    }
1035ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    classes.push_back(c);
1036ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    counts.push_back(0);
1037ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  }
1038412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CountInstances(classes, false, &counts[0]);
1039ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  return JDWP::ERR_NONE;
1040ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes}
1041ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes
10423b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott HughesJDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances)
10433b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1044412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
1045412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  // We only want reachable instances, so do a GC.
1046412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
10473b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  JDWP::JdwpError status;
10482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1049412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (c == nullptr) {
10503b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    return status;
10513b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
10522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
10533b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances);
10543b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
10553b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    instances.push_back(gRegistry->Add(raw_instances[i]));
10563b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
10573b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  return JDWP::ERR_NONE;
10583b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes}
10593b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes
10600cbaff584244ee767027aff35cd3c625aaee2994Elliott HughesJDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
10610cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes                                         std::vector<JDWP::ObjectId>& referring_objects)
10620cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1063412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
1064412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
10652dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
106664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
10670cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
10680cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
10692dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
1070412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->GetReferringObjects(o, max_count, raw_instances);
10710cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
10720cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    referring_objects.push_back(gRegistry->Add(raw_instances[i]));
10730cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
10740cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  return JDWP::ERR_NONE;
10750cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes}
10760cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes
107764f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id)
107864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1079e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
1080e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
1081e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
1082e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
108364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisableCollection(object_id);
108464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
108564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
108664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
108764f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id)
108864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1089e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
1090e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI
1091e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // also ignores these cases and never return an error. However it's not obvious why this command
1092e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // should behave differently from DisableCollection and IsCollected commands. So let's be more
1093e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // strict and return an error if this happens.
1094e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
1095e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
1096e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
109764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->EnableCollection(object_id);
109864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
109964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
110064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
110164f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected)
110264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
110365637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  if (object_id == 0) {
110465637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    // Null object id is invalid.
110565637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
110665637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  }
1107e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However
110865637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  // the RI seems to ignore this and assume object has been collected.
110965637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
1110e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
111165637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = true;
111265637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  } else {
111365637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = gRegistry->IsCollected(object_id);
1114e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
111564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
111664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
111764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
111864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count)
111964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
112064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisposeObject(object_id, reference_count);
112164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
112264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
1123d539167b7f11136fe570a77aff2ee4935842007aSebastien HertzJDWP::JdwpTypeTag Dbg::GetTypeTag(mirror::Class* klass) {
11244d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  DCHECK(klass != nullptr);
11254d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  if (klass->IsArrayClass()) {
11264d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_ARRAY;
11274d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else if (klass->IsInterface()) {
11284d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_INTERFACE;
11294d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else {
11304d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_CLASS;
11314d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  }
11324d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz}
11334d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz
113488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1135436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1137436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1138436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11397b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1140436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
11414d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(c);
11424d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  expandBufAdd1(pReply, type_tag);
114388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  expandBufAddRefTypeId(pReply, class_id);
1144436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1145872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1146872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
11477b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughesvoid Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) {
1148a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Get the complete list of reference classes (i.e. all classes except
1149a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // the primitive types).
1150a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Returns a newly-allocated buffer full of RefTypeId values.
1151a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  struct ClassListCreator {
1152ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) {
11537b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    }
11547b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
11552dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static bool Visit(mirror::Class* c, void* arg) {
1156a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return reinterpret_cast<ClassListCreator*>(arg)->Visit(c);
1157a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1158a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
115964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
116064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
116164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS {
1162a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (!c->IsPrimitive()) {
116364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes        classes.push_back(gRegistry->AddRefType(c));
1164a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
1165a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return true;
1166a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1167a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
11687b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    std::vector<JDWP::RefTypeId>& classes;
1169a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
1170a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
11717b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  ClassListCreator clc(classes);
117295795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz  Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(ClassListCreator::Visit,
117395795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz                                                                       &clc);
1174872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1175872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1176cb6b0f31ede2275e79e6199ec391147585a37a2aIan RogersJDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag,
1177cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers                                  uint32_t* pStatus, std::string* pDescriptor) {
1178436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11792dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1180436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1181436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11827b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
11837b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
1184a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (c->IsArrayClass()) {
1185a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
1186a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = JDWP::TT_ARRAY;
1187a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
1188a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsErroneous()) {
1189a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_ERROR;
1190a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    } else {
1191a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
1192a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1193a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1194a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1195a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
1196a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (pDescriptor != NULL) {
1197cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    std::string temp;
1198cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    *pDescriptor = c->GetDescriptor(&temp);
1199a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1200436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1201872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1202872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1203c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughesvoid Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) {
12042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
12056fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
12066fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  ids.clear();
12076fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  for (size_t i = 0; i < classes.size(); ++i) {
12086fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes    ids.push_back(gRegistry->Add(classes[i]));
12096fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  }
1210872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1211872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
121264f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply)
121364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
12142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
121564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
12162435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
12172435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
12182435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
12194d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass());
122064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
12212435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
12222435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAdd1(pReply, type_tag);
12232435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAddRefTypeId(pReply, type_id);
12242435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
12252435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1226872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1227872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1228fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan RogersJDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) {
12291fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  JDWP::JdwpError status;
12302dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
12311fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  if (c == NULL) {
12321fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes    return status;
12337b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1234cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  std::string temp;
1235cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  *signature = c->GetDescriptor(&temp);
12361fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  return JDWP::ERR_NONE;
1237872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1238872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
123988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) {
1240436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
12412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
12424206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  if (c == nullptr) {
1243436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
12447b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
12454206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  const char* source_file = c->GetSourceFile();
12464206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  if (source_file == nullptr) {
1247b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz    return JDWP::ERR_ABSENT_INFORMATION;
1248b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz  }
12494206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  result = source_file;
1250436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1251872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1252872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
125388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) {
12549837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
12552dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
125664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
1257546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
1258546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
12599837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  tag = TagFromObject(soa, o);
1260546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  return JDWP::ERR_NONE;
1261872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1262872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1263aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughessize_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
1264dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  switch (tag) {
1265dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_VOID:
1266dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 0;
1267dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BYTE:
1268dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BOOLEAN:
1269dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 1;
1270dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CHAR:
1271dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_SHORT:
1272dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 2;
1273dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_FLOAT:
1274dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_INT:
1275dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 4;
1276dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_ARRAY:
1277dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_OBJECT:
1278dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_STRING:
1279dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD:
1280dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD_GROUP:
1281dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_LOADER:
1282dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_OBJECT:
1283dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return sizeof(JDWP::ObjectId);
1284dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_DOUBLE:
1285dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_LONG:
1286dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 8;
1287dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  default:
12883d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Unknown tag " << tag;
1289dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return -1;
1290dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1291872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1292872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
129388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) {
12943d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
12952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
12963d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (a == NULL) {
12973d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
129824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
12993d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  length = a->GetLength();
13003d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1301872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1302872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
130388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) {
13043d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
13052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
13069837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (a == nullptr) {
13073d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
13083d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
130924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
131024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
131124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
13123d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
131324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
1314cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  JDWP::JdwpTag element_tag = BasicTagFromClass(a->GetClass()->GetComponentType());
1315cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  expandBufAdd1(pReply, element_tag);
13163d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd4BE(pReply, count);
13173d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
1318cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  if (IsPrimitiveTag(element_tag)) {
1319cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    size_t width = GetTagWidth(element_tag);
132024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    uint8_t* dst = expandBufAddSpace(pReply, count * width);
132124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    if (width == 8) {
1322ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t), 0));
132324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
132424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 4) {
1325ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t), 0));
132624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
132724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 2) {
1328ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t), 0));
132924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
133024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else {
1331ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t), 0));
133224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      memcpy(dst, &src[offset * width], count * width);
133324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
133424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else {
13359837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
13362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>();
133724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    for (int i = 0; i < count; ++i) {
13382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* element = oa->Get(offset + i);
13399837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element)
1340cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers                                                        : element_tag;
134124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAdd1(pReply, specific_tag);
134224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAddObjectId(pReply, gRegistry->Add(element));
134324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
134424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
134524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
13463d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1347872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1348872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1349ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate <typename T>
1350ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstatic void CopyArrayData(mirror::Array* a, JDWP::Request& src, int offset, int count)
1351ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    NO_THREAD_SAFETY_ANALYSIS {
1352ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  // TODO: fix when annotalysis correctly handles non-member functions.
13534b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  DCHECK(a->GetClass()->IsPrimitiveArray());
13544b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1355ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset));
13564b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  for (int i = 0; i < count; ++i) {
13574b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    *dst++ = src.ReadValue(sizeof(T));
13584b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
13594b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
13604b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
136188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
13624b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                      JDWP::Request& request)
1363b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
13643d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
13654b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  mirror::Array* dst = DecodeArray(array_id, status);
13664b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (dst == NULL) {
13673d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
13683d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
1369f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
13704b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
1371f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
13723d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
1373f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1374cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  JDWP::JdwpTag element_tag = BasicTagFromClass(dst->GetClass()->GetComponentType());
1375f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
1376cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  if (IsPrimitiveTag(element_tag)) {
1377cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    size_t width = GetTagWidth(element_tag);
1378f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    if (width == 8) {
13794b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint64_t>(dst, request, offset, count);
1380f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 4) {
13814b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint32_t>(dst, request, offset, count);
1382f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 2) {
13834b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint16_t>(dst, request, offset, count);
1384f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else {
13854b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint8_t>(dst, request, offset, count);
1386f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1387f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  } else {
13884b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>();
1389f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    for (int i = 0; i < count; ++i) {
13904b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      JDWP::ObjectId id = request.ReadObjectId();
13912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
139264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes      if (o == ObjectRegistry::kInvalidObject) {
1393436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
1394436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes      }
1395d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      oa->Set<false>(offset + i, o);
1396f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1397f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1398f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
13993d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1400872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1401872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
14027b3cdfcca472b779cf8745fb8460935e56229f11Elliott HughesJDWP::ObjectId Dbg::CreateString(const std::string& str) {
14032dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str()));
1404872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1405872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
140688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) {
1407436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14082dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1409436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1410436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14117b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
141250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  new_object = gRegistry->Add(c->AllocObject(Thread::Current()));
1413436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1414872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1415872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1416bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes/*
1417bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1418bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes */
141988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
142000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                       JDWP::ObjectId& new_array) {
1421436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14222dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(array_class_id, status);
1423436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1424436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14257b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
14266fac447555dc94a935b78198479cce645c837b89Ian Rogers  new_array = gRegistry->Add(mirror::Array::Alloc<true>(Thread::Current(), c, length,
14276fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        c->GetComponentSize(),
14286fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        Runtime::Current()->GetHeap()->GetCurrentAllocator()));
1429436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1430872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1431872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1432d539167b7f11136fe570a77aff2ee4935842007aSebastien HertzJDWP::FieldId Dbg::ToFieldId(const mirror::ArtField* f) {
1433590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
143403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
143503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
143603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1437ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::MethodId ToMethodId(const mirror::ArtMethod* m)
1438b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1439590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
144003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m));
144103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
144203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1443ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtField* FromFieldId(JDWP::FieldId fid)
1444b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1445590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
1446ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtField*>(static_cast<uintptr_t>(fid));
1447aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes}
1448aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1449ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtMethod* FromMethodId(JDWP::MethodId mid)
1450b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1451590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
1452ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtMethod*>(static_cast<uintptr_t>(mid));
145303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
145403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1455d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzbool Dbg::MatchThread(JDWP::ObjectId expected_thread_id, Thread* event_thread) {
1456d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  CHECK(event_thread != nullptr);
1457d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* expected_thread_peer = gRegistry->Get<mirror::Object*>(expected_thread_id);
1458d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return expected_thread_peer == event_thread->GetPeer();
1459d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
1460d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
1461d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzbool Dbg::MatchLocation(const JDWP::JdwpLocation& expected_location,
1462d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz                        const JDWP::EventLocation& event_location) {
1463d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  if (expected_location.dex_pc != event_location.dex_pc) {
1464d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    return false;
1465d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  }
1466d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::ArtMethod* m = FromMethodId(expected_location.method_id);
1467d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return m == event_location.method;
1468d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
1469d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
1470d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzbool Dbg::MatchType(mirror::Class* event_class, JDWP::RefTypeId class_id) {
1471bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  if (event_class == nullptr) {
1472bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz    return false;
1473bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  }
1474d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::JdwpError status;
1475d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Class* expected_class = DecodeClass(class_id, status);
1476d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  CHECK(expected_class != nullptr);
1477d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return expected_class->IsAssignableFrom(event_class);
1478d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
1479d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
1480d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzbool Dbg::MatchField(JDWP::RefTypeId expected_type_id, JDWP::FieldId expected_field_id,
1481d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz                     mirror::ArtField* event_field) {
1482d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::ArtField* expected_field = FromFieldId(expected_field_id);
1483d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  if (expected_field != event_field) {
1484d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    return false;
1485d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  }
1486d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return Dbg::MatchType(event_field->GetDeclaringClass(), expected_type_id);
1487d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
1488d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
1489d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzbool Dbg::MatchInstance(JDWP::ObjectId expected_instance_id, mirror::Object* event_instance) {
1490d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* modifier_instance = gRegistry->Get<mirror::Object*>(expected_instance_id);
1491d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return modifier_instance == event_instance;
1492d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
1493d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
1494d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzvoid Dbg::SetJdwpLocation(JDWP::JdwpLocation* location, mirror::ArtMethod* m, uint32_t dex_pc)
1495b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1496d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  if (m == nullptr) {
1497bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz    memset(location, 0, sizeof(*location));
149891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  } else {
14992dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = m->GetDeclaringClass();
1500d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->type_tag = GetTypeTag(c);
1501d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->class_id = gRegistry->AddRefType(c);
1502d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->method_id = ToMethodId(m);
1503d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint64_t>(-1) : dex_pc;
150491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
1505d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes}
1506d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
1507a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetMethodName(JDWP::MethodId method_id)
1508b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1509ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
1510bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  if (m == nullptr) {
1511bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz    return "NULL";
1512bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  }
1513bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  return m->GetName();
1514872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1515872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1516a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetFieldName(JDWP::FieldId field_id)
1517a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1518bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  mirror::ArtField* f = FromFieldId(field_id);
1519bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  if (f == nullptr) {
1520bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz    return "NULL";
1521bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  }
1522bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  return f->GetName();
1523a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes}
1524a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes
1525a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes/*
1526a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * Augment the access flags for synthetic methods and fields by setting
1527a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * the (as described by the spec) "0xf0000000 bit".  Also, strip out any
1528a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * flags not specified by the Java programming language.
1529a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes */
1530a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughesstatic uint32_t MangleAccessFlags(uint32_t accessFlags) {
1531a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  accessFlags &= kAccJavaFlagsMask;
1532a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  if ((accessFlags & kAccSynthetic) != 0) {
1533a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    accessFlags |= 0xf0000000;
1534a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1535a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return accessFlags;
1536872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1537872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1538dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes/*
1539b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come first. Debuggers
1540b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * expect slots to begin with arguments, but dex code places them at
1541b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * the end.
1542dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes */
1543b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Haostatic uint16_t MangleSlot(uint16_t slot, mirror::ArtMethod* m)
1544b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1545bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
1546cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1547cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1548cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1549cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m);
1550cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1551cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1552b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1553b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1554b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot >= locals_size) {
1555b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - locals_size;
1556b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1557b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + ins_size;
1558dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1559dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1560dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1561b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao/*
1562b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come last. Reverts
1563b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * slots to dex style argument placement.
1564b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao */
1565ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic uint16_t DemangleSlot(uint16_t slot, mirror::ArtMethod* m)
1566b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1567bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
1568cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1569cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1570cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1571cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m);
1572cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1573cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1574b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1575b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1576b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot < ins_size) {
1577b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + locals_size;
1578b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1579b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - ins_size;
1580dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1581dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1582dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
158388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1584436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
15852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1586436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1587436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
15887b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1589a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1590a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t instance_field_count = c->NumInstanceFields();
1591a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t static_field_count = c->NumStaticFields();
1592a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1593a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1594a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1595a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
1596ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count);
1597a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddFieldId(pReply, ToFieldId(f));
159861c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    expandBufAddUtf8String(pReply, f->GetName());
159961c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    expandBufAddUtf8String(pReply, f->GetTypeDescriptor());
1600c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1601a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1602a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1603a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1604a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1605a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1606436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1607a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1608a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
160988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
161000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                           JDWP::ExpandBuf* pReply) {
1611436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
16122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1613436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1614436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
16157b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1616a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1617a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t direct_method_count = c->NumDirectMethods();
1618a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t virtual_method_count = c->NumVirtualMethods();
1619a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1620a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
1621a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1622a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
1623ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
1624a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddMethodId(pReply, ToMethodId(m));
1625bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    expandBufAddUtf8String(pReply, m->GetName());
1626bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    expandBufAddUtf8String(pReply, m->GetSignature().ToString());
1627c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1628a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1629a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1630a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1631a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags()));
1632a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1633436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1634a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1635a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
163688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1637436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
1638f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  Thread* self = Thread::Current();
1639f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  StackHandleScope<1> hs(self);
1640f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  Handle<mirror::Class> c(hs.NewHandle(DecodeClass(class_id, status)));
1641f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  if (c.Get() == nullptr) {
1642436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
16437b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1644f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  size_t interface_count = c->NumDirectInterfaces();
1645a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, interface_count);
1646a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < interface_count; ++i) {
1647f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier    expandBufAddRefTypeId(pReply,
1648f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier                          gRegistry->AddRefType(mirror::Class::GetDirectInterface(self, c, i)));
1649a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1650436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1651872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1652872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
165388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply)
1654b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
165503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  struct DebugCallbackContext {
165603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    int numItems;
165703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    JDWP::ExpandBuf* pReply;
165803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
16592435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    static bool Callback(void* context, uint32_t address, uint32_t line_number) {
166003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
166103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      expandBufAdd8BE(pContext->pReply, address);
16622435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      expandBufAdd4BE(pContext->pReply, line_number);
166303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      pContext->numItems++;
1664f2910eef247b45ce1d489e323b36b5de6b6157aaSebastien Hertz      return false;
166503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
166603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
1667ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
1668bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
166903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  uint64_t start, end;
1670cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1671cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    DCHECK(m->IsNative() || m->IsProxyMethod());
167203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = -1;
167303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    end = -1;
167403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  } else {
167503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = 0;
167614f0db92225d34622fa5cb1a6dc9287334aaf6c7jeffhao    // Return the index of the last instruction
1677cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    end = code_item->insns_size_in_code_units_ - 1;
167803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  }
167903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
168003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, start);
168103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, end);
168203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
168303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  // Add numLines later
168403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  size_t numLinesOffset = expandBufGetLength(pReply);
168503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd4BE(pReply, 0);
168603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
168703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  DebugCallbackContext context;
168803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.numItems = 0;
168903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.pReply = pReply;
169003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1691cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1692bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
1693bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                     DebugCallbackContext::Callback, NULL, &context);
1694cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
169503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
169603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems);
1697872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1698872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1699bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartiervoid Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic,
1700bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                              JDWP::ExpandBuf* pReply) {
1701dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  struct DebugCallbackContext {
1702b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    mirror::ArtMethod* method;
1703dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    JDWP::ExpandBuf* pReply;
1704c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    size_t variable_count;
1705c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    bool with_generic;
1706dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1707bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress,
1708bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                         const char* name, const char* descriptor, const char* signature)
1709b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1710dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1711dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1712bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      VLOG(jdwp) << StringPrintf("    %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d",
1713bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                 pContext->variable_count, startAddress, endAddress - startAddress,
1714bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                 name, descriptor, signature, slot,
1715bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                 MangleSlot(slot, pContext->method));
1716dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1717b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao      slot = MangleSlot(slot, pContext->method);
171868fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
1719dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd8BE(pContext->pReply, startAddress);
1720dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, name);
1721dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, descriptor);
1722c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      if (pContext->with_generic) {
1723dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes        expandBufAddUtf8String(pContext->pReply, signature);
1724dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      }
1725dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, endAddress - startAddress);
1726dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, slot);
1727dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1728c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      ++pContext->variable_count;
1729dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    }
1730dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  };
1731ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
1732dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1733c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // arg_count considers doubles and longs to take 2 units.
1734c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // variable_count considers everything to take 1 unit.
1735bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  std::string shorty(m->GetShorty());
1736ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  expandBufAdd4BE(pReply, mirror::ArtMethod::NumArgRegisters(shorty));
1737dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1738c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // We don't know the total number of variables yet, so leave a blank and update it later.
1739c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  size_t variable_count_offset = expandBufGetLength(pReply);
1740dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  expandBufAdd4BE(pReply, 0);
1741dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1742dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  DebugCallbackContext context;
1743b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  context.method = m;
1744dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  context.pReply = pReply;
1745c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.variable_count = 0;
1746c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.with_generic = with_generic;
1747dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1748bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
1749cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1750bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    m->GetDexFile()->DecodeDebugInfo(
1751bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL, DebugCallbackContext::Callback,
1752bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        &context);
1753cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1754dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1755c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count);
1756872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1757872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1758579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value,
1759579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                                  JDWP::ExpandBuf* pReply) {
1760579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  mirror::ArtMethod* m = FromMethodId(method_id);
1761bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty());
1762579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  OutputJValue(tag, return_value, pReply);
1763579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1764579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
17653f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value,
17663f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                           JDWP::ExpandBuf* pReply) {
17673f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  mirror::ArtField* f = FromFieldId(field_id);
176861c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
17693f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  OutputJValue(tag, field_value, pReply);
17703f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
17713f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
17729777ba230c83a0edcbda2cf7b208339e77bf171bElliott HughesJDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
17739777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes                                  std::vector<uint8_t>& bytecodes)
17749777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1775ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
17769777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  if (m == NULL) {
17779777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    return JDWP::ERR_INVALID_METHODID;
17789777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
1779bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
17809777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  size_t byte_count = code_item->insns_size_in_code_units_ * 2;
17819777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_);
17829777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* end = begin + byte_count;
17839777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  for (const uint8_t* p = begin; p != end; ++p) {
17849777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    bytecodes.push_back(*p);
17859777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
17869777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  return JDWP::ERR_NONE;
17879777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes}
17889777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes
178988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
179061c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor());
1791872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1792872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
179388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
179461c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor());
1795872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1796872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
179788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
179888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                         JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
179900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         bool is_static)
1800b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
18010cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  JDWP::JdwpError status;
18022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(ref_type_id, status);
180388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  if (ref_type_id != 0 && c == NULL) {
18040cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    return status;
18050cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
18060cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
1807d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id);
180864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
18093f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
18103f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1811ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
18120cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
18132dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* receiver_class = c;
18140cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class == NULL && o != NULL) {
18150cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    receiver_class = o->GetClass();
18160cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
18170cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we give up now if receiver_class is NULL?
18180cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
18190cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class);
18203f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_FIELDID;
18213f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1822aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
18230cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
18240cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
18250cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
18260cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
18270cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
18280cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
18290cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
18300cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
18310cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
18320cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
18330cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
18340dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
18350dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
18360dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
18370cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
183861c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
1839579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  JValue field_value;
1840579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (tag == JDWP::JT_VOID) {
1841579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    LOG(FATAL) << "Unknown tag: " << tag;
1842579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (!IsPrimitiveTag(tag)) {
1843579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetL(f->GetObject(o));
1844579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1845579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetJ(f->Get64(o));
1846aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
1847579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetI(f->Get32(o));
1848aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
1849579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  Dbg::OutputJValue(tag, &field_value, pReply);
1850579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
18513f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  return JDWP::ERR_NONE;
1852872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1853872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
185488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
185500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ExpandBuf* pReply) {
185688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(0, object_id, field_id, pReply, false);
18573f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
18583f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
185988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) {
186088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
18613f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
18623f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
186388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
186400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         uint64_t value, int width, bool is_static)
1865b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1866d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id);
186764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
18683f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
18693f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1870ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
18710cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
18720cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
18730cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
18740cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
18750cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
18760cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
18770cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
18780cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
18790cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
18800cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
18810cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
18823f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
18830dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
18840dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
18850dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
1886aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
188761c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
1888aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1889aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  if (IsPrimitiveTag(tag)) {
1890aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
18911bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_EQ(width, 8);
1892d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1893d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set64<false>(o, value);
1894aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else {
18951bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_LE(width, 4);
1896d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1897d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set32<false>(o, value);
1898aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    }
1899aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
1900d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    mirror::Object* v = Dbg::GetObjectRegistry()->Get<mirror::Object*>(value);
190164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (v == ObjectRegistry::kInvalidObject) {
19023d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes      return JDWP::ERR_INVALID_OBJECT;
19033d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    }
19043f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    if (v != NULL) {
190561c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier      mirror::Class* field_type;
190661c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier      {
190761c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        StackHandleScope<3> hs(Thread::Current());
190861c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        HandleWrapper<mirror::Object> h_v(hs.NewHandleWrapper(&v));
190961c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f));
191061c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        HandleWrapper<mirror::Object> h_o(hs.NewHandleWrapper(&o));
191161c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        field_type = FieldHelper(h_f).GetType();
191261c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier      }
19133f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      if (!field_type->IsAssignableFrom(v->GetClass())) {
19143f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
19153f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      }
19163f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
1917d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    // Debugging can't use transactional mode (runtime only).
1918d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    f->SetObject<false>(o, v);
1919aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
19203d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
19213d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1922872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1923872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
192488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
192500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   int width) {
192688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(object_id, field_id, value, width, false);
1927872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1928872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
192988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
193088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(0, field_id, value, width, true);
1931872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1932872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
193329259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien HertzJDWP::JdwpError Dbg::StringToUtf8(JDWP::ObjectId string_id, std::string* str) {
193429259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  mirror::Object* obj = gRegistry->Get<mirror::Object*>(string_id);
193529259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  if (obj == nullptr || obj == ObjectRegistry::kInvalidObject) {
193629259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
193729259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  }
193829259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  {
193929259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz    ScopedObjectAccessUnchecked soa(Thread::Current());
194029259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz    mirror::Class* java_lang_String = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_String);
194129259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz    if (!java_lang_String->IsAssignableFrom(obj->GetClass())) {
194229259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz      // This isn't a string.
194329259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz      return JDWP::ERR_INVALID_STRING;
194429259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz    }
194529259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  }
194629259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  *str = obj->AsString()->ToModifiedUtf8();
194729259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  return JDWP::ERR_NONE;
1948872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1949872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1950579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) {
1951579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (IsPrimitiveTag(tag)) {
1952579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAdd1(pReply, tag);
1953579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
1954579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd1(pReply, return_value->GetI());
1955579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
1956579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd2BE(pReply, return_value->GetI());
1957579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
1958579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd4BE(pReply, return_value->GetI());
1959579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1960579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd8BE(pReply, return_value->GetJ());
1961579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else {
1962579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      CHECK_EQ(tag, JDWP::JT_VOID);
1963579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    }
1964579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else {
19659837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
1966579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    mirror::Object* value = return_value->GetL();
19679837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    expandBufAdd1(pReply, TagFromObject(soa, value));
1968579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAddObjectId(pReply, gRegistry->Add(value));
1969579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  }
1970579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1971579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
1972221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) {
1973a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  ScopedObjectAccessUnchecked soa(Thread::Current());
1974a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1975221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1976221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1977221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
1978221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1979a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1980221229cb523f849f165fdafbf9785010963715daElliott Hughes
1981221229cb523f849f165fdafbf9785010963715daElliott Hughes  // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
19822dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
1983ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* java_lang_Thread_name_field =
19842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
19852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s =
19862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object));
1987221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (s != NULL) {
1988221229cb523f849f165fdafbf9785010963715daElliott Hughes    name = s->ToModifiedUtf8();
1989221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1990221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1991872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1992872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1993221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
19945d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
19952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
199664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_object == ObjectRegistry::kInvalidObject) {
19972435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
19982435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
19999837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroup");
20002435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Okay, so it's an object, but is it actually a thread?
2001b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  JDWP::JdwpError error;
2002b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  {
2003b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2004b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    Thread* thread;
2005b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    error = DecodeThread(soa, thread_id, thread);
2006b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
2007221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
2008221229cb523f849f165fdafbf9785010963715daElliott Hughes    // Zombie threads are in the null group.
2009221229cb523f849f165fdafbf9785010963715daElliott Hughes    expandBufAddObjectId(pReply, JDWP::ObjectId(0));
201052d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    error = JDWP::ERR_NONE;
201152d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  } else if (error == JDWP::ERR_NONE) {
201252d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
201352d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(c != nullptr);
201452d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::ArtField* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;");
2015b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    CHECK(f != nullptr);
201652d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Object* group = f->GetObject(thread_object);
2017b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    CHECK(group != nullptr);
201852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    JDWP::ObjectId thread_group_id = gRegistry->Add(group);
201952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    expandBufAddObjectId(pReply, thread_group_id);
20202435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
20219837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
202252d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  return error;
2023872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2024872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
20255d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertzstatic mirror::Object* DecodeThreadGroup(ScopedObjectAccessUnchecked& soa,
20265d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz                                         JDWP::ObjectId thread_group_id, JDWP::JdwpError* error)
20275d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20285d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Object* thread_group = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_group_id);
20295d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  if (thread_group == nullptr || thread_group == ObjectRegistry::kInvalidObject) {
20305d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    *error = JDWP::ERR_INVALID_OBJECT;
20315d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    return nullptr;
20325d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
20339837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
20349837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
20355d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  if (!c->IsAssignableFrom(thread_group->GetClass())) {
20365d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    // This is not a java.lang.ThreadGroup.
20375d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    *error = JDWP::ERR_INVALID_THREAD_GROUP;
20385d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    return nullptr;
20395d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
20405d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  *error = JDWP::ERR_NONE;
20415d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  return thread_group;
20425d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz}
20435d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
20445d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien HertzJDWP::JdwpError Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) {
20455d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
20465d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  JDWP::JdwpError error;
20475d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
20485d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  if (error != JDWP::ERR_NONE) {
20495d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    return error;
20505d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
20515d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupName");
20525d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
2053ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("name", "Ljava/lang/String;");
2054499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(f != NULL);
20552dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group));
20569837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
20575d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
20585d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  std::string thread_group_name(s->ToModifiedUtf8());
20595d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  expandBufAddUtf8String(pReply, thread_group_name);
20605d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  return JDWP::ERR_NONE;
2061872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2062872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
20635d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien HertzJDWP::JdwpError Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) {
20649837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
20655d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  JDWP::JdwpError error;
20665d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
20675d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  if (error != JDWP::ERR_NONE) {
20685d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    return error;
20695d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
20709837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupParent");
20719837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
20729837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
2073ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;");
20744e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(f != NULL);
20752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* parent = f->GetObject(thread_group);
20769837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
20775d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
20785d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  JDWP::ObjectId parent_group_id = gRegistry->Add(parent);
20795d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  expandBufAddObjectId(pReply, parent_group_id);
20805d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  return JDWP::ERR_NONE;
2081872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2082872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
20835d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertzstatic void GetChildThreadGroups(ScopedObjectAccessUnchecked& soa, mirror::Object* thread_group,
20845d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz                                 std::vector<JDWP::ObjectId>* child_thread_group_ids)
20855d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20865d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  CHECK(thread_group != nullptr);
20875d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
20885d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  // Get the ArrayList<ThreadGroup> "groups" out of this thread group...
20895d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::ArtField* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;");
20905d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Object* groups_array_list = groups_field->GetObject(thread_group);
20915d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
20925d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  // Get the array and size out of the ArrayList<ThreadGroup>...
20935d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::ArtField* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;");
20945d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::ArtField* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I");
20955d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::ObjectArray<mirror::Object>* groups_array =
20965d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz      array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>();
20975d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  const int32_t size = size_field->GetInt(groups_array_list);
20985d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
20995d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  // Copy the first 'size' elements out of the array into the result.
21005d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  ObjectRegistry* registry = Dbg::GetObjectRegistry();
21015d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  for (int32_t i = 0; i < size; ++i) {
21025d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    child_thread_group_ids->push_back(registry->Add(groups_array->Get(i)));
21035d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
21045d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz}
21055d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
21065d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien HertzJDWP::JdwpError Dbg::GetThreadGroupChildren(JDWP::ObjectId thread_group_id,
21075d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz                                            JDWP::ExpandBuf* pReply) {
210800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
21095d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  JDWP::JdwpError error;
21105d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
21115d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  if (error != JDWP::ERR_NONE) {
21125d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    return error;
21135d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
21145d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
21155d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  // Add child threads.
21165d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  {
21175d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    std::vector<JDWP::ObjectId> child_thread_ids;
21185d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    GetThreads(thread_group, &child_thread_ids);
21195d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    expandBufAdd4BE(pReply, child_thread_ids.size());
21205d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    for (JDWP::ObjectId child_thread_id : child_thread_ids) {
21215d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz      expandBufAddObjectId(pReply, child_thread_id);
21225d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    }
21235d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
21245d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
21255d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  // Add child thread groups.
21265d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  {
21275d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    std::vector<JDWP::ObjectId> child_thread_groups_ids;
21285d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    GetChildThreadGroups(soa, thread_group, &child_thread_groups_ids);
21295d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    expandBufAdd4BE(pReply, child_thread_groups_ids.size());
21305d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    for (JDWP::ObjectId child_thread_group_id : child_thread_groups_ids) {
21315d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz      expandBufAddObjectId(pReply, child_thread_group_id);
21325d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    }
21335d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
21345d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
21355d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  return JDWP::ERR_NONE;
2136872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2137872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
21385d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien HertzJDWP::ObjectId Dbg::GetSystemThreadGroupId() {
21395d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
21405d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
21412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
2142365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
2143872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2144872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2145920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff HaoJDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) {
2146920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  switch (state) {
2147920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kBlocked:
2148920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_MONITOR;
2149920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kNative:
2150920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kRunnable:
2151920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSuspended:
2152920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_RUNNING;
2153920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSleeping:
2154920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_SLEEPING;
2155920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kStarting:
2156920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTerminated:
2157920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_ZOMBIE;
2158920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTimedWaiting:
2159bae182cbc6adc8796154162a87fc54ae804e0469Sebastien Hertz    case kWaitingForCheckPointsToRun:
2160920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSend:
2161920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSuspension:
2162920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerToAttach:
2163138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    case kWaitingForDeoptimization:
2164920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForGcToComplete:
2165920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForJniOnLoad:
2166bae182cbc6adc8796154162a87fc54ae804e0469Sebastien Hertz    case kWaitingForMethodTracingStart:
2167920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForSignalCatcherOutput:
2168920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainDebuggerLoop:
2169920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainSignalCatcherLoop:
2170920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingPerformingGc:
2171920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaiting:
2172920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_WAIT;
2173920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      // Don't add a 'default' here so the compiler can spot incompatible enum changes.
2174920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  }
2175920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  LOG(FATAL) << "Unknown thread state: " << state;
2176920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  return JDWP::TS_ZOMBIE;
2177920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao}
2178920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao
217952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus,
218052d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                     JDWP::JdwpSuspendStatus* pSuspendStatus) {
218100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
2182499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
21839e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
21849e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
218550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2186221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2187221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2188221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2189221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
2190221229cb523f849f165fdafbf9785010963715daElliott Hughes      *pThreadStatus = JDWP::TS_ZOMBIE;
2191221229cb523f849f165fdafbf9785010963715daElliott Hughes      return JDWP::ERR_NONE;
2192221229cb523f849f165fdafbf9785010963715daElliott Hughes    }
2193221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2194499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  }
2195499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
21969e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  if (IsSuspendedForDebugger(soa, thread)) {
21979e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
21989e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  }
219900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
2200920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  *pThreadStatus = ToJdwpThreadStatus(thread->GetState());
2201221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
2202872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2203872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2204221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
220500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
220650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2207221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2208221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2209221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2210221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
22112435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
221250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
221300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
22142435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
2215872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2216872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2217f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott HughesJDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
2218f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccess soa(Thread::Current());
2219f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2220f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
2221f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2222f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
2223f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
2224f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
2225dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  thread->Interrupt(soa.Self());
2226f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
2227f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
2228f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
2229d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertzstatic bool IsInDesiredThreadGroup(ScopedObjectAccessUnchecked& soa,
2230d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz                                   mirror::Object* desired_thread_group, mirror::Object* peer)
2231d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2232d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  // Do we want threads from all thread groups?
2233d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  if (desired_thread_group == nullptr) {
2234d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    return true;
2235d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  }
2236d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  mirror::ArtField* thread_group_field = soa.DecodeField(WellKnownClasses::java_lang_Thread_group);
2237d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  DCHECK(thread_group_field != nullptr);
2238d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  mirror::Object* group = thread_group_field->GetObject(peer);
2239d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  return (group == desired_thread_group);
2240d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz}
2241365c10235438607541fa2259a5fec48061b90bd8Ian Rogers
22425d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertzvoid Dbg::GetThreads(mirror::Object* thread_group, std::vector<JDWP::ObjectId>* thread_ids) {
2243d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
2244d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  std::list<Thread*> all_threads_list;
2245d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  {
2246d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
2247d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    all_threads_list = Runtime::Current()->GetThreadList()->GetList();
2248d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  }
2249d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  for (Thread* t : all_threads_list) {
2250d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    if (t == Dbg::GetDebugThread()) {
2251d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
2252d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // query all threads, so it's easier if we just don't tell them about this thread.
2253d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      continue;
2254a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
2255d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    if (t->IsStillStarting()) {
2256d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // This thread is being started (and has been registered in the thread list). However, it is
2257d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // not completely started yet so we must ignore it.
2258d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      continue;
2259a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
2260d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    mirror::Object* peer = t->GetPeer();
2261d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    if (peer == nullptr) {
2262d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // peer might be NULL if the thread is still starting up. We can't tell the debugger about
2263d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // this thread yet.
2264d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // TODO: if we identified threads to the debugger by their Thread*
2265d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // rather than their peer's mirror::Object*, we could fix this.
2266d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // Doing so might help us report ZOMBIE threads too.
2267d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      continue;
22680dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    }
2269d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    if (IsInDesiredThreadGroup(soa, thread_group, peer)) {
22705d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz      thread_ids->push_back(gRegistry->Add(peer));
2271d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    }
2272d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  }
2273a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes}
2274a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
22755d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertzstatic int GetStackDepth(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
22760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct CountStackDepthVisitor : public StackVisitor {
227793ba893c20532990a430741e0a97212900094e8cBrian Carlstrom    explicit CountStackDepthVisitor(Thread* thread)
22787a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth(0) {}
22790399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
228064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
228164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
228264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
22830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!GetMethod()->IsRuntimeMethod()) {
2284f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes        ++depth;
2285f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes      }
2286530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
2287a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
2288a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    size_t depth;
2289a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  };
229008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
22917a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  CountStackDepthVisitor visitor(thread);
22920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2293a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return visitor.depth;
2294872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2295872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2296221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) {
229700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
2298a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2299221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2300221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2301221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2302221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2303221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2304f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2305f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2306f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
2307221229cb523f849f165fdafbf9785010963715daElliott Hughes  result = GetStackDepth(thread);
2308221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
23098696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
23108696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2311306057fd278d75bf3794bd5243a3b6652c487d18Ian RogersJDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame,
2312306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers                                     size_t frame_count, JDWP::ExpandBuf* buf) {
23136e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  class GetFrameVisitor : public StackVisitor {
23146e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   public:
23157a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    GetFrameVisitor(Thread* thread, size_t start_frame, size_t frame_count, JDWP::ExpandBuf* buf)
2316b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
23177a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth_(0),
23186e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes          start_frame_(start_frame), frame_count_(frame_count), buf_(buf) {
23196e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      expandBufAdd4BE(buf_, frame_count_);
232003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
23210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
232200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
232300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
232400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
23250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetMethod()->IsRuntimeMethod()) {
23267934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        return true;  // The debugger can't do anything useful with a frame that has no Method*.
232703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
23286e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_ + frame_count_) {
2329530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes        return false;
233003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
23316e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_) {
23326e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::FrameId frame_id(GetFrameId());
23336e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::JdwpLocation location;
2334d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz        SetJdwpLocation(&location, GetMethod(), GetDexPc());
2335ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        VLOG(jdwp) << StringPrintf("    Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location;
23366e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAdd8BE(buf_, frame_id);
23376e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAddLocation(buf_, location);
23386e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      }
23396e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      ++depth_;
2340530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
234103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
23426e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes
23436e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   private:
23446e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    size_t depth_;
23456e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t start_frame_;
23466e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t frame_count_;
23476e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    JDWP::ExpandBuf* buf_;
234803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
234900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
235000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2351a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2352221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2353221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2354221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2355221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2356221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2357f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2358f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2359f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
23607a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetFrameVisitor visitor(thread, start_frame, frame_count, buf);
23610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
23626e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
2363872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2364872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2365872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetThreadSelfId() {
2366d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return GetThreadId(Thread::Current());
2367d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
2368d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
2369d539167b7f11136fe570a77aff2ee4935842007aSebastien HertzJDWP::ObjectId Dbg::GetThreadId(Thread* thread) {
2370dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier  ScopedObjectAccessUnchecked soa(Thread::Current());
2371d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return gRegistry->Add(thread->GetPeer());
2372872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2373872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2374475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::SuspendVM() {
237500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
2376872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2377872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2378872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ResumeVM() {
23794eec026ec4d86869baa27ad0afd9564cca8856c9Sebastien Hertz  Runtime::Current()->GetThreadList()->ResumeAllForDebugger();
2380872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2381872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2382221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
2383f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers  Thread* self = Thread::Current();
2384f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers  ScopedLocalRef<jobject> peer(self->GetJniEnv(), NULL);
238500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
2386f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers    ScopedObjectAccess soa(self);
23872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id)));
238800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
238900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (peer.get() == NULL) {
239000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
239100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
2392f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers  // Suspend thread to build stack trace. Take suspend thread lock to avoid races with threads
2393f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers  // trying to suspend this one.
2394f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers  MutexLock mu(self, *Locks::thread_list_suspend_thread_lock_);
2395f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  bool timed_out;
239637c16453a92bbf1a47f042000318a1b60381017dBrian Carlstrom  ThreadList* thread_list = Runtime::Current()->GetThreadList();
239737c16453a92bbf1a47f042000318a1b60381017dBrian Carlstrom  Thread* thread = thread_list->SuspendThreadByPeer(peer.get(), request_suspension, true,
239837c16453a92bbf1a47f042000318a1b60381017dBrian Carlstrom                                                    &timed_out);
239900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (thread != NULL) {
240000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_NONE;
2401f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else if (timed_out) {
240200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_INTERNAL;
240300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  } else {
240400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
24054e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
2406872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2407872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2408221229cb523f849f165fdafbf9785010963715daElliott Hughesvoid Dbg::ResumeThread(JDWP::ObjectId thread_id) {
240900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
24102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id);
2411a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  Thread* thread;
2412a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  {
2413a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2414a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    thread = Thread::FromManagedThread(soa, peer);
2415a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  }
24164e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  if (thread == NULL) {
24174e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    LOG(WARNING) << "No such thread for resume: " << peer;
24184e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    return;
24194e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
242000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool needs_resume;
242100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
242250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
242300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    needs_resume = thread->GetSuspendCount() > 0;
242400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
242500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (needs_resume) {
2426546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    Runtime::Current()->GetThreadList()->Resume(thread, true);
2427546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
2428872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2429872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2430872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::SuspendSelf() {
2431475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
2432872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2433872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
24340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct GetThisVisitor : public StackVisitor {
24357a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
2436b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
24377a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {}
243868fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
243900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
244000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
244100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
24426e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    if (frame_id != GetFrameId()) {
24430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return true;  // continue
24440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    } else {
244562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      this_object = GetThisObject();
244662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return false;
24470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
244868fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  }
2449dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
24502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* this_object;
24516e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  JDWP::FrameId frame_id;
24520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers};
2453ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
245400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersJDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
245500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ObjectId* result) {
245600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
245700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* thread;
245800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
245950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2460221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2461221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
2462221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
246300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
24649e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    if (!IsSuspendedForDebugger(soa, thread)) {
246500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return JDWP::ERR_THREAD_NOT_SUSPENDED;
246600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
24676e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  }
2468700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
24697a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor visitor(thread, context.get(), frame_id);
24700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
24716e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  *result = gRegistry->Add(visitor.this_object);
24726e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
24730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
24740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
24755797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz// Walks the stack until we find the frame with the given FrameId.
24765797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertzclass FindFrameVisitor FINAL : public StackVisitor {
24775797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz public:
24785797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  FindFrameVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
24795797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
24805797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      : StackVisitor(thread, context), frame_id_(frame_id), error_(JDWP::ERR_INVALID_FRAMEID) {}
2481ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
24825797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
24835797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // annotalysis.
24845797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
24855797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (GetFrameId() != frame_id_) {
24865797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      return true;  // Not our frame, carry on.
24870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
24885797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    mirror::ArtMethod* m = GetMethod();
24895797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (m->IsNative()) {
24905797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      // We can't read/write local value from/into native method.
24915797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      error_ = JDWP::ERR_OPAQUE_FRAME;
24925797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    } else {
24935797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      // We found our frame.
24945797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      error_ = JDWP::ERR_NONE;
24955797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
24965797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    return false;
24975797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
24985797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
24995797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::JdwpError GetError() const {
25005797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    return error_;
25015797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
25025797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
25035797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz private:
25045797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  const JDWP::FrameId frame_id_;
25055797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::JdwpError error_;
25065797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz};
25075797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
25085797aef3bbfa6fc762d384e25b31f6a121664987Sebastien HertzJDWP::JdwpError Dbg::GetLocalValues(JDWP::Request* request, JDWP::ExpandBuf* pReply) {
25095797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::ObjectId thread_id = request->ReadThreadId();
25105797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::FrameId frame_id = request->ReadFrameId();
251100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
251200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2513221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
25145797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  {
25155797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
25165797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
25175797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (error != JDWP::ERR_NONE) {
25185797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      return error;
25195797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
2520221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
25215797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // Find the frame with the given frame_id.
2522700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
25235797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  FindFrameVisitor visitor(thread, context.get(), frame_id);
25240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
25255797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  if (visitor.GetError() != JDWP::ERR_NONE) {
25265797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    return visitor.GetError();
25275797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
25280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
25295797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // Read the values from visitor's context.
25305797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  int32_t slot_count = request->ReadSigned32("slot count");
25315797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  expandBufAdd4BE(pReply, slot_count);     /* "int values" */
25325797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  for (int32_t i = 0; i < slot_count; ++i) {
25335797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    uint32_t slot = request->ReadUnsigned32("slot");
25345797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpTag reqSigByte = request->ReadTag();
2535ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
25365797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    VLOG(jdwp) << "    --> slot " << slot << " " << reqSigByte;
25375797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
25385797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    size_t width = Dbg::GetTagWidth(reqSigByte);
25395797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    uint8_t* ptr = expandBufAddSpace(pReply, width+1);
25405797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpError error = Dbg::GetLocalValue(visitor, soa, slot, reqSigByte, ptr, width);
25415797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (error != JDWP::ERR_NONE) {
25425797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      return error;
25435797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
25445797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
25455797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  return JDWP::ERR_NONE;
25465797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz}
25475797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
25485797aef3bbfa6fc762d384e25b31f6a121664987Sebastien HertzJDWP::JdwpError Dbg::GetLocalValue(const StackVisitor& visitor, ScopedObjectAccessUnchecked& soa,
25495797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                                   int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) {
25505797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  mirror::ArtMethod* m = visitor.GetMethod();
25515797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  uint16_t reg = DemangleSlot(slot, m);
25525797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // TODO: check that the tag is compatible with the actual type of the slot!
25535797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // TODO: check slot is valid for this method or return INVALID_SLOT error.
25545797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  constexpr JDWP::JdwpError kFailureErrorCode = JDWP::ERR_ABSENT_INFORMATION;
25555797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  switch (tag) {
25565797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_BOOLEAN: {
25575797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 1U);
25585797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
25595797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kIntVReg, &intVal)) {
25605797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get boolean local " << reg << " = " << intVal;
25615797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set1(buf + 1, intVal != 0);
25625797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
25635797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get boolean local " << reg;
25645797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
2565ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes      }
25665797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
25675797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
25685797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_BYTE: {
25695797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 1U);
25705797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
25715797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kIntVReg, &intVal)) {
25725797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get byte local " << reg << " = " << intVal;
25735797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set1(buf + 1, intVal);
25745797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
25755797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get byte local " << reg;
25765797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
2577cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      }
25785797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
25795797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
25805797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_SHORT:
25815797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CHAR: {
25825797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 2U);
25835797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
25845797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kIntVReg, &intVal)) {
25855797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get short/char local " << reg << " = " << intVal;
25865797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set2BE(buf + 1, intVal);
25875797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
25885797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get short/char local " << reg;
25895797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
25905797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
25915797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
25925797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
25935797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_INT: {
25945797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 4U);
25955797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
25965797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kIntVReg, &intVal)) {
25975797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get int local " << reg << " = " << intVal;
25985797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set4BE(buf + 1, intVal);
25995797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
26005797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get int local " << reg;
26015797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
26025797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
26035797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
26045797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
26055797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_FLOAT: {
26065797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 4U);
26075797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
26085797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kFloatVReg, &intVal)) {
26095797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get float local " << reg << " = " << intVal;
26105797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set4BE(buf + 1, intVal);
26115797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
26125797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get float local " << reg;
26135797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
26145797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
26155797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
26165797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
26175797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_ARRAY:
26185797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CLASS_LOADER:
26195797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CLASS_OBJECT:
26205797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_OBJECT:
26215797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_STRING:
26225797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_THREAD:
26235797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_THREAD_GROUP: {
26245797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, sizeof(JDWP::ObjectId));
26255797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
26265797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kReferenceVReg, &intVal)) {
26275797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        mirror::Object* o = reinterpret_cast<mirror::Object*>(intVal);
26285797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get " << tag << " object local " << reg << " = " << o;
26295797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
26305797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz          LOG(FATAL) << "Register " << reg << " expected to hold " << tag << " object: " << o;
26310bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        }
26325797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        tag = TagFromObject(soa, o);
26335797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::SetObjectId(buf + 1, gRegistry->Add(o));
26345797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
26355797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get " << tag << " object local " << reg;
26365797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
26370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
26385797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
26395797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
26405797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_DOUBLE: {
26415797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 8U);
26425797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint64_t longVal;
26435797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) {
26445797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get double local " << reg << " = " << longVal;
26455797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set8BE(buf + 1, longVal);
26465797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
26475797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get double local " << reg;
26485797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
26495797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
26505797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
2651cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    }
26525797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_LONG: {
26535797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 8U);
26545797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint64_t longVal;
26555797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &longVal)) {
26565797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get long local " << reg << " = " << longVal;
26575797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set8BE(buf + 1, longVal);
26585797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
26595797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get long local " << reg;
26605797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
26615797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
26625797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
26635797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
26645797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    default:
26655797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      LOG(FATAL) << "Unknown tag " << tag;
26665797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
26675797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
26680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
26695797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // Prepend tag, which may have been updated.
26705797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::Set1(buf, tag);
26715797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  return JDWP::ERR_NONE;
26725797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz}
26735797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
26745797aef3bbfa6fc762d384e25b31f6a121664987Sebastien HertzJDWP::JdwpError Dbg::SetLocalValues(JDWP::Request* request) {
26755797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::ObjectId thread_id = request->ReadThreadId();
26765797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::FrameId frame_id = request->ReadFrameId();
267700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
267800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2679221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
26805797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  {
26815797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
26825797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
26835797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (error != JDWP::ERR_NONE) {
26845797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      return error;
26855797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
2686221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
26875797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // Find the frame with the given frame_id.
2688700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
26895797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  FindFrameVisitor visitor(thread, context.get(), frame_id);
26900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
26915797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  if (visitor.GetError() != JDWP::ERR_NONE) {
26925797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    return visitor.GetError();
26935797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
26945797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
26955797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // Writes the values into visitor's context.
26965797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  int32_t slot_count = request->ReadSigned32("slot count");
26975797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  for (int32_t i = 0; i < slot_count; ++i) {
26985797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    uint32_t slot = request->ReadUnsigned32("slot");
26995797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpTag sigByte = request->ReadTag();
27005797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    size_t width = Dbg::GetTagWidth(sigByte);
27015797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    uint64_t value = request->ReadValue(width);
27025797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
27035797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    VLOG(jdwp) << "    --> slot " << slot << " " << sigByte << " " << value;
27045797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpError error = Dbg::SetLocalValue(visitor, slot, sigByte, value, width);
27055797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (error != JDWP::ERR_NONE) {
27065797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      return error;
27075797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
27085797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
27095797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  return JDWP::ERR_NONE;
27105797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz}
27115797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
27125797aef3bbfa6fc762d384e25b31f6a121664987Sebastien HertzJDWP::JdwpError Dbg::SetLocalValue(StackVisitor& visitor, int slot, JDWP::JdwpTag tag,
27135797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                                   uint64_t value, size_t width) {
27145797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  mirror::ArtMethod* m = visitor.GetMethod();
27155797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  uint16_t reg = DemangleSlot(slot, m);
27165797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // TODO: check that the tag is compatible with the actual type of the slot!
27175797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // TODO: check slot is valid for this method or return INVALID_SLOT error.
27185797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  constexpr JDWP::JdwpError kFailureErrorCode = JDWP::ERR_ABSENT_INFORMATION;
27195797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  switch (tag) {
27205797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_BOOLEAN:
27215797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_BYTE:
27225797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 1U);
27235797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVReg(m, reg, static_cast<uint32_t>(value), kIntVReg)) {
27245797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set boolean/byte local " << reg << " = "
27255797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                   << static_cast<uint32_t>(value);
27265797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27275797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27285797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27295797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_SHORT:
27305797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CHAR:
27315797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 2U);
27325797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVReg(m, reg, static_cast<uint32_t>(value), kIntVReg)) {
27335797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set short/char local " << reg << " = "
27345797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                   << static_cast<uint32_t>(value);
27355797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27365797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27375797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27385797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_INT:
27395797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 4U);
27405797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVReg(m, reg, static_cast<uint32_t>(value), kIntVReg)) {
27415797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set int local " << reg << " = "
27425797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                   << static_cast<uint32_t>(value);
27435797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27445797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27455797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27465797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_FLOAT:
27475797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 4U);
27485797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVReg(m, reg, static_cast<uint32_t>(value), kFloatVReg)) {
27495797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set float local " << reg << " = "
27505797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                   << static_cast<uint32_t>(value);
27515797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27525797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27535797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27545797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_ARRAY:
27555797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CLASS_LOADER:
27565797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CLASS_OBJECT:
27575797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_OBJECT:
27585797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_STRING:
27595797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_THREAD:
27605797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_THREAD_GROUP: {
27615797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, sizeof(JDWP::ObjectId));
27625797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value));
27635797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (o == ObjectRegistry::kInvalidObject) {
27645797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << tag << " object " << o << " is an invalid object";
27655797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return JDWP::ERR_INVALID_OBJECT;
27665797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else if (!visitor.SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)),
27675797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                          kReferenceVReg)) {
27685797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set " << tag << " object local " << reg << " = " << o;
27695797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27705797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27715797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27725797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
27735797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_DOUBLE: {
27745797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 8U);
27755797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVRegPair(m, reg, value, kDoubleLoVReg, kDoubleHiVReg)) {
27765797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set double local " << reg << " = " << value;
27775797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27785797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27795797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27805797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
27815797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_LONG: {
27825797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 8U);
27835797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVRegPair(m, reg, value, kLongLoVReg, kLongHiVReg)) {
27845797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set double local " << reg << " = " << value;
27855797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27865797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27875797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27885797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
27895797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    default:
27905797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      LOG(FATAL) << "Unknown tag " << tag;
27915797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27925797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
27935797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  return JDWP::ERR_NONE;
2794872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2795872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2796d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzstatic void SetEventLocation(JDWP::EventLocation* location, mirror::ArtMethod* m, uint32_t dex_pc)
2797d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2798d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  DCHECK(location != nullptr);
2799d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  if (m == nullptr) {
2800d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    memset(location, 0, sizeof(*location));
2801d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  } else {
2802d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->method = m;
2803d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint32_t>(-1) : dex_pc;
28043f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
28053f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
28063f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2807ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersvoid Dbg::PostLocationEvent(mirror::ArtMethod* m, int dex_pc, mirror::Object* this_object,
2808579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                            int event_flags, const JValue* return_value) {
28093f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
28103f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
28113f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
28123f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
28133f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK_EQ(m->IsStatic(), this_object == nullptr);
2814d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::EventLocation location;
2815d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  SetEventLocation(&location, m, dex_pc);
281691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
2817d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  gJdwpState->PostLocationEvent(&location, this_object, event_flags, return_value);
2818872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2819872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
28203f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldAccessEvent(mirror::ArtMethod* m, int dex_pc,
28213f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                               mirror::Object* this_object, mirror::ArtField* f) {
28223f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
28233f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
28243f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
28253f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
28263f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
2827d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::EventLocation location;
2828d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  SetEventLocation(&location, m, dex_pc);
28293f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2830d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  gJdwpState->PostFieldEvent(&location, f, this_object, nullptr, false);
28313f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
28323f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
28333f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldModificationEvent(mirror::ArtMethod* m, int dex_pc,
28343f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     mirror::Object* this_object, mirror::ArtField* f,
28353f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     const JValue* field_value) {
28363f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
28373f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
28383f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
28393f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
28403f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
28413f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(field_value != nullptr);
2842d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::EventLocation location;
2843d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  SetEventLocation(&location, m, dex_pc);
28443f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2845d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  gJdwpState->PostFieldEvent(&location, f, this_object, field_value, true);
28463f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
28473f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
28483f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostException(const ThrowLocation& throw_location,
2849ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                        mirror::ArtMethod* catch_method,
285064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes                        uint32_t catch_dex_pc, mirror::Throwable* exception_object) {
2851c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
28520ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers    return;
28530ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers  }
2854d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::EventLocation exception_throw_location;
2855d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  SetEventLocation(&exception_throw_location, throw_location.GetMethod(), throw_location.GetDexPc());
2856d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::EventLocation exception_catch_location;
2857d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  SetEventLocation(&exception_catch_location, catch_method, catch_dex_pc);
28584740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
2859d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  gJdwpState->PostException(&exception_throw_location, exception_object, &exception_catch_location,
2860d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz                            throw_location.GetThis());
2861872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2862872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
28632dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::PostClassPrepare(mirror::Class* c) {
2864c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
28654740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes    return;
28664740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  }
2867d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  gJdwpState->PostClassPrepare(c);
2868872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2869872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
287062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object,
28718379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz                         mirror::ArtMethod* m, uint32_t dex_pc,
28728379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz                         int event_flags, const JValue* return_value) {
287362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
28742aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes    return;
287591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
287691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
28778696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  if (IsBreakpoint(m, dex_pc)) {
28788696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    event_flags |= kBreakpoint;
287991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
288091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
288161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // If the debugger is single-stepping one of our threads, check to
288261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // see if we're that thread and we've reached a step point.
288361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  const SingleStepControl* single_step_control = thread->GetSingleStepControl();
288461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
288561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (single_step_control->is_active) {
288661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    CHECK(!m->IsNative());
288761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    if (single_step_control->step_depth == JDWP::SD_INTO) {
288861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step into method calls.  We break when the line number
288961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // or method pointer changes.  If we're in SS_MIN mode, we
289061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // always stop.
289161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (single_step_control->method != m) {
289261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
289361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new method";
289461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (single_step_control->step_size == JDWP::SS_MIN) {
289561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
289661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new instruction";
2897bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      } else if (single_step_control->ContainsDexPc(dex_pc)) {
289861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
289961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new line";
290061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
290161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else if (single_step_control->step_depth == JDWP::SD_OVER) {
290261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step over method calls.  We break when the line number is
290361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // different and the frame depth is <= the original frame
290461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth.  (We can't just compare on the method, because we
290561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // might get unrolled past it by an exception, and it's tricky
290661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // to identify recursion.)
290761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
290861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
290961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
291061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
291161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Popped up one or more frames, always trigger.
291261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
291361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
291461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (stack_depth == single_step_control->stack_depth) {
291561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Same depth, see if we moved.
291661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control->step_size == JDWP::SS_MIN) {
29178696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          event_flags |= kSingleStep;
29188696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          VLOG(jdwp) << "SS new instruction";
2919bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        } else if (single_step_control->ContainsDexPc(dex_pc)) {
29202435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          event_flags |= kSingleStep;
29212435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          VLOG(jdwp) << "SS new line";
292291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes        }
292361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
292461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else {
292561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      CHECK_EQ(single_step_control->step_depth, JDWP::SD_OUT);
292661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Return from the current method.  We break when the frame
292761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth pops up.
292861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
292961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // This differs from the "method exit" break in that it stops
293061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // with the PC at the next instruction in the returned-to
293161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // function, rather than the end of the returning function.
293261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
293361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
293461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
293561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
293661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
29378696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
293891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    }
293991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
294091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
294191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // If there's something interesting going on, see if it matches one
294291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // of the debugger filters.
294391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (event_flags != 0) {
29448379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value);
294591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
294691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes}
294791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
294842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) {
294942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  switch (instrumentation_event) {
295042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kMethodEntered:
295142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &method_enter_event_ref_count_;
295242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kMethodExited:
295342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &method_exit_event_ref_count_;
295442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kDexPcMoved:
295542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &dex_pc_change_event_ref_count_;
295642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kFieldRead:
295742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &field_read_event_ref_count_;
295842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kFieldWritten:
295942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &field_write_event_ref_count_;
296042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kExceptionCaught:
296142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &exception_catch_event_ref_count_;
296242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    default:
296342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return nullptr;
296442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  }
296542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz}
296642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz
29674d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz// Process request while all mutator threads are suspended.
29684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) {
2969138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
29700ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  switch (request.GetKind()) {
29714d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kNothing:
29724d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(WARNING) << "Ignoring empty deoptimization request.";
29734d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
297442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kRegisterForEvent:
297542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x",
29760ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                 request.InstrumentationEvent());
29770ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent());
29780ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation_events_ |= request.InstrumentationEvent();
297942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
298042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kUnregisterForEvent:
298142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x",
29820ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                 request.InstrumentationEvent());
298342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      instrumentation->RemoveListener(&gDebugInstrumentationListener,
29840ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                      request.InstrumentationEvent());
29850ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation_events_ &= ~request.InstrumentationEvent();
298642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
29874d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization:
29887ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world ...";
29894d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->DeoptimizeEverything();
29907ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world DONE";
29914d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
29924d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization:
29937ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world ...";
29944d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->UndeoptimizeEverything();
29957ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world DONE";
29964d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
29974d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization:
29980ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " ...";
29990ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation->Deoptimize(request.Method());
30000ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " DONE";
30014d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
30024d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization:
30030ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " ...";
30040ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation->Undeoptimize(request.Method());
30050ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " DONE";
30064d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
30074d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default:
30080ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind();
30094d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
30104d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
30114d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz}
30124d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz
30137ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::DelayFullUndeoptimization() {
3014f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  if (RequiresDeoptimization()) {
3015f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
3016f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    ++delayed_full_undeoptimization_count_;
3017f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    DCHECK_LE(delayed_full_undeoptimization_count_, full_deoptimization_event_count_);
3018f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  }
30197ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
30207ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
30217ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::ProcessDelayedFullUndeoptimizations() {
30227ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  // TODO: avoid taking the lock twice (once here and once in ManageDeoptimization).
30237ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  {
3024f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
30257ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    while (delayed_full_undeoptimization_count_ > 0) {
30267ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      DeoptimizationRequest req;
30270ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req.SetKind(DeoptimizationRequest::kFullUndeoptimization);
30280ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req.SetMethod(nullptr);
30297ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      RequestDeoptimizationLocked(req);
30307ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      --delayed_full_undeoptimization_count_;
30317ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    }
30327ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  }
30337ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  ManageDeoptimization();
30347ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
30357ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
30364d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::RequestDeoptimization(const DeoptimizationRequest& req) {
30370ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  if (req.GetKind() == DeoptimizationRequest::kNothing) {
30384d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    // Nothing to do.
30394d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    return;
30404d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
3041f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
30427ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  RequestDeoptimizationLocked(req);
30437ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
30447ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
30457ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) {
30460ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  switch (req.GetKind()) {
304742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kRegisterForEvent: {
30480ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK_NE(req.InstrumentationEvent(), 0u);
30490ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent());
305042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
30510ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                                req.InstrumentationEvent());
305242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      if (*counter == 0) {
30537d2ae437a87ceb2bdda098ab11f4da588c6a75f5Sebastien Hertz        VLOG(jdwp) << StringPrintf("Queue request #%zd to start listening to instrumentation event 0x%x",
30540ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                   deoptimization_requests_.size(), req.InstrumentationEvent());
305542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz        deoptimization_requests_.push_back(req);
305642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      }
305742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      *counter = *counter + 1;
305842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
305942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
306042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kUnregisterForEvent: {
30610ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK_NE(req.InstrumentationEvent(), 0u);
30620ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent());
306342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
30640ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                                req.InstrumentationEvent());
306542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      *counter = *counter - 1;
306642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      if (*counter == 0) {
30677d2ae437a87ceb2bdda098ab11f4da588c6a75f5Sebastien Hertz        VLOG(jdwp) << StringPrintf("Queue request #%zd to stop listening to instrumentation event 0x%x",
30680ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                   deoptimization_requests_.size(), req.InstrumentationEvent());
306942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz        deoptimization_requests_.push_back(req);
307042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      }
307142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
307242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
30734d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization: {
30740ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() == nullptr);
30754d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
30767ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
30777ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full deoptimization";
30784d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
3079138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
30804d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ++full_deoptimization_event_count_;
30814d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
30824d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
30834d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization: {
30840ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() == nullptr);
3085e713d9338ad122d6b8c7997387d0c9fc464eea3eSebastien Hertz      DCHECK_GT(full_deoptimization_event_count_, 0U);
30864d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      --full_deoptimization_event_count_;
30874d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
30887ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
30897ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full undeoptimization";
30904d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
3091138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
30924d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
30934d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
30944d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization: {
30950ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() != nullptr);
30967ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
30970ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                 << " for deoptimization of " << PrettyMethod(req.Method());
30984d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
30994d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
31004d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
31014d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization: {
31020ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() != nullptr);
31037ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
31040ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                 << " for undeoptimization of " << PrettyMethod(req.Method());
31054d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
31064d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
31074d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
31084d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default: {
31090ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind();
31104d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
3111138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
3112138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3113138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
3114138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3115138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertzvoid Dbg::ManageDeoptimization() {
3116138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Thread* const self = Thread::Current();
3117138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
3118138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    // Avoid suspend/resume if there is no pending request.
3119f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    MutexLock mu(self, *Locks::deoptimization_lock_);
31204d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    if (deoptimization_requests_.empty()) {
3121138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      return;
3122138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
3123138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3124138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
3125138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization);
3126138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  // We need to suspend mutator threads first.
3127138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Runtime* const runtime = Runtime::Current();
3128138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->SuspendAll();
3129138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  const ThreadState old_state = self->SetStateUnsafe(kRunnable);
31304d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  {
3131f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    MutexLock mu(self, *Locks::deoptimization_lock_);
31327ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    size_t req_index = 0;
31330ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    for (DeoptimizationRequest& request : deoptimization_requests_) {
31347ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Process deoptimization request #" << req_index++;
31354d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ProcessDeoptimizationRequest(request);
31364d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
31374d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    deoptimization_requests_.clear();
31384d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
3139138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
3140138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->ResumeAll();
3141138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromSuspendedToRunnable();
3142138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
3143138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3144a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic bool IsMethodPossiblyInlined(Thread* self, mirror::ArtMethod* m)
3145a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3146bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
3147a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (code_item == nullptr) {
3148a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // TODO We should not be asked to watch location in a native or abstract method so the code item
3149a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // should never be null. We could just check we never encounter this case.
3150a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    return false;
3151a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
31524d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  // Note: method verifier may cause thread suspension.
31534d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  self->AssertThreadSuspensionIsAllowable();
3154eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<2> hs(self);
3155bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  mirror::Class* declaring_class = m->GetDeclaringClass();
3156bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
3157bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
3158bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  verifier::MethodVerifier verifier(dex_cache->GetDexFile(), &dex_cache, &class_loader,
3159bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                    &m->GetClassDef(), code_item, m->GetDexMethodIndex(), m,
316046960fe5dcc1be07b39a55114338423a73554449Ian Rogers                                    m->GetAccessFlags(), false, true, false);
3161a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // Note: we don't need to verify the method.
3162a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr);
3163a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
3164138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3165a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic const Breakpoint* FindFirstBreakpointForMethod(mirror::ArtMethod* m)
316659d9d668d4f4286813afe2b4e7c6db839222ce96Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) {
31670ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  for (Breakpoint& breakpoint : gBreakpoints) {
31680ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    if (breakpoint.Method() == m) {
3169a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      return &breakpoint;
3170138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
3171a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
3172a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return nullptr;
3173a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
3174138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3175a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Sanity checks all existing breakpoints on the same method.
3176f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertzstatic void SanityCheckExistingBreakpoints(mirror::ArtMethod* m,
3177f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz                                           DeoptimizationRequest::Kind deoptimization_kind)
317859d9d668d4f4286813afe2b4e7c6db839222ce96Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) {
31794d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  for (const Breakpoint& breakpoint : gBreakpoints) {
3180f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    if (breakpoint.Method() == m) {
3181f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      CHECK_EQ(deoptimization_kind, breakpoint.GetDeoptimizationKind());
3182f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    }
31834d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  }
3184f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
3185f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) {
31864d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // We should have deoptimized everything but not "selectively" deoptimized this method.
3187f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK(instrumentation->AreAllMethodsDeoptimized());
3188f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK(!instrumentation->IsDeoptimized(m));
3189f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
31904d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // We should have "selectively" deoptimized this method.
31914d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // Note: while we have not deoptimized everything for this method, we may have done it for
31924d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // another event.
3193f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK(instrumentation->IsDeoptimized(m));
3194f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  } else {
3195f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    // This method does not require deoptimization.
3196f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing);
3197f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK(!instrumentation->IsDeoptimized(m));
3198138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3199a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
3200138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
32012f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz// Returns the deoptimization kind required to set a breakpoint in a method.
32022f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz// If a breakpoint has already been set, we also return the first breakpoint
32032f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz// through the given 'existing_brkpt' pointer.
3204f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertzstatic DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self,
32052f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz                                                                 mirror::ArtMethod* m,
32062f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz                                                                 const Breakpoint** existing_brkpt)
3207f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3208f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  if (!Dbg::RequiresDeoptimization()) {
3209f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    // We already run in interpreter-only mode so we don't need to deoptimize anything.
3210f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    VLOG(jdwp) << "No need for deoptimization when fully running with interpreter for method "
3211f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz               << PrettyMethod(m);
3212f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    return DeoptimizationRequest::kNothing;
3213f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  }
32142f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz  const Breakpoint* first_breakpoint;
32154d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  {
32164d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    ReaderMutexLock mu(self, *Locks::breakpoint_lock_);
32172f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz    first_breakpoint = FindFirstBreakpointForMethod(m);
32182f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz    *existing_brkpt = first_breakpoint;
32194d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  }
32202f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz
32212f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz  if (first_breakpoint == nullptr) {
3222a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no breakpoint on this method yet: we need to deoptimize. If this method may be
3223a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // inlined, we deoptimize everything; otherwise we deoptimize only this method.
32244d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // Note: IsMethodPossiblyInlined goes into the method verifier and may cause thread suspension.
32254d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // Therefore we must not hold any lock when we call it.
3226f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    bool need_full_deoptimization = IsMethodPossiblyInlined(self, m);
3227a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
3228f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      VLOG(jdwp) << "Need full deoptimization because of possible inlining of method "
3229f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz                 << PrettyMethod(m);
3230f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      return DeoptimizationRequest::kFullDeoptimization;
3231a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
3232f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      // We don't need to deoptimize if the method has not been compiled.
3233f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
3234f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      const bool is_compiled = class_linker->GetOatMethodQuickCodeFor(m) != nullptr;
3235f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      if (is_compiled) {
32367f418db815f0eaef5b2f43e1f06fb8773a415494Sebastien Hertz        // If the method may be called through its direct code pointer (without loading
32377f418db815f0eaef5b2f43e1f06fb8773a415494Sebastien Hertz        // its updated entrypoint), we need full deoptimization to not miss the breakpoint.
32387f418db815f0eaef5b2f43e1f06fb8773a415494Sebastien Hertz        if (class_linker->MayBeCalledWithDirectCodePointer(m)) {
32397f418db815f0eaef5b2f43e1f06fb8773a415494Sebastien Hertz          VLOG(jdwp) << "Need full deoptimization because of possible direct code call "
32407f418db815f0eaef5b2f43e1f06fb8773a415494Sebastien Hertz                     << "into image for compiled method " << PrettyMethod(m);
32417f418db815f0eaef5b2f43e1f06fb8773a415494Sebastien Hertz          return DeoptimizationRequest::kFullDeoptimization;
32427f418db815f0eaef5b2f43e1f06fb8773a415494Sebastien Hertz        } else {
32437f418db815f0eaef5b2f43e1f06fb8773a415494Sebastien Hertz          VLOG(jdwp) << "Need selective deoptimization for compiled method " << PrettyMethod(m);
32447f418db815f0eaef5b2f43e1f06fb8773a415494Sebastien Hertz          return DeoptimizationRequest::kSelectiveDeoptimization;
32457f418db815f0eaef5b2f43e1f06fb8773a415494Sebastien Hertz        }
3246f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      } else {
3247f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz        // Method is not compiled: we don't need to deoptimize.
3248f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz        VLOG(jdwp) << "No need for deoptimization for non-compiled method " << PrettyMethod(m);
3249f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz        return DeoptimizationRequest::kNothing;
3250f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      }
3251a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
3252a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
3253a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to deoptimize.
3254f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    // Let's check that all breakpoints are configured the same way for deoptimization.
3255f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    VLOG(jdwp) << "Breakpoint already set: no deoptimization is required";
32562f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz    DeoptimizationRequest::Kind deoptimization_kind = first_breakpoint->GetDeoptimizationKind();
32574d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    if (kIsDebugBuild) {
32584d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz      ReaderMutexLock mu(self, *Locks::breakpoint_lock_);
3259f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      SanityCheckExistingBreakpoints(m, deoptimization_kind);
32604d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    }
3261f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    return DeoptimizationRequest::kNothing;
3262f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  }
3263f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz}
3264f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz
3265f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz// Installs a breakpoint at the specified location. Also indicates through the deoptimization
3266f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz// request if we need to deoptimize.
3267f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertzvoid Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
3268f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  Thread* const self = Thread::Current();
3269f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  mirror::ArtMethod* m = FromMethodId(location->method_id);
3270f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
3271f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz
32722f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz  const Breakpoint* existing_breakpoint = nullptr;
32732f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz  const DeoptimizationRequest::Kind deoptimization_kind =
32742f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz      GetRequiredDeoptimizationKind(self, m, &existing_breakpoint);
3275f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  req->SetKind(deoptimization_kind);
3276f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
3277f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    req->SetMethod(m);
3278f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  } else {
3279f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK(deoptimization_kind == DeoptimizationRequest::kNothing ||
3280f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz          deoptimization_kind == DeoptimizationRequest::kFullDeoptimization);
3281f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    req->SetMethod(nullptr);
3282138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3283a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
32844d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  {
32854d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    WriterMutexLock mu(self, *Locks::breakpoint_lock_);
32862f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz    // If there is at least one existing breakpoint on the same method, the new breakpoint
32872f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz    // must have the same deoptimization kind than the existing breakpoint(s).
32882f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz    DeoptimizationRequest::Kind breakpoint_deoptimization_kind;
32892f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz    if (existing_breakpoint != nullptr) {
32902f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz      breakpoint_deoptimization_kind = existing_breakpoint->GetDeoptimizationKind();
32912f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz    } else {
32922f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz      breakpoint_deoptimization_kind = deoptimization_kind;
32932f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz    }
32942f5c202624f733d97fecdd4e31915199e0c77707Sebastien Hertz    gBreakpoints.push_back(Breakpoint(m, location->dex_pc, breakpoint_deoptimization_kind));
32954d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": "
32964d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz               << gBreakpoints[gBreakpoints.size() - 1];
32974d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  }
3298872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3299872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3300a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization
3301a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// request if we need to undeoptimize.
33024d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
330359d9d668d4f4286813afe2b4e7c6db839222ce96Sebastien Hertz  WriterMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
3304ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(location->method_id);
3305a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
3306f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  DeoptimizationRequest::Kind deoptimization_kind = DeoptimizationRequest::kNothing;
3307a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
33080ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].Method() == m) {
3309a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
3310f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      deoptimization_kind = gBreakpoints[i].GetDeoptimizationKind();
3311f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      DCHECK_EQ(deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization,
3312f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz                Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
3313a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      gBreakpoints.erase(gBreakpoints.begin() + i);
3314a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      break;
3315138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
3316138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3317a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
3318a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (existing_breakpoint == nullptr) {
3319a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no more breakpoint on this method: we need to undeoptimize.
3320f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) {
3321a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required full deoptimization: we need to undeoptimize everything.
33220ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetKind(DeoptimizationRequest::kFullUndeoptimization);
33230ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetMethod(nullptr);
3324f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
3325a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required selective deoptimization: we need to undeoptimize only that method.
33260ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization);
33270ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetMethod(m);
3328f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    } else {
3329f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      // This method had no need for deoptimization: do nothing.
3330f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing);
3331f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      req->SetKind(DeoptimizationRequest::kNothing);
3332f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      req->SetMethod(nullptr);
3333a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
3334a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
3335a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to undeoptimize.
33360ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    req->SetKind(DeoptimizationRequest::kNothing);
33370ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    req->SetMethod(nullptr);
33384d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    if (kIsDebugBuild) {
3339f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      SanityCheckExistingBreakpoints(m, deoptimization_kind);
33404d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    }
33418696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
3342872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3343872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3344449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
3345449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// cause suspension if the thread is the current thread.
3346449db33fafa29578df60e8a323f78d5eb6247e76Jeff Haoclass ScopedThreadSuspension {
3347449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao public:
334833e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers  ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
334952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz      LOCKS_EXCLUDED(Locks::thread_list_lock_)
335033e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
3351f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers      thread_(nullptr),
3352449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_(JDWP::ERR_NONE),
3353449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      self_suspend_(false),
335433e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      other_suspend_(false) {
3355449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    ScopedObjectAccessUnchecked soa(self);
3356449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    {
3357449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
3358449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_ = DecodeThread(soa, thread_id, thread_);
3359449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3360449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (error_ == JDWP::ERR_NONE) {
3361449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      if (thread_ == soa.Self()) {
3362449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        self_suspend_ = true;
3363449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      } else {
3364449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
3365d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz        jobject thread_peer = Dbg::GetObjectRegistry()->GetJObject(thread_id);
3366449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        bool timed_out;
3367f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers        Thread* suspended_thread;
3368f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers        {
3369f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers          // Take suspend thread lock to avoid races with threads trying to suspend this one.
3370f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers          MutexLock mu(soa.Self(), *Locks::thread_list_suspend_thread_lock_);
337137c16453a92bbf1a47f042000318a1b60381017dBrian Carlstrom          ThreadList* thread_list = Runtime::Current()->GetThreadList();
337237c16453a92bbf1a47f042000318a1b60381017dBrian Carlstrom          suspended_thread = thread_list->SuspendThreadByPeer(thread_peer, true, true, &timed_out);
3373f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers        }
3374449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension);
3375f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers        if (suspended_thread == nullptr) {
3376449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          // Thread terminated from under us while suspending.
3377449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          error_ = JDWP::ERR_INVALID_THREAD;
3378449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        } else {
3379449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          CHECK_EQ(suspended_thread, thread_);
3380449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          other_suspend_ = true;
3381449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        }
3382449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      }
3383449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3384449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3385449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3386449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* GetThread() const {
3387449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return thread_;
3388449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3389449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3390449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError GetError() const {
3391449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return error_;
3392449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3393449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3394449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ~ScopedThreadSuspension() {
3395449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (other_suspend_) {
3396449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      Runtime::Current()->GetThreadList()->Resume(thread_, true);
3397449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3398449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3399449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3400449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao private:
3401449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* thread_;
3402449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError error_;
3403449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool self_suspend_;
3404449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool other_suspend_;
3405449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao};
3406449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3407221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
340800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::JdwpStepDepth step_depth) {
3409449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* self = Thread::Current();
3410449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ScopedThreadSuspension sts(self, thread_id);
3411449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  if (sts.GetError() != JDWP::ERR_NONE) {
3412449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return sts.GetError();
34132435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
34148696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
34152435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
34162435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Work out what Method* we're in, the current line number, and how deep the stack currently
34172435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // is for step-out.
34182435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
34192435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
34200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SingleStepStackVisitor : public StackVisitor {
342161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    explicit SingleStepStackVisitor(Thread* thread, SingleStepControl* single_step_control,
342261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz                                    int32_t* line_number)
3423b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
342461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        : StackVisitor(thread, NULL), single_step_control_(single_step_control),
342561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          line_number_(line_number) {
342661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      DCHECK_EQ(single_step_control_, thread->GetSingleStepControl());
342761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->method = NULL;
342861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->stack_depth = 0;
34298696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
3430ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
343100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
343200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
343300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
343461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      mirror::ArtMethod* m = GetMethod();
34350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!m->IsRuntimeMethod()) {
343661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        ++single_step_control_->stack_depth;
343761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control_->method == NULL) {
3438ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache();
343961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->method = m;
344061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          *line_number_ = -1;
34412435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          if (dex_cache != NULL) {
34424445a7e3398a6143939168097a3aa275b734504dIan Rogers            const DexFile& dex_file = *dex_cache->GetDexFile();
344361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz            *line_number_ = dex_file.GetLineNumFromPC(m, GetDexPc());
34442435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          }
34458696433d1b3d8ba15288483b777edd888de69135Elliott Hughes        }
34468696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
3447530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
34488696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
344961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
345061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
345161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    int32_t* const line_number_;
34528696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  };
3453449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
345461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* const thread = sts.GetThread();
345561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepControl* const single_step_control = thread->GetSingleStepControl();
345661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
345761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  int32_t line_number = -1;
345861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepStackVisitor visitor(thread, single_step_control, &line_number);
34590399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
34608696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
34612435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
34622435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
34632435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
34642435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
34652435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  struct DebugCallbackContext {
3466bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    explicit DebugCallbackContext(SingleStepControl* single_step_control, int32_t line_number,
3467bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz                                  const DexFile::CodeItem* code_item)
3468bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      : single_step_control_(single_step_control), line_number_(line_number), code_item_(code_item),
346961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        last_pc_valid(false), last_pc(0) {
34702435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
34712435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
347261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) {
34732435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context);
347461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (static_cast<int32_t>(line_number) == context->line_number_) {
34752435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        if (!context->last_pc_valid) {
34762435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          // Everything from this address until the next line change is ours.
34772435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc = address;
34782435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc_valid = true;
34792435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
34802435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Otherwise, if we're already in a valid range for this line,
34812435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // just keep going (shouldn't really happen)...
34827934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      } else if (context->last_pc_valid) {  // and the line number is new
34832435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Add everything from the last entry up until here to the set
34842435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) {
348561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          context->single_step_control_->dex_pcs.insert(dex_pc);
34862435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
34872435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        context->last_pc_valid = false;
34882435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
34897934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return false;  // There may be multiple entries for any given line.
34902435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
34912435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
349261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    ~DebugCallbackContext() {
34932435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      // If the line number was the last in the position table...
34942435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      if (last_pc_valid) {
3495bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        size_t end = code_item_->insns_size_in_code_units_;
34962435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) {
349761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->dex_pcs.insert(dex_pc);
34982435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
34992435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
35002435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
35012435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
350261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
350361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    const int32_t line_number_;
3504bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    const DexFile::CodeItem* const code_item_;
35052435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    bool last_pc_valid;
35062435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    uint32_t last_pc;
35072435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  };
350861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->dex_pcs.clear();
3509ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  mirror::ArtMethod* m = single_step_control->method;
351061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (!m->IsNative()) {
3511bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    const DexFile::CodeItem* const code_item = m->GetCodeItem();
3512bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    DebugCallbackContext context(single_step_control, line_number, code_item);
3513bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
3514bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                     DebugCallbackContext::Callback, NULL, &context);
35153e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes  }
35162435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
35172435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
35182435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Everything else...
35192435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
35202435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
352161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_size = step_size;
352261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_depth = step_depth;
352361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->is_active = true;
35248696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
35252435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  if (VLOG_IS_ON(jdwp)) {
352661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step thread: " << *thread;
352761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step size: " << single_step_control->step_size;
352861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step depth: " << single_step_control->step_depth;
352961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->method);
353061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current line: " << line_number;
353161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->stack_depth;
35322435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step dex_pc values:";
3533bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    for (uint32_t dex_pc : single_step_control->dex_pcs) {
3534bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      VLOG(jdwp) << StringPrintf(" %#x", dex_pc);
35352435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
35362435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
35372435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
35382435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
3539872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3540872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
354161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertzvoid Dbg::UnconfigureStep(JDWP::ObjectId thread_id) {
354261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
354361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
354461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* thread;
354561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
354687118ed6f3f99e7df33214c277cf200a7b9a7499Sebastien Hertz  if (error == JDWP::ERR_NONE) {
354761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* single_step_control = thread->GetSingleStepControl();
354861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    DCHECK(single_step_control != nullptr);
3549bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    single_step_control->Clear();
355061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  }
3551872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3552872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
355345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughesstatic char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
355445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  switch (tag) {
355545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    default:
355645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
355745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
355845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Primitives.
355945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BYTE:    return 'B';
356045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CHAR:    return 'C';
356145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_FLOAT:   return 'F';
356245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_DOUBLE:  return 'D';
356345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_INT:     return 'I';
356445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_LONG:    return 'J';
356545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_SHORT:   return 'S';
356645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_VOID:    return 'V';
356745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BOOLEAN: return 'Z';
356845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
356945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Reference types.
357045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_ARRAY:
357145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_OBJECT:
357245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_STRING:
357345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD:
357445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD_GROUP:
357545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_LOADER:
357645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_OBJECT:
357745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return 'L';
357845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  }
357945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes}
358045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
358188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id,
358288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                  JDWP::RefTypeId class_id, JDWP::MethodId method_id,
358300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  uint32_t arg_count, uint64_t* arg_values,
358400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* arg_types, uint32_t options,
358500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* pResultTag, uint64_t* pResultValue,
358600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::ObjectId* pExceptionId) {
3587d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  ThreadList* thread_list = Runtime::Current()->GetThreadList();
3588d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3589d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  Thread* targetThread = NULL;
3590d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  DebugInvokeReq* req = NULL;
359100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* self = Thread::Current();
3592d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
359300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(self);
359450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
3595221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread);
3596221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
3597221229cb523f849f165fdafbf9785010963715daElliott Hughes      LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
3598221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
3599d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3600d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    req = targetThread->GetInvokeReq();
3601d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (!req->ready) {
3602d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
3603d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      return JDWP::ERR_INVALID_THREAD;
3604d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3605d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3606d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3607d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We currently have a bug where we don't successfully resume the
3608d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * target thread if the suspend count is too deep.  We're expected to
3609d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * require one "resume" for each "suspend", but when asked to execute
3610d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a method we have to resume fully and then re-suspend it back to the
3611d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * same level.  (The easiest way to cause this is to type "suspend"
3612d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * multiple times in jdb.)
3613d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3614d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * It's unclear what this means when the event specifies "resume all"
3615d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * and some threads are suspended more deeply than others.  This is
3616d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a rare problem, so for now we just prevent it from hanging forever
3617d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * by rejecting the method invocation request.  Without this, we will
3618d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * be stuck waiting on a suspended thread.
3619d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
362000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    int suspend_count;
362100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
362250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
362300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      suspend_count = targetThread->GetSuspendCount();
362400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
3625d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (suspend_count > 1) {
3626d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count;
36277934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return JDWP::ERR_THREAD_SUSPENDED;  // Probably not expected here.
3628d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3629d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
36303f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    JDWP::JdwpError status;
36312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id);
363264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (receiver == ObjectRegistry::kInvalidObject) {
36333f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
36343f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
363545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
36362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id);
363764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (thread == ObjectRegistry::kInvalidObject) {
36383f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
36393f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
364045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // TODO: check that 'thread' is actually a java.lang.Thread!
364145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
36422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_id, status);
364345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (c == NULL) {
36443f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return status;
36453f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
364645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3647ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = FromMethodId(method_id);
364845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic() != (receiver == NULL)) {
364945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_INVALID_METHODID;
365045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
365145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic()) {
365245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (m->GetDeclaringClass() != c) {
365345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
365445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
365545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    } else {
365645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
365745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
365845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
365945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
366045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
366145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Check the argument list matches the method.
3662bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    uint32_t shorty_len = 0;
3663bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    const char* shorty = m->GetShorty(&shorty_len);
3664bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    if (shorty_len - 1 != arg_count) {
366545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_ILLEGAL_ARGUMENT;
366645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
36670920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
3668bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    {
3669bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      StackHandleScope<3> hs(soa.Self());
3670bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      MethodHelper mh(hs.NewHandle(m));
3671bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver));
3672bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      HandleWrapper<mirror::Class> h_klass(hs.NewHandleWrapper(&c));
3673bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      const DexFile::TypeList* types = m->GetParameterTypeList();
3674bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      for (size_t i = 0; i < arg_count; ++i) {
3675bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
36760920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_ILLEGAL_ARGUMENT;
36770920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
36780920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
3679bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        if (shorty[i + 1] == 'L') {
3680bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          // Did we really get an argument of an appropriate reference type?
3681bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          mirror::Class* parameter_type = mh.GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
3682bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i]);
3683bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          if (argument == ObjectRegistry::kInvalidObject) {
3684bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier            return JDWP::ERR_INVALID_OBJECT;
3685bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          }
3686bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          if (argument != NULL && !argument->InstanceOf(parameter_type)) {
3687bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier            return JDWP::ERR_ILLEGAL_ARGUMENT;
3688bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          }
3689bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier
3690bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          // Turn the on-the-wire ObjectId into a jobject.
3691bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
3692bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          v.l = gRegistry->GetJObject(arg_values[i]);
3693bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        }
36940920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      }
3695bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      // Update in case it moved.
3696bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      m = mh.GetMethod();
369745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
369845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3699d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->receiver = receiver;
3700d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->thread = thread;
3701d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->klass = c;
3702d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->method = m;
3703d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_count = arg_count;
3704d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_values = arg_values;
3705d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->options = options;
3706d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->invoke_needed = true;
3707d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3708d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3709d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // The fact that we've released the thread list lock is a bit risky --- if the thread goes
3710d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // away we're sitting high and dry -- but we must release this before the ResumeAllThreads
3711d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // call, and it's unwise to hold it during WaitForSuspend.
3712d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3713d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
3714d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3715d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We change our (JDWP thread) status, which should be THREAD_RUNNING,
371681ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes     * so we can suspend for a GC if the invoke request causes us to
3717d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * run out of memory.  It's also a good idea to change it before locking
3718d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the invokeReq mutex, although that should never be held for long.
3719d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
372000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
3721d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
37224dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Transferring control to event thread";
3723d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    {
3724d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      MutexLock mu(self, req->lock);
3725d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3726d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
37274dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming all threads";
372800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers        thread_list->UndoDebuggerSuspensions();
3729d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      } else {
37304dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming event thread only";
3731d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes        thread_list->Resume(targetThread, true);
3732d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3733d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3734d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      // Wait for the request to finish executing.
3735d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      while (req->invoke_needed) {
3736d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz        req->cond.Wait(self);
3737d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3738d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
37394dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Control has returned from event thread";
3740d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3741d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* wait for thread to re-suspend itself */
3742df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom    SuspendThread(thread_id, false /* request_suspension */);
374300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
3744d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3745d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3746d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  /*
3747d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * Suspend the threads.  We waited for the target thread to suspend
3748d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * itself, so all we need to do is suspend the others.
3749d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   *
3750d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * The suspendAllThreads() call will double-suspend the event thread,
3751d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * so we want to resume the target thread once to keep the books straight.
3752d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   */
3753d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
375400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
37554dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Suspending all threads";
375600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    thread_list->SuspendAllForDebugger();
375700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
37584dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Resuming event thread to balance the count";
3759d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    thread_list->Resume(targetThread, true);
3760d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3761d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3762d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Copy the result.
3763d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pResultTag = req->result_tag;
3764d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (IsPrimitiveTag(req->result_tag)) {
3765f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = req->result_value.GetJ();
3766d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else {
3767f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = gRegistry->Add(req->result_value.GetL());
3768d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3769d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pExceptionId = req->exception;
3770d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  return req->error;
3771872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3772872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3773872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
377400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
3775d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
377681ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // We can be called while an exception is pending. We need
3777d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // to preserve that across the method invocation.
3778eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<4> hs(soa.Self());
3779eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_throw_this_object = hs.NewHandle<mirror::Object>(nullptr);
3780eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_throw_method = hs.NewHandle<mirror::ArtMethod>(nullptr);
3781eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_exception = hs.NewHandle<mirror::Throwable>(nullptr);
378262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  uint32_t old_throw_dex_pc;
37839f1020305292a21fd14a402b189c765a125226abSebastien Hertz  bool old_exception_report_flag;
378462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  {
378562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation old_throw_location;
378662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
3787eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_throw_this_object.Assign(old_throw_location.GetThis());
3788eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_throw_method.Assign(old_throw_location.GetMethod());
3789eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_exception.Assign(old_exception_obj);
379062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_dex_pc = old_throw_location.GetDexPc();
37919f1020305292a21fd14a402b189c765a125226abSebastien Hertz    old_exception_report_flag = soa.Self()->IsExceptionReportedToInstrumentation();
379262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ClearException();
379362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
3794d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3795d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Translate the method through the vtable, unless the debugger wants to suppress it.
3796eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ArtMethod> m(hs.NewHandle(pReq->method));
3797d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz  if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver != NULL) {
3798eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    mirror::ArtMethod* actual_method = pReq->klass->FindVirtualMethodForVirtualOrInterface(m.Get());
3799eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    if (actual_method != m.Get()) {
3800eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m.Get()) << " to " << PrettyMethod(actual_method);
3801eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      m.Assign(actual_method);
380245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
3803d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3804eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m.Get())
3805d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " receiver=" << pReq->receiver
3806d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " arg_count=" << pReq->arg_count;
3807eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  CHECK(m.Get() != nullptr);
3808d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3809d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  CHECK_EQ(sizeof(jvalue), sizeof(uint64_t));
3810d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3811eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  pReq->result_value = InvokeWithJValues(soa, pReq->receiver, soa.EncodeMethod(m.Get()),
381253b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers                                         reinterpret_cast<jvalue*>(pReq->arg_values));
3813d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
381462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  mirror::Throwable* exception = soa.Self()->GetException(NULL);
381562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ClearException();
381662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  pReq->exception = gRegistry->Add(exception);
3817bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  pReq->result_tag = BasicTagFromDescriptor(m.Get()->GetShorty());
3818d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (pReq->exception != 0) {
381962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    VLOG(jdwp) << "  JDWP invocation returning with exception=" << exception
382062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        << " " << exception->Dump();
3821f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    pReq->result_value.SetJ(0);
3822d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else if (pReq->result_tag == JDWP::JT_OBJECT) {
3823d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* if no exception thrown, examine object result more closely */
38249837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    JDWP::JdwpTag new_tag = TagFromObject(soa, pReq->result_value.GetL());
3825d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (new_tag != pReq->result_tag) {
38264dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes      VLOG(jdwp) << "  JDWP promoted result from " << pReq->result_tag << " to " << new_tag;
3827d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      pReq->result_tag = new_tag;
3828d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3829d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3830d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3831d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * Register the object.  We don't actually need an ObjectId yet,
3832d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * but we do need to be sure that the GC won't move or discard the
3833d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * object when we switch out of RUNNING.  The ObjectId conversion
3834d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * will add the object to the "do not touch" list.
3835d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3836d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We can't use the "tracked allocation" mechanism here because
3837d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the object is going to be handed off to a different thread.
3838d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
3839f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    gRegistry->Add(pReq->result_value.GetL());
3840d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3841d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3842eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  if (old_exception.Get() != NULL) {
3843eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
384462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                         old_throw_dex_pc);
3845eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
38469f1020305292a21fd14a402b189c765a125226abSebastien Hertz    soa.Self()->SetExceptionReportedToInstrumentation(old_exception_report_flag);
3847d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3848872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3849872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3850d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes/*
38514b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * "request" contains a full JDWP packet, possibly with multiple chunks.  We
3852f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * need to process each, accumulate the replies, and ship the whole thing
3853f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * back.
3854f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
3855f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * Returns "true" if we have a reply.  The reply buffer is newly allocated,
3856f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * and includes the chunk type/length, followed by the data.
3857f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
38583d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes * OLD-TODO: we currently assume that the request and reply include a single
3859f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * chunk.  If this becomes inconvenient we will need to adapt.
3860f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes */
38614b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesbool Dbg::DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen) {
3862f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  Thread* self = Thread::Current();
3863f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  JNIEnv* env = self->GetJniEnv();
3864f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
38654b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t type = request.ReadUnsigned32("type");
38664b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t length = request.ReadUnsigned32("length");
38674b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
38684b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  // Create a byte[] corresponding to 'request'.
38694b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  size_t request_length = request.size();
38704b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length));
38716a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (dataArray.get() == NULL) {
38724b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    LOG(WARNING) << "byte[] allocation failed: " << request_length;
3873f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3874f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3875f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
38764b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  env->SetByteArrayRegion(dataArray.get(), 0, request_length, reinterpret_cast<const jbyte*>(request.data()));
38774b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  request.Skip(request_length);
3878f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3879f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Run through and find all chunks.  [Currently just find the first.]
38806a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ScopedByteArrayRO contents(env, dataArray.get());
38814b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (length != request_length) {
3882ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%zd)", length, request_length);
3883f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3884f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3885f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3886f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
3887eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3888eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                                                                 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
38894b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                                                 type, dataArray.get(), 0, length));
3890f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (env->ExceptionCheck()) {
3891f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
3892f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionDescribe();
3893f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3894f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3895f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3896f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
38976a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (chunk.get() == NULL) {
3898f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3899f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3900f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3901f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  /*
3902f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * Pull the pieces out of the chunk.  We copy the results into a
3903f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * newly-allocated buffer that the caller can free.  We don't want to
3904f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * continue using the Chunk object because nothing has a reference to it.
3905f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3906f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * We could avoid this by returning type/data/offset/length and having
3907f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * the caller be aware of the object lifetime issues, but that
390881ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes   * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
3909f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * if we have responses for multiple chunks.
3910f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3911f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * So we're pretty much stuck with copying data around multiple times.
3912f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   */
3913eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
39144b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
3915eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
3916eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
3917f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
39184dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length);
39196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (length == 0 || replyData.get() == NULL) {
3920f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3921f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3922f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
39234b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  const int kChunkHdrLen = 8;
3924f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  uint8_t* reply = new uint8_t[length + kChunkHdrLen];
3925f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (reply == NULL) {
3926f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
3927f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3928f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3929f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 0, type);
3930f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 4, length);
39316a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
3932f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3933f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyBuf = reply;
3934f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyLen = length + kChunkHdrLen;
3935f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
39364b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length);
3937f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  return true;
3938872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3939872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3940a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::DdmBroadcast(bool connect) {
39414dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
394247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
394347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  Thread* self = Thread::Current();
394450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  if (self->GetState() != kRunnable) {
394550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
394650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    /* try anyway? */
394747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
394847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
394947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  JNIEnv* env = self->GetJniEnv();
395047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
3951eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3952eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
3953eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            event);
395447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (env->ExceptionCheck()) {
395547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
395647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionDescribe();
395747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionClear();
395847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
395947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
396047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3961872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmConnected() {
3962a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(true);
3963872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3964872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3965872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmDisconnected() {
3966a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(false);
396747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = false;
396847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
396947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
397047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes/*
39718218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes * Send a notification when a thread starts, stops, or changes its name.
397247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes *
397347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * Because we broadcast the full set of threads when the notifications are
397447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * first enabled, it's possible for "thread" to be actively executing.
397547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes */
39768218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
397747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (!gDdmThreadNotification) {
397847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    return;
397947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
398047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
39818218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  if (type == CHUNK_TYPE("THDE")) {
39828218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    uint8_t buf[4];
3983d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Set4BE(&buf[0], t->GetThreadId());
39848218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
39858218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  } else {
39868218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
398700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
3988eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    StackHandleScope<1> hs(soa.Self());
3989eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::String> name(hs.NewHandle(t->GetThreadName(soa)));
3990eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    size_t char_count = (name.Get() != NULL) ? name->GetLength() : 0;
3991eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    const jchar* chars = (name.Get() != NULL) ? name->GetCharArray()->GetData() : NULL;
399247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
399321f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    std::vector<uint8_t> bytes;
3994d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Append4BE(bytes, t->GetThreadId());
3995545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    JDWP::AppendUtf16BE(bytes, chars, char_count);
399621f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
399721f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    Dbg::DdmSendChunk(type, bytes);
399847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
399947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
400047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
400147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::DdmSetThreadNotification(bool enable) {
400200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Enable/disable thread notifications.
400347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = enable;
400447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (enable) {
400500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Suspend the VM then post thread start notifications for all threads. Threads attaching will
400600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // see a suspension in progress and block until that ends. They then post their own start
400700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // notification.
400800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    SuspendVM();
400900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    std::list<Thread*> threads;
401050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
401100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
401250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, *Locks::thread_list_lock_);
401300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      threads = Runtime::Current()->GetThreadList()->GetList();
401400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
401500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
401650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      ScopedObjectAccess soa(self);
401702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (Thread* thread : threads) {
401802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR"));
401900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
402000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
402100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ResumeVM();
402247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
402347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
402447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
4025a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
4026c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (IsDebuggerActive()) {
4027d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    gJdwpState->PostThreadChange(t, type == CHUNK_TYPE("THCR"));
402847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
40298218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  Dbg::DdmSendThreadNotification(t, type);
403047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
403147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
403247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadStart(Thread* t) {
4033a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
403447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
403547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
403647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadDeath(Thread* t) {
4037a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
4038872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
4039872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
40408218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) {
40413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(buf != NULL);
40423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  iovec vec[1];
40433bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
40443bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_len = byte_count;
40453bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Dbg::DdmSendChunkV(type, vec, 1);
4046872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
4047872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
404821f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) {
404921f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  DdmSendChunk(type, bytes.size(), &bytes[0]);
405021f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes}
405121f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes
4052f52935278fca8c7aa220543eef4544e3d1105d91Brian Carlstromvoid Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
40533bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (gJdwpState == NULL) {
40544dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
40553bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
4056cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    gJdwpState->DdmSendChunkV(type, iov, iov_count);
40573bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
4058872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
4059872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
4060767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesint Dbg::DdmHandleHpifChunk(HpifWhen when) {
4061767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when == HPIF_WHEN_NOW) {
40627162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(when);
4063767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return true;
4064767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
4065767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4066767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
4067767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
4068767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
4069767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
4070767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4071767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  gDdmHpifWhen = when;
4072767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
4073767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
4074767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4075767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesbool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
4076767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
4077767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
4078767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
4079767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
4080767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4081767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
4082767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
4083767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
4084767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
4085767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4086767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (native) {
4087767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhen = when;
4088767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhat = what;
4089767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  } else {
4090767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhen = when;
4091767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhat = what;
4092767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
4093767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
4094767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
4095767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
40967162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesvoid Dbg::DdmSendHeapInfo(HpifWhen reason) {
40977162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  // If there's a one-shot 'when', reset it.
40987162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  if (reason == gDdmHpifWhen) {
40997162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
41007162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes      gDdmHpifWhen = HPIF_WHEN_NEVER;
41017162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    }
41027162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  }
41037162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
41047162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  /*
41057162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Chunk HPIF (client --> server)
41067162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
41077162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Heap Info. General information about the heap,
41087162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * suitable for a summary display.
41097162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
41107162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   [u4]: number of heaps
41117162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
41127162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   For each heap:
41137162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: heap ID
41147162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u8]: timestamp in ms since Unix epoch
41157162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u1]: capture reason (same as 'when' value from server)
41167162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: max heap size in bytes (-Xmx)
41177162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current heap size in bytes
41187162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of bytes allocated
41197162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of objects allocated
41207162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   */
41217162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  uint8_t heap_count = 1;
41221d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::Heap* heap = Runtime::Current()->GetHeap();
412321f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  std::vector<uint8_t> bytes;
4124545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append4BE(bytes, heap_count);
41257934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, 1);  // Heap id (bogus; we only have one heap).
4126545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append8BE(bytes, MilliTime());
4127545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append1BE(bytes, reason);
41287934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetMaxMemory());  // Max allowed heap size in bytes.
41297934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetTotalMemory());  // Current heap size in bytes.
4130b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetBytesAllocated());
4131b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
413221f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
413321f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes);
4134767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
4135767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
41366a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgSolidity {
41376a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FREE = 0,
41386a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_HARD = 1,
41396a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SOFT = 2,
41406a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_WEAK = 3,
41416a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_PHANTOM = 4,
41426a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FINALIZABLE = 5,
41436a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SWEEP = 6,
41446a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
41456a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgKind {
41476a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_OBJECT = 0,
41486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_CLASS_OBJECT = 1,
41496a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_1 = 2,
41506a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_2 = 3,
41516a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_4 = 4,
41526a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_8 = 5,
41536a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_UNKNOWN = 6,
41546a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_NATIVE = 7,
41556a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
41566a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41576a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_PARTIAL (1<<7)
41586a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
41596a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
416030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogersclass HeapChunkContext {
416130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers public:
41626a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Maximum chunk size.  Obtain this from the formula:
41636a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
41646a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  HeapChunkContext(bool merge, bool native)
416530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      : buf_(16384 - 16),
416630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        type_(0),
416736dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        merge_(merge),
416836dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        chunk_overhead_(0) {
41696a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
41706a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    if (native) {
417130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = CHUNK_TYPE("NHSG");
41726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    } else {
417330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
41746a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
41756a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
41766a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41776a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ~HeapChunkContext() {
417830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (p_ > &buf_[0]) {
41796a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      Flush();
41806a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
41816a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
41826a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
418336dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  void SetChunkOverhead(size_t chunk_overhead) {
418436dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    chunk_overhead_ = chunk_overhead;
418536dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  }
418636dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier
418736dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  void ResetStartOfNextChunk() {
418836dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    startOfNextMemoryChunk_ = nullptr;
418936dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  }
419036dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier
41916a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void EnsureHeader(const void* chunk_ptr) {
419230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (!needHeader_) {
41936a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      return;
41946a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
41956a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41966a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Start a new HPSx chunk.
41977934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 1);  // Heap id (bogus; we only have one heap).
41987934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write1BE(&p_, 8);  // Size of allocation unit, in bytes.
41996a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
42007934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr));  // virtual address of segment start.
42017934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 0);  // offset of this piece (relative to the virtual address).
42026a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // [u4]: length of piece, in allocation units
42036a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // We won't know this until we're done, so save the offset and stuff in a dummy value.
420430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = p_;
420530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Write4BE(&p_, 0x55555555);
420630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = false;
42076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
42086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4209b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4210d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    if (pieceLenField_ == NULL) {
4211d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
4212d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      CHECK(needHeader_);
4213d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      return;
4214d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    }
42156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Patch the "length of piece" field.
421630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(&buf_[0], pieceLenField_);
421730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(pieceLenField_, p_);
421830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
42196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
422030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]);
42216a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
42226a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
42236a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
422400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg)
4225b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
4226b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
422730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes);
4228a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
4229a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
42306a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes private:
4231a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  enum { ALLOCATION_UNIT_SIZE = 8 };
4232a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
42336a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void Reset() {
423430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    p_ = &buf_[0];
423536dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    ResetStartOfNextChunk();
423630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    totalAllocationUnits_ = 0;
423730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = true;
423830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = NULL;
42396a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
42406a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
424100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes)
4242b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
4243b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
424430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
424530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // in the following code not to allocate memory, by ensuring buf_ is of the correct size
424615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (used_bytes == 0) {
424715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start == NULL) {
424815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            // Reset for start of new heap.
424915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
425015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
425115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
425215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Only process in use memory so that free region information
425315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // also includes dlmalloc book keeping.
4254a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
4255a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
42566a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
425715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    /* If we're looking at the native heap, we'll just return
425815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks
425915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     */
426015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bool native = type_ == CHUNK_TYPE("NHSG");
426115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
426236dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    // TODO: I'm not sure using start of next chunk works well with multiple spaces. We shouldn't
426336dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    // count gaps inbetween spaces as free memory.
426415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (startOfNextMemoryChunk_ != NULL) {
426515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Transmit any pending free memory. Native free memory of
426615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // over kMaxFreeLen could be because of the use of mmaps, so
426715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // don't report. If not free memory then start a new segment.
426815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        bool flush = true;
426915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start > startOfNextMemoryChunk_) {
427015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            const size_t kMaxFreeLen = 2 * kPageSize;
427115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeStart = startOfNextMemoryChunk_;
427215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeEnd = start;
42732d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom            size_t freeLen = reinterpret_cast<char*>(freeEnd) - reinterpret_cast<char*>(freeStart);
427415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            if (!native || freeLen < kMaxFreeLen) {
427515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), freeStart, freeLen);
427615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                flush = false;
427715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            }
427815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
427915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (flush) {
428015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
428115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
428215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
428315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
4284ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    mirror::Object* obj = reinterpret_cast<mirror::Object*>(start);
4285a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
4286a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Determine the type of this chunk.
4287a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
4288a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If it's the same, we should combine them.
428915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    uint8_t state = ExamineObject(obj, native);
429036dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    AppendChunk(state, start, used_bytes + chunk_overhead_);
429136dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_;
429215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  }
429315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
429415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void AppendChunk(uint8_t state, void* ptr, size_t length)
4295b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
429615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // Make sure there's enough room left in the buffer.
429715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
429815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // 17 bytes for any header.
429915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t needed = (((length/ALLOCATION_UNIT_SIZE + 255) / 256) * 2) + 17;
430015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
430115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
430215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      Flush();
430315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
4304a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
430515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
430615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
430715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
430815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers          << needed << " bytes)";
430915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return;
431015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
431115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    EnsureHeader(ptr);
4312a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Write out the chunk description.
431315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    length /= ALLOCATION_UNIT_SIZE;   // Convert to allocation units.
431415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    totalAllocationUnits_ += length;
431515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    while (length > 256) {
431630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = state | HPSG_PARTIAL;
431730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = 255;     // length - 1
431815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      length -= 256;
4319a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
432030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    *p_++ = state;
432115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    *p_++ = length - 1;
43226a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
43236a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4324ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint8_t ExamineObject(mirror::Object* o, bool is_native_heap)
4325ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
4326a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (o == NULL) {
4327a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_FREE, 0);
4328a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
43296a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4330a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // It's an allocated chunk. Figure out what it is.
43316a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4332a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If we're looking at the native heap, we'll just return
4333a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks.
433400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (is_native_heap) {
4335a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
43366a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
43376a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
43385bfa60ffcc953340feb711ed05cf576ac821905eIan Rogers    if (!Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) {
433915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
434000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
434100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
43422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = o->GetClass();
4343a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c == NULL) {
4344a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      // The object was probably just created but hasn't been initialized yet.
4345a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
4346a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
43476a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4348590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(c)) {
434915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
4350a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
4351a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
43526a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
435365370e37b3fd9e53e3c1bce5757912e8309cd305Mathieu Chartier    if (c->GetClass() == nullptr) {
435465370e37b3fd9e53e3c1bce5757912e8309cd305Mathieu Chartier      LOG(ERROR) << "Null class of class " << c << " for object " << o;
435565370e37b3fd9e53e3c1bce5757912e8309cd305Mathieu Chartier      return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
435665370e37b3fd9e53e3c1bce5757912e8309cd305Mathieu Chartier    }
435765370e37b3fd9e53e3c1bce5757912e8309cd305Mathieu Chartier
4358a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsClassClass()) {
4359a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
43606a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
43616a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4362a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsArrayClass()) {
4363a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (o->IsObjectArray()) {
4364a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
4365a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
4366a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      switch (c->GetComponentSize()) {
4367a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
4368a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
4369a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
4370a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
4371a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
43726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
43736a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4374a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
43756a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
43766a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
437730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  std::vector<uint8_t> buf_;
437830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* p_;
437930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* pieceLenField_;
438015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void* startOfNextMemoryChunk_;
438130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  size_t totalAllocationUnits_;
438230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint32_t type_;
438330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool merge_;
438430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool needHeader_;
438536dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  size_t chunk_overhead_;
438630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers
4387a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
4388a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes};
43896a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
439036dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartierstatic void BumpPointerSpaceCallback(mirror::Object* obj, void* arg)
439136dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
439236dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment);
439336dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  HeapChunkContext::HeapChunkCallback(
439436dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, arg);
439536dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier}
439636dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier
43976a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesvoid Dbg::DdmSendHeapSegments(bool native) {
43986a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhen when;
43996a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhat what;
44006a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (!native) {
44016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmHpsgWhen;
44026a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmHpsgWhat;
44036a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  } else {
44046a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmNhsgWhen;
44056a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmNhsgWhat;
44066a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
44076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (when == HPSG_WHEN_NEVER) {
44086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    return;
44096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
44106a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
44116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Figure out what kind of chunks we'll be sending.
44126a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what);
44136a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
44146a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // First, send a heap start chunk.
44156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  uint8_t heap_id[4];
44167934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Set4BE(&heap_id[0], 1);  // Heap id (bogus; we only have one heap).
44176a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id);
44186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4419cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Thread* self = Thread::Current();
4420cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
4421cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
4422cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
44236a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Send a series of heap segment chunks.
4424a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native);
4425a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (native) {
4426c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#ifdef USE_DLMALLOC
44271d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context);
4428c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#else
4429c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris    UNIMPLEMENTED(WARNING) << "Native heap inspection is only supported with dlmalloc";
4430c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#endif
4431a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
44321d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
443336dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    for (const auto& space : heap->GetContinuousSpaces()) {
443436dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      if (space->IsDlMallocSpace()) {
4435d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
443636dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
443736dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        // allocation then the first sizeof(size_t) may belong to it.
443836dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        context.SetChunkOverhead(sizeof(size_t));
443936dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
444036dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      } else if (space->IsRosAllocSpace()) {
444136dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        context.SetChunkOverhead(0);
4442d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since
4443d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        // RosAlloc's internal logic doesn't know to release and reacquire the heap bitmap lock.
4444d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        self->TransitionFromRunnableToSuspended(kSuspended);
4445d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        ThreadList* tl = Runtime::Current()->GetThreadList();
4446d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        tl->SuspendAll();
4447d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        {
4448d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier          ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
4449d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier          space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
4450d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        }
4451d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        tl->ResumeAll();
4452d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        self->TransitionFromSuspendedToRunnable();
445336dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      } else if (space->IsBumpPointerSpace()) {
4454d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
445536dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        context.SetChunkOverhead(0);
445636dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        space->AsBumpPointerSpace()->Walk(BumpPointerSpaceCallback, &context);
445736dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      } else {
445836dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space;
4459b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier      }
446036dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      context.ResetStartOfNextChunk();
4461b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
4462d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier    ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
4463e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    // Walk the large objects, these are not in the AllocSpace.
446436dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    context.SetChunkOverhead(0);
4465e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
4466a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
44676a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
44686a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Finally, send a heap end chunk.
44696a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id);
4470767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
4471767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4472b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughesstatic size_t GetAllocTrackerMax() {
4473b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#ifdef HAVE_ANDROID_OS
4474b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  // Check whether there's a system property overriding the number of records.
4475b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  const char* propertyName = "dalvik.vm.allocTrackerMax";
4476b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  char allocRecordMaxString[PROPERTY_VALUE_MAX];
4477b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (property_get(propertyName, allocRecordMaxString, "") > 0) {
4478b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    char* end;
4479b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    size_t value = strtoul(allocRecordMaxString, &end, 10);
4480b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (*end != '\0') {
44813e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
44823e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- invalid";
4483b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4484b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4485b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (!IsPowerOfTwo(value)) {
44863e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
44873e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- not power of two";
4488b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4489b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4490b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    return value;
4491b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  }
4492b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#endif
4493b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  return kDefaultNumAllocRecords;
4494b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes}
4495b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes
4496f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstromvoid Dbg::SetAllocTrackingEnabled(bool enable) {
4497f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  Thread* self = Thread::Current();
4498f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  if (enable) {
4499b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4500f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      MutexLock mu(self, *Locks::alloc_tracker_lock_);
4501f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      if (recent_allocation_records_ != NULL) {
4502f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom        return;  // Already enabled, bail.
4503b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      }
4504f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      alloc_record_max_ = GetAllocTrackerMax();
4505f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      LOG(INFO) << "Enabling alloc tracker (" << alloc_record_max_ << " entries of "
4506f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom                << kMaxAllocRecordStackDepth << " frames, taking "
4507f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom                << PrettySize(sizeof(AllocRecord) * alloc_record_max_) << ")";
4508f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      DCHECK_EQ(alloc_record_head_, 0U);
4509f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      DCHECK_EQ(alloc_record_count_, 0U);
4510f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      recent_allocation_records_ = new AllocRecord[alloc_record_max_];
4511f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      CHECK(recent_allocation_records_ != NULL);
4512545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4513a98ffd745bbecb2e84a492194950c0b94966546bMathieu Chartier    Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints();
4514545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  } else {
4515b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4516f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      ScopedObjectAccess soa(self);  // For type_cache_.Clear();
4517f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      MutexLock mu(self, *Locks::alloc_tracker_lock_);
4518f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      if (recent_allocation_records_ == NULL) {
4519f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom        return;  // Already disabled, bail.
4520f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      }
45214345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier      LOG(INFO) << "Disabling alloc tracker";
4522b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      delete[] recent_allocation_records_;
4523b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      recent_allocation_records_ = NULL;
4524f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      alloc_record_head_ = 0;
4525f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      alloc_record_count_ = 0;
45264345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier      type_cache_.Clear();
4527b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    }
4528f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    // If an allocation comes in before we uninstrument, we will safely drop it on the floor.
4529a98ffd745bbecb2e84a492194950c0b94966546bMathieu Chartier    Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
4530545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4531545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4532545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
45330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct AllocRecordStackVisitor : public StackVisitor {
45347a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor(Thread* thread, AllocRecord* record)
4535b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
45367a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, NULL), record(record), depth(0) {}
4537545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
453800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
453900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
454000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
4541545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if (depth >= kMaxAllocRecordStackDepth) {
4542530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return false;
4543545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4544ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = GetMethod();
45450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    if (!m->IsRuntimeMethod()) {
4546b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetMethod(m);
4547b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetDexPc(GetDexPc());
4548530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      ++depth;
4549545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4550530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes    return true;
4551545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4552545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4553545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  ~AllocRecordStackVisitor() {
4554545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // Clear out any unused stack trace elements.
4555545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (; depth < kMaxAllocRecordStackDepth; ++depth) {
4556b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetMethod(nullptr);
4557b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetDexPc(0);
4558545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4559545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4560545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4561545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  AllocRecord* record;
4562545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t depth;
4563545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4564545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
45652dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::RecordAllocation(mirror::Class* type, size_t byte_count) {
4566545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  Thread* self = Thread::Current();
4567545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  CHECK(self != NULL);
4568545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4569f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  MutexLock mu(self, *Locks::alloc_tracker_lock_);
4570545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4571f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    // In the process of shutting down recording, bail.
4572545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4573545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4574545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4575545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Advance and clip.
4576719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (++alloc_record_head_ == alloc_record_max_) {
4577719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    alloc_record_head_ = 0;
4578545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4579545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4580545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the basics.
4581719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  AllocRecord* record = &recent_allocation_records_[alloc_record_head_];
4582b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  record->SetType(type);
4583b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  record->SetByteCount(byte_count);
4584b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  record->SetThinLockId(self->GetThreadId());
4585545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4586545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the stack trace.
45877a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor visitor(self, record);
45880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
4589545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4590719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (alloc_record_count_ < alloc_record_max_) {
4591719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    ++alloc_record_count_;
4592545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4593545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4594545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4595a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// Returns the index of the head element.
4596a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4597f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom// We point at the most-recently-written record, so if alloc_record_count_ is 1
4598a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// we want to use the current element.  Take "head+1" and subtract count
4599a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// from it.
4600a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4601a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We need to handle underflow in our circular buffer, so we add
4602f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom// alloc_record_max_ and then mask it back down.
4603719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::HeadIndex() {
4604719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  return (Dbg::alloc_record_head_ + 1 + Dbg::alloc_record_max_ - Dbg::alloc_record_count_) &
4605719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      (Dbg::alloc_record_max_ - 1);
4606545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4607545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4608545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::DumpRecentAllocations() {
460900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
4610f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  MutexLock mu(soa.Self(), *Locks::alloc_tracker_lock_);
4611545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4612545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    LOG(INFO) << "Not recording tracked allocations";
4613545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4614545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4615545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4616545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // "i" is the head of the list.  We want to start at the end of the
4617545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // list and move forward to the tail.
4618a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t i = HeadIndex();
4619f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  const uint16_t capped_count = CappedAllocRecordCount(Dbg::alloc_record_count_);
4620f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  uint16_t count = capped_count;
4621545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4622719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  LOG(INFO) << "Tracked allocations, (head=" << alloc_record_head_ << " count=" << count << ")";
4623545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  while (count--) {
4624545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    AllocRecord* record = &recent_allocation_records_[i];
4625545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4626b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->ThinLockId(), record->ByteCount())
4627b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi              << PrettyClass(record->Type());
4628545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4629545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
4630b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      AllocRecordStackTraceElement* stack_element = record->StackElement(stack_frame);
4631b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      mirror::ArtMethod* m = stack_element->Method();
4632545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      if (m == NULL) {
4633545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes        break;
4634545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
4635b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      LOG(INFO) << "    " << PrettyMethod(m) << " line " << stack_element->LineNumber();
4636545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4637545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4638545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // pause periodically to help logcat catch up
4639545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if ((count % 5) == 0) {
4640545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      usleep(40000);
4641545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4642545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4643719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    i = (i + 1) & (alloc_record_max_ - 1);
4644545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4645545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4646545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4647545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesclass StringTable {
4648545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes public:
4649545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  StringTable() {
4650545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4651545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
46524345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  void Add(const std::string& str) {
46534345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    table_.insert(str);
46544345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  }
46554345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier
46564345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  void Add(const char* str) {
46574345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    table_.insert(str);
4658545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4659545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4660a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t IndexOf(const char* s) const {
466102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    auto it = table_.find(s);
4662a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    if (it == table_.end()) {
4663a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes      LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
4664a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    }
4665a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    return std::distance(table_.begin(), it);
4666545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4667545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4668a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t Size() const {
4669545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return table_.size();
4670545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4671545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4672a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  void WriteTo(std::vector<uint8_t>& bytes) const {
467302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const std::string& str : table_) {
467402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      const char* s = str.c_str();
46756d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      size_t s_len = CountModifiedUtf8Chars(s);
4676700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<uint16_t> s_utf16(new uint16_t[s_len]);
46776d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      ConvertModifiedUtf8ToUtf16(s_utf16.get(), s);
46786d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
4679545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4680545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4681545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4682545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes private:
4683a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  std::set<std::string> table_;
4684545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(StringTable);
4685545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4686545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4687bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartierstatic const char* GetMethodSourceFile(mirror::ArtMethod* method)
4688280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4689bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  DCHECK(method != nullptr);
4690bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const char* source_file = method->GetDeclaringClassSourceFile();
4691280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  return (source_file != nullptr) ? source_file : "";
4692280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz}
4693280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz
4694545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes/*
4695545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The data we send to DDMS contains everything we have recorded.
4696545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4697545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * Message header (all values big-endian):
4698545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) message header len (to allow future expansion); includes itself
4699545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) entry header len
4700545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) stack frame len
4701545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of entries
4702545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (4b) offset to string table from start of message
4703545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of class name strings
4704545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of method name strings
4705545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of source file name strings
4706545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * For each entry:
4707545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (4b) total allocation size
4708221229cb523f849f165fdafbf9785010963715daElliott Hughes *   (2b) thread id
4709545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (2b) allocated object's class name index
4710545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (1b) stack depth
4711545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   For each stack frame:
4712545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method's class name
4713545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method name
4714545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method source file
4715545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) line number, clipped to 32767; -2 if native; -1 if no source
4716545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) class name strings
4717545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) method name strings
4718545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) source file strings
4719545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4720545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * As with other DDM traffic, strings are sent as a 4-byte length
4721545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * followed by UTF-16 data.
4722545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4723545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We send up 16-bit unsigned indexes into string tables.  In theory there
4724f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in
4725545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * each table, but in practice there should be far fewer.
4726545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4727545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The chief reason for using a string table here is to keep the size of
4728545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * the DDMS message to a minimum.  This is partly to make the protocol
4729545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * efficient, but also because we have to form the whole thing up all at
4730545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * once in a memory buffer.
4731545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4732545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We use separate string tables for class names, method names, and source
4733545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * files to keep the indexes small.  There will generally be no overlap
4734545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * between the contents of these tables.
4735545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes */
4736545a064aca775ba801790fced3d713d8a87bfc61Elliott HughesjbyteArray Dbg::GetRecentAllocations() {
4737545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (false) {
4738545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    DumpRecentAllocations();
4739545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4740545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
474150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* self = Thread::Current();
474246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  std::vector<uint8_t> bytes;
474346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  {
4744f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    MutexLock mu(self, *Locks::alloc_tracker_lock_);
474546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
474646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 1: generate string tables.
474746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
474846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable class_names;
474946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable method_names;
475046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable filenames;
475146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4752f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    const uint16_t capped_count = CappedAllocRecordCount(Dbg::alloc_record_count_);
4753f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    uint16_t count = capped_count;
4754f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    size_t idx = HeadIndex();
475546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
475646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
4757cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      std::string temp;
4758cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      class_names.Add(record->Type()->GetDescriptor(&temp));
475946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
4760b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        mirror::ArtMethod* m = record->StackElement(i)->Method();
476146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        if (m != NULL) {
4762bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          class_names.Add(m->GetDeclaringClassDescriptor());
4763bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          method_names.Add(m->GetName());
4764bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          filenames.Add(GetMethodSourceFile(m));
476546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        }
476646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      }
4767545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4768719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
476946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    }
477046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4771f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    LOG(INFO) << "allocation records: " << capped_count;
477246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
477346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
477446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 2: Generate the output and store it in the buffer.
477546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
477646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
477746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) message header len (to allow future expansion); includes itself
477846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) entry header len
477946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) stack frame len
478046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kMessageHeaderLen = 15;
478146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kEntryHeaderLen = 9;
478246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kStackFrameLen = 8;
478346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kMessageHeaderLen);
478446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kEntryHeaderLen);
478546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kStackFrameLen);
478646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
478746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of entries
478846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (4b) offset to string table from start of message
478946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of class name strings
479046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of method name strings
479146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of source file name strings
4792f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    JDWP::Append2BE(bytes, capped_count);
479346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    size_t string_table_offset = bytes.size();
47947934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Append4BE(bytes, 0);  // We'll patch this later...
479546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, class_names.Size());
479646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, method_names.Size());
479746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, filenames.Size());
479846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
479946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    idx = HeadIndex();
4800cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    std::string temp;
4801f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    for (count = capped_count; count != 0; --count) {
480246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // For each entry:
480346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (4b) total allocation size
480446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) thread id
480546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) allocated object's class name index
480646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (1b) stack depth
480746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
480846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t stack_depth = record->GetDepth();
4809f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier      size_t allocated_object_class_name_index =
4810cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers          class_names.IndexOf(record->Type()->GetDescriptor(&temp));
4811b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      JDWP::Append4BE(bytes, record->ByteCount());
4812b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      JDWP::Append2BE(bytes, record->ThinLockId());
481346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, allocated_object_class_name_index);
481446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append1BE(bytes, stack_depth);
481546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
481646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
481746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // For each stack frame:
481846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method's class name
481946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method name
482046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method source file
482146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) line number, clipped to 32767; -2 if native; -1 if no source
4822b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        mirror::ArtMethod* m = record->StackElement(stack_frame)->Method();
4823bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor());
4824bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        size_t method_name_index = method_names.IndexOf(m->GetName());
4825bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m));
482646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, class_name_index);
482746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, method_name_index);
482846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, file_name_index);
4829b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        JDWP::Append2BE(bytes, record->StackElement(stack_frame)->LineNumber());
4830545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
4831719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
4832545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4833545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
483446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) class name strings
483546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) method name strings
483646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) source file strings
483746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
483846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    class_names.WriteTo(bytes);
483946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    method_names.WriteTo(bytes);
484046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    filenames.WriteTo(bytes);
4841545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
484250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  JNIEnv* env = self->GetJniEnv();
4843545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  jbyteArray result = env->NewByteArray(bytes.size());
4844545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (result != NULL) {
4845545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
4846545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4847545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  return result;
4848545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4849545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
48500ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchimirror::ArtMethod* DeoptimizationRequest::Method() const {
48510ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  ScopedObjectAccessUnchecked soa(Thread::Current());
48520ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  return soa.DecodeMethod(method_);
48530ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi}
48540ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
48550ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchivoid DeoptimizationRequest::SetMethod(mirror::ArtMethod* m) {
48560ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  ScopedObjectAccessUnchecked soa(Thread::Current());
48570ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  method_ = soa.EncodeMethod(m);
48580ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi}
48590ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
4860872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}  // namespace art
4861