debugger.cc revision f2134f684923454e00c8ca7675b431a8538131bc
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
3443b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid DebugInvokeReq::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
3453b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier                                RootType root_type) {
3463b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (receiver != nullptr) {
3473b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(&receiver, arg, tid, root_type);
3483b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3493b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (thread != nullptr) {
3503b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(&thread, arg, tid, root_type);
3513b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3523b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (klass != nullptr) {
3533b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&klass), arg, tid, root_type);
3543b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3553b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
3563b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, tid, root_type);
3573b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3583b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
3593b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
360bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid DebugInvokeReq::Clear() {
361bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  invoke_needed = false;
362bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  receiver = nullptr;
363bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  thread = nullptr;
364bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  klass = nullptr;
365bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
366bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
367bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
3683b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid SingleStepControl::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
3693b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier                                   RootType root_type) {
3703b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
3713b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, tid, root_type);
3723b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3733b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
3743b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
375bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzbool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const {
376bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  return dex_pcs.find(dex_pc) == dex_pcs.end();
377bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
378bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
379bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid SingleStepControl::Clear() {
380bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  is_active = false;
381bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
382bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  dex_pcs.clear();
383bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
384bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
385ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic bool IsBreakpoint(const mirror::ArtMethod* m, uint32_t dex_pc)
38609bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    LOCKS_EXCLUDED(Locks::breakpoint_lock_)
387b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
38859d9d668d4f4286813afe2b4e7c6db839222ce96Sebastien Hertz  ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
389138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
3900ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].Method() == m) {
3918696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
3928696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      return true;
3938696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
3948696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
3958696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return false;
3968696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
3978696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
39852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertzstatic bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread)
39952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) {
4009e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
4019e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // A thread may be suspended for GC; in this code, we really want to know whether
4029e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // there's a debugger suspension active.
4039e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
4049e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes}
4059e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
4062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status)
407b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
408d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id);
40964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
410436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
411436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
412436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
413436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsArrayInstance()) {
414436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_ARRAY;
415436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
416436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
417436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
418436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsArray();
419436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
420436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
4212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status)
422b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
423d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(id);
42464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
425436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
426436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
427436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
428436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsClass()) {
429436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_CLASS;
430436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
431436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
432436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
433436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsClass();
434436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
435436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
436221229cb523f849f165fdafbf9785010963715daElliott Hughesstatic JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread)
437a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
438b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
439b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
440d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* thread_peer = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_id);
44164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) {
442221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't even an object.
443221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_OBJECT;
444221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
445221229cb523f849f165fdafbf9785010963715daElliott Hughes
4462dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
447221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
448221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't a thread.
449221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_THREAD;
450221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
451221229cb523f849f165fdafbf9785010963715daElliott Hughes
452221229cb523f849f165fdafbf9785010963715daElliott Hughes  thread = Thread::FromManagedThread(soa, thread_peer);
453221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (thread == NULL) {
454221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This is a java.lang.Thread without a Thread*. Must be a zombie.
455221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_THREAD_NOT_ALIVE;
456436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
457221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
458436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
459436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
46024437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
46124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
46224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
46324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  return static_cast<JDWP::JdwpTag>(descriptor[0]);
46424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
46524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
466cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogersstatic JDWP::JdwpTag BasicTagFromClass(mirror::Class* klass)
467cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
468cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  std::string temp;
469cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  const char* descriptor = klass->GetDescriptor(&temp);
470cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  return BasicTagFromDescriptor(descriptor);
471cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers}
472cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers
4739837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogersstatic JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c)
474b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
47586b0010c79ef95b5333cd540b7d3af34a9f1a643Elliott Hughes  CHECK(c != NULL);
47624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsArrayClass()) {
47724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_ARRAY;
47824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
47924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsStringClass()) {
48024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_STRING;
4819837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
4829837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (c->IsClassClass()) {
48324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_CLASS_OBJECT;
48424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
4859837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
4869837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
4879837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_class->IsAssignableFrom(c)) {
4889837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD;
4899837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
4909837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
4919837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
4929837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_group_class =
4939837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
4949837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_group_class->IsAssignableFrom(c)) {
4959837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD_GROUP;
4969837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
4979837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
4989837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
4999837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* class_loader_class =
5009837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader);
5019837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (class_loader_class->IsAssignableFrom(c)) {
5029837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_CLASS_LOADER;
5039837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
5049837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
5059837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return JDWP::JT_OBJECT;
50624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
50724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
50824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes/*
50924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Objects declared to hold Object might actually hold a more specific
51024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * type.  The debugger may take a special interest in these (e.g. it
51124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * wants to display the contents of Strings), so we want to return an
51224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * appropriate tag.
51324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes *
51424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Null objects are tagged JT_OBJECT.
51524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes */
516d539167b7f11136fe570a77aff2ee4935842007aSebastien HertzJDWP::JdwpTag Dbg::TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o) {
5179837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass());
51824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
51924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
52024437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic bool IsPrimitiveTag(JDWP::JdwpTag tag) {
52124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  switch (tag) {
52224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BOOLEAN:
52324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BYTE:
52424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_CHAR:
52524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_FLOAT:
52624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_DOUBLE:
52724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_INT:
52824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_LONG:
52924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_SHORT:
53024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_VOID:
53124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return true;
53224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  default:
53324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return false;
53424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
53524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
53624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
5373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
5383bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Handle one of the JDWP name/value pairs.
5393bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
5403bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * JDWP options are:
5413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  help: if specified, show help message and bail
5423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  transport: may be dt_socket or dt_shmem
5433bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  address: for dt_socket, "host:port", or just "port" when listening
5443bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  server: if "y", wait for debugger to attach; if "n", attach to debugger
5453bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  timeout: how long to wait for debugger to connect / listen
5463bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
5473bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Useful with server=n (these aren't supported yet):
5483bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onthrow=<exception-name>: connect to debugger when exception thrown
5493bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onuncaught=y|n: connect to debugger when uncaught exception thrown
5503bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  launch=<command-line>: launch the debugger itself
5513bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
5523bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * The "transport" option is required, as is "address" if server=n.
5533bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
5543bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool ParseJdwpOption(const std::string& name, const std::string& value) {
5553bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (name == "transport") {
5563bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "dt_socket") {
557376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
5583bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "dt_android_adb") {
559376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
5603bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5613bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP transport not supported: " << value;
5623bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5633bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5643bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "server") {
5653bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
566376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = false;
5673bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
568376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = true;
5693bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5703bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
5713bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5723bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5733bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "suspend") {
5743bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
575376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = false;
5763bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
577376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = true;
5783bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5793bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
5803bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5813bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5823bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "address") {
5833bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* this is either <port> or <host>:<port> */
5843bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string port_string;
585376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.host.clear();
5863bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type colon = value.find(':');
5873bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (colon != std::string::npos) {
588376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.host = value.substr(0, colon);
5893bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value.substr(colon + 1);
5903bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5913bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value;
5923bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5933bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (port_string.empty()) {
5943bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address missing port: " << value;
5953bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5963bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5973bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    char* end;
598ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    uint64_t port = strtoul(port_string.c_str(), &end, 10);
599ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    if (*end != '\0' || port > 0xffff) {
6003bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address has junk in port field: " << value;
6013bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
6023bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
603376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.port = port;
6043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
6053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* valid but unsupported */
6063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
6073bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
6083bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
6093bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
6103bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
6113bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
6123bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
6133bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
6143bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
6153bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
6163bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * "transport=dt_socket,address=8000,server=y,suspend=n"
6173bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
6183bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesbool Dbg::ParseJdwpOptions(const std::string& options) {
6194dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "ParseJdwpOptions: " << options;
62047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
6213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  std::vector<std::string> pairs;
6223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Split(options, ',', pairs);
6233bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
6243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  for (size_t i = 0; i < pairs.size(); ++i) {
6253bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type equals = pairs[i].find('=');
6263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (equals == std::string::npos) {
6273bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
6283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
6293bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
6303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
6313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
6323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
633376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
6343bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP transport: " << options;
6353bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
636376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
6373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
6383bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    return false;
6393bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
6403bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
6413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gJdwpConfigured = true;
6423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
6433bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
6443bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
645d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StartJdwp() {
646c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!gJdwpAllowed || !IsJdwpConfigured()) {
647376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    // No JDWP for you!
648376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    return;
649376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  }
650376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes
651719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  CHECK(gRegistry == nullptr);
652475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gRegistry = new ObjectRegistry;
653475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
654d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // Init JDWP if the debugger is enabled. This may connect out to a
655d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger, passively listen for a debugger, or block waiting for a
656d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger.
657376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
658376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState == NULL) {
659f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // We probably failed because some other process has the port already, which means that
660f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // if we don't abort the user is likely to think they're talking to us when they're actually
661f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // talking to that other process.
6623d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Debugger thread failed to initialize";
663d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
664d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes
665d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // If a debugger has already attached, send the "welcome" message.
666d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // This may cause us to suspend all threads.
667376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState->IsActive()) {
66800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
669376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    if (!gJdwpState->PostVMStart()) {
6703d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes      LOG(WARNING) << "Failed to post 'start' message to debugger";
671d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes    }
672d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
673872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
674872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
675d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StopJdwp() {
676d8b3537b89fa68599534a65afc3b272639cd4a75Sebastien Hertz  // Post VM_DEATH event before the JDWP connection is closed (either by the JDWP thread or the
677d8b3537b89fa68599534a65afc3b272639cd4a75Sebastien Hertz  // destruction of gJdwpState).
678d8b3537b89fa68599534a65afc3b272639cd4a75Sebastien Hertz  if (gJdwpState != nullptr && gJdwpState->IsActive()) {
679d8b3537b89fa68599534a65afc3b272639cd4a75Sebastien Hertz    gJdwpState->PostVMDeath();
680d8b3537b89fa68599534a65afc3b272639cd4a75Sebastien Hertz  }
6810376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  // Prevent the JDWP thread from processing JDWP incoming packets after we close the connection.
6820376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  Disposed();
683376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  delete gJdwpState;
684719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gJdwpState = nullptr;
685475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  delete gRegistry;
686719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gRegistry = nullptr;
687872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
688872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
689767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesvoid Dbg::GcDidFinish() {
690767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
69100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6924d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Sending heap info to DDM";
6937162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(gDdmHpifWhen);
694767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
695767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
69600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6974d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Dumping heap to DDM";
6986a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(false);
699767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
700767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
70100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
7024d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Dumping native heap to DDM";
7036a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(true);
704767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
705767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
706767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
7074ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesvoid Dbg::SetJdwpAllowed(bool allowed) {
7084ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes  gJdwpAllowed = allowed;
7094ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes}
7104ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
711872d4ec7225444d9400d30f9027247deb91012fdElliott HughesDebugInvokeReq* Dbg::GetInvokeReq() {
712475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return Thread::Current()->GetInvokeReq();
713475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
714475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
715475fc23a4a7f35d1be87ea0b06c80df317a720acElliott HughesThread* Dbg::GetDebugThread() {
716475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
717475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
718475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
719475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::ClearWaitForEventThread() {
720475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gJdwpState->ClearWaitForEventThread();
721872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
722872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
723872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Connected() {
7243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(!gDebuggerConnected);
7254dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "JDWP has attached";
7263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gDebuggerConnected = true;
7278696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = false;
7288696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
7298696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
7308696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::Disposed() {
7318696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = true;
7328696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
7338696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
7348696433d1b3d8ba15288483b777edd888de69135Elliott Hughesbool Dbg::IsDisposed() {
7358696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return gDisposed;
736872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
737872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
738f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertzbool Dbg::RequiresDeoptimization() {
739f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  // We don't need deoptimization if everything runs with interpreter after
740f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  // enabling -Xint mode.
741f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  return !Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly();
742f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz}
743f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz
744a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::GoActive() {
745a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Enable all debugging features, including scans for breakpoints.
746a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // This is a no-op if we're already active.
747a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Only called from the JDWP handler thread.
748a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (gDebuggerActive) {
749a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return;
750a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
751a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
752c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  {
753c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
75459d9d668d4f4286813afe2b4e7c6db839222ce96Sebastien Hertz    ReaderMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
755c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    CHECK_EQ(gBreakpoints.size(), 0U);
756c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  }
757a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
758138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
759f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
7604d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(deoptimization_requests_.size(), 0U);
7614d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(full_deoptimization_event_count_, 0U);
7627ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    CHECK_EQ(delayed_full_undeoptimization_count_, 0U);
76342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(dex_pc_change_event_ref_count_, 0U);
76442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(method_enter_event_ref_count_, 0U);
76542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(method_exit_event_ref_count_, 0U);
76642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(field_read_event_ref_count_, 0U);
76742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(field_write_event_ref_count_, 0U);
76842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(exception_catch_event_ref_count_, 0U);
769138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
770138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
77162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
77262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
77362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
77462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
77562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_NE(old_state, kRunnable);
776f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  if (RequiresDeoptimization()) {
777f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    runtime->GetInstrumentation()->EnableDeoptimization();
778f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  }
77942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  instrumentation_events_ = 0;
780a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  gDebuggerActive = true;
78162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
78262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
78362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
78462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  LOG(INFO) << "Debugger is active";
785872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
786872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
787872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Disconnected() {
788234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  CHECK(gDebuggerConnected);
789234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
790c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  LOG(INFO) << "Debugger is no longer active";
791234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
79262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread
79362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener
79462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // and clear the object registry.
79562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
79662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
79762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
79862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
799aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz
800aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  // Debugger may not be active at this point.
801aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  if (gDebuggerActive) {
802aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    {
803aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // Since we're going to disable deoptimization, we clear the deoptimization requests queue.
804aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // This prevents us from having any pending deoptimization request when the debugger attaches
805aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // to us again while no event has been requested yet.
806f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
8074d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.clear();
8084d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      full_deoptimization_event_count_ = 0U;
8097ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      delayed_full_undeoptimization_count_ = 0U;
810aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    }
81142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    if (instrumentation_events_ != 0) {
81242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener,
81342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz                                                    instrumentation_events_);
81442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      instrumentation_events_ = 0;
81542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
816f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    if (RequiresDeoptimization()) {
817f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      runtime->GetInstrumentation()->DisableDeoptimization();
818f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    }
819aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    gDebuggerActive = false;
820aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  }
821234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gRegistry->Clear();
822234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gDebuggerConnected = false;
82362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
82462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
825872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
826872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
827c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsDebuggerActive() {
8283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gDebuggerActive;
829872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
830872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
831c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsJdwpConfigured() {
8323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gJdwpConfigured;
833872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
834872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
835872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesint64_t Dbg::LastDebuggerActivity() {
836ca9515205010099d006ac2fac244348a1e673dcbElliott Hughes  return gJdwpState->LastDebuggerActivity();
837872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
838872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
839872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::UndoDebuggerSuspensions() {
840234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
841872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
842872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
84388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
8442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id);
8453d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (o == NULL) {
846436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return "NULL";
8477b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
84864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
84988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
8503d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
8513d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (!o->IsClass()) {
8527934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return StringPrintf("non-class %p", o);  // This is only used for debugging output anyway.
8533d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
854d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return GetClassName(o->AsClass());
855d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
856d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
857d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzstd::string Dbg::GetClassName(mirror::Class* klass) {
858bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  if (klass == nullptr) {
859bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz    return "NULL";
860bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  }
861cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  std::string temp;
862d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return DescriptorToName(klass->GetDescriptor(&temp));
8633d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes}
8643d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
86588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) {
866436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
8672dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
868436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
869436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
8702435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
87188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  class_object_id = gRegistry->Add(c);
872436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
8738696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
8748696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
87588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) {
8763d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
8772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
8783d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c == NULL) {
8793d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
8803d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
8813d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c->IsInterface()) {
8823d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    // http://code.google.com/p/android/issues/detail?id=20856
88388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = 0;
8843d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else {
88588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = gRegistry->Add(c->GetSuperClass());
8863d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
8873d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
888872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
889872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
890436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
8912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
89264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
893436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
894436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
895436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader()));
896436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
897872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
898872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
899436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
900436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
9012dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
902436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
903436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
9047b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
905436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
906436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
907436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
908de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set,
909de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // not interfaces.
910436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
911de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  if ((access_flags & kAccInterface) == 0) {
912de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban    access_flags |= kAccSuper;
913de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  }
914436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
915436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAdd4BE(pReply, access_flags);
916436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
917436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
918872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
919872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
920f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott HughesJDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply)
921f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
9222dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
92364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
924f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
925f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
926f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
927f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  // Ensure all threads are suspended while we read objects' lock words.
928f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Thread* self = Thread::Current();
9295426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
9305426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromRunnableToSuspended(kSuspended);
9315426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->SuspendAll();
932f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
933f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  MonitorInfo monitor_info(o);
934f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
9355426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->ResumeAll();
9365426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromSuspendedToRunnable();
937f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
938d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (monitor_info.owner_ != NULL) {
939d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer()));
940f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else {
941f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    expandBufAddObjectId(reply, gRegistry->Add(NULL));
942f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
943d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.entry_count_);
944d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.waiters_.size());
945d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) {
946d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer()));
947f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
948f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  return JDWP::ERR_NONE;
949f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes}
950f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
951734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott HughesJDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
952734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes                                      std::vector<JDWP::ObjectId>& monitors,
95352d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                      std::vector<uint32_t>& stack_depths) {
9544993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  struct OwnedMonitorVisitor : public StackVisitor {
955b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    OwnedMonitorVisitor(Thread* thread, Context* context,
956cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi                        std::vector<JDWP::ObjectId>* monitor_vector,
957b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi                        std::vector<uint32_t>* stack_depth_vector)
9584993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
959b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      : StackVisitor(thread, context), current_stack_depth(0),
960b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        monitors(monitor_vector), stack_depths(stack_depth_vector) {}
9614993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
9624993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
9634993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // annotalysis.
9644993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
9654993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      if (!GetMethod()->IsRuntimeMethod()) {
9664993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        Monitor::VisitLocks(this, AppendOwnedMonitors, this);
967734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes        ++current_stack_depth;
9684993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      }
9694993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      return true;
9704993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
9714993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
972cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi    static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg)
973cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
9747a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
975cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi      visitor->monitors->push_back(gRegistry->Add(owned_monitor));
976b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      visitor->stack_depths->push_back(visitor->current_stack_depth);
9774993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
9784993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
979734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    size_t current_stack_depth;
980cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi    std::vector<JDWP::ObjectId>* monitors;
981b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    std::vector<uint32_t>* stack_depths;
9824993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  };
9834993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
984b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  ScopedObjectAccessUnchecked soa(Thread::Current());
985cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  Thread* thread;
986b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  {
987b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
988b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
989b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (error != JDWP::ERR_NONE) {
990b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return error;
991b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
992b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (!IsSuspendedForDebugger(soa, thread)) {
993b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return JDWP::ERR_THREAD_NOT_SUSPENDED;
994b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
9954993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
996cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  std::unique_ptr<Context> context(Context::Create());
997cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  OwnedMonitorVisitor visitor(thread, context.get(), &monitors, &stack_depths);
998cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  visitor.WalkStack();
9994993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  return JDWP::ERR_NONE;
10004993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes}
10014993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
100252d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id,
100352d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                         JDWP::ObjectId& contended_monitor) {
1004b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  mirror::Object* contended_monitor_obj;
1005f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
1006b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  {
1007b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1008b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    Thread* thread;
1009b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1010b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (error != JDWP::ERR_NONE) {
1011b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return error;
1012b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
1013b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (!IsSuspendedForDebugger(soa, thread)) {
1014b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return JDWP::ERR_THREAD_NOT_SUSPENDED;
1015b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
1016b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    contended_monitor_obj = Monitor::GetContendedMonitor(thread);
1017f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
1018b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  // Add() requires the thread_list_lock_ not held to avoid the lock
1019b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  // level violation.
1020b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  contended_monitor = gRegistry->Add(contended_monitor_obj);
1021f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
1022f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
1023f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
1024ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott HughesJDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
1025ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes                                       std::vector<uint64_t>& counts)
1026ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1027412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
1028412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
10292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
1030ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  counts.clear();
1031ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  for (size_t i = 0; i < class_ids.size(); ++i) {
1032ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    JDWP::JdwpError status;
10332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_ids[i], status);
1034ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    if (c == NULL) {
1035ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes      return status;
1036ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    }
1037ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    classes.push_back(c);
1038ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    counts.push_back(0);
1039ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  }
1040412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CountInstances(classes, false, &counts[0]);
1041ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  return JDWP::ERR_NONE;
1042ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes}
1043ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes
10443b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott HughesJDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances)
10453b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1046412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
1047412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  // We only want reachable instances, so do a GC.
1048412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
10493b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  JDWP::JdwpError status;
10502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1051412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (c == nullptr) {
10523b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    return status;
10533b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
10542dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
10553b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances);
10563b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
10573b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    instances.push_back(gRegistry->Add(raw_instances[i]));
10583b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
10593b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  return JDWP::ERR_NONE;
10603b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes}
10613b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes
10620cbaff584244ee767027aff35cd3c625aaee2994Elliott HughesJDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
10630cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes                                         std::vector<JDWP::ObjectId>& referring_objects)
10640cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1065412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
1066412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
10672dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
106864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
10690cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
10700cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
10712dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
1072412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->GetReferringObjects(o, max_count, raw_instances);
10730cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
10740cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    referring_objects.push_back(gRegistry->Add(raw_instances[i]));
10750cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
10760cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  return JDWP::ERR_NONE;
10770cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes}
10780cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes
107964f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id)
108064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1081e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
1082e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
1083e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
1084e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
108564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisableCollection(object_id);
108664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
108764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
108864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
108964f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id)
109064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1091e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
1092e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI
1093e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // also ignores these cases and never return an error. However it's not obvious why this command
1094e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // should behave differently from DisableCollection and IsCollected commands. So let's be more
1095e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // strict and return an error if this happens.
1096e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
1097e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
1098e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
109964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->EnableCollection(object_id);
110064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
110164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
110264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
110364f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected)
110464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
110565637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  if (object_id == 0) {
110665637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    // Null object id is invalid.
110765637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
110865637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  }
1109e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However
111065637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  // the RI seems to ignore this and assume object has been collected.
111165637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
1112e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
111365637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = true;
111465637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  } else {
111565637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = gRegistry->IsCollected(object_id);
1116e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
111764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
111864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
111964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
112064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count)
112164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
112264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisposeObject(object_id, reference_count);
112364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
112464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
1125d539167b7f11136fe570a77aff2ee4935842007aSebastien HertzJDWP::JdwpTypeTag Dbg::GetTypeTag(mirror::Class* klass) {
11264d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  DCHECK(klass != nullptr);
11274d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  if (klass->IsArrayClass()) {
11284d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_ARRAY;
11294d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else if (klass->IsInterface()) {
11304d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_INTERFACE;
11314d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else {
11324d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_CLASS;
11334d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  }
11344d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz}
11354d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz
113688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1137436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1139436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1140436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11417b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1142436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
11434d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(c);
11444d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  expandBufAdd1(pReply, type_tag);
114588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  expandBufAddRefTypeId(pReply, class_id);
1146436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1147872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1148872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
11497b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughesvoid Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) {
1150a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Get the complete list of reference classes (i.e. all classes except
1151a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // the primitive types).
1152a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Returns a newly-allocated buffer full of RefTypeId values.
1153a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  struct ClassListCreator {
1154ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) {
11557b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    }
11567b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
11572dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static bool Visit(mirror::Class* c, void* arg) {
1158a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return reinterpret_cast<ClassListCreator*>(arg)->Visit(c);
1159a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1160a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
116164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
116264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
116364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS {
1164a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (!c->IsPrimitive()) {
116564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes        classes.push_back(gRegistry->AddRefType(c));
1166a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
1167a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return true;
1168a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1169a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
11707b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    std::vector<JDWP::RefTypeId>& classes;
1171a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
1172a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
11737b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  ClassListCreator clc(classes);
117495795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz  Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(ClassListCreator::Visit,
117595795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz                                                                       &clc);
1176872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1177872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1178cb6b0f31ede2275e79e6199ec391147585a37a2aIan RogersJDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag,
1179cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers                                  uint32_t* pStatus, std::string* pDescriptor) {
1180436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11812dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1182436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1183436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11847b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
11857b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
1186a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (c->IsArrayClass()) {
1187a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
1188a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = JDWP::TT_ARRAY;
1189a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
1190a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsErroneous()) {
1191a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_ERROR;
1192a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    } else {
1193a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
1194a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1195a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1196a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1197a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
1198a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (pDescriptor != NULL) {
1199cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    std::string temp;
1200cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    *pDescriptor = c->GetDescriptor(&temp);
1201a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1202436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1203872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1204872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1205c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughesvoid Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) {
12062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
12076fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
12086fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  ids.clear();
12096fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  for (size_t i = 0; i < classes.size(); ++i) {
12106fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes    ids.push_back(gRegistry->Add(classes[i]));
12116fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  }
1212872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1213872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
121464f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply)
121564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
12162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
121764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
12182435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
12192435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
12202435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
12214d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass());
122264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
12232435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
12242435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAdd1(pReply, type_tag);
12252435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAddRefTypeId(pReply, type_id);
12262435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
12272435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1228872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1229872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1230fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan RogersJDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) {
12311fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  JDWP::JdwpError status;
12322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
12331fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  if (c == NULL) {
12341fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes    return status;
12357b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1236cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  std::string temp;
1237cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  *signature = c->GetDescriptor(&temp);
12381fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  return JDWP::ERR_NONE;
1239872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1240872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
124188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) {
1242436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
12432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
12444206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  if (c == nullptr) {
1245436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
12467b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
12474206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  const char* source_file = c->GetSourceFile();
12484206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  if (source_file == nullptr) {
1249b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz    return JDWP::ERR_ABSENT_INFORMATION;
1250b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz  }
12514206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  result = source_file;
1252436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1253872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1254872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
125588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) {
12569837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
12572dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
125864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
1259546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
1260546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
12619837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  tag = TagFromObject(soa, o);
1262546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  return JDWP::ERR_NONE;
1263872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1264872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1265aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughessize_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
1266dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  switch (tag) {
1267dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_VOID:
1268dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 0;
1269dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BYTE:
1270dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BOOLEAN:
1271dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 1;
1272dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CHAR:
1273dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_SHORT:
1274dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 2;
1275dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_FLOAT:
1276dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_INT:
1277dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 4;
1278dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_ARRAY:
1279dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_OBJECT:
1280dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_STRING:
1281dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD:
1282dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD_GROUP:
1283dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_LOADER:
1284dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_OBJECT:
1285dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return sizeof(JDWP::ObjectId);
1286dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_DOUBLE:
1287dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_LONG:
1288dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 8;
1289dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  default:
12903d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Unknown tag " << tag;
1291dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return -1;
1292dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1293872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1294872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
129588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) {
12963d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
12972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
12983d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (a == NULL) {
12993d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
130024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
13013d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  length = a->GetLength();
13023d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1303872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1304872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
130588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) {
13063d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
13072dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
13089837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (a == nullptr) {
13093d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
13103d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
131124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
131224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
131324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
13143d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
131524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
1316cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  JDWP::JdwpTag element_tag = BasicTagFromClass(a->GetClass()->GetComponentType());
1317cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  expandBufAdd1(pReply, element_tag);
13183d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd4BE(pReply, count);
13193d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
1320cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  if (IsPrimitiveTag(element_tag)) {
1321cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    size_t width = GetTagWidth(element_tag);
132224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    uint8_t* dst = expandBufAddSpace(pReply, count * width);
132324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    if (width == 8) {
1324ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t), 0));
132524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
132624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 4) {
1327ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t), 0));
132824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
132924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 2) {
1330ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t), 0));
133124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
133224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else {
1333ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t), 0));
133424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      memcpy(dst, &src[offset * width], count * width);
133524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
133624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else {
13379837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
13382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>();
133924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    for (int i = 0; i < count; ++i) {
13402dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* element = oa->Get(offset + i);
13419837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element)
1342cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers                                                        : element_tag;
134324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAdd1(pReply, specific_tag);
134424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAddObjectId(pReply, gRegistry->Add(element));
134524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
134624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
134724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
13483d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1349872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1350872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1351ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate <typename T>
1352ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstatic void CopyArrayData(mirror::Array* a, JDWP::Request& src, int offset, int count)
1353ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    NO_THREAD_SAFETY_ANALYSIS {
1354ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  // TODO: fix when annotalysis correctly handles non-member functions.
13554b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  DCHECK(a->GetClass()->IsPrimitiveArray());
13564b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1357ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset));
13584b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  for (int i = 0; i < count; ++i) {
13594b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    *dst++ = src.ReadValue(sizeof(T));
13604b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
13614b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
13624b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
136388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
13644b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                      JDWP::Request& request)
1365b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
13663d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
13674b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  mirror::Array* dst = DecodeArray(array_id, status);
13684b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (dst == NULL) {
13693d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
13703d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
1371f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
13724b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
1373f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
13743d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
1375f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1376cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  JDWP::JdwpTag element_tag = BasicTagFromClass(dst->GetClass()->GetComponentType());
1377f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
1378cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers  if (IsPrimitiveTag(element_tag)) {
1379cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    size_t width = GetTagWidth(element_tag);
1380f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    if (width == 8) {
13814b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint64_t>(dst, request, offset, count);
1382f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 4) {
13834b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint32_t>(dst, request, offset, count);
1384f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 2) {
13854b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint16_t>(dst, request, offset, count);
1386f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else {
13874b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint8_t>(dst, request, offset, count);
1388f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1389f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  } else {
13904b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>();
1391f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    for (int i = 0; i < count; ++i) {
13924b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      JDWP::ObjectId id = request.ReadObjectId();
13932dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
139464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes      if (o == ObjectRegistry::kInvalidObject) {
1395436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
1396436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes      }
1397d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      oa->Set<false>(offset + i, o);
1398f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1399f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1400f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
14013d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1402872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1403872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
14047b3cdfcca472b779cf8745fb8460935e56229f11Elliott HughesJDWP::ObjectId Dbg::CreateString(const std::string& str) {
14052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str()));
1406872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1407872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
140888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) {
1409436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1411436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1412436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14137b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
141450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  new_object = gRegistry->Add(c->AllocObject(Thread::Current()));
1415436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1416872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1417872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1418bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes/*
1419bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1420bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes */
142188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
142200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                       JDWP::ObjectId& new_array) {
1423436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14242dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(array_class_id, status);
1425436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1426436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14277b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
14286fac447555dc94a935b78198479cce645c837b89Ian Rogers  new_array = gRegistry->Add(mirror::Array::Alloc<true>(Thread::Current(), c, length,
14296fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        c->GetComponentSize(),
14306fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        Runtime::Current()->GetHeap()->GetCurrentAllocator()));
1431436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1432872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1433872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1434d539167b7f11136fe570a77aff2ee4935842007aSebastien HertzJDWP::FieldId Dbg::ToFieldId(const mirror::ArtField* f) {
1435590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
143603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
143703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
143803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1439ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::MethodId ToMethodId(const mirror::ArtMethod* m)
1440b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1441590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
144203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m));
144303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
144403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1445ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtField* FromFieldId(JDWP::FieldId fid)
1446b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1447590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
1448ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtField*>(static_cast<uintptr_t>(fid));
1449aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes}
1450aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1451ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtMethod* FromMethodId(JDWP::MethodId mid)
1452b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1453590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
1454ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtMethod*>(static_cast<uintptr_t>(mid));
145503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
145603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1457d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzbool Dbg::MatchThread(JDWP::ObjectId expected_thread_id, Thread* event_thread) {
1458d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  CHECK(event_thread != nullptr);
1459d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* expected_thread_peer = gRegistry->Get<mirror::Object*>(expected_thread_id);
1460d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return expected_thread_peer == event_thread->GetPeer();
1461d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
1462d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
1463d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzbool Dbg::MatchLocation(const JDWP::JdwpLocation& expected_location,
1464d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz                        const JDWP::EventLocation& event_location) {
1465d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  if (expected_location.dex_pc != event_location.dex_pc) {
1466d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    return false;
1467d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  }
1468d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::ArtMethod* m = FromMethodId(expected_location.method_id);
1469d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return m == event_location.method;
1470d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
1471d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
1472d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzbool Dbg::MatchType(mirror::Class* event_class, JDWP::RefTypeId class_id) {
1473bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  if (event_class == nullptr) {
1474bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz    return false;
1475bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  }
1476d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::JdwpError status;
1477d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Class* expected_class = DecodeClass(class_id, status);
1478d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  CHECK(expected_class != nullptr);
1479d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return expected_class->IsAssignableFrom(event_class);
1480d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
1481d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
1482d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzbool Dbg::MatchField(JDWP::RefTypeId expected_type_id, JDWP::FieldId expected_field_id,
1483d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz                     mirror::ArtField* event_field) {
1484d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::ArtField* expected_field = FromFieldId(expected_field_id);
1485d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  if (expected_field != event_field) {
1486d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    return false;
1487d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  }
1488d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return Dbg::MatchType(event_field->GetDeclaringClass(), expected_type_id);
1489d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
1490d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
1491d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzbool Dbg::MatchInstance(JDWP::ObjectId expected_instance_id, mirror::Object* event_instance) {
1492d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* modifier_instance = gRegistry->Get<mirror::Object*>(expected_instance_id);
1493d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return modifier_instance == event_instance;
1494d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
1495d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
1496d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzvoid Dbg::SetJdwpLocation(JDWP::JdwpLocation* location, mirror::ArtMethod* m, uint32_t dex_pc)
1497b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1498d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  if (m == nullptr) {
1499bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz    memset(location, 0, sizeof(*location));
150091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  } else {
15012dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = m->GetDeclaringClass();
1502d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->type_tag = GetTypeTag(c);
1503d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->class_id = gRegistry->AddRefType(c);
1504d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->method_id = ToMethodId(m);
1505d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint64_t>(-1) : dex_pc;
150691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
1507d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes}
1508d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
1509a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetMethodName(JDWP::MethodId method_id)
1510b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1511ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
1512bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  if (m == nullptr) {
1513bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz    return "NULL";
1514bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  }
1515bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  return m->GetName();
1516872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1517872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1518a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetFieldName(JDWP::FieldId field_id)
1519a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1520bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  mirror::ArtField* f = FromFieldId(field_id);
1521bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  if (f == nullptr) {
1522bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz    return "NULL";
1523bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  }
1524bbb63897d7f2d99219cb50721fe530521e08ddffSebastien Hertz  return f->GetName();
1525a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes}
1526a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes
1527a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes/*
1528a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * Augment the access flags for synthetic methods and fields by setting
1529a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * the (as described by the spec) "0xf0000000 bit".  Also, strip out any
1530a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * flags not specified by the Java programming language.
1531a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes */
1532a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughesstatic uint32_t MangleAccessFlags(uint32_t accessFlags) {
1533a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  accessFlags &= kAccJavaFlagsMask;
1534a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  if ((accessFlags & kAccSynthetic) != 0) {
1535a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    accessFlags |= 0xf0000000;
1536a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1537a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return accessFlags;
1538872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1539872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1540dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes/*
1541b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come first. Debuggers
1542b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * expect slots to begin with arguments, but dex code places them at
1543b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * the end.
1544dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes */
1545b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Haostatic uint16_t MangleSlot(uint16_t slot, mirror::ArtMethod* m)
1546b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1547bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
1548cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1549cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1550cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1551cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m);
1552cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1553cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1554b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1555b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1556b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot >= locals_size) {
1557b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - locals_size;
1558b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1559b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + ins_size;
1560dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1561dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1562dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1563b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao/*
1564b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come last. Reverts
1565b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * slots to dex style argument placement.
1566b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao */
1567ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic uint16_t DemangleSlot(uint16_t slot, mirror::ArtMethod* m)
1568b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1569bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
1570cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1571cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1572cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1573cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m);
1574cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1575cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1576b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1577b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1578b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot < ins_size) {
1579b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + locals_size;
1580b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1581b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - ins_size;
1582dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1583dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1584dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
158588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1586436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
15872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1588436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1589436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
15907b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1591a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1592a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t instance_field_count = c->NumInstanceFields();
1593a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t static_field_count = c->NumStaticFields();
1594a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1595a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1596a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1597a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
1598ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count);
1599a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddFieldId(pReply, ToFieldId(f));
160061c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    expandBufAddUtf8String(pReply, f->GetName());
160161c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    expandBufAddUtf8String(pReply, f->GetTypeDescriptor());
1602c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1603a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1604a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1605a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1606a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1607a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1608436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1609a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1610a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
161188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
161200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                           JDWP::ExpandBuf* pReply) {
1613436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
16142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1615436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1616436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
16177b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1618a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1619a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t direct_method_count = c->NumDirectMethods();
1620a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t virtual_method_count = c->NumVirtualMethods();
1621a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1622a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
1623a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1624a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
1625ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
1626a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddMethodId(pReply, ToMethodId(m));
1627bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    expandBufAddUtf8String(pReply, m->GetName());
1628bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    expandBufAddUtf8String(pReply, m->GetSignature().ToString());
1629c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1630a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1631a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1632a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1633a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags()));
1634a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1635436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1636a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1637a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
163888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1639436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
1640f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  Thread* self = Thread::Current();
1641f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  StackHandleScope<1> hs(self);
1642f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  Handle<mirror::Class> c(hs.NewHandle(DecodeClass(class_id, status)));
1643f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  if (c.Get() == nullptr) {
1644436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
16457b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1646f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  size_t interface_count = c->NumDirectInterfaces();
1647a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, interface_count);
1648a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < interface_count; ++i) {
1649f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier    expandBufAddRefTypeId(pReply,
1650f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier                          gRegistry->AddRefType(mirror::Class::GetDirectInterface(self, c, i)));
1651a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1652436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1653872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1654872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
165588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply)
1656b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
165703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  struct DebugCallbackContext {
165803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    int numItems;
165903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    JDWP::ExpandBuf* pReply;
166003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
16612435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    static bool Callback(void* context, uint32_t address, uint32_t line_number) {
166203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
166303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      expandBufAdd8BE(pContext->pReply, address);
16642435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      expandBufAdd4BE(pContext->pReply, line_number);
166503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      pContext->numItems++;
1666f2910eef247b45ce1d489e323b36b5de6b6157aaSebastien Hertz      return false;
166703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
166803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
1669ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
1670bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
167103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  uint64_t start, end;
1672cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1673cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    DCHECK(m->IsNative() || m->IsProxyMethod());
167403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = -1;
167503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    end = -1;
167603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  } else {
167703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = 0;
167814f0db92225d34622fa5cb1a6dc9287334aaf6c7jeffhao    // Return the index of the last instruction
1679cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    end = code_item->insns_size_in_code_units_ - 1;
168003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  }
168103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
168203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, start);
168303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, end);
168403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
168503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  // Add numLines later
168603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  size_t numLinesOffset = expandBufGetLength(pReply);
168703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd4BE(pReply, 0);
168803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
168903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  DebugCallbackContext context;
169003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.numItems = 0;
169103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.pReply = pReply;
169203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1693cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1694bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
1695bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                     DebugCallbackContext::Callback, NULL, &context);
1696cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
169703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
169803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems);
1699872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1700872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1701bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartiervoid Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic,
1702bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                              JDWP::ExpandBuf* pReply) {
1703dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  struct DebugCallbackContext {
1704b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    mirror::ArtMethod* method;
1705dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    JDWP::ExpandBuf* pReply;
1706c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    size_t variable_count;
1707c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    bool with_generic;
1708dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1709bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress,
1710bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                         const char* name, const char* descriptor, const char* signature)
1711b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1712dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1713dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1714bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      VLOG(jdwp) << StringPrintf("    %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d",
1715bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                 pContext->variable_count, startAddress, endAddress - startAddress,
1716bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                 name, descriptor, signature, slot,
1717bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                 MangleSlot(slot, pContext->method));
1718dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1719b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao      slot = MangleSlot(slot, pContext->method);
172068fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
1721dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd8BE(pContext->pReply, startAddress);
1722dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, name);
1723dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, descriptor);
1724c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      if (pContext->with_generic) {
1725dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes        expandBufAddUtf8String(pContext->pReply, signature);
1726dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      }
1727dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, endAddress - startAddress);
1728dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, slot);
1729dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1730c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      ++pContext->variable_count;
1731dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    }
1732dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  };
1733ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
1734dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1735c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // arg_count considers doubles and longs to take 2 units.
1736c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // variable_count considers everything to take 1 unit.
1737bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  std::string shorty(m->GetShorty());
1738ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  expandBufAdd4BE(pReply, mirror::ArtMethod::NumArgRegisters(shorty));
1739dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1740c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // We don't know the total number of variables yet, so leave a blank and update it later.
1741c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  size_t variable_count_offset = expandBufGetLength(pReply);
1742dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  expandBufAdd4BE(pReply, 0);
1743dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1744dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  DebugCallbackContext context;
1745b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  context.method = m;
1746dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  context.pReply = pReply;
1747c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.variable_count = 0;
1748c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.with_generic = with_generic;
1749dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1750bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
1751cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1752bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    m->GetDexFile()->DecodeDebugInfo(
1753bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL, DebugCallbackContext::Callback,
1754bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        &context);
1755cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1756dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1757c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count);
1758872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1759872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1760579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value,
1761579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                                  JDWP::ExpandBuf* pReply) {
1762579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  mirror::ArtMethod* m = FromMethodId(method_id);
1763bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty());
1764579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  OutputJValue(tag, return_value, pReply);
1765579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1766579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
17673f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value,
17683f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                           JDWP::ExpandBuf* pReply) {
17693f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  mirror::ArtField* f = FromFieldId(field_id);
177061c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
17713f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  OutputJValue(tag, field_value, pReply);
17723f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
17733f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
17749777ba230c83a0edcbda2cf7b208339e77bf171bElliott HughesJDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
17759777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes                                  std::vector<uint8_t>& bytecodes)
17769777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1777ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
17789777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  if (m == NULL) {
17799777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    return JDWP::ERR_INVALID_METHODID;
17809777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
1781bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
17829777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  size_t byte_count = code_item->insns_size_in_code_units_ * 2;
17839777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_);
17849777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* end = begin + byte_count;
17859777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  for (const uint8_t* p = begin; p != end; ++p) {
17869777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    bytecodes.push_back(*p);
17879777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
17889777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  return JDWP::ERR_NONE;
17899777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes}
17909777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes
179188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
179261c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor());
1793872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1794872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
179588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
179661c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor());
1797872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1798872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
179988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
180088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                         JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
180100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         bool is_static)
1802b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
18030cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  JDWP::JdwpError status;
18042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(ref_type_id, status);
180588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  if (ref_type_id != 0 && c == NULL) {
18060cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    return status;
18070cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
18080cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
1809d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id);
181064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
18113f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
18123f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1813ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
18140cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
18152dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* receiver_class = c;
18160cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class == NULL && o != NULL) {
18170cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    receiver_class = o->GetClass();
18180cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
18190cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we give up now if receiver_class is NULL?
18200cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
18210cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class);
18223f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_FIELDID;
18233f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1824aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
18250cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
18260cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
18270cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
18280cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
18290cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
18300cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
18310cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
18320cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
18330cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
18340cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
18350cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
18360dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
18370dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
18380dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
18390cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
184061c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
1841579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  JValue field_value;
1842579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (tag == JDWP::JT_VOID) {
1843579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    LOG(FATAL) << "Unknown tag: " << tag;
1844579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (!IsPrimitiveTag(tag)) {
1845579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetL(f->GetObject(o));
1846579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1847579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetJ(f->Get64(o));
1848aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
1849579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetI(f->Get32(o));
1850aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
1851579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  Dbg::OutputJValue(tag, &field_value, pReply);
1852579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
18533f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  return JDWP::ERR_NONE;
1854872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1855872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
185688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
185700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ExpandBuf* pReply) {
185888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(0, object_id, field_id, pReply, false);
18593f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
18603f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
186188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) {
186288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
18633f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
18643f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
186588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
186600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         uint64_t value, int width, bool is_static)
1867b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1868d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  mirror::Object* o = Dbg::GetObjectRegistry()->Get<mirror::Object*>(object_id);
186964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
18703f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
18713f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1872ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
18730cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
18740cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
18750cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
18760cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
18770cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
18780cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
18790cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
18800cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
18810cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
18820cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
18830cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
18843f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
18850dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
18860dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
18870dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
1888aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
188961c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
1890aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1891aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  if (IsPrimitiveTag(tag)) {
1892aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
18931bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_EQ(width, 8);
1894d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1895d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set64<false>(o, value);
1896aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else {
18971bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_LE(width, 4);
1898d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1899d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set32<false>(o, value);
1900aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    }
1901aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
1902d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    mirror::Object* v = Dbg::GetObjectRegistry()->Get<mirror::Object*>(value);
190364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (v == ObjectRegistry::kInvalidObject) {
19043d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes      return JDWP::ERR_INVALID_OBJECT;
19053d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    }
19063f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    if (v != NULL) {
190761c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier      mirror::Class* field_type;
190861c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier      {
190961c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        StackHandleScope<3> hs(Thread::Current());
191061c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        HandleWrapper<mirror::Object> h_v(hs.NewHandleWrapper(&v));
191161c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f));
191261c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        HandleWrapper<mirror::Object> h_o(hs.NewHandleWrapper(&o));
191361c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        field_type = FieldHelper(h_f).GetType();
191461c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier      }
19153f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      if (!field_type->IsAssignableFrom(v->GetClass())) {
19163f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
19173f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      }
19183f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
1919d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    // Debugging can't use transactional mode (runtime only).
1920d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    f->SetObject<false>(o, v);
1921aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
19223d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
19233d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1924872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1925872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
192688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
192700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   int width) {
192888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(object_id, field_id, value, width, false);
1929872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1930872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
193188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
193288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(0, field_id, value, width, true);
1933872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1934872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
193529259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien HertzJDWP::JdwpError Dbg::StringToUtf8(JDWP::ObjectId string_id, std::string* str) {
193629259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  mirror::Object* obj = gRegistry->Get<mirror::Object*>(string_id);
193729259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  if (obj == nullptr || obj == ObjectRegistry::kInvalidObject) {
193829259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
193929259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  }
194029259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  {
194129259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz    ScopedObjectAccessUnchecked soa(Thread::Current());
194229259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz    mirror::Class* java_lang_String = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_String);
194329259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz    if (!java_lang_String->IsAssignableFrom(obj->GetClass())) {
194429259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz      // This isn't a string.
194529259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz      return JDWP::ERR_INVALID_STRING;
194629259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz    }
194729259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  }
194829259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  *str = obj->AsString()->ToModifiedUtf8();
194929259fa6b0514866d2d4bf57d58c1557b26abbb7Sebastien Hertz  return JDWP::ERR_NONE;
1950872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1951872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1952579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) {
1953579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (IsPrimitiveTag(tag)) {
1954579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAdd1(pReply, tag);
1955579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
1956579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd1(pReply, return_value->GetI());
1957579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
1958579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd2BE(pReply, return_value->GetI());
1959579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
1960579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd4BE(pReply, return_value->GetI());
1961579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1962579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd8BE(pReply, return_value->GetJ());
1963579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else {
1964579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      CHECK_EQ(tag, JDWP::JT_VOID);
1965579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    }
1966579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else {
19679837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
1968579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    mirror::Object* value = return_value->GetL();
19699837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    expandBufAdd1(pReply, TagFromObject(soa, value));
1970579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAddObjectId(pReply, gRegistry->Add(value));
1971579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  }
1972579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1973579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
1974221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) {
1975a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  ScopedObjectAccessUnchecked soa(Thread::Current());
1976a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1977221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1978221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1979221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
1980221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1981a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1982221229cb523f849f165fdafbf9785010963715daElliott Hughes
1983221229cb523f849f165fdafbf9785010963715daElliott Hughes  // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
19842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
1985ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* java_lang_Thread_name_field =
19862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
19872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s =
19882dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object));
1989221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (s != NULL) {
1990221229cb523f849f165fdafbf9785010963715daElliott Hughes    name = s->ToModifiedUtf8();
1991221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1992221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1993872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1994872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1995221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
19965d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
19972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
199864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_object == ObjectRegistry::kInvalidObject) {
19992435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
20002435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
20019837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroup");
20022435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Okay, so it's an object, but is it actually a thread?
2003b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  JDWP::JdwpError error;
2004b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  {
2005b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2006b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    Thread* thread;
2007b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    error = DecodeThread(soa, thread_id, thread);
2008b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
2009221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
2010221229cb523f849f165fdafbf9785010963715daElliott Hughes    // Zombie threads are in the null group.
2011221229cb523f849f165fdafbf9785010963715daElliott Hughes    expandBufAddObjectId(pReply, JDWP::ObjectId(0));
201252d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    error = JDWP::ERR_NONE;
201352d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  } else if (error == JDWP::ERR_NONE) {
201452d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
201552d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(c != nullptr);
201652d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::ArtField* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;");
2017b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    CHECK(f != nullptr);
201852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Object* group = f->GetObject(thread_object);
2019b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    CHECK(group != nullptr);
202052d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    JDWP::ObjectId thread_group_id = gRegistry->Add(group);
202152d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    expandBufAddObjectId(pReply, thread_group_id);
20222435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
20239837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
202452d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  return error;
2025872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2026872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
20275d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertzstatic mirror::Object* DecodeThreadGroup(ScopedObjectAccessUnchecked& soa,
20285d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz                                         JDWP::ObjectId thread_group_id, JDWP::JdwpError* error)
20295d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20305d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Object* thread_group = Dbg::GetObjectRegistry()->Get<mirror::Object*>(thread_group_id);
20315d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  if (thread_group == nullptr || thread_group == ObjectRegistry::kInvalidObject) {
20325d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    *error = JDWP::ERR_INVALID_OBJECT;
20335d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    return nullptr;
20345d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
20359837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
20369837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
20375d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  if (!c->IsAssignableFrom(thread_group->GetClass())) {
20385d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    // This is not a java.lang.ThreadGroup.
20395d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    *error = JDWP::ERR_INVALID_THREAD_GROUP;
20405d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    return nullptr;
20415d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
20425d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  *error = JDWP::ERR_NONE;
20435d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  return thread_group;
20445d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz}
20455d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
20465d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien HertzJDWP::JdwpError Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) {
20475d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
20485d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  JDWP::JdwpError error;
20495d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
20505d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  if (error != JDWP::ERR_NONE) {
20515d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    return error;
20525d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
20535d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupName");
20545d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
2055ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("name", "Ljava/lang/String;");
2056499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(f != NULL);
20572dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group));
20589837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
20595d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
20605d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  std::string thread_group_name(s->ToModifiedUtf8());
20615d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  expandBufAddUtf8String(pReply, thread_group_name);
20625d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  return JDWP::ERR_NONE;
2063872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2064872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
20655d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien HertzJDWP::JdwpError Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id, JDWP::ExpandBuf* pReply) {
20669837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
20675d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  JDWP::JdwpError error;
20685d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
20695d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  if (error != JDWP::ERR_NONE) {
20705d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    return error;
20715d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
20729837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupParent");
20739837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
20749837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
2075ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;");
20764e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(f != NULL);
20772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* parent = f->GetObject(thread_group);
20789837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
20795d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
20805d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  JDWP::ObjectId parent_group_id = gRegistry->Add(parent);
20815d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  expandBufAddObjectId(pReply, parent_group_id);
20825d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  return JDWP::ERR_NONE;
2083872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2084872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
20855d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertzstatic void GetChildThreadGroups(ScopedObjectAccessUnchecked& soa, mirror::Object* thread_group,
20865d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz                                 std::vector<JDWP::ObjectId>* child_thread_group_ids)
20875d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20885d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  CHECK(thread_group != nullptr);
20895d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
20905d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  // Get the ArrayList<ThreadGroup> "groups" out of this thread group...
20915d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::ArtField* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;");
20925d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Object* groups_array_list = groups_field->GetObject(thread_group);
20935d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
20945d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  // Get the array and size out of the ArrayList<ThreadGroup>...
20955d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::ArtField* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;");
20965d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::ArtField* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I");
20975d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::ObjectArray<mirror::Object>* groups_array =
20985d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz      array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>();
20995d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  const int32_t size = size_field->GetInt(groups_array_list);
21005d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
21015d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  // Copy the first 'size' elements out of the array into the result.
21025d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  ObjectRegistry* registry = Dbg::GetObjectRegistry();
21035d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  for (int32_t i = 0; i < size; ++i) {
21045d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    child_thread_group_ids->push_back(registry->Add(groups_array->Get(i)));
21055d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
21065d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz}
21075d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
21085d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien HertzJDWP::JdwpError Dbg::GetThreadGroupChildren(JDWP::ObjectId thread_group_id,
21095d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz                                            JDWP::ExpandBuf* pReply) {
211000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
21115d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  JDWP::JdwpError error;
21125d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::Object* thread_group = DecodeThreadGroup(soa, thread_group_id, &error);
21135d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  if (error != JDWP::ERR_NONE) {
21145d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    return error;
21155d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
21165d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
21175d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  // Add child threads.
21185d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  {
21195d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    std::vector<JDWP::ObjectId> child_thread_ids;
21205d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    GetThreads(thread_group, &child_thread_ids);
21215d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    expandBufAdd4BE(pReply, child_thread_ids.size());
21225d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    for (JDWP::ObjectId child_thread_id : child_thread_ids) {
21235d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz      expandBufAddObjectId(pReply, child_thread_id);
21245d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    }
21255d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
21265d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
21275d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  // Add child thread groups.
21285d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  {
21295d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    std::vector<JDWP::ObjectId> child_thread_groups_ids;
21305d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    GetChildThreadGroups(soa, thread_group, &child_thread_groups_ids);
21315d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    expandBufAdd4BE(pReply, child_thread_groups_ids.size());
21325d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    for (JDWP::ObjectId child_thread_group_id : child_thread_groups_ids) {
21335d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz      expandBufAddObjectId(pReply, child_thread_group_id);
21345d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz    }
21355d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  }
21365d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz
21375d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  return JDWP::ERR_NONE;
2138872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2139872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
21405d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien HertzJDWP::ObjectId Dbg::GetSystemThreadGroupId() {
21415d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
21425d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
21432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
2144365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
2145872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2146872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2147920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff HaoJDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) {
2148920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  switch (state) {
2149920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kBlocked:
2150920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_MONITOR;
2151920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kNative:
2152920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kRunnable:
2153920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSuspended:
2154920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_RUNNING;
2155920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSleeping:
2156920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_SLEEPING;
2157920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kStarting:
2158920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTerminated:
2159920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_ZOMBIE;
2160920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTimedWaiting:
2161bae182cbc6adc8796154162a87fc54ae804e0469Sebastien Hertz    case kWaitingForCheckPointsToRun:
2162920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSend:
2163920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSuspension:
2164920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerToAttach:
2165138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    case kWaitingForDeoptimization:
2166920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForGcToComplete:
2167920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForJniOnLoad:
2168bae182cbc6adc8796154162a87fc54ae804e0469Sebastien Hertz    case kWaitingForMethodTracingStart:
2169920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForSignalCatcherOutput:
2170920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainDebuggerLoop:
2171920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainSignalCatcherLoop:
2172920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingPerformingGc:
2173920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaiting:
2174920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_WAIT;
2175920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      // Don't add a 'default' here so the compiler can spot incompatible enum changes.
2176920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  }
2177920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  LOG(FATAL) << "Unknown thread state: " << state;
2178920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  return JDWP::TS_ZOMBIE;
2179920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao}
2180920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao
218152d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus,
218252d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                     JDWP::JdwpSuspendStatus* pSuspendStatus) {
218300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
2184499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
21859e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
21869e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
218750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2188221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2189221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2190221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2191221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
2192221229cb523f849f165fdafbf9785010963715daElliott Hughes      *pThreadStatus = JDWP::TS_ZOMBIE;
2193221229cb523f849f165fdafbf9785010963715daElliott Hughes      return JDWP::ERR_NONE;
2194221229cb523f849f165fdafbf9785010963715daElliott Hughes    }
2195221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2196499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  }
2197499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
21989e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  if (IsSuspendedForDebugger(soa, thread)) {
21999e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
22009e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  }
220100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
2202920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  *pThreadStatus = ToJdwpThreadStatus(thread->GetState());
2203221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
2204872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2205872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2206221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
220700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
220850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2209221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2210221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2211221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2212221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
22132435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
221450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
221500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
22162435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
2217872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2218872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2219f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott HughesJDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
2220f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccess soa(Thread::Current());
2221f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2222f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
2223f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2224f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
2225f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
2226f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
2227dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  thread->Interrupt(soa.Self());
2228f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
2229f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
2230f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
2231d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertzstatic bool IsInDesiredThreadGroup(ScopedObjectAccessUnchecked& soa,
2232d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz                                   mirror::Object* desired_thread_group, mirror::Object* peer)
2233d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2234d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  // Do we want threads from all thread groups?
2235d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  if (desired_thread_group == nullptr) {
2236d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    return true;
2237d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  }
2238d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  mirror::ArtField* thread_group_field = soa.DecodeField(WellKnownClasses::java_lang_Thread_group);
2239d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  DCHECK(thread_group_field != nullptr);
2240d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  mirror::Object* group = thread_group_field->GetObject(peer);
2241d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  return (group == desired_thread_group);
2242d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz}
2243365c10235438607541fa2259a5fec48061b90bd8Ian Rogers
22445d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertzvoid Dbg::GetThreads(mirror::Object* thread_group, std::vector<JDWP::ObjectId>* thread_ids) {
2245d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
2246d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  std::list<Thread*> all_threads_list;
2247d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  {
2248d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
2249d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    all_threads_list = Runtime::Current()->GetThreadList()->GetList();
2250d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  }
2251d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  for (Thread* t : all_threads_list) {
2252d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    if (t == Dbg::GetDebugThread()) {
2253d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
2254d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // query all threads, so it's easier if we just don't tell them about this thread.
2255d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      continue;
2256a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
2257d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    if (t->IsStillStarting()) {
2258d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // This thread is being started (and has been registered in the thread list). However, it is
2259d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // not completely started yet so we must ignore it.
2260d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      continue;
2261a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
2262d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    mirror::Object* peer = t->GetPeer();
2263d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    if (peer == nullptr) {
2264d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // peer might be NULL if the thread is still starting up. We can't tell the debugger about
2265d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // this thread yet.
2266d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // TODO: if we identified threads to the debugger by their Thread*
2267d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // rather than their peer's mirror::Object*, we could fix this.
2268d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      // Doing so might help us report ZOMBIE threads too.
2269d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      continue;
22700dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    }
2271d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    if (IsInDesiredThreadGroup(soa, thread_group, peer)) {
22725d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertz      thread_ids->push_back(gRegistry->Add(peer));
2273d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz    }
2274d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  }
2275a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes}
2276a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
22775d1c1bbd4c8a1727027d0ae69277af6f6f6badf3Sebastien Hertzstatic int GetStackDepth(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
22780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct CountStackDepthVisitor : public StackVisitor {
227993ba893c20532990a430741e0a97212900094e8cBrian Carlstrom    explicit CountStackDepthVisitor(Thread* thread)
22807a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth(0) {}
22810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
228264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
228364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
228464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
22850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!GetMethod()->IsRuntimeMethod()) {
2286f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes        ++depth;
2287f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes      }
2288530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
2289a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
2290a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    size_t depth;
2291a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  };
229208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
22937a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  CountStackDepthVisitor visitor(thread);
22940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2295a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return visitor.depth;
2296872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2297872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2298221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) {
229900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
2300a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2301221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2302221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2303221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2304221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2305221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2306f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2307f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2308f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
2309221229cb523f849f165fdafbf9785010963715daElliott Hughes  result = GetStackDepth(thread);
2310221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
23118696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
23128696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2313306057fd278d75bf3794bd5243a3b6652c487d18Ian RogersJDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame,
2314306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers                                     size_t frame_count, JDWP::ExpandBuf* buf) {
23156e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  class GetFrameVisitor : public StackVisitor {
23166e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   public:
23177a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    GetFrameVisitor(Thread* thread, size_t start_frame, size_t frame_count, JDWP::ExpandBuf* buf)
2318b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
23197a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth_(0),
23206e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes          start_frame_(start_frame), frame_count_(frame_count), buf_(buf) {
23216e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      expandBufAdd4BE(buf_, frame_count_);
232203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
23230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
232400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
232500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
232600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
23270399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetMethod()->IsRuntimeMethod()) {
23287934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        return true;  // The debugger can't do anything useful with a frame that has no Method*.
232903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
23306e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_ + frame_count_) {
2331530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes        return false;
233203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
23336e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_) {
23346e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::FrameId frame_id(GetFrameId());
23356e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::JdwpLocation location;
2336d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz        SetJdwpLocation(&location, GetMethod(), GetDexPc());
2337ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        VLOG(jdwp) << StringPrintf("    Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location;
23386e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAdd8BE(buf_, frame_id);
23396e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAddLocation(buf_, location);
23406e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      }
23416e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      ++depth_;
2342530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
234303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
23446e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes
23456e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   private:
23466e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    size_t depth_;
23476e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t start_frame_;
23486e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t frame_count_;
23496e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    JDWP::ExpandBuf* buf_;
235003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
235100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
235200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2353a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2354221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2355221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2356221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2357221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2358221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2359f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2360f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2361f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
23627a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetFrameVisitor visitor(thread, start_frame, frame_count, buf);
23630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
23646e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
2365872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2366872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2367872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetThreadSelfId() {
2368d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return GetThreadId(Thread::Current());
2369d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz}
2370d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz
2371d539167b7f11136fe570a77aff2ee4935842007aSebastien HertzJDWP::ObjectId Dbg::GetThreadId(Thread* thread) {
2372dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier  ScopedObjectAccessUnchecked soa(Thread::Current());
2373d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  return gRegistry->Add(thread->GetPeer());
2374872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2375872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2376475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::SuspendVM() {
237700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
2378872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2379872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2380872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ResumeVM() {
23814eec026ec4d86869baa27ad0afd9564cca8856c9Sebastien Hertz  Runtime::Current()->GetThreadList()->ResumeAllForDebugger();
2382872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2383872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2384221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
2385f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers  Thread* self = Thread::Current();
2386f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers  ScopedLocalRef<jobject> peer(self->GetJniEnv(), NULL);
238700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
2388f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers    ScopedObjectAccess soa(self);
23892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id)));
239000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
239100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (peer.get() == NULL) {
239200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
239300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
2394f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers  // Suspend thread to build stack trace. Take suspend thread lock to avoid races with threads
2395f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers  // trying to suspend this one.
2396f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers  MutexLock mu(self, *Locks::thread_list_suspend_thread_lock_);
2397f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  bool timed_out;
239837c16453a92bbf1a47f042000318a1b60381017dBrian Carlstrom  ThreadList* thread_list = Runtime::Current()->GetThreadList();
239937c16453a92bbf1a47f042000318a1b60381017dBrian Carlstrom  Thread* thread = thread_list->SuspendThreadByPeer(peer.get(), request_suspension, true,
240037c16453a92bbf1a47f042000318a1b60381017dBrian Carlstrom                                                    &timed_out);
240100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (thread != NULL) {
240200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_NONE;
2403f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else if (timed_out) {
240400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_INTERNAL;
240500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  } else {
240600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
24074e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
2408872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2409872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2410221229cb523f849f165fdafbf9785010963715daElliott Hughesvoid Dbg::ResumeThread(JDWP::ObjectId thread_id) {
241100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
24122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id);
2413a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  Thread* thread;
2414a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  {
2415a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2416a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    thread = Thread::FromManagedThread(soa, peer);
2417a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  }
24184e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  if (thread == NULL) {
24194e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    LOG(WARNING) << "No such thread for resume: " << peer;
24204e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    return;
24214e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
242200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool needs_resume;
242300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
242450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
242500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    needs_resume = thread->GetSuspendCount() > 0;
242600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
242700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (needs_resume) {
2428546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    Runtime::Current()->GetThreadList()->Resume(thread, true);
2429546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
2430872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2431872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2432872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::SuspendSelf() {
2433475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
2434872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2435872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
24360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct GetThisVisitor : public StackVisitor {
24377a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
2438b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
24397a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {}
244068fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
244100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
244200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
244300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
24446e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    if (frame_id != GetFrameId()) {
24450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return true;  // continue
24460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    } else {
244762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      this_object = GetThisObject();
244862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return false;
24490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
245068fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  }
2451dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
24522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* this_object;
24536e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  JDWP::FrameId frame_id;
24540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers};
2455ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
245600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersJDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
245700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ObjectId* result) {
245800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
245900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* thread;
246000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
246150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2462221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2463221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
2464221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
246500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
24669e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    if (!IsSuspendedForDebugger(soa, thread)) {
246700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return JDWP::ERR_THREAD_NOT_SUSPENDED;
246800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
24696e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  }
2470700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
24717a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor visitor(thread, context.get(), frame_id);
24720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
24736e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  *result = gRegistry->Add(visitor.this_object);
24746e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
24750399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
24760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
24775797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz// Walks the stack until we find the frame with the given FrameId.
24785797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertzclass FindFrameVisitor FINAL : public StackVisitor {
24795797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz public:
24805797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  FindFrameVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
24815797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
24825797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      : StackVisitor(thread, context), frame_id_(frame_id), error_(JDWP::ERR_INVALID_FRAMEID) {}
2483ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
24845797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
24855797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // annotalysis.
24865797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
24875797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (GetFrameId() != frame_id_) {
24885797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      return true;  // Not our frame, carry on.
24890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
24905797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    mirror::ArtMethod* m = GetMethod();
24915797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (m->IsNative()) {
24925797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      // We can't read/write local value from/into native method.
24935797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      error_ = JDWP::ERR_OPAQUE_FRAME;
24945797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    } else {
24955797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      // We found our frame.
24965797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      error_ = JDWP::ERR_NONE;
24975797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
24985797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    return false;
24995797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
25005797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
25015797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::JdwpError GetError() const {
25025797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    return error_;
25035797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
25045797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
25055797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz private:
25065797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  const JDWP::FrameId frame_id_;
25075797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::JdwpError error_;
25085797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz};
25095797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
25105797aef3bbfa6fc762d384e25b31f6a121664987Sebastien HertzJDWP::JdwpError Dbg::GetLocalValues(JDWP::Request* request, JDWP::ExpandBuf* pReply) {
25115797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::ObjectId thread_id = request->ReadThreadId();
25125797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::FrameId frame_id = request->ReadFrameId();
251300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
251400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2515221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
25165797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  {
25175797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
25185797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
25195797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (error != JDWP::ERR_NONE) {
25205797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      return error;
25215797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
2522221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
25235797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // Find the frame with the given frame_id.
2524700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
25255797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  FindFrameVisitor visitor(thread, context.get(), frame_id);
25260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
25275797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  if (visitor.GetError() != JDWP::ERR_NONE) {
25285797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    return visitor.GetError();
25295797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
25300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
25315797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // Read the values from visitor's context.
25325797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  int32_t slot_count = request->ReadSigned32("slot count");
25335797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  expandBufAdd4BE(pReply, slot_count);     /* "int values" */
25345797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  for (int32_t i = 0; i < slot_count; ++i) {
25355797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    uint32_t slot = request->ReadUnsigned32("slot");
25365797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpTag reqSigByte = request->ReadTag();
2537ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
25385797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    VLOG(jdwp) << "    --> slot " << slot << " " << reqSigByte;
25395797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
25405797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    size_t width = Dbg::GetTagWidth(reqSigByte);
25415797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    uint8_t* ptr = expandBufAddSpace(pReply, width+1);
25425797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpError error = Dbg::GetLocalValue(visitor, soa, slot, reqSigByte, ptr, width);
25435797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (error != JDWP::ERR_NONE) {
25445797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      return error;
25455797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
25465797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
25475797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  return JDWP::ERR_NONE;
25485797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz}
25495797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
25505797aef3bbfa6fc762d384e25b31f6a121664987Sebastien HertzJDWP::JdwpError Dbg::GetLocalValue(const StackVisitor& visitor, ScopedObjectAccessUnchecked& soa,
25515797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                                   int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width) {
25525797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  mirror::ArtMethod* m = visitor.GetMethod();
25535797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  uint16_t reg = DemangleSlot(slot, m);
25545797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // TODO: check that the tag is compatible with the actual type of the slot!
25555797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // TODO: check slot is valid for this method or return INVALID_SLOT error.
25565797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  constexpr JDWP::JdwpError kFailureErrorCode = JDWP::ERR_ABSENT_INFORMATION;
25575797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  switch (tag) {
25585797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_BOOLEAN: {
25595797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 1U);
25605797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
25615797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kIntVReg, &intVal)) {
25625797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get boolean local " << reg << " = " << intVal;
25635797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set1(buf + 1, intVal != 0);
25645797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
25655797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get boolean local " << reg;
25665797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
2567ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes      }
25685797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
25695797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
25705797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_BYTE: {
25715797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 1U);
25725797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
25735797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kIntVReg, &intVal)) {
25745797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get byte local " << reg << " = " << intVal;
25755797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set1(buf + 1, intVal);
25765797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
25775797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get byte local " << reg;
25785797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
2579cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      }
25805797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
25815797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
25825797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_SHORT:
25835797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CHAR: {
25845797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 2U);
25855797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
25865797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kIntVReg, &intVal)) {
25875797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get short/char local " << reg << " = " << intVal;
25885797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set2BE(buf + 1, intVal);
25895797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
25905797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get short/char local " << reg;
25915797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
25925797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
25935797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
25945797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
25955797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_INT: {
25965797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 4U);
25975797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
25985797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kIntVReg, &intVal)) {
25995797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get int local " << reg << " = " << intVal;
26005797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set4BE(buf + 1, intVal);
26015797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
26025797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get int local " << reg;
26035797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
26045797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
26055797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
26065797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
26075797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_FLOAT: {
26085797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 4U);
26095797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
26105797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kFloatVReg, &intVal)) {
26115797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get float local " << reg << " = " << intVal;
26125797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set4BE(buf + 1, intVal);
26135797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
26145797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get float local " << reg;
26155797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
26165797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
26175797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
26185797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
26195797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_ARRAY:
26205797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CLASS_LOADER:
26215797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CLASS_OBJECT:
26225797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_OBJECT:
26235797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_STRING:
26245797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_THREAD:
26255797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_THREAD_GROUP: {
26265797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, sizeof(JDWP::ObjectId));
26275797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint32_t intVal;
26285797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVReg(m, reg, kReferenceVReg, &intVal)) {
26295797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        mirror::Object* o = reinterpret_cast<mirror::Object*>(intVal);
26305797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get " << tag << " object local " << reg << " = " << o;
26315797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
26325797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz          LOG(FATAL) << "Register " << reg << " expected to hold " << tag << " object: " << o;
26330bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        }
26345797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        tag = TagFromObject(soa, o);
26355797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::SetObjectId(buf + 1, gRegistry->Add(o));
26365797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
26375797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get " << tag << " object local " << reg;
26385797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
26390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
26405797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
26415797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
26425797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_DOUBLE: {
26435797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 8U);
26445797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint64_t longVal;
26455797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) {
26465797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get double local " << reg << " = " << longVal;
26475797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set8BE(buf + 1, longVal);
26485797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
26495797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get double local " << reg;
26505797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
26515797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
26525797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
2653cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    }
26545797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_LONG: {
26555797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 8U);
26565797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      uint64_t longVal;
26575797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (visitor.GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &longVal)) {
26585797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "get long local " << reg << " = " << longVal;
26595797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        JDWP::Set8BE(buf + 1, longVal);
26605797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else {
26615797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to get long local " << reg;
26625797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
26635797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
26645797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
26655797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
26665797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    default:
26675797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      LOG(FATAL) << "Unknown tag " << tag;
26685797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
26695797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
26700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
26715797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // Prepend tag, which may have been updated.
26725797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::Set1(buf, tag);
26735797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  return JDWP::ERR_NONE;
26745797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz}
26755797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
26765797aef3bbfa6fc762d384e25b31f6a121664987Sebastien HertzJDWP::JdwpError Dbg::SetLocalValues(JDWP::Request* request) {
26775797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::ObjectId thread_id = request->ReadThreadId();
26785797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  JDWP::FrameId frame_id = request->ReadFrameId();
267900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
268000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2681221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
26825797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  {
26835797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
26845797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
26855797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (error != JDWP::ERR_NONE) {
26865797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      return error;
26875797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
2688221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
26895797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // Find the frame with the given frame_id.
2690700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
26915797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  FindFrameVisitor visitor(thread, context.get(), frame_id);
26920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
26935797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  if (visitor.GetError() != JDWP::ERR_NONE) {
26945797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    return visitor.GetError();
26955797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
26965797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
26975797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // Writes the values into visitor's context.
26985797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  int32_t slot_count = request->ReadSigned32("slot count");
26995797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  for (int32_t i = 0; i < slot_count; ++i) {
27005797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    uint32_t slot = request->ReadUnsigned32("slot");
27015797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpTag sigByte = request->ReadTag();
27025797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    size_t width = Dbg::GetTagWidth(sigByte);
27035797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    uint64_t value = request->ReadValue(width);
27045797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
27055797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    VLOG(jdwp) << "    --> slot " << slot << " " << sigByte << " " << value;
27065797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    JDWP::JdwpError error = Dbg::SetLocalValue(visitor, slot, sigByte, value, width);
27075797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    if (error != JDWP::ERR_NONE) {
27085797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      return error;
27095797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
27105797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
27115797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  return JDWP::ERR_NONE;
27125797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz}
27135797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz
27145797aef3bbfa6fc762d384e25b31f6a121664987Sebastien HertzJDWP::JdwpError Dbg::SetLocalValue(StackVisitor& visitor, int slot, JDWP::JdwpTag tag,
27155797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                                   uint64_t value, size_t width) {
27165797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  mirror::ArtMethod* m = visitor.GetMethod();
27175797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  uint16_t reg = DemangleSlot(slot, m);
27185797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // TODO: check that the tag is compatible with the actual type of the slot!
27195797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  // TODO: check slot is valid for this method or return INVALID_SLOT error.
27205797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  constexpr JDWP::JdwpError kFailureErrorCode = JDWP::ERR_ABSENT_INFORMATION;
27215797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  switch (tag) {
27225797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_BOOLEAN:
27235797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_BYTE:
27245797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 1U);
27255797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVReg(m, reg, static_cast<uint32_t>(value), kIntVReg)) {
27265797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set boolean/byte local " << reg << " = "
27275797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                   << static_cast<uint32_t>(value);
27285797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27295797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27305797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27315797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_SHORT:
27325797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CHAR:
27335797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 2U);
27345797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVReg(m, reg, static_cast<uint32_t>(value), kIntVReg)) {
27355797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set short/char local " << reg << " = "
27365797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                   << static_cast<uint32_t>(value);
27375797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27385797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27395797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27405797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_INT:
27415797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 4U);
27425797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVReg(m, reg, static_cast<uint32_t>(value), kIntVReg)) {
27435797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set int local " << reg << " = "
27445797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                   << static_cast<uint32_t>(value);
27455797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27465797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27475797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27485797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_FLOAT:
27495797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 4U);
27505797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVReg(m, reg, static_cast<uint32_t>(value), kFloatVReg)) {
27515797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set float local " << reg << " = "
27525797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                   << static_cast<uint32_t>(value);
27535797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27545797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27555797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27565797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_ARRAY:
27575797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CLASS_LOADER:
27585797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_CLASS_OBJECT:
27595797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_OBJECT:
27605797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_STRING:
27615797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_THREAD:
27625797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_THREAD_GROUP: {
27635797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, sizeof(JDWP::ObjectId));
27645797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value));
27655797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (o == ObjectRegistry::kInvalidObject) {
27665797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << tag << " object " << o << " is an invalid object";
27675797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return JDWP::ERR_INVALID_OBJECT;
27685797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      } else if (!visitor.SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)),
27695797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz                          kReferenceVReg)) {
27705797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set " << tag << " object local " << reg << " = " << o;
27715797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27725797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27735797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27745797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
27755797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_DOUBLE: {
27765797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 8U);
27775797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVRegPair(m, reg, value, kDoubleLoVReg, kDoubleHiVReg)) {
27785797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set double local " << reg << " = " << value;
27795797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27805797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27815797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27825797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
27835797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    case JDWP::JT_LONG: {
27845797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      CHECK_EQ(width, 8U);
27855797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      if (!visitor.SetVRegPair(m, reg, value, kLongLoVReg, kLongHiVReg)) {
27865797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        VLOG(jdwp) << "failed to set double local " << reg << " = " << value;
27875797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz        return kFailureErrorCode;
27885797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      }
27895797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27905797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    }
27915797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz    default:
27925797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      LOG(FATAL) << "Unknown tag " << tag;
27935797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz      break;
27945797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  }
27955797aef3bbfa6fc762d384e25b31f6a121664987Sebastien Hertz  return JDWP::ERR_NONE;
2796872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2797872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2798d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertzstatic void SetEventLocation(JDWP::EventLocation* location, mirror::ArtMethod* m, uint32_t dex_pc)
2799d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2800d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  DCHECK(location != nullptr);
2801d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  if (m == nullptr) {
2802d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    memset(location, 0, sizeof(*location));
2803d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  } else {
2804d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->method = m;
2805d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    location->dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint32_t>(-1) : dex_pc;
28063f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
28073f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
28083f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2809ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersvoid Dbg::PostLocationEvent(mirror::ArtMethod* m, int dex_pc, mirror::Object* this_object,
2810579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                            int event_flags, const JValue* return_value) {
28113f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
28123f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
28133f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
28143f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
28153f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK_EQ(m->IsStatic(), this_object == nullptr);
2816d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::EventLocation location;
2817d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  SetEventLocation(&location, m, dex_pc);
281891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
2819d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  gJdwpState->PostLocationEvent(&location, this_object, event_flags, return_value);
2820872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2821872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
28223f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldAccessEvent(mirror::ArtMethod* m, int dex_pc,
28233f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                               mirror::Object* this_object, mirror::ArtField* f) {
28243f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
28253f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
28263f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
28273f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
28283f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
2829d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::EventLocation location;
2830d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  SetEventLocation(&location, m, dex_pc);
28313f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2832d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  gJdwpState->PostFieldEvent(&location, f, this_object, nullptr, false);
28333f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
28343f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
28353f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldModificationEvent(mirror::ArtMethod* m, int dex_pc,
28363f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     mirror::Object* this_object, mirror::ArtField* f,
28373f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     const JValue* field_value) {
28383f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
28393f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
28403f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
28413f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
28423f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
28433f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(field_value != nullptr);
2844d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::EventLocation location;
2845d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  SetEventLocation(&location, m, dex_pc);
28463f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2847d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  gJdwpState->PostFieldEvent(&location, f, this_object, field_value, true);
28483f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
28493f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
28503f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostException(const ThrowLocation& throw_location,
2851ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                        mirror::ArtMethod* catch_method,
285264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes                        uint32_t catch_dex_pc, mirror::Throwable* exception_object) {
2853c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
28540ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers    return;
28550ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers  }
2856d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::EventLocation exception_throw_location;
2857d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  SetEventLocation(&exception_throw_location, throw_location.GetMethod(), throw_location.GetDexPc());
2858d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  JDWP::EventLocation exception_catch_location;
2859d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  SetEventLocation(&exception_catch_location, catch_method, catch_dex_pc);
28604740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
2861d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  gJdwpState->PostException(&exception_throw_location, exception_object, &exception_catch_location,
2862d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz                            throw_location.GetThis());
2863872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2864872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
28652dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::PostClassPrepare(mirror::Class* c) {
2866c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
28674740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes    return;
28684740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  }
2869d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz  gJdwpState->PostClassPrepare(c);
2870872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2871872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
287262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object,
28738379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz                         mirror::ArtMethod* m, uint32_t dex_pc,
28748379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz                         int event_flags, const JValue* return_value) {
287562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
28762aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes    return;
287791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
287891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
28798696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  if (IsBreakpoint(m, dex_pc)) {
28808696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    event_flags |= kBreakpoint;
288191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
288291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
288361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // If the debugger is single-stepping one of our threads, check to
288461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // see if we're that thread and we've reached a step point.
288561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  const SingleStepControl* single_step_control = thread->GetSingleStepControl();
288661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
288761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (single_step_control->is_active) {
288861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    CHECK(!m->IsNative());
288961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    if (single_step_control->step_depth == JDWP::SD_INTO) {
289061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step into method calls.  We break when the line number
289161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // or method pointer changes.  If we're in SS_MIN mode, we
289261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // always stop.
289361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (single_step_control->method != m) {
289461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
289561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new method";
289661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (single_step_control->step_size == JDWP::SS_MIN) {
289761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
289861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new instruction";
2899bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      } else if (single_step_control->ContainsDexPc(dex_pc)) {
290061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
290161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new line";
290261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
290361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else if (single_step_control->step_depth == JDWP::SD_OVER) {
290461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step over method calls.  We break when the line number is
290561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // different and the frame depth is <= the original frame
290661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth.  (We can't just compare on the method, because we
290761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // might get unrolled past it by an exception, and it's tricky
290861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // to identify recursion.)
290961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
291061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
291161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
291261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
291361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Popped up one or more frames, always trigger.
291461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
291561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
291661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (stack_depth == single_step_control->stack_depth) {
291761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Same depth, see if we moved.
291861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control->step_size == JDWP::SS_MIN) {
29198696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          event_flags |= kSingleStep;
29208696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          VLOG(jdwp) << "SS new instruction";
2921bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        } else if (single_step_control->ContainsDexPc(dex_pc)) {
29222435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          event_flags |= kSingleStep;
29232435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          VLOG(jdwp) << "SS new line";
292491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes        }
292561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
292661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else {
292761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      CHECK_EQ(single_step_control->step_depth, JDWP::SD_OUT);
292861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Return from the current method.  We break when the frame
292961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth pops up.
293061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
293161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // This differs from the "method exit" break in that it stops
293261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // with the PC at the next instruction in the returned-to
293361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // function, rather than the end of the returning function.
293461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
293561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
293661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
293761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
293861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
29398696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
294091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    }
294191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
294291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
294391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // If there's something interesting going on, see if it matches one
294491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // of the debugger filters.
294591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (event_flags != 0) {
29468379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value);
294791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
294891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes}
294991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
295042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) {
295142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  switch (instrumentation_event) {
295242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kMethodEntered:
295342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &method_enter_event_ref_count_;
295442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kMethodExited:
295542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &method_exit_event_ref_count_;
295642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kDexPcMoved:
295742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &dex_pc_change_event_ref_count_;
295842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kFieldRead:
295942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &field_read_event_ref_count_;
296042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kFieldWritten:
296142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &field_write_event_ref_count_;
296242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kExceptionCaught:
296342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &exception_catch_event_ref_count_;
296442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    default:
296542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return nullptr;
296642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  }
296742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz}
296842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz
29694d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz// Process request while all mutator threads are suspended.
29704d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) {
2971138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
29720ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  switch (request.GetKind()) {
29734d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kNothing:
29744d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(WARNING) << "Ignoring empty deoptimization request.";
29754d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
297642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kRegisterForEvent:
297742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x",
29780ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                 request.InstrumentationEvent());
29790ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent());
29800ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation_events_ |= request.InstrumentationEvent();
298142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
298242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kUnregisterForEvent:
298342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x",
29840ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                 request.InstrumentationEvent());
298542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      instrumentation->RemoveListener(&gDebugInstrumentationListener,
29860ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                      request.InstrumentationEvent());
29870ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation_events_ &= ~request.InstrumentationEvent();
298842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
29894d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization:
29907ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world ...";
29914d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->DeoptimizeEverything();
29927ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world DONE";
29934d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
29944d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization:
29957ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world ...";
29964d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->UndeoptimizeEverything();
29977ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world DONE";
29984d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
29994d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization:
30000ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " ...";
30010ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation->Deoptimize(request.Method());
30020ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " DONE";
30034d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
30044d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization:
30050ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " ...";
30060ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation->Undeoptimize(request.Method());
30070ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " DONE";
30084d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
30094d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default:
30100ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind();
30114d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
30124d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
30134d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz}
30144d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz
30157ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::DelayFullUndeoptimization() {
3016f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  if (RequiresDeoptimization()) {
3017f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
3018f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    ++delayed_full_undeoptimization_count_;
3019f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    DCHECK_LE(delayed_full_undeoptimization_count_, full_deoptimization_event_count_);
3020f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  }
30217ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
30227ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
30237ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::ProcessDelayedFullUndeoptimizations() {
30247ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  // TODO: avoid taking the lock twice (once here and once in ManageDeoptimization).
30257ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  {
3026f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
30277ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    while (delayed_full_undeoptimization_count_ > 0) {
30287ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      DeoptimizationRequest req;
30290ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req.SetKind(DeoptimizationRequest::kFullUndeoptimization);
30300ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req.SetMethod(nullptr);
30317ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      RequestDeoptimizationLocked(req);
30327ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      --delayed_full_undeoptimization_count_;
30337ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    }
30347ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  }
30357ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  ManageDeoptimization();
30367ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
30377ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
30384d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::RequestDeoptimization(const DeoptimizationRequest& req) {
30390ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  if (req.GetKind() == DeoptimizationRequest::kNothing) {
30404d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    // Nothing to do.
30414d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    return;
30424d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
3043f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  MutexLock mu(Thread::Current(), *Locks::deoptimization_lock_);
30447ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  RequestDeoptimizationLocked(req);
30457ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
30467ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
30477ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) {
30480ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  switch (req.GetKind()) {
304942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kRegisterForEvent: {
30500ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK_NE(req.InstrumentationEvent(), 0u);
30510ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent());
305242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
30530ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                                req.InstrumentationEvent());
305442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      if (*counter == 0) {
30557d2ae437a87ceb2bdda098ab11f4da588c6a75f5Sebastien Hertz        VLOG(jdwp) << StringPrintf("Queue request #%zd to start listening to instrumentation event 0x%x",
30560ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                   deoptimization_requests_.size(), req.InstrumentationEvent());
305742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz        deoptimization_requests_.push_back(req);
305842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      }
305942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      *counter = *counter + 1;
306042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
306142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
306242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kUnregisterForEvent: {
30630ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK_NE(req.InstrumentationEvent(), 0u);
30640ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent());
306542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
30660ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                                req.InstrumentationEvent());
306742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      *counter = *counter - 1;
306842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      if (*counter == 0) {
30697d2ae437a87ceb2bdda098ab11f4da588c6a75f5Sebastien Hertz        VLOG(jdwp) << StringPrintf("Queue request #%zd to stop listening to instrumentation event 0x%x",
30700ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                   deoptimization_requests_.size(), req.InstrumentationEvent());
307142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz        deoptimization_requests_.push_back(req);
307242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      }
307342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
307442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
30754d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization: {
30760ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() == nullptr);
30774d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
30787ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
30797ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full deoptimization";
30804d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
3081138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
30824d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ++full_deoptimization_event_count_;
30834d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
30844d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
30854d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization: {
30860ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() == nullptr);
3087e713d9338ad122d6b8c7997387d0c9fc464eea3eSebastien Hertz      DCHECK_GT(full_deoptimization_event_count_, 0U);
30884d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      --full_deoptimization_event_count_;
30894d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
30907ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
30917ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full undeoptimization";
30924d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
3093138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
30944d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
30954d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
30964d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization: {
30970ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() != nullptr);
30987ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
30990ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                 << " for deoptimization of " << PrettyMethod(req.Method());
31004d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
31014d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
31024d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
31034d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization: {
31040ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() != nullptr);
31057ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
31060ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                 << " for undeoptimization of " << PrettyMethod(req.Method());
31074d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
31084d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
31094d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
31104d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default: {
31110ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind();
31124d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
3113138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
3114138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3115138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
3116138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3117138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertzvoid Dbg::ManageDeoptimization() {
3118138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Thread* const self = Thread::Current();
3119138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
3120138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    // Avoid suspend/resume if there is no pending request.
3121f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    MutexLock mu(self, *Locks::deoptimization_lock_);
31224d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    if (deoptimization_requests_.empty()) {
3123138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      return;
3124138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
3125138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3126138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
3127138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization);
3128138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  // We need to suspend mutator threads first.
3129138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Runtime* const runtime = Runtime::Current();
3130138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->SuspendAll();
3131138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  const ThreadState old_state = self->SetStateUnsafe(kRunnable);
31324d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  {
3133f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    MutexLock mu(self, *Locks::deoptimization_lock_);
31347ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    size_t req_index = 0;
31350ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    for (DeoptimizationRequest& request : deoptimization_requests_) {
31367ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Process deoptimization request #" << req_index++;
31374d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ProcessDeoptimizationRequest(request);
31384d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
31394d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    deoptimization_requests_.clear();
31404d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
3141138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
3142138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->ResumeAll();
3143138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromSuspendedToRunnable();
3144138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
3145138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3146a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic bool IsMethodPossiblyInlined(Thread* self, mirror::ArtMethod* m)
3147a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3148bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
3149a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (code_item == nullptr) {
3150a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // TODO We should not be asked to watch location in a native or abstract method so the code item
3151a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // should never be null. We could just check we never encounter this case.
3152a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    return false;
3153a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
31544d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  // Note: method verifier may cause thread suspension.
31554d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  self->AssertThreadSuspensionIsAllowable();
3156eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<2> hs(self);
3157bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  mirror::Class* declaring_class = m->GetDeclaringClass();
3158bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
3159bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
3160bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  verifier::MethodVerifier verifier(dex_cache->GetDexFile(), &dex_cache, &class_loader,
3161bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                    &m->GetClassDef(), code_item, m->GetDexMethodIndex(), m,
316246960fe5dcc1be07b39a55114338423a73554449Ian Rogers                                    m->GetAccessFlags(), false, true, false);
3163a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // Note: we don't need to verify the method.
3164a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr);
3165a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
3166138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3167a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic const Breakpoint* FindFirstBreakpointForMethod(mirror::ArtMethod* m)
316859d9d668d4f4286813afe2b4e7c6db839222ce96Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) {
31690ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  for (Breakpoint& breakpoint : gBreakpoints) {
31700ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    if (breakpoint.Method() == m) {
3171a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      return &breakpoint;
3172138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
3173a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
3174a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return nullptr;
3175a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
3176138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3177a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Sanity checks all existing breakpoints on the same method.
3178f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertzstatic void SanityCheckExistingBreakpoints(mirror::ArtMethod* m,
3179f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz                                           DeoptimizationRequest::Kind deoptimization_kind)
318059d9d668d4f4286813afe2b4e7c6db839222ce96Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::breakpoint_lock_) {
31814d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  for (const Breakpoint& breakpoint : gBreakpoints) {
3182f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    if (breakpoint.Method() == m) {
3183f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      CHECK_EQ(deoptimization_kind, breakpoint.GetDeoptimizationKind());
3184f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    }
31854d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  }
3186f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
3187f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) {
31884d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // We should have deoptimized everything but not "selectively" deoptimized this method.
3189f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK(instrumentation->AreAllMethodsDeoptimized());
3190f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK(!instrumentation->IsDeoptimized(m));
3191f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
31924d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // We should have "selectively" deoptimized this method.
31934d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // Note: while we have not deoptimized everything for this method, we may have done it for
31944d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // another event.
3195f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK(instrumentation->IsDeoptimized(m));
3196f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  } else {
3197f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    // This method does not require deoptimization.
3198f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing);
3199f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK(!instrumentation->IsDeoptimized(m));
3200138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3201a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
3202138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3203f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertzstatic DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self,
3204f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz                                                                 mirror::ArtMethod* m)
3205f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3206f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  if (!Dbg::RequiresDeoptimization()) {
3207f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    // We already run in interpreter-only mode so we don't need to deoptimize anything.
3208f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    VLOG(jdwp) << "No need for deoptimization when fully running with interpreter for method "
3209f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz               << PrettyMethod(m);
3210f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    return DeoptimizationRequest::kNothing;
3211f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  }
32124d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  const Breakpoint* existing_breakpoint;
32134d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  {
32144d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    ReaderMutexLock mu(self, *Locks::breakpoint_lock_);
32154d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    existing_breakpoint = FindFirstBreakpointForMethod(m);
32164d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  }
3217a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (existing_breakpoint == nullptr) {
3218a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no breakpoint on this method yet: we need to deoptimize. If this method may be
3219a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // inlined, we deoptimize everything; otherwise we deoptimize only this method.
32204d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // Note: IsMethodPossiblyInlined goes into the method verifier and may cause thread suspension.
32214d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    // Therefore we must not hold any lock when we call it.
3222f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    bool need_full_deoptimization = IsMethodPossiblyInlined(self, m);
3223a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
3224f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      VLOG(jdwp) << "Need full deoptimization because of possible inlining of method "
3225f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz                 << PrettyMethod(m);
3226f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      return DeoptimizationRequest::kFullDeoptimization;
3227a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
3228f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      // We don't need to deoptimize if the method has not been compiled.
3229f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
3230f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      const bool is_compiled = class_linker->GetOatMethodQuickCodeFor(m) != nullptr;
3231f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      if (is_compiled) {
3232f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz        VLOG(jdwp) << "Need selective deoptimization for compiled method " << PrettyMethod(m);
3233f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz        return DeoptimizationRequest::kSelectiveDeoptimization;
3234f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      } else {
3235f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz        // Method is not compiled: we don't need to deoptimize.
3236f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz        VLOG(jdwp) << "No need for deoptimization for non-compiled method " << PrettyMethod(m);
3237f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz        return DeoptimizationRequest::kNothing;
3238f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      }
3239a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
3240a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
3241a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to deoptimize.
3242f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    // Let's check that all breakpoints are configured the same way for deoptimization.
3243f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    VLOG(jdwp) << "Breakpoint already set: no deoptimization is required";
3244f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    DeoptimizationRequest::Kind deoptimization_kind = existing_breakpoint->GetDeoptimizationKind();
32454d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    if (kIsDebugBuild) {
32464d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz      ReaderMutexLock mu(self, *Locks::breakpoint_lock_);
3247f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      SanityCheckExistingBreakpoints(m, deoptimization_kind);
32484d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    }
3249f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    return DeoptimizationRequest::kNothing;
3250f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  }
3251f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz}
3252f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz
3253f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz// Installs a breakpoint at the specified location. Also indicates through the deoptimization
3254f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz// request if we need to deoptimize.
3255f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertzvoid Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
3256f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  Thread* const self = Thread::Current();
3257f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  mirror::ArtMethod* m = FromMethodId(location->method_id);
3258f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
3259f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz
3260f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  const DeoptimizationRequest::Kind deoptimization_kind = GetRequiredDeoptimizationKind(self, m);
3261f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  req->SetKind(deoptimization_kind);
3262f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
3263f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    req->SetMethod(m);
3264f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  } else {
3265f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    CHECK(deoptimization_kind == DeoptimizationRequest::kNothing ||
3266f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz          deoptimization_kind == DeoptimizationRequest::kFullDeoptimization);
3267f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    req->SetMethod(nullptr);
3268138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3269a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
32704d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  {
32714d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    WriterMutexLock mu(self, *Locks::breakpoint_lock_);
3272f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    gBreakpoints.push_back(Breakpoint(m, location->dex_pc, deoptimization_kind));
32734d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": "
32744d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz               << gBreakpoints[gBreakpoints.size() - 1];
32754d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz  }
3276872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3277872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3278a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization
3279a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// request if we need to undeoptimize.
32804d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
328159d9d668d4f4286813afe2b4e7c6db839222ce96Sebastien Hertz  WriterMutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
3282ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(location->method_id);
3283a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
3284f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz  DeoptimizationRequest::Kind deoptimization_kind = DeoptimizationRequest::kNothing;
3285a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
32860ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].Method() == m) {
3287a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
3288f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      deoptimization_kind = gBreakpoints[i].GetDeoptimizationKind();
3289f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      DCHECK_EQ(deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization,
3290f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz                Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
3291a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      gBreakpoints.erase(gBreakpoints.begin() + i);
3292a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      break;
3293138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
3294138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3295a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
3296a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (existing_breakpoint == nullptr) {
3297a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no more breakpoint on this method: we need to undeoptimize.
3298f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    if (deoptimization_kind == DeoptimizationRequest::kFullDeoptimization) {
3299a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required full deoptimization: we need to undeoptimize everything.
33000ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetKind(DeoptimizationRequest::kFullUndeoptimization);
33010ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetMethod(nullptr);
3302f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    } else if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
3303a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required selective deoptimization: we need to undeoptimize only that method.
33040ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization);
33050ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetMethod(m);
3306f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz    } else {
3307f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      // This method had no need for deoptimization: do nothing.
3308f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      CHECK_EQ(deoptimization_kind, DeoptimizationRequest::kNothing);
3309f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      req->SetKind(DeoptimizationRequest::kNothing);
3310f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      req->SetMethod(nullptr);
3311a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
3312a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
3313a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to undeoptimize.
33140ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    req->SetKind(DeoptimizationRequest::kNothing);
33150ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    req->SetMethod(nullptr);
33164d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    if (kIsDebugBuild) {
3317f2134f684923454e00c8ca7675b431a8538131bcSebastien Hertz      SanityCheckExistingBreakpoints(m, deoptimization_kind);
33184d6e48392771467b9e37fdeb32479ca29bf9c691Sebastien Hertz    }
33198696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
3320872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3321872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3322449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
3323449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// cause suspension if the thread is the current thread.
3324449db33fafa29578df60e8a323f78d5eb6247e76Jeff Haoclass ScopedThreadSuspension {
3325449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao public:
332633e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers  ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
332752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz      LOCKS_EXCLUDED(Locks::thread_list_lock_)
332833e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
3329f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers      thread_(nullptr),
3330449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_(JDWP::ERR_NONE),
3331449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      self_suspend_(false),
333233e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      other_suspend_(false) {
3333449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    ScopedObjectAccessUnchecked soa(self);
3334449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    {
3335449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
3336449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_ = DecodeThread(soa, thread_id, thread_);
3337449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3338449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (error_ == JDWP::ERR_NONE) {
3339449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      if (thread_ == soa.Self()) {
3340449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        self_suspend_ = true;
3341449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      } else {
3342449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
3343d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz        jobject thread_peer = Dbg::GetObjectRegistry()->GetJObject(thread_id);
3344449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        bool timed_out;
3345f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers        Thread* suspended_thread;
3346f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers        {
3347f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers          // Take suspend thread lock to avoid races with threads trying to suspend this one.
3348f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers          MutexLock mu(soa.Self(), *Locks::thread_list_suspend_thread_lock_);
334937c16453a92bbf1a47f042000318a1b60381017dBrian Carlstrom          ThreadList* thread_list = Runtime::Current()->GetThreadList();
335037c16453a92bbf1a47f042000318a1b60381017dBrian Carlstrom          suspended_thread = thread_list->SuspendThreadByPeer(thread_peer, true, true, &timed_out);
3351f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers        }
3352449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension);
3353f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers        if (suspended_thread == nullptr) {
3354449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          // Thread terminated from under us while suspending.
3355449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          error_ = JDWP::ERR_INVALID_THREAD;
3356449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        } else {
3357449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          CHECK_EQ(suspended_thread, thread_);
3358449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          other_suspend_ = true;
3359449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        }
3360449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      }
3361449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3362449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3363449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3364449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* GetThread() const {
3365449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return thread_;
3366449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3367449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3368449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError GetError() const {
3369449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return error_;
3370449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3371449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3372449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ~ScopedThreadSuspension() {
3373449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (other_suspend_) {
3374449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      Runtime::Current()->GetThreadList()->Resume(thread_, true);
3375449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3376449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3377449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3378449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao private:
3379449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* thread_;
3380449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError error_;
3381449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool self_suspend_;
3382449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool other_suspend_;
3383449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao};
3384449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3385221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
338600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::JdwpStepDepth step_depth) {
3387449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* self = Thread::Current();
3388449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ScopedThreadSuspension sts(self, thread_id);
3389449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  if (sts.GetError() != JDWP::ERR_NONE) {
3390449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return sts.GetError();
33912435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
33928696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
33932435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
33942435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Work out what Method* we're in, the current line number, and how deep the stack currently
33952435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // is for step-out.
33962435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
33972435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
33980399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SingleStepStackVisitor : public StackVisitor {
339961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    explicit SingleStepStackVisitor(Thread* thread, SingleStepControl* single_step_control,
340061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz                                    int32_t* line_number)
3401b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
340261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        : StackVisitor(thread, NULL), single_step_control_(single_step_control),
340361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          line_number_(line_number) {
340461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      DCHECK_EQ(single_step_control_, thread->GetSingleStepControl());
340561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->method = NULL;
340661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->stack_depth = 0;
34078696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
3408ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
340900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
341000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
341100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
341261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      mirror::ArtMethod* m = GetMethod();
34130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!m->IsRuntimeMethod()) {
341461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        ++single_step_control_->stack_depth;
341561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control_->method == NULL) {
3416ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache();
341761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->method = m;
341861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          *line_number_ = -1;
34192435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          if (dex_cache != NULL) {
34204445a7e3398a6143939168097a3aa275b734504dIan Rogers            const DexFile& dex_file = *dex_cache->GetDexFile();
342161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz            *line_number_ = dex_file.GetLineNumFromPC(m, GetDexPc());
34222435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          }
34238696433d1b3d8ba15288483b777edd888de69135Elliott Hughes        }
34248696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
3425530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
34268696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
342761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
342861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
342961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    int32_t* const line_number_;
34308696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  };
3431449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
343261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* const thread = sts.GetThread();
343361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepControl* const single_step_control = thread->GetSingleStepControl();
343461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
343561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  int32_t line_number = -1;
343661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepStackVisitor visitor(thread, single_step_control, &line_number);
34370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
34388696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
34392435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
34402435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
34412435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
34422435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
34432435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  struct DebugCallbackContext {
3444bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    explicit DebugCallbackContext(SingleStepControl* single_step_control, int32_t line_number,
3445bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz                                  const DexFile::CodeItem* code_item)
3446bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      : single_step_control_(single_step_control), line_number_(line_number), code_item_(code_item),
344761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        last_pc_valid(false), last_pc(0) {
34482435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
34492435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
345061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) {
34512435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context);
345261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (static_cast<int32_t>(line_number) == context->line_number_) {
34532435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        if (!context->last_pc_valid) {
34542435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          // Everything from this address until the next line change is ours.
34552435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc = address;
34562435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc_valid = true;
34572435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
34582435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Otherwise, if we're already in a valid range for this line,
34592435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // just keep going (shouldn't really happen)...
34607934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      } else if (context->last_pc_valid) {  // and the line number is new
34612435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Add everything from the last entry up until here to the set
34622435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) {
346361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          context->single_step_control_->dex_pcs.insert(dex_pc);
34642435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
34652435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        context->last_pc_valid = false;
34662435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
34677934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return false;  // There may be multiple entries for any given line.
34682435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
34692435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
347061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    ~DebugCallbackContext() {
34712435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      // If the line number was the last in the position table...
34722435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      if (last_pc_valid) {
3473bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        size_t end = code_item_->insns_size_in_code_units_;
34742435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) {
347561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->dex_pcs.insert(dex_pc);
34762435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
34772435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
34782435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
34792435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
348061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
348161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    const int32_t line_number_;
3482bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    const DexFile::CodeItem* const code_item_;
34832435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    bool last_pc_valid;
34842435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    uint32_t last_pc;
34852435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  };
348661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->dex_pcs.clear();
3487ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  mirror::ArtMethod* m = single_step_control->method;
348861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (!m->IsNative()) {
3489bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    const DexFile::CodeItem* const code_item = m->GetCodeItem();
3490bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    DebugCallbackContext context(single_step_control, line_number, code_item);
3491bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
3492bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                     DebugCallbackContext::Callback, NULL, &context);
34933e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes  }
34942435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
34952435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
34962435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Everything else...
34972435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
34982435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
349961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_size = step_size;
350061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_depth = step_depth;
350161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->is_active = true;
35028696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
35032435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  if (VLOG_IS_ON(jdwp)) {
350461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step thread: " << *thread;
350561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step size: " << single_step_control->step_size;
350661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step depth: " << single_step_control->step_depth;
350761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->method);
350861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current line: " << line_number;
350961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->stack_depth;
35102435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step dex_pc values:";
3511bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    for (uint32_t dex_pc : single_step_control->dex_pcs) {
3512bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      VLOG(jdwp) << StringPrintf(" %#x", dex_pc);
35132435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
35142435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
35152435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
35162435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
3517872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3518872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
351961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertzvoid Dbg::UnconfigureStep(JDWP::ObjectId thread_id) {
352061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
352161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
352261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* thread;
352361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
352487118ed6f3f99e7df33214c277cf200a7b9a7499Sebastien Hertz  if (error == JDWP::ERR_NONE) {
352561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* single_step_control = thread->GetSingleStepControl();
352661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    DCHECK(single_step_control != nullptr);
3527bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    single_step_control->Clear();
352861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  }
3529872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3530872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
353145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughesstatic char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
353245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  switch (tag) {
353345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    default:
353445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
353545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
353645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Primitives.
353745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BYTE:    return 'B';
353845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CHAR:    return 'C';
353945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_FLOAT:   return 'F';
354045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_DOUBLE:  return 'D';
354145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_INT:     return 'I';
354245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_LONG:    return 'J';
354345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_SHORT:   return 'S';
354445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_VOID:    return 'V';
354545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BOOLEAN: return 'Z';
354645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
354745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Reference types.
354845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_ARRAY:
354945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_OBJECT:
355045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_STRING:
355145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD:
355245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD_GROUP:
355345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_LOADER:
355445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_OBJECT:
355545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return 'L';
355645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  }
355745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes}
355845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
355988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id,
356088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                  JDWP::RefTypeId class_id, JDWP::MethodId method_id,
356100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  uint32_t arg_count, uint64_t* arg_values,
356200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* arg_types, uint32_t options,
356300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* pResultTag, uint64_t* pResultValue,
356400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::ObjectId* pExceptionId) {
3565d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  ThreadList* thread_list = Runtime::Current()->GetThreadList();
3566d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3567d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  Thread* targetThread = NULL;
3568d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  DebugInvokeReq* req = NULL;
356900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* self = Thread::Current();
3570d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
357100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(self);
357250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
3573221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread);
3574221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
3575221229cb523f849f165fdafbf9785010963715daElliott Hughes      LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
3576221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
3577d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3578d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    req = targetThread->GetInvokeReq();
3579d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (!req->ready) {
3580d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
3581d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      return JDWP::ERR_INVALID_THREAD;
3582d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3583d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3584d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3585d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We currently have a bug where we don't successfully resume the
3586d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * target thread if the suspend count is too deep.  We're expected to
3587d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * require one "resume" for each "suspend", but when asked to execute
3588d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a method we have to resume fully and then re-suspend it back to the
3589d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * same level.  (The easiest way to cause this is to type "suspend"
3590d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * multiple times in jdb.)
3591d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3592d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * It's unclear what this means when the event specifies "resume all"
3593d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * and some threads are suspended more deeply than others.  This is
3594d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a rare problem, so for now we just prevent it from hanging forever
3595d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * by rejecting the method invocation request.  Without this, we will
3596d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * be stuck waiting on a suspended thread.
3597d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
359800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    int suspend_count;
359900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
360050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
360100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      suspend_count = targetThread->GetSuspendCount();
360200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
3603d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (suspend_count > 1) {
3604d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count;
36057934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return JDWP::ERR_THREAD_SUSPENDED;  // Probably not expected here.
3606d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3607d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
36083f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    JDWP::JdwpError status;
36092dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id);
361064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (receiver == ObjectRegistry::kInvalidObject) {
36113f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
36123f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
361345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
36142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id);
361564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (thread == ObjectRegistry::kInvalidObject) {
36163f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
36173f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
361845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // TODO: check that 'thread' is actually a java.lang.Thread!
361945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
36202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_id, status);
362145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (c == NULL) {
36223f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return status;
36233f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
362445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3625ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = FromMethodId(method_id);
362645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic() != (receiver == NULL)) {
362745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_INVALID_METHODID;
362845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
362945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic()) {
363045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (m->GetDeclaringClass() != c) {
363145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
363245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
363345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    } else {
363445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
363545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
363645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
363745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
363845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
363945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Check the argument list matches the method.
3640bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    uint32_t shorty_len = 0;
3641bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    const char* shorty = m->GetShorty(&shorty_len);
3642bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    if (shorty_len - 1 != arg_count) {
364345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_ILLEGAL_ARGUMENT;
364445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
36450920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
3646bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    {
3647bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      StackHandleScope<3> hs(soa.Self());
3648bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      MethodHelper mh(hs.NewHandle(m));
3649bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver));
3650bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      HandleWrapper<mirror::Class> h_klass(hs.NewHandleWrapper(&c));
3651bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      const DexFile::TypeList* types = m->GetParameterTypeList();
3652bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      for (size_t i = 0; i < arg_count; ++i) {
3653bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
36540920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_ILLEGAL_ARGUMENT;
36550920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
36560920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
3657bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        if (shorty[i + 1] == 'L') {
3658bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          // Did we really get an argument of an appropriate reference type?
3659bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          mirror::Class* parameter_type = mh.GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
3660bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i]);
3661bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          if (argument == ObjectRegistry::kInvalidObject) {
3662bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier            return JDWP::ERR_INVALID_OBJECT;
3663bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          }
3664bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          if (argument != NULL && !argument->InstanceOf(parameter_type)) {
3665bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier            return JDWP::ERR_ILLEGAL_ARGUMENT;
3666bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          }
3667bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier
3668bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          // Turn the on-the-wire ObjectId into a jobject.
3669bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
3670bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          v.l = gRegistry->GetJObject(arg_values[i]);
3671bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        }
36720920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      }
3673bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      // Update in case it moved.
3674bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      m = mh.GetMethod();
367545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
367645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3677d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->receiver = receiver;
3678d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->thread = thread;
3679d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->klass = c;
3680d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->method = m;
3681d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_count = arg_count;
3682d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_values = arg_values;
3683d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->options = options;
3684d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->invoke_needed = true;
3685d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3686d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3687d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // The fact that we've released the thread list lock is a bit risky --- if the thread goes
3688d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // away we're sitting high and dry -- but we must release this before the ResumeAllThreads
3689d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // call, and it's unwise to hold it during WaitForSuspend.
3690d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3691d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
3692d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3693d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We change our (JDWP thread) status, which should be THREAD_RUNNING,
369481ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes     * so we can suspend for a GC if the invoke request causes us to
3695d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * run out of memory.  It's also a good idea to change it before locking
3696d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the invokeReq mutex, although that should never be held for long.
3697d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
369800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
3699d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
37004dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Transferring control to event thread";
3701d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    {
3702d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      MutexLock mu(self, req->lock);
3703d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3704d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
37054dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming all threads";
370600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers        thread_list->UndoDebuggerSuspensions();
3707d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      } else {
37084dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming event thread only";
3709d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes        thread_list->Resume(targetThread, true);
3710d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3711d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3712d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      // Wait for the request to finish executing.
3713d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      while (req->invoke_needed) {
3714d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz        req->cond.Wait(self);
3715d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3716d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
37174dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Control has returned from event thread";
3718d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3719d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* wait for thread to re-suspend itself */
3720df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom    SuspendThread(thread_id, false /* request_suspension */);
372100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
3722d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3723d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3724d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  /*
3725d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * Suspend the threads.  We waited for the target thread to suspend
3726d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * itself, so all we need to do is suspend the others.
3727d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   *
3728d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * The suspendAllThreads() call will double-suspend the event thread,
3729d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * so we want to resume the target thread once to keep the books straight.
3730d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   */
3731d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
373200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
37334dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Suspending all threads";
373400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    thread_list->SuspendAllForDebugger();
373500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
37364dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Resuming event thread to balance the count";
3737d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    thread_list->Resume(targetThread, true);
3738d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3739d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3740d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Copy the result.
3741d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pResultTag = req->result_tag;
3742d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (IsPrimitiveTag(req->result_tag)) {
3743f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = req->result_value.GetJ();
3744d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else {
3745f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = gRegistry->Add(req->result_value.GetL());
3746d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3747d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pExceptionId = req->exception;
3748d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  return req->error;
3749872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3750872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3751872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
375200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
3753d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
375481ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // We can be called while an exception is pending. We need
3755d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // to preserve that across the method invocation.
3756eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<4> hs(soa.Self());
3757eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_throw_this_object = hs.NewHandle<mirror::Object>(nullptr);
3758eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_throw_method = hs.NewHandle<mirror::ArtMethod>(nullptr);
3759eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_exception = hs.NewHandle<mirror::Throwable>(nullptr);
376062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  uint32_t old_throw_dex_pc;
37619f1020305292a21fd14a402b189c765a125226abSebastien Hertz  bool old_exception_report_flag;
376262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  {
376362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation old_throw_location;
376462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
3765eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_throw_this_object.Assign(old_throw_location.GetThis());
3766eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_throw_method.Assign(old_throw_location.GetMethod());
3767eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_exception.Assign(old_exception_obj);
376862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_dex_pc = old_throw_location.GetDexPc();
37699f1020305292a21fd14a402b189c765a125226abSebastien Hertz    old_exception_report_flag = soa.Self()->IsExceptionReportedToInstrumentation();
377062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ClearException();
377162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
3772d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3773d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Translate the method through the vtable, unless the debugger wants to suppress it.
3774eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ArtMethod> m(hs.NewHandle(pReq->method));
3775d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz  if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver != NULL) {
3776eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    mirror::ArtMethod* actual_method = pReq->klass->FindVirtualMethodForVirtualOrInterface(m.Get());
3777eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    if (actual_method != m.Get()) {
3778eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m.Get()) << " to " << PrettyMethod(actual_method);
3779eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      m.Assign(actual_method);
378045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
3781d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3782eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m.Get())
3783d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " receiver=" << pReq->receiver
3784d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " arg_count=" << pReq->arg_count;
3785eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  CHECK(m.Get() != nullptr);
3786d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3787d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  CHECK_EQ(sizeof(jvalue), sizeof(uint64_t));
3788d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3789eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  pReq->result_value = InvokeWithJValues(soa, pReq->receiver, soa.EncodeMethod(m.Get()),
379053b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers                                         reinterpret_cast<jvalue*>(pReq->arg_values));
3791d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
379262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  mirror::Throwable* exception = soa.Self()->GetException(NULL);
379362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ClearException();
379462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  pReq->exception = gRegistry->Add(exception);
3795bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  pReq->result_tag = BasicTagFromDescriptor(m.Get()->GetShorty());
3796d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (pReq->exception != 0) {
379762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    VLOG(jdwp) << "  JDWP invocation returning with exception=" << exception
379862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        << " " << exception->Dump();
3799f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    pReq->result_value.SetJ(0);
3800d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else if (pReq->result_tag == JDWP::JT_OBJECT) {
3801d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* if no exception thrown, examine object result more closely */
38029837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    JDWP::JdwpTag new_tag = TagFromObject(soa, pReq->result_value.GetL());
3803d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (new_tag != pReq->result_tag) {
38044dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes      VLOG(jdwp) << "  JDWP promoted result from " << pReq->result_tag << " to " << new_tag;
3805d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      pReq->result_tag = new_tag;
3806d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3807d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3808d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3809d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * Register the object.  We don't actually need an ObjectId yet,
3810d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * but we do need to be sure that the GC won't move or discard the
3811d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * object when we switch out of RUNNING.  The ObjectId conversion
3812d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * will add the object to the "do not touch" list.
3813d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3814d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We can't use the "tracked allocation" mechanism here because
3815d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the object is going to be handed off to a different thread.
3816d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
3817f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    gRegistry->Add(pReq->result_value.GetL());
3818d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3819d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3820eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  if (old_exception.Get() != NULL) {
3821eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
382262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                         old_throw_dex_pc);
3823eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
38249f1020305292a21fd14a402b189c765a125226abSebastien Hertz    soa.Self()->SetExceptionReportedToInstrumentation(old_exception_report_flag);
3825d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3826872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3827872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3828d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes/*
38294b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * "request" contains a full JDWP packet, possibly with multiple chunks.  We
3830f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * need to process each, accumulate the replies, and ship the whole thing
3831f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * back.
3832f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
3833f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * Returns "true" if we have a reply.  The reply buffer is newly allocated,
3834f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * and includes the chunk type/length, followed by the data.
3835f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
38363d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes * OLD-TODO: we currently assume that the request and reply include a single
3837f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * chunk.  If this becomes inconvenient we will need to adapt.
3838f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes */
38394b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesbool Dbg::DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen) {
3840f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  Thread* self = Thread::Current();
3841f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  JNIEnv* env = self->GetJniEnv();
3842f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
38434b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t type = request.ReadUnsigned32("type");
38444b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t length = request.ReadUnsigned32("length");
38454b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
38464b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  // Create a byte[] corresponding to 'request'.
38474b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  size_t request_length = request.size();
38484b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length));
38496a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (dataArray.get() == NULL) {
38504b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    LOG(WARNING) << "byte[] allocation failed: " << request_length;
3851f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3852f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3853f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
38544b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  env->SetByteArrayRegion(dataArray.get(), 0, request_length, reinterpret_cast<const jbyte*>(request.data()));
38554b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  request.Skip(request_length);
3856f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3857f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Run through and find all chunks.  [Currently just find the first.]
38586a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ScopedByteArrayRO contents(env, dataArray.get());
38594b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (length != request_length) {
3860ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%zd)", length, request_length);
3861f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3862f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3863f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3864f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
3865eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3866eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                                                                 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
38674b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                                                 type, dataArray.get(), 0, length));
3868f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (env->ExceptionCheck()) {
3869f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
3870f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionDescribe();
3871f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3872f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3873f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3874f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
38756a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (chunk.get() == NULL) {
3876f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3877f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3878f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3879f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  /*
3880f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * Pull the pieces out of the chunk.  We copy the results into a
3881f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * newly-allocated buffer that the caller can free.  We don't want to
3882f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * continue using the Chunk object because nothing has a reference to it.
3883f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3884f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * We could avoid this by returning type/data/offset/length and having
3885f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * the caller be aware of the object lifetime issues, but that
388681ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes   * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
3887f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * if we have responses for multiple chunks.
3888f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3889f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * So we're pretty much stuck with copying data around multiple times.
3890f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   */
3891eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
38924b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
3893eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
3894eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
3895f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
38964dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length);
38976a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (length == 0 || replyData.get() == NULL) {
3898f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3899f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3900f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
39014b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  const int kChunkHdrLen = 8;
3902f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  uint8_t* reply = new uint8_t[length + kChunkHdrLen];
3903f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (reply == NULL) {
3904f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
3905f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3906f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3907f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 0, type);
3908f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 4, length);
39096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
3910f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3911f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyBuf = reply;
3912f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyLen = length + kChunkHdrLen;
3913f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
39144b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length);
3915f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  return true;
3916872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3917872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3918a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::DdmBroadcast(bool connect) {
39194dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
392047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
392147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  Thread* self = Thread::Current();
392250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  if (self->GetState() != kRunnable) {
392350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
392450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    /* try anyway? */
392547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
392647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
392747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  JNIEnv* env = self->GetJniEnv();
392847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
3929eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3930eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
3931eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            event);
393247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (env->ExceptionCheck()) {
393347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
393447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionDescribe();
393547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionClear();
393647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
393747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
393847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3939872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmConnected() {
3940a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(true);
3941872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3942872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3943872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmDisconnected() {
3944a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(false);
394547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = false;
394647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
394747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
394847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes/*
39498218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes * Send a notification when a thread starts, stops, or changes its name.
395047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes *
395147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * Because we broadcast the full set of threads when the notifications are
395247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * first enabled, it's possible for "thread" to be actively executing.
395347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes */
39548218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
395547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (!gDdmThreadNotification) {
395647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    return;
395747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
395847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
39598218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  if (type == CHUNK_TYPE("THDE")) {
39608218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    uint8_t buf[4];
3961d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Set4BE(&buf[0], t->GetThreadId());
39628218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
39638218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  } else {
39648218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
396500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
3966eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    StackHandleScope<1> hs(soa.Self());
3967eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::String> name(hs.NewHandle(t->GetThreadName(soa)));
3968eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    size_t char_count = (name.Get() != NULL) ? name->GetLength() : 0;
3969eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    const jchar* chars = (name.Get() != NULL) ? name->GetCharArray()->GetData() : NULL;
397047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
397121f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    std::vector<uint8_t> bytes;
3972d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Append4BE(bytes, t->GetThreadId());
3973545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    JDWP::AppendUtf16BE(bytes, chars, char_count);
397421f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
397521f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    Dbg::DdmSendChunk(type, bytes);
397647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
397747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
397847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
397947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::DdmSetThreadNotification(bool enable) {
398000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Enable/disable thread notifications.
398147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = enable;
398247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (enable) {
398300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Suspend the VM then post thread start notifications for all threads. Threads attaching will
398400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // see a suspension in progress and block until that ends. They then post their own start
398500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // notification.
398600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    SuspendVM();
398700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    std::list<Thread*> threads;
398850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
398900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
399050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, *Locks::thread_list_lock_);
399100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      threads = Runtime::Current()->GetThreadList()->GetList();
399200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
399300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
399450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      ScopedObjectAccess soa(self);
399502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (Thread* thread : threads) {
399602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR"));
399700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
399800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
399900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ResumeVM();
400047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
400147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
400247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
4003a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
4004c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (IsDebuggerActive()) {
4005d539167b7f11136fe570a77aff2ee4935842007aSebastien Hertz    gJdwpState->PostThreadChange(t, type == CHUNK_TYPE("THCR"));
400647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
40078218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  Dbg::DdmSendThreadNotification(t, type);
400847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
400947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
401047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadStart(Thread* t) {
4011a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
401247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
401347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
401447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadDeath(Thread* t) {
4015a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
4016872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
4017872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
40188218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) {
40193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(buf != NULL);
40203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  iovec vec[1];
40213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
40223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_len = byte_count;
40233bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Dbg::DdmSendChunkV(type, vec, 1);
4024872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
4025872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
402621f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) {
402721f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  DdmSendChunk(type, bytes.size(), &bytes[0]);
402821f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes}
402921f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes
4030f52935278fca8c7aa220543eef4544e3d1105d91Brian Carlstromvoid Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
40313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (gJdwpState == NULL) {
40324dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
40333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
4034cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    gJdwpState->DdmSendChunkV(type, iov, iov_count);
40353bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
4036872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
4037872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
4038767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesint Dbg::DdmHandleHpifChunk(HpifWhen when) {
4039767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when == HPIF_WHEN_NOW) {
40407162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(when);
4041767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return true;
4042767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
4043767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4044767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
4045767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
4046767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
4047767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
4048767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4049767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  gDdmHpifWhen = when;
4050767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
4051767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
4052767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4053767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesbool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
4054767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
4055767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
4056767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
4057767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
4058767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4059767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
4060767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
4061767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
4062767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
4063767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4064767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (native) {
4065767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhen = when;
4066767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhat = what;
4067767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  } else {
4068767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhen = when;
4069767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhat = what;
4070767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
4071767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
4072767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
4073767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
40747162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesvoid Dbg::DdmSendHeapInfo(HpifWhen reason) {
40757162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  // If there's a one-shot 'when', reset it.
40767162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  if (reason == gDdmHpifWhen) {
40777162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
40787162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes      gDdmHpifWhen = HPIF_WHEN_NEVER;
40797162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    }
40807162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  }
40817162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
40827162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  /*
40837162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Chunk HPIF (client --> server)
40847162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
40857162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Heap Info. General information about the heap,
40867162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * suitable for a summary display.
40877162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
40887162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   [u4]: number of heaps
40897162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
40907162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   For each heap:
40917162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: heap ID
40927162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u8]: timestamp in ms since Unix epoch
40937162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u1]: capture reason (same as 'when' value from server)
40947162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: max heap size in bytes (-Xmx)
40957162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current heap size in bytes
40967162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of bytes allocated
40977162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of objects allocated
40987162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   */
40997162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  uint8_t heap_count = 1;
41001d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::Heap* heap = Runtime::Current()->GetHeap();
410121f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  std::vector<uint8_t> bytes;
4102545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append4BE(bytes, heap_count);
41037934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, 1);  // Heap id (bogus; we only have one heap).
4104545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append8BE(bytes, MilliTime());
4105545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append1BE(bytes, reason);
41067934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetMaxMemory());  // Max allowed heap size in bytes.
41077934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetTotalMemory());  // Current heap size in bytes.
4108b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetBytesAllocated());
4109b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
411021f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
411121f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes);
4112767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
4113767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
41146a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgSolidity {
41156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FREE = 0,
41166a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_HARD = 1,
41176a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SOFT = 2,
41186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_WEAK = 3,
41196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_PHANTOM = 4,
41206a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FINALIZABLE = 5,
41216a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SWEEP = 6,
41226a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
41236a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41246a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgKind {
41256a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_OBJECT = 0,
41266a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_CLASS_OBJECT = 1,
41276a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_1 = 2,
41286a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_2 = 3,
41296a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_4 = 4,
41306a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_8 = 5,
41316a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_UNKNOWN = 6,
41326a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_NATIVE = 7,
41336a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
41346a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41356a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_PARTIAL (1<<7)
41366a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
41376a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
413830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogersclass HeapChunkContext {
413930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers public:
41406a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Maximum chunk size.  Obtain this from the formula:
41416a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
41426a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  HeapChunkContext(bool merge, bool native)
414330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      : buf_(16384 - 16),
414430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        type_(0),
414536dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        merge_(merge),
414636dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        chunk_overhead_(0) {
41476a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
41486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    if (native) {
414930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = CHUNK_TYPE("NHSG");
41506a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    } else {
415130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
41526a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
41536a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
41546a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41556a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ~HeapChunkContext() {
415630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (p_ > &buf_[0]) {
41576a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      Flush();
41586a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
41596a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
41606a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
416136dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  void SetChunkOverhead(size_t chunk_overhead) {
416236dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    chunk_overhead_ = chunk_overhead;
416336dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  }
416436dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier
416536dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  void ResetStartOfNextChunk() {
416636dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    startOfNextMemoryChunk_ = nullptr;
416736dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  }
416836dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier
41696a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void EnsureHeader(const void* chunk_ptr) {
417030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (!needHeader_) {
41716a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      return;
41726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
41736a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41746a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Start a new HPSx chunk.
41757934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 1);  // Heap id (bogus; we only have one heap).
41767934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write1BE(&p_, 8);  // Size of allocation unit, in bytes.
41776a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41787934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr));  // virtual address of segment start.
41797934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 0);  // offset of this piece (relative to the virtual address).
41806a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // [u4]: length of piece, in allocation units
41816a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // We won't know this until we're done, so save the offset and stuff in a dummy value.
418230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = p_;
418330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Write4BE(&p_, 0x55555555);
418430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = false;
41856a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
41866a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4187b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4188d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    if (pieceLenField_ == NULL) {
4189d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
4190d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      CHECK(needHeader_);
4191d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      return;
4192d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    }
41936a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Patch the "length of piece" field.
419430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(&buf_[0], pieceLenField_);
419530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(pieceLenField_, p_);
419630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
41976a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
419830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]);
41996a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
42006a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
42016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
420200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg)
4203b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
4204b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
420530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes);
4206a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
4207a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
42086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes private:
4209a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  enum { ALLOCATION_UNIT_SIZE = 8 };
4210a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
42116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void Reset() {
421230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    p_ = &buf_[0];
421336dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    ResetStartOfNextChunk();
421430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    totalAllocationUnits_ = 0;
421530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = true;
421630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = NULL;
42176a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
42186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
421900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes)
4220b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
4221b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
422230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
422330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // in the following code not to allocate memory, by ensuring buf_ is of the correct size
422415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (used_bytes == 0) {
422515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start == NULL) {
422615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            // Reset for start of new heap.
422715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
422815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
422915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
423015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Only process in use memory so that free region information
423115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // also includes dlmalloc book keeping.
4232a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
4233a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
42346a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
423515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    /* If we're looking at the native heap, we'll just return
423615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks
423715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     */
423815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bool native = type_ == CHUNK_TYPE("NHSG");
423915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
424036dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    // TODO: I'm not sure using start of next chunk works well with multiple spaces. We shouldn't
424136dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    // count gaps inbetween spaces as free memory.
424215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (startOfNextMemoryChunk_ != NULL) {
424315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Transmit any pending free memory. Native free memory of
424415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // over kMaxFreeLen could be because of the use of mmaps, so
424515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // don't report. If not free memory then start a new segment.
424615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        bool flush = true;
424715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start > startOfNextMemoryChunk_) {
424815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            const size_t kMaxFreeLen = 2 * kPageSize;
424915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeStart = startOfNextMemoryChunk_;
425015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeEnd = start;
42512d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom            size_t freeLen = reinterpret_cast<char*>(freeEnd) - reinterpret_cast<char*>(freeStart);
425215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            if (!native || freeLen < kMaxFreeLen) {
425315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), freeStart, freeLen);
425415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                flush = false;
425515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            }
425615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
425715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (flush) {
425815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
425915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
426015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
426115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
4262ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    mirror::Object* obj = reinterpret_cast<mirror::Object*>(start);
4263a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
4264a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Determine the type of this chunk.
4265a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
4266a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If it's the same, we should combine them.
426715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    uint8_t state = ExamineObject(obj, native);
426836dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    AppendChunk(state, start, used_bytes + chunk_overhead_);
426936dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_;
427015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  }
427115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
427215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void AppendChunk(uint8_t state, void* ptr, size_t length)
4273b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
427415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // Make sure there's enough room left in the buffer.
427515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
427615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // 17 bytes for any header.
427715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t needed = (((length/ALLOCATION_UNIT_SIZE + 255) / 256) * 2) + 17;
427815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
427915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
428015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      Flush();
428115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
4282a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
428315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
428415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
428515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
428615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers          << needed << " bytes)";
428715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return;
428815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
428915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    EnsureHeader(ptr);
4290a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Write out the chunk description.
429115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    length /= ALLOCATION_UNIT_SIZE;   // Convert to allocation units.
429215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    totalAllocationUnits_ += length;
429315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    while (length > 256) {
429430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = state | HPSG_PARTIAL;
429530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = 255;     // length - 1
429615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      length -= 256;
4297a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
429830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    *p_++ = state;
429915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    *p_++ = length - 1;
43006a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
43016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4302ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint8_t ExamineObject(mirror::Object* o, bool is_native_heap)
4303ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
4304a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (o == NULL) {
4305a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_FREE, 0);
4306a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
43076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4308a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // It's an allocated chunk. Figure out what it is.
43096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4310a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If we're looking at the native heap, we'll just return
4311a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks.
431200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (is_native_heap) {
4313a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
43146a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
43156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
43165bfa60ffcc953340feb711ed05cf576ac821905eIan Rogers    if (!Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) {
431715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
431800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
431900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
43202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = o->GetClass();
4321a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c == NULL) {
4322a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      // The object was probably just created but hasn't been initialized yet.
4323a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
4324a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
43256a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4326590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(c)) {
432715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
4328a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
4329a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
43306a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4331a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsClassClass()) {
4332a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
43336a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
43346a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4335a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsArrayClass()) {
4336a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (o->IsObjectArray()) {
4337a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
4338a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
4339a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      switch (c->GetComponentSize()) {
4340a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
4341a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
4342a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
4343a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
4344a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
43456a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
43466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4347a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
43486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
43496a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
435030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  std::vector<uint8_t> buf_;
435130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* p_;
435230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* pieceLenField_;
435315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void* startOfNextMemoryChunk_;
435430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  size_t totalAllocationUnits_;
435530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint32_t type_;
435630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool merge_;
435730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool needHeader_;
435836dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  size_t chunk_overhead_;
435930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers
4360a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
4361a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes};
43626a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
436336dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartierstatic void BumpPointerSpaceCallback(mirror::Object* obj, void* arg)
436436dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
436536dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment);
436636dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier  HeapChunkContext::HeapChunkCallback(
436736dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, arg);
436836dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier}
436936dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier
43706a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesvoid Dbg::DdmSendHeapSegments(bool native) {
43716a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhen when;
43726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhat what;
43736a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (!native) {
43746a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmHpsgWhen;
43756a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmHpsgWhat;
43766a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  } else {
43776a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmNhsgWhen;
43786a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmNhsgWhat;
43796a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
43806a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (when == HPSG_WHEN_NEVER) {
43816a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    return;
43826a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
43836a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
43846a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Figure out what kind of chunks we'll be sending.
43856a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what);
43866a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
43876a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // First, send a heap start chunk.
43886a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  uint8_t heap_id[4];
43897934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Set4BE(&heap_id[0], 1);  // Heap id (bogus; we only have one heap).
43906a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id);
43916a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4392cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Thread* self = Thread::Current();
4393cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
4394cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
4395cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
43966a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Send a series of heap segment chunks.
4397a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native);
4398a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (native) {
4399c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#ifdef USE_DLMALLOC
44001d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context);
4401c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#else
4402c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris    UNIMPLEMENTED(WARNING) << "Native heap inspection is only supported with dlmalloc";
4403c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#endif
4404a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
44051d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
440636dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    for (const auto& space : heap->GetContinuousSpaces()) {
440736dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      if (space->IsDlMallocSpace()) {
4408d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
440936dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
441036dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        // allocation then the first sizeof(size_t) may belong to it.
441136dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        context.SetChunkOverhead(sizeof(size_t));
441236dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
441336dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      } else if (space->IsRosAllocSpace()) {
441436dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        context.SetChunkOverhead(0);
4415d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since
4416d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        // RosAlloc's internal logic doesn't know to release and reacquire the heap bitmap lock.
4417d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        self->TransitionFromRunnableToSuspended(kSuspended);
4418d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        ThreadList* tl = Runtime::Current()->GetThreadList();
4419d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        tl->SuspendAll();
4420d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        {
4421d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier          ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
4422d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier          space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
4423d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        }
4424d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        tl->ResumeAll();
4425d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        self->TransitionFromSuspendedToRunnable();
442636dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      } else if (space->IsBumpPointerSpace()) {
4427d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier        ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
442836dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        context.SetChunkOverhead(0);
442936dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        space->AsBumpPointerSpace()->Walk(BumpPointerSpaceCallback, &context);
443036dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      } else {
443136dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier        UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space;
4432b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier      }
443336dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier      context.ResetStartOfNextChunk();
4434b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
4435d6527cf8e824d9057f32755f2ff4bdcf46c7095bMathieu Chartier    ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
4436e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    // Walk the large objects, these are not in the AllocSpace.
443736dab3607e77505ce139eacef1c62a1c4bc4affdMathieu Chartier    context.SetChunkOverhead(0);
4438e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
4439a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
44406a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
44416a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Finally, send a heap end chunk.
44426a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id);
4443767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
4444767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4445b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughesstatic size_t GetAllocTrackerMax() {
4446b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#ifdef HAVE_ANDROID_OS
4447b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  // Check whether there's a system property overriding the number of records.
4448b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  const char* propertyName = "dalvik.vm.allocTrackerMax";
4449b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  char allocRecordMaxString[PROPERTY_VALUE_MAX];
4450b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (property_get(propertyName, allocRecordMaxString, "") > 0) {
4451b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    char* end;
4452b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    size_t value = strtoul(allocRecordMaxString, &end, 10);
4453b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (*end != '\0') {
44543e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
44553e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- invalid";
4456b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4457b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4458b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (!IsPowerOfTwo(value)) {
44593e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
44603e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- not power of two";
4461b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4462b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4463b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    return value;
4464b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  }
4465b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#endif
4466b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  return kDefaultNumAllocRecords;
4467b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes}
4468b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes
4469f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstromvoid Dbg::SetAllocTrackingEnabled(bool enable) {
4470f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  Thread* self = Thread::Current();
4471f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  if (enable) {
4472b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4473f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      MutexLock mu(self, *Locks::alloc_tracker_lock_);
4474f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      if (recent_allocation_records_ != NULL) {
4475f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom        return;  // Already enabled, bail.
4476b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      }
4477f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      alloc_record_max_ = GetAllocTrackerMax();
4478f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      LOG(INFO) << "Enabling alloc tracker (" << alloc_record_max_ << " entries of "
4479f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom                << kMaxAllocRecordStackDepth << " frames, taking "
4480f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom                << PrettySize(sizeof(AllocRecord) * alloc_record_max_) << ")";
4481f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      DCHECK_EQ(alloc_record_head_, 0U);
4482f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      DCHECK_EQ(alloc_record_count_, 0U);
4483f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      recent_allocation_records_ = new AllocRecord[alloc_record_max_];
4484f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      CHECK(recent_allocation_records_ != NULL);
4485545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4486a98ffd745bbecb2e84a492194950c0b94966546bMathieu Chartier    Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints();
4487545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  } else {
4488b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4489f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      ScopedObjectAccess soa(self);  // For type_cache_.Clear();
4490f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      MutexLock mu(self, *Locks::alloc_tracker_lock_);
4491f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      if (recent_allocation_records_ == NULL) {
4492f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom        return;  // Already disabled, bail.
4493f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      }
44944345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier      LOG(INFO) << "Disabling alloc tracker";
4495b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      delete[] recent_allocation_records_;
4496b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      recent_allocation_records_ = NULL;
4497f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      alloc_record_head_ = 0;
4498f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom      alloc_record_count_ = 0;
44994345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier      type_cache_.Clear();
4500b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    }
4501f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    // If an allocation comes in before we uninstrument, we will safely drop it on the floor.
4502a98ffd745bbecb2e84a492194950c0b94966546bMathieu Chartier    Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
4503545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4504545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4505545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
45060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct AllocRecordStackVisitor : public StackVisitor {
45077a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor(Thread* thread, AllocRecord* record)
4508b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
45097a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, NULL), record(record), depth(0) {}
4510545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
451100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
451200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
451300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
4514545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if (depth >= kMaxAllocRecordStackDepth) {
4515530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return false;
4516545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4517ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = GetMethod();
45180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    if (!m->IsRuntimeMethod()) {
4519b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetMethod(m);
4520b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetDexPc(GetDexPc());
4521530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      ++depth;
4522545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4523530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes    return true;
4524545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4525545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4526545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  ~AllocRecordStackVisitor() {
4527545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // Clear out any unused stack trace elements.
4528545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (; depth < kMaxAllocRecordStackDepth; ++depth) {
4529b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetMethod(nullptr);
4530b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetDexPc(0);
4531545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4532545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4533545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4534545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  AllocRecord* record;
4535545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t depth;
4536545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4537545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
45382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::RecordAllocation(mirror::Class* type, size_t byte_count) {
4539545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  Thread* self = Thread::Current();
4540545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  CHECK(self != NULL);
4541545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4542f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  MutexLock mu(self, *Locks::alloc_tracker_lock_);
4543545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4544f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    // In the process of shutting down recording, bail.
4545545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4546545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4547545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4548545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Advance and clip.
4549719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (++alloc_record_head_ == alloc_record_max_) {
4550719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    alloc_record_head_ = 0;
4551545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4552545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4553545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the basics.
4554719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  AllocRecord* record = &recent_allocation_records_[alloc_record_head_];
4555b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  record->SetType(type);
4556b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  record->SetByteCount(byte_count);
4557b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  record->SetThinLockId(self->GetThreadId());
4558545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4559545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the stack trace.
45607a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor visitor(self, record);
45610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
4562545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4563719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (alloc_record_count_ < alloc_record_max_) {
4564719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    ++alloc_record_count_;
4565545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4566545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4567545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4568a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// Returns the index of the head element.
4569a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4570f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom// We point at the most-recently-written record, so if alloc_record_count_ is 1
4571a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// we want to use the current element.  Take "head+1" and subtract count
4572a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// from it.
4573a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4574a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We need to handle underflow in our circular buffer, so we add
4575f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom// alloc_record_max_ and then mask it back down.
4576719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::HeadIndex() {
4577719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  return (Dbg::alloc_record_head_ + 1 + Dbg::alloc_record_max_ - Dbg::alloc_record_count_) &
4578719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      (Dbg::alloc_record_max_ - 1);
4579545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4580545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4581545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::DumpRecentAllocations() {
458200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
4583f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  MutexLock mu(soa.Self(), *Locks::alloc_tracker_lock_);
4584545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4585545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    LOG(INFO) << "Not recording tracked allocations";
4586545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4587545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4588545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4589545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // "i" is the head of the list.  We want to start at the end of the
4590545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // list and move forward to the tail.
4591a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t i = HeadIndex();
4592f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  const uint16_t capped_count = CappedAllocRecordCount(Dbg::alloc_record_count_);
4593f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom  uint16_t count = capped_count;
4594545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4595719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  LOG(INFO) << "Tracked allocations, (head=" << alloc_record_head_ << " count=" << count << ")";
4596545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  while (count--) {
4597545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    AllocRecord* record = &recent_allocation_records_[i];
4598545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4599b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->ThinLockId(), record->ByteCount())
4600b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi              << PrettyClass(record->Type());
4601545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4602545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
4603b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      AllocRecordStackTraceElement* stack_element = record->StackElement(stack_frame);
4604b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      mirror::ArtMethod* m = stack_element->Method();
4605545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      if (m == NULL) {
4606545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes        break;
4607545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
4608b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      LOG(INFO) << "    " << PrettyMethod(m) << " line " << stack_element->LineNumber();
4609545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4610545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4611545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // pause periodically to help logcat catch up
4612545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if ((count % 5) == 0) {
4613545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      usleep(40000);
4614545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4615545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4616719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    i = (i + 1) & (alloc_record_max_ - 1);
4617545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4618545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4619545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4620545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesclass StringTable {
4621545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes public:
4622545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  StringTable() {
4623545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4624545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
46254345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  void Add(const std::string& str) {
46264345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    table_.insert(str);
46274345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  }
46284345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier
46294345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  void Add(const char* str) {
46304345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    table_.insert(str);
4631545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4632545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4633a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t IndexOf(const char* s) const {
463402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    auto it = table_.find(s);
4635a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    if (it == table_.end()) {
4636a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes      LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
4637a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    }
4638a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    return std::distance(table_.begin(), it);
4639545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4640545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4641a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t Size() const {
4642545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return table_.size();
4643545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4644545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4645a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  void WriteTo(std::vector<uint8_t>& bytes) const {
464602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const std::string& str : table_) {
464702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      const char* s = str.c_str();
46486d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      size_t s_len = CountModifiedUtf8Chars(s);
4649700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<uint16_t> s_utf16(new uint16_t[s_len]);
46506d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      ConvertModifiedUtf8ToUtf16(s_utf16.get(), s);
46516d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
4652545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4653545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4654545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4655545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes private:
4656a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  std::set<std::string> table_;
4657545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(StringTable);
4658545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4659545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4660bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartierstatic const char* GetMethodSourceFile(mirror::ArtMethod* method)
4661280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4662bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  DCHECK(method != nullptr);
4663bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const char* source_file = method->GetDeclaringClassSourceFile();
4664280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  return (source_file != nullptr) ? source_file : "";
4665280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz}
4666280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz
4667545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes/*
4668545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The data we send to DDMS contains everything we have recorded.
4669545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4670545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * Message header (all values big-endian):
4671545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) message header len (to allow future expansion); includes itself
4672545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) entry header len
4673545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) stack frame len
4674545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of entries
4675545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (4b) offset to string table from start of message
4676545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of class name strings
4677545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of method name strings
4678545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of source file name strings
4679545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * For each entry:
4680545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (4b) total allocation size
4681221229cb523f849f165fdafbf9785010963715daElliott Hughes *   (2b) thread id
4682545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (2b) allocated object's class name index
4683545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (1b) stack depth
4684545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   For each stack frame:
4685545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method's class name
4686545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method name
4687545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method source file
4688545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) line number, clipped to 32767; -2 if native; -1 if no source
4689545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) class name strings
4690545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) method name strings
4691545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) source file strings
4692545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4693545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * As with other DDM traffic, strings are sent as a 4-byte length
4694545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * followed by UTF-16 data.
4695545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4696545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We send up 16-bit unsigned indexes into string tables.  In theory there
4697f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in
4698545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * each table, but in practice there should be far fewer.
4699545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4700545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The chief reason for using a string table here is to keep the size of
4701545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * the DDMS message to a minimum.  This is partly to make the protocol
4702545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * efficient, but also because we have to form the whole thing up all at
4703545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * once in a memory buffer.
4704545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4705545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We use separate string tables for class names, method names, and source
4706545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * files to keep the indexes small.  There will generally be no overlap
4707545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * between the contents of these tables.
4708545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes */
4709545a064aca775ba801790fced3d713d8a87bfc61Elliott HughesjbyteArray Dbg::GetRecentAllocations() {
4710545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (false) {
4711545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    DumpRecentAllocations();
4712545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4713545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
471450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* self = Thread::Current();
471546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  std::vector<uint8_t> bytes;
471646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  {
4717f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    MutexLock mu(self, *Locks::alloc_tracker_lock_);
471846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
471946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 1: generate string tables.
472046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
472146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable class_names;
472246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable method_names;
472346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable filenames;
472446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4725f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    const uint16_t capped_count = CappedAllocRecordCount(Dbg::alloc_record_count_);
4726f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    uint16_t count = capped_count;
4727f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    size_t idx = HeadIndex();
472846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
472946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
4730cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      std::string temp;
4731cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers      class_names.Add(record->Type()->GetDescriptor(&temp));
473246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
4733b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        mirror::ArtMethod* m = record->StackElement(i)->Method();
473446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        if (m != NULL) {
4735bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          class_names.Add(m->GetDeclaringClassDescriptor());
4736bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          method_names.Add(m->GetName());
4737bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          filenames.Add(GetMethodSourceFile(m));
473846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        }
473946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      }
4740545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4741719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
474246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    }
474346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4744f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    LOG(INFO) << "allocation records: " << capped_count;
474546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
474646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
474746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 2: Generate the output and store it in the buffer.
474846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
474946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
475046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) message header len (to allow future expansion); includes itself
475146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) entry header len
475246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) stack frame len
475346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kMessageHeaderLen = 15;
475446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kEntryHeaderLen = 9;
475546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kStackFrameLen = 8;
475646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kMessageHeaderLen);
475746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kEntryHeaderLen);
475846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kStackFrameLen);
475946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
476046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of entries
476146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (4b) offset to string table from start of message
476246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of class name strings
476346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of method name strings
476446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of source file name strings
4765f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    JDWP::Append2BE(bytes, capped_count);
476646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    size_t string_table_offset = bytes.size();
47677934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Append4BE(bytes, 0);  // We'll patch this later...
476846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, class_names.Size());
476946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, method_names.Size());
477046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, filenames.Size());
477146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
477246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    idx = HeadIndex();
4773cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers    std::string temp;
4774f4cb036808b88fe60d71a705b2744284155cbc01Brian Carlstrom    for (count = capped_count; count != 0; --count) {
477546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // For each entry:
477646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (4b) total allocation size
477746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) thread id
477846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) allocated object's class name index
477946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (1b) stack depth
478046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
478146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t stack_depth = record->GetDepth();
4782f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier      size_t allocated_object_class_name_index =
4783cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers          class_names.IndexOf(record->Type()->GetDescriptor(&temp));
4784b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      JDWP::Append4BE(bytes, record->ByteCount());
4785b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      JDWP::Append2BE(bytes, record->ThinLockId());
478646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, allocated_object_class_name_index);
478746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append1BE(bytes, stack_depth);
478846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
478946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
479046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // For each stack frame:
479146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method's class name
479246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method name
479346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method source file
479446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) line number, clipped to 32767; -2 if native; -1 if no source
4795b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        mirror::ArtMethod* m = record->StackElement(stack_frame)->Method();
4796bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor());
4797bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        size_t method_name_index = method_names.IndexOf(m->GetName());
4798bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m));
479946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, class_name_index);
480046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, method_name_index);
480146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, file_name_index);
4802b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        JDWP::Append2BE(bytes, record->StackElement(stack_frame)->LineNumber());
4803545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
4804719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
4805545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4806545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
480746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) class name strings
480846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) method name strings
480946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) source file strings
481046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
481146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    class_names.WriteTo(bytes);
481246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    method_names.WriteTo(bytes);
481346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    filenames.WriteTo(bytes);
4814545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
481550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  JNIEnv* env = self->GetJniEnv();
4816545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  jbyteArray result = env->NewByteArray(bytes.size());
4817545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (result != NULL) {
4818545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
4819545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4820545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  return result;
4821545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4822545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
48230ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchimirror::ArtMethod* DeoptimizationRequest::Method() const {
48240ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  ScopedObjectAccessUnchecked soa(Thread::Current());
48250ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  return soa.DecodeMethod(method_);
48260ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi}
48270ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
48280ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchivoid DeoptimizationRequest::SetMethod(mirror::ArtMethod* m) {
48290ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  ScopedObjectAccessUnchecked soa(Thread::Current());
48300ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  method_ = soa.EncodeMethod(m);
48310ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi}
48320ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
4833872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}  // namespace art
4834